demo.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. // set include_path to library/ directory only -- see ticket #11
  22. set_include_path( dirname(dirname(dirname(dirname(__FILE__))))
  23. . DIRECTORY_SEPARATOR . 'library' );
  24. require_once 'Zend/Pdf.php';
  25. require_once 'Zend/Pdf/Style.php';
  26. require_once 'Zend/Pdf/Color/Cmyk.php';
  27. require_once 'Zend/Pdf/Color/Html.php';
  28. require_once 'Zend/Pdf/Color/GrayScale.php';
  29. require_once 'Zend/Pdf/Color/Rgb.php';
  30. require_once 'Zend/Pdf/Page.php';
  31. require_once 'Zend/Pdf/Font.php';
  32. if (!isset($argv[1])) {
  33. echo "USAGE: php demo.php <pdf_file> [<output_pdf_file>]\n";
  34. exit;
  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. if (!isset($argv[2])) {
  43. // force complete file rewriting (instead of updating)
  44. $argv[2] = $argv[1];
  45. }
  46. } else {
  47. // Throw an exception if it's not the "Can't open file" exception
  48. throw $e;
  49. }
  50. }
  51. //------------------------------------------------------------------------------------
  52. // Reverse page order
  53. $pdf->pages = array_reverse($pdf->pages);
  54. // Create new Style
  55. $style = new Zend_Pdf_Style();
  56. $style->setFillColor(new Zend_Pdf_Color_Rgb(0, 0, 0.9));
  57. $style->setLineColor(new Zend_Pdf_Color_GrayScale(0.2));
  58. $style->setLineWidth(3);
  59. $style->setLineDashingPattern(array(3, 2, 3, 4), 1.6);
  60. $style->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD), 32);
  61. try {
  62. // Create new image object
  63. require_once 'Zend/Pdf/Image.php';
  64. $stampImage = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/stamp.jpg');
  65. } catch (Zend_Pdf_Exception $e) {
  66. // Example of operating with image loading exceptions.
  67. if ($e->getMessage() != 'Image extension is not installed.' &&
  68. $e->getMessage() != 'JPG support is not configured properly.') {
  69. throw $e;
  70. }
  71. $stampImage = null;
  72. }
  73. // Mark page as modified
  74. foreach ($pdf->pages as $page){
  75. $page->saveGS()
  76. ->setAlpha(0.25)
  77. ->setStyle($style)
  78. ->rotate(0, 0, M_PI_2/3);
  79. $page->saveGS();
  80. $page->clipCircle(550, -10, 50);
  81. if ($stampImage != null) {
  82. $page->drawImage($stampImage, 500, -60, 600, 40);
  83. }
  84. $page->restoreGS();
  85. $page->drawText('Modified by Zend Framework!', 150, 0)
  86. ->restoreGS();
  87. }
  88. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  89. $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
  90. // Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
  91. $pdf->pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));
  92. // Create new font
  93. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  94. // Apply font and draw text
  95. $page1->setFont($font, 36)
  96. ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
  97. ->drawText('Helvetica 36 text string', 60, 500);
  98. // Use font object for another page
  99. $page2->setFont($font, 24)
  100. ->drawText('Helvetica 24 text string', 60, 500);
  101. // Use another font
  102. $page2->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES), 32)
  103. ->drawText('Times-Roman 32 text string', 60, 450);
  104. // Draw rectangle
  105. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
  106. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
  107. ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
  108. ->drawRectangle(60, 400, 400, 350);
  109. // Draw circle
  110. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
  111. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  112. ->drawCircle(85, 375, 25);
  113. // Draw sectors
  114. $page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
  115. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  116. ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
  117. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  118. ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
  119. // Draw ellipse
  120. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  121. ->drawEllipse(250, 400, 400, 350)
  122. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  123. ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
  124. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  125. ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
  126. // Draw and fill polygon
  127. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
  128. $x = array();
  129. $y = array();
  130. for ($count = 0; $count < 8; $count++) {
  131. $x[] = 140 + 25*cos(3*M_PI_4*$count);
  132. $y[] = 375 + 25*sin(3*M_PI_4*$count);
  133. }
  134. $page2->drawPolygon($x, $y,
  135. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  136. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
  137. // ---------- Draw figures in modified coordination system -----------------------------------
  138. // Coordination system movement
  139. $page2->saveGS();
  140. $page2->translate(60, 250); // Shift coordination system
  141. // Draw rectangle
  142. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
  143. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
  144. ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
  145. ->drawRectangle(0, 50, 340, 0);
  146. // Draw circle
  147. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
  148. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  149. ->drawCircle(25, 25, 25);
  150. // Draw sectors
  151. $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
  152. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  153. ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
  154. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  155. ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
  156. // Draw ellipse
  157. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  158. ->drawEllipse(190, 50, 340, 0)
  159. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  160. ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
  161. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  162. ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
  163. // Draw and fill polygon
  164. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
  165. $x = array();
  166. $y = array();
  167. for ($count = 0; $count < 8; $count++) {
  168. $x[] = 80 + 25*cos(3*M_PI_4*$count);
  169. $y[] = 25 + 25*sin(3*M_PI_4*$count);
  170. }
  171. $page2->drawPolygon($x, $y,
  172. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  173. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
  174. // Draw line
  175. $page2->setLineWidth(0.5)
  176. ->drawLine(0, 25, 340, 25);
  177. $page2->restoreGS();
  178. // Coordination system movement, skewing and scaling
  179. $page2->saveGS();
  180. $page2->translate(60, 150) // Shift coordination system
  181. ->skew(0, 0, 0, -M_PI/9) // Skew coordination system
  182. ->scale(0.9, 0.9); // Scale coordination system
  183. // Draw rectangle
  184. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
  185. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
  186. ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
  187. ->drawRectangle(0, 50, 340, 0);
  188. // Draw circle
  189. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
  190. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  191. ->drawCircle(25, 25, 25);
  192. // Draw sectors
  193. $page2->drawCircle(140, 25, 25, 2*M_PI/3, -M_PI/6)
  194. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  195. ->drawCircle(140, 25, 25, M_PI/6, 2*M_PI/3)
  196. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  197. ->drawCircle(140, 25, 25, -M_PI/6, M_PI/6);
  198. // Draw ellipse
  199. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  200. ->drawEllipse(190, 50, 340, 0)
  201. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  202. ->drawEllipse(190, 50, 340, 0, M_PI/6, 2*M_PI/3)
  203. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  204. ->drawEllipse(190, 50, 340, 0, -M_PI/6, M_PI/6);
  205. // Draw and fill polygon
  206. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
  207. $x = array();
  208. $y = array();
  209. for ($count = 0; $count < 8; $count++) {
  210. $x[] = 80 + 25*cos(3*M_PI_4*$count);
  211. $y[] = 25 + 25*sin(3*M_PI_4*$count);
  212. }
  213. $page2->drawPolygon($x, $y,
  214. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  215. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
  216. // Draw line
  217. $page2->setLineWidth(0.5)
  218. ->drawLine(0, 25, 340, 25);
  219. $page2->restoreGS();
  220. //------------------------------------------------------------------------------------
  221. if (isset($argv[2])) {
  222. $pdf->save($argv[2]);
  223. } else {
  224. $pdf->save($argv[1], true /* update */);
  225. }