Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zendpdf.pages.html

Size:19259
Storage flags:no_autoload,compress/gzip (22%)

Working with Pages — Zend Framework 2 2.4.2 documentation

Working with Pages

Page Creation

The pages in a PDF document are represented as ZendPdf\Page instances in ZendPdf.

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

New pages can be created by instantiating new ZendPdf\Page objects directly or by calling the ZendPdf\PdfDocument::newPage() method, which returns a ZendPdf\Page object. ZendPdf\PdfDocument::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 ZendPdf\PdfDocument::newPage() method and the ZendPdf\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:

  • ZendPdfPage::SIZE_A4
  • ZendPdfPage::SIZE_A4_LANDSCAPE
  • ZendPdfPage::SIZE_LETTER
  • ZendPdfPage::SIZE_LETTER_LANDSCAPE

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

PDF document pages management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
...
// Reverse page order
$pdf->pages = array_reverse($pdf->pages);
...
// Add new page
$pdf->pages[] = new ZendPdf\Page(ZendPdf\Page::SIZE_A4);
// Add new page
$pdf->pages[] = $pdf->newPage(ZendPdf\Page::SIZE_A4);

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

...

Page cloning

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

Duplicating existing page

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
...
// Store template page in a separate variable
$template = $pdf->pages[$templatePageIndex];
...
// Add new page
$page1 = new ZendPdf\Page($template);
$page1->drawText('Some text...', $x, $y);
$pdf->pages[] = $page1;
...

// Add another page
$page2 = new ZendPdf\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:

Cloning existing page

1
2
3
4
5
6
7
8
$page1 = clone $pdf1->pages[$templatePageIndex1];
$page2 = clone $pdf2->pages[$templatePageIndex2];
$page1->drawText('Some text...', $x, $y);
$page2->drawText('Another text...', $x, $y);
...
$pdf = new ZendPdf\PdfDocument();
$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 ZendPdf\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):

Cloning existing page using ZendPdf\Resource\Extractor class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$extractor = new ZendPdf\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 ZendPdf\PdfDocument();
$pdf->pages[] = $page1;
$pdf->pages[] = $page2;

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Working with Pages on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net