1Text::PDF::File(3) User Contributed Perl Documentation Text::PDF::File(3)
2
3
4
6 Text::PDF::File - Holds the trailers and cross-reference tables for a
7 PDF file
8
10 $p = Text::PDF::File->open("filename.pdf", 1);
11 $p->new_obj($obj_ref);
12 $p->free_obj($obj_ref);
13 $p->append_file;
14 $p->close_file;
15 $p->release; # IMPORTANT!
16
18 This class keeps track of the directory aspects of a PDF file. There
19 are two parts to the directory: the main directory object which is the
20 parent to all other objects and a chain of cross-reference tables and
21 corresponding trailer dictionaries starting with the main directory
22 object.
23
25 Within this class hierarchy, rather than making everything visible via
26 methods, which would be a lot of work, there are various instance
27 variables which are accessible via associative array referencing. To
28 distinguish instance variables from content variables (which may come
29 from the PDF content itself), each such variable will start with a
30 space.
31
32 Variables which do not start with a space directly reflect elements in
33 a PDF dictionary. In the case of a Text::PDF::File, the elements
34 reflect those in the trailer dictionary.
35
36 Since some variables are not designed for class users to access,
37 variables are marked in the documentation with (R) to indicate that
38 such an entry should only be used as read-only information. (P)
39 indicates that the information is private and not designed for user use
40 at all, but is included in the documentation for completeness and to
41 ensure that nobody else tries to use it.
42
43 newroot
44 This variable allows the user to create a new root entry to occur
45 in the trailer dictionary which is output when the file is written
46 or appended. If you wish to over-ride the root element in the
47 dictionary you have, use this entry to indicate that without losing
48 the current Root entry. Notice that newroot should point to a PDF
49 level object and not just to a dictionary which does not have
50 object status.
51
52 INFILE (R)
53 Contains the filehandle used to read this information into this PDF
54 directory. Is an IO object.
55
56 fname (R)
57 This is the filename which is reflected by INFILE, or the original
58 IO object passed in.
59
60 update (R)
61 This indicates that the read file has been opened for update and
62 that at some point, $p->appendfile() can be called to update the
63 file with the changes that have been made to the memory
64 representation.
65
66 maxobj (R)
67 Contains the first useable object number above any that have
68 already appeared in the file so far.
69
70 outlist (P)
71 This is a list of Objind which are to be output when the next
72 appendfile or outfile occurs.
73
74 firstfree (P)
75 Contains the first free object in the free object list. Free
76 objects are removed from the front of the list and added to the
77 end.
78
79 lastfree (P)
80 Contains the last free object in the free list. It may be the same
81 as the firstfree if there is only one free object.
82
83 objcache (P)
84 All objects are held in the cache to ensure that a system only has
85 one occurrence of each object. In effect, the objind class acts as
86 a container type class to hold the PDF object structure and it
87 would be unfortunate if there were two identical place-holders
88 floating around a system.
89
90 epos (P)
91 The end location of the read-file.
92
93 Each trailer dictionary contains a number of private instance variables
94 which hold the chain together.
95
96 loc (P)
97 Contains the location of the start of the cross-reference table
98 preceding the trailer.
99
100 xref (P)
101 Contains an anonymous array of each cross-reference table entry.
102
103 prev (P)
104 A reference to the previous table. Note this differs from the Prev
105 entry which is in PDF which contains the location of the previous
106 cross-reference table.
107
109 Text::PDF::File->new
110 Creates a new, empty file object which can act as the host to other PDF
111 objects. Since there is no file associated with this object, it is
112 assumed that the object is created in readiness for creating a new PDF
113 file.
114
115 $p = Text::PDF::File->open($filename, $update)
116 Opens the file and reads all the trailers and cross reference tables to
117 build a complete directory of objects.
118
119 $update specifies whether this file is being opened for updating and
120 editing, or simply to be read.
121
122 $filename may be an IO object
123
124 $p->release()
125 Releases ALL of the memory used by the PDF document and all of its
126 component objects. After calling this method, do NOT expect to have
127 anything left in the "Text::PDF::File" object (so if you need to save,
128 be sure to do it before calling this method).
129
130 NOTE, that it is important that you call this method on any
131 "Text::PDF::File" object when you wish to destruct it and free up its
132 memory. Internally, PDF files have an enormous number of cross-
133 references and this causes circular references within the internal data
134 structures. Calling 'release()' forces a brute-force cleanup of the
135 data structures, freeing up all of the memory. Once you've called this
136 method, though, don't expect to be able to do anything else with the
137 "Text::PDF::File" object; it'll have no internal state whatsoever.
138
139 Developer note: As part of the brute-force cleanup done here, this
140 method will throw a warning message whenever unexpected key values are
141 found within the "Text::PDF::File" object. This is done to help ensure
142 that any unexpected and unfreed values are brought to your attention so
143 that you can bug us to keep the module updated properly; otherwise the
144 potential for memory leaks due to dangling circular references will
145 exist.
146
147 $p->append_file()
148 Appends the objects for output to the read file and then appends the
149 appropriate tale.
150
151 $p->out_file($fname)
152 Writes a PDF file to a file of the given filename based on the current
153 list of objects to be output. It creates the trailer dictionary based
154 on information in $self.
155
156 $fname may be an IO object;
157
158 $p->create_file($fname)
159 Creates a new output file (no check is made of an existing open file)
160 of the given filename or IO object. Note, make sure that $p->{'
161 version'} is set correctly before calling this function.
162
163 $p->close_file
164 Closes up the open file for output by outputting the trailer etc.
165
166 ($value, $str) = $p->readval($str, %opts)
167 Reads a PDF value from the current position in the file. If $str is too
168 short then read some more from the current location in the file until
169 the whole object is read. This is a recursive call which may slurp in a
170 whole big stream (unprocessed).
171
172 Returns the recursive data structure read and also the current $str
173 that has been read from the file.
174
175 $ref = $p->read_obj($objind, %opts)
176 Given an indirect object reference, locate it and read the object
177 returning the read in object.
178
179 $ref = $p->read_objnum($num, $gen, %opts)
180 Returns a fully read object of given number and generation in this file
181
182 $objind = $p->new_obj($obj)
183 Creates a new, free object reference based on free space in the cross
184 reference chain. If nothing free then thinks up a new number. If $obj
185 then turns that object into this new object rather than returning a new
186 object.
187
188 $p->out_obj($objind)
189 Indicates that the given object reference should appear in the output
190 xref table whether with data or freed.
191
192 $p->free_obj($objind)
193 Marks an object reference for output as being freed.
194
195 $p->remove_obj($objind)
196 Removes the object from all places where we might remember it
197
198 $p->ship_out(@objects)
199 Ships the given objects (or all objects for output if @objects is
200 empty) to the currently open output file (assuming there is one). Freed
201 objects are not shipped, and once an object is shipped it is switched
202 such that this file becomes its source and it will not be shipped again
203 unless out_obj is called again. Notice that a shipped out object can be
204 re-output or even freed, but that it will not cause the data already
205 output to be changed.
206
207 $p->copy($outpdf, \&filter)
208 Iterates over every object in the file reading the object, calling
209 filter with the object and outputting the result. if filter is not
210 defined, then just copies input to output.
211
213 The following methods and functions are considered private to this
214 class. This does not mean you cannot use them if you have a need, just
215 that they aren't really designed for users of this class.
216
217 $offset = $p->locate_obj($num, $gen)
218 Returns a file offset to the object asked for by following the chain of
219 cross reference tables until it finds the one you want.
220
221 update($fh, $str)
222 Keeps reading $fh for more data to ensure that $str has at least a line
223 full for "readval" to work on. At this point we also take the
224 opportunity to ignore comments.
225
226 $objind = $p->test_obj($num, $gen)
227 Tests the cache to see whether an object reference (which may or may
228 not have been getobj()ed) has been cached. Returns it if it has.
229
230 $p->add_obj($objind)
231 Adds the given object to the internal object cache.
232
233 $tdict = $p->readxrtr($xpos)
234 Recursive function which reads each of the cross-reference and trailer
235 tables in turn until there are no more.
236
237 Returns a dictionary corresponding to the trailer chain. Each trailer
238 also includes the corresponding cross-reference table.
239
240 The structure of the xref private element in a trailer dictionary is of
241 an anonymous hash of cross reference elements by object number. Each
242 element consists of an array of 3 elements corresponding to the three
243 elements read in [location, generation number, free or used]. See the
244 PDF Specification for details.
245
246 $p->out_trailer($tdict)
247 Outputs the body and trailer for a PDF file by outputting all the
248 objects in the ' outlist' and then outputting a xref table for those
249 objects and any freed ones. It then outputs the trailing dictionary and
250 the trailer code.
251
252 Text::PDF::File->_new
253 Creates a very empty PDF file object (used by new and open)
254
256 Martin Hosken Martin_Hosken@sil.org
257
258 Copyright Martin Hosken 1999 and onwards
259
260 No warranty or expression of effectiveness, least of all regarding
261 anyone's safety, is implied in this software or documentation.
262
263 Licensing
264 This Perl Text::PDF module is licensed under the Perl Artistic License.
265
266
267
268perl v5.36.0 2023-01-20 Text::PDF::File(3)