1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: pdflib_adapter.cls.php,v $ |
---|
6 | * Created on: 2005-02-28 |
---|
7 | * |
---|
8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
9 | * |
---|
10 | * This library is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU Lesser General Public |
---|
12 | * License as published by the Free Software Foundation; either |
---|
13 | * version 2.1 of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * This library is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public License |
---|
21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
23 | * 02111-1307 USA |
---|
24 | * |
---|
25 | * Alternatively, you may distribute this software under the terms of the |
---|
26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
29 | * |
---|
30 | * The latest version of DOMPDF might be available at: |
---|
31 | * http://www.digitaljunkies.ca/dompdf |
---|
32 | * |
---|
33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
34 | * @copyright 2004 Benj Carson |
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
36 | * @package dompdf |
---|
37 | * @version 0.5.1 |
---|
38 | */ |
---|
39 | |
---|
40 | /* $Id: pdflib_adapter.cls.php,v 1.23 2006/07/07 21:31:04 benjcarson Exp $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * PDF rendering interface |
---|
44 | * |
---|
45 | * PDFLib_Adapter provides a simple, stateless interface to the one |
---|
46 | * provided by PDFLib. |
---|
47 | * |
---|
48 | * Unless otherwise mentioned, all dimensions are in points (1/72 in). |
---|
49 | * The coordinate origin is in the top left corner and y values |
---|
50 | * increase downwards. |
---|
51 | * |
---|
52 | * See {@link http://www.pdflib.com/} for more complete documentation |
---|
53 | * on the underlying PDFlib functions. |
---|
54 | * |
---|
55 | * @package dompdf |
---|
56 | */ |
---|
57 | class PDFLib_Adapter implements Canvas { |
---|
58 | |
---|
59 | /** |
---|
60 | * Dimensions of paper sizes in points |
---|
61 | * |
---|
62 | * @var array; |
---|
63 | */ |
---|
64 | static public $PAPER_SIZES = array(); // Set to |
---|
65 | // CPDF_Adapter::$PAPER_SIZES below. |
---|
66 | |
---|
67 | /** |
---|
68 | * Fudge factor to adjust reported font heights |
---|
69 | * |
---|
70 | * CPDF reports larger font heights than PDFLib. This factor |
---|
71 | * adjusts the height reported by get_font_height(). |
---|
72 | * |
---|
73 | * @var float |
---|
74 | */ |
---|
75 | const FONT_HEIGHT_SCALE = 1.2; |
---|
76 | |
---|
77 | /** |
---|
78 | * Whether to create PDFs in memory or on disk |
---|
79 | * |
---|
80 | * @var bool |
---|
81 | */ |
---|
82 | static $IN_MEMORY = true; |
---|
83 | |
---|
84 | /** |
---|
85 | * Instance of PDFLib class |
---|
86 | * |
---|
87 | * @var PDFlib |
---|
88 | */ |
---|
89 | private $_pdf; |
---|
90 | |
---|
91 | /** |
---|
92 | * Name of temporary file used for PDFs created on disk |
---|
93 | * |
---|
94 | * @var string |
---|
95 | */ |
---|
96 | private $_file; |
---|
97 | |
---|
98 | /** |
---|
99 | * PDF width, in points |
---|
100 | * |
---|
101 | * @var float |
---|
102 | */ |
---|
103 | private $_width; |
---|
104 | |
---|
105 | /** |
---|
106 | * PDF height, in points |
---|
107 | * |
---|
108 | * @var height |
---|
109 | */ |
---|
110 | private $_height; |
---|
111 | |
---|
112 | /** |
---|
113 | * Last fill colour used |
---|
114 | * |
---|
115 | * @var array |
---|
116 | */ |
---|
117 | private $_last_fill_color; |
---|
118 | |
---|
119 | /** |
---|
120 | * Last stroke colour used |
---|
121 | * |
---|
122 | * @var array |
---|
123 | */ |
---|
124 | private $_last_stroke_color; |
---|
125 | |
---|
126 | /** |
---|
127 | * Cache of image handles |
---|
128 | * |
---|
129 | * @var array |
---|
130 | */ |
---|
131 | private $_imgs; |
---|
132 | |
---|
133 | /** |
---|
134 | * Cache of font handles |
---|
135 | * |
---|
136 | * @var array |
---|
137 | */ |
---|
138 | private $_fonts; |
---|
139 | |
---|
140 | /** |
---|
141 | * List of objects (templates) to add to multiple pages |
---|
142 | * |
---|
143 | * @var array |
---|
144 | */ |
---|
145 | private $_objs; |
---|
146 | |
---|
147 | /** |
---|
148 | * Current page number |
---|
149 | * |
---|
150 | * @var int |
---|
151 | */ |
---|
152 | private $_page_number; |
---|
153 | |
---|
154 | /** |
---|
155 | * Total number of pages |
---|
156 | * |
---|
157 | * @var int |
---|
158 | */ |
---|
159 | private $_page_count; |
---|
160 | |
---|
161 | /** |
---|
162 | * Text to display on every page |
---|
163 | * |
---|
164 | * @var array |
---|
165 | */ |
---|
166 | private $_page_text; |
---|
167 | |
---|
168 | /** |
---|
169 | * Array of pages for accesing after rendering is initially complete |
---|
170 | * |
---|
171 | * @var array |
---|
172 | */ |
---|
173 | private $_pages; |
---|
174 | |
---|
175 | /** |
---|
176 | * Class constructor |
---|
177 | * |
---|
178 | * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or |
---|
179 | * an array(xmin,ymin,xmax,ymax) |
---|
180 | * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') |
---|
181 | */ |
---|
182 | function __construct($paper = "letter", $orientation = "portrait") { |
---|
183 | if ( is_array($paper) ) |
---|
184 | $size = $paper; |
---|
185 | else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) |
---|
186 | $size = self::$PAPER_SIZES[$paper]; |
---|
187 | else |
---|
188 | $size = self::$PAPER_SIZES["letter"]; |
---|
189 | |
---|
190 | if ( mb_strtolower($orientation) == "landscape" ) { |
---|
191 | $a = $size[3]; |
---|
192 | $size[3] = $size[2]; |
---|
193 | $size[2] = $a; |
---|
194 | } |
---|
195 | $this->_width = $size[2] - $size[0]; |
---|
196 | $this->_height= $size[3] - $size[1]; |
---|
197 | |
---|
198 | $this->_pdf = new PDFLib(); |
---|
199 | |
---|
200 | if ( defined("DOMPDF_PDFLIB_LICENSE") ) |
---|
201 | $this->_pdf->set_parameter( "license", DOMPDF_PDFLIB_LICENSE); |
---|
202 | |
---|
203 | $this->_pdf->set_parameter("textformat", "utf8"); |
---|
204 | $this->_pdf->set_parameter("fontwarning", "false"); |
---|
205 | |
---|
206 | $this->_pdf->set_info("Creator", "DOMPDF Converter"); |
---|
207 | |
---|
208 | // Silence pedantic warnings about missing TZ settings |
---|
209 | if ( function_exists("date_default_timezone_get") ) { |
---|
210 | $tz = @date_default_timezone_get(); |
---|
211 | date_default_timezone_set("UTC"); |
---|
212 | $this->_pdf->set_info("Date", date("Y-m-d")); |
---|
213 | date_default_timezone_set($tz); |
---|
214 | } else { |
---|
215 | $this->_pdf->set_info("Date", date("Y-m-d")); |
---|
216 | } |
---|
217 | |
---|
218 | if ( self::$IN_MEMORY ) |
---|
219 | $this->_pdf->begin_document("",""); |
---|
220 | else { |
---|
221 | $this->_file = tempnam(DOMPDF_TEMP_DIR, "dompdf_tmp_"); |
---|
222 | $this->_pdf->begin_document($this->_file,""); |
---|
223 | } |
---|
224 | |
---|
225 | $this->_pdf->begin_page_ext($this->_width, $this->_height, ""); |
---|
226 | |
---|
227 | $this->_page_number = $this->_page_count = 1; |
---|
228 | $this->_page_text = array(); |
---|
229 | |
---|
230 | $this->_imgs = array(); |
---|
231 | $this->_fonts = array(); |
---|
232 | $this->_objs = array(); |
---|
233 | |
---|
234 | // Set up font paths |
---|
235 | $families = Font_Metrics::get_font_families(); |
---|
236 | foreach ($families as $family => $files) { |
---|
237 | foreach ($files as $style => $file) { |
---|
238 | $face = basename($file); |
---|
239 | |
---|
240 | // Prefer ttfs to afms |
---|
241 | if ( file_exists($file.".ttf") ) |
---|
242 | $file .= ".ttf"; |
---|
243 | |
---|
244 | else if ( file_exists($file .".TTF") ) |
---|
245 | $file .= ".TTF"; |
---|
246 | |
---|
247 | else if ( file_exists($file . ".pfb") ) |
---|
248 | $file .= ".pfb"; |
---|
249 | |
---|
250 | else if ( file_exists($file . ".PFB") ) |
---|
251 | $file .= ".PFB"; |
---|
252 | |
---|
253 | else |
---|
254 | continue; |
---|
255 | |
---|
256 | $this->_pdf->set_parameter("FontOutline", "\{$face\}=\{$file\}"); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Close the pdf |
---|
263 | */ |
---|
264 | protected function _close() { |
---|
265 | $this->_place_objects(); |
---|
266 | |
---|
267 | // Close all pages |
---|
268 | $this->_pdf->suspend_page(""); |
---|
269 | for ($p = 1; $p <= $this->_page_count; $p++) { |
---|
270 | $this->_pdf->resume_page("pagenumber=$p"); |
---|
271 | $this->_pdf->end_page_ext(""); |
---|
272 | } |
---|
273 | |
---|
274 | $this->_pdf->end_document(""); |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | /** |
---|
279 | * Returns the PDFLib instance |
---|
280 | * |
---|
281 | * @return PDFLib |
---|
282 | */ |
---|
283 | function get_pdflib() { return $this->_pdf; } |
---|
284 | |
---|
285 | /** |
---|
286 | * Opens a new 'object' (template in PDFLib-speak) |
---|
287 | * |
---|
288 | * While an object is open, all drawing actions are recorded to the |
---|
289 | * object instead of being drawn on the current page. Objects can |
---|
290 | * be added later to a specific page or to several pages. |
---|
291 | * |
---|
292 | * The return value is an integer ID for the new object. |
---|
293 | * |
---|
294 | * @see PDFLib_Adapter::close_object() |
---|
295 | * @see PDFLib_Adapter::add_object() |
---|
296 | * |
---|
297 | * @return int |
---|
298 | */ |
---|
299 | function open_object() { |
---|
300 | $this->_pdf->suspend_page(""); |
---|
301 | $ret = $this->_pdf->begin_template($this->_width, $this->_height); |
---|
302 | $this->_pdf->save(); |
---|
303 | $this->_objs[$ret] = array("start_page" => $this->_page_number); |
---|
304 | return $ret; |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Reopen an existing object (NOT IMPLEMENTED) |
---|
309 | * |
---|
310 | * PDFLib does not seem to support reopening templates. |
---|
311 | * |
---|
312 | * @param int $object the ID of a previously opened object |
---|
313 | */ |
---|
314 | function reopen_object($object) { |
---|
315 | throw new DOMPDF_Exception("PDFLib does not support reopening objects."); |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Close the current template |
---|
320 | * |
---|
321 | * @see PDFLib_Adapter::open_object() |
---|
322 | */ |
---|
323 | function close_object() { |
---|
324 | $this->_pdf->restore(); |
---|
325 | $this->_pdf->end_template(); |
---|
326 | $this->_pdf->resume_page("pagenumber=".$this->_page_number); |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Adds the specified object to the document |
---|
331 | * |
---|
332 | * $where can be one of: |
---|
333 | * - 'add' add to current page only |
---|
334 | * - 'all' add to every page from the current one onwards |
---|
335 | * - 'odd' add to all odd numbered pages from now on |
---|
336 | * - 'even' add to all even numbered pages from now on |
---|
337 | * - 'next' add the object to the next page only |
---|
338 | * - 'nextodd' add to all odd numbered pages from the next one |
---|
339 | * - 'nexteven' add to all even numbered pages from the next one |
---|
340 | * |
---|
341 | * @param int $object the object handle returned by open_object() |
---|
342 | * @param string $where |
---|
343 | */ |
---|
344 | function add_object($object, $where = 'all') { |
---|
345 | |
---|
346 | if ( mb_strpos($where, "next") !== false ) { |
---|
347 | $this->_objs[$object]["start_page"]++; |
---|
348 | $where = str_replace("next", "", $where); |
---|
349 | if ( $where == "" ) |
---|
350 | $where = "add"; |
---|
351 | } |
---|
352 | |
---|
353 | $this->_objs[$object]["where"] = $where; |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * Stops the specified template from appearing in the document. |
---|
358 | * |
---|
359 | * The object will stop being displayed on the page following the |
---|
360 | * current one. |
---|
361 | * |
---|
362 | * @param int $object |
---|
363 | */ |
---|
364 | function stop_object($object) { |
---|
365 | |
---|
366 | if ( !isset($this->_objs[$object]) ) |
---|
367 | return; |
---|
368 | |
---|
369 | $start = $this->_objs[$object]["start_page"]; |
---|
370 | $where = $this->_objs[$object]["where"]; |
---|
371 | |
---|
372 | // Place the object on this page if required |
---|
373 | if ( $this->_page_number >= $start && |
---|
374 | (($this->_page_number % 2 == 0 && $where == "even") || |
---|
375 | ($this->_page_number % 2 == 1 && $where == "odd") || |
---|
376 | ($where == "all")) ) |
---|
377 | $this->_pdf->fit_image($object,0,0,""); |
---|
378 | |
---|
379 | unset($this->_objs[$object]); |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Add all active objects to the current page |
---|
384 | */ |
---|
385 | protected function _place_objects() { |
---|
386 | |
---|
387 | foreach ( $this->_objs as $obj => $props ) { |
---|
388 | $start = $props["start_page"]; |
---|
389 | $where = $props["where"]; |
---|
390 | |
---|
391 | // Place the object on this page if required |
---|
392 | if ( $this->_page_number >= $start && |
---|
393 | (($this->_page_number % 2 == 0 && $where == "even") || |
---|
394 | ($this->_page_number % 2 == 1 && $where == "odd") || |
---|
395 | ($where == "all")) ) { |
---|
396 | $this->_pdf->fit_image($obj,0,0,""); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | } |
---|
401 | |
---|
402 | function get_width() { return $this->_width; } |
---|
403 | |
---|
404 | function get_height() { return $this->_height; } |
---|
405 | |
---|
406 | function get_page_number() { return $this->_page_number; } |
---|
407 | |
---|
408 | function get_page_count() { return $this->_page_count; } |
---|
409 | |
---|
410 | function set_page_number($num) { $this->_page_number = (int)$num; } |
---|
411 | |
---|
412 | function set_page_count($count) { $this->_page_count = (int)$count; } |
---|
413 | |
---|
414 | |
---|
415 | /** |
---|
416 | * Sets the line style |
---|
417 | * |
---|
418 | * @param float width |
---|
419 | * @param string corner |
---|
420 | * @param string join |
---|
421 | * @param array dash |
---|
422 | */ |
---|
423 | protected function _set_line_style($width, $cap, $join, $dash) { |
---|
424 | |
---|
425 | if ( count($dash) == 1 ) |
---|
426 | $dash[] = $dash[0]; |
---|
427 | |
---|
428 | if ( count($dash) > 1 ) |
---|
429 | $this->_pdf->setdashpattern("dasharray={" . join(" ", $dash) . "}"); |
---|
430 | else |
---|
431 | $this->_pdf->setdash(0,0); |
---|
432 | |
---|
433 | switch ( $join ) { |
---|
434 | case "miter": |
---|
435 | $this->_pdf->setlinejoin(0); |
---|
436 | break; |
---|
437 | |
---|
438 | case "round": |
---|
439 | $this->_pdf->setlinejoin(1); |
---|
440 | break; |
---|
441 | |
---|
442 | case "bevel": |
---|
443 | $this->_pdf->setlinejoin(2); |
---|
444 | break; |
---|
445 | |
---|
446 | default: |
---|
447 | break; |
---|
448 | } |
---|
449 | |
---|
450 | switch ( $cap ) { |
---|
451 | case "butt": |
---|
452 | $this->_pdf->setlinecap(0); |
---|
453 | break; |
---|
454 | |
---|
455 | case "round": |
---|
456 | $this->_pdf->setlinecap(1); |
---|
457 | break; |
---|
458 | |
---|
459 | case "square": |
---|
460 | $this->_pdf->setlinecap(2); |
---|
461 | break; |
---|
462 | |
---|
463 | default: |
---|
464 | break; |
---|
465 | } |
---|
466 | |
---|
467 | $this->_pdf->setlinewidth($width); |
---|
468 | |
---|
469 | } |
---|
470 | |
---|
471 | /** |
---|
472 | * Sets the line color |
---|
473 | * |
---|
474 | * @param array $color array(r,g,b) |
---|
475 | */ |
---|
476 | protected function _set_stroke_color($color) { |
---|
477 | if($this->_last_stroke_color == $color) |
---|
478 | return; |
---|
479 | |
---|
480 | $this->_last_stroke_color = $color; |
---|
481 | |
---|
482 | list($r,$g,$b) = $color; |
---|
483 | $this->_pdf->setcolor("stroke", "rgb", $r, $g, $b, 0); |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | * Sets the fill color |
---|
488 | * |
---|
489 | * @param array $color array(r,g,b) |
---|
490 | */ |
---|
491 | protected function _set_fill_color($color) { |
---|
492 | if($this->_last_fill_color == $color) |
---|
493 | return; |
---|
494 | |
---|
495 | $this->_last_fill_color = $color; |
---|
496 | |
---|
497 | list($r,$g,$b) = $color; |
---|
498 | $this->_pdf->setcolor("fill", "rgb", $r, $g, $b, 0); |
---|
499 | } |
---|
500 | |
---|
501 | /** |
---|
502 | * Loads a specific font and stores the corresponding descriptor. |
---|
503 | * |
---|
504 | * @param string $font |
---|
505 | * @return int the font descriptor for the font |
---|
506 | */ |
---|
507 | protected function _load_font($font, $encoding = null, $options = "") { |
---|
508 | |
---|
509 | // Check if the font is a native PDF font |
---|
510 | // Embed non-native fonts |
---|
511 | $native_fonts = array("courier", "courier-bold", "courier-oblique", "courier-boldoblique", |
---|
512 | "helvetica", "helvetica-bold", "helvetica-oblique", "helvetica-boldoblique", |
---|
513 | "times-roman", "times-bold", "times-italic", "times-bolditalic", |
---|
514 | "symbol", "zapfdinbats"); |
---|
515 | |
---|
516 | $test = strtolower(basename($font)); |
---|
517 | if ( in_array($test, $native_fonts) ) { |
---|
518 | $font = basename($font); |
---|
519 | |
---|
520 | } else { |
---|
521 | // Embed non-native fonts |
---|
522 | $options .= " embedding=true"; |
---|
523 | } |
---|
524 | |
---|
525 | if ( is_null($encoding) ) { |
---|
526 | |
---|
527 | // Unicode encoding is only available for the commerical |
---|
528 | // version of PDFlib and not PDFlib-Lite |
---|
529 | if ( defined("DOMPDF_PDFLIB_LICENSE") ) |
---|
530 | $encoding = "unicode"; |
---|
531 | else |
---|
532 | $encoding = "auto"; |
---|
533 | |
---|
534 | } |
---|
535 | |
---|
536 | $key = $font .":". $encoding .":". $options; |
---|
537 | |
---|
538 | if ( isset($this->_fonts[$key]) ) |
---|
539 | return $this->_fonts[$key]; |
---|
540 | |
---|
541 | else { |
---|
542 | |
---|
543 | $this->_fonts[$key] = $this->_pdf->load_font($font, $encoding, $options); |
---|
544 | return $this->_fonts[$key]; |
---|
545 | |
---|
546 | } |
---|
547 | |
---|
548 | } |
---|
549 | |
---|
550 | /** |
---|
551 | * Remaps y coords from 4th to 1st quadrant |
---|
552 | * |
---|
553 | * @param float $y |
---|
554 | * @return float |
---|
555 | */ |
---|
556 | protected function y($y) { return $this->_height - $y; } |
---|
557 | |
---|
558 | //........................................................................ |
---|
559 | |
---|
560 | function line($x1, $y1, $x2, $y2, $color, $width, $style = null) { |
---|
561 | $this->_set_line_style($width, "butt", "", $style); |
---|
562 | $this->_set_stroke_color($color); |
---|
563 | |
---|
564 | $y1 = $this->y($y1); |
---|
565 | $y2 = $this->y($y2); |
---|
566 | |
---|
567 | $this->_pdf->moveto($x1,$y1); |
---|
568 | $this->_pdf->lineto($x2, $y2); |
---|
569 | $this->_pdf->stroke(); |
---|
570 | } |
---|
571 | |
---|
572 | //........................................................................ |
---|
573 | |
---|
574 | function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) { |
---|
575 | $this->_set_stroke_color($color); |
---|
576 | $this->_set_line_style($width, "square", "miter", $style); |
---|
577 | |
---|
578 | $y1 = $this->y($y1) - $h; |
---|
579 | |
---|
580 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
581 | $this->_pdf->stroke(); |
---|
582 | } |
---|
583 | |
---|
584 | //........................................................................ |
---|
585 | |
---|
586 | function filled_rectangle($x1, $y1, $w, $h, $color) { |
---|
587 | $this->_set_fill_color($color); |
---|
588 | |
---|
589 | $y1 = $this->y($y1) - $h; |
---|
590 | |
---|
591 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
592 | $this->_pdf->fill(); |
---|
593 | } |
---|
594 | |
---|
595 | //........................................................................ |
---|
596 | |
---|
597 | function polygon($points, $color, $width = null, $style = null, $fill = false) { |
---|
598 | |
---|
599 | $this->_set_fill_color($color); |
---|
600 | $this->_set_stroke_color($color); |
---|
601 | |
---|
602 | if ( !$fill && isset($width) ) |
---|
603 | $this->_set_line_style($width, "square", "miter", $style); |
---|
604 | |
---|
605 | $y = $this->y(array_pop($points)); |
---|
606 | $x = array_pop($points); |
---|
607 | $this->_pdf->moveto($x,$y); |
---|
608 | |
---|
609 | while (count($points) > 1) { |
---|
610 | $y = $this->y(array_pop($points)); |
---|
611 | $x = array_pop($points); |
---|
612 | $this->_pdf->lineto($x,$y); |
---|
613 | } |
---|
614 | |
---|
615 | if ( $fill ) |
---|
616 | $this->_pdf->fill(); |
---|
617 | else |
---|
618 | $this->_pdf->closepath_stroke(); |
---|
619 | } |
---|
620 | |
---|
621 | //........................................................................ |
---|
622 | |
---|
623 | function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) { |
---|
624 | |
---|
625 | $this->_set_fill_color($color); |
---|
626 | $this->_set_stroke_color($color); |
---|
627 | |
---|
628 | if ( !$fill && isset($width) ) |
---|
629 | $this->_set_line_style($width, "round", "round", $style); |
---|
630 | |
---|
631 | $y = $this->y($y); |
---|
632 | |
---|
633 | $this->_pdf->circle($x, $y, $r); |
---|
634 | |
---|
635 | if ( $fill ) |
---|
636 | $this->_pdf->fill(); |
---|
637 | else |
---|
638 | $this->_pdf->stroke(); |
---|
639 | |
---|
640 | } |
---|
641 | |
---|
642 | //........................................................................ |
---|
643 | |
---|
644 | function image($img_url, $img_type, $x, $y, $w, $h) { |
---|
645 | $w = (int)$w; |
---|
646 | $h = (int)$h; |
---|
647 | |
---|
648 | $img_type = strtolower($img_type); |
---|
649 | |
---|
650 | if ( $img_type == "jpg" ) |
---|
651 | $img_type = "jpeg"; |
---|
652 | |
---|
653 | if ( isset($this->_imgs[$img_url]) ) |
---|
654 | $img = $this->_imgs[$img_url]; |
---|
655 | |
---|
656 | else { |
---|
657 | |
---|
658 | $img = $this->_imgs[$img_url] = $this->_pdf->load_image($img_type, $img_url, ""); |
---|
659 | } |
---|
660 | |
---|
661 | $y = $this->y($y) - $h; |
---|
662 | $this->_pdf->fit_image($img, $x, $y, 'boxsize={'. "$w $h" .'} fitmethod=entire'); |
---|
663 | |
---|
664 | } |
---|
665 | |
---|
666 | //........................................................................ |
---|
667 | |
---|
668 | function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0) { |
---|
669 | $fh = $this->_load_font($font); |
---|
670 | |
---|
671 | $this->_pdf->setfont($fh, $size); |
---|
672 | $this->_set_fill_color($color); |
---|
673 | |
---|
674 | $y = $this->y($y) - Font_Metrics::get_font_height($font, $size); |
---|
675 | |
---|
676 | $adjust = (float)$adjust; |
---|
677 | $angle = -(float)$angle; |
---|
678 | |
---|
679 | //$this->_pdf->fit_textline(utf8_decode($text), $x, $y, "rotate=$angle wordspacing=$adjust"); |
---|
680 | $this->_pdf->fit_textline($text, $x, $y, "rotate=$angle wordspacing=$adjust"); |
---|
681 | |
---|
682 | } |
---|
683 | |
---|
684 | //........................................................................ |
---|
685 | |
---|
686 | /** |
---|
687 | * Add a named destination (similar to <a name="foo">...</a> in html) |
---|
688 | * |
---|
689 | * @param string $anchorname The name of the named destination |
---|
690 | */ |
---|
691 | function add_named_dest($anchorname) { |
---|
692 | $this->_pdf->add_nameddest($anchorname,""); |
---|
693 | } |
---|
694 | |
---|
695 | //........................................................................ |
---|
696 | |
---|
697 | /** |
---|
698 | * Add a link to the pdf |
---|
699 | * |
---|
700 | * @param string $url The url to link to |
---|
701 | * @param float $x The x position of the link |
---|
702 | * @param float $y The y position of the link |
---|
703 | * @param float $width The width of the link |
---|
704 | * @param float $height The height of the link |
---|
705 | */ |
---|
706 | function add_link($url, $x, $y, $width, $height) { |
---|
707 | |
---|
708 | $y = $this->y($y) - $height; |
---|
709 | if ( strpos($url, '#') === 0 ) { |
---|
710 | // Local link |
---|
711 | $name = substr($url,1); |
---|
712 | if ( $name ) |
---|
713 | $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} destname=". substr($url,1) . " linewidth=0"); |
---|
714 | } else { |
---|
715 | |
---|
716 | list($proto, $host, $path, $file) = explode_url($url); |
---|
717 | |
---|
718 | if ( $proto == "" || $proto == "file://" ) |
---|
719 | return; // Local links are not allowed |
---|
720 | $url = build_url($proto, $host, $path, $file); |
---|
721 | $url = str_replace("=", "%3D", rawurldecode($url)); |
---|
722 | |
---|
723 | $action = $this->_pdf->create_action("URI", "url=" . $url); |
---|
724 | $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} action={activate=$action} linewidth=0"); |
---|
725 | } |
---|
726 | } |
---|
727 | |
---|
728 | //........................................................................ |
---|
729 | |
---|
730 | function get_text_width($text, $font, $size, $spacing = 0) { |
---|
731 | $fh = $this->_load_font($font); |
---|
732 | |
---|
733 | // Determine the additional width due to extra spacing |
---|
734 | $num_spaces = mb_substr_count($text," "); |
---|
735 | $delta = $spacing * $num_spaces; |
---|
736 | return $this->_pdf->stringwidth($text, $fh, $size) + $delta; |
---|
737 | } |
---|
738 | |
---|
739 | //........................................................................ |
---|
740 | |
---|
741 | function get_font_height($font, $size) { |
---|
742 | |
---|
743 | $fh = $this->_load_font($font); |
---|
744 | |
---|
745 | $this->_pdf->setfont($fh, $size); |
---|
746 | |
---|
747 | $asc = $this->_pdf->get_value("ascender", $fh); |
---|
748 | $desc = $this->_pdf->get_value("descender", $fh); |
---|
749 | |
---|
750 | // $desc is usually < 0, |
---|
751 | return self::FONT_HEIGHT_SCALE * $size * ($asc - $desc); |
---|
752 | } |
---|
753 | |
---|
754 | //........................................................................ |
---|
755 | |
---|
756 | function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), |
---|
757 | $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) { |
---|
758 | $this->_page_text[] = compact("x", "y", "text", "font", "size", "color", "adjust", "angle"); |
---|
759 | |
---|
760 | } |
---|
761 | |
---|
762 | //........................................................................ |
---|
763 | |
---|
764 | function new_page() { |
---|
765 | |
---|
766 | // Add objects to the current page |
---|
767 | $this->_place_objects(); |
---|
768 | |
---|
769 | $this->_pdf->suspend_page(""); |
---|
770 | $this->_pdf->begin_page_ext($this->_width, $this->_height, ""); |
---|
771 | $this->_page_number = ++$this->_page_count; |
---|
772 | |
---|
773 | } |
---|
774 | |
---|
775 | //........................................................................ |
---|
776 | |
---|
777 | /** |
---|
778 | * Add text to each page after rendering is complete |
---|
779 | */ |
---|
780 | protected function _add_page_text() { |
---|
781 | |
---|
782 | if ( !count($this->_page_text) ) |
---|
783 | return; |
---|
784 | |
---|
785 | $this->_pdf->suspend_page(""); |
---|
786 | |
---|
787 | for ($p = 1; $p <= $this->_page_count; $p++) { |
---|
788 | |
---|
789 | $this->_pdf->resume_page("pagenumber=$p"); |
---|
790 | |
---|
791 | foreach ($this->_page_text as $pt) { |
---|
792 | extract($pt); |
---|
793 | |
---|
794 | $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"), |
---|
795 | array($p, $this->_page_count), $text); |
---|
796 | |
---|
797 | $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle); |
---|
798 | |
---|
799 | } |
---|
800 | |
---|
801 | $this->_pdf->suspend_page(""); |
---|
802 | |
---|
803 | } |
---|
804 | |
---|
805 | $this->_pdf->resume_page("pagenumber=".$this->_page_number); |
---|
806 | } |
---|
807 | |
---|
808 | //........................................................................ |
---|
809 | |
---|
810 | function stream($filename, $options = null) { |
---|
811 | |
---|
812 | // Add page text |
---|
813 | $this->_add_page_text(); |
---|
814 | |
---|
815 | if ( isset($options["compress"]) && $options["compress"] != 1 ) |
---|
816 | $this->_pdf->set_value("compress", 0); |
---|
817 | else |
---|
818 | $this->_pdf->set_value("compress", 6); |
---|
819 | |
---|
820 | $this->_close(); |
---|
821 | |
---|
822 | if ( self::$IN_MEMORY ) { |
---|
823 | $data = $this->_pdf->get_buffer(); |
---|
824 | $size = strlen($data); |
---|
825 | |
---|
826 | } else |
---|
827 | $size = filesize($this->_file); |
---|
828 | |
---|
829 | |
---|
830 | $filename = str_replace(array("\n","'"),"", $filename); |
---|
831 | $attach = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline"; |
---|
832 | |
---|
833 | header("Cache-Control: private"); |
---|
834 | header("Content-type: application/pdf"); |
---|
835 | header("Content-Disposition: $attach; filename=\"$filename\""); |
---|
836 | |
---|
837 | //header("Content-length: " . $size); |
---|
838 | |
---|
839 | if ( self::$IN_MEMORY ) |
---|
840 | echo $data; |
---|
841 | |
---|
842 | else { |
---|
843 | |
---|
844 | // Chunked readfile() |
---|
845 | $chunk = (1 << 21); // 2 MB |
---|
846 | $fh = fopen($this->_file, "rb"); |
---|
847 | if ( !$fh ) |
---|
848 | throw new DOMPDF_Exception("Unable to load temporary PDF file: " . $this->_file); |
---|
849 | |
---|
850 | while ( !feof($fh) ) |
---|
851 | echo fread($fh,$chunk); |
---|
852 | fclose($fh); |
---|
853 | unlink($this->_file); |
---|
854 | $this->_file = null; |
---|
855 | } |
---|
856 | |
---|
857 | flush(); |
---|
858 | |
---|
859 | |
---|
860 | } |
---|
861 | |
---|
862 | //........................................................................ |
---|
863 | |
---|
864 | function output($options = null) { |
---|
865 | |
---|
866 | // Add page text |
---|
867 | $this->_add_page_text(); |
---|
868 | |
---|
869 | if ( isset($options["compress"]) && $options["compress"] != 1 ) |
---|
870 | $this->_pdf->set_value("compress", 0); |
---|
871 | else |
---|
872 | $this->_pdf->set_value("compress", 6); |
---|
873 | |
---|
874 | $this->_close(); |
---|
875 | |
---|
876 | if ( self::$IN_MEMORY ) |
---|
877 | $data = $this->_pdf->get_buffer(); |
---|
878 | |
---|
879 | else { |
---|
880 | $data = file_get_contents($this->_file); |
---|
881 | unlink($this->_file); |
---|
882 | $this->_file = null; |
---|
883 | } |
---|
884 | |
---|
885 | return $data; |
---|
886 | } |
---|
887 | } |
---|
888 | |
---|
889 | // Workaround for idiotic limitation on statics... |
---|
890 | PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES; |
---|
891 | ?> |
---|