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