Introduction to Zend Framework

 Learning Zend Framework

appendix

 Zend Framework Reference


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    54.4. Working with Pages

    54.4.1. 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

    • Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE

    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 54.4. PDF document pages management

    ...
    // 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]);

    ...

    54.4.2. Page cloning

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

    Example 54.5. Duplicating existing page

    ...
    // Store template page in a separate variable
    $template $pdf->pages[$templatePageIndex];
    ...
    // Add new page
    $page1 = new Zend_Pdf_Page($template);
    $page1->drawText('Some text...'$x$y);
    $pdf->pages[] = $page1;
    ...

    // Add another page
    $page2 = new Zend_Pdf_Page($template);
    $page2->drawText('Another text...'$x$y);
    $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] Caution

    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 54.6. Cloning existing page

    $page1 = clone $pdf1->pages[$templatePageIndex1];
    $page2 = clone $pdf2->pages[$templatePageIndex2];
    $page1->drawText('Some text...'$x$y);
    $page2->drawText('Another text...'$x$y);
    ...
    $pdf = new Zend_Pdf();
    $pdf->pages[] = $page1;
    $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 54.7.  Cloning existing page using Zend_Pdf_Resource_Extractor class

    $extractor = new Zend_Pdf_Resource_Extractor();
    ....
    $page1 $extractor->clonePage($pdf->pages[$templatePageIndex1]);
    $page2 $extractor->clonePage($pdf->pages[$templatePageIndex2]);
    $page1->drawText('Some text...'$x$y);
    $page2->drawText('Another text...'$x$y);
    ...
    $pdf = new Zend_Pdf();
    $pdf->pages[] = $page1;
    $pdf->pages[] = $page2;

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...