Zend_Pdf-Pages.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.pdf.pages">
  4. <title>Working with Pages</title>
  5. <sect2 id="zend.pdf.pages.creation">
  6. <title>Page Creation</title>
  7. <para>
  8. The pages in a <acronym>PDF</acronym> document are represented as
  9. <classname>Zend_Pdf_Page</classname> instances in <classname>Zend_Pdf</classname>.
  10. </para>
  11. <para>
  12. <acronym>PDF</acronym> pages either are loaded from an existing <acronym>PDF</acronym>
  13. or created using the <classname>Zend_Pdf</classname> <acronym>API</acronym>.
  14. </para>
  15. <para>
  16. New pages can be created by instantiating new <classname>Zend_Pdf_Page</classname>
  17. objects directly or by calling the <methodname>Zend_Pdf::newPage()</methodname> method,
  18. which returns a <classname>Zend_Pdf_Page</classname> object.
  19. <methodname>Zend_Pdf::newPage()</methodname> creates a page that is already attached to
  20. a document. Attached pages can't be used with another <acronym>PDF</acronym>
  21. documents until it's not cloned. See <link linkend="zend.pdf.pages.cloning">Page
  22. cloning</link> section for the details.
  23. </para>
  24. <para>
  25. The <methodname>Zend_Pdf::newPage()</methodname> method and the
  26. <classname>Zend_Pdf_Page</classname> constructor take the same parameters specifying
  27. page size. They can take either the size of page ($x, $y) in points (1/72 inch) or a
  28. predefined constant representing a page type:
  29. <itemizedlist>
  30. <listitem><para>Zend_Pdf_Page::SIZE_A4</para></listitem>
  31. <listitem><para>Zend_Pdf_Page::SIZE_A4_LANDSCAPE</para></listitem>
  32. <listitem><para>Zend_Pdf_Page::SIZE_LETTER</para></listitem>
  33. <listitem><para>Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE</para></listitem>
  34. </itemizedlist>
  35. </para>
  36. <para>
  37. Document pages are stored in the <varname>$pages</varname> public attribute of the
  38. <classname>Zend_Pdf</classname> class. The attribute holds an array of
  39. <classname>Zend_Pdf_Page</classname> objects and completely defines the instances and
  40. order of pages. This array can be manipulated like any other <acronym>PHP</acronym>
  41. array:
  42. </para>
  43. <example id="zend.pdf.pages.example-1">
  44. <title>PDF document pages management</title>
  45. <programlisting language="php"><![CDATA[
  46. ...
  47. // Reverse page order
  48. $pdf->pages = array_reverse($pdf->pages);
  49. ...
  50. // Add new page
  51. $pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  52. // Add new page
  53. $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  54. // Remove specified page.
  55. unset($pdf->pages[$id]);
  56. ...
  57. ]]></programlisting>
  58. </example>
  59. </sect2>
  60. <sect2 id="zend.pdf.pages.cloning">
  61. <title>Page cloning</title>
  62. <para>
  63. Existing <acronym>PDF</acronym> page can be duplicated by creating new
  64. <classname>Zend_Pdf_Page</classname> object with existing page as a parameter:
  65. </para>
  66. <example id="zend.pdf.pages.example-2">
  67. <title>Duplicating existing page</title>
  68. <programlisting language="php"><![CDATA[
  69. ...
  70. // Store template page in a separate variable
  71. $template = $pdf->pages[$templatePageIndex];
  72. ...
  73. // Add new page
  74. $page1 = new Zend_Pdf_Page($template);
  75. $page1->drawText('Some text...', $x, $y);
  76. $pdf->pages[] = $page1;
  77. ...
  78. // Add another page
  79. $page2 = new Zend_Pdf_Page($template);
  80. $page2->drawText('Another text...', $x, $y);
  81. $pdf->pages[] = $page2;
  82. ...
  83. // Remove source template page from the documents.
  84. unset($pdf->pages[$templatePageIndex]);
  85. ...
  86. ]]></programlisting>
  87. </example>
  88. <para>
  89. It's useful if you need several pages to be created using one template.
  90. </para>
  91. <caution>
  92. <para>
  93. Important! Duplicated page shares some <acronym>PDF</acronym> resources with
  94. a template page, so it can be used only within the same document as a template page.
  95. Modified document can be saved as new one.
  96. </para>
  97. </caution>
  98. <para>
  99. <code>clone</code> operator may be used to create page which is not attached to any document.
  100. It takes more time than duplicating page since it needs to copy all dependent objects
  101. (used fonts, images and other resources), but it allows to use pages from different source
  102. documents to create new one:
  103. </para>
  104. <example id="zend.pdf.pages.example-3">
  105. <title>Cloning existing page</title>
  106. <programlisting language="php"><![CDATA[
  107. $page1 = clone $pdf1->pages[$templatePageIndex1];
  108. $page2 = clone $pdf2->pages[$templatePageIndex2];
  109. $page1->drawText('Some text...', $x, $y);
  110. $page2->drawText('Another text...', $x, $y);
  111. ...
  112. $pdf = new Zend_Pdf();
  113. $pdf->pages[] = $page1;
  114. $pdf->pages[] = $page2;
  115. ]]></programlisting>
  116. </example>
  117. <para>
  118. If several template pages are planned to be used as templates then it could be more efficient
  119. to utilize <classname>Zend_Pdf_Resource_Extractor</classname> class which gives an ability
  120. to share resources between cloned pages - fonts, images, etc. (otherwise new resource copy
  121. will be created for each cloned page):
  122. </para>
  123. <example id="zend.pdf.pages.example-4">
  124. <title>Cloning existing page using Zend_Pdf_Resource_Extractor class</title>
  125. <programlisting language="php"><![CDATA[
  126. $extractor = new Zend_Pdf_Resource_Extractor();
  127. ....
  128. $page1 = $extractor->clonePage($pdf->pages[$templatePageIndex1]);
  129. $page2 = $extractor->clonePage($pdf->pages[$templatePageIndex2]);
  130. $page1->drawText('Some text...', $x, $y);
  131. $page2->drawText('Another text...', $x, $y);
  132. ...
  133. $pdf = new Zend_Pdf();
  134. $pdf->pages[] = $page1;
  135. $pdf->pages[] = $page2;
  136. ]]></programlisting>
  137. </example>
  138. </sect2>
  139. </sect1>
  140. <!--
  141. vim:se ts=4 sw=4 et:
  142. -->