Pdf.php 7.0 KB

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