Working with Pages - Zend_Pdf

Working with Pages

Page Creation

The pages in a PDF document are represented as Zend_Pdf_Page instances in Zend_Pdf.

PDF pages either are loaded from an existing PDF or created using the Zend_Pdf API.

New pages can be created by instantiating new Zend_Pdf_Page objects directly or by calling the Zend_Pdf::newPage() method, which returns a Zend_Pdf_Page object. Zend_Pdf::newPage() creates a page that is already attached to a document. Attached pages can't be used with another PDF documents until it's not cloned. See Page cloning section for the details.

The Zend_Pdf::newPage() method and the Zend_Pdf_Page constructor take the same parameters specifying page size. They can take either the size of page ($x, $y) in points (1/72 inch) or a predefined constant representing a page type:

  • Zend_Pdf_Page::SIZE_A4

  • Zend_Pdf_Page::SIZE_A4_LANDSCAPE

  • Zend_Pdf_Page::SIZE_LETTER


Document pages are stored in the $pages public attribute of the Zend_Pdf class. The attribute holds an array of Zend_Pdf_Page objects and completely defines the instances and order of pages. This array can be manipulated like any other PHP array:

Example #1 PDF document pages management

  1. ...
  2. // Reverse page order
  3. $pdf->pages = array_reverse($pdf->pages);
  4. ...
  5. // Add new page
  6. $pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  7. // Add new page
  8. $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  10. // Remove specified page.
  11. unset($pdf->pages[$id]);
  13. ...

Page cloning

Existing PDF page can be duplicated by creating new Zend_Pdf_Page object with existing page as a parameter:

Example #2 Duplicating existing page

  1. ...
  2. // Store template page in a separate variable
  3. $template = $pdf->pages[$templatePageIndex];
  4. ...
  5. // Add new page
  6. $page1 = new Zend_Pdf_Page($template);
  7. $page1->drawText('Some text...', $x, $y);
  8. $pdf->pages[] = $page1;
  9. ...
  11. // Add another page
  12. $page2 = new Zend_Pdf_Page($template);
  13. $page2->drawText('Another text...', $x, $y);
  14. $pdf->pages[] = $page2;
  15. ...
  17. // Remove source template page from the documents.
  18. unset($pdf->pages[$templatePageIndex]);
  20. ...

It's useful if you need several pages to be created using one template.


Important! Duplicated page shares some PDF resources with a template page, so it can be used only within the same document as a template page. Modified document can be saved as new one.

clone operator may be used to create page which is not attached to any document. It takes more time than duplicating page since it needs to copy all dependent objects (used fonts, images and other resources), but it allows to use pages from different source documents to create new one:

Example #3 Cloning existing page

  1. $page1 = clone $pdf1->pages[$templatePageIndex1];
  2. $page2 = clone $pdf2->pages[$templatePageIndex2];
  3. $page1->drawText('Some text...', $x, $y);
  4. $page2->drawText('Another text...', $x, $y);
  5. ...
  6. $pdf = new Zend_Pdf();
  7. $pdf->pages[] = $page1;
  8. $pdf->pages[] = $page2;

If several template pages are planned to be used as templates then it could be more efficient to utilize Zend_Pdf_Resource_Extractor class which gives an ability to share resources between cloned pages - fonts, images, etc. (otherwise new resource copy will be created for each cloned page):

Example #4 Cloning existing page using Zend_Pdf_Resource_Extractor class

  1. $extractor = new Zend_Pdf_Resource_Extractor();
  2. ....
  3. $page1 = $extractor->clonePage($pdf->pages[$templatePageIndex1]);
  4. $page2 = $extractor->clonePage($pdf->pages[$templatePageIndex2]);
  5. $page1->drawText('Some text...', $x, $y);
  6. $page2->drawText('Another text...', $x, $y);
  7. ...
  8. $pdf = new Zend_Pdf();
  9. $pdf->pages[] = $page1;
  10. $pdf->pages[] = $page2;


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.