table of contents
PDF::Builder::Basic::PDF::File(3) | User Contributed Perl Documentation | PDF::Builder::Basic::PDF::File(3) |
NAME¶
PDF::Builder::Basic::PDF::File - Holds the trailers and cross-reference tables for a PDF file
SYNOPSIS¶
$p = PDF::Builder::Basic::PDF::File->open("filename.pdf", 1); $p->new_obj($obj_ref); $p->free_obj($obj_ref); $p->append_file(); $p->close_file(); $p->release(); # IMPORTANT!
DESCRIPTION¶
This class keeps track of the directory aspects of a PDF file. There are two parts to the directory: the main directory object, which is the parent to all other objects, and a chain of cross-reference tables and corresponding trailer dictionaries, starting with the main directory object.
INSTANCE VARIABLES¶
Within this class hierarchy, rather than making everything visible via methods, which would be a lot of work, there are various instance variables which are accessible via associative array referencing. To distinguish instance variables from content variables (which may come from the PDF content itself), each such variable name will start with a space.
Variable names which do not start with a space directly reflect elements in a PDF dictionary. In the case of a "PDF::Builder::Basic::PDF::File", the elements reflect those in the trailer dictionary.
Since some variables are not designed for class users to access, variables are marked in the documentation with (R) to indicate that such an entry should only be used as read-only information. (P) indicates that the information is private, and not designed for user use at all, but is included in the documentation for completeness and to ensure that nobody else tries to use it.
- newroot
- This variable allows the user to create a new root entry to occur in the trailer dictionary which is output when the file is written or appended. If you wish to override the root element in the dictionary you have, use this entry to indicate that without losing the current Root entry. Notice that newroot should point to a PDF level object and not just to a dictionary, which does not have object status.
- INFILE (R)
- Contains the filehandle used to read this information into this PDF directory. It is an IO object.
- fname (R)
- This is the filename which is reflected by INFILE, or the original IO object passed in.
- update (R)
- This indicates that the read file has been opened for update and that at some point, "$p->appendfile()" can be called to update the file with the changes that have been made to the memory representation.
- maxobj (R)
- Contains the first usable object number above any that have already appeared in the file so far.
- outlist (P)
- This is a list of Objind which are to be output when the next appendfile() or outfile() occurs.
- firstfree (P)
- Contains the first free object in the free object list. Free objects are removed from the front of the list and added to the end.
- lastfree (P)
- Contains the last free object in the free list. It may be the same as the "firstfree" if there is only one free object.
- objcache (P)
- All objects are held in the cache to ensure that a system only has one occurrence of each object. In effect, the objind class acts as a container type class to hold the PDF object structure, and it would be unfortunate if there were two identical place-holders floating around a system.
- epos (P)
- The end location of the read-file.
Each trailer dictionary contains a number of private instance variables which hold the chain together.
- loc (P)
- Contains the location of the start of the cross-reference table preceding the trailer.
- xref (P)
- Contains an anonymous array of each cross-reference table entry.
- prev (P)
- A reference to the previous table. Note this differs from the Prev entry which is in PDF, which contains the location of the previous cross-reference table.
METHODS¶
new¶
PDF::Builder::Basic::PDF::File->new()
open¶
$p = PDF::Builder::Basic::PDF::File->open($filename, $update, %options)
$filename may be a string or an IO object.
$update specifies whether this file is being opened for updating and editing (TRUE value), or simply to be read (FALSE or undefined value).
%options may include
- diags => 1
- If "diags" is set to 1, various warning
messages will be given if a suspicious PDF structure is found, and some
fixup may be attempted. There is no guarantee that any fixup will change
the PDF to legitimate, or that there won't be other problems found further
down the line. If this flag is not given, and a structural problem
is found, it is fairly likely that errors (and even a program
crash) may happen further along. If you experience crashes when
reading in a PDF file, try running with
"diags" and see what is reported.
There are many PDF files out "in the wild" which, while failing to conform to Adobe's standards, appear to be tolerated by PDF Readers. Thus, Builder will no longer fail on them, but merely comment on their existence.
version¶
$new_version = $p->version($version, %opts) # Set $ver = $p->version() # Get
For compatibility with earlier releases, if no decimal point is given, assume "1." precedes the number given.
A warning message is given if you attempt to decrease the PDF version, as you might have already read in a higher level file, or used a higher level feature. This message is suppressed if the 'silent' option is given with any value.
header_version¶
$new_version = $p->header_version($version, %opts) # Set $version = $p->header_version() # Get
For compatibility with earlier releases, if no decimal point is given, assume "1." precedes the number given.
A warning message is given if you attempt to decrease the PDF version, as you might have already read in a higher level file, or used a higher level feature. This message is suppressed if the 'silent' option is given with any value.
trailer_version¶
$new_version = $p->trailer_version($version, %opts) # Set $version = $p->trailer_version() # Get
Note that the minimum PDF level for a trailer version is 1.4. It is not permitted to set a PDF level of 1.3 or lower. An existing PDF (read in) of 1.3 or below returns undefined.
For compatibility with earlier releases, if no decimal point is given, assume "1." precedes the number given.
A warning message is given if you attempt to decrease the PDF version, as you might have already read in a higher level file, or used a higher level feature. This message is suppressed if the 'silent' option is given with any value.
require_version¶
$prev_version = $p->require_version($version)
release¶
$p->release()
NOTE, that it is important that you call this method on any "PDF::Builder::Basic::PDF::File" object when you wish to destroy it and free up its memory. Internally, PDF files have an enormous number of cross-references, and this causes circular references within the internal data structures. Calling release() causes a brute-force cleanup of the data structures, freeing up all of the memory. Once you've called this method, though, don't expect to be able to do anything else with the "PDF::Builder::Basic::PDF::File" object; it'll have no internal state whatsoever.
append_file¶
$p->append_file()
out_file¶
$p->out_file($fname)
$fname may be a string or an IO object.
create_file¶
$p->create_file($fname)
close_file¶
$p->close_file()
readval¶
($value, $str) = $p->readval($str, %opts)
Returns the recursive data structure read and also the current $str that has been read from the file.
read_obj¶
$ref = $p->read_obj($objind, %opts)
read_objnum¶
$ref = $p->read_objnum($num, $gen, %opts)
new_obj¶
$objind = $p->new_obj($obj)
out_obj¶
$p->out_obj($obj)
free_obj¶
$p->free_obj($obj)
remove_obj¶
$p->remove_obj($objind)
ship_out¶
$p->ship_out(@objects) $p->ship_out()
copy¶
$p->copy($outpdf, \&filter)
PRIVATE METHODS & FUNCTIONS¶
The following methods and functions are considered private to this class. This does not mean you cannot use them if you have a need, just that they aren't really designed for users of this class.
locate_obj¶
$offset = $p->locate_obj($num, $gen)
update¶
update($fh, $str, $instream)
test_obj¶
$objind = $p->test_obj($num, $gen)
add_obj¶
$p->add_obj($objind)
readxrtr¶
$tdict = $p->readxrtr($xpos, %options)
Returns a dictionary corresponding to the trailer chain. Each trailer also includes the corresponding cross-reference table.
The structure of the xref private element in a trailer dictionary is of an anonymous hash of cross reference elements by object number. Each element consists of an array of 3 elements corresponding to the three elements read in [location, generation number, free or used]. See the PDF specification for details.
See "open" for options allowed.
out_trailer¶
$p->out_trailer($tdict, $update) $p->out_trailer($tdict)
_new¶
PDF::Builder::Basic::PDF::File->_new()
AUTHOR¶
Martin Hosken Martin_Hosken@sil.org
Copyright Martin Hosken 1999
No warranty or expression of effectiveness, least of all regarding anyone's safety, is implied in this software or documentation.
2024-03-08 | perl v5.40.0 |