|
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_Barcode |
|
17 * @subpackage Renderer |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Pdf.php 22418 2010-06-11 16:27:22Z mikaelkael $ |
|
21 */ |
|
22 |
|
23 /** @see Zend_Barcode_Renderer_RendererAbstract */ |
|
24 require_once 'Zend/Barcode/Renderer/RendererAbstract.php'; |
|
25 |
|
26 /** @see Zend_Pdf */ |
|
27 require_once 'Zend/Pdf.php'; |
|
28 |
|
29 /** @see Zend_Pdf_Page */ |
|
30 require_once 'Zend/Pdf/Page.php'; |
|
31 |
|
32 /** @see Zend_Pdf_Color_Rgb */ |
|
33 require_once 'Zend/Pdf/Color/Rgb.php'; |
|
34 |
|
35 /** |
|
36 * Class for rendering the barcode in PDF resource |
|
37 * |
|
38 * @category Zend |
|
39 * @package Zend_Barcode |
|
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
41 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
42 */ |
|
43 class Zend_Barcode_Renderer_Pdf extends Zend_Barcode_Renderer_RendererAbstract |
|
44 { |
|
45 /** |
|
46 * PDF resource |
|
47 * @var Zend_Pdf |
|
48 */ |
|
49 protected $_resource = null; |
|
50 |
|
51 /** |
|
52 * Page number in PDF resource |
|
53 * @var integer |
|
54 */ |
|
55 protected $_page = 0; |
|
56 |
|
57 /** |
|
58 * Module size rendering |
|
59 * @var float |
|
60 */ |
|
61 protected $_moduleSize = 0.5; |
|
62 |
|
63 /** |
|
64 * Set an image resource to draw the barcode inside |
|
65 * @param resource $value |
|
66 * @return Zend_Barcode_Renderer |
|
67 * @throw Zend_Barcode_Renderer_Exception |
|
68 */ |
|
69 public function setResource($pdf, $page = 0) |
|
70 { |
|
71 if (!$pdf instanceof Zend_Pdf) { |
|
72 require_once 'Zend/Barcode/Renderer/Exception.php'; |
|
73 throw new Zend_Barcode_Renderer_Exception( |
|
74 'Invalid Zend_Pdf resource provided to setResource()' |
|
75 ); |
|
76 } |
|
77 |
|
78 $this->_resource = $pdf; |
|
79 $this->_page = intval($page); |
|
80 |
|
81 if (!count($this->_resource->pages)) { |
|
82 $this->_page = 0; |
|
83 $this->_resource->pages[] = new Zend_Pdf_Page( |
|
84 Zend_Pdf_Page::SIZE_A4 |
|
85 ); |
|
86 } |
|
87 return $this; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Check renderer parameters |
|
92 * |
|
93 * @return void |
|
94 */ |
|
95 protected function _checkParams() |
|
96 { |
|
97 } |
|
98 |
|
99 /** |
|
100 * Draw the barcode in the PDF, send headers and the PDF |
|
101 * @return mixed |
|
102 */ |
|
103 public function render() |
|
104 { |
|
105 $this->draw(); |
|
106 header("Content-Type: application/pdf"); |
|
107 echo $this->_resource->render(); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Initialize the PDF resource |
|
112 * @return void |
|
113 */ |
|
114 protected function _initRenderer() |
|
115 { |
|
116 if ($this->_resource === null) { |
|
117 $this->_resource = new Zend_Pdf(); |
|
118 $this->_resource->pages[] = new Zend_Pdf_Page( |
|
119 Zend_Pdf_Page::SIZE_A4 |
|
120 ); |
|
121 } |
|
122 |
|
123 $pdfPage = $this->_resource->pages[$this->_page]; |
|
124 $this->_adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth()); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Draw a polygon in the rendering resource |
|
129 * @param array $points |
|
130 * @param integer $color |
|
131 * @param boolean $filled |
|
132 */ |
|
133 protected function _drawPolygon($points, $color, $filled = true) |
|
134 { |
|
135 $page = $this->_resource->pages[$this->_page]; |
|
136 foreach ($points as $point) { |
|
137 $x[] = $point[0] * $this->_moduleSize + $this->_leftOffset; |
|
138 $y[] = $page->getHeight() - $point[1] * $this->_moduleSize - $this->_topOffset; |
|
139 } |
|
140 if (count($y) == 4) { |
|
141 if ($x[0] != $x[3] && $y[0] == $y[3]) { |
|
142 $y[0] -= ($this->_moduleSize / 2); |
|
143 $y[3] -= ($this->_moduleSize / 2); |
|
144 } |
|
145 if ($x[1] != $x[2] && $y[1] == $y[2]) { |
|
146 $y[1] += ($this->_moduleSize / 2); |
|
147 $y[2] += ($this->_moduleSize / 2); |
|
148 } |
|
149 } |
|
150 |
|
151 $color = new Zend_Pdf_Color_Rgb( |
|
152 (($color & 0xFF0000) >> 16) / 255.0, |
|
153 (($color & 0x00FF00) >> 8) / 255.0, |
|
154 ($color & 0x0000FF) / 255.0 |
|
155 ); |
|
156 |
|
157 $page->setLineColor($color); |
|
158 $page->setFillColor($color); |
|
159 $page->setLineWidth($this->_moduleSize); |
|
160 |
|
161 $fillType = ($filled) |
|
162 ? Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE |
|
163 : Zend_Pdf_Page::SHAPE_DRAW_STROKE; |
|
164 |
|
165 $page->drawPolygon($x, $y, $fillType); |
|
166 } |
|
167 |
|
168 /** |
|
169 * Draw a text in the rendering resource |
|
170 * @param string $text |
|
171 * @param float $size |
|
172 * @param array $position |
|
173 * @param string $font |
|
174 * @param integer $color |
|
175 * @param string $alignment |
|
176 * @param float $orientation |
|
177 */ |
|
178 protected function _drawText( |
|
179 $text, |
|
180 $size, |
|
181 $position, |
|
182 $font, |
|
183 $color, |
|
184 $alignment = 'center', |
|
185 $orientation = 0 |
|
186 ) { |
|
187 $page = $this->_resource->pages[$this->_page]; |
|
188 $color = new Zend_Pdf_Color_Rgb( |
|
189 (($color & 0xFF0000) >> 16) / 255.0, |
|
190 (($color & 0x00FF00) >> 8) / 255.0, |
|
191 ($color & 0x0000FF) / 255.0 |
|
192 ); |
|
193 |
|
194 $page->setLineColor($color); |
|
195 $page->setFillColor($color); |
|
196 $page->setFont(Zend_Pdf_Font::fontWithPath($font), $size * $this->_moduleSize * 1.2); |
|
197 |
|
198 $width = $this->widthForStringUsingFontSize( |
|
199 $text, |
|
200 Zend_Pdf_Font::fontWithPath($font), |
|
201 $size * $this->_moduleSize |
|
202 ); |
|
203 |
|
204 $angle = pi() * $orientation / 180; |
|
205 $left = $position[0] * $this->_moduleSize + $this->_leftOffset; |
|
206 $top = $page->getHeight() - $position[1] * $this->_moduleSize - $this->_topOffset; |
|
207 |
|
208 switch ($alignment) { |
|
209 case 'center': |
|
210 $left -= ($width / 2) * cos($angle); |
|
211 $top -= ($width / 2) * sin($angle); |
|
212 break; |
|
213 case 'right': |
|
214 $left -= $width; |
|
215 break; |
|
216 } |
|
217 $page->rotate($left, $top, $angle); |
|
218 $page->drawText($text, $left, $top); |
|
219 $page->rotate($left, $top, - $angle); |
|
220 } |
|
221 |
|
222 /** |
|
223 * Calculate the width of a string: |
|
224 * in case of using alignment parameter in drawText |
|
225 * @param string $text |
|
226 * @param Zend_Pdf_Font $font |
|
227 * @param float $fontSize |
|
228 * @return float |
|
229 */ |
|
230 public function widthForStringUsingFontSize($text, $font, $fontSize) |
|
231 { |
|
232 $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $text); |
|
233 $characters = array(); |
|
234 for ($i = 0; $i < strlen($drawingString); $i ++) { |
|
235 $characters[] = (ord($drawingString[$i ++]) << 8) | ord($drawingString[$i]); |
|
236 } |
|
237 $glyphs = $font->glyphNumbersForCharacters($characters); |
|
238 $widths = $font->widthsForGlyphs($glyphs); |
|
239 $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize; |
|
240 return $stringWidth; |
|
241 } |
|
242 } |