1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile$ |
---|
6 | * Created on: 2004-08-04 |
---|
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: tcpdf_adapter.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
41 | |
---|
42 | require_once(DOMPDF_LIB_DIR . '/tcpdf/tcpdf.php'); |
---|
43 | |
---|
44 | /** |
---|
45 | * TCPDF PDF Rendering interface |
---|
46 | * |
---|
47 | * TCPDF_Adapter provides a simple, stateless interface to TCPDF. |
---|
48 | * |
---|
49 | * Unless otherwise mentioned, all dimensions are in points (1/72 in). |
---|
50 | * The coordinate origin is in the top left corner and y values |
---|
51 | * increase downwards. |
---|
52 | * |
---|
53 | * See {@link http://tcpdf.sourceforge.net} for more information on |
---|
54 | * the underlying TCPDF class. |
---|
55 | * |
---|
56 | * @package dompdf |
---|
57 | */ |
---|
58 | |
---|
59 | class TCPDF_Adapter implements Canvas { |
---|
60 | |
---|
61 | /** |
---|
62 | * Dimensions of paper sizes in points |
---|
63 | * |
---|
64 | * @var array; |
---|
65 | */ |
---|
66 | static public $PAPER_SIZES = array(); // Set to |
---|
67 | // CPDF_Adapter::$PAPER_SIZES below. |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * Instance of the TCPDF class |
---|
72 | * |
---|
73 | * @var TCPDF |
---|
74 | */ |
---|
75 | private $_pdf; |
---|
76 | |
---|
77 | /** |
---|
78 | * PDF width in points |
---|
79 | * |
---|
80 | * @var float |
---|
81 | */ |
---|
82 | private $_width; |
---|
83 | |
---|
84 | /** |
---|
85 | * PDF height in points |
---|
86 | * |
---|
87 | * @var float |
---|
88 | */ |
---|
89 | private $_height; |
---|
90 | |
---|
91 | /** |
---|
92 | * Last fill colour used |
---|
93 | * |
---|
94 | * @var array |
---|
95 | */ |
---|
96 | private $_last_fill_color; |
---|
97 | |
---|
98 | /** |
---|
99 | * Last stroke colour used |
---|
100 | * |
---|
101 | * @var array |
---|
102 | */ |
---|
103 | private $_last_stroke_color; |
---|
104 | |
---|
105 | /** |
---|
106 | * Last line width used |
---|
107 | * |
---|
108 | * @var float |
---|
109 | */ |
---|
110 | private $_last_line_width; |
---|
111 | |
---|
112 | /** |
---|
113 | * Total number of pages |
---|
114 | * |
---|
115 | * @var int |
---|
116 | */ |
---|
117 | private $_page_count; |
---|
118 | |
---|
119 | /** |
---|
120 | * Text to display on every page |
---|
121 | * |
---|
122 | * @var array |
---|
123 | */ |
---|
124 | private $_page_text; |
---|
125 | |
---|
126 | /** |
---|
127 | * Array of pages for accessing after initial rendering is complete |
---|
128 | * |
---|
129 | * @var array |
---|
130 | */ |
---|
131 | private $_pages; |
---|
132 | |
---|
133 | /** |
---|
134 | * Class constructor |
---|
135 | * |
---|
136 | * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or |
---|
137 | * an array(xmin,ymin,xmax,ymax) |
---|
138 | * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') |
---|
139 | */ |
---|
140 | function __construct($paper = "letter", $orientation = "portrait") { |
---|
141 | |
---|
142 | if ( is_array($paper) ) |
---|
143 | $size = $paper; |
---|
144 | else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) |
---|
145 | $size = self::$PAPER_SIZE[$paper]; |
---|
146 | else |
---|
147 | $size = self::$PAPER_SIZE["letter"]; |
---|
148 | |
---|
149 | if ( mb_strtolower($orientation) == "landscape" ) { |
---|
150 | $a = $size[3]; |
---|
151 | $size[3] = $size[2]; |
---|
152 | $size[2] = $a; |
---|
153 | } |
---|
154 | |
---|
155 | $this->_width = $size[2] - $size[0]; |
---|
156 | $this->_height = $size[3] - $size[1]; |
---|
157 | |
---|
158 | $this->_pdf = new TCPDF("P", "pt", array($this->_width, $this->_height)); |
---|
159 | $this->_pdf->Setcreator("DOMPDF Converter"); |
---|
160 | |
---|
161 | $this->_pdf->AddPage(); |
---|
162 | |
---|
163 | $this->_page_number = $this->_page_count = 1; |
---|
164 | $this->_page_text = array(); |
---|
165 | |
---|
166 | $this->_last_fill_color = |
---|
167 | $this->_last_stroke_color = |
---|
168 | $this->_last_line_width = null; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Remaps y coords from 4th to 1st quadrant |
---|
174 | * |
---|
175 | * @param float $y |
---|
176 | * @return float |
---|
177 | */ |
---|
178 | protected function y($y) { return $this->_height - $y; } |
---|
179 | |
---|
180 | /** |
---|
181 | * Sets the stroke colour |
---|
182 | * |
---|
183 | * @param array $color |
---|
184 | */ |
---|
185 | protected function _set_stroke_colour($colour) { |
---|
186 | $colour[0] = round(255 * $colour[0]); |
---|
187 | $colour[1] = round(255 * $colour[1]); |
---|
188 | $colour[2] = round(255 * $colour[2]); |
---|
189 | |
---|
190 | if ( is_null($this->_last_stroke_color) || $color != $this->_last_stroke_color ) { |
---|
191 | $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]); |
---|
192 | $this->_last_stroke_color = $color; |
---|
193 | } |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Sets the fill colour |
---|
199 | * |
---|
200 | * @param array $color |
---|
201 | */ |
---|
202 | protected function _set_fill_colour($colour) { |
---|
203 | $colour[0] = round(255 * $colour[0]); |
---|
204 | $colour[1] = round(255 * $colour[1]); |
---|
205 | $colour[2] = round(255 * $colour[2]); |
---|
206 | |
---|
207 | if ( is_null($this->_last_fill_color) || $color != $this->_last_fill_color ) { |
---|
208 | $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]); |
---|
209 | $this->_last_fill_color = $color; |
---|
210 | } |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Return the TCPDF instance |
---|
216 | * |
---|
217 | * @return TCPDF |
---|
218 | */ |
---|
219 | function get_tcpdf() { return $this->_pdf; } |
---|
220 | |
---|
221 | /** |
---|
222 | * Returns the current page number |
---|
223 | * |
---|
224 | * @return int |
---|
225 | */ |
---|
226 | function get_page_number() { |
---|
227 | return $this->_page_number; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Returns the total number of pages |
---|
232 | * |
---|
233 | * @return int |
---|
234 | */ |
---|
235 | function get_page_count() { |
---|
236 | return $this->_page_count; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Sets the total number of pages |
---|
241 | * |
---|
242 | * @param int $count |
---|
243 | */ |
---|
244 | function set_page_count($count) { |
---|
245 | $this->_page_count = (int)$count; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Draws a line from x1,y1 to x2,y2 |
---|
250 | * |
---|
251 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
252 | * See {@link Cpdf::setLineStyle()} for a description of the format of the |
---|
253 | * $style parameter (aka dash). |
---|
254 | * |
---|
255 | * @param float $x1 |
---|
256 | * @param float $y1 |
---|
257 | * @param float $x2 |
---|
258 | * @param float $y2 |
---|
259 | * @param array $color |
---|
260 | * @param float $width |
---|
261 | * @param array $style |
---|
262 | */ |
---|
263 | function line($x1, $y1, $x2, $y2, $color, $width, $style = null) { |
---|
264 | |
---|
265 | if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) { |
---|
266 | $this->_pdf->SetLineWidth($width); |
---|
267 | $this->_last_line_width = $width; |
---|
268 | } |
---|
269 | |
---|
270 | $this->_set_stroke_colour($color); |
---|
271 | |
---|
272 | // FIXME: ugh, need to handle different styles here |
---|
273 | $this->_pdf->line($x1, $y1, $x2, $y2); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * Draws a rectangle at x1,y1 with width w and height h |
---|
278 | * |
---|
279 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
280 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
281 | * parameter (aka dash) |
---|
282 | * |
---|
283 | * @param float $x1 |
---|
284 | * @param float $y1 |
---|
285 | * @param float $w |
---|
286 | * @param float $h |
---|
287 | * @param array $color |
---|
288 | * @param float $width |
---|
289 | * @param array $style |
---|
290 | */ |
---|
291 | function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) { |
---|
292 | |
---|
293 | if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) { |
---|
294 | $this->_pdf->SetLineWidth($width); |
---|
295 | $this->_last_line_width = $width; |
---|
296 | } |
---|
297 | |
---|
298 | $this->_set_stroke_colour($color); |
---|
299 | |
---|
300 | // FIXME: ugh, need to handle styles here |
---|
301 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Draws a filled rectangle at x1,y1 with width w and height h |
---|
307 | * |
---|
308 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
309 | * |
---|
310 | * @param float $x1 |
---|
311 | * @param float $y1 |
---|
312 | * @param float $w |
---|
313 | * @param float $h |
---|
314 | * @param array $color |
---|
315 | */ |
---|
316 | function filled_rectangle($x1, $y1, $w, $h, $color) { |
---|
317 | |
---|
318 | $this->_set_fill_colour($color); |
---|
319 | |
---|
320 | // FIXME: ugh, need to handle styles here |
---|
321 | $this->_pdf->rect($x1, $y1, $w, $h, "F"); |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | * Draws a polygon |
---|
326 | * |
---|
327 | * The polygon is formed by joining all the points stored in the $points |
---|
328 | * array. $points has the following structure: |
---|
329 | * <code> |
---|
330 | * array(0 => x1, |
---|
331 | * 1 => y1, |
---|
332 | * 2 => x2, |
---|
333 | * 3 => y2, |
---|
334 | * ... |
---|
335 | * ); |
---|
336 | * </code> |
---|
337 | * |
---|
338 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
339 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
340 | * parameter (aka dash) |
---|
341 | * |
---|
342 | * @param array $points |
---|
343 | * @param array $color |
---|
344 | * @param float $width |
---|
345 | * @param array $style |
---|
346 | * @param bool $fill Fills the polygon if true |
---|
347 | */ |
---|
348 | function polygon($points, $color, $width = null, $style = null, $fill = false) { |
---|
349 | // FIXME: FPDF sucks |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * Draws a circle at $x,$y with radius $r |
---|
354 | * |
---|
355 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
356 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
357 | * parameter (aka dash) |
---|
358 | * |
---|
359 | * @param float $x |
---|
360 | * @param float $y |
---|
361 | * @param float $r |
---|
362 | * @param array $color |
---|
363 | * @param float $width |
---|
364 | * @param array $style |
---|
365 | * @param bool $fill Fills the circle if true |
---|
366 | */ |
---|
367 | function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false){} |
---|
368 | |
---|
369 | /** |
---|
370 | * Add an image to the pdf. |
---|
371 | * |
---|
372 | * The image is placed at the specified x and y coordinates with the |
---|
373 | * given width and height. |
---|
374 | * |
---|
375 | * @param string $img_url the path to the image |
---|
376 | * @param string $img_type the type (e.g. extension) of the image |
---|
377 | * @param float $x x position |
---|
378 | * @param float $y y position |
---|
379 | * @param int $w width (in pixels) |
---|
380 | * @param int $h height (in pixels) |
---|
381 | */ |
---|
382 | function image($img_url, $img_type, $x, $y, $w, $h){} |
---|
383 | |
---|
384 | /** |
---|
385 | * Writes text at the specified x and y coordinates |
---|
386 | * |
---|
387 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
388 | * |
---|
389 | * @param float $x |
---|
390 | * @param float $y |
---|
391 | * @param string $text the text to write |
---|
392 | * @param string $font the font file to use |
---|
393 | * @param float $size the font size, in points |
---|
394 | * @param array $color |
---|
395 | * @param float $adjust word spacing adjustment |
---|
396 | */ |
---|
397 | function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0){} |
---|
398 | |
---|
399 | /** |
---|
400 | * Add a named destination (similar to <a name="foo">...</a> in html) |
---|
401 | * |
---|
402 | * @param string $anchorname The name of the named destination |
---|
403 | */ |
---|
404 | function add_named_dest($anchorname){} |
---|
405 | |
---|
406 | /** |
---|
407 | * Add a link to the pdf |
---|
408 | * |
---|
409 | * @param string $url The url to link to |
---|
410 | * @param float $x The x position of the link |
---|
411 | * @param float $y The y position of the link |
---|
412 | * @param float $width The width of the link |
---|
413 | * @param float $height The height of the link |
---|
414 | */ |
---|
415 | function add_link($url, $x, $y, $width, $height){} |
---|
416 | |
---|
417 | /** |
---|
418 | * Calculates text size, in points |
---|
419 | * |
---|
420 | * @param string $text the text to be sized |
---|
421 | * @param string $font the desired font |
---|
422 | * @param float $size the desired font size |
---|
423 | * @param float $spacing word spacing, if any |
---|
424 | * @return float |
---|
425 | */ |
---|
426 | function get_text_width($text, $font, $size, $spacing = 0){} |
---|
427 | |
---|
428 | /** |
---|
429 | * Calculates font height, in points |
---|
430 | * |
---|
431 | * @param string $font |
---|
432 | * @param float $size |
---|
433 | * @return float |
---|
434 | */ |
---|
435 | function get_font_height($font, $size){} |
---|
436 | |
---|
437 | |
---|
438 | /** |
---|
439 | * Starts a new page |
---|
440 | * |
---|
441 | * Subsequent drawing operations will appear on the new page. |
---|
442 | */ |
---|
443 | function new_page(){} |
---|
444 | |
---|
445 | /** |
---|
446 | * Streams the PDF directly to the browser |
---|
447 | * |
---|
448 | * @param string $filename the name of the PDF file |
---|
449 | * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0 |
---|
450 | */ |
---|
451 | function stream($filename, $options = null){} |
---|
452 | |
---|
453 | /** |
---|
454 | * Returns the PDF as a string |
---|
455 | * |
---|
456 | * @param array $options associative array: 'compress' => 1 or 0 |
---|
457 | * @return string |
---|
458 | */ |
---|
459 | function output($options = null){} |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | // Workaround for idiotic limitation on statics... |
---|
464 | PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES; |
---|
465 | ?> |
---|