Zend_Pdf-Drawing.xml 41 KB

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