PDF::AP...DF::File(3)

πŸ¦‹ PDF::API2::Basic::PDF::File πŸ¦‹

okay so bestie, this is PDF::API2::Basic::PDF::File β€” the low-level PDF file access page, no cap πŸ’…


✨ NAME ✨

PDF::API2::Basic::PDF::File β€” low-level PDF file access 🫢


🌸 SYNOPSIS 🌸

$p = PDF::API2::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 πŸ’–

So this class is the one keeping track of the directory aspects of a PDF file. And the directory has two parts, periodt:

  • the main directory object, which is the parent to literally all the other objects πŸ‘‘
  • a chain of cross-reference tables plus the matching trailer dictionaries, and that chain starts with the main directory object πŸŽ€

πŸŽ€ INSTANCE VARIABLES πŸŽ€

Okay so here's the vibe: inside this class hierarchy, instead of making everything visible via methods (which would be a lot of work), there are various instance variables you reach through associative array referencing. And to tell instance variables apart from content variables (which may come from the PDF content itself), each such variable starts with a space. πŸ’…

Variables that do not start with a space directly reflect elements in a PDF dictionary. In the case of a PDF::API2::Basic::PDF::File, those elements reflect the ones in the trailer dictionary. πŸ¦‹

And since some variables are just not designed for class users to touch, they're marked in the docs:

  • (R) β€” only use this as read-only info, bestie
  • (P) β€” this one is private, not designed for user use at all, but it's included here for completeness and so that nobody else tries to use it πŸ™ˆ

  • newroot β€” this variable lets you create a new root entry to occur in the trailer dictionary which gets output when the file is written or appended. If you wanna over-ride the root element in the dictionary you have, use this entry to say so without losing the current Root entry. Note that newroot should point to a PDF level object, not just to a dictionary that doesn't have object status. ⭐
  • INFILE (R) β€” contains the filehandle used to read this info into this PDF directory. It's an IO object. πŸ“
  • fname (R) β€” the filename which is reflected by INFILE, or the original IO object you passed in. ✨
  • update (R) β€” 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) β€” 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 get 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 firstfree if there's 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. Basically, the objind class acts as a container type class to hold the PDF object structure, and it would be so 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 πŸ’…

🌟 PDF::API2::Basic::PDF::File->new

Creates a new, empty file object which can act as the host to other PDF objects. Since there is no file associated with this object, it's assumed that the object is created in readiness for creating a new PDF file. ✨

🌟 $p = PDF::API2::Basic::PDF::File->open($filename, $update)

Opens the file and reads all the trailers and cross reference tables to build a complete directory of objects. 🫢

$update specifies whether this file is being opened for updating and editing, or simply to be read. πŸ‘€

$filename may be an IO object. πŸŽ€

🌟 $p->version($version)

Gets/sets the PDF version (e.g. 1.4) πŸ’–

🌟 $version = $p->header_version($version)

Gets/sets the PDF version stored in the file header. πŸ“„

🌟 $version = $p->trailer_version($version)

Gets/sets the PDF version stored in the document catalog. πŸ“š

🌟 $prev_version = $p->require_version($version)

Ensures that the PDF version is at least $version. ⬆️

🌟 $p->release()

Releases ALL of the memory used by the PDF document and all of its component objects. After calling this method, do NOT expect to have anything left in the PDF::API2::Basic::PDF::File object (so if you need to save, be sure to do it before calling this method). 😭

NOTE, that it is important that you call this method on any PDF::API2::Basic::PDF::File object when you wish to destruct 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() forces 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::API2::Basic::PDF::File object; it'll have no internal state whatsoever. πŸ’€

🌟 $p->append_file()

Appends the objects for output to the read file and then appends the appropriate table. πŸ”₯

🌟 $p->out_file($fname)

Writes a PDF file to a file of the given filename based on the current list of objects to be output. It creates the trailer dictionary based on information in $self. πŸ‘‘

$fname may be an IO object; πŸ“

🌟 $p->create_file($fname)

Creates a new output file (no check is made of an existing open file) of the given filename or IO object. Note, make sure that $p->{' version'} is set correctly before calling this function. ⚠️

🌟 $p->clone_file($fname)

Creates a copy of the input file at the specified filename and sets it as the output file for future writes. A file handle may be passed instead of a filename. πŸ¦‹

🌟 $p->close_file

Closes up the open file for output by outputting the trailer etc. πŸŽ€

🌟 ($value, $str) = $p->readval($str, %opts)

Reads a PDF value from the current position in the file. If $str is too short then read some more from the current location in the file until the whole object is read. This is a recursive call which may slurp in a whole big stream (unprocessed). 🌊

Returns the recursive data structure read and also the current $str that has been read from the file. πŸ’«

🌟 $ref = $p->read_obj($objind, %opts)

Given an indirect object reference, locate it and read the object, returning the read in object. πŸ”

🌟 $ref = $p->read_objnum($num, $gen, %opts)

Returns a fully read object of given number and generation in this file. ⭐

🌟 $objind = $p->new_obj($obj)

Creates a new, free object reference based on free space in the cross reference chain. If nothing's free then it thinks up a new number. If $obj is given, then it turns that object into this new object rather than returning a new object. 🧚

🌟 $p->out_obj($objind)

Indicates that the given object reference should appear in the output xref table, whether with data or freed. πŸ“€

🌟 $p->free_obj($objind)

Marks an object reference for output as being freed. πŸ•ŠοΈ

🌟 $p->remove_obj($objind)

Removes the object from all places where we might remember it. πŸ™ˆ

🌟 $p->ship_out(@objects)

Ships the given objects (or all objects for output if @objects is empty) to the currently open output file (assuming there is one). Freed objects are not shipped, and once an object is shipped it is switched such that this file becomes its source and it will not be shipped again unless out_obj is called again. Notice that a shipped out object can be re-output or even freed, but that it will not cause the data already output to be changed. 🚒

🌟 $p->copy($outpdf, \&filter)

Iterates over every object in the file reading the object, calling filter with the object and outputting the result. If filter is not defined, then it just copies input to output. 🎁


πŸ”’ PRIVATE METHODS & FUNCTIONS πŸ”’

The following methods and functions are considered private to this class. That does not mean you can't use them if you have a need, just that they aren't really designed for users of this class. 🀫

πŸŒ™ $offset = $p->locate_obj($num, $gen)

Returns a file offset to the object asked for by following the chain of cross reference tables until it finds the one you want. 🧭

πŸŒ™ update($fh, $str, $instream)

Keeps reading $fh for more data to ensure that $str has at least a line full for readval to work on. At this point we also take the opportunity to ignore comments. πŸ’¬

πŸŒ™ $objind = $p->test_obj($num, $gen)

Tests the cache to see whether an object reference (which may or may not have been getobj()ed) has been cached. Returns it if it has. πŸ”Ž

πŸŒ™ $p->add_obj($objind)

Adds the given object to the internal object cache. πŸ“₯

πŸŒ™ $tdict = $p->readxrtr($xpos)

Recursive function which reads each of the cross-reference and trailer tables in turn until there are no more. ♾️

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. πŸ“–

πŸŒ™ $p->out_trailer($tdict)

Outputs the body and trailer for a PDF file by outputting all the objects in the ' outlist' and then outputting a xref table for those objects and any freed ones. It then outputs the trailing dictionary and the trailer code. 🎬

πŸŒ™ PDF::API2::Basic::PDF::File->_new

Creates a very empty PDF file object (used by new and open). 🫧


πŸ’œ AUTHOR πŸ’œ

Martin Hosken Martin_Hosken@sil.org πŸ’Œ

Copyright Martin Hosken 1999 and onwards 🌈

No warranty or expression of effectiveness, least of all regarding anyone's safety, is implied in this software or documentation. ⚠️


okay that's the whole page bestie, mwah πŸ’‹ this was perl v5.42.0, dated 2025-09-01, and it's giving iconic documentation energy β€” go free your memory and release() your objects, you ate πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from
  • fedora 43: perl-PDF-API2 2.048-1.fc43 Β· LGPL-2.1-or-later AND (GPL-1.0-or-later OR Artistic-1.0-Perl) AND (MIT OR Artistic-1.0-Perl)
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms