Zend_Pdf-Drawing.xml 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.pdf.drawing">
  4. <title>Drawing</title>
  5. <sect2 id="zend.pdf.drawing.geometry">
  6. <title>Geometry</title>
  7. <para>
  8. <acronym>PDF</acronym> uses the same geometry as PostScript. It starts from bottom-left
  9. corner of page and by default is measured in points (1/72 of an inch).
  10. </para>
  11. <para>
  12. Page size can be retrieved from a page object:
  13. </para>
  14. <programlisting language="php"><![CDATA[
  15. $width = $pdfPage->getWidth();
  16. $height = $pdfPage->getHeight();
  17. ]]></programlisting>
  18. </sect2>
  19. <sect2 id="zend.pdf.drawing.color">
  20. <title>Colors</title>
  21. <para>
  22. <acronym>PDF</acronym> has a powerful capabilities for colors representation.
  23. <classname>Zend_Pdf</classname> module supports Gray Scale, RGB and CMYK color spaces.
  24. Any of them can be used in any place, where <classname>Zend_Pdf_Color</classname> object
  25. is required. <classname>Zend_Pdf_Color_GrayScale</classname>,
  26. <classname>Zend_Pdf_Color_Rgb</classname> and <classname>Zend_Pdf_Color_Cmyk</classname>
  27. classes provide this functionality:
  28. </para>
  29. <programlisting language="php"><![CDATA[
  30. // $grayLevel (float number). 0.0 (black) - 1.0 (white)
  31. $color1 = new Zend_Pdf_Color_GrayScale($grayLevel);
  32. // $r, $g, $b (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
  33. $color2 = new Zend_Pdf_Color_Rgb($r, $g, $b);
  34. // $c, $m, $y, $k (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
  35. $color3 = new Zend_Pdf_Color_Cmyk($c, $m, $y, $k);
  36. ]]></programlisting>
  37. <para>
  38. <acronym>HTML</acronym> style colors are also provided with
  39. <classname>Zend_Pdf_Color_Html</classname> class:
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. $color1 = new Zend_Pdf_Color_Html('#3366FF');
  43. $color2 = new Zend_Pdf_Color_Html('silver');
  44. $color3 = new Zend_Pdf_Color_Html('forestgreen');
  45. ]]></programlisting>
  46. </sect2>
  47. <sect2 id="zend.pdf.drawing.shape-drawing">
  48. <title>Shape Drawing</title>
  49. <para>
  50. All drawing operations can be done in a context of <acronym>PDF</acronym> page.
  51. </para>
  52. <para>
  53. <classname>Zend_Pdf_Page</classname> class provides a set of drawing primitives:
  54. </para>
  55. <programlisting language="php"><![CDATA[
  56. /**
  57. * Draw a line from x1,y1 to x2,y2.
  58. *
  59. * @param float $x1
  60. * @param float $y1
  61. * @param float $x2
  62. * @param float $y2
  63. * @return Zend_Pdf_Page
  64. */
  65. public function drawLine($x1, $y1, $x2, $y2);
  66. ]]></programlisting>
  67. <programlisting language="php"><![CDATA[
  68. /**
  69. * Draw a rectangle.
  70. *
  71. * Fill types:
  72. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
  73. * and stroke (default)
  74. * Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke rectangle
  75. * Zend_Pdf_Page::SHAPE_DRAW_FILL - fill rectangle
  76. *
  77. * @param float $x1
  78. * @param float $y1
  79. * @param float $x2
  80. * @param float $y2
  81. * @param integer $fillType
  82. * @return Zend_Pdf_Page
  83. */
  84. public function drawRectangle($x1, $y1, $x2, $y2,
  85. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
  86. ]]></programlisting>
  87. <programlisting language="php"><![CDATA[
  88. /**
  89. * Draw a rounded rectangle.
  90. *
  91. * Fill types:
  92. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle and stroke (default)
  93. * Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke rectangle
  94. * Zend_Pdf_Page::SHAPE_DRAW_FILL - fill rectangle
  95. *
  96. * radius is an integer representing radius of the four corners, or an array
  97. * of four integers representing the radius starting at top left, going
  98. * clockwise
  99. *
  100. * @param float $x1
  101. * @param float $y1
  102. * @param float $x2
  103. * @param float $y2
  104. * @param integer|array $radius
  105. * @param integer $fillType
  106. * @return Zend_Pdf_Page
  107. */
  108. public function drawRoundedRectangle($x1, $y1, $x2, $y2, $radius,
  109. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
  110. ]]></programlisting>
  111. <programlisting language="php"><![CDATA[
  112. /**
  113. * Draw a polygon.
  114. *
  115. * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
  116. * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
  117. * See detailed description of these methods in a PDF documentation
  118. * (section 4.4.2 Path painting Operators, Filling)
  119. *
  120. * @param array $x - array of float (the X co-ordinates of the vertices)
  121. * @param array $y - array of float (the Y co-ordinates of the vertices)
  122. * @param integer $fillType
  123. * @param integer $fillMethod
  124. * @return Zend_Pdf_Page
  125. */
  126. public function drawPolygon($x, $y,
  127. $fillType =
  128. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  129. $fillMethod =
  130. Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
  131. ]]></programlisting>
  132. <programlisting language="php"><![CDATA[
  133. /**
  134. * Draw a circle centered on x, y with a radius of radius.
  135. *
  136. * Angles are specified in radians
  137. *
  138. * Method signatures:
  139. * drawCircle($x, $y, $radius);
  140. * drawCircle($x, $y, $radius, $fillType);
  141. * drawCircle($x, $y, $radius, $startAngle, $endAngle);
  142. * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
  143. *
  144. *
  145. * It's not a really circle, because PDF supports only cubic Bezier
  146. * curves. But very good approximation.
  147. * It differs from a real circle on a maximum 0.00026 radiuses (at PI/8,
  148. * 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 and 15*PI/8 angles).
  149. * At 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 and 7*PI/4 it's exactly
  150. * a tangent to a circle.
  151. *
  152. * @param float $x
  153. * @param float $y
  154. * @param float $radius
  155. * @param mixed $param4
  156. * @param mixed $param5
  157. * @param mixed $param6
  158. * @return Zend_Pdf_Page
  159. */
  160. public function drawCircle($x,
  161. $y,
  162. $radius,
  163. $param4 = null,
  164. $param5 = null,
  165. $param6 = null);
  166. ]]></programlisting>
  167. <programlisting language="php"><![CDATA[
  168. /**
  169. * Draw an ellipse inside the specified rectangle.
  170. *
  171. * Method signatures:
  172. * drawEllipse($x1, $y1, $x2, $y2);
  173. * drawEllipse($x1, $y1, $x2, $y2, $fillType);
  174. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  175. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  176. *
  177. * Angles are specified in radians
  178. *
  179. * @param float $x1
  180. * @param float $y1
  181. * @param float $x2
  182. * @param float $y2
  183. * @param mixed $param5
  184. * @param mixed $param6
  185. * @param mixed $param7
  186. * @return Zend_Pdf_Page
  187. */
  188. public function drawEllipse($x1,
  189. $y1,
  190. $x2,
  191. $y2,
  192. $param5 = null,
  193. $param6 = null,
  194. $param7 = null);
  195. ]]></programlisting>
  196. </sect2>
  197. <sect2 id="zend.pdf.drawing.text-drawing">
  198. <title>Text Drawing</title>
  199. <para>
  200. Text drawing operations also exist in the context of a <acronym>PDF</acronym> page. You
  201. can draw a single line of text at any position on the page by supplying the x and y
  202. coordinates of the baseline. Current font and current font size are used for text
  203. drawing operations (see detailed description below).
  204. </para>
  205. <programlisting language="php"><![CDATA[
  206. /**
  207. * Draw a line of text at the specified position.
  208. *
  209. * @param string $text
  210. * @param float $x
  211. * @param float $y
  212. * @param string $charEncoding (optional) Character encoding of source
  213. * text.Defaults to current locale.
  214. * @throws Zend_Pdf_Exception
  215. * @return Zend_Pdf_Page
  216. */
  217. public function drawText($text, $x, $y, $charEncoding = '');
  218. ]]></programlisting>
  219. <example id="zend.pdf.drawing.text-drawing.example-1">
  220. <title>Draw a string on the page</title>
  221. <programlisting language="php"><![CDATA[
  222. ...
  223. $pdfPage->drawText('Hello world!', 72, 720);
  224. ...
  225. ]]></programlisting>
  226. </example>
  227. <para>
  228. By default, text strings are interpreted using the character encoding method of the
  229. current locale. if you have a string that uses a different encoding method (such as a
  230. UTF-8 string read from a file on disk, or a MacRoman string obtained from a legacy
  231. database), you can indicate the character encoding at draw time and
  232. <classname>Zend_Pdf</classname> will handle the conversion for you. You can supply
  233. source strings in any encoding method supported by <acronym>PHP</acronym>'s
  234. <code><ulink url="http://www.php.net/manual/function.iconv.php">iconv()</ulink></code>
  235. function:
  236. </para>
  237. <example id="zend.pdf.drawing.text-drawing.example-2">
  238. <title>Draw a UTF-8-encoded string on the page</title>
  239. <programlisting language="php"><![CDATA[
  240. ...
  241. // Read a UTF-8-encoded string from disk
  242. $unicodeString = fread($fp, 1024);
  243. // Draw the string on the page
  244. $pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');
  245. ...
  246. ]]></programlisting>
  247. </example>
  248. </sect2>
  249. <sect2 id="zend.pdf.drawing.using-fonts">
  250. <title>Using fonts</title>
  251. <para>
  252. <methodname>Zend_Pdf_Page::drawText()</methodname> uses the page's current font and font
  253. size, which is set with the <methodname>Zend_Pdf_Page::setFont()</methodname> method:
  254. </para>
  255. <programlisting language="php"><![CDATA[
  256. /**
  257. * Set current font.
  258. *
  259. * @param Zend_Pdf_Resource_Font $font
  260. * @param float $fontSize
  261. * @return Zend_Pdf_Page
  262. */
  263. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
  264. ]]></programlisting>
  265. <para>
  266. <acronym>PDF</acronym> documents support PostScript Type 1 and TrueType fonts, as well
  267. as two specialized <acronym>PDF</acronym> types, Type 3 and composite fonts. There are
  268. also 14 standard Type 1 fonts built-in to every <acronym>PDF</acronym> viewer: Courier
  269. (4 styles), Helvetica (4 styles), Times (4 styles), Symbol, and Zapf Dingbats.
  270. </para>
  271. <para>
  272. <classname>Zend_Pdf</classname> currently supports the standard 14
  273. <acronym>PDF</acronym> fonts as well as your own custom TrueType fonts. Font objects are
  274. obtained via one of two factory methods:
  275. <methodname>Zend_Pdf_Font::fontWithName($fontName)</methodname> for the standard 14
  276. <acronym>PDF</acronym> fonts or
  277. <methodname>Zend_Pdf_Font::fontWithPath($filePath)</methodname> for custom fonts.
  278. </para>
  279. <example id="zend.pdf.drawing.using-fonts.example-1">
  280. <title>Create a standard font</title>
  281. <programlisting language="php"><![CDATA[
  282. ...
  283. // Create new font
  284. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  285. // Apply font
  286. $pdfPage->setFont($font, 36);
  287. ...
  288. ]]></programlisting>
  289. </example>
  290. <para>
  291. Constants for the standard 14 <acronym>PDF</acronym> font names are defined in the
  292. <classname>Zend_Pdf_Font</classname> class:
  293. <itemizedlist>
  294. <listitem><para>Zend_Pdf_Font::FONT_COURIER</para></listitem>
  295. <listitem><para>Zend_Pdf_Font::FONT_COURIER_BOLD</para></listitem>
  296. <listitem><para>Zend_Pdf_Font::FONT_COURIER_ITALIC</para></listitem>
  297. <listitem><para>Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC</para></listitem>
  298. <listitem><para>Zend_Pdf_Font::FONT_TIMES</para></listitem>
  299. <listitem><para>Zend_Pdf_Font::FONT_TIMES_BOLD</para></listitem>
  300. <listitem><para>Zend_Pdf_Font::FONT_TIMES_ITALIC</para></listitem>
  301. <listitem><para>Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC</para></listitem>
  302. <listitem><para>Zend_Pdf_Font::FONT_HELVETICA</para></listitem>
  303. <listitem><para>Zend_Pdf_Font::FONT_HELVETICA_BOLD</para></listitem>
  304. <listitem><para>Zend_Pdf_Font::FONT_HELVETICA_ITALIC</para></listitem>
  305. <listitem><para>Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC</para></listitem>
  306. <listitem><para>Zend_Pdf_Font::FONT_SYMBOL</para></listitem>
  307. <listitem><para>Zend_Pdf_Font::FONT_ZAPFDINGBATS</para></listitem>
  308. </itemizedlist>
  309. </para>
  310. <para>
  311. You can also use any individual TrueType font (which usually has a '.ttf' extension) or
  312. an OpenType font ('.otf' extension) if it contains TrueType outlines. Currently
  313. unsupported, but planned for a future release are Mac OS X .dfont files and Microsoft
  314. TrueType Collection ('.ttc' extension) files.
  315. </para>
  316. <para>
  317. To use a TrueType font, you must provide the full file path to the font program. If the
  318. font cannot be read for some reason, or if it is not a TrueType font, the factory method
  319. will throw an exception:
  320. </para>
  321. <example id="zend.pdf.drawing.using-fonts.example-2">
  322. <title>Create a TrueType font</title>
  323. <programlisting language="php"><![CDATA[
  324. ...
  325. // Create new font
  326. $goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF');
  327. // Apply font
  328. $pdfPage->setFont($goodDogCoolFont, 36);
  329. ...
  330. ]]></programlisting>
  331. </example>
  332. <para>
  333. By default, custom fonts will be embedded in the resulting <acronym>PDF</acronym>
  334. document. This allows recipients to view the page as intended, even if they don't have
  335. the proper fonts installed on their system. If you are concerned about file size, you
  336. can request that the font program not be embedded by passing a 'do not embed' option to
  337. the factory method:
  338. </para>
  339. <example id="zend.pdf.drawing.using-fonts.example-3">
  340. <title>Create a TrueType font, but do not embed it in the PDF document</title>
  341. <programlisting language="php"><![CDATA[
  342. ...
  343. // Create new font
  344. $goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF',
  345. Zend_Pdf_Font::EMBED_DONT_EMBED);
  346. // Apply font
  347. $pdfPage->setFont($goodDogCoolFont, 36);
  348. ...
  349. ]]></programlisting>
  350. </example>
  351. <para>
  352. If the font program is not embedded but the recipient of the <acronym>PDF</acronym> file
  353. has the font installed on their system, they will see the document as intended. If they
  354. do not have the correct font installed, the <acronym>PDF</acronym> viewer application
  355. will do its best to synthesize a replacement.
  356. </para>
  357. <para>
  358. Some fonts have very specific licensing rules which prevent them from being embedded in
  359. <acronym>PDF</acronym> documents. So you are not caught off-guard by this, if you try to
  360. use a font that cannot be embedded, the factory method will throw an exception.
  361. </para>
  362. <para>
  363. You can still use these fonts, but you must either pass the do not embed flag as
  364. described above, or you can simply suppress the exception:
  365. </para>
  366. <example id="zend.pdf.drawing.using-fonts.example-4">
  367. <title>Do not throw an exception for fonts that cannot be embedded</title>
  368. <programlisting language="php"><![CDATA[
  369. ...
  370. $font = Zend_Pdf_Font::fontWithPath(
  371. '/path/to/unEmbeddableFont.ttf',
  372. Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
  373. );
  374. ...
  375. ]]></programlisting>
  376. </example>
  377. <para>
  378. This suppression technique is preferred if you allow an end-user to choose their own
  379. fonts. Fonts which can be embedded in the <acronym>PDF</acronym> document will be; those
  380. that cannot, won't.
  381. </para>
  382. <para>
  383. Font programs can be rather large, some reaching into the tens of megabytes. By default,
  384. all embedded fonts are compressed using the Flate compression scheme, resulting in a
  385. space savings of 50% on average. If, for some reason, you do not want to compress the
  386. font program, you can disable it with an option:
  387. </para>
  388. <example id="zend.pdf.drawing.using-fonts.example-5">
  389. <title>Do not compress an embedded font</title>
  390. <programlisting language="php"><![CDATA[
  391. ...
  392. $font = Zend_Pdf_Font::fontWithPath('/path/to/someReallyBigFont.ttf',
  393. Zend_Pdf_Font::EMBED_DONT_COMPRESS);
  394. ...
  395. ]]></programlisting>
  396. </example>
  397. <para>
  398. Finally, when necessary, you can combine the embedding options by using the bitwise OR
  399. operator:
  400. </para>
  401. <example id="zend.pdf.drawing.using-fonts.example-6">
  402. <title>Combining font embedding options</title>
  403. <programlisting language="php"><![CDATA[
  404. ...
  405. $font = Zend_Pdf_Font::fontWithPath(
  406. $someUserSelectedFontPath,
  407. (Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
  408. Zend_Pdf_Font::EMBED_DONT_COMPRESS));
  409. ...
  410. ]]></programlisting>
  411. </example>
  412. </sect2>
  413. <sect2 id="zend.pdf.drawing.standard-fonts-limitations">
  414. <title>Standard PDF fonts limitations</title>
  415. <para>
  416. Standard <acronym>PDF</acronym> fonts use several single byte encodings internally
  417. (see <ulink url="http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf">PDF
  418. Reference, Sixth Edition, version 1.7</ulink> Appendix D for details). They are
  419. generally equal to Latin1 character set (except Symbol and ZapfDingbats fonts).
  420. </para>
  421. <para>
  422. <classname>Zend_Pdf</classname> uses CP1252 (WinLatin1) for drawing text with standard
  423. fonts.
  424. </para>
  425. <para>
  426. Text still can be provided in any other encoding, which must be specified if it differs
  427. from a current locale. Only WinLatin1 characters will be actually drawn.
  428. </para>
  429. <example id="zend.pdf.drawing.using-fonts.example-7">
  430. <title>Combining font embedding options</title>
  431. <programlisting language="php"><![CDATA[
  432. ...
  433. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
  434. $pdfPage->setFont($font, 36)
  435. ->drawText('Euro sign - €', 72, 720, 'UTF-8')
  436. ->drawText('Text with umlauts - à è ì', 72, 650, 'UTF-8');
  437. ...
  438. ]]></programlisting>
  439. </example>
  440. </sect2>
  441. <sect2 id="zend.pdf.drawing.extracting-fonts">
  442. <title>Extracting fonts</title>
  443. <para>
  444. <classname>Zend_Pdf</classname> module provides a possibility to extract fonts from
  445. loaded documents.
  446. </para>
  447. <para>
  448. It may be useful for incremental document updates. Without this functionality you have
  449. to attach and possibly embed font into a document each time you want to update it.
  450. </para>
  451. <para>
  452. <classname>Zend_Pdf</classname> and <classname>Zend_Pdf_Page</classname> objects provide
  453. special methods to extract all fonts mentioned within a document or a page:
  454. </para>
  455. <example id="zend.pdf.drawing.extracting-fonts.example-1">
  456. <title>Extracting fonts from a loaded document</title>
  457. <programlisting language="php"><![CDATA[
  458. ...
  459. $pdf = Zend_Pdf::load($documentPath);
  460. ...
  461. // Get all document fonts
  462. $fontList = $pdf->extractFonts();
  463. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  464. $yPosition = 700;
  465. foreach ($fontList as $font) {
  466. $page->setFont($font, 15);
  467. $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
  468. 'en',
  469. 'UTF-8');
  470. $page->drawText($fontName . ': The quick brown fox jumps over the lazy dog',
  471. 100,
  472. $yPosition,
  473. 'UTF-8');
  474. $yPosition -= 30;
  475. }
  476. ...
  477. // Get fonts referenced within the first document page
  478. $firstPage = reset($pdf->pages);
  479. $firstPageFonts = $firstPage->extractFonts();
  480. ...
  481. ]]></programlisting>
  482. </example>
  483. <example id="zend.pdf.drawing.extracting-fonts.example-2">
  484. <title>Extracting font from a loaded document by specifying font name</title>
  485. <programlisting language="php"><![CDATA[
  486. ...
  487. $pdf = new Zend_Pdf();
  488. ...
  489. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  490. $font = Zend_Pdf_Font::fontWithPath($fontPath);
  491. $page->setFont($font, $fontSize);
  492. $page->drawText($text, $x, $y);
  493. ...
  494. // This font name should be stored somewhere...
  495. $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
  496. 'en',
  497. 'UTF-8');
  498. ...
  499. $pdf->save($docPath);
  500. ...
  501. ]]></programlisting>
  502. <programlisting language="php"><![CDATA[
  503. ...
  504. $pdf = Zend_Pdf::load($docPath);
  505. ...
  506. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  507. /* $srcPage->extractFont($fontName) can also be used here */
  508. $font = $pdf->extractFont($fontName);
  509. $page->setFont($font, $fontSize);
  510. $page->drawText($text, $x, $y);
  511. ...
  512. $pdf->save($docPath, true /* incremental update mode */);
  513. ...
  514. ]]></programlisting>
  515. </example>
  516. <para>
  517. Extracted fonts can be used in the place of any other font with the following
  518. limitations:
  519. <itemizedlist>
  520. <listitem>
  521. <para>
  522. Extracted font can be used only in the context of the document from which it
  523. was extracted.
  524. </para>
  525. </listitem>
  526. <listitem>
  527. <para>
  528. Possibly embedded font program is actually not extracted. So extracted font
  529. can't provide correct font metrics and original font has to be used for text
  530. width calculations:
  531. </para>
  532. <programlisting language="php"><![CDATA[
  533. ...
  534. $font = $pdf->extractFont($fontName);
  535. $originalFont = Zend_Pdf_Font::fontWithPath($fontPath);
  536. $page->setFont($font /* use extracted font for drawing */, $fontSize);
  537. $xPosition = $x;
  538. for ($charIndex = 0; $charIndex < strlen($text); $charIndex++) {
  539. $page->drawText($text[$charIndex], xPosition, $y);
  540. // Use original font for text width calculation
  541. $width = $originalFont->widthForGlyph(
  542. $originalFont->glyphNumberForCharacter($text[$charIndex])
  543. );
  544. $xPosition += $width/$originalFont->getUnitsPerEm()*$fontSize;
  545. }
  546. ...
  547. ]]></programlisting>
  548. </listitem>
  549. </itemizedlist>
  550. </para>
  551. </sect2>
  552. <sect2 id="zend.pdf.drawing.image-drawing">
  553. <title>Image Drawing</title>
  554. <para>
  555. <classname>Zend_Pdf_Page</classname> class provides drawImage() method to draw image:
  556. </para>
  557. <programlisting language="php"><![CDATA[
  558. /**
  559. * Draw an image at the specified position on the page.
  560. *
  561. * @param Zend_Pdf_Resource_Image $image
  562. * @param float $x1
  563. * @param float $y1
  564. * @param float $x2
  565. * @param float $y2
  566. * @return Zend_Pdf_Page
  567. */
  568. public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);
  569. ]]></programlisting>
  570. <para>
  571. Image objects should be created with
  572. <methodname>Zend_Pdf_Image::imageWithPath($filePath)</methodname> method (JPG, PNG and
  573. TIFF images are supported now):
  574. </para>
  575. <example id="zend.pdf.drawing.image-drawing.example-1">
  576. <title>Image drawing</title>
  577. <programlisting language="php"><![CDATA[
  578. ...
  579. // load image
  580. $image = Zend_Pdf_Image::imageWithPath('my_image.jpg');
  581. $pdfPage->drawImage($image, 100, 100, 400, 300);
  582. ...
  583. ]]></programlisting>
  584. </example>
  585. <para>
  586. <emphasis>Important! JPEG support requires <acronym>PHP</acronym> GD extension to be
  587. configured.</emphasis><emphasis>Important! PNG support requires ZLIB extension to be
  588. configured to work with Alpha channel images.</emphasis>
  589. </para>
  590. <para>
  591. Refer to the <acronym>PHP</acronym> documentation for detailed information (<ulink
  592. url="http://www.php.net/manual/en/ref.image.php">http://www.php.net/manual/en/ref.image.php</ulink>).
  593. (<ulink
  594. url="http://www.php.net/manual/en/ref.zlib.php">http://www.php.net/manual/en/ref.zlib.php</ulink>).
  595. </para>
  596. </sect2>
  597. <sect2 id="zend.pdf.drawing.line-drawing-style">
  598. <title>Line drawing style</title>
  599. <para>
  600. Line drawing style is defined by line width, line color and line dashing pattern.
  601. All of this parameters can be assigned by <classname>Zend_Pdf_Page</classname>
  602. class methods:
  603. </para>
  604. <programlisting language="php"><![CDATA[
  605. /** Set line color. */
  606. public function setLineColor(Zend_Pdf_Color $color);
  607. /** Set line width. */
  608. public function setLineWidth(float $width);
  609. /**
  610. * Set line dashing pattern.
  611. *
  612. * Pattern is an array of floats:
  613. * array(on_length, off_length, on_length, off_length, ...)
  614. * Phase is shift from the beginning of line.
  615. *
  616. * @param array $pattern
  617. * @param array $phase
  618. * @return Zend_Pdf_Page
  619. */
  620. public function setLineDashingPattern($pattern, $phase = 0);
  621. ]]></programlisting>
  622. </sect2>
  623. <sect2 id="zend.pdf.drawing.fill-style">
  624. <title>Fill style</title>
  625. <para>
  626. <methodname>Zend_Pdf_Page::drawRectangle()</methodname>,
  627. <methodname>Zend_Pdf_Page::drawPolygon()</methodname>,
  628. <methodname>Zend_Pdf_Page::drawCircle()</methodname> and
  629. <methodname>Zend_Pdf_Page::drawEllipse()</methodname> methods take
  630. <varname>$fillType</varname> argument as an optional parameter. It can be:
  631. </para>
  632. <itemizedlist>
  633. <listitem>
  634. <para>Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke shape</para>
  635. </listitem>
  636. <listitem>
  637. <para>Zend_Pdf_Page::SHAPE_DRAW_FILL - only fill shape</para>
  638. </listitem>
  639. <listitem>
  640. <para>
  641. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill and stroke (default behavior)
  642. </para>
  643. </listitem>
  644. </itemizedlist>
  645. <para>
  646. <methodname>Zend_Pdf_Page::drawPolygon()</methodname> methods also takes an additional
  647. parameter <varname>$fillMethod</varname>:
  648. </para>
  649. <itemizedlist>
  650. <listitem>
  651. <para>Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING (default behavior)</para>
  652. <para>
  653. <citetitle>PDF reference</citetitle> describes this rule as follows:
  654. <blockquote>
  655. <para>
  656. The nonzero winding number rule determines whether a given point is
  657. inside a path by conceptually drawing a ray from that point to infinity
  658. in any direction and then examining the places where a segment of the
  659. path crosses the ray. Starting with a count of 0, the rule adds 1 each
  660. time a path segment crosses the ray from left to right and subtracts 1
  661. each time a segment crosses from right to left. After counting all the
  662. crossings, if the result is 0 then the point is outside the path;
  663. otherwise it is inside. Note: The method just described does not specify
  664. what to do if a path segment coincides with or is tangent to the chosen
  665. ray. Since the direction of the ray is arbitrary, the rule simply
  666. chooses a ray that does not encounter such problem intersections. For
  667. simple convex paths, the nonzero winding number rule defines the inside
  668. and outside as one would intuitively expect. The more interesting cases
  669. are those involving complex or self-intersecting paths like the ones
  670. shown in Figure 4.10 (in a <acronym>PDF</acronym> Reference). For a path
  671. consisting of a five-pointed star, drawn with five connected straight
  672. line segments intersecting each other, the rule considers the inside to
  673. be the entire area enclosed by the star, including the pentagon in the
  674. center. For a path composed of two concentric circles, the areas
  675. enclosed by both circles are considered to be inside, provided that both
  676. are drawn in the same direction. If the circles are drawn in opposite
  677. directions, only the "doughnut" shape between them is inside, according
  678. to the rule; the "doughnut hole" is outside.
  679. </para>
  680. </blockquote>
  681. </para>
  682. </listitem>
  683. <listitem>
  684. <para>Zend_Pdf_Page::FILL_METHOD_EVEN_ODD</para>
  685. <para>
  686. <citetitle>PDF reference</citetitle> describes this rule as follows:
  687. <blockquote>
  688. <para>
  689. An alternative to the nonzero winding number rule is the even-odd rule.
  690. This rule determines the "insideness" of a point by drawing a ray from
  691. that point in any direction and simply counting the number of path
  692. segments that cross the ray, regardless of direction. If this number is
  693. odd, the point is inside; if even, the point is outside. This yields the
  694. same results as the nonzero winding number rule for paths with simple
  695. shapes, but produces different results for more complex shapes. Figure
  696. 4.11 (in a <acronym>PDF</acronym> Reference) shows the effects of
  697. applying the even-odd rule to complex paths. For the five-pointed star,
  698. the rule considers the triangular points to be inside the path, but not
  699. the pentagon in the center. For the two concentric circles, only the
  700. "doughnut" shape between the two circles is considered inside,
  701. regardless of the directions in which the circles are drawn.
  702. </para>
  703. </blockquote>
  704. </para>
  705. </listitem>
  706. </itemizedlist>
  707. </sect2>
  708. <sect2 id="zend.pdf.drawing.linear-transformations">
  709. <title>Linear Transformations</title>
  710. <sect3 id="zend.pdf.drawing.linear-transformations.rotations">
  711. <title>Rotations</title>
  712. <para>
  713. <acronym>PDF</acronym> page can be rotated before applying any draw operation.
  714. It can be done by <methodname>Zend_Pdf_Page::rotate()</methodname> method:
  715. </para>
  716. <programlisting language="php"><![CDATA[
  717. /**
  718. * Rotate the page.
  719. *
  720. * @param float $x - the X co-ordinate of rotation point
  721. * @param float $y - the Y co-ordinate of rotation point
  722. * @param float $angle - rotation angle
  723. * @return Zend_Pdf_Page
  724. */
  725. public function rotate($x, $y, $angle);
  726. ]]></programlisting>
  727. </sect3>
  728. <sect3 id="zend.pdf.drawing.linear-transformations.scale">
  729. <title>Starting from ZF 1.8, scaling</title>
  730. <para>
  731. Scaling transformation is provided by
  732. <methodname>Zend_Pdf_Page::scale()</methodname> method:
  733. </para>
  734. <programlisting language="php"><![CDATA[
  735. /**
  736. * Scale coordination system.
  737. *
  738. * @param float $xScale - X dimention scale factor
  739. * @param float $yScale - Y dimention scale factor
  740. * @return Zend_Pdf_Page
  741. */
  742. public function scale($xScale, $yScale);
  743. ]]></programlisting>
  744. </sect3>
  745. <sect3 id="zend.pdf.drawing.linear-transformations.translate">
  746. <title>Starting from ZF 1.8, translating</title>
  747. <para>
  748. Coordinate system shifting is performed by
  749. <methodname>Zend_Pdf_Page::translate()</methodname> method:
  750. </para>
  751. <programlisting language="php"><![CDATA[
  752. /**
  753. * Translate coordination system.
  754. *
  755. * @param float $xShift - X coordinate shift
  756. * @param float $yShift - Y coordinate shift
  757. * @return Zend_Pdf_Page
  758. */
  759. public function translate($xShift, $yShift);
  760. ]]></programlisting>
  761. </sect3>
  762. <sect3 id="zend.pdf.drawing.linear-transformations.skew">
  763. <title>Starting from ZF 1.8, skewing</title>
  764. <para>
  765. Page skewing can be done using <methodname>Zend_Pdf_Page::skew()</methodname>
  766. method:
  767. </para>
  768. <programlisting language="php"><![CDATA[
  769. /**
  770. * Translate coordination system.
  771. *
  772. * @param float $x - the X co-ordinate of axis skew point
  773. * @param float $y - the Y co-ordinate of axis skew point
  774. * @param float $xAngle - X axis skew angle
  775. * @param float $yAngle - Y axis skew angle
  776. * @return Zend_Pdf_Page
  777. */
  778. public function skew($x, $y, $xAngle, $yAngle);
  779. ]]></programlisting>
  780. </sect3>
  781. </sect2>
  782. <sect2 id="zend.pdf.drawing.save-restore">
  783. <title>Save/restore graphics state</title>
  784. <para>
  785. At any time page graphics state (current font, font size, line color, fill color,
  786. line style, page rotation, clip area) can be saved and then restored. Save operation
  787. puts data to a graphics state stack, restore operation retrieves it from there.
  788. </para>
  789. <para>
  790. There are two methods in <classname>Zend_Pdf_Page</classname> class for these
  791. operations:
  792. </para>
  793. <programlisting language="php"><![CDATA[
  794. /**
  795. * Save the graphics state of this page.
  796. * This takes a snapshot of the currently applied style, position,
  797. * clipping area and any rotation/translation/scaling that has been
  798. * applied.
  799. *
  800. * @return Zend_Pdf_Page
  801. */
  802. public function saveGS();
  803. /**
  804. * Restore the graphics state that was saved with the last call to
  805. * saveGS().
  806. *
  807. * @return Zend_Pdf_Page
  808. */
  809. public function restoreGS();
  810. ]]></programlisting>
  811. </sect2>
  812. <sect2 id="zend.pdf.drawing.clipping">
  813. <title>Clipping draw area</title>
  814. <para>
  815. <acronym>PDF</acronym> and <classname>Zend_Pdf</classname> module support clipping of
  816. draw area. Current clip area limits the regions of the page affected by painting
  817. operators. It's a whole page initially.
  818. </para>
  819. <para>
  820. <classname>Zend_Pdf_Page</classname> class provides a set of methods for clipping
  821. operations.
  822. </para>
  823. <programlisting language="php"><![CDATA[
  824. /**
  825. * Intersect current clipping area with a rectangle.
  826. *
  827. * @param float $x1
  828. * @param float $y1
  829. * @param float $x2
  830. * @param float $y2
  831. * @return Zend_Pdf_Page
  832. */
  833. public function clipRectangle($x1, $y1, $x2, $y2);
  834. ]]></programlisting>
  835. <programlisting language="php"><![CDATA[
  836. /**
  837. * Intersect current clipping area with a polygon.
  838. *
  839. * @param array $x - array of float (the X co-ordinates of the vertices)
  840. * @param array $y - array of float (the Y co-ordinates of the vertices)
  841. * @param integer $fillMethod
  842. * @return Zend_Pdf_Page
  843. */
  844. public function clipPolygon($x,
  845. $y,
  846. $fillMethod =
  847. Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
  848. ]]></programlisting>
  849. <programlisting language="php"><![CDATA[
  850. /**
  851. * Intersect current clipping area with a circle.
  852. *
  853. * @param float $x
  854. * @param float $y
  855. * @param float $radius
  856. * @param float $startAngle
  857. * @param float $endAngle
  858. * @return Zend_Pdf_Page
  859. */
  860. public function clipCircle($x,
  861. $y,
  862. $radius,
  863. $startAngle = null,
  864. $endAngle = null);
  865. ]]></programlisting>
  866. <programlisting language="php"><![CDATA[
  867. /**
  868. * Intersect current clipping area with an ellipse.
  869. *
  870. * Method signatures:
  871. * drawEllipse($x1, $y1, $x2, $y2);
  872. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  873. *
  874. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  875. *
  876. * @param float $x1
  877. * @param float $y1
  878. * @param float $x2
  879. * @param float $y2
  880. * @param float $startAngle
  881. * @param float $endAngle
  882. * @return Zend_Pdf_Page
  883. */
  884. public function clipEllipse($x1,
  885. $y1,
  886. $x2,
  887. $y2,
  888. $startAngle = null,
  889. $endAngle = null);
  890. ]]></programlisting>
  891. </sect2>
  892. <sect2 id="zend.pdf.drawing.styles">
  893. <title>Styles</title>
  894. <para>
  895. <classname>Zend_Pdf_Style</classname> class provides styles functionality.
  896. </para>
  897. <para>
  898. Styles can be used to store a set of graphic state parameters and apply it to a
  899. <acronym>PDF</acronym> page by one operation:
  900. </para>
  901. <programlisting language="php"><![CDATA[
  902. /**
  903. * Set the style to use for future drawing operations on this page
  904. *
  905. * @param Zend_Pdf_Style $style
  906. * @return Zend_Pdf_Page
  907. */
  908. public function setStyle(Zend_Pdf_Style $style);
  909. /**
  910. * Return the style, applied to the page.
  911. *
  912. * @return Zend_Pdf_Style|null
  913. */
  914. public function getStyle();
  915. ]]></programlisting>
  916. <para>
  917. <classname>Zend_Pdf_Style</classname> class provides a set of methods to set or get
  918. different graphics state parameters:
  919. </para>
  920. <programlisting language="php"><![CDATA[
  921. /**
  922. * Set line color.
  923. *
  924. * @param Zend_Pdf_Color $color
  925. * @return Zend_Pdf_Page
  926. */
  927. public function setLineColor(Zend_Pdf_Color $color);
  928. ]]></programlisting>
  929. <programlisting language="php"><![CDATA[
  930. /**
  931. * Get line color.
  932. *
  933. * @return Zend_Pdf_Color|null
  934. */
  935. public function getLineColor();
  936. ]]></programlisting>
  937. <programlisting language="php"><![CDATA[
  938. /**
  939. * Set line width.
  940. *
  941. * @param float $width
  942. * @return Zend_Pdf_Page
  943. */
  944. public function setLineWidth($width);
  945. ]]></programlisting>
  946. <programlisting language="php"><![CDATA[
  947. /**
  948. * Get line width.
  949. *
  950. * @return float
  951. */
  952. public function getLineWidth();
  953. ]]></programlisting>
  954. <programlisting language="php"><![CDATA[
  955. /**
  956. * Set line dashing pattern
  957. *
  958. * @param array $pattern
  959. * @param float $phase
  960. * @return Zend_Pdf_Page
  961. */
  962. public function setLineDashingPattern($pattern, $phase = 0);
  963. ]]></programlisting>
  964. <programlisting language="php"><![CDATA[
  965. /**
  966. * Get line dashing pattern
  967. *
  968. * @return array
  969. */
  970. public function getLineDashingPattern();
  971. ]]></programlisting>
  972. <programlisting language="php"><![CDATA[
  973. /**
  974. * Get line dashing phase
  975. *
  976. * @return float
  977. */
  978. public function getLineDashingPhase();
  979. ]]></programlisting>
  980. <programlisting language="php"><![CDATA[
  981. /**
  982. * Set fill color.
  983. *
  984. * @param Zend_Pdf_Color $color
  985. * @return Zend_Pdf_Page
  986. */
  987. public function setFillColor(Zend_Pdf_Color $color);
  988. ]]></programlisting>
  989. <programlisting language="php"><![CDATA[
  990. /**
  991. * Get fill color.
  992. *
  993. * @return Zend_Pdf_Color|null
  994. */
  995. public function getFillColor();
  996. ]]></programlisting>
  997. <programlisting language="php"><![CDATA[
  998. /**
  999. * Set current font.
  1000. *
  1001. * @param Zend_Pdf_Resource_Font $font
  1002. * @param float $fontSize
  1003. * @return Zend_Pdf_Page
  1004. */
  1005. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
  1006. ]]></programlisting>
  1007. <programlisting language="php"><![CDATA[
  1008. /**
  1009. * Modify current font size
  1010. *
  1011. * @param float $fontSize
  1012. * @return Zend_Pdf_Page
  1013. */
  1014. public function setFontSize($fontSize);
  1015. ]]></programlisting>
  1016. <programlisting language="php"><![CDATA[
  1017. /**
  1018. * Get current font.
  1019. *
  1020. * @return Zend_Pdf_Resource_Font $font
  1021. */
  1022. public function getFont();
  1023. ]]></programlisting>
  1024. <programlisting language="php"><![CDATA[
  1025. /**
  1026. * Get current font size
  1027. *
  1028. * @return float $fontSize
  1029. */
  1030. public function getFontSize();
  1031. ]]></programlisting>
  1032. </sect2>
  1033. <sect2 id="zend.pdf.drawing.alpha">
  1034. <title>Transparency</title>
  1035. <para>
  1036. <classname>Zend_Pdf</classname> module supports transparency handling.
  1037. </para>
  1038. <para>
  1039. Transparency may be set using <methodname>Zend_Pdf_Page::setAlpha()</methodname> method:
  1040. </para>
  1041. <programlisting language="php"><![CDATA[
  1042. /**
  1043. * Set the transparency
  1044. *
  1045. * $alpha == 0 - transparent
  1046. * $alpha == 1 - opaque
  1047. *
  1048. * Transparency modes, supported by PDF:
  1049. * Normal (default), Multiply, Screen, Overlay, Darken, Lighten,
  1050. * ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion
  1051. *
  1052. * @param float $alpha
  1053. * @param string $mode
  1054. * @throws Zend_Pdf_Exception
  1055. * @return Zend_Pdf_Page
  1056. */
  1057. public function setAlpha($alpha, $mode = 'Normal');
  1058. ]]></programlisting>
  1059. </sect2>
  1060. </sect1>