Programmer's Reference Guide

Save changes to the PDF document.

Document pages.

Page creation.

PDF document page abstraction is represented by Zend_Pdf_Page class.

PDF pages either are loaded from existing PDF, or created.

New page can be obtained by creating new Zend_Pdf_Page object or calling Zend_Pdf::newPage() method, which returns Zend_Pdf_Page object. The difference is that Zend_Pdf::newPage() method creates a page, already attached to the document. In difference from unattached pages it can't be used with several PDF documents, but has a little bit better performance. [1] . It's your choice, which approach should be used.

Zend_Pdf::newPage() method and Zend_Pdf_Page constructors take the same set of parameters specifying page size. It either the size of page ($x, $y) in a points (1/72 inch), or predefined constant, which is treated as a page type:

  • Zend_Pdf_Page::SIZE_A4

  • Zend_Pdf_Page::SIZE_A4_LANDSCAPE

  • Zend_Pdf_Page::SIZE_LETTER

  • Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE

Document pages are stored in $pages public member of Zend_Pdf class. It's an array of Zend_Pdf_Page objects. It completely defines set and order of document pages and can be manipulated as a common array:

Example #1 PDF document pages management.

<?php
    ...
    // Reverse page order
    $pdf->pages = array_reverse($pdf->pages);
    ...
    // Add new page
    $pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    // Add new page
    $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);

    // Remove specified page.
    unset($pdf->pages[$id]);

    ...
            

Page cloning.

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

Example #2 Cloning existing page.

<?php
    ...
    // Store template page in a separate variable
    $template = $pdf->pages[$templatePageIndex];
    ...
    // Add new page
    $page1 = new Zend_Pdf_Page($template);
    $pdf->pages[] = $page1;
    ...

    // Add another page
    $page2 = new Zend_Pdf_Page($template);
    $pdf->pages[] = $page2;
    ...

    // Remove source template page from the documents.
    unset($pdf->pages[$templatePageIndex]);

    ...
            

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

Caution

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

[1] It's a limitation of V1.0 version of Zend_Pdf module. It will be eliminated in future versions. But unattached pages will always give better (more optimal) result for sharing pages between documents.

Save changes to the PDF document.
blog comments powered by Disqus

Select a Version

Languages Available

Components

Search the Manual