PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Page cloning

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

Example 661. Duplicating existing page

<?php
...
// 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

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

<?php
$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 663. Cloning existing page using Zend_Pdf_Resource_Extractor class

<?php
$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;

Zend Framework