설명 없음

cached_pdf_decorator.cls.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. /**
  9. * Caching canvas implementation
  10. *
  11. * Each rendered page is serialized and stored in the {@link Page_Cache}.
  12. * This is useful for static forms/pages that do not need to be re-rendered
  13. * all the time.
  14. *
  15. * This class decorates normal CPDF_Adapters. It is currently completely
  16. * experimental.
  17. *
  18. * @access private
  19. * @package dompdf
  20. */
  21. class Cached_PDF_Decorator extends CPDF_Adapter implements Canvas {
  22. /**
  23. * @var CPDF_Adapter
  24. */
  25. protected $_pdf;
  26. protected $_cache_id;
  27. protected $_current_page_id;
  28. protected $_fonts; // fonts used in this document
  29. function __construct($paper = "letter", $orientation = "portrait", DOMPDF $dompdf) {
  30. $this->_fonts = array();
  31. }
  32. /**
  33. * Must be called after constructor
  34. *
  35. * @param int $cache_id
  36. * @param CPDF_Adapter $pdf
  37. */
  38. function init($cache_id, CPDF_Adapter $pdf) {
  39. $this->_cache_id = $cache_id;
  40. $this->_pdf = $pdf;
  41. $this->_current_page_id = $this->_pdf->open_object();
  42. }
  43. //........................................................................
  44. function get_cpdf() { return $this->_pdf->get_cpdf(); }
  45. function open_object() { $this->_pdf->open_object(); }
  46. function reopen_object($object) { $this->_pdf->reopen_object($object); }
  47. function close_object() { $this->_pdf->close_object(); }
  48. function add_object($object, $where = 'all') { $this->_pdf->add_object($object, $where); }
  49. function serialize_object($id) { $this->_pdf->serialize_object($id); }
  50. function reopen_serialized_object($obj) { $this->_pdf->reopen_serialized_object($obj); }
  51. //........................................................................
  52. function get_width() { return $this->_pdf->get_width(); }
  53. function get_height() { return $this->_pdf->get_height(); }
  54. function get_page_number() { return $this->_pdf->get_page_number(); }
  55. function get_page_count() { return $this->_pdf->get_page_count(); }
  56. function set_page_number($num) { $this->_pdf->set_page_number($num); }
  57. function set_page_count($count) { $this->_pdf->set_page_count($count); }
  58. function line($x1, $y1, $x2, $y2, $color, $width, $style = array()) {
  59. $this->_pdf->line($x1, $y1, $x2, $y2, $color, $width, $style);
  60. }
  61. function rectangle($x1, $y1, $w, $h, $color, $width, $style = array()) {
  62. $this->_pdf->rectangle($x1, $y1, $w, $h, $color, $width, $style);
  63. }
  64. function filled_rectangle($x1, $y1, $w, $h, $color) {
  65. $this->_pdf->filled_rectangle($x1, $y1, $w, $h, $color);
  66. }
  67. function polygon($points, $color, $width = null, $style = array(), $fill = false) {
  68. $this->_pdf->polygon($points, $color, $width, $style, $fill);
  69. }
  70. function circle($x, $y, $r1, $color, $width = null, $style = null, $fill = false) {
  71. $this->_pdf->circle($x, $y, $r1, $color, $width, $style, $fill);
  72. }
  73. function image($img_url, $x, $y, $w, $h, $resolution = "normal") {
  74. $this->_pdf->image($img_url, $x, $y, $w, $h, $resolution);
  75. }
  76. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) {
  77. $this->_fonts[$font] = true;
  78. $this->_pdf->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
  79. }
  80. function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) {
  81. // We want to remove this from cached pages since it may not be correct
  82. $this->_pdf->close_object();
  83. $this->_pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
  84. $this->_pdf->reopen_object($this->_current_page_id);
  85. }
  86. function page_script($script, $type = 'text/php') {
  87. // We want to remove this from cached pages since it may not be correct
  88. $this->_pdf->close_object();
  89. $this->_pdf->page_script($script, $type);
  90. $this->_pdf->reopen_object($this->_current_page_id);
  91. }
  92. function new_page() {
  93. $this->_pdf->close_object();
  94. // Add the object to the current page
  95. $this->_pdf->add_object($this->_current_page_id, "add");
  96. $this->_pdf->new_page();
  97. Page_Cache::store_page($this->_cache_id,
  98. $this->_pdf->get_page_number() - 1,
  99. $this->_pdf->serialize_object($this->_current_page_id));
  100. $this->_current_page_id = $this->_pdf->open_object();
  101. return $this->_current_page_id;
  102. }
  103. function stream($filename, $options = null) {
  104. // Store the last page in the page cache
  105. if ( !is_null($this->_current_page_id) ) {
  106. $this->_pdf->close_object();
  107. $this->_pdf->add_object($this->_current_page_id, "add");
  108. Page_Cache::store_page($this->_cache_id,
  109. $this->_pdf->get_page_number(),
  110. $this->_pdf->serialize_object($this->_current_page_id));
  111. Page_Cache::store_fonts($this->_cache_id, $this->_fonts);
  112. $this->_current_page_id = null;
  113. }
  114. $this->_pdf->stream($filename);
  115. }
  116. function output($options = null) {
  117. // Store the last page in the page cache
  118. if ( !is_null($this->_current_page_id) ) {
  119. $this->_pdf->close_object();
  120. $this->_pdf->add_object($this->_current_page_id, "add");
  121. Page_Cache::store_page($this->_cache_id,
  122. $this->_pdf->get_page_number(),
  123. $this->_pdf->serialize_object($this->_current_page_id));
  124. $this->_current_page_id = null;
  125. }
  126. return $this->_pdf->output();
  127. }
  128. function get_messages() { return $this->_pdf->get_messages(); }
  129. }