1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.pdf.usage">
4 <title>Zend_Pdf module usage example</title>
7 This section provides an example of module usage.
11 This example can be found in a <filename>demos/Zend/Pdf/demo.php</filename> file.
15 There are also <filename>test.pdf</filename> file, which can be used with this demo
19 <example id="zend.pdf.usage.example-1">
20 <title>Zend_Pdf module usage demo</title>
22 <programlisting language="php"><![CDATA[
28 if (!isset($argv[1])) {
29 echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
34 $pdf = Zend_Pdf::load($argv[1]);
35 } catch (Zend_Pdf_Exception $e) {
36 if ($e->getMessage() == 'Can not open \'' . $argv[1] .
37 '\' file for reading.') {
38 // Create new PDF if file doesn't exist
39 $pdf = new Zend_Pdf();
41 if (!isset($argv[2])) {
42 // force complete file rewriting (instead of updating)
46 // Throw an exception if it's not the "Can't open file
52 //------------------------------------------------------------------------
54 $pdf->pages = array_reverse($pdf->pages);
57 $style = new Zend_Pdf_Style();
58 $style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
59 $style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
60 $style->setLineWidth(3);
61 $style->setLineDashingPattern(array(3, 2, 3, 4), 1.6);
62 $fontH = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
63 $style->setFont($fontH, 32);
66 // Create new image object
67 $imageFile = dirname(__FILE__) . '/stamp.jpg';
68 $stampImage = Zend_Pdf_Image::imageWithPath($imageFile);
69 } catch (Zend_Pdf_Exception $e) {
70 // Example of operating with image loading exceptions.
71 if ($e->getMessage() != 'Image extension is not installed.' &&
72 $e->getMessage() != 'JPG support is not configured properly.') {
78 // Mark page as modified
79 foreach ($pdf->pages as $page){
83 ->rotate(0, 0, M_PI_2/3);
86 $page->clipCircle(550, -10, 50);
87 if ($stampImage != null) {
88 $page->drawImage($stampImage, 500, -60, 600, 40);
92 $page->drawText('Modified by Zend Framework!', 150, 0)
96 // Add new page generated by Zend_Pdf object
97 // (page is attached to the specified the document)
98 $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
100 // Add new page generated by Zend_Pdf_Page object
101 // (page is not attached to the document)
102 $page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE);
103 $pdf->pages[] = $page2;
106 $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
108 // Apply font and draw text
109 $page1->setFont($font, 36)
110 ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
111 ->drawText('Helvetica 36 text string', 60, 500);
113 // Use font object for another page
114 $page2->setFont($font, 24)
115 ->drawText('Helvetica 24 text string', 60, 500);
118 $fontT = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
119 $page2->setFont($fontT, 32)
120 ->drawText('Times-Roman 32 text string', 60, 450);
123 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
124 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
125 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
126 ->drawRectangle(60, 400, 400, 350);
129 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
130 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
131 ->drawCircle(85, 375, 25);
134 $page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
135 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
136 ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
137 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
138 ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
141 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
142 ->drawEllipse(250, 400, 400, 350)
143 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
144 ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
145 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
146 ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
148 // Draw and fill polygon
149 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
152 for ($count = 0; $count < 8; $count++) {
153 $x[] = 140 + 25*cos(3*M_PI_4*$count);
154 $y[] = 375 + 25*sin(3*M_PI_4*$count);
156 $page2->drawPolygon($x, $y,
157 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
158 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
160 // ----------- Draw figures in modified coordination system --------------
162 // Coordination system movement
164 $page2->translate(60, 250); // Shift coordination system
167 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
168 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
169 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
170 ->drawRectangle(0, 50, 340, 0);
173 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
174 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
175 ->drawCircle(25, 25, 25);
178 $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
179 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
180 ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
181 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
182 ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
185 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
186 ->drawEllipse(190, 50, 340, 0)
187 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
188 ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
189 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
190 ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
192 // Draw and fill polygon
193 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
196 for ($count = 0; $count < 8; $count++) {
197 $x[] = 80 + 25*cos(3*M_PI_4*$count);
198 $y[] = 25 + 25*sin(3*M_PI_4*$count);
200 $page2->drawPolygon($x, $y,
201 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
202 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
205 $page2->setLineWidth(0.5)
206 ->drawLine(0, 25, 340, 25);
210 // Coordination system movement, skewing and scaling
212 $page2->translate(60, 150) // Shift coordination system
213 ->skew(0, 0, 0, -M_PI/9) // Skew coordination system
214 ->scale(0.9, 0.9); // Scale coordination system
217 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
218 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
219 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
220 ->drawRectangle(0, 50, 340, 0);
223 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
224 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
225 ->drawCircle(25, 25, 25);
228 $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
229 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
230 ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
231 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
232 ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
235 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
236 ->drawEllipse(190, 50, 340, 0)
237 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
238 ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
239 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
240 ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
242 // Draw and fill polygon
243 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
246 for ($count = 0; $count < 8; $count++) {
247 $x[] = 80 + 25*cos(3*M_PI_4*$count);
248 $y[] = 25 + 25*sin(3*M_PI_4*$count);
250 $page2->drawPolygon($x, $y,
251 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
252 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
255 $page2->setLineWidth(0.5)
256 ->drawLine(0, 25, 340, 25);
260 //------------------------------------------------------------------------
262 if (isset($argv[2])) {
263 $pdf->save($argv[2]);
265 $pdf->save($argv[1], true /* update */);