composer package updates
[openemr.git] / vendor / dompdf / dompdf / src / Adapter / CPDF.php
blob6221ee1b7763bfb485e22e993c3109b6fedfba3d
1 <?php
2 /**
3 * @package dompdf
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;
15 use Dompdf\Canvas;
16 use Dompdf\Dompdf;
17 use Dompdf\Helpers;
18 use Dompdf\Exception;
19 use Dompdf\Image\Cache;
20 use Dompdf\PhpEvaluator;
22 /**
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
30 * downwards.
32 * See {@link http://www.ros.co.nz/pdf/} for more complete documentation
33 * on the underlying {@link Cpdf} class.
35 * @package dompdf
37 class CPDF implements Canvas
40 /**
41 * Dimensions of paper sizes in points
43 * @var array;
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),
106 * The Dompdf object
108 * @var Dompdf
110 private $_dompdf;
113 * Instance of Cpdf class
115 * @var Cpdf
117 private $_pdf;
120 * PDF width, in points
122 * @var float
124 private $_width;
127 * PDF height, in points
129 * @var float;
131 private $_height;
134 * Current page number
136 * @var int
138 private $_page_number;
141 * Total number of pages
143 * @var int
145 private $_page_count;
148 * Text to display on every page
150 * @var array
152 private $_page_text;
155 * Array of pages for accesing after rendering is initially complete
157 * @var array
159 private $_pages;
162 * Array of temporary cached images to be deleted when processing is complete
164 * @var array
166 private $_image_cache;
169 * Currently-applied opacity level (0 - 1)
171 * @var float
173 private $_current_opacity = 1;
176 * Class constructor
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)) {
185 $size = $paper;
186 } else if (isset(self::$PAPER_SIZES[mb_strtolower($paper)])) {
187 $size = self::$PAPER_SIZES[mb_strtolower($paper)];
188 } else {
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(
199 $size,
200 true,
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();
222 * @return Dompdf
224 public function get_dompdf()
226 return $this->_dompdf;
230 * Class destructor
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)) {
242 continue;
245 if ($this->_dompdf->getOptions()->getDebugPng()) {
246 print '[__destruct unlink ' . $img . ']';
248 if (!$this->_dompdf->getOptions()->getDebugKeepTemp()) {
249 unlink($img);
255 * Returns the Cpdf instance
257 * @return \Cpdf
259 public function get_cpdf()
261 return $this->_pdf;
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()
287 * @return int
289 public function open_object()
291 $ret = $this->_pdf->openObject();
292 $this->_pdf->saveState();
293 return $ret;
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()
334 * @param int $object
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
346 * one.
348 * @param int $object
350 public function stop_object($object)
352 $this->_pdf->stopObject($object);
356 * @access private
358 public function serialize_object($id)
360 // Serialize the pdf object's current state for retrieval later
361 return $this->_pdf->serializeObject($id);
365 * @access private
367 public function reopen_serialized_object($obj)
369 return $this->_pdf->restoreSerializedObject($obj);
372 //........................................................................
375 * Returns the PDF's width in points
376 * @return float
378 public function get_width()
380 return $this->_width;
384 * Returns the PDF's height in points
385 * @return float
387 public function get_height()
389 return $this->_height;
393 * Returns the current page number
394 * @return int
396 public function get_page_number()
398 return $this->_page_number;
402 * Returns the total number of pages in the document
403 * @return int
405 public function get_page_count()
407 return $this->_page_count;
411 * Sets the current page number
413 * @param int $num
415 public function set_page_number($num)
417 $this->_page_number = $num;
421 * Sets the page count
423 * @param int $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,
470 * Exclusion
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,
488 * Exclusion
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
504 * @param string $cap
505 * @param string $join
506 * @param array $dash
508 protected function _set_line_style($width, $cap, $join, $dash)
510 $this->_pdf->setLineStyle($width, $cap, $join, $dash);
514 * Sets the opacity
516 * @param $opacity
517 * @param $mode
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
535 * @param float $y
536 * @return float
538 protected function y($y)
540 return $this->_height - $y;
544 * Canvas implementation
546 * @param float $x1
547 * @param float $y1
548 * @param float $x2
549 * @param float $y2
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),
560 $x2, $this->y($y2));
561 $this->_set_line_transparency("Normal", $this->_current_opacity);
565 * @param float $x
566 * @param float $y
567 * @param float $r1
568 * @param float $r2
569 * @param float $astart
570 * @param float $aend
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
590 * @throws Exception
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);
607 if ($im) {
608 imageinterlace($im, false);
610 $tmp_dir = $this->_dompdf->getOptions()->getTempDir();
611 $tmp_name = tempnam($tmp_dir, "{$type}dompdf_img_");
612 @unlink($tmp_name);
613 $filename = "$tmp_name.png";
614 $this->_image_cache[] = $filename;
616 imagepng($im, $filename);
617 imagedestroy($im);
618 } else {
619 $filename = Cache::$broken_image;
622 restore_error_handler();
624 return $filename;
628 * @param float $x1
629 * @param float $y1
630 * @param float $w
631 * @param float $h
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);
645 * @param float $x1
646 * @param float $y1
647 * @param float $w
648 * @param float $h
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);
659 * @param float $x1
660 * @param float $y1
661 * @param float $w
662 * @param float $h
664 public function clipping_rectangle($x1, $y1, $w, $h)
666 $this->_pdf->clippingRectangle($x1, $this->y($y1) - $h, $w, $h);
670 * @param float $x1
671 * @param float $y1
672 * @param float $w
673 * @param float $h
674 * @param float $rTL
675 * @param float $rTR
676 * @param float $rBR
677 * @param float $rBL
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();
709 * @param $angle
710 * @param $x
711 * @param $y
713 public function rotate($angle, $x, $y)
715 $this->_pdf->rotate($angle, $x, $y);
719 * @param $angle_x
720 * @param $angle_y
721 * @param $x
722 * @param $y
724 public function skew($angle_x, $angle_y, $x, $y)
726 $this->_pdf->skew($angle_x, $angle_y, $x, $y);
730 * @param $s_x
731 * @param $s_y
732 * @param $x
733 * @param $y
735 public function scale($s_x, $s_y, $x, $y)
737 $this->_pdf->scale($s_x, $s_y, $x, $y);
741 * @param $t_x
742 * @param $t_y
744 public function translate($t_x, $t_y)
746 $this->_pdf->translate($t_x, $t_y);
750 * @param $a
751 * @param $b
752 * @param $c
753 * @param $d
754 * @param $e
755 * @param $f
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
765 * @param null $width
766 * @param array $style
767 * @param bool $fill
769 public function polygon($points, $color, $width = null, $style = array(), $fill = false)
771 $this->_set_fill_color($color);
772 $this->_set_stroke_color($color);
774 // Adjust y values
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);
786 * @param float $x
787 * @param float $y
788 * @param float $r1
789 * @param array $color
790 * @param null $width
791 * @param null $style
792 * @param bool $fill
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);
810 * @param string $img
811 * @param float $x
812 * @param float $y
813 * @param int $w
814 * @param int $h
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();
823 if ($debug_png) {
824 print "[image:$img|$width|$height|$type]";
827 switch ($type) {
828 case "jpeg":
829 if ($debug_png) {
830 print '!!!jpg!!!';
832 $this->_pdf->addJpegFromFile($img, $x, $this->y($y) - $h, $w, $h);
833 break;
835 case "gif":
836 /** @noinspection PhpMissingBreakStatementInspection */
837 case "bmp":
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);
842 case "png":
843 if ($debug_png) print '!!!png!!!';
845 $this->_pdf->addPngFromFile($img, $x, $this->y($y) - $h, $w, $h);
846 break;
848 case "svg":
849 if ($debug_png) print '!!!SVG!!!';
851 $this->_pdf->addSvgFromFile($img, $x, $this->y($y) - $h, $w, $h);
852 break;
854 default:
855 if ($debug_png) print '!!!unknown!!!';
860 * @param float $x
861 * @param float $y
862 * @param string $text
863 * @param string $font
864 * @param float $size
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)
872 $pdf = $this->_pdf;
874 $this->_set_fill_color($color);
876 $font .= ".afm";
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) {
947 // Local link
948 $name = substr($url, 1);
949 if ($name) {
950 $this->_pdf->addInternalLink($name, $x, $y, $x + $width, $y + $height);
952 } else {
953 $this->_pdf->addLink(rawurldecode($url), $x, $y, $x + $width, $y + $height);
958 * @param string $text
959 * @param string $font
960 * @param float $size
961 * @param int $word_spacing
962 * @param int $char_spacing
963 * @return float|int
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);
972 * @param $font
973 * @param $string
975 public function register_string_subset($font, $string)
977 $this->_pdf->registerText($font, $string);
981 * @param string $font
982 * @param float $size
983 * @return float|int
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
1002 * @return float
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.
1018 * @param float $x
1019 * @param float $y
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)
1030 $_t = "text";
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")
1047 $_t = "script";
1048 $this->_page_text[] = compact("_t", "code", "type");
1052 * @return int
1054 public function new_page()
1056 $this->_page_number++;
1057 $this->_page_count++;
1059 $ret = $this->_pdf->newPage();
1060 $this->_pages[] = $ret;
1061 return $ret;
1065 * Add text to each page after rendering is complete
1067 protected function _add_page_text()
1069 if (!count($this->_page_text)) {
1070 return;
1073 $page_number = 1;
1074 $eval = null;
1076 foreach ($this->_pages as $pid) {
1077 $this->reopen_object($pid);
1079 foreach ($this->_page_text as $pt) {
1080 extract($pt);
1082 switch ($_t) {
1083 case "text":
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);
1087 break;
1089 case "script":
1090 if (!$eval) {
1091 $eval = new PhpEvaluator($this);
1093 $eval->evaluate($code, array('PAGE_NUM' => $page_number, 'PAGE_COUNT' => $this->_page_count));
1094 break;
1098 $this->close_object();
1099 $page_number++;
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));
1131 echo $tmp;
1132 flush();
1136 * Returns the PDF as a string.
1138 * @param array $options Associative array: 'compress' => 1 or 0 (default 1).
1139 * @return string
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
1155 * @return string
1157 public function get_messages()
1159 return $this->_pdf->messages;