PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Barcode/Renderer/Pdf.php

https://bitbucket.org/gkawka/zend-framework
PHP | 242 lines | 130 code | 25 blank | 87 comment | 15 complexity | dc9b39e57a9b21c5657f58d2d14bb5ba 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. * @param resource $value
  58. * @return Zend_Barcode_Renderer
  59. * @throw Zend_Barcode_Renderer_Exception
  60. */
  61. public function setResource($pdf, $page = 0)
  62. {
  63. if (!$pdf instanceof Zend_Pdf) {
  64. require_once 'Zend/Barcode/Renderer/Exception.php';
  65. throw new Zend_Barcode_Renderer_Exception(
  66. 'Invalid Zend_Pdf resource provided to setResource()'
  67. );
  68. }
  69. $this->_resource = $pdf;
  70. $this->_page = intval($page);
  71. if (!count($this->_resource->pages)) {
  72. $this->_page = 0;
  73. $this->_resource->pages[] = new Zend_Pdf_Page(
  74. Zend_Pdf_Page::SIZE_A4
  75. );
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Check renderer parameters
  81. *
  82. * @return void
  83. */
  84. protected function _checkParams()
  85. {
  86. }
  87. /**
  88. * Draw the barcode in the PDF, send headers and the PDF
  89. * @return mixed
  90. */
  91. public function render()
  92. {
  93. $this->draw();
  94. header("Content-Type: application/pdf");
  95. echo $this->_resource->render();
  96. }
  97. /**
  98. * Initialize the PDF resource
  99. * @return void
  100. */
  101. protected function _initRenderer()
  102. {
  103. if ($this->_resource === null) {
  104. $this->_resource = new Zend_Pdf();
  105. $this->_resource->pages[] = new Zend_Pdf_Page(
  106. Zend_Pdf_Page::SIZE_A4
  107. );
  108. }
  109. $pdfPage = $this->_resource->pages[$this->_page];
  110. $this->_adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth());
  111. }
  112. /**
  113. * Draw a polygon in the rendering resource
  114. * @param array $points
  115. * @param integer $color
  116. * @param boolean $filled
  117. */
  118. protected function _drawPolygon($points, $color, $filled = true)
  119. {
  120. $page = $this->_resource->pages[$this->_page];
  121. foreach ($points as $point) {
  122. $x[] = $point[0] * $this->_moduleSize + $this->_leftOffset;
  123. $y[] = $page->getHeight() - $point[1] * $this->_moduleSize - $this->_topOffset;
  124. }
  125. if (count($y) == 4) {
  126. if ($x[0] != $x[3] && $y[0] == $y[3]) {
  127. $y[0] -= ($this->_moduleSize / 2);
  128. $y[3] -= ($this->_moduleSize / 2);
  129. }
  130. if ($x[1] != $x[2] && $y[1] == $y[2]) {
  131. $y[1] += ($this->_moduleSize / 2);
  132. $y[2] += ($this->_moduleSize / 2);
  133. }
  134. }
  135. $color = new Zend_Pdf_Color_Rgb(
  136. (($color & 0xFF0000) >> 16) / 255.0,
  137. (($color & 0x00FF00) >> 8) / 255.0,
  138. ($color & 0x0000FF) / 255.0
  139. );
  140. $page->setLineColor($color);
  141. $page->setFillColor($color);
  142. $page->setLineWidth($this->_moduleSize);
  143. $fillType = ($filled)
  144. ? Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE
  145. : Zend_Pdf_Page::SHAPE_DRAW_STROKE;
  146. $page->drawPolygon($x, $y, $fillType);
  147. }
  148. /**
  149. * Draw a text in the rendering resource
  150. * @param string $text
  151. * @param float $size
  152. * @param array $position
  153. * @param string $font
  154. * @param integer $color
  155. * @param string $alignment
  156. * @param float $orientation
  157. */
  158. protected function _drawText(
  159. $text,
  160. $size,
  161. $position,
  162. $font,
  163. $color,
  164. $alignment = 'center',
  165. $orientation = 0
  166. ) {
  167. $page = $this->_resource->pages[$this->_page];
  168. $color = new Zend_Pdf_Color_Rgb(
  169. (($color & 0xFF0000) >> 16) / 255.0,
  170. (($color & 0x00FF00) >> 8) / 255.0,
  171. ($color & 0x0000FF) / 255.0
  172. );
  173. $page->setLineColor($color);
  174. $page->setFillColor($color);
  175. $page->setFont(Zend_Pdf_Font::fontWithPath($font), $size * $this->_moduleSize * 1.2);
  176. $width = $this->widthForStringUsingFontSize(
  177. $text,
  178. Zend_Pdf_Font::fontWithPath($font),
  179. $size * $this->_moduleSize
  180. );
  181. $angle = pi() * $orientation / 180;
  182. $left = $position[0] * $this->_moduleSize + $this->_leftOffset;
  183. $top = $page->getHeight() - $position[1] * $this->_moduleSize - $this->_topOffset;
  184. switch ($alignment) {
  185. case 'center':
  186. $left -= ($width / 2) * cos($angle);
  187. $top -= ($width / 2) * sin($angle);
  188. break;
  189. case 'right':
  190. $left -= $width;
  191. break;
  192. }
  193. $page->rotate($left, $top, $angle);
  194. $page->drawText($text, $left, $top);
  195. $page->rotate($left, $top, - $angle);
  196. }
  197. /**
  198. * Calculate the width of a string:
  199. * in case of using alignment parameter in drawText
  200. * @param string $text
  201. * @param Zend_Pdf_Font $font
  202. * @param float $fontSize
  203. * @return float
  204. */
  205. public function widthForStringUsingFontSize($text, $font, $fontSize)
  206. {
  207. $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $text);
  208. $characters = array();
  209. for ($i = 0; $i < strlen($drawingString); $i ++) {
  210. $characters[] = (ord($drawingString[$i ++]) << 8) | ord($drawingString[$i]);
  211. }
  212. $glyphs = $font->glyphNumbersForCharacters($characters);
  213. $widths = $font->widthsForGlyphs($glyphs);
  214. $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
  215. return $stringWidth;
  216. }
  217. }