Zend_Pdf-Drawing.xml 39 KB

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