Document Info and Metadata | Zend_Pdf Programmer's Reference Guide | Zend_ProgressBar

# Zend_Pdf module usage example

This section provides an example of module usage.

This example can be found in a demos/Zend/Pdf/demo.php file.

There are also test.pdf file, which can be used with this demo for test purposes.

Example #1 Zend_Pdf module usage demo

1. /**
2. * @package Zend_Pdf
3. * @subpackage demo
4. */
6. if (!isset ($argv[1])) {
7. echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
8. exit;
9. }
11. try {
12. $pdf = Zend_Pdf::load($argv[1]);
13. } catch (Zend_Pdf_Exception $e) {
14. if ($e->getMessage() == 'Can not open \'' . $argv[1] .
15. '\' file for reading.') {
16. // Create new PDF if file doesn't exist
17. $pdf = new Zend_Pdf();
19. if (!isset ($argv[2])) {
20. // force complete file rewriting (instead of updating)
21. $argv[2] = $argv[1];
22. }
23. } else {
24. // Throw an exception if it's not the "Can't open file
25. // exception
26. throw $e;
27. }
28. }
30. //------------------------------------------------------------------------
31. // Reverse page order
32. $pdf->pages = array_reverse ($pdf->pages);
34. // Create new Style
35. $style = new Zend_Pdf_Style();
36. $style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
37. $style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
38. $style->setLineWidth(3);
39. $style->setLineDashingPattern( array (3, 2, 3, 4), 1.6);
40. $fontH = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
41. $style->setFont($fontH, 32);
43. try {
44. // Create new image object
45. $imageFile = dirname (__FILE__) . '/stamp.jpg';
46. $stampImage = Zend_Pdf_Image::imageWithPath($imageFile);
47. } catch (Zend_Pdf_Exception $e) {
48. // Example of operating with image loading exceptions.
49. if ($e->getMessage() != 'Image extension is not installed.' &&
50. $e->getMessage() != 'JPG support is not configured properly.') {
51. throw $e;
52. }
53. $stampImage = null;
54. }
56. // Mark page as modified
57. foreach ($pdf->pages as $page){
58. $page->saveGS()
59. ->setAlpha(0.25)
60. ->setStyle($style)
61. ->rotate(0, 0, M_PI_2/3);
63. $page->saveGS();
64. $page->clipCircle(550, -10, 50);
65. if ($stampImage != null) {
66. $page->drawImage($stampImage, 500, -60, 600, 40);
67. }
68. $page->restoreGS();
70. $page->drawText('Modified by Zend Framework!', 150, 0)
71. ->restoreGS();
72. }
74. // Add new page generated by Zend_Pdf object
75. // (page is attached to the specified the document)
76. $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
78. // Add new page generated by Zend_Pdf_Page object
79. // (page is not attached to the document)
80. $page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE);
81. $pdf->pages[] = $page2;
83. // Create new font
84. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
86. // Apply font and draw text
87. $page1->setFont($font, 36)
88. ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
89. ->drawText('Helvetica 36 text string', 60, 500);
91. // Use font object for another page
92. $page2->setFont($font, 24)
93. ->drawText('Helvetica 24 text string', 60, 500);
95. // Use another font
96. $fontT = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
97. $page2->setFont($fontT, 32)
98. ->drawText('Times-Roman 32 text string', 60, 450);
100. // Draw rectangle
101. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
102. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
103. ->setLineDashingPattern( array (3, 2, 3, 4), 1.6)
104. ->drawRectangle(60, 400, 400, 350);
106. // Draw circle
107. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
108. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
109. ->drawCircle(85, 375, 25);
111. // Draw sectors
112. $page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
113. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
114. ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
115. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
116. ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
118. // Draw ellipse
119. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
120. ->drawEllipse(250, 400, 400, 350)
121. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
122. ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
123. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
124. ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
126. // Draw and fill polygon
127. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
128. $x = array ();
129. $y = array ();
130. for ($count = 0; $count < 8; $count++) {
131. $x[] = 140 + 25*cos (3*M_PI_4*$count);
132. $y[] = 375 + 25*sin (3*M_PI_4*$count);
133. }
134. $page2->drawPolygon($x, $y,
135. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
136. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
138. // ----------- Draw figures in modified coordination system --------------
140. // Coordination system movement
141. $page2->saveGS();
142. $page2->translate(60, 250); // Shift coordination system
144. // Draw rectangle
145. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
146. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
147. ->setLineDashingPattern( array (3, 2, 3, 4), 1.6)
148. ->drawRectangle(0, 50, 340, 0);
150. // Draw circle
151. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
152. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
153. ->drawCircle(25, 25, 25);
155. // Draw sectors
156. $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
157. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
158. ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
159. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
160. ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
162. // Draw ellipse
163. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
164. ->drawEllipse(190, 50, 340, 0)
165. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
166. ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
167. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
168. ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
170. // Draw and fill polygon
171. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
172. $x = array ();
173. $y = array ();
174. for ($count = 0; $count < 8; $count++) {
175. $x[] = 80 + 25*cos (3*M_PI_4*$count);
176. $y[] = 25 + 25*sin (3*M_PI_4*$count);
177. }
178. $page2->drawPolygon($x, $y,
179. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
180. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
182. // Draw line
183. $page2->setLineWidth(0.5)
184. ->drawLine(0, 25, 340, 25);
186. $page2->restoreGS();
188. // Coordination system movement, skewing and scaling
189. $page2->saveGS();
190. $page2->translate(60, 150) // Shift coordination system
191. ->skew(0, 0, 0, -M_PI/9) // Skew coordination system
192. ->scale(0.9, 0.9); // Scale coordination system
194. // Draw rectangle
195. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
196. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
197. ->setLineDashingPattern( array (3, 2, 3, 4), 1.6)
198. ->drawRectangle(0, 50, 340, 0);
200. // Draw circle
201. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
202. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
203. ->drawCircle(25, 25, 25);
205. // Draw sectors
206. $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
207. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
208. ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
209. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
210. ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
212. // Draw ellipse
213. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
214. ->drawEllipse(190, 50, 340, 0)
215. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
216. ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
217. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
218. ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
220. // Draw and fill polygon
221. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
222. $x = array ();
223. $y = array ();
224. for ($count = 0; $count < 8; $count++) {
225. $x[] = 80 + 25*cos (3*M_PI_4*$count);
226. $y[] = 25 + 25*sin (3*M_PI_4*$count);
227. }
228. $page2->drawPolygon($x, $y,
229. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
230. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
232. // Draw line
233. $page2->setLineWidth(0.5)
234. ->drawLine(0, 25, 340, 25);
236. $page2->restoreGS();
238. //------------------------------------------------------------------------
240. if ( isset ($argv[2])) {
241. $pdf->save($argv[2]);
242. } else {
243. $pdf->save($argv[1], true /* update */);
244. }

Document Info and Metadata | Zend_Pdf Programmer's Reference Guide | Zend_ProgressBar