/library/Zend/Barcode/Renderer/Pdf.php

https://github.com/Exercise/zf2 · PHP · 244 lines · 131 code · 22 blank · 91 comment · 15 complexity · 20a1c6b2bc0eaad1f0d8490fa3de0506 MD5 · raw file

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