Zend_Pdf-Drawing.xml 40 KB

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