1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.pdf.pages">
<title>Working with Pages</title>
<sect2 id="zend.pdf.pages.creation">
<title>Page Creation</title>
<para>
The pages in a <acronym>PDF</acronym> document are represented as
<classname>Zend_Pdf_Page</classname> instances in <classname>Zend_Pdf</classname>.
</para>
<para>
<acronym>PDF</acronym> pages either are loaded from an existing <acronym>PDF</acronym>
or created using the <classname>Zend_Pdf</classname> <acronym>API</acronym>.
</para>
<para>
New pages can be created by instantiating new <classname>Zend_Pdf_Page</classname>
objects directly or by calling the <methodname>Zend_Pdf::newPage()</methodname> method,
which returns a <classname>Zend_Pdf_Page</classname> object.
<methodname>Zend_Pdf::newPage()</methodname> creates a page that is already attached to
a document. Attached pages can't be used with another <acronym>PDF</acronym>
documents until it's not cloned. See <link linkend="zend.pdf.pages.cloning">Page
cloning</link> section for the details.
</para>
<para>
The <methodname>Zend_Pdf::newPage()</methodname> method and the
<classname>Zend_Pdf_Page</classname> 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:
<itemizedlist>
<listitem><para>Zend_Pdf_Page::SIZE_A4</para></listitem>
<listitem><para>Zend_Pdf_Page::SIZE_A4_LANDSCAPE</para></listitem>
<listitem><para>Zend_Pdf_Page::SIZE_LETTER</para></listitem>
<listitem><para>Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE</para></listitem>
</itemizedlist>
</para>
<para>
Document pages are stored in the <varname>$pages</varname> public attribute of the
<classname>Zend_Pdf</classname> class. The attribute holds an array of
<classname>Zend_Pdf_Page</classname> objects and completely defines the instances and
order of pages. This array can be manipulated like any other <acronym>PHP</acronym>
array:
</para>
<example id="zend.pdf.pages.example-1">
<title>PDF document pages management</title>
<programlisting language="php"><![CDATA[
...
// 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]);
...
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.pdf.pages.cloning">
<title>Page cloning</title>
<para>
Existing <acronym>PDF</acronym> page can be duplicated by creating new
<classname>Zend_Pdf_Page</classname> object with existing page as a parameter:
</para>
<example id="zend.pdf.pages.example-2">
<title>Duplicating existing page</title>
<programlisting language="php"><![CDATA[
...
// 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]);
...
]]></programlisting>
</example>
<para>
It's useful if you need several pages to be created using one template.
</para>
<caution>
<para>
Important! Duplicated page shares some <acronym>PDF</acronym> 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.
</para>
</caution>
<para>
<code>clone</code> 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:
</para>
<example id="zend.pdf.pages.example-3">
<title>Cloning existing page</title>
<programlisting language="php"><![CDATA[
$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;
]]></programlisting>
</example>
<para>
If several template pages are planned to be used as templates then it could be more efficient
to utilize <classname>Zend_Pdf_Resource_Extractor</classname> 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):
</para>
<example id="zend.pdf.pages.example-4">
<title>Cloning existing page using Zend_Pdf_Resource_Extractor class</title>
<programlisting language="php"><![CDATA[
$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;
]]></programlisting>
</example>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|