Zend_Pdf-Drawing.xml 41 KB

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