Creating and Loading PDF Documents

The Zend_Pdf class represents PDF documents and provides document-level operations.

To create a new document, a new Zend_Pdf object should first be created.

Zend_Pdf class also provides two static methods to load an existing PDF document. These are the Zend_Pdf::load() and Zend_Pdf::parse() methods. Both of them return Zend_Pdf objects as a result or throw an exception if an error occurs.

Example #1 Create new or load existing PDF document

  1. ...
  2. // Create a new PDF document
  3. $pdf1 = new Zend_Pdf();
  4.  
  5. // Load a PDF document from a file
  6. $pdf2 = Zend_Pdf::load($fileName);
  7.  
  8. // Load a PDF document from a string
  9. $pdf3 = Zend_Pdf::parse($pdfString);
  10. ...

The PDF file format supports incremental document update. Thus each time a document is updated, then a new revision of the document is created. Zend_Pdf component supports the retrieval of a specified revision.

A revision can be specified as a second parameter to the Zend_Pdf::load() and Zend_Pdf::parse() methods or requested by calling the Zend_Pdf::rollback() method. [1] Zend_Pdf::rollback() call.

Example #2 Requesting Specific Revisions of a PDF Document

  1. ...
  2. // Load the previous revision of the PDF document
  3. $pdf1 = Zend_Pdf::load($fileName, 1);
  4.  
  5. // Load the previous revision of the PDF document
  6. $pdf2 = Zend_Pdf::parse($pdfString, 1);
  7.  
  8. // Load the first revision of the PDF document
  9. $pdf3 = Zend_Pdf::load($fileName);
  10. $revisions = $pdf3->revisions();
  11. $pdf3->rollback($revisions - 1);
  12. ...
[1] method must be invoked before any changes are applied to the document, otherwise the behavior is not defined.
blog comments powered by Disqus