4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Orion Richardson <orionr@yahoo.com>
7 * @author Helmut Tischer <htischer@weihenstephan.org>
8 * @author Fabien Ménager <fabien.menager@gmail.com>
9 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
12 // FIXME: Need to sanity check inputs to this class
13 namespace Dompdf\Adapter
;
19 use Dompdf\Image\Cache
;
20 use Dompdf\PhpEvaluator
;
23 * PDF rendering interface
25 * Dompdf\Adapter\CPDF provides a simple stateless interface to the stateful one
26 * provided by the Cpdf class.
28 * Unless otherwise mentioned, all dimensions are in points (1/72 in). The
29 * coordinate origin is in the top left corner, and y values increase
32 * See {@link http://www.ros.co.nz/pdf/} for more complete documentation
33 * on the underlying {@link Cpdf} class.
37 class CPDF
implements Canvas
41 * Dimensions of paper sizes in points
45 static $PAPER_SIZES = array(
46 "4a0" => array(0, 0, 4767.87, 6740.79),
47 "2a0" => array(0, 0, 3370.39, 4767.87),
48 "a0" => array(0, 0, 2383.94, 3370.39),
49 "a1" => array(0, 0, 1683.78, 2383.94),
50 "a2" => array(0, 0, 1190.55, 1683.78),
51 "a3" => array(0, 0, 841.89, 1190.55),
52 "a4" => array(0, 0, 595.28, 841.89),
53 "a5" => array(0, 0, 419.53, 595.28),
54 "a6" => array(0, 0, 297.64, 419.53),
55 "a7" => array(0, 0, 209.76, 297.64),
56 "a8" => array(0, 0, 147.40, 209.76),
57 "a9" => array(0, 0, 104.88, 147.40),
58 "a10" => array(0, 0, 73.70, 104.88),
59 "b0" => array(0, 0, 2834.65, 4008.19),
60 "b1" => array(0, 0, 2004.09, 2834.65),
61 "b2" => array(0, 0, 1417.32, 2004.09),
62 "b3" => array(0, 0, 1000.63, 1417.32),
63 "b4" => array(0, 0, 708.66, 1000.63),
64 "b5" => array(0, 0, 498.90, 708.66),
65 "b6" => array(0, 0, 354.33, 498.90),
66 "b7" => array(0, 0, 249.45, 354.33),
67 "b8" => array(0, 0, 175.75, 249.45),
68 "b9" => array(0, 0, 124.72, 175.75),
69 "b10" => array(0, 0, 87.87, 124.72),
70 "c0" => array(0, 0, 2599.37, 3676.54),
71 "c1" => array(0, 0, 1836.85, 2599.37),
72 "c2" => array(0, 0, 1298.27, 1836.85),
73 "c3" => array(0, 0, 918.43, 1298.27),
74 "c4" => array(0, 0, 649.13, 918.43),
75 "c5" => array(0, 0, 459.21, 649.13),
76 "c6" => array(0, 0, 323.15, 459.21),
77 "c7" => array(0, 0, 229.61, 323.15),
78 "c8" => array(0, 0, 161.57, 229.61),
79 "c9" => array(0, 0, 113.39, 161.57),
80 "c10" => array(0, 0, 79.37, 113.39),
81 "ra0" => array(0, 0, 2437.80, 3458.27),
82 "ra1" => array(0, 0, 1729.13, 2437.80),
83 "ra2" => array(0, 0, 1218.90, 1729.13),
84 "ra3" => array(0, 0, 864.57, 1218.90),
85 "ra4" => array(0, 0, 609.45, 864.57),
86 "sra0" => array(0, 0, 2551.18, 3628.35),
87 "sra1" => array(0, 0, 1814.17, 2551.18),
88 "sra2" => array(0, 0, 1275.59, 1814.17),
89 "sra3" => array(0, 0, 907.09, 1275.59),
90 "sra4" => array(0, 0, 637.80, 907.09),
91 "letter" => array(0, 0, 612.00, 792.00),
92 "half-letter" => array(0, 0, 396.00, 612.00),
93 "legal" => array(0, 0, 612.00, 1008.00),
94 "ledger" => array(0, 0, 1224.00, 792.00),
95 "tabloid" => array(0, 0, 792.00, 1224.00),
96 "executive" => array(0, 0, 521.86, 756.00),
97 "folio" => array(0, 0, 612.00, 936.00),
98 "commercial #10 envelope" => array(0, 0, 684, 297),
99 "catalog #10 1/2 envelope" => array(0, 0, 648, 864),
100 "8.5x11" => array(0, 0, 612.00, 792.00),
101 "8.5x14" => array(0, 0, 612.00, 1008.0),
102 "11x17" => array(0, 0, 792.00, 1224.00),
113 * Instance of Cpdf class
120 * PDF width, in points
127 * PDF height, in points
134 * Current page number
138 private $_page_number;
141 * Total number of pages
145 private $_page_count;
148 * Text to display on every page
155 * Array of pages for accesing after rendering is initially complete
162 * Array of temporary cached images to be deleted when processing is complete
166 private $_image_cache;
169 * Currently-applied opacity level (0 - 1)
173 private $_current_opacity = 1;
178 * @param mixed $paper The size of paper to use in this PDF ({@link CPDF::$PAPER_SIZES})
179 * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
180 * @param Dompdf $dompdf The Dompdf instance
182 public function __construct($paper = "letter", $orientation = "portrait", Dompdf
$dompdf)
184 if (is_array($paper)) {
186 } else if (isset(self
::$PAPER_SIZES[mb_strtolower($paper)])) {
187 $size = self
::$PAPER_SIZES[mb_strtolower($paper)];
189 $size = self
::$PAPER_SIZES["letter"];
192 if (mb_strtolower($orientation) === "landscape") {
193 list($size[2], $size[3]) = array($size[3], $size[2]);
196 $this->_dompdf
= $dompdf;
198 $this->_pdf
= new \
Cpdf(
201 $dompdf->getOptions()->getFontCache(),
202 $dompdf->getOptions()->getTempDir()
205 $this->_pdf
->addInfo("Producer", sprintf("%s + CPDF", $dompdf->version
));
206 $time = substr_replace(date('YmdHisO'), '\'', -2, 0) . '\'';
207 $this->_pdf
->addInfo("CreationDate", "D:$time");
208 $this->_pdf
->addInfo("ModDate", "D:$time");
210 $this->_width
= $size[2] - $size[0];
211 $this->_height
= $size[3] - $size[1];
213 $this->_page_number
= $this->_page_count
= 1;
214 $this->_page_text
= array();
216 $this->_pages
= array($this->_pdf
->getFirstPageId());
218 $this->_image_cache
= array();
224 public function get_dompdf()
226 return $this->_dompdf
;
232 * Deletes all temporary image files
234 public function __destruct()
236 foreach ($this->_image_cache
as $img) {
237 // The file might be already deleted by 3rd party tmp cleaner,
238 // the file might not have been created at all
239 // (if image outputting commands failed)
240 // or because the destructor was called twice accidentally.
241 if (!file_exists($img)) {
245 if ($this->_dompdf
->getOptions()->getDebugPng()) {
246 print '[__destruct unlink ' . $img . ']';
248 if (!$this->_dompdf
->getOptions()->getDebugKeepTemp()) {
255 * Returns the Cpdf instance
259 public function get_cpdf()
265 * Add meta information to the PDF
267 * @param string $label label of the value (Creator, Producer, etc.)
268 * @param string $value the text to set
270 public function add_info($label, $value)
272 $this->_pdf
->addInfo($label, $value);
276 * Opens a new 'object'
278 * While an object is open, all drawing actions are recored in the object,
279 * as opposed to being drawn on the current page. Objects can be added
280 * later to a specific page or to several pages.
282 * The return value is an integer ID for the new object.
284 * @see CPDF::close_object()
285 * @see CPDF::add_object()
289 public function open_object()
291 $ret = $this->_pdf
->openObject();
292 $this->_pdf
->saveState();
297 * Reopens an existing 'object'
299 * @see CPDF::open_object()
300 * @param int $object the ID of a previously opened object
302 public function reopen_object($object)
304 $this->_pdf
->reopenObject($object);
305 $this->_pdf
->saveState();
309 * Closes the current 'object'
311 * @see CPDF::open_object()
313 public function close_object()
315 $this->_pdf
->restoreState();
316 $this->_pdf
->closeObject();
320 * Adds a specified 'object' to the document
322 * $object int specifying an object created with {@link
323 * CPDF::open_object()}. $where can be one of:
324 * - 'add' add to current page only
325 * - 'all' add to every page from the current one onwards
326 * - 'odd' add to all odd numbered pages from now on
327 * - 'even' add to all even numbered pages from now on
328 * - 'next' add the object to the next page only
329 * - 'nextodd' add to all odd numbered pages from the next one
330 * - 'nexteven' add to all even numbered pages from the next one
332 * @see Cpdf::addObject()
335 * @param string $where
337 public function add_object($object, $where = 'all')
339 $this->_pdf
->addObject($object, $where);
343 * Stops the specified 'object' from appearing in the document.
345 * The object will stop being displayed on the page following the current
350 public function stop_object($object)
352 $this->_pdf
->stopObject($object);
358 public function serialize_object($id)
360 // Serialize the pdf object's current state for retrieval later
361 return $this->_pdf
->serializeObject($id);
367 public function reopen_serialized_object($obj)
369 return $this->_pdf
->restoreSerializedObject($obj);
372 //........................................................................
375 * Returns the PDF's width in points
378 public function get_width()
380 return $this->_width
;
384 * Returns the PDF's height in points
387 public function get_height()
389 return $this->_height
;
393 * Returns the current page number
396 public function get_page_number()
398 return $this->_page_number
;
402 * Returns the total number of pages in the document
405 public function get_page_count()
407 return $this->_page_count
;
411 * Sets the current page number
415 public function set_page_number($num)
417 $this->_page_number
= $num;
421 * Sets the page count
425 public function set_page_count($count)
427 $this->_page_count
= $count;
431 * Sets the stroke color
433 * See {@link Style::set_color()} for the format of the color array.
434 * @param array $color
436 protected function _set_stroke_color($color)
438 $this->_pdf
->setStrokeColor($color);
439 $alpha = isset($color["alpha"]) ?
$color["alpha"] : 1;
440 if ($this->_current_opacity
!= 1) {
441 $alpha *= $this->_current_opacity
;
443 $this->_set_line_transparency("Normal", $alpha);
447 * Sets the fill colour
449 * See {@link Style::set_color()} for the format of the colour array.
450 * @param array $color
452 protected function _set_fill_color($color)
454 $this->_pdf
->setColor($color);
455 $alpha = isset($color["alpha"]) ?
$color["alpha"] : 1;
456 if ($this->_current_opacity
) {
457 $alpha *= $this->_current_opacity
;
459 $this->_set_fill_transparency("Normal", $alpha);
463 * Sets line transparency
464 * @see Cpdf::setLineTransparency()
466 * Valid blend modes are (case-sensitive):
468 * Normal, Multiply, Screen, Overlay, Darken, Lighten,
469 * ColorDodge, ColorBurn, HardLight, SoftLight, Difference,
472 * @param string $mode the blending mode to use
473 * @param float $opacity 0.0 fully transparent, 1.0 fully opaque
475 protected function _set_line_transparency($mode, $opacity)
477 $this->_pdf
->setLineTransparency($mode, $opacity);
481 * Sets fill transparency
482 * @see Cpdf::setFillTransparency()
484 * Valid blend modes are (case-sensitive):
486 * Normal, Multiply, Screen, Overlay, Darken, Lighten,
487 * ColorDogde, ColorBurn, HardLight, SoftLight, Difference,
490 * @param string $mode the blending mode to use
491 * @param float $opacity 0.0 fully transparent, 1.0 fully opaque
493 protected function _set_fill_transparency($mode, $opacity)
495 $this->_pdf
->setFillTransparency($mode, $opacity);
499 * Sets the line style
501 * @see Cpdf::setLineStyle()
503 * @param float $width
505 * @param string $join
508 protected function _set_line_style($width, $cap, $join, $dash)
510 $this->_pdf
->setLineStyle($width, $cap, $join, $dash);
519 public function set_opacity($opacity, $mode = "Normal")
521 $this->_set_line_transparency($mode, $opacity);
522 $this->_set_fill_transparency($mode, $opacity);
523 $this->_current_opacity
= $opacity;
526 public function set_default_view($view, $options = array())
528 array_unshift($options, $view);
529 call_user_func_array(array($this->_pdf
, "openHere"), $options);
533 * Remaps y coords from 4th to 1st quadrant
538 protected function y($y)
540 return $this->_height
- $y;
544 * Canvas implementation
550 * @param array $color
551 * @param float $width
552 * @param array $style
554 public function line($x1, $y1, $x2, $y2, $color, $width, $style = array())
556 $this->_set_stroke_color($color);
557 $this->_set_line_style($width, "butt", "", $style);
559 $this->_pdf
->line($x1, $this->y($y1),
561 $this->_set_line_transparency("Normal", $this->_current_opacity
);
569 * @param float $astart
571 * @param array $color
572 * @param float $width
573 * @param array $style
575 public function arc($x, $y, $r1, $r2, $astart, $aend, $color, $width, $style = array())
577 $this->_set_stroke_color($color);
578 $this->_set_line_style($width, "butt", "", $style);
580 $this->_pdf
->ellipse($x, $this->y($y), $r1, $r2, 0, 8, $astart, $aend, false, false, true, false);
581 $this->_set_line_transparency("Normal", $this->_current_opacity
);
585 * Convert a GIF or BMP image to a PNG image
587 * @param string $image_url
588 * @param integer $type
591 * @return string The url of the newly converted image
593 protected function _convert_gif_bmp_to_png($image_url, $type)
595 $func_name = "imagecreatefrom$type";
597 if (!function_exists($func_name)) {
598 if (!method_exists("Dompdf\Helpers", $func_name)) {
599 throw new Exception("Function $func_name() not found. Cannot convert $type image: $image_url. Please install the image PHP extension.");
601 $func_name = "\\Dompdf\\Helpers::" . $func_name;
604 set_error_handler(array("\\Dompdf\\Helpers", "record_warnings"));
605 $im = call_user_func($func_name, $image_url);
608 imageinterlace($im, false);
610 $tmp_dir = $this->_dompdf
->getOptions()->getTempDir();
611 $tmp_name = tempnam($tmp_dir, "{$type}dompdf_img_");
613 $filename = "$tmp_name.png";
614 $this->_image_cache
[] = $filename;
616 imagepng($im, $filename);
619 $filename = Cache
::$broken_image;
622 restore_error_handler();
632 * @param array $color
633 * @param float $width
634 * @param array $style
636 public function rectangle($x1, $y1, $w, $h, $color, $width, $style = array())
638 $this->_set_stroke_color($color);
639 $this->_set_line_style($width, "butt", "", $style);
640 $this->_pdf
->rectangle($x1, $this->y($y1) - $h, $w, $h);
641 $this->_set_line_transparency("Normal", $this->_current_opacity
);
649 * @param array $color
651 public function filled_rectangle($x1, $y1, $w, $h, $color)
653 $this->_set_fill_color($color);
654 $this->_pdf
->filledRectangle($x1, $this->y($y1) - $h, $w, $h);
655 $this->_set_fill_transparency("Normal", $this->_current_opacity
);
664 public function clipping_rectangle($x1, $y1, $w, $h)
666 $this->_pdf
->clippingRectangle($x1, $this->y($y1) - $h, $w, $h);
679 public function clipping_roundrectangle($x1, $y1, $w, $h, $rTL, $rTR, $rBR, $rBL)
681 $this->_pdf
->clippingRectangleRounded($x1, $this->y($y1) - $h, $w, $h, $rTL, $rTR, $rBR, $rBL);
687 public function clipping_end()
689 $this->_pdf
->clippingEnd();
695 public function save()
697 $this->_pdf
->saveState();
703 public function restore()
705 $this->_pdf
->restoreState();
713 public function rotate($angle, $x, $y)
715 $this->_pdf
->rotate($angle, $x, $y);
724 public function skew($angle_x, $angle_y, $x, $y)
726 $this->_pdf
->skew($angle_x, $angle_y, $x, $y);
735 public function scale($s_x, $s_y, $x, $y)
737 $this->_pdf
->scale($s_x, $s_y, $x, $y);
744 public function translate($t_x, $t_y)
746 $this->_pdf
->translate($t_x, $t_y);
757 public function transform($a, $b, $c, $d, $e, $f)
759 $this->_pdf
->transform(array($a, $b, $c, $d, $e, $f));
763 * @param array $points
764 * @param array $color
766 * @param array $style
769 public function polygon($points, $color, $width = null, $style = array(), $fill = false)
771 $this->_set_fill_color($color);
772 $this->_set_stroke_color($color);
775 for ($i = 1; $i < count($points); $i +
= 2) {
776 $points[$i] = $this->y($points[$i]);
779 $this->_pdf
->polygon($points, count($points) / 2, $fill);
781 $this->_set_fill_transparency("Normal", $this->_current_opacity
);
782 $this->_set_line_transparency("Normal", $this->_current_opacity
);
789 * @param array $color
794 public function circle($x, $y, $r1, $color, $width = null, $style = null, $fill = false)
796 $this->_set_fill_color($color);
797 $this->_set_stroke_color($color);
799 if (!$fill && isset($width)) {
800 $this->_set_line_style($width, "round", "round", $style);
803 $this->_pdf
->ellipse($x, $this->y($y), $r1, 0, 0, 8, 0, 360, 1, $fill);
805 $this->_set_fill_transparency("Normal", $this->_current_opacity
);
806 $this->_set_line_transparency("Normal", $this->_current_opacity
);
815 * @param string $resolution
817 public function image($img, $x, $y, $w, $h, $resolution = "normal")
819 list($width, $height, $type) = Helpers
::dompdf_getimagesize($img, $this->get_dompdf()->getHttpContext());
821 $debug_png = $this->_dompdf
->getOptions()->getDebugPng();
824 print "[image:$img|$width|$height|$type]";
832 $this->_pdf
->addJpegFromFile($img, $x, $this->y($y) - $h, $w, $h);
836 /** @noinspection PhpMissingBreakStatementInspection */
838 if ($debug_png) print '!!!bmp or gif!!!';
839 // @todo use cache for BMP and GIF
840 $img = $this->_convert_gif_bmp_to_png($img, $type);
843 if ($debug_png) print '!!!png!!!';
845 $this->_pdf
->addPngFromFile($img, $x, $this->y($y) - $h, $w, $h);
849 if ($debug_png) print '!!!SVG!!!';
851 $this->_pdf
->addSvgFromFile($img, $x, $this->y($y) - $h, $w, $h);
855 if ($debug_png) print '!!!unknown!!!';
862 * @param string $text
863 * @param string $font
865 * @param array $color
866 * @param float $word_space
867 * @param float $char_space
868 * @param float $angle
870 public function text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0)
874 $this->_set_fill_color($color);
877 $pdf->selectFont($font);
879 //FontMetrics::getFontHeight($font, $size) ==
880 //$this->getFontHeight($font, $size) ==
881 //$this->_pdf->selectFont($font),$this->_pdf->getFontHeight($size)
882 //- FontBBoxheight+FontHeightOffset, scaled to $size, in pt
883 //$this->_pdf->getFontDescender($size)
884 //- Descender scaled to size
886 //$this->_pdf->fonts[$this->_pdf->currentFont] sizes:
887 //['FontBBox'][0] left, ['FontBBox'][1] bottom, ['FontBBox'][2] right, ['FontBBox'][3] top
888 //Maximum extent of all glyphs of the font from the baseline point
889 //['Ascender'] maximum height above baseline except accents
890 //['Descender'] maximum depth below baseline, negative number means below baseline
891 //['FontHeightOffset'] manual enhancement of .afm files to trim windows fonts. currently not used.
892 //Values are in 1/1000 pt for a font size of 1 pt
894 //['FontBBox'][1] should be close to ['Descender']
895 //['FontBBox'][3] should be close to ['Ascender']+Accents
896 //in practice, FontBBox values are a little bigger
898 //The text position is referenced to the baseline, not to the lower corner of the FontBBox,
899 //for what the left,top corner is given.
900 //FontBBox spans also the background box for the text.
901 //If the lower corner would be used as reference point, the Descents of the glyphs would
902 //hang over the background box border.
903 //Therefore compensate only the extent above the Baseline.
905 //print '<pre>['.$font.','.$size.','.$pdf->getFontHeight($size).','.$pdf->getFontDescender($size).','.$pdf->fonts[$pdf->currentFont]['FontBBox'][3].','.$pdf->fonts[$pdf->currentFont]['FontBBox'][1].','.$pdf->fonts[$pdf->currentFont]['FontHeightOffset'].','.$pdf->fonts[$pdf->currentFont]['Ascender'].','.$pdf->fonts[$pdf->currentFont]['Descender'].']</pre>';
907 //$pdf->addText($x, $this->y($y) - ($pdf->fonts[$pdf->currentFont]['FontBBox'][3]*$size)/1000, $size, $text, $angle, $word_space, $char_space);
908 $pdf->addText($x, $this->y($y) - $pdf->getFontHeight($size), $size, $text, $angle, $word_space, $char_space);
910 $this->_set_fill_transparency("Normal", $this->_current_opacity
);
914 * @param string $code
916 public function javascript($code)
918 $this->_pdf
->addJavascript($code);
921 //........................................................................
924 * Add a named destination (similar to <a name="foo">...</a> in html)
926 * @param string $anchorname The name of the named destination
928 public function add_named_dest($anchorname)
930 $this->_pdf
->addDestination($anchorname, "Fit");
934 * Add a link to the pdf
936 * @param string $url The url to link to
937 * @param float $x The x position of the link
938 * @param float $y The y position of the link
939 * @param float $width The width of the link
940 * @param float $height The height of the link
942 public function add_link($url, $x, $y, $width, $height)
944 $y = $this->y($y) - $height;
946 if (strpos($url, '#') === 0) {
948 $name = substr($url, 1);
950 $this->_pdf
->addInternalLink($name, $x, $y, $x +
$width, $y +
$height);
953 $this->_pdf
->addLink(rawurldecode($url), $x, $y, $x +
$width, $y +
$height);
958 * @param string $text
959 * @param string $font
961 * @param int $word_spacing
962 * @param int $char_spacing
965 public function get_text_width($text, $font, $size, $word_spacing = 0, $char_spacing = 0)
967 $this->_pdf
->selectFont($font);
968 return $this->_pdf
->getTextWidth($size, $text, $word_spacing, $char_spacing);
975 public function register_string_subset($font, $string)
977 $this->_pdf
->registerText($font, $string);
981 * @param string $font
985 public function get_font_height($font, $size)
987 $this->_pdf
->selectFont($font);
989 $ratio = $this->_dompdf
->getOptions()->getFontHeightRatio();
990 return $this->_pdf
->getFontHeight($size) * $ratio;
993 /*function get_font_x_height($font, $size) {
994 $this->_pdf->selectFont($font);
995 $ratio = $this->_dompdf->getOptions()->getFontHeightRatio();
996 return $this->_pdf->getFontXHeight($size) * $ratio;
1000 * @param string $font
1001 * @param float $size
1004 public function get_font_baseline($font, $size)
1006 $ratio = $this->_dompdf
->getOptions()->getFontHeightRatio();
1007 return $this->get_font_height($font, $size) / $ratio;
1011 * Writes text at the specified x and y coordinates on every page
1013 * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced
1014 * with their current values.
1016 * See {@link Style::munge_color()} for the format of the colour array.
1020 * @param string $text the text to write
1021 * @param string $font the font file to use
1022 * @param float $size the font size, in points
1023 * @param array $color
1024 * @param float $word_space word spacing adjustment
1025 * @param float $char_space char spacing adjustment
1026 * @param float $angle angle to write the text at, measured CW starting from the x-axis
1028 public function page_text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0)
1031 $this->_page_text
[] = compact("_t", "x", "y", "text", "font", "size", "color", "word_space", "char_space", "angle");
1035 * Processes a script on every page
1037 * The variables $pdf, $PAGE_NUM, and $PAGE_COUNT are available.
1039 * This function can be used to add page numbers to all pages
1040 * after the first one, for example.
1042 * @param string $code the script code
1043 * @param string $type the language type for script
1045 public function page_script($code, $type = "text/php")
1048 $this->_page_text
[] = compact("_t", "code", "type");
1054 public function new_page()
1056 $this->_page_number++
;
1057 $this->_page_count++
;
1059 $ret = $this->_pdf
->newPage();
1060 $this->_pages
[] = $ret;
1065 * Add text to each page after rendering is complete
1067 protected function _add_page_text()
1069 if (!count($this->_page_text
)) {
1076 foreach ($this->_pages
as $pid) {
1077 $this->reopen_object($pid);
1079 foreach ($this->_page_text
as $pt) {
1084 $text = str_replace(array("{PAGE_NUM}", "{PAGE_COUNT}"),
1085 array($page_number, $this->_page_count
), $text);
1086 $this->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
1091 $eval = new PhpEvaluator($this);
1093 $eval->evaluate($code, array('PAGE_NUM' => $page_number, 'PAGE_COUNT' => $this->_page_count
));
1098 $this->close_object();
1104 * Streams the PDF to the client.
1106 * @param string $filename The filename to present to the client.
1107 * @param array $options Associative array: 'compress' => 1 or 0 (default 1); 'Attachment' => 1 or 0 (default 1).
1109 public function stream($filename = "document.pdf", $options = array())
1111 if (headers_sent()) {
1112 die("Unable to stream pdf: headers already sent");
1115 if (!isset($options["compress"])) $options["compress"] = true;
1116 if (!isset($options["Attachment"])) $options["Attachment"] = true;
1118 $this->_add_page_text();
1120 $debug = !$options['compress'];
1121 $tmp = ltrim($this->_pdf
->output($debug));
1123 header("Cache-Control: private");
1124 header("Content-Type: application/pdf");
1125 header("Content-Length: " . mb_strlen($tmp, "8bit"));
1127 $filename = str_replace(array("\n", "'"), "", basename($filename, ".pdf")) . ".pdf";
1128 $attachment = $options["Attachment"] ?
"attachment" : "inline";
1129 header(Helpers
::buildContentDispositionHeader($attachment, $filename));
1136 * Returns the PDF as a string.
1138 * @param array $options Associative array: 'compress' => 1 or 0 (default 1).
1141 public function output($options = array())
1143 if (!isset($options["compress"])) $options["compress"] = true;
1145 $this->_add_page_text();
1147 $debug = !$options['compress'];
1149 return $this->_pdf
->output($debug);
1153 * Returns logging messages generated by the Cpdf class
1157 public function get_messages()
1159 return $this->_pdf
->messages
;