initially import ZendFramework-1.9.5 into repository
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Pdf / demo.php
1 <?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Pdf
17 * @subpackage Demos
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 // set include_path to library/ directory only -- see ticket #11
23 set_include_path( dirname(dirname(dirname(dirname(__FILE__))))
24 . DIRECTORY_SEPARATOR . 'library' );
25
26 /**
27 * @see Zend_Pdf
28 */
29 require_once 'Zend/Pdf.php';
30
31 if (!isset($argv[1])) {
32 echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
33 exit;
34 }
35
36 try {
37 $pdf = Zend_Pdf::load($argv[1]);
38 } catch (Zend_Pdf_Exception $e) {
39 if ($e->getMessage() == 'Can not open \'' . $argv[1] . '\' file for reading.') {
40 // Create new PDF if file doesn't exist
41 $pdf = new Zend_Pdf();
42
43 if (!isset($argv[2])) {
44 // force complete file rewriting (instead of updating)
45 $argv[2] = $argv[1];
46 }
47 } else {
48 // Throw an exception if it's not the "Can't open file" exception
49 throw $e;
50 }
51 }
52
53 //------------------------------------------------------------------------------------
54 // Reverse page order
55 $pdf->pages = array_reverse($pdf->pages);
56
57 // Create new Style
58 $style = new Zend_Pdf_Style();
59 $style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
60 $style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
61 $style->setLineWidth(3);
62 $style->setLineDashingPattern(array(3, 2, 3, 4), 1.6);
63 $style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);
64
65 try {
66 // Create new image object
67 $stampImage = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/stamp.jpg');
68 } catch (Zend_Pdf_Exception $e) {
69 // Example of operating with image loading exceptions.
70 if ($e->getMessage() != 'Image extension is not installed.' &&
71 $e->getMessage() != 'JPG support is not configured properly.') {
72 throw $e;
73 }
74 $stampImage = null;
75 }
76
77 // Mark page as modified
78 foreach ($pdf->pages as $page){
79 $page->saveGS()
80 ->setAlpha(0.25)
81 ->setStyle($style)
82 ->rotate(0, 0, M_PI_2/3);
83
84 $page->saveGS();
85 $page->clipCircle(550, -10, 50);
86 if ($stampImage != null) {
87 $page->drawImage($stampImage, 500, -60, 600, 40);
88 }
89 $page->restoreGS();
90
91 $page->drawText('Modified by Zend Framework!', 150, 0)
92 ->restoreGS();
93 }
94
95 // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
96 $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
97
98 // Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
99 $pdf->pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));
100
101 // Create new font
102 $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
103
104 // Apply font and draw text
105 $page1->setFont($font, 36)
106 ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
107 ->drawText('Helvetica 36 text string', 60, 500);
108
109 // Use font object for another page
110 $page2->setFont($font, 24)
111 ->drawText('Helvetica 24 text string', 60, 500);
112
113 // Use another font
114 $page2->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES), 32)
115 ->drawText('Times-Roman 32 text string', 60, 450);
116
117 // Draw rectangle
118 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
119 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
120 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
121 ->drawRectangle(60, 400, 400, 350);
122
123 // Draw circle
124 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
125 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
126 ->drawCircle(85, 375, 25);
127
128 // Draw sectors
129 $page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
130 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
131 ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
132 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
133 ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
134
135 // Draw ellipse
136 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
137 ->drawEllipse(250, 400, 400, 350)
138 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
139 ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
140 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
141 ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
142
143 // Draw and fill polygon
144 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
145 $x = array();
146 $y = array();
147 for ($count = 0; $count < 8; $count++) {
148 $x[] = 140 + 25*cos(3*M_PI_4*$count);
149 $y[] = 375 + 25*sin(3*M_PI_4*$count);
150 }
151 $page2->drawPolygon($x, $y,
152 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
153 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
154
155 // ---------- Draw figures in modified coordination system -----------------------------------
156
157 // Coordination system movement
158 $page2->saveGS();
159 $page2->translate(60, 250); // Shift coordination system
160
161 // Draw rectangle
162 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
163 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
164 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
165 ->drawRectangle(0, 50, 340, 0);
166
167 // Draw circle
168 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
169 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
170 ->drawCircle(25, 25, 25);
171
172 // Draw sectors
173 $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
174 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
175 ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
176 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
177 ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
178
179 // Draw ellipse
180 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
181 ->drawEllipse(190, 50, 340, 0)
182 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
183 ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
184 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
185 ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
186
187 // Draw and fill polygon
188 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
189 $x = array();
190 $y = array();
191 for ($count = 0; $count < 8; $count++) {
192 $x[] = 80 + 25*cos(3*M_PI_4*$count);
193 $y[] = 25 + 25*sin(3*M_PI_4*$count);
194 }
195 $page2->drawPolygon($x, $y,
196 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
197 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
198
199 // Draw line
200 $page2->setLineWidth(0.5)
201 ->drawLine(0, 25, 340, 25);
202
203 $page2->restoreGS();
204
205
206 // Coordination system movement, skewing and scaling
207 $page2->saveGS();
208 $page2->translate(60, 150) // Shift coordination system
209 ->skew(0, 0, 0, -M_PI/9) // Skew coordination system
210 ->scale(0.9, 0.9); // Scale coordination system
211
212 // Draw rectangle
213 $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
214 ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
215 ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
216 ->drawRectangle(0, 50, 340, 0);
217
218 // Draw circle
219 $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
220 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
221 ->drawCircle(25, 25, 25);
222
223 // Draw sectors
224 $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
225 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
226 ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
227 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
228 ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
229
230 // Draw ellipse
231 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
232 ->drawEllipse(190, 50, 340, 0)
233 ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
234 ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
235 ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
236 ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
237
238 // Draw and fill polygon
239 $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
240 $x = array();
241 $y = array();
242 for ($count = 0; $count < 8; $count++) {
243 $x[] = 80 + 25*cos(3*M_PI_4*$count);
244 $y[] = 25 + 25*sin(3*M_PI_4*$count);
245 }
246 $page2->drawPolygon($x, $y,
247 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
248 Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
249
250 // Draw line
251 $page2->setLineWidth(0.5)
252 ->drawLine(0, 25, 340, 25);
253
254 $page2->restoreGS();
255
256 //------------------------------------------------------------------------------------
257
258 if (isset($argv[2])) {
259 $pdf->save($argv[2]);
260 } else {
261 $pdf->save($argv[1], true /* update */);
262 }