Merge branch 'MDL-29847_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / lib / tcpdf / tcpdf.php
blobb9a17725df705b6dfe056ef232179828f24711a1
1 <?php
2 //============================================================+
3 // File name : tcpdf.php
4 // Version : 5.9.133
5 // Begin : 2002-08-03
6 // Last Update : 2011-10-26
7 // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
8 // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3 + YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE GENERATED PDF DOCUMENTS.
9 // -------------------------------------------------------------------
11 // This file is part of TCPDF software library.
13 // TCPDF is free software: you can redistribute it and/or modify it
14 // under the terms of the GNU Lesser General Public License as
15 // published by the Free Software Foundation, either version 3 of the
16 // License, or (at your option) any later version. Additionally,
17 // YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE
18 // GENERATED PDF DOCUMENTS.
20 // TCPDF is distributed in the hope that it will be useful, but
21 // WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 // See the GNU Lesser General Public License for more details.
25 // You should have received a copy of the License
26 // along with TCPDF. If not, see
27 // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
29 // See LICENSE.TXT file for more information.
30 // -------------------------------------------------------------------
32 // Description :
33 // This is a PHP class for generating PDF documents without requiring external extensions.
35 // NOTE:
36 // This class was originally derived in 2002 from the Public
37 // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
38 // but now is almost entirely rewritten and contains thousands of
39 // new lines of code and hundreds new features.
41 // Main features:
42 // * no external libraries are required for the basic functions;
43 // * all standard page formats, custom page formats, custom margins and units of measure;
44 // * UTF-8 Unicode and Right-To-Left languages;
45 // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
46 // * font subsetting;
47 // * methods to publish some XHTML + CSS code, Javascript and Forms;
48 // * images, graphic (geometric figures) and transformation methods;
49 // * supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)
50 // * 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;
51 // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
52 // * automatic page header and footer management;
53 // * document encryption up to 256 bit and digital signature certifications;
54 // * transactions to UNDO commands;
55 // * PDF annotations, including links, text and file attachments;
56 // * text rendering modes (fill, stroke and clipping);
57 // * multiple columns mode;
58 // * no-write page regions;
59 // * bookmarks, named destinations and table of content;
60 // * text hyphenation;
61 // * text stretching and spacing (tracking/kerning);
62 // * automatic page break, line break and text alignments including justification;
63 // * automatic page numbering and page groups;
64 // * move and delete pages;
65 // * page compression (requires php-zlib extension);
66 // * XOBject Templates;
67 // * Layers and object visibility.
68 // * PDF/A-1b support.
70 // -----------------------------------------------------------
71 // THANKS TO:
73 // Olivier Plathey (http://www.fpdf.org) for original FPDF.
74 // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
75 // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
76 // Warren Sherliker (wsherliker@gmail.com) for better image handling.
77 // dullus for text Justification.
78 // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
79 // Patrick Benny for text stretch suggestion on Cell().
80 // Johannes Güntert for JavaScript support.
81 // Denis Van Nuffelen for Dynamic Form.
82 // Jacek Czekaj for multibyte justification
83 // Anthony Ferrara for the reintroduction of legacy image methods.
84 // Sourceforge user 1707880 (hucste) for line-trough mode.
85 // Larry Stanbery for page groups.
86 // Martin Hall-May for transparency.
87 // Aaron C. Spike for Polycurve method.
88 // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
89 // Moritz Wagner and Andreas Wurmser for graphic functions.
90 // Andrew Whitehead for core fonts support.
91 // Esteban Joël Marín for OpenType font conversion.
92 // Teus Hagen for several suggestions and fixes.
93 // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
94 // Kosmas Papachristos for some CSS improvements.
95 // Marcel Partap for some fixes.
96 // Won Kyu Park for several suggestions, fixes and patches.
97 // Dominik Dzienia for QR-code support.
98 // Laurent Minguet for some suggestions.
99 // Christian Deligant for some suggestions and fixes.
100 // Anyone that has reported a bug or sent a suggestion.
101 //============================================================+
104 * @file
105 * This is a PHP class for generating PDF documents without requiring external extensions.<br>
106 * TCPDF project (http://www.tcpdf.org) was originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
107 * <h3>TCPDF main features are:</h3>
108 * <ul>
109 * <li>no external libraries are required for the basic functions;</li>
110 * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
111 * <li>UTF-8 Unicode and Right-To-Left languages;</li>
112 * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
113 * <li>font subsetting;</li>
114 * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
115 * <li>images, graphic (geometric figures) and transformation methods;
116 * <li>supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)</li>
117 * <li>1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;</li>
118 * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
119 * <li>automatic page header and footer management;</li>
120 * <li>document encryption up to 256 bit and digital signature certifications;</li>
121 * <li>transactions to UNDO commands;</li>
122 * <li>PDF annotations, including links, text and file attachments;</li>
123 * <li>text rendering modes (fill, stroke and clipping);</li>
124 * <li>multiple columns mode;</li>
125 * <li>no-write page regions;</li>
126 * <li>bookmarks, named destinations and table of content;</li>
127 * <li>text hyphenation;</li>
128 * <li>text stretching and spacing (tracking/kerning);</li>
129 * <li>automatic page break, line break and text alignments including justification;</li>
130 * <li>automatic page numbering and page groups;</li>
131 * <li>move and delete pages;</li>
132 * <li>page compression (requires php-zlib extension);</li>
133 * <li>XOBject Templates;</li>
134 * <li>Layers and object visibility;</li>
135 * <li>PDF/A-1b support.</li>
136 * </ul>
137 * Tools to encode your unicode fonts are on fonts/utils directory.</p>
138 * @package com.tecnick.tcpdf
139 * @author Nicola Asuni
140 * @version 5.9.133
143 // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
144 require_once(dirname(__FILE__).'/config/tcpdf_config.php');
147 * @class TCPDF
148 * PHP class for generating PDF documents without requiring external extensions.
149 * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
150 * @package com.tecnick.tcpdf
151 * @brief PHP class for generating PDF documents without requiring external extensions.
152 * @version 5.9.133
153 * @author Nicola Asuni - info@tecnick.com
155 class TCPDF {
157 // private properties
160 * Current TCPDF version.
161 * @private
163 private $tcpdf_version = '5.9.133';
165 // Protected properties
168 * Current page number.
169 * @protected
171 protected $page;
174 * Current object number.
175 * @protected
177 protected $n;
180 * Array of object offsets.
181 * @protected
183 protected $offsets;
186 * Buffer holding in-memory PDF.
187 * @protected
189 protected $buffer;
192 * Array containing pages.
193 * @protected
195 protected $pages = array();
198 * Current document state.
199 * @protected
201 protected $state;
204 * Compression flag.
205 * @protected
207 protected $compress;
210 * Current page orientation (P = Portrait, L = Landscape).
211 * @protected
213 protected $CurOrientation;
216 * Page dimensions.
217 * @protected
219 protected $pagedim = array();
222 * Scale factor (number of points in user unit).
223 * @protected
225 protected $k;
228 * Width of page format in points.
229 * @protected
231 protected $fwPt;
234 * Height of page format in points.
235 * @protected
237 protected $fhPt;
240 * Current width of page in points.
241 * @protected
243 protected $wPt;
246 * Current height of page in points.
247 * @protected
249 protected $hPt;
252 * Current width of page in user unit.
253 * @protected
255 protected $w;
258 * Current height of page in user unit.
259 * @protected
261 protected $h;
264 * Left margin.
265 * @protected
267 protected $lMargin;
270 * Top margin.
271 * @protected
273 protected $tMargin;
276 * Right margin.
277 * @protected
279 protected $rMargin;
282 * Page break margin.
283 * @protected
285 protected $bMargin;
288 * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
289 * @since 5.9.000 (2010-10-03)
290 * @protected
292 protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
295 * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
296 * @since 5.9.000 (2010-10-04)
297 * @protected
299 protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
302 * Current horizontal position in user unit for cell positioning.
303 * @protected
305 protected $x;
308 * Current vertical position in user unit for cell positioning.
309 * @protected
311 protected $y;
314 * Height of last cell printed.
315 * @protected
317 protected $lasth;
320 * Line width in user unit.
321 * @protected
323 protected $LineWidth;
326 * Array of standard font names.
327 * @protected
329 protected $CoreFonts;
332 * Array of used fonts.
333 * @protected
335 protected $fonts = array();
338 * Array of font files.
339 * @protected
341 protected $FontFiles = array();
344 * Array of encoding differences.
345 * @protected
347 protected $diffs = array();
350 * Array of used images.
351 * @protected
353 protected $images = array();
356 * Array of Annotations in pages.
357 * @protected
359 protected $PageAnnots = array();
362 * Array of internal links.
363 * @protected
365 protected $links = array();
368 * Current font family.
369 * @protected
371 protected $FontFamily;
374 * Current font style.
375 * @protected
377 protected $FontStyle;
380 * Current font ascent (distance between font top and baseline).
381 * @protected
382 * @since 2.8.000 (2007-03-29)
384 protected $FontAscent;
387 * Current font descent (distance between font bottom and baseline).
388 * @protected
389 * @since 2.8.000 (2007-03-29)
391 protected $FontDescent;
394 * Underlining flag.
395 * @protected
397 protected $underline;
400 * Overlining flag.
401 * @protected
403 protected $overline;
406 * Current font info.
407 * @protected
409 protected $CurrentFont;
412 * Current font size in points.
413 * @protected
415 protected $FontSizePt;
418 * Current font size in user unit.
419 * @protected
421 protected $FontSize;
424 * Commands for drawing color.
425 * @protected
427 protected $DrawColor;
430 * Commands for filling color.
431 * @protected
433 protected $FillColor;
436 * Commands for text color.
437 * @protected
439 protected $TextColor;
442 * Indicates whether fill and text colors are different.
443 * @protected
445 protected $ColorFlag;
448 * Automatic page breaking.
449 * @protected
451 protected $AutoPageBreak;
454 * Threshold used to trigger page breaks.
455 * @protected
457 protected $PageBreakTrigger;
460 * Flag set when processing page header.
461 * @protected
463 protected $InHeader = false;
466 * Flag set when processing page footer.
467 * @protected
469 protected $InFooter = false;
472 * Zoom display mode.
473 * @protected
475 protected $ZoomMode;
478 * Layout display mode.
479 * @protected
481 protected $LayoutMode;
484 * If true set the document information dictionary in Unicode.
485 * @protected
487 protected $docinfounicode = true;
490 * Document title.
491 * @protected
493 protected $title = '';
496 * Document subject.
497 * @protected
499 protected $subject = '';
502 * Document author.
503 * @protected
505 protected $author = '';
508 * Document keywords.
509 * @protected
511 protected $keywords = '';
514 * Document creator.
515 * @protected
517 protected $creator = '';
520 * Starting page number.
521 * @protected
523 protected $starting_page_number = 1;
526 * String alias for total number of pages.
527 * @protected
529 protected $alias_tot_pages = '{:ptp:}';
532 * String alias for page number.
533 * @protected
535 protected $alias_num_page = '{:pnp:}';
538 * String alias for total number of pages in a single group.
539 * @protected
541 protected $alias_group_tot_pages = '{:ptg:}';
544 * String alias for group page number.
545 * @protected
547 protected $alias_group_num_page = '{:png:}';
550 * String alias for right shift compensation used to correctly align page numbers on the right.
551 * @protected
553 protected $alias_right_shift = '{rsc:';
556 * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
557 * @since 2002-07-31
558 * @author Nicola Asuni
559 * @protected
561 protected $img_rb_x;
564 * The right-bottom corner Y coordinate of last inserted image.
565 * @since 2002-07-31
566 * @author Nicola Asuni
567 * @protected
569 protected $img_rb_y;
572 * Adjusting factor to convert pixels to user units.
573 * @since 2004-06-14
574 * @author Nicola Asuni
575 * @protected
577 protected $imgscale = 1;
580 * Boolean flag set to true when the input text is unicode (require unicode fonts).
581 * @since 2005-01-02
582 * @author Nicola Asuni
583 * @protected
585 protected $isunicode = false;
588 * Object containing unicode data.
589 * @since 5.9.004 (2010-10-18)
590 * @author Nicola Asuni
591 * @protected
593 protected $unicode;
596 * Object containing font encoding maps.
597 * @since 5.9.123 (2011-10-01)
598 * @author Nicola Asuni
599 * @protected
601 protected $encmaps;
604 * PDF version.
605 * @since 1.5.3
606 * @protected
608 protected $PDFVersion = '1.7';
611 * ID of the stored default header template (-1 = not set).
612 * @protected
614 protected $header_xobjid = -1;
617 * If true reset the Header Xobject template at each page
618 * @protected
620 protected $header_xobj_autoreset = false;
623 * Minimum distance between header and top page margin.
624 * @protected
626 protected $header_margin;
629 * Minimum distance between footer and bottom page margin.
630 * @protected
632 protected $footer_margin;
635 * Original left margin value.
636 * @protected
637 * @since 1.53.0.TC013
639 protected $original_lMargin;
642 * Original right margin value.
643 * @protected
644 * @since 1.53.0.TC013
646 protected $original_rMargin;
649 * Default font used on page header.
650 * @protected
652 protected $header_font;
655 * Default font used on page footer.
656 * @protected
658 protected $footer_font;
661 * Language templates.
662 * @protected
664 protected $l;
667 * Barcode to print on page footer (only if set).
668 * @protected
670 protected $barcode = false;
673 * Boolean flag to print/hide page header.
674 * @protected
676 protected $print_header = true;
679 * Boolean flag to print/hide page footer.
680 * @protected
682 protected $print_footer = true;
685 * Header image logo.
686 * @protected
688 protected $header_logo = '';
691 * Width of header image logo in user units.
692 * @protected
694 protected $header_logo_width = 30;
697 * Title to be printed on default page header.
698 * @protected
700 protected $header_title = '';
703 * String to pring on page header after title.
704 * @protected
706 protected $header_string = '';
709 * Default number of columns for html table.
710 * @protected
712 protected $default_table_columns = 4;
714 // variables for html parser
717 * HTML PARSER: array to store current link and rendering styles.
718 * @protected
720 protected $HREF = array();
723 * List of available fonts on filesystem.
724 * @protected
726 protected $fontlist = array();
729 * Current foreground color.
730 * @protected
732 protected $fgcolor;
735 * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
736 * @protected
738 protected $listordered = array();
741 * HTML PARSER: array count list items on nested lists.
742 * @protected
744 protected $listcount = array();
747 * HTML PARSER: current list nesting level.
748 * @protected
750 protected $listnum = 0;
753 * HTML PARSER: indent amount for lists.
754 * @protected
756 protected $listindent = 0;
759 * HTML PARSER: current list indententation level.
760 * @protected
762 protected $listindentlevel = 0;
765 * Current background color.
766 * @protected
768 protected $bgcolor;
771 * Temporary font size in points.
772 * @protected
774 protected $tempfontsize = 10;
777 * Spacer string for LI tags.
778 * @protected
780 protected $lispacer = '';
783 * Default encoding.
784 * @protected
785 * @since 1.53.0.TC010
787 protected $encoding = 'UTF-8';
790 * PHP internal encoding.
791 * @protected
792 * @since 1.53.0.TC016
794 protected $internal_encoding;
797 * Boolean flag to indicate if the document language is Right-To-Left.
798 * @protected
799 * @since 2.0.000
801 protected $rtl = false;
804 * Boolean flag used to force RTL or LTR string direction.
805 * @protected
806 * @since 2.0.000
808 protected $tmprtl = false;
810 // --- Variables used for document encryption:
813 * IBoolean flag indicating whether document is protected.
814 * @protected
815 * @since 2.0.000 (2008-01-02)
817 protected $encrypted;
820 * Array containing encryption settings.
821 * @protected
822 * @since 5.0.005 (2010-05-11)
824 protected $encryptdata = array();
827 * Last RC4 key encrypted (cached for optimisation).
828 * @protected
829 * @since 2.0.000 (2008-01-02)
831 protected $last_enc_key;
834 * Last RC4 computed key.
835 * @protected
836 * @since 2.0.000 (2008-01-02)
838 protected $last_enc_key_c;
841 * Encryption padding string.
842 * @protected
844 protected $enc_padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
847 * File ID (used on document trailer).
848 * @protected
849 * @since 5.0.005 (2010-05-12)
851 protected $file_id;
853 // --- bookmark ---
856 * Outlines for bookmark.
857 * @protected
858 * @since 2.1.002 (2008-02-12)
860 protected $outlines = array();
863 * Outline root for bookmark.
864 * @protected
865 * @since 2.1.002 (2008-02-12)
867 protected $OutlineRoot;
869 // --- javascript and form ---
872 * Javascript code.
873 * @protected
874 * @since 2.1.002 (2008-02-12)
876 protected $javascript = '';
879 * Javascript counter.
880 * @protected
881 * @since 2.1.002 (2008-02-12)
883 protected $n_js;
886 * line trough state
887 * @protected
888 * @since 2.8.000 (2008-03-19)
890 protected $linethrough;
893 * Array with additional document-wide usage rights for the document.
894 * @protected
895 * @since 5.8.014 (2010-08-23)
897 protected $ur = array();
900 * DPI (Dot Per Inch) Document Resolution (do not change).
901 * @protected
902 * @since 3.0.000 (2008-03-27)
904 protected $dpi = 72;
907 * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
908 * @protected
909 * @since 3.0.000 (2008-03-27)
911 protected $newpagegroup = array();
914 * Array that contains the number of pages in each page group.
915 * @protected
916 * @since 3.0.000 (2008-03-27)
918 protected $pagegroups = array();
921 * Current page group number.
922 * @protected
923 * @since 3.0.000 (2008-03-27)
925 protected $currpagegroup = 0;
928 * Array of transparency objects and parameters.
929 * @protected
930 * @since 3.0.000 (2008-03-27)
932 protected $extgstates;
935 * Set the default JPEG compression quality (1-100).
936 * @protected
937 * @since 3.0.000 (2008-03-27)
939 protected $jpeg_quality;
942 * Default cell height ratio.
943 * @protected
944 * @since 3.0.014 (2008-05-23)
946 protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
949 * PDF viewer preferences.
950 * @protected
951 * @since 3.1.000 (2008-06-09)
953 protected $viewer_preferences;
956 * A name object specifying how the document should be displayed when opened.
957 * @protected
958 * @since 3.1.000 (2008-06-09)
960 protected $PageMode;
963 * Array for storing gradient information.
964 * @protected
965 * @since 3.1.000 (2008-06-09)
967 protected $gradients = array();
970 * Array used to store positions inside the pages buffer (keys are the page numbers).
971 * @protected
972 * @since 3.2.000 (2008-06-26)
974 protected $intmrk = array();
977 * Array used to store positions inside the pages buffer (keys are the page numbers).
978 * @protected
979 * @since 5.7.000 (2010-08-03)
981 protected $bordermrk = array();
984 * Array used to store page positions to track empty pages (keys are the page numbers).
985 * @protected
986 * @since 5.8.007 (2010-08-18)
988 protected $emptypagemrk = array();
991 * Array used to store content positions inside the pages buffer (keys are the page numbers).
992 * @protected
993 * @since 4.6.021 (2009-07-20)
995 protected $cntmrk = array();
998 * Array used to store footer positions of each page.
999 * @protected
1000 * @since 3.2.000 (2008-07-01)
1002 protected $footerpos = array();
1005 * Array used to store footer length of each page.
1006 * @protected
1007 * @since 4.0.014 (2008-07-29)
1009 protected $footerlen = array();
1012 * Boolean flag to indicate if a new line is created.
1013 * @protected
1014 * @since 3.2.000 (2008-07-01)
1016 protected $newline = true;
1019 * End position of the latest inserted line.
1020 * @protected
1021 * @since 3.2.000 (2008-07-01)
1023 protected $endlinex = 0;
1026 * PDF string for width value of the last line.
1027 * @protected
1028 * @since 4.0.006 (2008-07-16)
1030 protected $linestyleWidth = '';
1033 * PDF string for CAP value of the last line.
1034 * @protected
1035 * @since 4.0.006 (2008-07-16)
1037 protected $linestyleCap = '0 J';
1040 * PDF string for join value of the last line.
1041 * @protected
1042 * @since 4.0.006 (2008-07-16)
1044 protected $linestyleJoin = '0 j';
1047 * PDF string for dash value of the last line.
1048 * @protected
1049 * @since 4.0.006 (2008-07-16)
1051 protected $linestyleDash = '[] 0 d';
1054 * Boolean flag to indicate if marked-content sequence is open.
1055 * @protected
1056 * @since 4.0.013 (2008-07-28)
1058 protected $openMarkedContent = false;
1061 * Count the latest inserted vertical spaces on HTML.
1062 * @protected
1063 * @since 4.0.021 (2008-08-24)
1065 protected $htmlvspace = 0;
1068 * Array of Spot colors.
1069 * @protected
1070 * @since 4.0.024 (2008-09-12)
1072 protected $spot_colors = array();
1075 * Symbol used for HTML unordered list items.
1076 * @protected
1077 * @since 4.0.028 (2008-09-26)
1079 protected $lisymbol = '';
1082 * String used to mark the beginning and end of EPS image blocks.
1083 * @protected
1084 * @since 4.1.000 (2008-10-18)
1086 protected $epsmarker = 'x#!#EPS#!#x';
1089 * Array of transformation matrix.
1090 * @protected
1091 * @since 4.2.000 (2008-10-29)
1093 protected $transfmatrix = array();
1096 * Current key for transformation matrix.
1097 * @protected
1098 * @since 4.8.005 (2009-09-17)
1100 protected $transfmatrix_key = 0;
1103 * Booklet mode for double-sided pages.
1104 * @protected
1105 * @since 4.2.000 (2008-10-29)
1107 protected $booklet = false;
1110 * Epsilon value used for float calculations.
1111 * @protected
1112 * @since 4.2.000 (2008-10-29)
1114 protected $feps = 0.005;
1117 * Array used for custom vertical spaces for HTML tags.
1118 * @protected
1119 * @since 4.2.001 (2008-10-30)
1121 protected $tagvspaces = array();
1124 * HTML PARSER: custom indent amount for lists. Negative value means disabled.
1125 * @protected
1126 * @since 4.2.007 (2008-11-12)
1128 protected $customlistindent = -1;
1131 * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
1132 * @protected
1133 * @since 4.2.010 (2008-11-14)
1135 protected $opencell = true;
1138 * Array of files to embedd.
1139 * @protected
1140 * @since 4.4.000 (2008-12-07)
1142 protected $embeddedfiles = array();
1145 * Boolean flag to indicate if we are inside a PRE tag.
1146 * @protected
1147 * @since 4.4.001 (2008-12-08)
1149 protected $premode = false;
1152 * Array used to store positions of graphics transformation blocks inside the page buffer.
1153 * keys are the page numbers
1154 * @protected
1155 * @since 4.4.002 (2008-12-09)
1157 protected $transfmrk = array();
1160 * Default color for html links.
1161 * @protected
1162 * @since 4.4.003 (2008-12-09)
1164 protected $htmlLinkColorArray = array(0, 0, 255);
1167 * Default font style to add to html links.
1168 * @protected
1169 * @since 4.4.003 (2008-12-09)
1171 protected $htmlLinkFontStyle = 'U';
1174 * Counts the number of pages.
1175 * @protected
1176 * @since 4.5.000 (2008-12-31)
1178 protected $numpages = 0;
1181 * Array containing page lengths in bytes.
1182 * @protected
1183 * @since 4.5.000 (2008-12-31)
1185 protected $pagelen = array();
1188 * Counts the number of pages.
1189 * @protected
1190 * @since 4.5.000 (2008-12-31)
1192 protected $numimages = 0;
1195 * Store the image keys.
1196 * @protected
1197 * @since 4.5.000 (2008-12-31)
1199 protected $imagekeys = array();
1202 * Length of the buffer in bytes.
1203 * @protected
1204 * @since 4.5.000 (2008-12-31)
1206 protected $bufferlen = 0;
1209 * If true enables disk caching.
1210 * @protected
1211 * @since 4.5.000 (2008-12-31)
1213 protected $diskcache = false;
1216 * Counts the number of fonts.
1217 * @protected
1218 * @since 4.5.000 (2009-01-02)
1220 protected $numfonts = 0;
1223 * Store the font keys.
1224 * @protected
1225 * @since 4.5.000 (2009-01-02)
1227 protected $fontkeys = array();
1230 * Store the font object IDs.
1231 * @protected
1232 * @since 4.8.001 (2009-09-09)
1234 protected $font_obj_ids = array();
1237 * Store the fage status (true when opened, false when closed).
1238 * @protected
1239 * @since 4.5.000 (2009-01-02)
1241 protected $pageopen = array();
1244 * Default monospace font.
1245 * @protected
1246 * @since 4.5.025 (2009-03-10)
1248 protected $default_monospaced_font = 'courier';
1251 * Cloned copy of the current class object.
1252 * @protected
1253 * @since 4.5.029 (2009-03-19)
1255 protected $objcopy;
1258 * Array used to store the lengths of cache files.
1259 * @protected
1260 * @since 4.5.029 (2009-03-19)
1262 protected $cache_file_length = array();
1265 * Table header content to be repeated on each new page.
1266 * @protected
1267 * @since 4.5.030 (2009-03-20)
1269 protected $thead = '';
1272 * Margins used for table header.
1273 * @protected
1274 * @since 4.5.030 (2009-03-20)
1276 protected $theadMargins = array();
1279 * Cache array for UTF8StringToArray() method.
1280 * @protected
1281 * @since 4.5.037 (2009-04-07)
1283 protected $cache_UTF8StringToArray = array();
1286 * Maximum size of cache array used for UTF8StringToArray() method.
1287 * @protected
1288 * @since 4.5.037 (2009-04-07)
1290 protected $cache_maxsize_UTF8StringToArray = 8;
1293 * Current size of cache array used for UTF8StringToArray() method.
1294 * @protected
1295 * @since 4.5.037 (2009-04-07)
1297 protected $cache_size_UTF8StringToArray = 0;
1300 * Boolean flag to enable document digital signature.
1301 * @protected
1302 * @since 4.6.005 (2009-04-24)
1304 protected $sign = false;
1307 * Digital signature data.
1308 * @protected
1309 * @since 4.6.005 (2009-04-24)
1311 protected $signature_data = array();
1314 * Digital signature max length.
1315 * @protected
1316 * @since 4.6.005 (2009-04-24)
1318 protected $signature_max_length = 11742;
1321 * Data for digital signature appearance.
1322 * @protected
1323 * @since 5.3.011 (2010-06-16)
1325 protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
1328 * Array of empty digital signature appearances.
1329 * @protected
1330 * @since 5.9.101 (2011-07-06)
1332 protected $empty_signature_appearance = array();
1335 * Regular expression used to find blank characters (required for word-wrapping).
1336 * @protected
1337 * @since 4.6.006 (2009-04-28)
1339 protected $re_spaces = '/[^\S\xa0]/';
1342 * Array of $re_spaces parts.
1343 * @protected
1344 * @since 5.5.011 (2010-07-09)
1346 protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
1349 * Digital signature object ID.
1350 * @protected
1351 * @since 4.6.022 (2009-06-23)
1353 protected $sig_obj_id = 0;
1356 * ByteRange placemark used during digital signature process.
1357 * @protected
1358 * @since 4.6.028 (2009-08-25)
1360 protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
1363 * Placemark used during digital signature process.
1364 * @protected
1365 * @since 4.6.028 (2009-08-25)
1367 protected $sig_annot_ref = '***SIGANNREF*** 0 R';
1370 * ID of page objects.
1371 * @protected
1372 * @since 4.7.000 (2009-08-29)
1374 protected $page_obj_id = array();
1377 * List of form annotations IDs.
1378 * @protected
1379 * @since 4.8.000 (2009-09-07)
1381 protected $form_obj_id = array();
1384 * Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
1385 * @protected
1386 * @since 4.8.000 (2009-09-07)
1388 protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
1391 * Javascript objects array.
1392 * @protected
1393 * @since 4.8.000 (2009-09-07)
1395 protected $js_objects = array();
1398 * Current form action (used during XHTML rendering).
1399 * @protected
1400 * @since 4.8.000 (2009-09-07)
1402 protected $form_action = '';
1405 * Current form encryption type (used during XHTML rendering).
1406 * @protected
1407 * @since 4.8.000 (2009-09-07)
1409 protected $form_enctype = 'application/x-www-form-urlencoded';
1412 * Current method to submit forms.
1413 * @protected
1414 * @since 4.8.000 (2009-09-07)
1416 protected $form_mode = 'post';
1419 * List of fonts used on form fields (fontname => fontkey).
1420 * @protected
1421 * @since 4.8.001 (2009-09-09)
1423 protected $annotation_fonts = array();
1426 * List of radio buttons parent objects.
1427 * @protected
1428 * @since 4.8.001 (2009-09-09)
1430 protected $radiobutton_groups = array();
1433 * List of radio group objects IDs.
1434 * @protected
1435 * @since 4.8.001 (2009-09-09)
1437 protected $radio_groups = array();
1440 * Text indentation value (used for text-indent CSS attribute).
1441 * @protected
1442 * @since 4.8.006 (2009-09-23)
1444 protected $textindent = 0;
1447 * Store page number when startTransaction() is called.
1448 * @protected
1449 * @since 4.8.006 (2009-09-23)
1451 protected $start_transaction_page = 0;
1454 * Store Y position when startTransaction() is called.
1455 * @protected
1456 * @since 4.9.001 (2010-03-28)
1458 protected $start_transaction_y = 0;
1461 * True when we are printing the thead section on a new page.
1462 * @protected
1463 * @since 4.8.027 (2010-01-25)
1465 protected $inthead = false;
1468 * Array of column measures (width, space, starting Y position).
1469 * @protected
1470 * @since 4.9.001 (2010-03-28)
1472 protected $columns = array();
1475 * Number of colums.
1476 * @protected
1477 * @since 4.9.001 (2010-03-28)
1479 protected $num_columns = 1;
1482 * Current column number.
1483 * @protected
1484 * @since 4.9.001 (2010-03-28)
1486 protected $current_column = 0;
1489 * Starting page for columns.
1490 * @protected
1491 * @since 4.9.001 (2010-03-28)
1493 protected $column_start_page = 0;
1496 * Maximum page and column selected.
1497 * @protected
1498 * @since 5.8.000 (2010-08-11)
1500 protected $maxselcol = array('page' => 0, 'column' => 0);
1503 * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
1504 * @protected
1505 * @since 5.8.000 (2010-08-11)
1507 protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
1510 * Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
1511 * @protected
1512 * @since 4.9.008 (2010-04-03)
1514 protected $textrendermode = 0;
1517 * Text stroke width in doc units.
1518 * @protected
1519 * @since 4.9.008 (2010-04-03)
1521 protected $textstrokewidth = 0;
1524 * Current stroke color.
1525 * @protected
1526 * @since 4.9.008 (2010-04-03)
1528 protected $strokecolor;
1531 * Default unit of measure for document.
1532 * @protected
1533 * @since 5.0.000 (2010-04-22)
1535 protected $pdfunit = 'mm';
1538 * Boolean flag true when we are on TOC (Table Of Content) page.
1539 * @protected
1541 protected $tocpage = false;
1544 * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
1545 * @protected
1546 * @since 5.0.000 (2010-04-26)
1548 protected $rasterize_vector_images = false;
1551 * Boolean flag: if true enables font subsetting by default.
1552 * @protected
1553 * @since 5.3.002 (2010-06-07)
1555 protected $font_subsetting = true;
1558 * Array of default graphic settings.
1559 * @protected
1560 * @since 5.5.008 (2010-07-02)
1562 protected $default_graphic_vars = array();
1565 * Array of XObjects.
1566 * @protected
1567 * @since 5.8.014 (2010-08-23)
1569 protected $xobjects = array();
1572 * Boolean value true when we are inside an XObject.
1573 * @protected
1574 * @since 5.8.017 (2010-08-24)
1576 protected $inxobj = false;
1579 * Current XObject ID.
1580 * @protected
1581 * @since 5.8.017 (2010-08-24)
1583 protected $xobjid = '';
1586 * Percentage of character stretching.
1587 * @protected
1588 * @since 5.9.000 (2010-09-29)
1590 protected $font_stretching = 100;
1593 * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
1594 * @protected
1595 * @since 5.9.000 (2010-09-29)
1597 protected $font_spacing = 0;
1600 * Array of no-write regions.
1601 * ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
1602 * @protected
1603 * @since 5.9.003 (2010-10-14)
1605 protected $page_regions = array();
1608 * Array containing HTML color names and values.
1609 * @protected
1610 * @since 5.9.004 (2010-10-18)
1612 protected $webcolor = array();
1615 * Array containing spot color names and values.
1616 * @protected
1617 * @since 5.9.012 (2010-11-11)
1619 protected $spotcolor = array();
1622 * Array of PDF layers data.
1623 * @protected
1624 * @since 5.9.102 (2011-07-13)
1626 protected $pdflayers = array();
1629 * A dictionary of names and corresponding destinations (Dests key on document Catalog).
1630 * @protected
1631 * @since 5.9.097 (2011-06-23)
1633 protected $dests = array();
1636 * Object ID for Named Destinations
1637 * @protected
1638 * @since 5.9.097 (2011-06-23)
1640 protected $n_dests;
1643 * Directory used for the last SVG image.
1644 * @protected
1645 * @since 5.0.000 (2010-05-05)
1647 protected $svgdir = '';
1650 * Deafult unit of measure for SVG.
1651 * @protected
1652 * @since 5.0.000 (2010-05-02)
1654 protected $svgunit = 'px';
1657 * Array of SVG gradients.
1658 * @protected
1659 * @since 5.0.000 (2010-05-02)
1661 protected $svggradients = array();
1664 * ID of last SVG gradient.
1665 * @protected
1666 * @since 5.0.000 (2010-05-02)
1668 protected $svggradientid = 0;
1671 * Boolean value true when in SVG defs group.
1672 * @protected
1673 * @since 5.0.000 (2010-05-02)
1675 protected $svgdefsmode = false;
1678 * Array of SVG defs.
1679 * @protected
1680 * @since 5.0.000 (2010-05-02)
1682 protected $svgdefs = array();
1685 * Boolean value true when in SVG clipPath tag.
1686 * @protected
1687 * @since 5.0.000 (2010-04-26)
1689 protected $svgclipmode = false;
1692 * Array of SVG clipPath commands.
1693 * @protected
1694 * @since 5.0.000 (2010-05-02)
1696 protected $svgclippaths = array();
1699 * Array of SVG clipPath tranformation matrix.
1700 * @protected
1701 * @since 5.8.022 (2010-08-31)
1703 protected $svgcliptm = array();
1706 * ID of last SVG clipPath.
1707 * @protected
1708 * @since 5.0.000 (2010-05-02)
1710 protected $svgclipid = 0;
1713 * SVG text.
1714 * @protected
1715 * @since 5.0.000 (2010-05-02)
1717 protected $svgtext = '';
1720 * SVG text properties.
1721 * @protected
1722 * @since 5.8.013 (2010-08-23)
1724 protected $svgtextmode = array();
1727 * Array of hinheritable SVG properties.
1728 * @protected
1729 * @since 5.0.000 (2010-05-02)
1731 protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
1734 * Array of SVG properties.
1735 * @protected
1736 * @since 5.0.000 (2010-05-02)
1738 protected $svgstyles = array(array(
1739 'alignment-baseline' => 'auto',
1740 'baseline-shift' => 'baseline',
1741 'clip' => 'auto',
1742 'clip-path' => 'none',
1743 'clip-rule' => 'nonzero',
1744 'color' => 'black',
1745 'color-interpolation' => 'sRGB',
1746 'color-interpolation-filters' => 'linearRGB',
1747 'color-profile' => 'auto',
1748 'color-rendering' => 'auto',
1749 'cursor' => 'auto',
1750 'direction' => 'ltr',
1751 'display' => 'inline',
1752 'dominant-baseline' => 'auto',
1753 'enable-background' => 'accumulate',
1754 'fill' => 'black',
1755 'fill-opacity' => 1,
1756 'fill-rule' => 'nonzero',
1757 'filter' => 'none',
1758 'flood-color' => 'black',
1759 'flood-opacity' => 1,
1760 'font' => '',
1761 'font-family' => 'helvetica',
1762 'font-size' => 'medium',
1763 'font-size-adjust' => 'none',
1764 'font-stretch' => 'normal',
1765 'font-style' => 'normal',
1766 'font-variant' => 'normal',
1767 'font-weight' => 'normal',
1768 'glyph-orientation-horizontal' => '0deg',
1769 'glyph-orientation-vertical' => 'auto',
1770 'image-rendering' => 'auto',
1771 'kerning' => 'auto',
1772 'letter-spacing' => 'normal',
1773 'lighting-color' => 'white',
1774 'marker' => '',
1775 'marker-end' => 'none',
1776 'marker-mid' => 'none',
1777 'marker-start' => 'none',
1778 'mask' => 'none',
1779 'opacity' => 1,
1780 'overflow' => 'auto',
1781 'pointer-events' => 'visiblePainted',
1782 'shape-rendering' => 'auto',
1783 'stop-color' => 'black',
1784 'stop-opacity' => 1,
1785 'stroke' => 'none',
1786 'stroke-dasharray' => 'none',
1787 'stroke-dashoffset' => 0,
1788 'stroke-linecap' => 'butt',
1789 'stroke-linejoin' => 'miter',
1790 'stroke-miterlimit' => 4,
1791 'stroke-opacity' => 1,
1792 'stroke-width' => 1,
1793 'text-anchor' => 'start',
1794 'text-decoration' => 'none',
1795 'text-rendering' => 'auto',
1796 'unicode-bidi' => 'normal',
1797 'visibility' => 'visible',
1798 'word-spacing' => 'normal',
1799 'writing-mode' => 'lr-tb',
1800 'text-color' => 'black',
1801 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
1805 * If true force sRGB color profile for all document.
1806 * @protected
1807 * @since 5.9.121 (2011-09-28)
1809 protected $force_srgb = false;
1812 * If true set the document to PDF/A mode.
1813 * @protected
1814 * @since 5.9.121 (2011-09-27)
1816 protected $pdfa_mode = false;
1819 * Document creation date
1820 * @protected
1821 * @since 5.9.121 (2011-09-28)
1823 protected $doc_date;
1826 * Custom XMP data.
1827 * @protected
1828 * @since 5.9.128 (2011-10-06)
1830 protected $custom_xmp = '';
1832 //------------------------------------------------------------
1833 // METHODS
1834 //------------------------------------------------------------
1837 * This is the class constructor.
1838 * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
1839 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
1840 * @param $unit (string) User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
1841 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
1842 * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
1843 * @param $encoding (string) Charset encoding; default is UTF-8.
1844 * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
1845 * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
1846 * @public
1847 * @see getPageSizeFromFormat(), setPageFormat()
1849 public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
1850 /* Set internal character encoding to ASCII */
1851 if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
1852 $this->internal_encoding = mb_internal_encoding();
1853 mb_internal_encoding('ASCII');
1855 // get array of HTML colors
1856 require(dirname(__FILE__).'/htmlcolors.php');
1857 $this->webcolor = $webcolor;
1858 // get array of custom spot colors
1859 if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
1860 require(dirname(__FILE__).'/spotcolors.php');
1861 $this->spotcolor = $spotcolor;
1862 } else {
1863 $this->spotcolor = array();
1865 require_once(dirname(__FILE__).'/unicode_data.php');
1866 $this->unicode = new TCPDF_UNICODE_DATA();
1867 require_once(dirname(__FILE__).'/encodings_maps.php');
1868 $this->encmaps = new TCPDF_ENCODING_MAPS();
1869 $this->font_obj_ids = array();
1870 $this->page_obj_id = array();
1871 $this->form_obj_id = array();
1872 // set pdf/a mode
1873 $this->pdfa_mode = $pdfa;
1874 $this->force_srgb = false;
1875 // set disk caching
1876 $this->diskcache = $diskcache ? true : false;
1877 // set language direction
1878 $this->rtl = false;
1879 $this->tmprtl = false;
1880 // some checks
1881 $this->_dochecks();
1882 // initialization of properties
1883 $this->isunicode = $unicode;
1884 $this->page = 0;
1885 $this->transfmrk[0] = array();
1886 $this->pagedim = array();
1887 $this->n = 2;
1888 $this->buffer = '';
1889 $this->pages = array();
1890 $this->state = 0;
1891 $this->fonts = array();
1892 $this->FontFiles = array();
1893 $this->diffs = array();
1894 $this->images = array();
1895 $this->links = array();
1896 $this->gradients = array();
1897 $this->InFooter = false;
1898 $this->lasth = 0;
1899 $this->FontFamily = 'helvetica';
1900 $this->FontStyle = '';
1901 $this->FontSizePt = 12;
1902 $this->underline = false;
1903 $this->overline = false;
1904 $this->linethrough = false;
1905 $this->DrawColor = '0 G';
1906 $this->FillColor = '0 g';
1907 $this->TextColor = '0 g';
1908 $this->ColorFlag = false;
1909 $this->pdflayers = array();
1910 // encryption values
1911 $this->encrypted = false;
1912 $this->last_enc_key = '';
1913 // standard Unicode fonts
1914 $this->CoreFonts = array(
1915 'courier'=>'Courier',
1916 'courierB'=>'Courier-Bold',
1917 'courierI'=>'Courier-Oblique',
1918 'courierBI'=>'Courier-BoldOblique',
1919 'helvetica'=>'Helvetica',
1920 'helveticaB'=>'Helvetica-Bold',
1921 'helveticaI'=>'Helvetica-Oblique',
1922 'helveticaBI'=>'Helvetica-BoldOblique',
1923 'times'=>'Times-Roman',
1924 'timesB'=>'Times-Bold',
1925 'timesI'=>'Times-Italic',
1926 'timesBI'=>'Times-BoldItalic',
1927 'symbol'=>'Symbol',
1928 'zapfdingbats'=>'ZapfDingbats'
1930 // set scale factor
1931 $this->setPageUnit($unit);
1932 // set page format and orientation
1933 $this->setPageFormat($format, $orientation);
1934 // page margins (1 cm)
1935 $margin = 28.35 / $this->k;
1936 $this->SetMargins($margin, $margin);
1937 // internal cell padding
1938 $cpadding = $margin / 10;
1939 $this->setCellPaddings($cpadding, 0, $cpadding, 0);
1940 // cell margins
1941 $this->setCellMargins(0, 0, 0, 0);
1942 // line width (0.2 mm)
1943 $this->LineWidth = 0.57 / $this->k;
1944 $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
1945 $this->linestyleCap = '0 J';
1946 $this->linestyleJoin = '0 j';
1947 $this->linestyleDash = '[] 0 d';
1948 // automatic page break
1949 $this->SetAutoPageBreak(true, (2 * $margin));
1950 // full width display mode
1951 $this->SetDisplayMode('fullwidth');
1952 // compression
1953 $this->SetCompression();
1954 // set default PDF version number
1955 $this->setPDFVersion();
1956 $this->encoding = $encoding;
1957 $this->HREF = array();
1958 $this->getFontsList();
1959 $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
1960 $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
1961 $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
1962 $this->extgstates = array();
1963 // user's rights
1964 $this->sign = false;
1965 $this->ur['enabled'] = false;
1966 $this->ur['document'] = '/FullSave';
1967 $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
1968 $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
1969 $this->ur['signature'] = '/Modify';
1970 $this->ur['ef'] = '/Create/Delete/Modify/Import';
1971 $this->ur['formex'] = '';
1972 $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
1973 $this->empty_signature_appearance = array();
1974 // set default JPEG quality
1975 $this->jpeg_quality = 75;
1976 // initialize some settings
1977 $this->utf8Bidi(array(''), '');
1978 // set default font
1979 $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
1980 // check if PCRE Unicode support is enabled
1981 if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
1982 // PCRE unicode support is turned ON
1983 // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
1984 // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
1985 // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
1986 //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
1987 $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
1988 } else {
1989 // PCRE unicode support is turned OFF
1990 $this->setSpacesRE('/[^\S\xa0]/');
1992 $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
1993 // set file ID for trailer
1994 $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
1995 // set document date
1996 $this->doc_date = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
1997 // get default graphic vars
1998 $this->default_graphic_vars = $this->getGraphicVars();
1999 $this->header_xobj_autoreset = false;
2000 $this->custom_xmp = '';
2004 * Default destructor.
2005 * @public
2006 * @since 1.53.0.TC016
2008 public function __destruct() {
2009 // restore internal encoding
2010 if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
2011 mb_internal_encoding($this->internal_encoding);
2013 // unset all class variables
2014 $this->_destroy(true);
2018 * Return the current TCPDF version.
2019 * @return TCPDF version string
2020 * @public
2021 * @since 5.9.012 (2010-11-10)
2023 public function getTCPDFVersion() {
2024 return $this->tcpdf_version;
2028 * Set the units of measure for the document.
2029 * @param $unit (string) User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
2030 * @public
2031 * @since 3.0.015 (2008-06-06)
2033 public function setPageUnit($unit) {
2034 $unit = strtolower($unit);
2035 //Set scale factor
2036 switch ($unit) {
2037 // points
2038 case 'px':
2039 case 'pt': {
2040 $this->k = 1;
2041 break;
2043 // millimeters
2044 case 'mm': {
2045 $this->k = $this->dpi / 25.4;
2046 break;
2048 // centimeters
2049 case 'cm': {
2050 $this->k = $this->dpi / 2.54;
2051 break;
2053 // inches
2054 case 'in': {
2055 $this->k = $this->dpi;
2056 break;
2058 // unsupported unit
2059 default : {
2060 $this->Error('Incorrect unit: '.$unit);
2061 break;
2064 $this->pdfunit = $unit;
2065 if (isset($this->CurOrientation)) {
2066 $this->setPageOrientation($this->CurOrientation);
2071 * Get page dimensions from format name.
2072 * @param $format (mixed) The format name. It can be: <ul>
2073 * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
2074 * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
2075 * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
2076 * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
2077 * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
2078 * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
2079 * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
2080 * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
2081 * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
2082 * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
2083 * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
2084 * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
2085 * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
2086 * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
2087 * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
2088 * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
2089 * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
2090 * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
2091 * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
2092 * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
2093 * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
2094 * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
2095 * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
2096 * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
2097 * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
2098 * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
2099 * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
2100 * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
2101 * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
2102 * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
2103 * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
2104 * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
2105 * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
2106 * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
2107 * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
2108 * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
2109 * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
2110 * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
2111 * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
2112 * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
2113 * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
2114 * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
2115 * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
2116 * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
2117 * <li><b>SIS 014711 E Series</b></li>
2118 * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
2119 * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
2120 * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
2121 * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
2122 * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
2123 * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
2124 * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
2125 * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
2126 * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
2127 * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
2128 * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
2129 * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
2130 * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
2131 * <li><b>SIS 014711 G Series</b></li>
2132 * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
2133 * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
2134 * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
2135 * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
2136 * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
2137 * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
2138 * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
2139 * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
2140 * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
2141 * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
2142 * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
2143 * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
2144 * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
2145 * <li><b>ISO Press</b></li>
2146 * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
2147 * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
2148 * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
2149 * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
2150 * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
2151 * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
2152 * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
2153 * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
2154 * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
2155 * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
2156 * <li><b>German DIN 476</b></li>
2157 * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
2158 * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
2159 * <li><b>Variations on the ISO Standard</b></li>
2160 * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
2161 * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
2162 * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
2163 * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
2164 * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
2165 * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
2166 * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
2167 * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
2168 * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
2169 * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
2170 * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
2171 * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
2172 * <li><b>ANSI Series</b></li>
2173 * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
2174 * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
2175 * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
2176 * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
2177 * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
2178 * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
2179 * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
2180 * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
2181 * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
2182 * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
2183 * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
2184 * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
2185 * <li><b>Other North American Paper Sizes</b></li>
2186 * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
2187 * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
2188 * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
2189 * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
2190 * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
2191 * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
2192 * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
2193 * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
2194 * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
2195 * <li><b>Canadian standard CAN 2-9.60M</b></li>
2196 * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
2197 * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
2198 * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
2199 * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
2200 * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
2201 * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
2202 * <li><b>North American Architectural Sizes</b></li>
2203 * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
2204 * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
2205 * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
2206 * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
2207 * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
2208 * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
2209 * <li><b>Announcement Envelopes</b></li>
2210 * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
2211 * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
2212 * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
2213 * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
2214 * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
2215 * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
2216 * <li><b>Commercial Envelopes</b></li>
2217 * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
2218 * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
2219 * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
2220 * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
2221 * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
2222 * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
2223 * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
2224 * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
2225 * <li><b>Catalogue Envelopes</b></li>
2226 * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
2227 * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
2228 * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
2229 * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
2230 * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
2231 * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
2232 * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
2233 * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
2234 * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
2235 * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
2236 * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
2237 * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
2238 * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
2239 * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
2240 * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
2241 * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
2242 * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
2243 * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
2244 * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
2245 * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
2246 * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
2247 * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
2248 * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
2249 * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
2250 * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
2251 * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
2252 * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
2253 * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
2254 * <li><b>PA Series</b></li>
2255 * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
2256 * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
2257 * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
2258 * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
2259 * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
2260 * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
2261 * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
2262 * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
2263 * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
2264 * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
2265 * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
2266 * <li><b>Standard Photographic Print Sizes</b></li>
2267 * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
2268 * <li>E (82x120 mm ; 3.25x4.72 in)</li>
2269 * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
2270 * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
2271 * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
2272 * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
2273 * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
2274 * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
2275 * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
2276 * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
2277 * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
2278 * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
2279 * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
2280 * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
2281 * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
2282 * <li><b>Common Newspaper Sizes</b></li>
2283 * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
2284 * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
2285 * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
2286 * <li><b>Business Cards</b></li>
2287 * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
2288 * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
2289 * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
2290 * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
2291 * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
2292 * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
2293 * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
2294 * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
2295 * <li><b>Billboards</b></li>
2296 * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
2297 * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
2298 * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
2299 * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
2300 * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
2301 * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
2302 * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
2303 * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
2304 * <li><b>Old Imperial English (some are still used in USA)</b></li>
2305 * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
2306 * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
2307 * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
2308 * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
2309 * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
2310 * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
2311 * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
2312 * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
2313 * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
2314 * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
2315 * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
2316 * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
2317 * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
2318 * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
2319 * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
2320 * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
2321 * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
2322 * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
2323 * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
2324 * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
2325 * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
2326 * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
2327 * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
2328 * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
2329 * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
2330 * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
2331 * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
2332 * <li><b>Old Imperial Belgian</b></li>
2333 * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
2334 * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
2335 * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
2336 * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
2337 * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
2338 * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
2339 * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
2340 * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
2341 * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
2342 * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
2343 * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
2344 * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
2345 * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
2346 * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
2347 * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
2348 * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
2349 * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
2350 * <li><b>Old Imperial French</b></li>
2351 * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
2352 * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
2353 * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
2354 * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
2355 * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
2356 * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
2357 * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
2358 * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
2359 * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
2360 * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
2361 * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
2362 * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
2363 * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
2364 * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
2365 * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
2366 * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
2367 * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
2368 * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
2369 * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
2370 * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
2371 * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
2372 * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
2373 * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
2374 * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
2375 * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
2376 * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
2377 * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
2378 * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
2379 * </ul>
2380 * @return array containing page width and height in points
2381 * @public
2382 * @since 5.0.010 (2010-05-17)
2384 public function getPageSizeFromFormat($format) {
2385 // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
2386 switch (strtoupper($format)) {
2387 // ISO 216 A Series + 2 SIS 014711 extensions
2388 case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
2389 case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
2390 case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
2391 case 'A3' : {$pf = array( 841.890, 1190.551); break;}
2392 case 'A4' : {$pf = array( 595.276, 841.890); break;}
2393 case 'A5' : {$pf = array( 419.528, 595.276); break;}
2394 case 'A6' : {$pf = array( 297.638, 419.528); break;}
2395 case 'A7' : {$pf = array( 209.764, 297.638); break;}
2396 case 'A8' : {$pf = array( 147.402, 209.764); break;}
2397 case 'A9' : {$pf = array( 104.882, 147.402); break;}
2398 case 'A10': {$pf = array( 73.701, 104.882); break;}
2399 case 'A11': {$pf = array( 51.024, 73.701); break;}
2400 case 'A12': {$pf = array( 36.850, 51.024); break;}
2401 // ISO 216 B Series + 2 SIS 014711 extensions
2402 case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
2403 case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
2404 case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
2405 case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
2406 case 'B4' : {$pf = array( 708.661, 1000.630); break;}
2407 case 'B5' : {$pf = array( 498.898, 708.661); break;}
2408 case 'B6' : {$pf = array( 354.331, 498.898); break;}
2409 case 'B7' : {$pf = array( 249.449, 354.331); break;}
2410 case 'B8' : {$pf = array( 175.748, 249.449); break;}
2411 case 'B9' : {$pf = array( 124.724, 175.748); break;}
2412 case 'B10': {$pf = array( 87.874, 124.724); break;}
2413 case 'B11': {$pf = array( 62.362, 87.874); break;}
2414 case 'B12': {$pf = array( 42.520, 62.362); break;}
2415 // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
2416 case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
2417 case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
2418 case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
2419 case 'C3' : {$pf = array( 918.425, 1298.268); break;}
2420 case 'C4' : {$pf = array( 649.134, 918.425); break;}
2421 case 'C5' : {$pf = array( 459.213, 649.134); break;}
2422 case 'C6' : {$pf = array( 323.150, 459.213); break;}
2423 case 'C7' : {$pf = array( 229.606, 323.150); break;}
2424 case 'C8' : {$pf = array( 161.575, 229.606); break;}
2425 case 'C9' : {$pf = array( 113.386, 161.575); break;}
2426 case 'C10': {$pf = array( 79.370, 113.386); break;}
2427 case 'C11': {$pf = array( 56.693, 79.370); break;}
2428 case 'C12': {$pf = array( 39.685, 56.693); break;}
2429 case 'C76': {$pf = array( 229.606, 459.213); break;}
2430 case 'DL' : {$pf = array( 311.811, 623.622); break;}
2431 // SIS 014711 E Series
2432 case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
2433 case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
2434 case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
2435 case 'E3' : {$pf = array( 878.740, 1247.244); break;}
2436 case 'E4' : {$pf = array( 623.622, 878.740); break;}
2437 case 'E5' : {$pf = array( 439.370, 623.622); break;}
2438 case 'E6' : {$pf = array( 311.811, 439.370); break;}
2439 case 'E7' : {$pf = array( 221.102, 311.811); break;}
2440 case 'E8' : {$pf = array( 155.906, 221.102); break;}
2441 case 'E9' : {$pf = array( 110.551, 155.906); break;}
2442 case 'E10': {$pf = array( 76.535, 110.551); break;}
2443 case 'E11': {$pf = array( 53.858, 76.535); break;}
2444 case 'E12': {$pf = array( 36.850, 53.858); break;}
2445 // SIS 014711 G Series
2446 case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
2447 case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
2448 case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
2449 case 'G3' : {$pf = array( 958.110, 1357.795); break;}
2450 case 'G4' : {$pf = array( 677.480, 958.110); break;}
2451 case 'G5' : {$pf = array( 479.055, 677.480); break;}
2452 case 'G6' : {$pf = array( 337.323, 479.055); break;}
2453 case 'G7' : {$pf = array( 238.110, 337.323); break;}
2454 case 'G8' : {$pf = array( 167.244, 238.110); break;}
2455 case 'G9' : {$pf = array( 119.055, 167.244); break;}
2456 case 'G10': {$pf = array( 82.205, 119.055); break;}
2457 case 'G11': {$pf = array( 59.528, 82.205); break;}
2458 case 'G12': {$pf = array( 39.685, 59.528); break;}
2459 // ISO Press
2460 case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
2461 case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
2462 case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
2463 case 'RA3': {$pf = array( 864.567, 1218.898); break;}
2464 case 'RA4': {$pf = array( 609.449, 864.567); break;}
2465 case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
2466 case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
2467 case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
2468 case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
2469 case 'SRA4': {$pf = array( 637.795, 907.087); break;}
2470 // German DIN 476
2471 case '4A0': {$pf = array( 4767.874, 6740.787); break;}
2472 case '2A0': {$pf = array( 3370.394, 4767.874); break;}
2473 // Variations on the ISO Standard
2474 case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
2475 case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
2476 case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
2477 case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
2478 case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
2479 case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
2480 case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
2481 case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
2482 case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
2483 case 'F4' : {$pf = array( 595.276, 935.433); break;}
2484 case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
2485 case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
2486 // ANSI Series
2487 case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
2488 case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
2489 case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
2490 case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
2491 case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
2492 // Traditional 'Loose' North American Paper Sizes
2493 case 'USLEDGER':
2494 case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
2495 case 'ORGANIZERK':
2496 case 'BIBLE':
2497 case 'USTABLOID':
2498 case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
2499 case 'ORGANIZERM':
2500 case 'USLETTER':
2501 case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
2502 case 'USLEGAL':
2503 case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
2504 case 'GOVERNMENTLETTER':
2505 case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
2506 case 'JUNIORLEGAL':
2507 case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
2508 // Other North American Paper Sizes
2509 case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
2510 case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
2511 case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
2512 case 'GOVERNMENTLEGAL':
2513 case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
2514 case 'MONARCH':
2515 case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
2516 case 'ORGANIZERL':
2517 case 'STATEMENT':
2518 case 'MEMO': {$pf = array( 396.000, 612.000); break;}
2519 case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
2520 case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
2521 case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
2522 // Canadian standard CAN 2-9.60M
2523 case 'P1': {$pf = array( 1587.402, 2437.795); break;}
2524 case 'P2': {$pf = array( 1218.898, 1587.402); break;}
2525 case 'P3': {$pf = array( 793.701, 1218.898); break;}
2526 case 'P4': {$pf = array( 609.449, 793.701); break;}
2527 case 'P5': {$pf = array( 396.850, 609.449); break;}
2528 case 'P6': {$pf = array( 303.307, 396.850); break;}
2529 // North American Architectural Sizes
2530 case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
2531 case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
2532 case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
2533 case 'BROADSHEET':
2534 case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
2535 case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
2536 case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
2537 // --- North American Envelope Sizes ---
2538 // - Announcement Envelopes
2539 case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
2540 case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
2541 case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
2542 case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
2543 case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
2544 case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
2545 // - Commercial Envelopes
2546 case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
2547 case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
2548 case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
2549 case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
2550 case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
2551 case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
2552 case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
2553 case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
2554 // - Catalogue Envelopes
2555 case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
2556 case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
2557 case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
2558 case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
2559 case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
2560 case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
2561 case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
2562 case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
2563 case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
2564 case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
2565 case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
2566 case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
2567 case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
2568 case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
2569 // Japanese (JIS P 0138-61) Standard B-Series
2570 case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
2571 case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
2572 case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
2573 case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
2574 case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
2575 case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
2576 case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
2577 case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
2578 case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
2579 case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
2580 case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
2581 case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
2582 case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
2583 // PA Series
2584 case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
2585 case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
2586 case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
2587 case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
2588 case 'PA4' : {$pf = array( 595.276, 793.701); break;}
2589 case 'PA5' : {$pf = array( 396.850, 595.276); break;}
2590 case 'PA6' : {$pf = array( 297.638, 396.850); break;}
2591 case 'PA7' : {$pf = array( 198.425, 297.638); break;}
2592 case 'PA8' : {$pf = array( 147.402, 198.425); break;}
2593 case 'PA9' : {$pf = array( 99.213, 147.402); break;}
2594 case 'PA10': {$pf = array( 73.701, 99.213); break;}
2595 // Standard Photographic Print Sizes
2596 case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
2597 case 'E' : {$pf = array( 233.858, 340.157); break;}
2598 case 'L':
2599 case '3R' : {$pf = array( 252.283, 360.000); break;}
2600 case 'KG':
2601 case '4R' : {$pf = array( 289.134, 430.866); break;}
2602 case '4D' : {$pf = array( 340.157, 430.866); break;}
2603 case '2L':
2604 case '5R' : {$pf = array( 360.000, 504.567); break;}
2605 case '8P':
2606 case '6R' : {$pf = array( 430.866, 575.433); break;}
2607 case '6P':
2608 case '8R' : {$pf = array( 575.433, 720.000); break;}
2609 case '6PW':
2610 case 'S8R' : {$pf = array( 575.433, 864.567); break;}
2611 case '4P':
2612 case '10R' : {$pf = array( 720.000, 864.567); break;}
2613 case '4PW':
2614 case 'S10R': {$pf = array( 720.000, 1080.000); break;}
2615 case '11R' : {$pf = array( 790.866, 1009.134); break;}
2616 case 'S11R': {$pf = array( 790.866, 1224.567); break;}
2617 case '12R' : {$pf = array( 864.567, 1080.000); break;}
2618 case 'S12R': {$pf = array( 864.567, 1292.598); break;}
2619 // Common Newspaper Sizes
2620 case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
2621 case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
2622 case 'NEWSPAPER_TABLOID':
2623 case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
2624 // Business Cards
2625 case 'CREDIT_CARD':
2626 case 'BUSINESS_CARD':
2627 case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
2628 case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
2629 case 'BUSINESS_CARD_IT':
2630 case 'BUSINESS_CARD_UK':
2631 case 'BUSINESS_CARD_FR':
2632 case 'BUSINESS_CARD_DE':
2633 case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
2634 case 'BUSINESS_CARD_CA':
2635 case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
2636 case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
2637 case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
2638 case 'BUSINESS_CARD_AU':
2639 case 'BUSINESS_CARD_DK':
2640 case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
2641 case 'BUSINESS_CARD_RU':
2642 case 'BUSINESS_CARD_CZ':
2643 case 'BUSINESS_CARD_FI':
2644 case 'BUSINESS_CARD_HU':
2645 case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
2646 // Billboards
2647 case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
2648 case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
2649 case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
2650 case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
2651 case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
2652 case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
2653 case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
2654 case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
2655 // Old European Sizes
2656 // - Old Imperial English Sizes
2657 case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
2658 case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
2659 case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
2660 case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
2661 case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
2662 case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
2663 case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
2664 case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
2665 case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
2666 case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
2667 case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
2668 case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
2669 case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
2670 case 'EN_SHEET':
2671 case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
2672 case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
2673 case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
2674 case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
2675 case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
2676 case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
2677 case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
2678 case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
2679 case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
2680 case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
2681 case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
2682 case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
2683 case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
2684 case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
2685 // - Old Imperial Belgian Sizes
2686 case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
2687 case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
2688 case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
2689 case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
2690 case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
2691 case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
2692 case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
2693 case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
2694 case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
2695 case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
2696 case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
2697 case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
2698 case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
2699 case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
2700 case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
2701 case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
2702 case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
2703 // - Old Imperial French Sizes
2704 case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
2705 case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
2706 case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
2707 case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
2708 case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
2709 case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
2710 case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
2711 case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
2712 case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
2713 case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
2714 case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
2715 case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
2716 case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
2717 case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
2718 case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
2719 case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
2720 case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
2721 case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
2722 case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
2723 case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
2724 case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
2725 case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
2726 case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
2727 case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
2728 case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
2729 case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
2730 case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
2731 case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
2732 // DEFAULT ISO A4
2733 default: {$pf = array( 595.276, 841.890); break;}
2735 return $pf;
2739 * Change the format of the current page
2740 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numners (width, height) or an array containing the following measures and options:<ul>
2741 * <li>['format'] = page format name (one of the above);</li>
2742 * <li>['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li>
2743 * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
2744 * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
2745 * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
2746 * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
2747 * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
2748 * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
2749 * <li>['CropBox'] : the visible region of default user space:</li>
2750 * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
2751 * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
2752 * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
2753 * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
2754 * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
2755 * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
2756 * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
2757 * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
2758 * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
2759 * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
2760 * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
2761 * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
2762 * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
2763 * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
2764 * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
2765 * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
2766 * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
2767 * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
2768 * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
2769 * <li>['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:</li>
2770 * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
2771 * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
2772 * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
2773 * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
2774 * <li>['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation</li>
2775 * <li>['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li>
2776 * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
2777 * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
2778 * <li>['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li>
2779 * <li>['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li>
2780 * <li>['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li>
2781 * <li>['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.</li>
2782 * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
2783 * </ul>
2784 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
2785 * <li>P or Portrait (default)</li>
2786 * <li>L or Landscape</li>
2787 * <li>'' (empty string) for automatic orientation</li>
2788 * </ul>
2789 * @protected
2790 * @since 3.0.015 (2008-06-06)
2791 * @see getPageSizeFromFormat()
2793 protected function setPageFormat($format, $orientation='P') {
2794 if (!empty($format) AND isset($this->pagedim[$this->page])) {
2795 // remove inherited values
2796 unset($this->pagedim[$this->page]);
2798 if (is_string($format)) {
2799 // get page measures from format name
2800 $pf = $this->getPageSizeFromFormat($format);
2801 $this->fwPt = $pf[0];
2802 $this->fhPt = $pf[1];
2803 } else {
2804 // the boundaries of the physical medium on which the page shall be displayed or printed
2805 if (isset($format['MediaBox'])) {
2806 $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
2807 $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
2808 $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
2809 } else {
2810 if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
2811 $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
2812 } else {
2813 if (!isset($format['format'])) {
2814 // default value
2815 $format['format'] = 'A4';
2817 $pf = $this->getPageSizeFromFormat($format['format']);
2819 $this->fwPt = $pf[0];
2820 $this->fhPt = $pf[1];
2821 $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
2823 // the visible region of default user space
2824 if (isset($format['CropBox'])) {
2825 $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
2827 // the region to which the contents of the page shall be clipped when output in a production environment
2828 if (isset($format['BleedBox'])) {
2829 $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
2831 // the intended dimensions of the finished page after trimming
2832 if (isset($format['TrimBox'])) {
2833 $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
2835 // the page's meaningful content (including potential white space)
2836 if (isset($format['ArtBox'])) {
2837 $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
2839 // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
2840 if (isset($format['BoxColorInfo'])) {
2841 $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
2843 if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
2844 // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
2845 $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
2847 if (isset($format['PZ'])) {
2848 // The page's preferred zoom (magnification) factor
2849 $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
2851 if (isset($format['trans'])) {
2852 // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
2853 if (isset($format['trans']['Dur'])) {
2854 // The page's display duration
2855 $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
2857 $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
2858 if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
2859 // The transition style that shall be used when moving to this page from another during a presentation
2860 $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
2861 $valid_effect = array('Split', 'Blinds');
2862 $valid_vals = array('H', 'V');
2863 if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
2864 $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
2866 $valid_effect = array('Split', 'Box', 'Fly');
2867 $valid_vals = array('I', 'O');
2868 if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
2869 $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
2871 $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
2872 if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
2873 if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
2874 OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
2875 OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
2876 $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
2879 if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
2880 $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
2882 if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
2883 $this->pagedim[$this->page]['trans']['B'] = 'true';
2885 } else {
2886 $this->pagedim[$this->page]['trans']['S'] = 'R';
2888 if (isset($format['trans']['D'])) {
2889 // The duration of the transition effect, in seconds
2890 $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
2891 } else {
2892 $this->pagedim[$this->page]['trans']['D'] = 1;
2896 $this->setPageOrientation($orientation);
2900 * Set page boundaries.
2901 * @param $page (int) page number
2902 * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
2903 * @param $llx (float) lower-left x coordinate in user units
2904 * @param $lly (float) lower-left y coordinate in user units
2905 * @param $urx (float) upper-right x coordinate in user units
2906 * @param $ury (float) upper-right y coordinate in user units
2907 * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
2908 * @public
2909 * @since 5.0.010 (2010-05-17)
2911 public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
2912 if (!isset($this->pagedim[$page])) {
2913 // initialize array
2914 $this->pagedim[$page] = array();
2916 $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
2917 if (!in_array($type, $pageboxes)) {
2918 return;
2920 if ($points) {
2921 $k = 1;
2922 } else {
2923 $k = $this->k;
2925 $this->pagedim[$page][$type]['llx'] = ($llx * $k);
2926 $this->pagedim[$page][$type]['lly'] = ($lly * $k);
2927 $this->pagedim[$page][$type]['urx'] = ($urx * $k);
2928 $this->pagedim[$page][$type]['ury'] = ($ury * $k);
2932 * Swap X and Y coordinates of page boxes (change page boxes orientation).
2933 * @param $page (int) page number
2934 * @protected
2935 * @since 5.0.010 (2010-05-17)
2937 protected function swapPageBoxCoordinates($page) {
2938 $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
2939 foreach ($pageboxes as $type) {
2940 // swap X and Y coordinates
2941 if (isset($this->pagedim[$page][$type])) {
2942 $tmp = $this->pagedim[$page][$type]['llx'];
2943 $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
2944 $this->pagedim[$page][$type]['lly'] = $tmp;
2945 $tmp = $this->pagedim[$page][$type]['urx'];
2946 $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
2947 $this->pagedim[$page][$type]['ury'] = $tmp;
2953 * Set page orientation.
2954 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
2955 * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
2956 * @param $bottommargin (float) bottom margin of the page.
2957 * @public
2958 * @since 3.0.015 (2008-06-06)
2960 public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
2961 if (!isset($this->pagedim[$this->page]['MediaBox'])) {
2962 // the boundaries of the physical medium on which the page shall be displayed or printed
2963 $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
2965 if (!isset($this->pagedim[$this->page]['CropBox'])) {
2966 // the visible region of default user space
2967 $this->setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true);
2969 if (!isset($this->pagedim[$this->page]['BleedBox'])) {
2970 // the region to which the contents of the page shall be clipped when output in a production environment
2971 $this->setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
2973 if (!isset($this->pagedim[$this->page]['TrimBox'])) {
2974 // the intended dimensions of the finished page after trimming
2975 $this->setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
2977 if (!isset($this->pagedim[$this->page]['ArtBox'])) {
2978 // the page's meaningful content (including potential white space)
2979 $this->setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
2981 if (!isset($this->pagedim[$this->page]['Rotate'])) {
2982 // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
2983 $this->pagedim[$this->page]['Rotate'] = 0;
2985 if (!isset($this->pagedim[$this->page]['PZ'])) {
2986 // The page's preferred zoom (magnification) factor
2987 $this->pagedim[$this->page]['PZ'] = 1;
2989 if ($this->fwPt > $this->fhPt) {
2990 // landscape
2991 $default_orientation = 'L';
2992 } else {
2993 // portrait
2994 $default_orientation = 'P';
2996 $valid_orientations = array('P', 'L');
2997 if (empty($orientation)) {
2998 $orientation = $default_orientation;
2999 } else {
3000 $orientation = strtoupper($orientation{0});
3002 if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
3003 $this->CurOrientation = $orientation;
3004 $this->wPt = $this->fhPt;
3005 $this->hPt = $this->fwPt;
3006 } else {
3007 $this->CurOrientation = $default_orientation;
3008 $this->wPt = $this->fwPt;
3009 $this->hPt = $this->fhPt;
3011 if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
3012 // swap X and Y coordinates (change page orientation)
3013 $this->swapPageBoxCoordinates($this->page);
3015 $this->w = $this->wPt / $this->k;
3016 $this->h = $this->hPt / $this->k;
3017 if ($this->empty_string($autopagebreak)) {
3018 if (isset($this->AutoPageBreak)) {
3019 $autopagebreak = $this->AutoPageBreak;
3020 } else {
3021 $autopagebreak = true;
3024 if ($this->empty_string($bottommargin)) {
3025 if (isset($this->bMargin)) {
3026 $bottommargin = $this->bMargin;
3027 } else {
3028 // default value = 2 cm
3029 $bottommargin = 2 * 28.35 / $this->k;
3032 $this->SetAutoPageBreak($autopagebreak, $bottommargin);
3033 // store page dimensions
3034 $this->pagedim[$this->page]['w'] = $this->wPt;
3035 $this->pagedim[$this->page]['h'] = $this->hPt;
3036 $this->pagedim[$this->page]['wk'] = $this->w;
3037 $this->pagedim[$this->page]['hk'] = $this->h;
3038 $this->pagedim[$this->page]['tm'] = $this->tMargin;
3039 $this->pagedim[$this->page]['bm'] = $bottommargin;
3040 $this->pagedim[$this->page]['lm'] = $this->lMargin;
3041 $this->pagedim[$this->page]['rm'] = $this->rMargin;
3042 $this->pagedim[$this->page]['pb'] = $autopagebreak;
3043 $this->pagedim[$this->page]['or'] = $this->CurOrientation;
3044 $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
3045 $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
3049 * Set regular expression to detect withespaces or word separators.
3050 * The pattern delimiter must be the forward-slash character "/".
3051 * Some example patterns are:
3052 * <pre>
3053 * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
3054 * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
3055 * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
3056 * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
3057 * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
3058 * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
3059 * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
3060 * </pre>
3061 * @param $re (string) regular expression (leave empty for default).
3062 * @public
3063 * @since 4.6.016 (2009-06-15)
3065 public function setSpacesRE($re='/[^\S\xa0]/') {
3066 $this->re_spaces = $re;
3067 $re_parts = explode('/', $re);
3068 // get pattern parts
3069 $this->re_space = array();
3070 if (isset($re_parts[1]) AND !empty($re_parts[1])) {
3071 $this->re_space['p'] = $re_parts[1];
3072 } else {
3073 $this->re_space['p'] = '[\s]';
3075 // set pattern modifiers
3076 if (isset($re_parts[2]) AND !empty($re_parts[2])) {
3077 $this->re_space['m'] = $re_parts[2];
3078 } else {
3079 $this->re_space['m'] = '';
3084 * Enable or disable Right-To-Left language mode
3085 * @param $enable (Boolean) if true enable Right-To-Left language mode.
3086 * @param $resetx (Boolean) if true reset the X position on direction change.
3087 * @public
3088 * @since 2.0.000 (2008-01-03)
3090 public function setRTL($enable, $resetx=true) {
3091 $enable = $enable ? true : false;
3092 $resetx = ($resetx AND ($enable != $this->rtl));
3093 $this->rtl = $enable;
3094 $this->tmprtl = false;
3095 if ($resetx) {
3096 $this->Ln(0);
3101 * Return the RTL status
3102 * @return boolean
3103 * @public
3104 * @since 4.0.012 (2008-07-24)
3106 public function getRTL() {
3107 return $this->rtl;
3111 * Force temporary RTL language direction
3112 * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
3113 * @public
3114 * @since 2.1.000 (2008-01-09)
3116 public function setTempRTL($mode) {
3117 $newmode = false;
3118 switch (strtoupper($mode)) {
3119 case 'LTR':
3120 case 'L': {
3121 if ($this->rtl) {
3122 $newmode = 'L';
3124 break;
3126 case 'RTL':
3127 case 'R': {
3128 if (!$this->rtl) {
3129 $newmode = 'R';
3131 break;
3133 case false:
3134 default: {
3135 $newmode = false;
3136 break;
3139 $this->tmprtl = $newmode;
3143 * Return the current temporary RTL status
3144 * @return boolean
3145 * @public
3146 * @since 4.8.014 (2009-11-04)
3148 public function isRTLTextDir() {
3149 return ($this->rtl OR ($this->tmprtl == 'R'));
3153 * Set the last cell height.
3154 * @param $h (float) cell height.
3155 * @author Nicola Asuni
3156 * @public
3157 * @since 1.53.0.TC034
3159 public function setLastH($h) {
3160 $this->lasth = $h;
3164 * Reset the last cell height.
3165 * @public
3166 * @since 5.9.000 (2010-10-03)
3168 public function resetLastH() {
3169 $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
3173 * Get the last cell height.
3174 * @return last cell height
3175 * @public
3176 * @since 4.0.017 (2008-08-05)
3178 public function getLastH() {
3179 return $this->lasth;
3183 * Set the adjusting factor to convert pixels to user units.
3184 * @param $scale (float) adjusting factor to convert pixels to user units.
3185 * @author Nicola Asuni
3186 * @public
3187 * @since 1.5.2
3189 public function setImageScale($scale) {
3190 $this->imgscale = $scale;
3194 * Returns the adjusting factor to convert pixels to user units.
3195 * @return float adjusting factor to convert pixels to user units.
3196 * @author Nicola Asuni
3197 * @public
3198 * @since 1.5.2
3200 public function getImageScale() {
3201 return $this->imgscale;
3205 * Returns an array of page dimensions:
3206 * <ul><li>$this->pagedim[$this->page]['w'] = page width in points</li><li>$this->pagedim[$this->page]['h'] = height in points</li><li>$this->pagedim[$this->page]['wk'] = page width in user units</li><li>$this->pagedim[$this->page]['hk'] = page height in user units</li><li>$this->pagedim[$this->page]['tm'] = top margin</li><li>$this->pagedim[$this->page]['bm'] = bottom margin</li><li>$this->pagedim[$this->page]['lm'] = left margin</li><li>$this->pagedim[$this->page]['rm'] = right margin</li><li>$this->pagedim[$this->page]['pb'] = auto page break</li><li>$this->pagedim[$this->page]['or'] = page orientation</li><li>$this->pagedim[$this->page]['olm'] = original left margin</li><li>$this->pagedim[$this->page]['orm'] = original right margin</li><li>$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li><li>$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.</li><li>$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation<ul><li>$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li><li>$this->pagedim[$this->page]['trans']['S'] = transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li><li>$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.</li><li>$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li><li>$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li><li>$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li><li>$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0. </li><li>$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li></ul></li><li>$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed<ul><li>$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['CropBox'] : the visible region of default user space<ul><li>$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment<ul><li>$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming<ul><li>$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content<ul><li>$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points</li></ul></li></ul>
3207 * @param $pagenum (int) page number (empty = current page)
3208 * @return array of page dimensions.
3209 * @author Nicola Asuni
3210 * @public
3211 * @since 4.5.027 (2009-03-16)
3213 public function getPageDimensions($pagenum='') {
3214 if (empty($pagenum)) {
3215 $pagenum = $this->page;
3217 return $this->pagedim[$pagenum];
3221 * Returns the page width in units.
3222 * @param $pagenum (int) page number (empty = current page)
3223 * @return int page width.
3224 * @author Nicola Asuni
3225 * @public
3226 * @since 1.5.2
3227 * @see getPageDimensions()
3229 public function getPageWidth($pagenum='') {
3230 if (empty($pagenum)) {
3231 return $this->w;
3233 return $this->pagedim[$pagenum]['w'];
3237 * Returns the page height in units.
3238 * @param $pagenum (int) page number (empty = current page)
3239 * @return int page height.
3240 * @author Nicola Asuni
3241 * @public
3242 * @since 1.5.2
3243 * @see getPageDimensions()
3245 public function getPageHeight($pagenum='') {
3246 if (empty($pagenum)) {
3247 return $this->h;
3249 return $this->pagedim[$pagenum]['h'];
3253 * Returns the page break margin.
3254 * @param $pagenum (int) page number (empty = current page)
3255 * @return int page break margin.
3256 * @author Nicola Asuni
3257 * @public
3258 * @since 1.5.2
3259 * @see getPageDimensions()
3261 public function getBreakMargin($pagenum='') {
3262 if (empty($pagenum)) {
3263 return $this->bMargin;
3265 return $this->pagedim[$pagenum]['bm'];
3269 * Returns the scale factor (number of points in user unit).
3270 * @return int scale factor.
3271 * @author Nicola Asuni
3272 * @public
3273 * @since 1.5.2
3275 public function getScaleFactor() {
3276 return $this->k;
3280 * Defines the left, top and right margins.
3281 * @param $left (float) Left margin.
3282 * @param $top (float) Top margin.
3283 * @param $right (float) Right margin. Default value is the left one.
3284 * @param $keepmargins (boolean) if true overwrites the default page margins
3285 * @public
3286 * @since 1.0
3287 * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
3289 public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
3290 //Set left, top and right margins
3291 $this->lMargin = $left;
3292 $this->tMargin = $top;
3293 if ($right == -1) {
3294 $right = $left;
3296 $this->rMargin = $right;
3297 if ($keepmargins) {
3298 // overwrite original values
3299 $this->original_lMargin = $this->lMargin;
3300 $this->original_rMargin = $this->rMargin;
3305 * Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
3306 * @param $margin (float) The margin.
3307 * @public
3308 * @since 1.4
3309 * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
3311 public function SetLeftMargin($margin) {
3312 //Set left margin
3313 $this->lMargin = $margin;
3314 if (($this->page > 0) AND ($this->x < $margin)) {
3315 $this->x = $margin;
3320 * Defines the top margin. The method can be called before creating the first page.
3321 * @param $margin (float) The margin.
3322 * @public
3323 * @since 1.5
3324 * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
3326 public function SetTopMargin($margin) {
3327 //Set top margin
3328 $this->tMargin = $margin;
3329 if (($this->page > 0) AND ($this->y < $margin)) {
3330 $this->y = $margin;
3335 * Defines the right margin. The method can be called before creating the first page.
3336 * @param $margin (float) The margin.
3337 * @public
3338 * @since 1.5
3339 * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
3341 public function SetRightMargin($margin) {
3342 $this->rMargin = $margin;
3343 if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
3344 $this->x = $this->w - $margin;
3349 * Set the same internal Cell padding for top, right, bottom, left-
3350 * @param $pad (float) internal padding.
3351 * @public
3352 * @since 2.1.000 (2008-01-09)
3353 * @see getCellPaddings(), setCellPaddings()
3355 public function SetCellPadding($pad) {
3356 if ($pad >= 0) {
3357 $this->cell_padding['L'] = $pad;
3358 $this->cell_padding['T'] = $pad;
3359 $this->cell_padding['R'] = $pad;
3360 $this->cell_padding['B'] = $pad;
3365 * Set the internal Cell paddings.
3366 * @param $left (float) left padding
3367 * @param $top (float) top padding
3368 * @param $right (float) right padding
3369 * @param $bottom (float) bottom padding
3370 * @public
3371 * @since 5.9.000 (2010-10-03)
3372 * @see getCellPaddings(), SetCellPadding()
3374 public function setCellPaddings($left='', $top='', $right='', $bottom='') {
3375 if (($left !== '') AND ($left >= 0)) {
3376 $this->cell_padding['L'] = $left;
3378 if (($top !== '') AND ($top >= 0)) {
3379 $this->cell_padding['T'] = $top;
3381 if (($right !== '') AND ($right >= 0)) {
3382 $this->cell_padding['R'] = $right;
3384 if (($bottom !== '') AND ($bottom >= 0)) {
3385 $this->cell_padding['B'] = $bottom;
3390 * Get the internal Cell padding array.
3391 * @return array of padding values
3392 * @public
3393 * @since 5.9.000 (2010-10-03)
3394 * @see setCellPaddings(), SetCellPadding()
3396 public function getCellPaddings() {
3397 return $this->cell_padding;
3401 * Set the internal Cell margins.
3402 * @param $left (float) left margin
3403 * @param $top (float) top margin
3404 * @param $right (float) right margin
3405 * @param $bottom (float) bottom margin
3406 * @public
3407 * @since 5.9.000 (2010-10-03)
3408 * @see getCellMargins()
3410 public function setCellMargins($left='', $top='', $right='', $bottom='') {
3411 if (($left !== '') AND ($left >= 0)) {
3412 $this->cell_margin['L'] = $left;
3414 if (($top !== '') AND ($top >= 0)) {
3415 $this->cell_margin['T'] = $top;
3417 if (($right !== '') AND ($right >= 0)) {
3418 $this->cell_margin['R'] = $right;
3420 if (($bottom !== '') AND ($bottom >= 0)) {
3421 $this->cell_margin['B'] = $bottom;
3426 * Get the internal Cell margin array.
3427 * @return array of margin values
3428 * @public
3429 * @since 5.9.000 (2010-10-03)
3430 * @see setCellMargins()
3432 public function getCellMargins() {
3433 return $this->cell_margin;
3437 * Adjust the internal Cell padding array to take account of the line width.
3438 * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
3439 * @return array of adjustments
3440 * @public
3441 * @since 5.9.000 (2010-10-03)
3443 protected function adjustCellPadding($brd=0) {
3444 if (empty($brd)) {
3445 return;
3447 if (is_string($brd)) {
3448 // convert string to array
3449 $slen = strlen($brd);
3450 $newbrd = array();
3451 for ($i = 0; $i < $slen; ++$i) {
3452 $newbrd[$brd{$i}] = true;
3454 $brd = $newbrd;
3455 } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
3456 $brd = array('LRTB' => true);
3458 if (!is_array($brd)) {
3459 return;
3461 // store current cell padding
3462 $cp = $this->cell_padding;
3463 // select border mode
3464 if (isset($brd['mode'])) {
3465 $mode = $brd['mode'];
3466 unset($brd['mode']);
3467 } else {
3468 $mode = 'normal';
3470 // process borders
3471 foreach ($brd as $border => $style) {
3472 $line_width = $this->LineWidth;
3473 if (is_array($style) AND isset($style['width'])) {
3474 // get border width
3475 $line_width = $style['width'];
3477 $adj = 0; // line width inside the cell
3478 switch ($mode) {
3479 case 'ext': {
3480 $adj = 0;
3481 break;
3483 case 'int': {
3484 $adj = $line_width;
3485 break;
3487 case 'normal':
3488 default: {
3489 $adj = ($line_width / 2);
3490 break;
3493 // correct internal cell padding if required to avoid overlap between text and lines
3494 if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
3495 $this->cell_padding['T'] = $adj;
3497 if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
3498 $this->cell_padding['R'] = $adj;
3500 if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
3501 $this->cell_padding['B'] = $adj;
3503 if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
3504 $this->cell_padding['L'] = $adj;
3507 return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
3511 * Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
3512 * @param $auto (boolean) Boolean indicating if mode should be on or off.
3513 * @param $margin (float) Distance from the bottom of the page.
3514 * @public
3515 * @since 1.0
3516 * @see Cell(), MultiCell(), AcceptPageBreak()
3518 public function SetAutoPageBreak($auto, $margin=0) {
3519 $this->AutoPageBreak = $auto ? true : false;
3520 $this->bMargin = $margin;
3521 $this->PageBreakTrigger = $this->h - $margin;
3525 * Return the auto-page-break mode (true or false).
3526 * @return boolean auto-page-break mode
3527 * @public
3528 * @since 5.9.088
3530 public function getAutoPageBreak() {
3531 return $this->AutoPageBreak;
3535 * Defines the way the document is to be displayed by the viewer.
3536 * @param $zoom (mixed) The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use. <ul><li>fullpage: displays the entire page on screen </li><li>fullwidth: uses maximum width of window</li><li>real: uses real size (equivalent to 100% zoom)</li><li>default: uses viewer default mode</li></ul>
3537 * @param $layout (string) The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
3538 * @param $mode (string) A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
3539 * @public
3540 * @since 1.2
3542 public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
3543 if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
3544 $this->ZoomMode = $zoom;
3545 } else {
3546 $this->Error('Incorrect zoom display mode: '.$zoom);
3548 switch ($layout) {
3549 case 'default':
3550 case 'single':
3551 case 'SinglePage': {
3552 $this->LayoutMode = 'SinglePage';
3553 break;
3555 case 'continuous':
3556 case 'OneColumn': {
3557 $this->LayoutMode = 'OneColumn';
3558 break;
3560 case 'two':
3561 case 'TwoColumnLeft': {
3562 $this->LayoutMode = 'TwoColumnLeft';
3563 break;
3565 case 'TwoColumnRight': {
3566 $this->LayoutMode = 'TwoColumnRight';
3567 break;
3569 case 'TwoPageLeft': {
3570 $this->LayoutMode = 'TwoPageLeft';
3571 break;
3573 case 'TwoPageRight': {
3574 $this->LayoutMode = 'TwoPageRight';
3575 break;
3577 default: {
3578 $this->LayoutMode = 'SinglePage';
3581 switch ($mode) {
3582 case 'UseNone': {
3583 $this->PageMode = 'UseNone';
3584 break;
3586 case 'UseOutlines': {
3587 $this->PageMode = 'UseOutlines';
3588 break;
3590 case 'UseThumbs': {
3591 $this->PageMode = 'UseThumbs';
3592 break;
3594 case 'FullScreen': {
3595 $this->PageMode = 'FullScreen';
3596 break;
3598 case 'UseOC': {
3599 $this->PageMode = 'UseOC';
3600 break;
3602 case '': {
3603 $this->PageMode = 'UseAttachments';
3604 break;
3606 default: {
3607 $this->PageMode = 'UseNone';
3613 * Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
3614 * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
3615 * @param $compress (boolean) Boolean indicating if compression must be enabled.
3616 * @public
3617 * @since 1.4
3619 public function SetCompression($compress=true) {
3620 if (function_exists('gzcompress')) {
3621 $this->compress = $compress ? true : false;
3622 } else {
3623 $this->compress = false;
3628 * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
3629 * @param $mode (boolean) If true force sRGB output intent.
3630 * @public
3631 * @since 5.9.121 (2011-09-28)
3633 public function setSRGBmode($mode=false) {
3634 $this->force_srgb = $mode ? true : false;
3638 * Turn on/off Unicode mode for document information dictionary (meta tags).
3639 * This has effect only when unicode mode is set to false.
3640 * @param $unicode (boolean) if true set the meta information in Unicode
3641 * @since 5.9.027 (2010-12-01)
3642 * @public
3644 public function SetDocInfoUnicode($unicode=true) {
3645 $this->docinfounicode = $unicode ? true : false;
3649 * Defines the title of the document.
3650 * @param $title (string) The title.
3651 * @public
3652 * @since 1.2
3653 * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
3655 public function SetTitle($title) {
3656 $this->title = $title;
3660 * Defines the subject of the document.
3661 * @param $subject (string) The subject.
3662 * @public
3663 * @since 1.2
3664 * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
3666 public function SetSubject($subject) {
3667 $this->subject = $subject;
3671 * Defines the author of the document.
3672 * @param $author (string) The name of the author.
3673 * @public
3674 * @since 1.2
3675 * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
3677 public function SetAuthor($author) {
3678 $this->author = $author;
3682 * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
3683 * @param $keywords (string) The list of keywords.
3684 * @public
3685 * @since 1.2
3686 * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
3688 public function SetKeywords($keywords) {
3689 $this->keywords = $keywords;
3693 * Defines the creator of the document. This is typically the name of the application that generates the PDF.
3694 * @param $creator (string) The name of the creator.
3695 * @public
3696 * @since 1.2
3697 * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
3699 public function SetCreator($creator) {
3700 $this->creator = $creator;
3704 * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
3705 * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
3706 * @param $msg (string) The error message
3707 * @public
3708 * @since 1.0
3710 public function Error($msg) {
3711 // unset all class variables
3712 $this->_destroy(true);
3713 // exit program and print error
3714 die('<strong>TCPDF ERROR: </strong>'.$msg);
3718 * This method begins the generation of the PDF document.
3719 * It is not necessary to call it explicitly because AddPage() does it automatically.
3720 * Note: no page is created by this method
3721 * @public
3722 * @since 1.0
3723 * @see AddPage(), Close()
3725 public function Open() {
3726 $this->state = 1;
3730 * Terminates the PDF document.
3731 * It is not necessary to call this method explicitly because Output() does it automatically.
3732 * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
3733 * @public
3734 * @since 1.0
3735 * @see Open(), Output()
3737 public function Close() {
3738 if ($this->state == 3) {
3739 return;
3741 if ($this->page == 0) {
3742 $this->AddPage();
3744 $this->endLayer();
3745 // save current graphic settings
3746 $gvars = $this->getGraphicVars();
3747 $this->setEqualColumns();
3748 $this->lastpage(true);
3749 $this->SetAutoPageBreak(false);
3750 $this->x = 0;
3751 $this->y = $this->h - (1 / $this->k);
3752 $this->lMargin = 0;
3753 $this->_out('q');
3754 $this->SetFont('helvetica', '', 1);
3755 $this->setTextRenderingMode(0, false, false);
3756 $msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
3757 $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
3758 $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
3759 $this->_out('Q');
3760 // restore graphic settings
3761 $this->setGraphicVars($gvars);
3762 // close page
3763 $this->endPage();
3764 // close document
3765 $this->_enddoc();
3766 // unset all class variables (except critical ones)
3767 $this->_destroy(false);
3771 * Move pointer at the specified document page and update page dimensions.
3772 * @param $pnum (int) page number (1 ... numpages)
3773 * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
3774 * @public
3775 * @since 2.1.000 (2008-01-07)
3776 * @see getPage(), lastpage(), getNumPages()
3778 public function setPage($pnum, $resetmargins=false) {
3779 if (($pnum == $this->page) AND ($this->state == 2)) {
3780 return;
3782 if (($pnum > 0) AND ($pnum <= $this->numpages)) {
3783 $this->state = 2;
3784 // save current graphic settings
3785 //$gvars = $this->getGraphicVars();
3786 $oldpage = $this->page;
3787 $this->page = $pnum;
3788 $this->wPt = $this->pagedim[$this->page]['w'];
3789 $this->hPt = $this->pagedim[$this->page]['h'];
3790 $this->w = $this->pagedim[$this->page]['wk'];
3791 $this->h = $this->pagedim[$this->page]['hk'];
3792 $this->tMargin = $this->pagedim[$this->page]['tm'];
3793 $this->bMargin = $this->pagedim[$this->page]['bm'];
3794 $this->original_lMargin = $this->pagedim[$this->page]['olm'];
3795 $this->original_rMargin = $this->pagedim[$this->page]['orm'];
3796 $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
3797 $this->CurOrientation = $this->pagedim[$this->page]['or'];
3798 $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
3799 // restore graphic settings
3800 //$this->setGraphicVars($gvars);
3801 if ($resetmargins) {
3802 $this->lMargin = $this->pagedim[$this->page]['olm'];
3803 $this->rMargin = $this->pagedim[$this->page]['orm'];
3804 $this->SetY($this->tMargin);
3805 } else {
3806 // account for booklet mode
3807 if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
3808 $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
3809 $this->lMargin += $deltam;
3810 $this->rMargin -= $deltam;
3813 } else {
3814 $this->Error('Wrong page number on setPage() function: '.$pnum);
3819 * Reset pointer to the last document page.
3820 * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
3821 * @public
3822 * @since 2.0.000 (2008-01-04)
3823 * @see setPage(), getPage(), getNumPages()
3825 public function lastPage($resetmargins=false) {
3826 $this->setPage($this->getNumPages(), $resetmargins);
3830 * Get current document page number.
3831 * @return int page number
3832 * @public
3833 * @since 2.1.000 (2008-01-07)
3834 * @see setPage(), lastpage(), getNumPages()
3836 public function getPage() {
3837 return $this->page;
3841 * Get the total number of insered pages.
3842 * @return int number of pages
3843 * @public
3844 * @since 2.1.000 (2008-01-07)
3845 * @see setPage(), getPage(), lastpage()
3847 public function getNumPages() {
3848 return $this->numpages;
3852 * Adds a new TOC (Table Of Content) page to the document.
3853 * @param $orientation (string) page orientation.
3854 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
3855 * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
3856 * @public
3857 * @since 5.0.001 (2010-05-06)
3858 * @see AddPage(), startPage(), endPage(), endTOCPage()
3860 public function addTOCPage($orientation='', $format='', $keepmargins=false) {
3861 $this->AddPage($orientation, $format, $keepmargins, true);
3865 * Terminate the current TOC (Table Of Content) page
3866 * @public
3867 * @since 5.0.001 (2010-05-06)
3868 * @see AddPage(), startPage(), endPage(), addTOCPage()
3870 public function endTOCPage() {
3871 $this->endPage(true);
3875 * Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
3876 * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
3877 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
3878 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
3879 * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
3880 * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
3881 * @public
3882 * @since 1.0
3883 * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
3885 public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
3886 if ($this->inxobj) {
3887 // we are inside an XObject template
3888 return;
3890 if (!isset($this->original_lMargin) OR $keepmargins) {
3891 $this->original_lMargin = $this->lMargin;
3893 if (!isset($this->original_rMargin) OR $keepmargins) {
3894 $this->original_rMargin = $this->rMargin;
3896 // terminate previous page
3897 $this->endPage();
3898 // start new page
3899 $this->startPage($orientation, $format, $tocpage);
3903 * Terminate the current page
3904 * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
3905 * @public
3906 * @since 4.2.010 (2008-11-14)
3907 * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
3909 public function endPage($tocpage=false) {
3910 // check if page is already closed
3911 if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
3912 return;
3914 // print page footer
3915 $this->setFooter();
3916 // close page
3917 $this->_endpage();
3918 // mark page as closed
3919 $this->pageopen[$this->page] = false;
3920 if ($tocpage) {
3921 $this->tocpage = false;
3926 * Starts a new page to the document. The page must be closed using the endPage() function.
3927 * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
3928 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
3929 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
3930 * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
3931 * @since 4.2.010 (2008-11-14)
3932 * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
3933 * @public
3935 public function startPage($orientation='', $format='', $tocpage=false) {
3936 if ($tocpage) {
3937 $this->tocpage = true;
3939 // move page numbers of documents to be attached
3940 if ($this->tocpage) {
3941 // move reference to unexistent pages (used for page attachments)
3942 // adjust outlines
3943 $tmpoutlines = $this->outlines;
3944 foreach ($tmpoutlines as $key => $outline) {
3945 if ($outline['p'] > $this->numpages) {
3946 $this->outlines[$key]['p'] = ($outline['p'] + 1);
3949 // adjust dests
3950 $tmpdests = $this->dests;
3951 foreach ($tmpdests as $key => $dest) {
3952 if ($dest['p'] > $this->numpages) {
3953 $this->dests[$key]['p'] = ($dest['p'] + 1);
3956 // adjust links
3957 $tmplinks = $this->links;
3958 foreach ($tmplinks as $key => $link) {
3959 if ($link[0] > $this->numpages) {
3960 $this->links[$key][0] = ($link[0] + 1);
3964 if ($this->numpages > $this->page) {
3965 // this page has been already added
3966 $this->setPage($this->page + 1);
3967 $this->SetY($this->tMargin);
3968 return;
3970 // start a new page
3971 if ($this->state == 0) {
3972 $this->Open();
3974 ++$this->numpages;
3975 $this->swapMargins($this->booklet);
3976 // save current graphic settings
3977 $gvars = $this->getGraphicVars();
3978 // start new page
3979 $this->_beginpage($orientation, $format);
3980 // mark page as open
3981 $this->pageopen[$this->page] = true;
3982 // restore graphic settings
3983 $this->setGraphicVars($gvars);
3984 // mark this point
3985 $this->setPageMark();
3986 // print page header
3987 $this->setHeader();
3988 // restore graphic settings
3989 $this->setGraphicVars($gvars);
3990 // mark this point
3991 $this->setPageMark();
3992 // print table header (if any)
3993 $this->setTableHeader();
3994 // set mark for empty page check
3995 $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
3999 * Set start-writing mark on current page stream used to put borders and fills.
4000 * Borders and fills are always created after content and inserted on the position marked by this method.
4001 * This function must be called after calling Image() function for a background image.
4002 * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
4003 * @public
4004 * @since 4.0.016 (2008-07-30)
4006 public function setPageMark() {
4007 $this->intmrk[$this->page] = $this->pagelen[$this->page];
4008 $this->bordermrk[$this->page] = $this->intmrk[$this->page];
4009 $this->setContentMark();
4013 * Set start-writing mark on selected page.
4014 * Borders and fills are always created after content and inserted on the position marked by this method.
4015 * @param $page (int) page number (default is the current page)
4016 * @protected
4017 * @since 4.6.021 (2009-07-20)
4019 protected function setContentMark($page=0) {
4020 if ($page <= 0) {
4021 $page = $this->page;
4023 if (isset($this->footerlen[$page])) {
4024 $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
4025 } else {
4026 $this->cntmrk[$page] = $this->pagelen[$page];
4031 * Set header data.
4032 * @param $ln (string) header image logo
4033 * @param $lw (string) header image logo width in mm
4034 * @param $ht (string) string to print as title on document header
4035 * @param $hs (string) string to print on document header
4036 * @public
4038 public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
4039 $this->header_logo = $ln;
4040 $this->header_logo_width = $lw;
4041 $this->header_title = $ht;
4042 $this->header_string = $hs;
4046 * Returns header data:
4047 * <ul><li>$ret['logo'] = logo image</li><li>$ret['logo_width'] = width of the image logo in user units</li><li>$ret['title'] = header title</li><li>$ret['string'] = header description string</li></ul>
4048 * @return array()
4049 * @public
4050 * @since 4.0.012 (2008-07-24)
4052 public function getHeaderData() {
4053 $ret = array();
4054 $ret['logo'] = $this->header_logo;
4055 $ret['logo_width'] = $this->header_logo_width;
4056 $ret['title'] = $this->header_title;
4057 $ret['string'] = $this->header_string;
4058 return $ret;
4062 * Set header margin.
4063 * (minimum distance between header and top page margin)
4064 * @param $hm (int) distance in user units
4065 * @public
4067 public function setHeaderMargin($hm=10) {
4068 $this->header_margin = $hm;
4072 * Returns header margin in user units.
4073 * @return float
4074 * @since 4.0.012 (2008-07-24)
4075 * @public
4077 public function getHeaderMargin() {
4078 return $this->header_margin;
4082 * Set footer margin.
4083 * (minimum distance between footer and bottom page margin)
4084 * @param $fm (int) distance in user units
4085 * @public
4087 public function setFooterMargin($fm=10) {
4088 $this->footer_margin = $fm;
4092 * Returns footer margin in user units.
4093 * @return float
4094 * @since 4.0.012 (2008-07-24)
4095 * @public
4097 public function getFooterMargin() {
4098 return $this->footer_margin;
4101 * Set a flag to print page header.
4102 * @param $val (boolean) set to true to print the page header (default), false otherwise.
4103 * @public
4105 public function setPrintHeader($val=true) {
4106 $this->print_header = $val ? true : false;
4110 * Set a flag to print page footer.
4111 * @param $val (boolean) set to true to print the page footer (default), false otherwise.
4112 * @public
4114 public function setPrintFooter($val=true) {
4115 $this->print_footer = $val ? true : false;
4119 * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
4120 * @return float
4121 * @public
4123 public function getImageRBX() {
4124 return $this->img_rb_x;
4128 * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
4129 * @return float
4130 * @public
4132 public function getImageRBY() {
4133 return $this->img_rb_y;
4137 * Reset the xobject template used by Header() method.
4138 * @public
4140 public function resetHeaderTemplate() {
4141 $this->header_xobjid = -1;
4145 * Set a flag to automatically reset the xobject template used by Header() method at each page.
4146 * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
4147 * @public
4149 public function setHeaderTemplateAutoreset($val=true) {
4150 $this->header_xobj_autoreset = $val ? true : false;
4154 * This method is used to render the page header.
4155 * It is automatically called by AddPage() and could be overwritten in your own inherited class.
4156 * @public
4158 public function Header() {
4159 if ($this->header_xobjid < 0) {
4160 // start a new XObject Template
4161 $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
4162 $headerfont = $this->getHeaderFont();
4163 $headerdata = $this->getHeaderData();
4164 $this->y = $this->header_margin;
4165 if ($this->rtl) {
4166 $this->x = $this->w - $this->original_rMargin;
4167 } else {
4168 $this->x = $this->original_lMargin;
4170 if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
4171 $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
4172 if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
4173 $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
4174 } elseif ($imgtype == 'svg') {
4175 $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
4176 } else {
4177 $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
4179 $imgy = $this->getImageRBY();
4180 } else {
4181 $imgy = $this->y;
4183 $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
4184 // set starting margin for text data cell
4185 if ($this->getRTL()) {
4186 $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
4187 } else {
4188 $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
4190 $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
4191 $this->SetTextColor(0, 0, 0);
4192 // header title
4193 $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
4194 $this->SetX($header_x);
4195 $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
4196 // header string
4197 $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
4198 $this->SetX($header_x);
4199 $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
4200 // print an ending header line
4201 $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
4202 $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
4203 if ($this->rtl) {
4204 $this->SetX($this->original_rMargin);
4205 } else {
4206 $this->SetX($this->original_lMargin);
4208 $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
4209 $this->endTemplate();
4211 // print header template
4212 $x = 0;
4213 $dx = 0;
4214 if ($this->booklet AND (($this->page % 2) == 0)) {
4215 // adjust margins for booklet mode
4216 $dx = ($this->original_lMargin - $this->original_rMargin);
4218 if ($this->rtl) {
4219 $x = $this->w + $dx;
4220 } else {
4221 $x = 0 + $dx;
4223 $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
4224 if ($this->header_xobj_autoreset) {
4225 // reset header xobject template at each page
4226 $this->header_xobjid = -1;
4231 * This method is used to render the page footer.
4232 * It is automatically called by AddPage() and could be overwritten in your own inherited class.
4233 * @public
4235 public function Footer() {
4236 $cur_y = $this->y;
4237 $this->SetTextColor(0, 0, 0);
4238 //set style for cell border
4239 $line_width = 0.85 / $this->k;
4240 $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
4241 //print document barcode
4242 $barcode = $this->getBarcode();
4243 if (!empty($barcode)) {
4244 $this->Ln($line_width);
4245 $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
4246 $style = array(
4247 'position' => $this->rtl?'R':'L',
4248 'align' => $this->rtl?'R':'L',
4249 'stretch' => false,
4250 'fitwidth' => true,
4251 'cellfitalign' => '',
4252 'border' => false,
4253 'padding' => 0,
4254 'fgcolor' => array(0,0,0),
4255 'bgcolor' => false,
4256 'text' => false
4258 $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
4260 if (empty($this->pagegroups)) {
4261 $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
4262 } else {
4263 $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
4265 $this->SetY($cur_y);
4266 //Print page number
4267 if ($this->getRTL()) {
4268 $this->SetX($this->original_rMargin);
4269 $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
4270 } else {
4271 $this->SetX($this->original_lMargin);
4272 $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
4277 * This method is used to render the page header.
4278 * @protected
4279 * @since 4.0.012 (2008-07-24)
4281 protected function setHeader() {
4282 if (!$this->print_header) {
4283 return;
4285 $this->InHeader = true;
4286 $this->setGraphicVars($this->default_graphic_vars);
4287 $temp_thead = $this->thead;
4288 $temp_theadMargins = $this->theadMargins;
4289 $lasth = $this->lasth;
4290 $this->_out('q');
4291 $this->rMargin = $this->original_rMargin;
4292 $this->lMargin = $this->original_lMargin;
4293 $this->SetCellPadding(0);
4294 //set current position
4295 if ($this->rtl) {
4296 $this->SetXY($this->original_rMargin, $this->header_margin);
4297 } else {
4298 $this->SetXY($this->original_lMargin, $this->header_margin);
4300 $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
4301 $this->Header();
4302 //restore position
4303 if ($this->rtl) {
4304 $this->SetXY($this->original_rMargin, $this->tMargin);
4305 } else {
4306 $this->SetXY($this->original_lMargin, $this->tMargin);
4308 $this->_out('Q');
4309 $this->lasth = $lasth;
4310 $this->thead = $temp_thead;
4311 $this->theadMargins = $temp_theadMargins;
4312 $this->newline = false;
4313 $this->InHeader = false;
4317 * This method is used to render the page footer.
4318 * @protected
4319 * @since 4.0.012 (2008-07-24)
4321 protected function setFooter() {
4322 //Page footer
4323 $this->InFooter = true;
4324 // save current graphic settings
4325 $gvars = $this->getGraphicVars();
4326 // mark this point
4327 $this->footerpos[$this->page] = $this->pagelen[$this->page];
4328 $this->_out("\n");
4329 if ($this->print_footer) {
4330 $this->setGraphicVars($this->default_graphic_vars);
4331 $this->current_column = 0;
4332 $this->num_columns = 1;
4333 $temp_thead = $this->thead;
4334 $temp_theadMargins = $this->theadMargins;
4335 $lasth = $this->lasth;
4336 $this->_out('q');
4337 $this->rMargin = $this->original_rMargin;
4338 $this->lMargin = $this->original_lMargin;
4339 $this->SetCellPadding(0);
4340 //set current position
4341 $footer_y = $this->h - $this->footer_margin;
4342 if ($this->rtl) {
4343 $this->SetXY($this->original_rMargin, $footer_y);
4344 } else {
4345 $this->SetXY($this->original_lMargin, $footer_y);
4347 $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
4348 $this->Footer();
4349 //restore position
4350 if ($this->rtl) {
4351 $this->SetXY($this->original_rMargin, $this->tMargin);
4352 } else {
4353 $this->SetXY($this->original_lMargin, $this->tMargin);
4355 $this->_out('Q');
4356 $this->lasth = $lasth;
4357 $this->thead = $temp_thead;
4358 $this->theadMargins = $temp_theadMargins;
4360 // restore graphic settings
4361 $this->setGraphicVars($gvars);
4362 $this->current_column = $gvars['current_column'];
4363 $this->num_columns = $gvars['num_columns'];
4364 // calculate footer length
4365 $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
4366 $this->InFooter = false;
4370 * Check if we are on the page body (excluding page header and footer).
4371 * @return true if we are not in page header nor in page footer, false otherwise.
4372 * @protected
4373 * @since 5.9.091 (2011-06-15)
4375 protected function inPageBody() {
4376 return (($this->InHeader === false) AND ($this->InFooter === false));
4380 * This method is used to render the table header on new page (if any).
4381 * @protected
4382 * @since 4.5.030 (2009-03-25)
4384 protected function setTableHeader() {
4385 if ($this->num_columns > 1) {
4386 // multi column mode
4387 return;
4389 if (isset($this->theadMargins['top'])) {
4390 // restore the original top-margin
4391 $this->tMargin = $this->theadMargins['top'];
4392 $this->pagedim[$this->page]['tm'] = $this->tMargin;
4393 $this->y = $this->tMargin;
4395 if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
4396 // set margins
4397 $prev_lMargin = $this->lMargin;
4398 $prev_rMargin = $this->rMargin;
4399 $prev_cell_padding = $this->cell_padding;
4400 $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
4401 $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
4402 $this->cell_padding = $this->theadMargins['cell_padding'];
4403 if ($this->rtl) {
4404 $this->x = $this->w - $this->rMargin;
4405 } else {
4406 $this->x = $this->lMargin;
4408 // account for special "cell" mode
4409 if ($this->theadMargins['cell']) {
4410 if ($this->rtl) {
4411 $this->x -= $this->cell_padding['R'];
4412 } else {
4413 $this->x += $this->cell_padding['L'];
4416 // print table header
4417 $this->writeHTML($this->thead, false, false, false, false, '');
4418 // set new top margin to skip the table headers
4419 if (!isset($this->theadMargins['top'])) {
4420 $this->theadMargins['top'] = $this->tMargin;
4422 // store end of header position
4423 if (!isset($this->columns[0]['th'])) {
4424 $this->columns[0]['th'] = array();
4426 $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
4427 $this->tMargin = $this->y;
4428 $this->pagedim[$this->page]['tm'] = $this->tMargin;
4429 $this->lasth = 0;
4430 $this->lMargin = $prev_lMargin;
4431 $this->rMargin = $prev_rMargin;
4432 $this->cell_padding = $prev_cell_padding;
4437 * Returns the current page number.
4438 * @return int page number
4439 * @public
4440 * @since 1.0
4441 * @see getAliasNbPages()
4443 public function PageNo() {
4444 return $this->page;
4448 * Defines a new spot color.
4449 * It can be expressed in RGB components or gray scale.
4450 * The method can be called before the first page is created and the value is retained from page to page.
4451 * @param $name (string) Full name of the spot color.
4452 * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
4453 * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
4454 * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
4455 * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
4456 * @public
4457 * @since 4.0.024 (2008-09-12)
4458 * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
4460 public function AddSpotColor($name, $c, $m, $y, $k) {
4461 if (!isset($this->spot_colors[$name])) {
4462 $i = (1 + count($this->spot_colors));
4463 $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
4468 * Return the Spot color array.
4469 * @param $name (string) Name of the spot color.
4470 * @return (array) Spot color array or false if not defined.
4471 * @public
4472 * @since 5.9.125 (2011-10-03)
4474 public function getSpotColor($name) {
4475 if (isset($this->spot_colors[$name])) {
4476 return $this->spot_colors[$name];
4478 $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
4479 $color = strtolower($color);
4480 if (isset($this->spotcolor[$color])) {
4481 $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
4482 return $this->spot_colors[$name];
4484 return false;
4488 * Set the spot color for the specified type ('draw', 'fill', 'text').
4489 * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
4490 * @param $name (string) Name of the spot color.
4491 * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
4492 * @return (string) PDF color command.
4493 * @public
4494 * @since 5.9.125 (2011-10-03)
4496 public function setSpotColor($type, $name, $tint=100) {
4497 $spotcolor = $this->getSpotColor($name);
4498 if ($spotcolor === false) {
4499 $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
4501 $tint = (max(0, min(100, $tint)) / 100);
4502 $intcolor = array('C' => $spotcolor['c'], 'M' => $spotcolor['m'], 'Y' => $spotcolor['y'], 'K' => $spotcolor['k'], 'name' => $spotcolor['i']);
4503 $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
4504 switch ($type) {
4505 case 'draw': {
4506 $pdfcolor .= sprintf('CS %.3F SCN', $tint);
4507 $this->DrawColor = $pdfcolor;
4508 $this->strokecolor = $intcolor;
4509 break;
4511 case 'fill': {
4512 $pdfcolor .= sprintf('cs %.3F scn', $tint);
4513 $this->FillColor = $pdfcolor;
4514 $this->bgcolor = $intcolor;
4515 break;
4517 case 'text': {
4518 $pdfcolor .= sprintf('cs %.3F scn', $tint);
4519 $this->TextColor = $pdfcolor;
4520 $this->fgcolor = $intcolor;
4521 break;
4524 $this->ColorFlag = ($this->FillColor != $this->TextColor);
4525 if ($this->page > 0) {
4526 $this->_out($pdfcolor);
4528 if ($this->inxobj) {
4529 // we are inside an XObject template
4530 $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
4532 return $pdfcolor;
4536 * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
4537 * @param $name (string) Name of the spot color.
4538 * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
4539 * @public
4540 * @since 4.0.024 (2008-09-12)
4541 * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
4543 public function SetDrawSpotColor($name, $tint=100) {
4544 $this->setSpotColor('draw', $name, $tint);
4548 * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
4549 * @param $name (string) Name of the spot color.
4550 * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
4551 * @public
4552 * @since 4.0.024 (2008-09-12)
4553 * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
4555 public function SetFillSpotColor($name, $tint=100) {
4556 $this->setSpotColor('fill', $name, $tint);
4560 * Defines the spot color used for text.
4561 * @param $name (string) Name of the spot color.
4562 * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
4563 * @public
4564 * @since 4.0.024 (2008-09-12)
4565 * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
4567 public function SetTextSpotColor($name, $tint=100) {
4568 $this->setSpotColor('text', $name, $tint);
4572 * Set the color array for the specified type ('draw', 'fill', 'text').
4573 * It can be expressed in RGB, CMYK or GRAY SCALE components.
4574 * The method can be called before the first page is created and the value is retained from page to page.
4575 * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
4576 * @param $color (array) Array of colors (1, 3 or 4 values).
4577 * @param $ret (boolean) If true do not send the PDF command.
4578 * @return (string) The PDF command or empty string.
4579 * @public
4580 * @since 3.1.000 (2008-06-11)
4582 public function setColorArray($type, $color, $ret=false) {
4583 if (is_array($color)) {
4584 $color = array_values($color);
4585 // component: grey, RGB red or CMYK cyan
4586 $r = isset($color[0]) ? $color[0] : -1;
4587 // component: RGB green or CMYK magenta
4588 $g = isset($color[1]) ? $color[1] : -1;
4589 // component: RGB blue or CMYK yellow
4590 $b = isset($color[2]) ? $color[2] : -1;
4591 // component: CMYK black
4592 $k = isset($color[3]) ? $color[3] : -1;
4593 // spot color name
4594 $name = isset($color[4]) ? $color[4] : '';
4595 if ($r >= 0) {
4596 return $this->setColor($type, $r, $g, $b, $k, $ret, $name);
4599 return '';
4603 * Defines the color used for all drawing operations (lines, rectangles and cell borders).
4604 * It can be expressed in RGB, CMYK or GRAY SCALE components.
4605 * The method can be called before the first page is created and the value is retained from page to page.
4606 * @param $color (array) Array of colors (1, 3 or 4 values).
4607 * @param $ret (boolean) If true do not send the PDF command.
4608 * @return string the PDF command
4609 * @public
4610 * @since 3.1.000 (2008-06-11)
4611 * @see SetDrawColor()
4613 public function SetDrawColorArray($color, $ret=false) {
4614 return $this->setColorArray('draw', $color, $ret);
4618 * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
4619 * It can be expressed in RGB, CMYK or GRAY SCALE components.
4620 * The method can be called before the first page is created and the value is retained from page to page.
4621 * @param $color (array) Array of colors (1, 3 or 4 values).
4622 * @param $ret (boolean) If true do not send the PDF command.
4623 * @public
4624 * @since 3.1.000 (2008-6-11)
4625 * @see SetFillColor()
4627 public function SetFillColorArray($color, $ret=false) {
4628 return $this->setColorArray('fill', $color, $ret);
4632 * Defines the color used for text. It can be expressed in RGB components or gray scale.
4633 * The method can be called before the first page is created and the value is retained from page to page.
4634 * @param $color (array) Array of colors (1, 3 or 4 values).
4635 * @param $ret (boolean) If true do not send the PDF command.
4636 * @public
4637 * @since 3.1.000 (2008-6-11)
4638 * @see SetFillColor()
4640 public function SetTextColorArray($color, $ret=false) {
4641 return $this->setColorArray('text', $color, $ret);
4645 * Defines the color used by the specified type ('draw', 'fill', 'text').
4646 * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
4647 * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
4648 * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
4649 * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
4650 * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
4651 * @param $ret (boolean) If true do not send the command.
4652 * @param $name (string) spot color name (if any)
4653 * @return (string) The PDF command or empty string.
4654 * @public
4655 * @since 5.9.125 (2011-10-03)
4657 public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
4658 // set default values
4659 if (!is_numeric($col1)) {
4660 $col1 = 0;
4662 if (!is_numeric($col2)) {
4663 $col2 = -1;
4665 if (!is_numeric($col3)) {
4666 $col3 = -1;
4668 if (!is_numeric($col4)) {
4669 $col4 = -1;
4671 // set color by case
4672 $suffix = '';
4673 if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
4674 // Grey scale
4675 $col1 = max(0, min(255, $col1));
4676 $intcolor = array('G' => $col1);
4677 $pdfcolor = sprintf('%.3F ', ($col1 / 255));
4678 $suffix = 'g';
4679 } elseif ($col4 == -1) {
4680 // RGB
4681 $col1 = max(0, min(255, $col1));
4682 $col2 = max(0, min(255, $col2));
4683 $col3 = max(0, min(255, $col3));
4684 $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
4685 $pdfcolor = sprintf('%.3F %.3F %.3F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
4686 $suffix = 'rg';
4687 } else {
4688 $col1 = max(0, min(100, $col1));
4689 $col2 = max(0, min(100, $col2));
4690 $col3 = max(0, min(100, $col3));
4691 $col4 = max(0, min(100, $col4));
4692 if (empty($name)) {
4693 // CMYK
4694 $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
4695 $pdfcolor = sprintf('%.3F %.3F %.3F %.3F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
4696 $suffix = 'k';
4697 } else {
4698 // SPOT COLOR
4699 $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
4700 $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
4701 $pdfcolor = $this->setSpotColor($type, $name, 100);
4704 switch ($type) {
4705 case 'draw': {
4706 $pdfcolor .= strtoupper($suffix);
4707 $this->DrawColor = $pdfcolor;
4708 $this->strokecolor = $intcolor;
4709 break;
4711 case 'fill': {
4712 $pdfcolor .= $suffix;
4713 $this->FillColor = $pdfcolor;
4714 $this->bgcolor = $intcolor;
4715 break;
4717 case 'text': {
4718 $pdfcolor .= $suffix;
4719 $this->TextColor = $pdfcolor;
4720 $this->fgcolor = $intcolor;
4721 break;
4724 $this->ColorFlag = ($this->FillColor != $this->TextColor);
4725 if (($type != 'text') AND ($this->page > 0)) {
4726 if (!$ret) {
4727 $this->_out($pdfcolor);
4729 return $pdfcolor;
4731 return '';
4735 * Defines the color used for all drawing operations (lines, rectangles and cell borders). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
4736 * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
4737 * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
4738 * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
4739 * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
4740 * @param $ret (boolean) If true do not send the command.
4741 * @param $name (string) spot color name (if any)
4742 * @return string the PDF command
4743 * @public
4744 * @since 1.3
4745 * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
4747 public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
4748 return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
4752 * Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
4753 * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
4754 * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
4755 * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
4756 * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
4757 * @param $ret (boolean) If true do not send the command.
4758 * @param $name (string) Spot color name (if any).
4759 * @return (string) The PDF command.
4760 * @public
4761 * @since 1.3
4762 * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
4764 public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
4765 return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
4769 * Defines the color used for text. It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
4770 * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
4771 * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
4772 * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
4773 * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
4774 * @param $ret (boolean) If true do not send the command.
4775 * @param $name (string) Spot color name (if any).
4776 * @return (string) Empty string.
4777 * @public
4778 * @since 1.3
4779 * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
4781 public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
4782 return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
4786 * Returns the length of a string in user unit. A font must be selected.<br>
4787 * @param $s (string) The string whose length is to be computed
4788 * @param $fontname (string) Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
4789 * @param $fontstyle (string) Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line-trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
4790 * @param $fontsize (float) Font size in points. The default value is the current size.
4791 * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
4792 * @return mixed int total string length or array of characted widths
4793 * @author Nicola Asuni
4794 * @public
4795 * @since 1.2
4797 public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
4798 return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
4802 * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
4803 * @param $sa (string) The array of chars whose total length is to be computed
4804 * @param $fontname (string) Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
4805 * @param $fontstyle (string) Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
4806 * @param $fontsize (float) Font size in points. The default value is the current size.
4807 * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
4808 * @return mixed int total string length or array of characted widths
4809 * @author Nicola Asuni
4810 * @public
4811 * @since 2.4.000 (2008-03-06)
4813 public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
4814 // store current values
4815 if (!$this->empty_string($fontname)) {
4816 $prev_FontFamily = $this->FontFamily;
4817 $prev_FontStyle = $this->FontStyle;
4818 $prev_FontSizePt = $this->FontSizePt;
4819 $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
4821 // convert UTF-8 array to Latin1 if required
4822 $sa = $this->UTF8ArrToLatin1($sa);
4823 $w = 0; // total width
4824 $wa = array(); // array of characters widths
4825 foreach ($sa as $ck => $char) {
4826 // character width
4827 $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
4828 $wa[] = $cw;
4829 $w += $cw;
4831 // restore previous values
4832 if (!$this->empty_string($fontname)) {
4833 $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
4835 if ($getarray) {
4836 return $wa;
4838 return $w;
4842 * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
4843 * @param $char (int) The char code whose length is to be returned
4844 * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
4845 * @return float char width
4846 * @author Nicola Asuni
4847 * @public
4848 * @since 2.4.000 (2008-03-06)
4850 public function GetCharWidth($char, $notlast=true) {
4851 // get raw width
4852 $chw = $this->getRawCharWidth($char);
4853 if (($this->font_spacing != 0) AND $notlast) {
4854 // increase/decrease font spacing
4855 $chw += $this->font_spacing;
4857 if ($this->font_stretching != 100) {
4858 // fixed stretching mode
4859 $chw *= ($this->font_stretching / 100);
4861 return $chw;
4865 * Returns the length of the char in user unit for the current font.
4866 * @param $char (int) The char code whose length is to be returned
4867 * @return float char width
4868 * @author Nicola Asuni
4869 * @public
4870 * @since 5.9.000 (2010-09-28)
4872 public function getRawCharWidth($char) {
4873 if ($char == 173) {
4874 // SHY character will not be printed
4875 return (0);
4877 if (isset($this->CurrentFont['cw'][$char])) {
4878 $w = $this->CurrentFont['cw'][$char];
4879 } elseif (isset($this->CurrentFont['dw'])) {
4880 // default width
4881 $w = $this->CurrentFont['dw'];
4882 } elseif (isset($this->CurrentFont['cw'][32])) {
4883 // default width
4884 $w = $this->CurrentFont['cw'][32];
4885 } else {
4886 $w = 600;
4888 return ($w * $this->FontSize / 1000);
4892 * Returns the numbero of characters in a string.
4893 * @param $s (string) The input string.
4894 * @return int number of characters
4895 * @public
4896 * @since 2.0.0001 (2008-01-07)
4898 public function GetNumChars($s) {
4899 if ($this->isUnicodeFont()) {
4900 return count($this->UTF8StringToArray($s));
4902 return strlen($s);
4906 * Fill the list of available fonts ($this->fontlist).
4907 * @protected
4908 * @since 4.0.013 (2008-07-28)
4910 protected function getFontsList() {
4911 $fontsdir = opendir($this->_getfontpath());
4912 while (($file = readdir($fontsdir)) !== false) {
4913 if (substr($file, -4) == '.php') {
4914 array_push($this->fontlist, strtolower(basename($file, '.php')));
4917 closedir($fontsdir);
4921 * Imports a TrueType, Type1, core, or CID0 font and makes it available.
4922 * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
4923 * The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
4924 * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
4925 * @param $style (string) Font style. Possible values are (case insensitive):<ul><li>empty string: regular (default)</li><li>B: bold</li><li>I: italic</li><li>BI or IB: bold italic</li></ul>
4926 * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
4927 * @return array containing the font data, or false in case of error.
4928 * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
4929 * @public
4930 * @since 1.5
4931 * @see SetFont(), setFontSubsetting()
4933 public function AddFont($family, $style='', $fontfile='', $subset='default') {
4934 if ($subset === 'default') {
4935 $subset = $this->font_subsetting;
4937 if ($this->pdfa_mode) {
4938 $subset = false;
4940 if ($this->empty_string($family)) {
4941 if (!$this->empty_string($this->FontFamily)) {
4942 $family = $this->FontFamily;
4943 } else {
4944 $this->Error('Empty font family');
4947 // move embedded styles on $style
4948 if (substr($family, -1) == 'I') {
4949 $style .= 'I';
4950 $family = substr($family, 0, -1);
4952 if (substr($family, -1) == 'B') {
4953 $style .= 'B';
4954 $family = substr($family, 0, -1);
4956 // normalize family name
4957 $family = strtolower($family);
4958 if ((!$this->isunicode) AND ($family == 'arial')) {
4959 $family = 'helvetica';
4961 if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
4962 $style = '';
4964 if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
4965 // all fonts must be embedded
4966 $family = 'pdfa'.$family;
4968 $tempstyle = strtoupper($style);
4969 $style = '';
4970 // underline
4971 if (strpos($tempstyle, 'U') !== false) {
4972 $this->underline = true;
4973 } else {
4974 $this->underline = false;
4976 // line-through (deleted)
4977 if (strpos($tempstyle, 'D') !== false) {
4978 $this->linethrough = true;
4979 } else {
4980 $this->linethrough = false;
4982 // overline
4983 if (strpos($tempstyle, 'O') !== false) {
4984 $this->overline = true;
4985 } else {
4986 $this->overline = false;
4988 // bold
4989 if (strpos($tempstyle, 'B') !== false) {
4990 $style .= 'B';
4992 // oblique
4993 if (strpos($tempstyle, 'I') !== false) {
4994 $style .= 'I';
4996 $bistyle = $style;
4997 $fontkey = $family.$style;
4998 $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
4999 $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
5000 // check if the font has been already added
5001 $fb = $this->getFontBuffer($fontkey);
5002 if ($fb !== false) {
5003 if ($this->inxobj) {
5004 // we are inside an XObject template
5005 $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
5007 return $fontdata;
5009 if (isset($type)) {
5010 unset($type);
5012 if (isset($cw)) {
5013 unset($cw);
5015 // get specified font directory (if any)
5016 $fontdir = false;
5017 if (!$this->empty_string($fontfile)) {
5018 $fontdir = dirname($fontfile);
5019 if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
5020 $fontdir = '';
5021 } else {
5022 $fontdir .= '/';
5025 $missing_style = false; // true when the font style variation is missing
5026 // search and include font file
5027 if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
5028 // build a standard filenames for specified font
5029 $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
5030 // search files on various directories
5031 if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
5032 $fontfile = $fontdir.$tmp_fontfile;
5033 } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
5034 $fontfile = $this->_getfontpath().$tmp_fontfile;
5035 } elseif (file_exists($tmp_fontfile)) {
5036 $fontfile = $tmp_fontfile;
5037 } elseif (!$this->empty_string($style)) {
5038 $missing_style = true;
5039 // try to remove the style part
5040 $tmp_fontfile = str_replace(' ', '', $family).'.php';
5041 if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
5042 $fontfile = $fontdir.$tmp_fontfile;
5043 } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
5044 $fontfile = $this->_getfontpath().$tmp_fontfile;
5045 } else {
5046 $fontfile = $tmp_fontfile;
5050 // include font file
5051 if (file_exists($fontfile)) {
5052 include($fontfile);
5053 } else {
5054 $this->Error('Could not include font definition file: '.$family.'');
5056 // check font parameters
5057 if ((!isset($type)) OR (!isset($cw))) {
5058 $this->Error('The font definition file has a bad format: '.$fontfile.'');
5060 // SET default parameters
5061 if (!isset($file) OR $this->empty_string($file)) {
5062 $file = '';
5064 if (!isset($enc) OR $this->empty_string($enc)) {
5065 $enc = '';
5067 if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
5068 $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
5069 $cidinfo['uni2cid'] = array();
5071 if (!isset($ctg) OR $this->empty_string($ctg)) {
5072 $ctg = '';
5074 if (!isset($desc) OR $this->empty_string($desc)) {
5075 $desc = array();
5077 if (!isset($up) OR $this->empty_string($up)) {
5078 $up = -100;
5080 if (!isset($ut) OR $this->empty_string($ut)) {
5081 $ut = 50;
5083 if (!isset($cw) OR $this->empty_string($cw)) {
5084 $cw = array();
5086 if (!isset($dw) OR $this->empty_string($dw)) {
5087 // set default width
5088 if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
5089 $dw = $desc['MissingWidth'];
5090 } elseif (isset($cw[32])) {
5091 $dw = $cw[32];
5092 } else {
5093 $dw = 600;
5096 ++$this->numfonts;
5097 if ($type == 'core') {
5098 $name = $this->CoreFonts[$fontkey];
5099 $subset = false;
5100 } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
5101 $subset = false;
5102 } elseif ($type == 'TrueTypeUnicode') {
5103 $enc = 'Identity-H';
5104 } elseif ($type == 'cidfont0') {
5105 if ($this->pdfa_mode) {
5106 $this->Error('All fonts must be embedded in PDF/A mode!');
5108 } else {
5109 $this->Error('Unknow font type: '.$type.'');
5111 // set name if unset
5112 if (!isset($name) OR empty($name)) {
5113 $name = $fontkey;
5115 // create artificial font style variations if missing (only works with non-embedded fonts)
5116 if (($type != 'core') AND $missing_style) {
5117 // style variations
5118 $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
5119 $name .= $styles[$bistyle];
5120 // artificial bold
5121 if (strpos($bistyle, 'B') !== false) {
5122 if (isset($desc['StemV'])) {
5123 // from normal to bold
5124 $desc['StemV'] = round($desc['StemV'] * 1.75);
5125 } else {
5126 // bold
5127 $desc['StemV'] = 123;
5130 // artificial italic
5131 if (strpos($bistyle, 'I') !== false) {
5132 if (isset($desc['ItalicAngle'])) {
5133 $desc['ItalicAngle'] -= 11;
5134 } else {
5135 $desc['ItalicAngle'] = -11;
5137 if (isset($desc['Flags'])) {
5138 $desc['Flags'] |= 64; //bit 7
5139 } else {
5140 $desc['Flags'] = 64;
5144 // initialize subsetchars to contain default ASCII values (0-255)
5145 $subsetchars = array_fill(0, 256, true);
5146 $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
5147 if ($this->inxobj) {
5148 // we are inside an XObject template
5149 $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
5151 if (isset($diff) AND (!empty($diff))) {
5152 //Search existing encodings
5153 $d = 0;
5154 $nb = count($this->diffs);
5155 for ($i=1; $i <= $nb; ++$i) {
5156 if ($this->diffs[$i] == $diff) {
5157 $d = $i;
5158 break;
5161 if ($d == 0) {
5162 $d = $nb + 1;
5163 $this->diffs[$d] = $diff;
5165 $this->setFontSubBuffer($fontkey, 'diff', $d);
5167 if (!$this->empty_string($file)) {
5168 if (!isset($this->FontFiles[$file])) {
5169 if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
5170 $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
5171 } elseif ($type != 'core') {
5172 $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
5174 } else {
5175 // update fontkeys that are sharing this font file
5176 $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
5177 if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
5178 $this->FontFiles[$file]['fontkeys'][] = $fontkey;
5182 return $fontdata;
5186 * Sets the font used to print character strings.
5187 * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
5188 * The method can be called before the first page is created and the font is retained from page to page.
5189 * If you just wish to change the current font size, it is simpler to call SetFontSize().
5190 * Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:<ul><li>They are in the current directory (the one where the running script lies)</li><li>They are in one of the directories defined by the include_path parameter</li><li>They are in the directory defined by the K_PATH_FONTS constant</li></ul><br />
5191 * @param $family (string) Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):<ul><li>times (Times-Roman)</li><li>timesb (Times-Bold)</li><li>timesi (Times-Italic)</li><li>timesbi (Times-BoldItalic)</li><li>helvetica (Helvetica)</li><li>helveticab (Helvetica-Bold)</li><li>helveticai (Helvetica-Oblique)</li><li>helveticabi (Helvetica-BoldOblique)</li><li>courier (Courier)</li><li>courierb (Courier-Bold)</li><li>courieri (Courier-Oblique)</li><li>courierbi (Courier-BoldOblique)</li><li>symbol (Symbol)</li><li>zapfdingbats (ZapfDingbats)</li></ul> It is also possible to pass an empty string. In that case, the current family is retained.
5192 * @param $style (string) Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li><li>O: overline</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
5193 * @param $size (float) Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
5194 * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
5195 * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
5196 * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
5197 * @author Nicola Asuni
5198 * @public
5199 * @since 1.0
5200 * @see AddFont(), SetFontSize()
5202 public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default', $out=true) {
5203 //Select a font; size given in points
5204 if ($size == 0) {
5205 $size = $this->FontSizePt;
5207 // try to add font (if not already added)
5208 $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
5209 $this->FontFamily = $fontdata['family'];
5210 $this->FontStyle = $fontdata['style'];
5211 $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
5212 $this->SetFontSize($size, $out);
5216 * Defines the size of the current font.
5217 * @param $size (float) The font size in points.
5218 * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
5219 * @public
5220 * @since 1.0
5221 * @see SetFont()
5223 public function SetFontSize($size, $out=true) {
5224 // font size in points
5225 $this->FontSizePt = $size;
5226 // font size in user units
5227 $this->FontSize = $size / $this->k;
5228 // calculate some font metrics
5229 if (isset($this->CurrentFont['desc']['FontBBox'])) {
5230 $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
5231 $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
5232 } else {
5233 $font_height = $size * 1.219;
5235 if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
5236 $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
5238 if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
5239 $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
5241 if (!isset($font_ascent) AND !isset($font_descent)) {
5242 // core font
5243 $font_ascent = 0.76 * $font_height;
5244 $font_descent = $font_height - $font_ascent;
5245 } elseif (!isset($font_descent)) {
5246 $font_descent = $font_height - $font_ascent;
5247 } elseif (!isset($font_ascent)) {
5248 $font_ascent = $font_height - $font_descent;
5250 $this->FontAscent = ($font_ascent / $this->k);
5251 $this->FontDescent = ($font_descent / $this->k);
5252 if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
5253 $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
5258 * Return the font descent value
5259 * @param $font (string) font name
5260 * @param $style (string) font style
5261 * @param $size (float) The size (in points)
5262 * @return int font descent
5263 * @public
5264 * @author Nicola Asuni
5265 * @since 4.9.003 (2010-03-30)
5267 public function getFontDescent($font, $style='', $size=0) {
5268 $fontdata = $this->AddFont($font, $style);
5269 $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
5270 if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
5271 $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
5272 } else {
5273 $descent = 1.219 * 0.24 * $size;
5275 return ($descent / $this->k);
5279 * Return the font ascent value
5280 * @param $font (string) font name
5281 * @param $style (string) font style
5282 * @param $size (float) The size (in points)
5283 * @return int font ascent
5284 * @public
5285 * @author Nicola Asuni
5286 * @since 4.9.003 (2010-03-30)
5288 public function getFontAscent($font, $style='', $size=0) {
5289 $fontdata = $this->AddFont($font, $style);
5290 $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
5291 if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
5292 $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
5293 } else {
5294 $ascent = 1.219 * 0.76 * $size;
5296 return ($ascent / $this->k);
5300 * Defines the default monospaced font.
5301 * @param $font (string) Font name.
5302 * @public
5303 * @since 4.5.025
5305 public function SetDefaultMonospacedFont($font) {
5306 $this->default_monospaced_font = $font;
5310 * Creates a new internal link and returns its identifier. An internal link is a clickable area which directs to another place within the document.<br />
5311 * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
5312 * @public
5313 * @since 1.5
5314 * @see Cell(), Write(), Image(), Link(), SetLink()
5316 public function AddLink() {
5317 //Create a new internal link
5318 $n = count($this->links) + 1;
5319 $this->links[$n] = array(0, 0);
5320 return $n;
5324 * Defines the page and position a link points to.
5325 * @param $link (int) The link identifier returned by AddLink()
5326 * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
5327 * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
5328 * @public
5329 * @since 1.5
5330 * @see AddLink()
5332 public function SetLink($link, $y=0, $page=-1) {
5333 if ($y == -1) {
5334 $y = $this->y;
5336 if ($page == -1) {
5337 $page = $this->page;
5339 $this->links[$link] = array($page, $y);
5343 * Puts a link on a rectangular area of the page.
5344 * Text or image links are generally put via Cell(), Write() or Image(), but this method can be useful for instance to define a clickable area inside an image.
5345 * @param $x (float) Abscissa of the upper-left corner of the rectangle
5346 * @param $y (float) Ordinate of the upper-left corner of the rectangle
5347 * @param $w (float) Width of the rectangle
5348 * @param $h (float) Height of the rectangle
5349 * @param $link (mixed) URL or identifier returned by AddLink()
5350 * @param $spaces (int) number of spaces on the text to link
5351 * @public
5352 * @since 1.5
5353 * @see AddLink(), Annotation(), Cell(), Write(), Image()
5355 public function Link($x, $y, $w, $h, $link, $spaces=0) {
5356 $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
5360 * Puts a markup annotation on a rectangular area of the page.
5361 * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
5362 * @param $x (float) Abscissa of the upper-left corner of the rectangle
5363 * @param $y (float) Ordinate of the upper-left corner of the rectangle
5364 * @param $w (float) Width of the rectangle
5365 * @param $h (float) Height of the rectangle
5366 * @param $text (string) annotation text or alternate content
5367 * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
5368 * @param $spaces (int) number of spaces on the text to link
5369 * @public
5370 * @since 4.0.018 (2008-08-06)
5372 public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
5373 if ($this->inxobj) {
5374 // store parameters for later use on template
5375 $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
5376 return;
5378 if ($x === '') {
5379 $x = $this->x;
5381 if ($y === '') {
5382 $y = $this->y;
5384 // check page for no-write regions and adapt page margins if necessary
5385 list($x, $y) = $this->checkPageRegions($h, $x, $y);
5386 // recalculate coordinates to account for graphic transformations
5387 if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
5388 for ($i=$this->transfmatrix_key; $i > 0; --$i) {
5389 $maxid = count($this->transfmatrix[$i]) - 1;
5390 for ($j=$maxid; $j >= 0; --$j) {
5391 $ctm = $this->transfmatrix[$i][$j];
5392 if (isset($ctm['a'])) {
5393 $x = $x * $this->k;
5394 $y = ($this->h - $y) * $this->k;
5395 $w = $w * $this->k;
5396 $h = $h * $this->k;
5397 // top left
5398 $xt = $x;
5399 $yt = $y;
5400 $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
5401 $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
5402 // top right
5403 $xt = $x + $w;
5404 $yt = $y;
5405 $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
5406 $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
5407 // bottom left
5408 $xt = $x;
5409 $yt = $y - $h;
5410 $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
5411 $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
5412 // bottom right
5413 $xt = $x + $w;
5414 $yt = $y - $h;
5415 $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
5416 $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
5417 // new coordinates (rectangle area)
5418 $x = min($x1, $x2, $x3, $x4);
5419 $y = max($y1, $y2, $y3, $y4);
5420 $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
5421 $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
5422 $x = $x / $this->k;
5423 $y = $this->h - ($y / $this->k);
5428 if ($this->page <= 0) {
5429 $page = 1;
5430 } else {
5431 $page = $this->page;
5433 if (!isset($this->PageAnnots[$page])) {
5434 $this->PageAnnots[$page] = array();
5436 ++$this->n;
5437 $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
5438 if (!$this->pdfa_mode) {
5439 if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
5440 ++$this->n;
5441 $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
5444 // Add widgets annotation's icons
5445 if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
5446 $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
5448 if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
5449 $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
5451 if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
5452 $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
5457 * Embedd the attached files.
5458 * @since 4.4.000 (2008-12-07)
5459 * @protected
5460 * @see Annotation()
5462 protected function _putEmbeddedFiles() {
5463 if ($this->pdfa_mode) {
5464 // embedded files are not allowed in PDF/A mode
5465 return;
5467 reset($this->embeddedfiles);
5468 foreach ($this->embeddedfiles as $filename => $filedata) {
5469 $data = file_get_contents($filedata['file']);
5470 $filter = '';
5471 if ($this->compress) {
5472 $data = gzcompress($data);
5473 $filter = ' /Filter /FlateDecode';
5475 $stream = $this->_getrawstream($data, $filedata['n']);
5476 $out = $this->_getobj($filedata['n'])."\n";
5477 $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
5478 $out .= ' stream'."\n".$stream."\n".'endstream';
5479 $out .= "\n".'endobj';
5480 $this->_out($out);
5485 * Prints a text cell at the specified position.
5486 * This method allows to place a string precisely on the page.
5487 * @param $x (float) Abscissa of the cell origin
5488 * @param $y (float) Ordinate of the cell origin
5489 * @param $txt (string) String to print
5490 * @param $fstroke (int) outline size in user units (false = disable)
5491 * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
5492 * @param $ffill (boolean) if true fills the text
5493 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
5494 * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
5495 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
5496 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
5497 * @param $link (mixed) URL or identifier returned by AddLink().
5498 * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
5499 * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
5500 * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li><li>B : cell bottom</li></ul>
5501 * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
5502 * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
5503 * @public
5504 * @since 1.0
5505 * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
5507 public function Text($x, $y, $txt, $fstroke=false, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
5508 $textrendermode = $this->textrendermode;
5509 $textstrokewidth = $this->textstrokewidth;
5510 $this->setTextRenderingMode($fstroke, $ffill, $fclip);
5511 $this->SetXY($x, $y, $rtloff);
5512 $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
5513 // restore previous rendering mode
5514 $this->textrendermode = $textrendermode;
5515 $this->textstrokewidth = $textstrokewidth;
5519 * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
5520 * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
5521 * This method is called automatically and should not be called directly by the application.
5522 * @return boolean
5523 * @public
5524 * @since 1.4
5525 * @see SetAutoPageBreak()
5527 public function AcceptPageBreak() {
5528 if ($this->num_columns > 1) {
5529 // multi column mode
5530 if ($this->current_column < ($this->num_columns - 1)) {
5531 // go to next column
5532 $this->selectColumn($this->current_column + 1);
5533 } else {
5534 // add a new page
5535 $this->AddPage();
5536 // set first column
5537 $this->selectColumn(0);
5539 // avoid page breaking from checkPageBreak()
5540 return false;
5542 return $this->AutoPageBreak;
5546 * Add page if needed.
5547 * @param $h (float) Cell height. Default value: 0.
5548 * @param $y (mixed) starting y position, leave empty for current position.
5549 * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
5550 * @return boolean true in case of page break, false otherwise.
5551 * @since 3.2.000 (2008-07-01)
5552 * @protected
5554 protected function checkPageBreak($h=0, $y='', $addpage=true) {
5555 if ($this->empty_string($y)) {
5556 $y = $this->y;
5558 $current_page = $this->page;
5559 if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
5560 if ($addpage) {
5561 //Automatic page break
5562 $x = $this->x;
5563 $this->AddPage($this->CurOrientation);
5564 $this->y = $this->tMargin;
5565 $oldpage = $this->page - 1;
5566 if ($this->rtl) {
5567 if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
5568 $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
5569 } else {
5570 $this->x = $x;
5572 } else {
5573 if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
5574 $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
5575 } else {
5576 $this->x = $x;
5580 return true;
5582 if ($current_page != $this->page) {
5583 // account for columns mode
5584 return true;
5586 return false;
5590 * Removes SHY characters from text.
5591 * Unicode Data:<ul>
5592 * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
5593 * <li>HTML Entity (decimal): "&amp;#173;"</li>
5594 * <li>HTML Entity (hex): "&amp;#xad;"</li>
5595 * <li>HTML Entity (named): "&amp;shy;"</li>
5596 * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
5597 * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
5598 * <li>UTF-8 character: chr(194).chr(173)</li>
5599 * </ul>
5600 * @param $txt (string) input string
5601 * @return string without SHY characters.
5602 * @public
5603 * @since (4.5.019) 2009-02-28
5605 public function removeSHY($txt='') {
5606 $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
5607 if (!$this->isunicode) {
5608 $txt = preg_replace('/([\\xad]{1})/', '', $txt);
5610 return $txt;
5614 * Prints a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
5615 * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
5616 * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
5617 * @param $h (float) Cell height. Default value: 0.
5618 * @param $txt (string) String to print. Default value: empty string.
5619 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
5620 * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul> Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
5621 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
5622 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
5623 * @param $link (mixed) URL or identifier returned by AddLink().
5624 * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
5625 * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
5626 * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
5627 * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
5628 * @public
5629 * @since 1.0
5630 * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
5632 public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
5633 $prev_cell_margin = $this->cell_margin;
5634 $prev_cell_padding = $this->cell_padding;
5635 $this->adjustCellPadding($border);
5636 if (!$ignore_min_height) {
5637 $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
5638 if ($h < $min_cell_height) {
5639 $h = $min_cell_height;
5642 $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
5643 $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
5644 $this->cell_padding = $prev_cell_padding;
5645 $this->cell_margin = $prev_cell_margin;
5649 * Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
5650 * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
5651 * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
5652 * @param $h (float) Cell height. Default value: 0.
5653 * @param $txt (string) String to print. Default value: empty string.
5654 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
5655 * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
5656 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
5657 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
5658 * @param $link (mixed) URL or identifier returned by AddLink().
5659 * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
5660 * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
5661 * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
5662 * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul>
5663 * @return string containing cell code
5664 * @protected
5665 * @since 1.0
5666 * @see Cell()
5668 protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
5669 // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
5670 $txt = str_replace($this->unichr(160), ' ', $txt);
5671 $prev_cell_margin = $this->cell_margin;
5672 $prev_cell_padding = $this->cell_padding;
5673 $txt = $this->removeSHY($txt);
5674 $rs = ''; //string to be returned
5675 $this->adjustCellPadding($border);
5676 if (!$ignore_min_height) {
5677 $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
5678 if ($h < $min_cell_height) {
5679 $h = $min_cell_height;
5682 $k = $this->k;
5683 // check page for no-write regions and adapt page margins if necessary
5684 list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
5685 if ($this->rtl) {
5686 $x = $this->x - $this->cell_margin['R'];
5687 } else {
5688 $x = $this->x + $this->cell_margin['L'];
5690 $y = $this->y + $this->cell_margin['T'];
5691 $prev_font_stretching = $this->font_stretching;
5692 $prev_font_spacing = $this->font_spacing;
5693 // cell vertical alignment
5694 switch ($calign) {
5695 case 'A': {
5696 // font top
5697 switch ($valign) {
5698 case 'T': {
5699 // top
5700 $y -= $this->cell_padding['T'];
5701 break;
5703 case 'B': {
5704 // bottom
5705 $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
5706 break;
5708 default:
5709 case 'C':
5710 case 'M': {
5711 // center
5712 $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
5713 break;
5716 break;
5718 case 'L': {
5719 // font baseline
5720 switch ($valign) {
5721 case 'T': {
5722 // top
5723 $y -= ($this->cell_padding['T'] + $this->FontAscent);
5724 break;
5726 case 'B': {
5727 // bottom
5728 $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
5729 break;
5731 default:
5732 case 'C':
5733 case 'M': {
5734 // center
5735 $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
5736 break;
5739 break;
5741 case 'D': {
5742 // font bottom
5743 switch ($valign) {
5744 case 'T': {
5745 // top
5746 $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
5747 break;
5749 case 'B': {
5750 // bottom
5751 $y -= ($h - $this->cell_padding['B']);
5752 break;
5754 default:
5755 case 'C':
5756 case 'M': {
5757 // center
5758 $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
5759 break;
5762 break;
5764 case 'B': {
5765 // cell bottom
5766 $y -= $h;
5767 break;
5769 case 'C':
5770 case 'M': {
5771 // cell center
5772 $y -= ($h / 2);
5773 break;
5775 default:
5776 case 'T': {
5777 // cell top
5778 break;
5781 // text vertical alignment
5782 switch ($valign) {
5783 case 'T': {
5784 // top
5785 $yt = $y + $this->cell_padding['T'];
5786 break;
5788 case 'B': {
5789 // bottom
5790 $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
5791 break;
5793 default:
5794 case 'C':
5795 case 'M': {
5796 // center
5797 $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
5798 break;
5801 $basefonty = $yt + $this->FontAscent;
5802 if ($this->empty_string($w) OR ($w <= 0)) {
5803 if ($this->rtl) {
5804 $w = $x - $this->lMargin;
5805 } else {
5806 $w = $this->w - $this->rMargin - $x;
5809 $s = '';
5810 // fill and borders
5811 if (is_string($border) AND (strlen($border) == 4)) {
5812 // full border
5813 $border = 1;
5815 if ($fill OR ($border == 1)) {
5816 if ($fill) {
5817 $op = ($border == 1) ? 'B' : 'f';
5818 } else {
5819 $op = 'S';
5821 if ($this->rtl) {
5822 $xk = (($x - $w) * $k);
5823 } else {
5824 $xk = ($x * $k);
5826 $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
5828 // draw borders
5829 $s .= $this->getCellBorder($x, $y, $w, $h, $border);
5830 if ($txt != '') {
5831 $txt2 = $txt;
5832 if ($this->isunicode) {
5833 if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
5834 $txt2 = $this->UTF8ToLatin1($txt2);
5835 } else {
5836 $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
5837 $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
5838 if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
5839 // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
5840 // NOTE: this doesn't work with HTML justification
5841 // Symbols that could overlap on the font top (only works in LTR)
5842 $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
5843 $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
5844 $numchars = count($unicode); // number of chars
5845 $unik = 0;
5846 $uniblock = array();
5847 $uniblock[$unik] = array();
5848 $uniblock[$unik][] = $unicode[0];
5849 // resolve overlapping conflicts by splitting the string in several parts
5850 for ($i = 1; $i < $numchars; ++$i) {
5851 // check if symbols overlaps at top
5852 if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
5853 // move symbols to another array
5854 ++$unik;
5855 $uniblock[$unik] = array();
5856 $uniblock[$unik][] = $unicode[$i];
5857 ++$unik;
5858 $uniblock[$unik] = array();
5859 $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
5860 } else {
5861 $uniblock[$unik][] = $unicode[$i];
5864 // ---- END OF Fix for bug #2977340
5866 $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
5869 $txt2 = $this->_escape($txt2);
5870 // get current text width (considering general font stretching and spacing)
5871 $txwidth = $this->GetStringWidth($txt);
5872 $width = $txwidth;
5873 // check for stretch mode
5874 if ($stretch > 0) {
5875 // calculate ratio between cell width and text width
5876 if ($width <= 0) {
5877 $ratio = 1;
5878 } else {
5879 $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
5881 // check if stretching is required
5882 if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
5883 // the text will be stretched to fit cell width
5884 if ($stretch > 2) {
5885 // set new character spacing
5886 $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
5887 } else {
5888 // set new horizontal stretching
5889 $this->font_stretching *= $ratio;
5891 // recalculate text width (the text fills the entire cell)
5892 $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
5893 // reset alignment
5894 $align = '';
5897 if ($this->font_stretching != 100) {
5898 // apply font stretching
5899 $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
5901 if ($this->font_spacing != 0) {
5902 // increase/decrease font spacing
5903 $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
5905 if ($this->ColorFlag AND ($this->textrendermode < 4)) {
5906 $s .= 'q '.$this->TextColor.' ';
5908 // rendering mode
5909 $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
5910 // count number of spaces
5911 $ns = substr_count($txt, chr(32));
5912 // Justification
5913 $spacewidth = 0;
5914 if (($align == 'J') AND ($ns > 0)) {
5915 if ($this->isUnicodeFont()) {
5916 // get string width without spaces
5917 $width = $this->GetStringWidth(str_replace(' ', '', $txt));
5918 // calculate average space width
5919 $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
5920 if ($this->font_stretching != 100) {
5921 // word spacing is affected by stretching
5922 $spacewidth /= ($this->font_stretching / 100);
5924 // set word position to be used with TJ operator
5925 $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
5926 $unicode_justification = true;
5927 } else {
5928 // get string width
5929 $width = $txwidth;
5930 // new space width
5931 $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
5932 if ($this->font_stretching != 100) {
5933 // word spacing (Tw) is affected by stretching
5934 $spacewidth /= ($this->font_stretching / 100);
5936 // set word spacing
5937 $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
5939 $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
5941 // replace carriage return characters
5942 $txt2 = str_replace("\r", ' ', $txt2);
5943 switch ($align) {
5944 case 'C': {
5945 $dx = ($w - $width) / 2;
5946 break;
5948 case 'R': {
5949 if ($this->rtl) {
5950 $dx = $this->cell_padding['R'];
5951 } else {
5952 $dx = $w - $width - $this->cell_padding['R'];
5954 break;
5956 case 'L': {
5957 if ($this->rtl) {
5958 $dx = $w - $width - $this->cell_padding['L'];
5959 } else {
5960 $dx = $this->cell_padding['L'];
5962 break;
5964 case 'J':
5965 default: {
5966 if ($this->rtl) {
5967 $dx = $this->cell_padding['R'];
5968 } else {
5969 $dx = $this->cell_padding['L'];
5971 break;
5974 if ($this->rtl) {
5975 $xdx = $x - $dx - $width;
5976 } else {
5977 $xdx = $x + $dx;
5979 $xdk = $xdx * $k;
5980 // print text
5981 $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
5982 if (isset($uniblock)) {
5983 // print overlapping characters as separate string
5984 $xshift = 0; // horizontal shift
5985 $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
5986 $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
5987 foreach ($uniblock as $uk => $uniarr) {
5988 if (($uk % 2) == 0) {
5989 // x space to skip
5990 if ($spacewidth != 0) {
5991 // justification shift
5992 $xshift += (count(array_keys($uniarr, 32)) * $spw);
5994 $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
5995 } else {
5996 // character to print
5997 $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
5998 $topchr = $this->_escape($topchr);
5999 $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
6003 if ($this->underline) {
6004 $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
6006 if ($this->linethrough) {
6007 $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
6009 if ($this->overline) {
6010 $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
6012 if ($this->ColorFlag AND ($this->textrendermode < 4)) {
6013 $s .= ' Q';
6015 if ($link) {
6016 $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
6019 // output cell
6020 if ($s) {
6021 // output cell
6022 $rs .= $s;
6023 if ($this->font_spacing != 0) {
6024 // reset font spacing mode
6025 $rs .= ' BT 0 Tc ET';
6027 if ($this->font_stretching != 100) {
6028 // reset font stretching mode
6029 $rs .= ' BT 100 Tz ET';
6032 // reset word spacing
6033 if (!$this->isUnicodeFont() AND ($align == 'J')) {
6034 $rs .= ' BT 0 Tw ET';
6036 // reset stretching and spacing
6037 $this->font_stretching = $prev_font_stretching;
6038 $this->font_spacing = $prev_font_spacing;
6039 $this->lasth = $h;
6040 if ($ln > 0) {
6041 //Go to the beginning of the next line
6042 $this->y = $y + $h + $this->cell_margin['B'];
6043 if ($ln == 1) {
6044 if ($this->rtl) {
6045 $this->x = $this->w - $this->rMargin;
6046 } else {
6047 $this->x = $this->lMargin;
6050 } else {
6051 // go left or right by case
6052 if ($this->rtl) {
6053 $this->x = $x - $w - $this->cell_margin['L'];
6054 } else {
6055 $this->x = $x + $w + $this->cell_margin['R'];
6058 $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
6059 $rs = $gstyles.$rs;
6060 $this->cell_padding = $prev_cell_padding;
6061 $this->cell_margin = $prev_cell_margin;
6062 return $rs;
6066 * Returns the code to draw the cell border
6067 * @param $x (float) X coordinate.
6068 * @param $y (float) Y coordinate.
6069 * @param $w (float) Cell width.
6070 * @param $h (float) Cell height.
6071 * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
6072 * @return string containing cell border code
6073 * @protected
6074 * @see SetLineStyle()
6075 * @since 5.7.000 (2010-08-02)
6077 protected function getCellBorder($x, $y, $w, $h, $brd) {
6078 $s = ''; // string to be returned
6079 if (empty($brd)) {
6080 return $s;
6082 if ($brd == 1) {
6083 $brd = array('LRTB' => true);
6085 // calculate coordinates for border
6086 $k = $this->k;
6087 if ($this->rtl) {
6088 $xeL = ($x - $w) * $k;
6089 $xeR = $x * $k;
6090 } else {
6091 $xeL = $x * $k;
6092 $xeR = ($x + $w) * $k;
6094 $yeL = (($this->h - ($y + $h)) * $k);
6095 $yeT = (($this->h - $y) * $k);
6096 $xeT = $xeL;
6097 $xeB = $xeR;
6098 $yeR = $yeT;
6099 $yeB = $yeL;
6100 if (is_string($brd)) {
6101 // convert string to array
6102 $slen = strlen($brd);
6103 $newbrd = array();
6104 for ($i = 0; $i < $slen; ++$i) {
6105 $newbrd[$brd{$i}] = array('cap' => 'square', 'join' => 'miter');
6107 $brd = $newbrd;
6109 if (isset($brd['mode'])) {
6110 $mode = $brd['mode'];
6111 unset($brd['mode']);
6112 } else {
6113 $mode = 'normal';
6115 foreach ($brd as $border => $style) {
6116 if (is_array($style) AND !empty($style)) {
6117 // apply border style
6118 $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
6119 $s .= $this->SetLineStyle($style, true)."\n";
6121 switch ($mode) {
6122 case 'ext': {
6123 $off = (($this->LineWidth / 2) * $k);
6124 $xL = $xeL - $off;
6125 $xR = $xeR + $off;
6126 $yT = $yeT + $off;
6127 $yL = $yeL - $off;
6128 $xT = $xL;
6129 $xB = $xR;
6130 $yR = $yT;
6131 $yB = $yL;
6132 $w += $this->LineWidth;
6133 $h += $this->LineWidth;
6134 break;
6136 case 'int': {
6137 $off = ($this->LineWidth / 2) * $k;
6138 $xL = $xeL + $off;
6139 $xR = $xeR - $off;
6140 $yT = $yeT - $off;
6141 $yL = $yeL + $off;
6142 $xT = $xL;
6143 $xB = $xR;
6144 $yR = $yT;
6145 $yB = $yL;
6146 $w -= $this->LineWidth;
6147 $h -= $this->LineWidth;
6148 break;
6150 case 'normal':
6151 default: {
6152 $xL = $xeL;
6153 $xT = $xeT;
6154 $xB = $xeB;
6155 $xR = $xeR;
6156 $yL = $yeL;
6157 $yT = $yeT;
6158 $yB = $yeB;
6159 $yR = $yeR;
6160 break;
6163 // draw borders by case
6164 if (strlen($border) == 4) {
6165 $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
6166 } elseif (strlen($border) == 3) {
6167 if (strpos($border,'B') === false) { // LTR
6168 $s .= sprintf('%.2F %.2F m ', $xL, $yL);
6169 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6170 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6171 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6172 $s .= 'S ';
6173 } elseif (strpos($border,'L') === false) { // TRB
6174 $s .= sprintf('%.2F %.2F m ', $xT, $yT);
6175 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6176 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6177 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6178 $s .= 'S ';
6179 } elseif (strpos($border,'T') === false) { // RBL
6180 $s .= sprintf('%.2F %.2F m ', $xR, $yR);
6181 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6182 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6183 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6184 $s .= 'S ';
6185 } elseif (strpos($border,'R') === false) { // BLT
6186 $s .= sprintf('%.2F %.2F m ', $xB, $yB);
6187 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6188 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6189 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6190 $s .= 'S ';
6192 } elseif (strlen($border) == 2) {
6193 if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
6194 $s .= sprintf('%.2F %.2F m ', $xL, $yL);
6195 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6196 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6197 $s .= 'S ';
6198 } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
6199 $s .= sprintf('%.2F %.2F m ', $xT, $yT);
6200 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6201 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6202 $s .= 'S ';
6203 } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
6204 $s .= sprintf('%.2F %.2F m ', $xR, $yR);
6205 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6206 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6207 $s .= 'S ';
6208 } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
6209 $s .= sprintf('%.2F %.2F m ', $xB, $yB);
6210 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6211 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6212 $s .= 'S ';
6213 } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
6214 $s .= sprintf('%.2F %.2F m ', $xL, $yL);
6215 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6216 $s .= 'S ';
6217 $s .= sprintf('%.2F %.2F m ', $xR, $yR);
6218 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6219 $s .= 'S ';
6220 } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
6221 $s .= sprintf('%.2F %.2F m ', $xT, $yT);
6222 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6223 $s .= 'S ';
6224 $s .= sprintf('%.2F %.2F m ', $xB, $yB);
6225 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6226 $s .= 'S ';
6228 } else { // strlen($border) == 1
6229 if (strpos($border,'L') !== false) { // L
6230 $s .= sprintf('%.2F %.2F m ', $xL, $yL);
6231 $s .= sprintf('%.2F %.2F l ', $xT, $yT);
6232 $s .= 'S ';
6233 } elseif (strpos($border,'T') !== false) { // T
6234 $s .= sprintf('%.2F %.2F m ', $xT, $yT);
6235 $s .= sprintf('%.2F %.2F l ', $xR, $yR);
6236 $s .= 'S ';
6237 } elseif (strpos($border,'R') !== false) { // R
6238 $s .= sprintf('%.2F %.2F m ', $xR, $yR);
6239 $s .= sprintf('%.2F %.2F l ', $xB, $yB);
6240 $s .= 'S ';
6241 } elseif (strpos($border,'B') !== false) { // B
6242 $s .= sprintf('%.2F %.2F m ', $xB, $yB);
6243 $s .= sprintf('%.2F %.2F l ', $xL, $yL);
6244 $s .= 'S ';
6247 if (is_array($style) AND !empty($style)) {
6248 // reset border style to previous value
6249 $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
6252 return $s;
6256 * This method allows printing text with line breaks.
6257 * They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other.<br />
6258 * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
6259 * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
6260 * @param $h (float) Cell minimum height. The cell extends automatically if needed.
6261 * @param $txt (string) String to print
6262 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
6263 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align</li><li>C: center</li><li>R: right align</li><li>J: justification (default value when $ishtml=false)</li></ul>
6264 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
6265 * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right</li><li>1: to the beginning of the next line [DEFAULT]</li><li>2: below</li></ul>
6266 * @param $x (float) x position in user units
6267 * @param $y (float) y position in user units
6268 * @param $reseth (boolean) if true reset the last cell height (default true).
6269 * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
6270 * @param $ishtml (boolean) INTERNAL USE ONLY -- set to true if $txt is HTML content (default = false). Never set this parameter to true, use instead writeHTMLCell() or writeHTML() methods.
6271 * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
6272 * @param $maxh (float) maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
6273 * @param $valign (string) Vertical alignment of text (requires $maxh = $h > 0). Possible values are:<ul><li>T: TOP</li><li>M: middle</li><li>B: bottom</li></ul>. This feature works only when $ishtml=false and the cell must fit in a single page.
6274 * @param $fitcell (boolean) if true attempt to fit all the text within the cell by reducing the font size (do not work in HTML mode).
6275 * @return int Return the number of cells or 1 for html mode.
6276 * @public
6277 * @since 1.3
6278 * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
6280 public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) {
6281 $prev_cell_margin = $this->cell_margin;
6282 $prev_cell_padding = $this->cell_padding;
6283 // adjust internal padding
6284 $this->adjustCellPadding($border);
6285 $mc_padding = $this->cell_padding;
6286 $mc_margin = $this->cell_margin;
6287 $this->cell_padding['T'] = 0;
6288 $this->cell_padding['B'] = 0;
6289 $this->setCellMargins(0, 0, 0, 0);
6290 if ($this->empty_string($this->lasth) OR $reseth) {
6291 // reset row height
6292 $this->resetLastH();
6294 if (!$this->empty_string($y)) {
6295 $this->SetY($y);
6296 } else {
6297 $y = $this->GetY();
6299 $resth = 0;
6300 if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
6301 // spit cell in more pages/columns
6302 $newh = ($this->PageBreakTrigger - $y);
6303 $resth = ($h - $newh); // cell to be printed on the next page/column
6304 $h = $newh;
6306 // get current page number
6307 $startpage = $this->page;
6308 // get current column
6309 $startcolumn = $this->current_column;
6310 if (!$this->empty_string($x)) {
6311 $this->SetX($x);
6312 } else {
6313 $x = $this->GetX();
6315 // check page for no-write regions and adapt page margins if necessary
6316 list($x, $y) = $this->checkPageRegions(0, $x, $y);
6317 // apply margins
6318 $oy = $y + $mc_margin['T'];
6319 if ($this->rtl) {
6320 $ox = $this->w - $x - $mc_margin['R'];
6321 } else {
6322 $ox = $x + $mc_margin['L'];
6324 $this->x = $ox;
6325 $this->y = $oy;
6326 // set width
6327 if ($this->empty_string($w) OR ($w <= 0)) {
6328 if ($this->rtl) {
6329 $w = $this->x - $this->lMargin - $mc_margin['L'];
6330 } else {
6331 $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
6334 // store original margin values
6335 $lMargin = $this->lMargin;
6336 $rMargin = $this->rMargin;
6337 if ($this->rtl) {
6338 $this->rMargin = $this->w - $this->x;
6339 $this->lMargin = $this->x - $w;
6340 } else {
6341 $this->lMargin = $this->x;
6342 $this->rMargin = $this->w - $this->x - $w;
6344 if ($autopadding) {
6345 // add top padding
6346 $this->y += $mc_padding['T'];
6348 if ($ishtml) { // ******* Write HTML text
6349 $this->writeHTML($txt, true, false, $reseth, true, $align);
6350 $nl = 1;
6351 } else { // ******* Write simple text
6352 $prev_FontSizePt = $this->FontSizePt;
6353 // vertical alignment
6354 if ($maxh > 0) {
6355 // get text height
6356 $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
6357 if ($fitcell) {
6358 // try to reduce font size to fit text on cell (use a quick search algorithm)
6359 $fmin = 1;
6360 $fmax = $this->FontSizePt;
6361 $prev_text_height = $text_height;
6362 $maxit = 100; // max number of iterations
6363 while ($maxit > 0) {
6364 $fmid = (($fmax + $fmin) / 2);
6365 $this->SetFontSize($fmid, false);
6366 $this->resetLastH();
6367 $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
6368 if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
6369 break;
6370 } elseif ($text_height < $maxh) {
6371 $fmin = $fmid;
6372 } else {
6373 $fmax = $fmid;
6375 --$maxit;
6377 $this->SetFontSize($this->FontSizePt);
6379 if ($text_height < $maxh) {
6380 if ($valign == 'M') {
6381 // text vertically centered
6382 $this->y += (($maxh - $text_height) / 2);
6383 } elseif ($valign == 'B') {
6384 // text vertically aligned on bottom
6385 $this->y += ($maxh - $text_height);
6389 $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
6390 if ($fitcell) {
6391 // restore font size
6392 $this->SetFontSize($prev_FontSizePt);
6395 if ($autopadding) {
6396 // add bottom padding
6397 $this->y += $mc_padding['B'];
6399 // Get end-of-text Y position
6400 $currentY = $this->y;
6401 // get latest page number
6402 $endpage = $this->page;
6403 if ($resth > 0) {
6404 $skip = ($endpage - $startpage);
6405 $tmpresth = $resth;
6406 while ($tmpresth > 0) {
6407 if ($skip <= 0) {
6408 // add a page (or trig AcceptPageBreak() for multicolumn mode)
6409 $this->checkPageBreak($this->PageBreakTrigger + 1);
6411 if ($this->num_columns > 1) {
6412 $tmpresth -= ($this->h - $this->y - $this->bMargin);
6413 } else {
6414 $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
6416 --$skip;
6418 $currentY = $this->y;
6419 $endpage = $this->page;
6421 // get latest column
6422 $endcolumn = $this->current_column;
6423 if ($this->num_columns == 0) {
6424 $this->num_columns = 1;
6426 // get border modes
6427 $border_start = $this->getBorderMode($border, $position='start');
6428 $border_end = $this->getBorderMode($border, $position='end');
6429 $border_middle = $this->getBorderMode($border, $position='middle');
6430 // design borders around HTML cells.
6431 for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
6432 $ccode = '';
6433 $this->setPage($page);
6434 if ($this->num_columns < 2) {
6435 // single-column mode
6436 $this->SetX($x);
6437 $this->y = $this->tMargin;
6439 // account for margin changes
6440 if ($page > $startpage) {
6441 if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
6442 $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
6443 } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
6444 $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
6447 if ($startpage == $endpage) {
6448 // single page
6449 for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
6450 $this->selectColumn($column);
6451 if ($this->rtl) {
6452 $this->x -= $mc_margin['R'];
6453 } else {
6454 $this->x += $mc_margin['L'];
6456 if ($startcolumn == $endcolumn) { // single column
6457 $cborder = $border;
6458 $h = max($h, ($currentY - $oy));
6459 $this->y = $oy;
6460 } elseif ($column == $startcolumn) { // first column
6461 $cborder = $border_start;
6462 $this->y = $oy;
6463 $h = $this->h - $this->y - $this->bMargin;
6464 } elseif ($column == $endcolumn) { // end column
6465 $cborder = $border_end;
6466 $h = $currentY - $this->y;
6467 if ($resth > $h) {
6468 $h = $resth;
6470 } else { // middle column
6471 $cborder = $border_middle;
6472 $h = $this->h - $this->y - $this->bMargin;
6473 $resth -= $h;
6475 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
6476 } // end for each column
6477 } elseif ($page == $startpage) { // first page
6478 for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
6479 $this->selectColumn($column);
6480 if ($this->rtl) {
6481 $this->x -= $mc_margin['R'];
6482 } else {
6483 $this->x += $mc_margin['L'];
6485 if ($column == $startcolumn) { // first column
6486 $cborder = $border_start;
6487 $this->y = $oy;
6488 $h = $this->h - $this->y - $this->bMargin;
6489 } else { // middle column
6490 $cborder = $border_middle;
6491 $h = $this->h - $this->y - $this->bMargin;
6492 $resth -= $h;
6494 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
6495 } // end for each column
6496 } elseif ($page == $endpage) { // last page
6497 for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
6498 $this->selectColumn($column);
6499 if ($this->rtl) {
6500 $this->x -= $mc_margin['R'];
6501 } else {
6502 $this->x += $mc_margin['L'];
6504 if ($column == $endcolumn) {
6505 // end column
6506 $cborder = $border_end;
6507 $h = $currentY - $this->y;
6508 if ($resth > $h) {
6509 $h = $resth;
6511 } else {
6512 // middle column
6513 $cborder = $border_middle;
6514 $h = $this->h - $this->y - $this->bMargin;
6515 $resth -= $h;
6517 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
6518 } // end for each column
6519 } else { // middle page
6520 for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
6521 $this->selectColumn($column);
6522 if ($this->rtl) {
6523 $this->x -= $mc_margin['R'];
6524 } else {
6525 $this->x += $mc_margin['L'];
6527 $cborder = $border_middle;
6528 $h = $this->h - $this->y - $this->bMargin;
6529 $resth -= $h;
6530 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
6531 } // end for each column
6533 if ($cborder OR $fill) {
6534 $offsetlen = strlen($ccode);
6535 // draw border and fill
6536 if ($this->inxobj) {
6537 // we are inside an XObject template
6538 if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
6539 $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
6540 $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
6541 $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
6542 } else {
6543 $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
6544 $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
6546 $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
6547 $pstart = substr($pagebuff, 0, $pagemark);
6548 $pend = substr($pagebuff, $pagemark);
6549 $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
6550 } else {
6551 if (end($this->transfmrk[$this->page]) !== false) {
6552 $pagemarkkey = key($this->transfmrk[$this->page]);
6553 $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
6554 $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
6555 } elseif ($this->InFooter) {
6556 $pagemark = $this->footerpos[$this->page];
6557 $this->footerpos[$this->page] += $offsetlen;
6558 } else {
6559 $pagemark = $this->intmrk[$this->page];
6560 $this->intmrk[$this->page] += $offsetlen;
6562 $pagebuff = $this->getPageBuffer($this->page);
6563 $pstart = substr($pagebuff, 0, $pagemark);
6564 $pend = substr($pagebuff, $pagemark);
6565 $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
6568 } // end for each page
6569 // Get end-of-cell Y position
6570 $currentY = $this->GetY();
6571 // restore previous values
6572 if ($this->num_columns > 1) {
6573 $this->selectColumn();
6574 } else {
6575 // restore original margins
6576 $this->lMargin = $lMargin;
6577 $this->rMargin = $rMargin;
6578 if ($this->page > $startpage) {
6579 // check for margin variations between pages (i.e. booklet mode)
6580 $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
6581 $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
6582 if (($dl != 0) OR ($dr != 0)) {
6583 $this->lMargin += $dl;
6584 $this->rMargin += $dr;
6588 if ($ln > 0) {
6589 //Go to the beginning of the next line
6590 $this->SetY($currentY + $mc_margin['B']);
6591 if ($ln == 2) {
6592 $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
6594 } else {
6595 // go left or right by case
6596 $this->setPage($startpage);
6597 $this->y = $y;
6598 $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
6600 $this->setContentMark();
6601 $this->cell_padding = $prev_cell_padding;
6602 $this->cell_margin = $prev_cell_margin;
6603 return $nl;
6607 * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
6608 * @param $brd (mixed) Indicates if borders must be drawn around the cell block. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
6609 * @param $position (string) multicell position: 'start', 'middle', 'end'
6610 * @return border mode array
6611 * @protected
6612 * @since 4.4.002 (2008-12-09)
6614 protected function getBorderMode($brd, $position='start') {
6615 if ((!$this->opencell) OR empty($brd)) {
6616 return $brd;
6618 if ($brd == 1) {
6619 $brd = 'LTRB';
6621 if (is_string($brd)) {
6622 // convert string to array
6623 $slen = strlen($brd);
6624 $newbrd = array();
6625 for ($i = 0; $i < $slen; ++$i) {
6626 $newbrd[$brd{$i}] = array('cap' => 'square', 'join' => 'miter');
6628 $brd = $newbrd;
6630 foreach ($brd as $border => $style) {
6631 switch ($position) {
6632 case 'start': {
6633 if (strpos($border, 'B') !== false) {
6634 // remove bottom line
6635 $newkey = str_replace('B', '', $border);
6636 if (strlen($newkey) > 0) {
6637 $brd[$newkey] = $style;
6639 unset($brd[$border]);
6641 break;
6643 case 'middle': {
6644 if (strpos($border, 'B') !== false) {
6645 // remove bottom line
6646 $newkey = str_replace('B', '', $border);
6647 if (strlen($newkey) > 0) {
6648 $brd[$newkey] = $style;
6650 unset($brd[$border]);
6651 $border = $newkey;
6653 if (strpos($border, 'T') !== false) {
6654 // remove bottom line
6655 $newkey = str_replace('T', '', $border);
6656 if (strlen($newkey) > 0) {
6657 $brd[$newkey] = $style;
6659 unset($brd[$border]);
6661 break;
6663 case 'end': {
6664 if (strpos($border, 'T') !== false) {
6665 // remove bottom line
6666 $newkey = str_replace('T', '', $border);
6667 if (strlen($newkey) > 0) {
6668 $brd[$newkey] = $style;
6670 unset($brd[$border]);
6672 break;
6676 return $brd;
6680 * This method return the estimated number of lines for print a simple text string using Multicell() method.
6681 * @param $txt (string) String for calculating his height
6682 * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
6683 * @param $reseth (boolean) if true reset the last cell height (default false).
6684 * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
6685 * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
6686 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
6687 * @return float Return the minimal height needed for multicell method for printing the $txt param.
6688 * @author Alexander Escalona Fernández, Nicola Asuni
6689 * @public
6690 * @since 4.5.011
6692 public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
6693 if ($txt === '') {
6694 // empty string
6695 return 1;
6697 // adjust internal padding
6698 $prev_cell_padding = $this->cell_padding;
6699 $prev_lasth = $this->lasth;
6700 if (is_array($cellpadding)) {
6701 $this->cell_padding = $cellpadding;
6703 $this->adjustCellPadding($border);
6704 if ($this->empty_string($w) OR ($w <= 0)) {
6705 if ($this->rtl) {
6706 $w = $this->x - $this->lMargin;
6707 } else {
6708 $w = $this->w - $this->rMargin - $this->x;
6711 $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
6712 if ($reseth) {
6713 // reset row height
6714 $this->resetLastH();
6716 $lines = 1;
6717 $sum = 0;
6718 $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
6719 $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
6720 $length = count($chars);
6721 $lastSeparator = -1;
6722 for ($i = 0; $i < $length; ++$i) {
6723 $charWidth = $charsWidth[$i];
6724 if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
6725 $lastSeparator = $i;
6727 if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
6728 ++$lines;
6729 if ($chars[$i] == 10) {
6730 $lastSeparator = -1;
6731 $sum = 0;
6732 } elseif ($lastSeparator != -1) {
6733 $i = $lastSeparator;
6734 $lastSeparator = -1;
6735 $sum = 0;
6736 } else {
6737 $sum = $charWidth;
6739 } else {
6740 $sum += $charWidth;
6743 if ($chars[($length - 1)] == 10) {
6744 --$lines;
6746 $this->cell_padding = $prev_cell_padding;
6747 $this->lasth = $prev_lasth;
6748 return $lines;
6752 * This method return the estimated height needed for printing a simple text string using the Multicell() method.
6753 * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
6754 * @pre
6755 * // store current object
6756 * $pdf->startTransaction();
6757 * // store starting values
6758 * $start_y = $pdf->GetY();
6759 * $start_page = $pdf->getPage();
6760 * // call your printing functions with your parameters
6761 * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6762 * $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
6763 * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6764 * // get the new Y
6765 * $end_y = $pdf->GetY();
6766 * $end_page = $pdf->getPage();
6767 * // calculate height
6768 * $height = 0;
6769 * if ($end_page == $start_page) {
6770 * $height = $end_y - $start_y;
6771 * } else {
6772 * for ($page=$start_page; $page <= $end_page; ++$page) {
6773 * $this->setPage($page);
6774 * if ($page == $start_page) {
6775 * // first page
6776 * $height = $this->h - $start_y - $this->bMargin;
6777 * } elseif ($page == $end_page) {
6778 * // last page
6779 * $height = $end_y - $this->tMargin;
6780 * } else {
6781 * $height = $this->h - $this->tMargin - $this->bMargin;
6785 * // restore previous object
6786 * $pdf = $pdf->rollbackTransaction();
6788 * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
6789 * @param $txt (string) String for calculating his height
6790 * @param $reseth (boolean) if true reset the last cell height (default false).
6791 * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
6792 * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
6793 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
6794 * @return float Return the minimal height needed for multicell method for printing the $txt param.
6795 * @author Nicola Asuni, Alexander Escalona Fernández
6796 * @public
6798 public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
6799 // adjust internal padding
6800 $prev_cell_padding = $this->cell_padding;
6801 $prev_lasth = $this->lasth;
6802 if (is_array($cellpadding)) {
6803 $this->cell_padding = $cellpadding;
6805 $this->adjustCellPadding($border);
6806 $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
6807 $height = $lines * ($this->FontSize * $this->cell_height_ratio);
6808 if ($autopadding) {
6809 // add top and bottom padding
6810 $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
6812 $this->cell_padding = $prev_cell_padding;
6813 $this->lasth = $prev_lasth;
6814 return $height;
6818 * This method prints text from the current position.<br />
6819 * @param $h (float) Line height
6820 * @param $txt (string) String to print
6821 * @param $link (mixed) URL or identifier returned by AddLink()
6822 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
6823 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
6824 * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
6825 * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
6826 * @param $firstline (boolean) if true prints only the first line and return the remaining string.
6827 * @param $firstblock (boolean) if true the string is the starting of a line.
6828 * @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
6829 * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
6830 * @param $margin (array) margin array of the parent container
6831 * @return mixed Return the number of cells or the remaining string if $firstline = true.
6832 * @public
6833 * @since 1.5
6835 public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
6836 // check page for no-write regions and adapt page margins if necessary
6837 list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
6838 if (strlen($txt) == 0) {
6839 // fix empty text
6840 $txt = ' ';
6842 if ($margin === '') {
6843 // set default margins
6844 $margin = $this->cell_margin;
6846 // remove carriage returns
6847 $s = str_replace("\r", '', $txt);
6848 // check if string contains arabic text
6849 if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
6850 $arabic = true;
6851 } else {
6852 $arabic = false;
6854 // check if string contains RTL text
6855 if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
6856 $rtlmode = true;
6857 } else {
6858 $rtlmode = false;
6860 // get a char width
6861 $chrwidth = $this->GetCharWidth(46); // dot character
6862 // get array of unicode values
6863 $chars = $this->UTF8StringToArray($s);
6864 // get array of chars
6865 $uchars = $this->UTF8ArrayToUniArray($chars);
6866 // get the number of characters
6867 $nb = count($chars);
6868 // replacement for SHY character (minus symbol)
6869 $shy_replacement = 45;
6870 $shy_replacement_char = $this->unichr($shy_replacement);
6871 // widht for SHY replacement
6872 $shy_replacement_width = $this->GetCharWidth($shy_replacement);
6873 // max Y
6874 $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
6875 // page width
6876 $pw = $w = $this->w - $this->lMargin - $this->rMargin;
6877 // calculate remaining line width ($w)
6878 if ($this->rtl) {
6879 $w = $this->x - $this->lMargin;
6880 } else {
6881 $w = $this->w - $this->rMargin - $this->x;
6883 // max column width
6884 $wmax = $w - $wadj;
6885 if (!$firstline) {
6886 $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
6888 if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
6889 // a single character do not fit on column
6890 return '';
6892 // minimum row height
6893 $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
6894 $start_page = $this->page;
6895 $i = 0; // character position
6896 $j = 0; // current starting position
6897 $sep = -1; // position of the last blank space
6898 $shy = false; // true if the last blank is a soft hypen (SHY)
6899 $l = 0; // current string length
6900 $nl = 0; //number of lines
6901 $linebreak = false;
6902 $pc = 0; // previous character
6903 // for each character
6904 while ($i < $nb) {
6905 if (($maxh > 0) AND ($this->y >= $maxy) ) {
6906 break;
6908 //Get the current character
6909 $c = $chars[$i];
6910 if ($c == 10) { // 10 = "\n" = new line
6911 //Explicit line break
6912 if ($align == 'J') {
6913 if ($this->rtl) {
6914 $talign = 'R';
6915 } else {
6916 $talign = 'L';
6918 } else {
6919 $talign = $align;
6921 $tmpstr = $this->UniArrSubString($uchars, $j, $i);
6922 if ($firstline) {
6923 $startx = $this->x;
6924 $tmparr = array_slice($chars, $j, ($i - $j));
6925 if ($rtlmode) {
6926 $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
6928 $linew = $this->GetArrStringWidth($tmparr);
6929 unset($tmparr);
6930 if ($this->rtl) {
6931 $this->endlinex = $startx - $linew;
6932 } else {
6933 $this->endlinex = $startx + $linew;
6935 $w = $linew;
6936 $tmpcellpadding = $this->cell_padding;
6937 if ($maxh == 0) {
6938 $this->SetCellPadding(0);
6941 if ($firstblock AND $this->isRTLTextDir()) {
6942 $tmpstr = $this->stringRightTrim($tmpstr);
6944 // Skip newlines at the begining of a page or column
6945 if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
6946 $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
6948 unset($tmpstr);
6949 if ($firstline) {
6950 $this->cell_padding = $tmpcellpadding;
6951 return ($this->UniArrSubString($uchars, $i));
6953 ++$nl;
6954 $j = $i + 1;
6955 $l = 0;
6956 $sep = -1;
6957 $shy = false;
6958 // account for margin changes
6959 if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
6960 $this->AcceptPageBreak();
6961 if ($this->rtl) {
6962 $this->x -= $margin['R'];
6963 } else {
6964 $this->x += $margin['L'];
6966 $this->lMargin += $margin['L'];
6967 $this->rMargin += $margin['R'];
6969 $w = $this->getRemainingWidth();
6970 $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
6971 } else {
6972 // 160 is the non-breaking space.
6973 // 173 is SHY (Soft Hypen).
6974 // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
6975 // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
6976 // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
6977 if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
6978 // update last blank space position
6979 $sep = $i;
6980 // check if is a SHY
6981 if ($c == 173) {
6982 $shy = true;
6983 if ($pc == 45) {
6984 $tmp_shy_replacement_width = 0;
6985 $tmp_shy_replacement_char = '';
6986 } else {
6987 $tmp_shy_replacement_width = $shy_replacement_width;
6988 $tmp_shy_replacement_char = $shy_replacement_char;
6990 } else {
6991 $shy = false;
6994 // update string length
6995 if ($this->isUnicodeFont() AND ($arabic)) {
6996 // with bidirectional algorithm some chars may be changed affecting the line length
6997 // *** very slow ***
6998 $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
6999 } else {
7000 $l += $this->GetCharWidth($c);
7002 if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
7003 // we have reached the end of column
7004 if ($sep == -1) {
7005 // check if the line was already started
7006 if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
7007 OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
7008 // print a void cell and go to next line
7009 $this->Cell($w, $h, '', 0, 1);
7010 $linebreak = true;
7011 if ($firstline) {
7012 return ($this->UniArrSubString($uchars, $j));
7014 } else {
7015 // truncate the word because do not fit on column
7016 $tmpstr = $this->UniArrSubString($uchars, $j, $i);
7017 if ($firstline) {
7018 $startx = $this->x;
7019 $tmparr = array_slice($chars, $j, ($i - $j));
7020 if ($rtlmode) {
7021 $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
7023 $linew = $this->GetArrStringWidth($tmparr);
7024 unset($tmparr);
7025 if ($this->rtl) {
7026 $this->endlinex = $startx - $linew;
7027 } else {
7028 $this->endlinex = $startx + $linew;
7030 $w = $linew;
7031 $tmpcellpadding = $this->cell_padding;
7032 if ($maxh == 0) {
7033 $this->SetCellPadding(0);
7036 if ($firstblock AND $this->isRTLTextDir()) {
7037 $tmpstr = $this->stringRightTrim($tmpstr);
7039 $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
7040 unset($tmpstr);
7041 if ($firstline) {
7042 $this->cell_padding = $tmpcellpadding;
7043 return ($this->UniArrSubString($uchars, $i));
7045 $j = $i;
7046 --$i;
7048 } else {
7049 // word wrapping
7050 if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
7051 $endspace = 1;
7052 } else {
7053 $endspace = 0;
7055 // check the length of the next string
7056 $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
7057 $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
7058 if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
7059 // truncate the word because do not fit on a full page width
7060 $tmpstr = $this->UniArrSubString($uchars, $j, $i);
7061 if ($firstline) {
7062 $startx = $this->x;
7063 $tmparr = array_slice($chars, $j, ($i - $j));
7064 if ($rtlmode) {
7065 $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
7067 $linew = $this->GetArrStringWidth($tmparr);
7068 unset($tmparr);
7069 if ($this->rtl) {
7070 $this->endlinex = $startx - $linew;
7071 } else {
7072 $this->endlinex = $startx + $linew;
7074 $w = $linew;
7075 $tmpcellpadding = $this->cell_padding;
7076 if ($maxh == 0) {
7077 $this->SetCellPadding(0);
7080 if ($firstblock AND $this->isRTLTextDir()) {
7081 $tmpstr = $this->stringRightTrim($tmpstr);
7083 $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
7084 unset($tmpstr);
7085 if ($firstline) {
7086 $this->cell_padding = $tmpcellpadding;
7087 return ($this->UniArrSubString($uchars, $i));
7089 $j = $i;
7090 --$i;
7091 } else {
7092 // word wrapping
7093 if ($shy) {
7094 // add hypen (minus symbol) at the end of the line
7095 $shy_width = $tmp_shy_replacement_width;
7096 if ($this->rtl) {
7097 $shy_char_left = $tmp_shy_replacement_char;
7098 $shy_char_right = '';
7099 } else {
7100 $shy_char_left = '';
7101 $shy_char_right = $tmp_shy_replacement_char;
7103 } else {
7104 $shy_width = 0;
7105 $shy_char_left = '';
7106 $shy_char_right = '';
7108 $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
7109 if ($firstline) {
7110 $startx = $this->x;
7111 $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
7112 if ($rtlmode) {
7113 $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
7115 $linew = $this->GetArrStringWidth($tmparr);
7116 unset($tmparr);
7117 if ($this->rtl) {
7118 $this->endlinex = $startx - $linew - $shy_width;
7119 } else {
7120 $this->endlinex = $startx + $linew + $shy_width;
7122 $w = $linew;
7123 $tmpcellpadding = $this->cell_padding;
7124 if ($maxh == 0) {
7125 $this->SetCellPadding(0);
7128 // print the line
7129 if ($firstblock AND $this->isRTLTextDir()) {
7130 $tmpstr = $this->stringRightTrim($tmpstr);
7132 $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
7133 unset($tmpstr);
7134 if ($firstline) {
7135 // return the remaining text
7136 $this->cell_padding = $tmpcellpadding;
7137 return ($this->UniArrSubString($uchars, ($sep + $endspace)));
7139 $i = $sep;
7140 $sep = -1;
7141 $shy = false;
7142 $j = ($i+1);
7145 // account for margin changes
7146 if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
7147 $this->AcceptPageBreak();
7148 if ($this->rtl) {
7149 $this->x -= $margin['R'];
7150 } else {
7151 $this->x += $margin['L'];
7153 $this->lMargin += $margin['L'];
7154 $this->rMargin += $margin['R'];
7156 $w = $this->getRemainingWidth();
7157 $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
7158 if ($linebreak) {
7159 $linebreak = false;
7160 } else {
7161 ++$nl;
7162 $l = 0;
7166 // save last character
7167 $pc = $c;
7168 ++$i;
7169 } // end while i < nb
7170 // print last substring (if any)
7171 if ($l > 0) {
7172 switch ($align) {
7173 case 'J':
7174 case 'C': {
7175 $w = $w;
7176 break;
7178 case 'L': {
7179 if ($this->rtl) {
7180 $w = $w;
7181 } else {
7182 $w = $l;
7184 break;
7186 case 'R': {
7187 if ($this->rtl) {
7188 $w = $l;
7189 } else {
7190 $w = $w;
7192 break;
7194 default: {
7195 $w = $l;
7196 break;
7199 $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
7200 if ($firstline) {
7201 $startx = $this->x;
7202 $tmparr = array_slice($chars, $j, ($nb - $j));
7203 if ($rtlmode) {
7204 $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
7206 $linew = $this->GetArrStringWidth($tmparr);
7207 unset($tmparr);
7208 if ($this->rtl) {
7209 $this->endlinex = $startx - $linew;
7210 } else {
7211 $this->endlinex = $startx + $linew;
7213 $w = $linew;
7214 $tmpcellpadding = $this->cell_padding;
7215 if ($maxh == 0) {
7216 $this->SetCellPadding(0);
7219 if ($firstblock AND $this->isRTLTextDir()) {
7220 $tmpstr = $this->stringRightTrim($tmpstr);
7222 $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
7223 unset($tmpstr);
7224 if ($firstline) {
7225 $this->cell_padding = $tmpcellpadding;
7226 return ($this->UniArrSubString($uchars, $nb));
7228 ++$nl;
7230 if ($firstline) {
7231 return '';
7233 return $nl;
7237 * Returns the remaining width between the current position and margins.
7238 * @return int Return the remaining width
7239 * @protected
7241 protected function getRemainingWidth() {
7242 list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
7243 if ($this->rtl) {
7244 return ($this->x - $this->lMargin);
7245 } else {
7246 return ($this->w - $this->rMargin - $this->x);
7251 * Extract a slice of the $strarr array and return it as string.
7252 * @param $strarr (string) The input array of characters.
7253 * @param $start (int) the starting element of $strarr.
7254 * @param $end (int) first element that will not be returned.
7255 * @return Return part of a string
7256 * @public
7258 public function UTF8ArrSubString($strarr, $start='', $end='') {
7259 if (strlen($start) == 0) {
7260 $start = 0;
7262 if (strlen($end) == 0) {
7263 $end = count($strarr);
7265 $string = '';
7266 for ($i=$start; $i < $end; ++$i) {
7267 $string .= $this->unichr($strarr[$i]);
7269 return $string;
7273 * Extract a slice of the $uniarr array and return it as string.
7274 * @param $uniarr (string) The input array of characters.
7275 * @param $start (int) the starting element of $strarr.
7276 * @param $end (int) first element that will not be returned.
7277 * @return Return part of a string
7278 * @public
7279 * @since 4.5.037 (2009-04-07)
7281 public function UniArrSubString($uniarr, $start='', $end='') {
7282 if (strlen($start) == 0) {
7283 $start = 0;
7285 if (strlen($end) == 0) {
7286 $end = count($uniarr);
7288 $string = '';
7289 for ($i=$start; $i < $end; ++$i) {
7290 $string .= $uniarr[$i];
7292 return $string;
7296 * Convert an array of UTF8 values to array of unicode characters
7297 * @param $ta (string) The input array of UTF8 values.
7298 * @return Return array of unicode characters
7299 * @public
7300 * @since 4.5.037 (2009-04-07)
7302 public function UTF8ArrayToUniArray($ta) {
7303 return array_map(array($this, 'unichr'), $ta);
7307 * Returns the unicode caracter specified by UTF-8 value
7308 * @param $c (int) UTF-8 value
7309 * @return Returns the specified character.
7310 * @author Miguel Perez, Nicola Asuni
7311 * @public
7312 * @since 2.3.000 (2008-03-05)
7314 public function unichr($c) {
7315 if (!$this->isunicode) {
7316 return chr($c);
7317 } elseif ($c <= 0x7F) {
7318 // one byte
7319 return chr($c);
7320 } elseif ($c <= 0x7FF) {
7321 // two bytes
7322 return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
7323 } elseif ($c <= 0xFFFF) {
7324 // three bytes
7325 return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
7326 } elseif ($c <= 0x10FFFF) {
7327 // four bytes
7328 return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
7329 } else {
7330 return '';
7335 * Return the image type given the file name or array returned by getimagesize() function.
7336 * @param $imgfile (string) image file name
7337 * @param $iminfo (array) array of image information returned by getimagesize() function.
7338 * @return string image type
7339 * @since 4.8.017 (2009-11-27)
7341 public function getImageFileType($imgfile, $iminfo=array()) {
7342 $type = '';
7343 if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
7344 $mime = explode('/', $iminfo['mime']);
7345 if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
7346 $type = strtolower(trim($mime[1]));
7349 if (empty($type)) {
7350 $fileinfo = pathinfo($imgfile);
7351 if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
7352 $type = strtolower(trim($fileinfo['extension']));
7355 if ($type == 'jpg') {
7356 $type = 'jpeg';
7358 return $type;
7362 * Set the block dimensions accounting for page breaks and page/column fitting
7363 * @param $w (float) width
7364 * @param $h (float) height
7365 * @param $x (float) X coordinate
7366 * @param $y (float) Y coodiante
7367 * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
7368 * @return array($w, $h, $x, $y)
7369 * @protected
7370 * @since 5.5.009 (2010-07-05)
7372 protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
7373 if ($w <= 0) {
7374 // set maximum width
7375 $w = ($this->w - $this->lMargin - $this->rMargin);
7377 if ($h <= 0) {
7378 // set maximum height
7379 $h = ($this->PageBreakTrigger - $this->tMargin);
7381 // resize the block to be vertically contained on a single page or single column
7382 if ($fitonpage OR $this->AutoPageBreak) {
7383 $ratio_wh = ($w / $h);
7384 if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
7385 $h = $this->PageBreakTrigger - $this->tMargin;
7386 $w = ($h * $ratio_wh);
7388 // resize the block to be horizontally contained on a single page or single column
7389 if ($fitonpage) {
7390 $maxw = ($this->w - $this->lMargin - $this->rMargin);
7391 if ($w > $maxw) {
7392 $w = $maxw;
7393 $h = ($w / $ratio_wh);
7397 // Check whether we need a new page or new column first as this does not fit
7398 $prev_x = $this->x;
7399 $prev_y = $this->y;
7400 if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
7401 $y = $this->y;
7402 if ($this->rtl) {
7403 $x += ($prev_x - $this->x);
7404 } else {
7405 $x += ($this->x - $prev_x);
7407 $this->newline = true;
7409 // resize the block to be contained on the remaining available page or column space
7410 if ($fitonpage) {
7411 $ratio_wh = ($w / $h);
7412 if (($y + $h) > $this->PageBreakTrigger) {
7413 $h = $this->PageBreakTrigger - $y;
7414 $w = ($h * $ratio_wh);
7416 if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
7417 $w = $this->w - $this->rMargin - $x;
7418 $h = ($w / $ratio_wh);
7419 } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
7420 $w = $x - $this->lMargin;
7421 $h = ($w / $ratio_wh);
7424 return array($w, $h, $x, $y);
7428 * Puts an image in the page.
7429 * The upper-left corner must be given.
7430 * The dimensions can be specified in different ways:<ul>
7431 * <li>explicit width and height (expressed in user unit)</li>
7432 * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
7433 * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
7434 * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
7435 * The format can be specified explicitly or inferred from the file extension.<br />
7436 * It is possible to put a link on the image.<br />
7437 * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
7438 * @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
7439 * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
7440 * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
7441 * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
7442 * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
7443 * @param $type (string) Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
7444 * @param $link (mixed) URL or identifier returned by AddLink().
7445 * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
7446 * @param $resize (mixed) If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
7447 * @param $dpi (int) dot-per-inch resolution used on resize
7448 * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
7449 * @param $ismask (boolean) true if this image is a mask, false otherwise
7450 * @param $imgmask (mixed) image object returned by this function or false
7451 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
7452 * @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
7453 * @param $hidden (boolean) If true do not display the image.
7454 * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
7455 * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
7456 * @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
7457 * @return image information
7458 * @public
7459 * @since 1.1
7461 public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) {
7462 if ($x === '') {
7463 $x = $this->x;
7465 if ($y === '') {
7466 $y = $this->y;
7468 // check page for no-write regions and adapt page margins if necessary
7469 list($x, $y) = $this->checkPageRegions($h, $x, $y);
7470 $cached_file = false; // true when the file is cached
7471 $exurl = ''; // external streams
7472 // check if we are passing an image as file or string
7473 if ($file{0} === '@') {
7474 // image from string
7475 $imgdata = substr($file, 1);
7476 $file = K_PATH_CACHE.'img_'.md5($imgdata);
7477 $fp = fopen($file, 'w');
7478 fwrite($fp, $imgdata);
7479 fclose($fp);
7480 unset($imgdata);
7481 $cached_file = true;
7482 $imsize = @getimagesize($file);
7483 if ($imsize === FALSE) {
7484 unlink($file);
7485 $cached_file = false;
7487 } else { // image file
7488 if ($file{0} === '*') {
7489 // image as external stream
7490 $file = substr($file, 1);
7491 $exurl = $file;
7493 // check if is local file
7494 if (!@file_exists($file)) {
7495 // encode spaces on filename (file is probably an URL)
7496 $file = str_replace(' ', '%20', $file);
7498 if (@file_exists($file)) {
7499 // get image dimensions
7500 $imsize = @getimagesize($file);
7501 } else {
7502 $imsize = false;
7504 if ($imsize === FALSE) {
7505 if (function_exists('curl_init')) {
7506 // try to get remote file data using cURL
7507 $cs = curl_init(); // curl session
7508 curl_setopt($cs, CURLOPT_URL, $file);
7509 curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
7510 curl_setopt($cs, CURLOPT_FAILONERROR, true);
7511 curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
7512 curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
7513 curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
7514 curl_setopt($cs, CURLOPT_TIMEOUT, 30);
7515 curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
7516 curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
7517 curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
7518 $imgdata = curl_exec($cs);
7519 curl_close($cs);
7520 if ($imgdata !== FALSE) {
7521 // copy image to cache
7522 $file = K_PATH_CACHE.'img_'.md5($imgdata);
7523 $fp = fopen($file, 'w');
7524 fwrite($fp, $imgdata);
7525 fclose($fp);
7526 unset($imgdata);
7527 $cached_file = true;
7528 $imsize = @getimagesize($file);
7529 if ($imsize === FALSE) {
7530 unlink($file);
7531 $cached_file = false;
7534 } elseif (($w > 0) AND ($h > 0)) {
7535 // get measures from specified data
7536 $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
7537 $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
7538 $imsize = array($pw, $ph);
7542 if ($imsize === FALSE) {
7543 if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
7544 // get measures from specified data
7545 $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
7546 $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
7547 $imsize = array($pw, $ph);
7548 } else {
7549 $this->Error('[Image] Unable to get image: '.$file);
7552 // get original image width and height in pixels
7553 list($pixw, $pixh) = $imsize;
7554 // calculate image width and height on document
7555 if (($w <= 0) AND ($h <= 0)) {
7556 // convert image size to document unit
7557 $w = $this->pixelsToUnits($pixw);
7558 $h = $this->pixelsToUnits($pixh);
7559 } elseif ($w <= 0) {
7560 $w = $h * $pixw / $pixh;
7561 } elseif ($h <= 0) {
7562 $h = $w * $pixh / $pixw;
7563 } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
7564 if (strlen($fitbox) !== 2) {
7565 // set default alignment
7566 $fitbox = '--';
7568 // scale image dimensions proportionally to fit within the ($w, $h) box
7569 if ((($w * $pixh) / ($h * $pixw)) < 1) {
7570 // store current height
7571 $oldh = $h;
7572 // calculate new height
7573 $h = $w * $pixh / $pixw;
7574 // height difference
7575 $hdiff = ($oldh - $h);
7576 // vertical alignment
7577 switch (strtoupper($fitbox{1})) {
7578 case 'T': {
7579 break;
7581 case 'M': {
7582 $y += ($hdiff / 2);
7583 break;
7585 case 'B': {
7586 $y += $hdiff;
7587 break;
7590 } else {
7591 // store current width
7592 $oldw = $w;
7593 // calculate new width
7594 $w = $h * $pixw / $pixh;
7595 // width difference
7596 $wdiff = ($oldw - $w);
7597 // horizontal alignment
7598 switch (strtoupper($fitbox{0})) {
7599 case 'L': {
7600 if ($this->rtl) {
7601 $x -= $wdiff;
7603 break;
7605 case 'C': {
7606 if ($this->rtl) {
7607 $x -= ($wdiff / 2);
7608 } else {
7609 $x += ($wdiff / 2);
7611 break;
7613 case 'R': {
7614 if (!$this->rtl) {
7615 $x += $wdiff;
7617 break;
7622 // fit the image on available space
7623 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
7624 // calculate new minimum dimensions in pixels
7625 $neww = round($w * $this->k * $dpi / $this->dpi);
7626 $newh = round($h * $this->k * $dpi / $this->dpi);
7627 // check if resize is necessary (resize is used only to reduce the image)
7628 $newsize = ($neww * $newh);
7629 $pixsize = ($pixw * $pixh);
7630 if (intval($resize) == 2) {
7631 $resize = true;
7632 } elseif ($newsize >= $pixsize) {
7633 $resize = false;
7635 // check if image has been already added on document
7636 $newimage = true;
7637 if (in_array($file, $this->imagekeys)) {
7638 $newimage = false;
7639 // get existing image data
7640 $info = $this->getImageBuffer($file);
7641 if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
7642 // check if the newer image is larger
7643 $oldsize = ($info['w'] * $info['h']);
7644 if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
7645 $newimage = true;
7648 } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
7649 // check for cached images with alpha channel
7650 $filehash = md5($file);
7651 $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
7652 $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
7653 if (in_array($tempfile_plain, $this->imagekeys)) {
7654 // get existing image data
7655 $info = $this->getImageBuffer($tempfile_plain);
7656 // check if the newer image is larger
7657 $oldsize = ($info['w'] * $info['h']);
7658 if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
7659 $newimage = true;
7660 } else {
7661 $newimage = false;
7662 // embed mask image
7663 $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
7664 // embed image, masked with previously embedded mask
7665 return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
7669 if ($newimage) {
7670 //First use of image, get info
7671 $type = strtolower($type);
7672 if ($type == '') {
7673 $type = $this->getImageFileType($file, $imsize);
7674 } elseif ($type == 'jpg') {
7675 $type = 'jpeg';
7677 $mqr = $this->get_mqr();
7678 $this->set_mqr(false);
7679 // Specific image handlers
7680 $mtd = '_parse'.$type;
7681 // GD image handler function
7682 $gdfunction = 'imagecreatefrom'.$type;
7683 $info = false;
7684 if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
7685 // TCPDF image functions
7686 $info = $this->$mtd($file);
7687 if ($info == 'pngalpha') {
7688 return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
7691 if (!$info) {
7692 if (function_exists($gdfunction)) {
7693 // GD library
7694 $img = $gdfunction($file);
7695 if ($resize) {
7696 $imgr = imagecreatetruecolor($neww, $newh);
7697 if (($type == 'gif') OR ($type == 'png')) {
7698 $imgr = $this->_setGDImageTransparency($imgr, $img);
7700 imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
7701 if (($type == 'gif') OR ($type == 'png')) {
7702 $info = $this->_toPNG($imgr);
7703 } else {
7704 $info = $this->_toJPEG($imgr);
7706 } else {
7707 if (($type == 'gif') OR ($type == 'png')) {
7708 $info = $this->_toPNG($img);
7709 } else {
7710 $info = $this->_toJPEG($img);
7713 } elseif (extension_loaded('imagick')) {
7714 // ImageMagick library
7715 $img = new Imagick();
7716 if ($type == 'SVG') {
7717 // get SVG file content
7718 $svgimg = file_get_contents($file);
7719 // get width and height
7720 $regs = array();
7721 if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
7722 $svgtag = $regs[1];
7723 $tmp = array();
7724 if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
7725 $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
7726 $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
7727 $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
7728 } else {
7729 $ow = $w;
7731 $tmp = array();
7732 if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
7733 $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
7734 $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
7735 $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
7736 } else {
7737 $oh = $h;
7739 $tmp = array();
7740 if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
7741 $vbw = ($ow * $this->imgscale * $this->k);
7742 $vbh = ($oh * $this->imgscale * $this->k);
7743 $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
7744 $svgtag = $vbox.$svgtag;
7746 $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
7748 $img->readImageBlob($svgimg);
7749 } else {
7750 $img->readImage($file);
7752 if ($resize) {
7753 $img->resizeImage($neww, $newh, 10, 1, false);
7755 $img->setCompressionQuality($this->jpeg_quality);
7756 $img->setImageFormat('jpeg');
7757 $tempname = tempnam(K_PATH_CACHE, 'jpg_');
7758 $img->writeImage($tempname);
7759 $info = $this->_parsejpeg($tempname);
7760 unlink($tempname);
7761 $img->destroy();
7762 } else {
7763 return;
7766 if ($info === false) {
7767 //If false, we cannot process image
7768 return;
7770 $this->set_mqr($mqr);
7771 if ($ismask) {
7772 // force grayscale
7773 $info['cs'] = 'DeviceGray';
7775 $info['i'] = $this->numimages;
7776 if (!in_array($file, $this->imagekeys)) {
7777 ++$info['i'];
7779 if ($imgmask !== false) {
7780 $info['masked'] = $imgmask;
7782 if (!empty($exurl)) {
7783 $info['exurl'] = $exurl;
7785 // array of alternative images
7786 $info['altimgs'] = $altimgs;
7787 // add image to document
7788 $this->setImageBuffer($file, $info);
7790 if ($cached_file) {
7791 // remove cached file
7792 unlink($file);
7794 // set alignment
7795 $this->img_rb_y = $y + $h;
7796 // set alignment
7797 if ($this->rtl) {
7798 if ($palign == 'L') {
7799 $ximg = $this->lMargin;
7800 } elseif ($palign == 'C') {
7801 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
7802 } elseif ($palign == 'R') {
7803 $ximg = $this->w - $this->rMargin - $w;
7804 } else {
7805 $ximg = $x - $w;
7807 $this->img_rb_x = $ximg;
7808 } else {
7809 if ($palign == 'L') {
7810 $ximg = $this->lMargin;
7811 } elseif ($palign == 'C') {
7812 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
7813 } elseif ($palign == 'R') {
7814 $ximg = $this->w - $this->rMargin - $w;
7815 } else {
7816 $ximg = $x;
7818 $this->img_rb_x = $ximg + $w;
7820 if ($ismask OR $hidden) {
7821 // image is not displayed
7822 return $info['i'];
7824 $xkimg = $ximg * $this->k;
7825 if (!$alt) {
7826 // only non-alternative immages will be set
7827 $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
7829 if (!empty($border)) {
7830 $bx = $this->x;
7831 $by = $this->y;
7832 $this->x = $ximg;
7833 if ($this->rtl) {
7834 $this->x += $w;
7836 $this->y = $y;
7837 $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
7838 $this->x = $bx;
7839 $this->y = $by;
7841 if ($link) {
7842 $this->Link($ximg, $y, $w, $h, $link, 0);
7844 // set pointer to align the next text/objects
7845 switch($align) {
7846 case 'T': {
7847 $this->y = $y;
7848 $this->x = $this->img_rb_x;
7849 break;
7851 case 'M': {
7852 $this->y = $y + round($h/2);
7853 $this->x = $this->img_rb_x;
7854 break;
7856 case 'B': {
7857 $this->y = $this->img_rb_y;
7858 $this->x = $this->img_rb_x;
7859 break;
7861 case 'N': {
7862 $this->SetY($this->img_rb_y);
7863 break;
7865 default:{
7866 break;
7869 $this->endlinex = $this->img_rb_x;
7870 if ($this->inxobj) {
7871 // we are inside an XObject template
7872 $this->xobjects[$this->xobjid]['images'][] = $info['i'];
7874 return $info['i'];
7878 * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
7879 * @param $mqr (boolean) FALSE for off, TRUE for on.
7880 * @since 4.6.025 (2009-08-17)
7882 public function set_mqr($mqr) {
7883 if (!defined('PHP_VERSION_ID')) {
7884 $version = PHP_VERSION;
7885 define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
7887 if (PHP_VERSION_ID < 50300) {
7888 @set_magic_quotes_runtime($mqr);
7893 * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
7894 * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
7895 * @since 4.6.025 (2009-08-17)
7897 public function get_mqr() {
7898 if (!defined('PHP_VERSION_ID')) {
7899 $version = PHP_VERSION;
7900 define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
7902 if (PHP_VERSION_ID < 50300) {
7903 return @get_magic_quotes_runtime();
7905 return 0;
7909 * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
7910 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
7911 * @param $image (image) Image object.
7912 * return image JPEG image object.
7913 * @protected
7915 protected function _toJPEG($image) {
7916 $tempname = tempnam(K_PATH_CACHE, 'jpg_');
7917 imagejpeg($image, $tempname, $this->jpeg_quality);
7918 imagedestroy($image);
7919 $retvars = $this->_parsejpeg($tempname);
7920 // tidy up by removing temporary image
7921 unlink($tempname);
7922 return $retvars;
7926 * Convert the loaded image to a PNG and then return a structure for the PDF creator.
7927 * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
7928 * @param $image (image) Image object.
7929 * return image PNG image object.
7930 * @protected
7931 * @since 4.9.016 (2010-04-20)
7933 protected function _toPNG($image) {
7934 // set temporary image file name
7935 $tempname = tempnam(K_PATH_CACHE, 'jpg_');
7936 // turn off interlaced mode
7937 imageinterlace($image, 0);
7938 // create temporary PNG image
7939 imagepng($image, $tempname);
7940 // remove image from memory
7941 imagedestroy($image);
7942 // get PNG image data
7943 $retvars = $this->_parsepng($tempname);
7944 // tidy up by removing temporary image
7945 unlink($tempname);
7946 return $retvars;
7950 * Set the transparency for the given GD image.
7951 * @param $new_image (image) GD image object
7952 * @param $image (image) GD image object.
7953 * return GD image object.
7954 * @protected
7955 * @since 4.9.016 (2010-04-20)
7957 protected function _setGDImageTransparency($new_image, $image) {
7958 // transparency index
7959 $tid = imagecolortransparent($image);
7960 // default transparency color
7961 $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
7962 if ($tid >= 0) {
7963 // get the colors for the transparency index
7964 $tcol = imagecolorsforindex($image, $tid);
7966 $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
7967 imagefill($new_image, 0, 0, $tid);
7968 imagecolortransparent($new_image, $tid);
7969 return $new_image;
7973 * Extract info from a JPEG file without using the GD library.
7974 * @param $file (string) image file to parse
7975 * @return array structure containing the image data
7976 * @protected
7978 protected function _parsejpeg($file) {
7979 $a = getimagesize($file);
7980 if (empty($a)) {
7981 $this->Error('Missing or incorrect image file: '.$file);
7983 if ($a[2] != 2) {
7984 $this->Error('Not a JPEG file: '.$file);
7986 // bits per pixel
7987 $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
7988 // number of image channels
7989 if (!isset($a['channels'])) {
7990 $channels = 3;
7991 } else {
7992 $channels = intval($a['channels']);
7994 // default colour space
7995 switch ($channels) {
7996 case 1: {
7997 $colspace = 'DeviceGray';
7998 break;
8000 case 3: {
8001 $colspace = 'DeviceRGB';
8002 break;
8004 case 4: {
8005 $colspace = 'DeviceCMYK';
8006 break;
8008 default: {
8009 $channels = 3;
8010 $colspace = 'DeviceRGB';
8011 break;
8014 // get file content
8015 $data = file_get_contents($file);
8016 // check for embedded ICC profile
8017 $icc = array();
8018 $offset = 0;
8019 while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
8020 // get ICC sequence length
8021 $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
8022 // marker sequence number
8023 $msn = max(1, ord($data{($pos + 12)}));
8024 // number of markers (total of APP2 used)
8025 $nom = max(1, ord($data{($pos + 13)}));
8026 // get sequence segment
8027 $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
8028 // move forward to next sequence
8029 $offset = ($pos + 14 + $length);
8031 // order and compact ICC segments
8032 if (count($icc) > 0) {
8033 ksort($icc);
8034 $icc = implode('', $icc);
8035 if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
8036 // invalid ICC profile
8037 $icc = false;
8039 } else {
8040 $icc = false;
8042 return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
8046 * Extract info from a PNG file without using the GD library.
8047 * @param $file (string) image file to parse
8048 * @return array structure containing the image data
8049 * @protected
8051 protected function _parsepng($file) {
8052 $f = fopen($file, 'rb');
8053 if ($f === false) {
8054 $this->Error('Can\'t open image file: '.$file);
8056 //Check signature
8057 if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
8058 $this->Error('Not a PNG file: '.$file);
8060 //Read header chunk
8061 fread($f, 4);
8062 if (fread($f, 4) != 'IHDR') {
8063 $this->Error('Incorrect PNG file: '.$file);
8065 $w = $this->_freadint($f);
8066 $h = $this->_freadint($f);
8067 $bpc = ord(fread($f, 1));
8068 if ($bpc > 8) {
8069 //$this->Error('16-bit depth not supported: '.$file);
8070 fclose($f);
8071 return false;
8073 $ct = ord(fread($f, 1));
8074 if ($ct == 0) {
8075 $colspace = 'DeviceGray';
8076 } elseif ($ct == 2) {
8077 $colspace = 'DeviceRGB';
8078 } elseif ($ct == 3) {
8079 $colspace = 'Indexed';
8080 } else {
8081 // alpha channel
8082 fclose($f);
8083 return 'pngalpha';
8085 if (ord(fread($f, 1)) != 0) {
8086 //$this->Error('Unknown compression method: '.$file);
8087 fclose($f);
8088 return false;
8090 if (ord(fread($f, 1)) != 0) {
8091 //$this->Error('Unknown filter method: '.$file);
8092 fclose($f);
8093 return false;
8095 if (ord(fread($f, 1)) != 0) {
8096 //$this->Error('Interlacing not supported: '.$file);
8097 fclose($f);
8098 return false;
8100 fread($f, 4);
8101 $channels = ($ct == 2 ? 3 : 1);
8102 $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
8103 //Scan chunks looking for palette, transparency and image data
8104 $pal = '';
8105 $trns = '';
8106 $data = '';
8107 $icc = false;
8108 do {
8109 $n = $this->_freadint($f);
8110 $type = fread($f, 4);
8111 if ($type == 'PLTE') {
8112 // read palette
8113 $pal = $this->rfread($f, $n);
8114 fread($f, 4);
8115 } elseif ($type == 'tRNS') {
8116 // read transparency info
8117 $t = $this->rfread($f, $n);
8118 if ($ct == 0) {
8119 $trns = array(ord($t{1}));
8120 } elseif ($ct == 2) {
8121 $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
8122 } else {
8123 $pos = strpos($t, chr(0));
8124 if ($pos !== false) {
8125 $trns = array($pos);
8128 fread($f, 4);
8129 } elseif ($type == 'IDAT') {
8130 // read image data block
8131 $data .= $this->rfread($f, $n);
8132 fread($f, 4);
8133 } elseif ($type == 'iCCP') {
8134 // skip profile name and null separator
8135 $len = 0;
8136 while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
8137 ++$len;
8139 // get compression method
8140 if (ord(fread($f, 1)) != 0) {
8141 //$this->Error('Unknown filter method: '.$file);
8142 fclose($f);
8143 return false;
8145 // read ICC Color Profile
8146 $icc = $this->rfread($f, ($n - $len - 2));
8147 // decompress profile
8148 $icc = gzuncompress($icc);
8149 fread($f, 4);
8150 } elseif ($type == 'IEND') {
8151 break;
8152 } else {
8153 $this->rfread($f, $n + 4);
8155 } while ($n);
8156 if (($colspace == 'Indexed') AND (empty($pal))) {
8157 //$this->Error('Missing palette in '.$file);
8158 fclose($f);
8159 return false;
8161 fclose($f);
8162 return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
8166 * Binary-safe and URL-safe file read.
8167 * Reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met: length bytes have been read; EOF (end of file) is reached.
8168 * @param $handle (resource)
8169 * @param $length (int)
8170 * @return Returns the read string or FALSE in case of error.
8171 * @author Nicola Asuni
8172 * @protected
8173 * @since 4.5.027 (2009-03-16)
8175 protected function rfread($handle, $length) {
8176 $data = fread($handle, $length);
8177 if ($data === false) {
8178 return false;
8180 $rest = $length - strlen($data);
8181 if ($rest > 0) {
8182 $data .= $this->rfread($handle, $rest);
8184 return $data;
8188 * Extract info from a PNG image with alpha channel using the GD library.
8189 * @param $file (string) Name of the file containing the image.
8190 * @param $x (float) Abscissa of the upper-left corner.
8191 * @param $y (float) Ordinate of the upper-left corner.
8192 * @param $wpx (float) Original width of the image in pixels.
8193 * @param $hpx (float) original height of the image in pixels.
8194 * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
8195 * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
8196 * @param $type (string) Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
8197 * @param $link (mixed) URL or identifier returned by AddLink().
8198 * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
8199 * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
8200 * @param $dpi (int) dot-per-inch resolution used on resize
8201 * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
8202 * @param $filehash (string) File hash used to build unique file names.
8203 * @author Nicola Asuni
8204 * @protected
8205 * @since 4.3.007 (2008-12-04)
8206 * @see Image()
8208 protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
8209 if (empty($filehash)) {
8210 $filehash = md5($file);
8212 // create temp image file (without alpha channel)
8213 $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
8214 // create temp alpha file
8215 $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
8216 if (extension_loaded('imagick')) { // ImageMagick extension
8217 // ImageMagick library
8218 $img = new Imagick();
8219 $img->readImage($file);
8220 // clone image object
8221 $imga = $img->clone();
8222 // extract alpha channel
8223 $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
8224 $img->negateImage(true);
8225 $img->setImageFormat('png');
8226 $img->writeImage($tempfile_alpha);
8227 // remove alpha channel
8228 $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
8229 $imga->setImageFormat('png');
8230 $imga->writeImage($tempfile_plain);
8231 } elseif (function_exists('imagecreatefrompng')) { // GD extension
8232 // generate images
8233 $img = imagecreatefrompng($file);
8234 $imgalpha = imagecreate($wpx, $hpx);
8235 // generate gray scale palette (0 -> 255)
8236 for ($c = 0; $c < 256; ++$c) {
8237 ImageColorAllocate($imgalpha, $c, $c, $c);
8239 // extract alpha channel
8240 for ($xpx = 0; $xpx < $wpx; ++$xpx) {
8241 for ($ypx = 0; $ypx < $hpx; ++$ypx) {
8242 $color = imagecolorat($img, $xpx, $ypx);
8243 $alpha = ($color >> 24); // shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
8244 $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
8245 $alpha = $this->getGDgamma($alpha); // correct gamma
8246 imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
8249 imagepng($imgalpha, $tempfile_alpha);
8250 imagedestroy($imgalpha);
8251 // extract image without alpha channel
8252 $imgplain = imagecreatetruecolor($wpx, $hpx);
8253 imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
8254 imagepng($imgplain, $tempfile_plain);
8255 imagedestroy($imgplain);
8256 } else {
8257 $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
8259 // embed mask image
8260 $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
8261 // embed image, masked with previously embedded mask
8262 $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
8263 // remove temp files
8264 unlink($tempfile_alpha);
8265 unlink($tempfile_plain);
8269 * Correct the gamma value to be used with GD library
8270 * @param $v (float) the gamma value to be corrected
8271 * @protected
8272 * @since 4.3.007 (2008-12-04)
8274 protected function getGDgamma($v) {
8275 return (pow(($v / 255), 2.2) * 255);
8279 * Performs a line break.
8280 * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
8281 * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
8282 * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
8283 * @public
8284 * @since 1.0
8285 * @see Cell()
8287 public function Ln($h='', $cell=false) {
8288 if (($this->num_columns > 1) AND ($this->y == $this->columns[$this->current_column]['y']) AND isset($this->columns[$this->current_column]['x']) AND ($this->x == $this->columns[$this->current_column]['x'])) {
8289 // revove vertical space from the top of the column
8290 return;
8292 if ($cell) {
8293 if ($this->rtl) {
8294 $cellpadding = $this->cell_padding['R'];
8295 } else {
8296 $cellpadding = $this->cell_padding['L'];
8298 } else {
8299 $cellpadding = 0;
8301 if ($this->rtl) {
8302 $this->x = $this->w - $this->rMargin - $cellpadding;
8303 } else {
8304 $this->x = $this->lMargin + $cellpadding;
8306 if (is_string($h)) {
8307 $this->y += $this->lasth;
8308 } else {
8309 $this->y += $h;
8311 $this->newline = true;
8315 * Returns the relative X value of current position.
8316 * The value is relative to the left border for LTR languages and to the right border for RTL languages.
8317 * @return float
8318 * @public
8319 * @since 1.2
8320 * @see SetX(), GetY(), SetY()
8322 public function GetX() {
8323 //Get x position
8324 if ($this->rtl) {
8325 return ($this->w - $this->x);
8326 } else {
8327 return $this->x;
8332 * Returns the absolute X value of current position.
8333 * @return float
8334 * @public
8335 * @since 1.2
8336 * @see SetX(), GetY(), SetY()
8338 public function GetAbsX() {
8339 return $this->x;
8343 * Returns the ordinate of the current position.
8344 * @return float
8345 * @public
8346 * @since 1.0
8347 * @see SetY(), GetX(), SetX()
8349 public function GetY() {
8350 return $this->y;
8354 * Defines the abscissa of the current position.
8355 * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
8356 * @param $x (float) The value of the abscissa.
8357 * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
8358 * @public
8359 * @since 1.2
8360 * @see GetX(), GetY(), SetY(), SetXY()
8362 public function SetX($x, $rtloff=false) {
8363 if (!$rtloff AND $this->rtl) {
8364 if ($x >= 0) {
8365 $this->x = $this->w - $x;
8366 } else {
8367 $this->x = abs($x);
8369 } else {
8370 if ($x >= 0) {
8371 $this->x = $x;
8372 } else {
8373 $this->x = $this->w + $x;
8376 if ($this->x < 0) {
8377 $this->x = 0;
8379 if ($this->x > $this->w) {
8380 $this->x = $this->w;
8385 * Moves the current abscissa back to the left margin and sets the ordinate.
8386 * If the passed value is negative, it is relative to the bottom of the page.
8387 * @param $y (float) The value of the ordinate.
8388 * @param $resetx (bool) if true (default) reset the X position.
8389 * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
8390 * @public
8391 * @since 1.0
8392 * @see GetX(), GetY(), SetY(), SetXY()
8394 public function SetY($y, $resetx=true, $rtloff=false) {
8395 if ($resetx) {
8396 //reset x
8397 if (!$rtloff AND $this->rtl) {
8398 $this->x = $this->w - $this->rMargin;
8399 } else {
8400 $this->x = $this->lMargin;
8403 if ($y >= 0) {
8404 $this->y = $y;
8405 } else {
8406 $this->y = $this->h + $y;
8408 if ($this->y < 0) {
8409 $this->y = 0;
8411 if ($this->y > $this->h) {
8412 $this->y = $this->h;
8417 * Defines the abscissa and ordinate of the current position.
8418 * If the passed values are negative, they are relative respectively to the right and bottom of the page.
8419 * @param $x (float) The value of the abscissa.
8420 * @param $y (float) The value of the ordinate.
8421 * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
8422 * @public
8423 * @since 1.2
8424 * @see SetX(), SetY()
8426 public function SetXY($x, $y, $rtloff=false) {
8427 $this->SetY($y, false, $rtloff);
8428 $this->SetX($x, $rtloff);
8432 * Ouput input data and compress it if possible.
8433 * @param $data (string) Data to output.
8434 * @param $length (int) Data length in bytes.
8435 * @protected
8436 * @since 5.9.086
8438 protected function sendOutputData($data, $length) {
8439 if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
8440 // the content length may vary if the server is using compression
8441 header('Content-Length: '.$length);
8443 echo $data;
8447 * Send the document to a given destination: string, local file or browser.
8448 * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
8449 * The method first calls Close() if necessary to terminate the document.
8450 * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
8451 * @param $dest (string) Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local server file with the name given by name.</li><li>S: return the document as a string (name is ignored).</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
8452 * @public
8453 * @since 1.0
8454 * @see Close()
8456 public function Output($name='doc.pdf', $dest='I') {
8457 //Output PDF to some destination
8458 //Finish document if necessary
8459 if ($this->state < 3) {
8460 $this->Close();
8462 //Normalize parameters
8463 if (is_bool($dest)) {
8464 $dest = $dest ? 'D' : 'F';
8466 $dest = strtoupper($dest);
8467 if ($dest{0} != 'F') {
8468 $name = preg_replace('/[\s]+/', '_', $name);
8469 $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
8471 if ($this->sign) {
8472 // *** apply digital signature to the document ***
8473 // get the document content
8474 $pdfdoc = $this->getBuffer();
8475 // remove last newline
8476 $pdfdoc = substr($pdfdoc, 0, -1);
8477 // Remove the original buffer
8478 if (isset($this->diskcache) AND $this->diskcache) {
8479 // remove buffer file from cache
8480 unlink($this->buffer);
8482 unset($this->buffer);
8483 // remove filler space
8484 $byterange_string_len = strlen($this->byterange_string);
8485 // define the ByteRange
8486 $byte_range = array();
8487 $byte_range[0] = 0;
8488 $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
8489 $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
8490 $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
8491 $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
8492 // replace the ByteRange
8493 $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
8494 $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
8495 $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
8496 // write the document to a temporary folder
8497 $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
8498 $f = fopen($tempdoc, 'wb');
8499 if (!$f) {
8500 $this->Error('Unable to create temporary file: '.$tempdoc);
8502 $pdfdoc_length = strlen($pdfdoc);
8503 fwrite($f, $pdfdoc, $pdfdoc_length);
8504 fclose($f);
8505 // get digital signature via openssl library
8506 $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
8507 if (empty($this->signature_data['extracerts'])) {
8508 openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
8509 } else {
8510 openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED, $this->signature_data['extracerts']);
8512 unlink($tempdoc);
8513 // read signature
8514 $signature = file_get_contents($tempsign);
8515 unlink($tempsign);
8516 // extract signature
8517 $signature = substr($signature, $pdfdoc_length);
8518 $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
8519 $tmparr = explode("\n\n", $signature);
8520 $signature = $tmparr[1];
8521 unset($tmparr);
8522 // decode signature
8523 $signature = base64_decode(trim($signature));
8524 // convert signature to hex
8525 $signature = current(unpack('H*', $signature));
8526 $signature = str_pad($signature, $this->signature_max_length, '0');
8527 // disable disk caching
8528 $this->diskcache = false;
8529 // Add signature to the document
8530 $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
8531 $this->bufferlen = strlen($this->buffer);
8533 switch($dest) {
8534 case 'I': {
8535 // Send PDF to the standard output
8536 if (ob_get_contents()) {
8537 $this->Error('Some data has already been output, can\'t send PDF file');
8539 if (php_sapi_name() != 'cli') {
8540 // send output to a browser
8541 header('Content-Type: application/pdf');
8542 if (headers_sent()) {
8543 $this->Error('Some data has already been output to browser, can\'t send PDF file');
8545 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
8546 header('Pragma: public');
8547 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
8548 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
8549 header('Content-Disposition: inline; filename="'.basename($name).'";');
8550 $this->sendOutputData($this->getBuffer(), $this->bufferlen);
8551 } else {
8552 echo $this->getBuffer();
8554 break;
8556 case 'D': {
8557 // download PDF as file
8558 if (ob_get_contents()) {
8559 $this->Error('Some data has already been output, can\'t send PDF file');
8561 header('Content-Description: File Transfer');
8562 if (headers_sent()) {
8563 $this->Error('Some data has already been output to browser, can\'t send PDF file');
8565 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
8566 header('Pragma: public');
8567 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
8568 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
8569 // force download dialog
8570 if (strpos(php_sapi_name(), 'cgi') === false) {
8571 header('Content-Type: application/force-download');
8572 header('Content-Type: application/octet-stream', false);
8573 header('Content-Type: application/download', false);
8574 header('Content-Type: application/pdf', false);
8575 } else {
8576 header('Content-Type: application/pdf');
8578 // use the Content-Disposition header to supply a recommended filename
8579 header('Content-Disposition: attachment; filename="'.basename($name).'";');
8580 header('Content-Transfer-Encoding: binary');
8581 $this->sendOutputData($this->getBuffer(), $this->bufferlen);
8582 break;
8584 case 'F':
8585 case 'FI':
8586 case 'FD': {
8587 // save PDF to a local file
8588 if ($this->diskcache) {
8589 copy($this->buffer, $name);
8590 } else {
8591 $f = fopen($name, 'wb');
8592 if (!$f) {
8593 $this->Error('Unable to create output file: '.$name);
8595 fwrite($f, $this->getBuffer(), $this->bufferlen);
8596 fclose($f);
8598 if ($dest == 'FI') {
8599 // send headers to browser
8600 header('Content-Type: application/pdf');
8601 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
8602 header('Pragma: public');
8603 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
8604 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
8605 header('Content-Disposition: inline; filename="'.basename($name).'";');
8606 $this->sendOutputData(file_get_contents($name), filesize($name));
8607 } elseif ($dest == 'FD') {
8608 // send headers to browser
8609 if (ob_get_contents()) {
8610 $this->Error('Some data has already been output, can\'t send PDF file');
8612 header('Content-Description: File Transfer');
8613 if (headers_sent()) {
8614 $this->Error('Some data has already been output to browser, can\'t send PDF file');
8616 header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
8617 header('Pragma: public');
8618 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
8619 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
8620 // force download dialog
8621 if (strpos(php_sapi_name(), 'cgi') === false) {
8622 header('Content-Type: application/force-download');
8623 header('Content-Type: application/octet-stream', false);
8624 header('Content-Type: application/download', false);
8625 header('Content-Type: application/pdf', false);
8626 } else {
8627 header('Content-Type: application/pdf');
8629 // use the Content-Disposition header to supply a recommended filename
8630 header('Content-Disposition: attachment; filename="'.basename($name).'";');
8631 header('Content-Transfer-Encoding: binary');
8632 $this->sendOutputData(file_get_contents($name), filesize($name));
8634 break;
8636 case 'E': {
8637 // return PDF as base64 mime multi-part email attachment (RFC 2045)
8638 $retval = 'Content-Type: application/pdf;'."\r\n";
8639 $retval .= ' name="'.$name.'"'."\r\n";
8640 $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
8641 $retval .= 'Content-Disposition: attachment;'."\r\n";
8642 $retval .= ' filename="'.$name.'"'."\r\n\r\n";
8643 $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
8644 return $retval;
8646 case 'S': {
8647 // returns PDF as a string
8648 return $this->getBuffer();
8650 default: {
8651 $this->Error('Incorrect output destination: '.$dest);
8654 return '';
8658 * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
8659 * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
8660 * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
8661 * @public
8662 * @since 4.5.016 (2009-02-24)
8664 public function _destroy($destroyall=false, $preserve_objcopy=false) {
8665 if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
8666 // remove buffer file from cache
8667 unlink($this->buffer);
8669 foreach (array_keys(get_object_vars($this)) as $val) {
8670 if ($destroyall OR (
8671 ($val != 'internal_encoding')
8672 AND ($val != 'state')
8673 AND ($val != 'bufferlen')
8674 AND ($val != 'buffer')
8675 AND ($val != 'diskcache')
8676 AND ($val != 'sign')
8677 AND ($val != 'signature_data')
8678 AND ($val != 'signature_max_length')
8679 AND ($val != 'byterange_string')
8680 )) {
8681 if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
8682 unset($this->$val);
8689 * Check for locale-related bug
8690 * @protected
8692 protected function _dochecks() {
8693 //Check for locale-related bug
8694 if (1.1 == 1) {
8695 $this->Error('Don\'t alter the locale before including class file');
8697 //Check for decimal separator
8698 if (sprintf('%.1F', 1.0) != '1.0') {
8699 setlocale(LC_NUMERIC, 'C');
8704 * Return fonts path
8705 * @return string
8706 * @protected
8708 protected function _getfontpath() {
8709 if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
8710 define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
8712 return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
8716 * Return an array containing variations for the basic page number alias.
8717 * @param $a (string) Base alias.
8718 * @return array of page number aliases
8719 * @protected
8721 protected function getInternalPageNumberAliases($a= '') {
8722 $alias = array();
8723 // build array of Unicode + ASCII variants (the order is important)
8724 $alias = array('u' => array(), 'a' => array());
8725 $u = '{'.$a.'}';
8726 $alias['u'][] = $this->_escape($u);
8727 if ($this->isunicode) {
8728 $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
8729 $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
8730 $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
8731 $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
8733 $alias['a'][] = $this->_escape($a);
8734 return $alias;
8738 * Return an array containing all internal page aliases.
8739 * @return array of page number aliases
8740 * @protected
8742 protected function getAllInternalPageNumberAliases() {
8743 $basic_alias = array($this->alias_tot_pages, $this->alias_num_page, $this->alias_group_tot_pages, $this->alias_group_num_page, $this->alias_right_shift);
8744 $pnalias = array();
8745 foreach($basic_alias as $k => $a) {
8746 $pnalias[$k] = $this->getInternalPageNumberAliases($a);
8748 return $pnalias;
8752 * Replace page number aliases with number.
8753 * @param $page (string) Page content.
8754 * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
8755 * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
8756 * @return replaced page content and updated $diff parameter as array.
8757 * @protected
8759 protected function replacePageNumAliases($page, $replace, $diff=0) {
8760 foreach ($replace as $rep) {
8761 foreach ($rep[3] as $a) {
8762 if (strpos($page, $a) !== false) {
8763 $page = str_replace($a, $rep[0], $page);
8764 $diff += ($rep[2] - $rep[1]);
8768 return array($page, $diff);
8772 * Replace right shift page number aliases with spaces to correct right alignment.
8773 * This works perfectly only when using monospaced fonts.
8774 * @param $page (string) Page content.
8775 * @param $aliases (array) Array of page aliases.
8776 * @param $diff (int) initial difference to add.
8777 * @return replaced page content.
8778 * @protected
8780 protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
8781 foreach ($aliases as $type => $alias) {
8782 foreach ($alias as $a) {
8783 // find position of compensation factor
8784 $startnum = (strpos($a, ':') + 1);
8785 $a = substr($a, 0, $startnum);
8786 if (($pos = strpos($page, $a)) !== false) {
8787 // end of alias
8788 $endnum = strpos($page, '}', $pos);
8789 // string to be replaced
8790 $aa = substr($page, $pos, ($endnum - $pos + 1));
8791 // get compensation factor
8792 $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
8793 $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
8794 $ratio = floatval($ratio);
8795 if ($type == 'u') {
8796 $chrdiff = floor(($diff + 12) * $ratio);
8797 $shift = str_repeat(' ', $chrdiff);
8798 $shift = $this->UTF8ToUTF16BE($shift, false);
8799 } else {
8800 $chrdiff = floor(($diff + 11) * $ratio);
8801 $shift = str_repeat(' ', $chrdiff);
8803 $page = str_replace($aa, $shift, $page);
8807 return $page;
8811 * Output pages (and replace page number aliases).
8812 * @protected
8814 protected function _putpages() {
8815 $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
8816 // get internal aliases for page numbers
8817 $pnalias = $this->getAllInternalPageNumberAliases();
8818 $num_pages = $this->numpages;
8819 $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
8820 $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
8821 $ptp_num_chars = $this->GetNumChars($ptpa);
8822 $pagegroupnum = 0;
8823 $groupnum = 0;
8824 $ptgu = 1;
8825 $ptga = 1;
8826 for ($n = 1; $n <= $num_pages; ++$n) {
8827 // get current page
8828 $temppage = $this->getPageBuffer($n);
8829 $pagelen = strlen($temppage);
8830 // set replacements for total pages number
8831 $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
8832 $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
8833 $pnp_num_chars = $this->GetNumChars($pnpa);
8834 $pdiff = 0; // difference used for right shift alignment of page numbers
8835 $gdiff = 0; // difference used for right shift alignment of page group numbers
8836 if (!empty($this->pagegroups)) {
8837 if (isset($this->newpagegroup[$n])) {
8838 $pagegroupnum = 0;
8839 ++$groupnum;
8840 $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
8841 $ptgu = $this->UTF8ToUTF16BE($ptga, false);
8842 $ptg_num_chars = $this->GetNumChars($ptga);
8844 ++$pagegroupnum;
8845 $pnga = $this->formatPageNumber($pagegroupnum);
8846 $pngu = $this->UTF8ToUTF16BE($pnga, false);
8847 $png_num_chars = $this->GetNumChars($pnga);
8848 // replace page numbers
8849 $replace = array();
8850 $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
8851 $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
8852 $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
8853 $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
8854 list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
8856 // replace page numbers
8857 $replace = array();
8858 $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
8859 $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
8860 $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
8861 $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
8862 list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
8863 // replace right shift alias
8864 $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
8865 // replace EPS marker
8866 $temppage = str_replace($this->epsmarker, '', $temppage);
8867 //Page
8868 $this->page_obj_id[$n] = $this->_newobj();
8869 $out = '<<';
8870 $out .= ' /Type /Page';
8871 $out .= ' /Parent 1 0 R';
8872 $out .= ' /LastModified '.$this->_datestring();
8873 $out .= ' /Resources 2 0 R';
8874 $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
8875 foreach ($boxes as $box) {
8876 $out .= ' /'.$box;
8877 $out .= sprintf(' [%.2F %.2F %.2F %.2F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
8879 if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
8880 $out .= ' /BoxColorInfo <<';
8881 foreach ($boxes as $box) {
8882 if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
8883 $out .= ' /'.$box.' <<';
8884 if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
8885 $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
8886 $out .= ' /C [';
8887 $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
8888 $out .= ' ]';
8890 if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
8891 $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
8893 if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
8894 $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
8896 if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
8897 $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
8898 $out .= ' /D [';
8899 foreach ($dashes as $dash) {
8900 $out .= sprintf(' %.3F', ($dash * $this->k));
8902 $out .= ' ]';
8904 $out .= ' >>';
8907 $out .= ' >>';
8909 $out .= ' /Contents '.($this->n + 1).' 0 R';
8910 $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
8911 if (!$this->pdfa_mode) {
8912 $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
8914 if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
8915 // page transitions
8916 if (isset($this->pagedim[$n]['trans']['Dur'])) {
8917 $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
8919 $out .= ' /Trans <<';
8920 $out .= ' /Type /Trans';
8921 if (isset($this->pagedim[$n]['trans']['S'])) {
8922 $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
8924 if (isset($this->pagedim[$n]['trans']['D'])) {
8925 $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
8927 if (isset($this->pagedim[$n]['trans']['Dm'])) {
8928 $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
8930 if (isset($this->pagedim[$n]['trans']['M'])) {
8931 $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
8933 if (isset($this->pagedim[$n]['trans']['Di'])) {
8934 $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
8936 if (isset($this->pagedim[$n]['trans']['SS'])) {
8937 $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
8939 if (isset($this->pagedim[$n]['trans']['B'])) {
8940 $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
8942 $out .= ' >>';
8944 $out .= $this->_getannotsrefs($n);
8945 $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
8946 $out .= ' >>';
8947 $out .= "\n".'endobj';
8948 $this->_out($out);
8949 //Page content
8950 $p = ($this->compress) ? gzcompress($temppage) : $temppage;
8951 $this->_newobj();
8952 $p = $this->_getrawstream($p);
8953 $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
8954 if ($this->diskcache) {
8955 // remove temporary files
8956 unlink($this->pages[$n]);
8959 //Pages root
8960 $out = $this->_getobj(1)."\n";
8961 $out .= '<< /Type /Pages /Kids [';
8962 foreach($this->page_obj_id as $page_obj) {
8963 $out .= ' '.$page_obj.' 0 R';
8965 $out .= ' ] /Count '.$num_pages.' >>';
8966 $out .= "\n".'endobj';
8967 $this->_out($out);
8971 * Output references to page annotations
8972 * @param $n (int) page number
8973 * @protected
8974 * @author Nicola Asuni
8975 * @since 4.7.000 (2008-08-29)
8976 * @deprecated
8978 protected function _putannotsrefs($n) {
8979 $this->_out($this->_getannotsrefs($n));
8983 * Get references to page annotations.
8984 * @param $n (int) page number
8985 * @return string
8986 * @protected
8987 * @author Nicola Asuni
8988 * @since 5.0.010 (2010-05-17)
8990 protected function _getannotsrefs($n) {
8991 if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
8992 return '';
8994 $out = ' /Annots [';
8995 if (isset($this->PageAnnots[$n])) {
8996 foreach ($this->PageAnnots[$n] as $key => $val) {
8997 if (!in_array($val['n'], $this->radio_groups)) {
8998 $out .= ' '.$val['n'].' 0 R';
9001 // add radiobutton groups
9002 if (isset($this->radiobutton_groups[$n])) {
9003 foreach ($this->radiobutton_groups[$n] as $key => $data) {
9004 if (isset($data['n'])) {
9005 $out .= ' '.$data['n'].' 0 R';
9010 if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
9011 // set reference for signature object
9012 $out .= ' '.$this->sig_obj_id.' 0 R';
9014 if (!empty($this->empty_signature_appearance)) {
9015 foreach ($this->empty_signature_appearance as $esa) {
9016 if ($esa['page'] == $n) {
9017 // set reference for empty signature objects
9018 $out .= ' '.$esa['objid'].' 0 R';
9022 $out .= ' ]';
9023 return $out;
9027 * Output annotations objects for all pages.
9028 * !!! THIS METHOD IS NOT YET COMPLETED !!!
9029 * See section 12.5 of PDF 32000_2008 reference.
9030 * @protected
9031 * @author Nicola Asuni
9032 * @since 4.0.018 (2008-08-06)
9034 protected function _putannotsobjs() {
9035 // reset object counter
9036 for ($n=1; $n <= $this->numpages; ++$n) {
9037 if (isset($this->PageAnnots[$n])) {
9038 // set page annotations
9039 foreach ($this->PageAnnots[$n] as $key => $pl) {
9040 $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
9041 // create annotation object for grouping radiobuttons
9042 if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
9043 $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
9044 $annots = '<<';
9045 $annots .= ' /Type /Annot';
9046 $annots .= ' /Subtype /Widget';
9047 $annots .= ' /Rect [0 0 0 0]';
9048 if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
9049 // read only
9050 $annots .= ' /F 68';
9051 $annots .= ' /Ff 49153';
9052 } else {
9053 $annots .= ' /F 4'; // default print for PDF/A
9054 $annots .= ' /Ff 49152';
9056 $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
9057 $annots .= ' /FT /Btn';
9058 $annots .= ' /Kids [';
9059 foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
9060 if (isset($data['kid'])) {
9061 $annots .= ' '.$data['kid'].' 0 R';
9062 if ($data['def'] !== 'Off') {
9063 $defval = $data['def'];
9067 $annots .= ' ]';
9068 if (isset($defval)) {
9069 $annots .= ' /V /'.$defval;
9071 $annots .= ' >>';
9072 $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
9073 $this->form_obj_id[] = $radio_button_obj_id;
9074 // store object id to be used on Parent entry of Kids
9075 $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
9077 $formfield = false;
9078 $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
9079 $a = $pl['x'] * $this->k;
9080 $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
9081 $c = $pl['w'] * $this->k;
9082 $d = $pl['h'] * $this->k;
9083 $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
9084 // create new annotation object
9085 $annots = '<</Type /Annot';
9086 $annots .= ' /Subtype /'.$pl['opt']['subtype'];
9087 $annots .= ' /Rect ['.$rect.']';
9088 $ft = array('Btn', 'Tx', 'Ch', 'Sig');
9089 if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
9090 $annots .= ' /FT /'.$pl['opt']['ft'];
9091 $formfield = true;
9093 $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
9094 $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
9095 $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
9096 $annots .= ' /M '.$this->_datestring($annot_obj_id);
9097 if (isset($pl['opt']['f'])) {
9098 $fval = 0;
9099 if (is_array($pl['opt']['f'])) {
9100 foreach ($pl['opt']['f'] as $f) {
9101 switch (strtolower($f)) {
9102 case 'invisible': {
9103 $fval += 1 << 0;
9104 break;
9106 case 'hidden': {
9107 $fval += 1 << 1;
9108 break;
9110 case 'print': {
9111 $fval += 1 << 2;
9112 break;
9114 case 'nozoom': {
9115 $fval += 1 << 3;
9116 break;
9118 case 'norotate': {
9119 $fval += 1 << 4;
9120 break;
9122 case 'noview': {
9123 $fval += 1 << 5;
9124 break;
9126 case 'readonly': {
9127 $fval += 1 << 6;
9128 break;
9130 case 'locked': {
9131 $fval += 1 << 8;
9132 break;
9134 case 'togglenoview': {
9135 $fval += 1 << 9;
9136 break;
9138 case 'lockedcontents': {
9139 $fval += 1 << 10;
9140 break;
9142 default: {
9143 break;
9147 } else {
9148 $fval = intval($pl['opt']['f']);
9150 } else {
9151 $fval = 4;
9153 if ($this->pdfa_mode) {
9154 // force print flag for PDF/A mode
9155 $fval |= 4;
9157 $annots .= ' /F '.intval($fval);
9158 if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
9159 $annots .= ' /AS /'.$pl['opt']['as'];
9161 if (isset($pl['opt']['ap'])) {
9162 // appearance stream
9163 $annots .= ' /AP <<';
9164 if (is_array($pl['opt']['ap'])) {
9165 foreach ($pl['opt']['ap'] as $apmode => $apdef) {
9166 // $apmode can be: n = normal; r = rollover; d = down;
9167 $annots .= ' /'.strtoupper($apmode);
9168 if (is_array($apdef)) {
9169 $annots .= ' <<';
9170 foreach ($apdef as $apstate => $stream) {
9171 // reference to XObject that define the appearance for this mode-state
9172 $apsobjid = $this->_putAPXObject($c, $d, $stream);
9173 $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
9175 $annots .= ' >>';
9176 } else {
9177 // reference to XObject that define the appearance for this mode
9178 $apsobjid = $this->_putAPXObject($c, $d, $apdef);
9179 $annots .= ' '.$apsobjid.' 0 R';
9182 } else {
9183 $annots .= $pl['opt']['ap'];
9185 $annots .= ' >>';
9187 if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
9188 $annots .= ' /BS <<';
9189 $annots .= ' /Type /Border';
9190 if (isset($pl['opt']['bs']['w'])) {
9191 $annots .= ' /W '.intval($pl['opt']['bs']['w']);
9193 $bstyles = array('S', 'D', 'B', 'I', 'U');
9194 if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
9195 $annots .= ' /S /'.$pl['opt']['bs']['s'];
9197 if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
9198 $annots .= ' /D [';
9199 foreach ($pl['opt']['bs']['d'] as $cord) {
9200 $annots .= ' '.intval($cord);
9202 $annots .= ']';
9204 $annots .= ' >>';
9205 } else {
9206 $annots .= ' /Border [';
9207 if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
9208 $annots .= intval($pl['opt']['border'][0]).' ';
9209 $annots .= intval($pl['opt']['border'][1]).' ';
9210 $annots .= intval($pl['opt']['border'][2]);
9211 if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
9212 $annots .= ' [';
9213 foreach ($pl['opt']['border'][3] as $dash) {
9214 $annots .= intval($dash).' ';
9216 $annots .= ']';
9218 } else {
9219 $annots .= '0 0 0';
9221 $annots .= ']';
9223 if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
9224 $annots .= ' /BE <<';
9225 $bstyles = array('S', 'C');
9226 if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
9227 $annots .= ' /S /'.$pl['opt']['bs']['s'];
9228 } else {
9229 $annots .= ' /S /S';
9231 if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
9232 $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
9234 $annots .= '>>';
9236 if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
9237 $annots .= ' /C [';
9238 foreach ($pl['opt']['c'] as $col) {
9239 $col = intval($col);
9240 $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
9241 $annots .= sprintf(' %.4F', $color);
9243 $annots .= ']';
9245 //$annots .= ' /StructParent ';
9246 //$annots .= ' /OC ';
9247 $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
9248 if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
9249 // this is a markup type
9250 if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
9251 $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
9253 //$annots .= ' /Popup ';
9254 if (isset($pl['opt']['ca'])) {
9255 $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
9257 if (isset($pl['opt']['rc'])) {
9258 $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
9260 $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
9261 //$annots .= ' /IRT ';
9262 if (isset($pl['opt']['subj'])) {
9263 $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
9265 //$annots .= ' /RT ';
9266 //$annots .= ' /IT ';
9267 //$annots .= ' /ExData ';
9269 $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
9270 // Annotation types
9271 switch (strtolower($pl['opt']['subtype'])) {
9272 case 'text': {
9273 if (isset($pl['opt']['open'])) {
9274 $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
9276 $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
9277 if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
9278 $annots .= ' /Name /'.$pl['opt']['name'];
9279 } else {
9280 $annots .= ' /Name /Note';
9282 $statemodels = array('Marked', 'Review');
9283 if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
9284 $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
9285 } else {
9286 $pl['opt']['statemodel'] = 'Marked';
9287 $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
9289 if ($pl['opt']['statemodel'] == 'Marked') {
9290 $states = array('Accepted', 'Unmarked');
9291 } else {
9292 $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
9294 if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
9295 $annots .= ' /State /'.$pl['opt']['state'];
9296 } else {
9297 if ($pl['opt']['statemodel'] == 'Marked') {
9298 $annots .= ' /State /Unmarked';
9299 } else {
9300 $annots .= ' /State /None';
9303 break;
9305 case 'link': {
9306 if (is_string($pl['txt'])) {
9307 // external URI link
9308 $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
9309 } else {
9310 // internal link
9311 $l = $this->links[$pl['txt']];
9312 if (isset($this->page_obj_id[($l[0])])) {
9313 $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
9316 $hmodes = array('N', 'I', 'O', 'P');
9317 if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
9318 $annots .= ' /H /'.$pl['opt']['h'];
9319 } else {
9320 $annots .= ' /H /I';
9322 //$annots .= ' /PA ';
9323 //$annots .= ' /Quadpoints ';
9324 break;
9326 case 'freetext': {
9327 if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
9328 $annots .= ' /DA ('.$pl['opt']['da'].')';
9330 if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
9331 $annots .= ' /Q '.intval($pl['opt']['q']);
9333 if (isset($pl['opt']['rc'])) {
9334 $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
9336 if (isset($pl['opt']['ds'])) {
9337 $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
9339 if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
9340 $annots .= ' /CL [';
9341 foreach ($pl['opt']['cl'] as $cl) {
9342 $annots .= sprintf('%.4F ', $cl * $this->k);
9344 $annots .= ']';
9346 $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
9347 if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
9348 $annots .= ' /IT /'.$pl['opt']['it'];
9350 if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
9351 $l = $pl['opt']['rd'][0] * $this->k;
9352 $r = $pl['opt']['rd'][1] * $this->k;
9353 $t = $pl['opt']['rd'][2] * $this->k;
9354 $b = $pl['opt']['rd'][3] * $this->k;
9355 $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
9357 if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
9358 $annots .= ' /LE /'.$pl['opt']['le'];
9360 break;
9362 case 'line': {
9363 break;
9365 case 'square': {
9366 break;
9368 case 'circle': {
9369 break;
9371 case 'polygon': {
9372 break;
9374 case 'polyline': {
9375 break;
9377 case 'highlight': {
9378 break;
9380 case 'underline': {
9381 break;
9383 case 'squiggly': {
9384 break;
9386 case 'strikeout': {
9387 break;
9389 case 'stamp': {
9390 break;
9392 case 'caret': {
9393 break;
9395 case 'ink': {
9396 break;
9398 case 'popup': {
9399 break;
9401 case 'fileattachment': {
9402 if ($this->pdfa_mode) {
9403 // embedded files are not allowed in PDF/A mode
9404 break;
9406 if (!isset($pl['opt']['fs'])) {
9407 break;
9409 $filename = basename($pl['opt']['fs']);
9410 if (isset($this->embeddedfiles[$filename]['n'])) {
9411 $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
9412 $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
9413 if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
9414 $annots .= ' /Name /'.$pl['opt']['name'];
9415 } else {
9416 $annots .= ' /Name /PushPin';
9419 break;
9421 case 'sound': {
9422 if (!isset($pl['opt']['fs'])) {
9423 break;
9425 $filename = basename($pl['opt']['fs']);
9426 if (isset($this->embeddedfiles[$filename]['n'])) {
9427 // ... TO BE COMPLETED ...
9428 // /R /C /B /E /CO /CP
9429 $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
9430 $iconsapp = array('Speaker', 'Mic');
9431 if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
9432 $annots .= ' /Name /'.$pl['opt']['name'];
9433 } else {
9434 $annots .= ' /Name /Speaker';
9437 break;
9439 case 'movie': {
9440 break;
9442 case 'widget': {
9443 $hmode = array('N', 'I', 'O', 'P', 'T');
9444 if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
9445 $annots .= ' /H /'.$pl['opt']['h'];
9447 if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
9448 $annots .= ' /MK <<';
9449 if (isset($pl['opt']['mk']['r'])) {
9450 $annots .= ' /R '.$pl['opt']['mk']['r'];
9452 if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
9453 $annots .= ' /BC [';
9454 foreach($pl['opt']['mk']['bc'] AS $col) {
9455 $col = intval($col);
9456 $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
9457 $annots .= sprintf(' %.2F', $color);
9459 $annots .= ']';
9461 if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
9462 $annots .= ' /BG [';
9463 foreach($pl['opt']['mk']['bg'] AS $col) {
9464 $col = intval($col);
9465 $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
9466 $annots .= sprintf(' %.2F', $color);
9468 $annots .= ']';
9470 if (isset($pl['opt']['mk']['ca'])) {
9471 $annots .= ' /CA '.$pl['opt']['mk']['ca'];
9473 if (isset($pl['opt']['mk']['rc'])) {
9474 $annots .= ' /RC '.$pl['opt']['mk']['rc'];
9476 if (isset($pl['opt']['mk']['ac'])) {
9477 $annots .= ' /AC '.$pl['opt']['mk']['ac'];
9479 if (isset($pl['opt']['mk']['i'])) {
9480 $info = $this->getImageBuffer($pl['opt']['mk']['i']);
9481 if ($info !== false) {
9482 $annots .= ' /I '.$info['n'].' 0 R';
9485 if (isset($pl['opt']['mk']['ri'])) {
9486 $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
9487 if ($info !== false) {
9488 $annots .= ' /RI '.$info['n'].' 0 R';
9491 if (isset($pl['opt']['mk']['ix'])) {
9492 $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
9493 if ($info !== false) {
9494 $annots .= ' /IX '.$info['n'].' 0 R';
9497 if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
9498 $annots .= ' /IF <<';
9499 $if_sw = array('A', 'B', 'S', 'N');
9500 if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
9501 $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
9503 $if_s = array('A', 'P');
9504 if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
9505 $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
9507 if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
9508 $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
9510 if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
9511 $annots .= ' /FB true';
9513 $annots .= '>>';
9515 if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
9516 $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
9518 $annots .= '>>';
9519 } // end MK
9520 // --- Entries for field dictionaries ---
9521 if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
9522 // set parent
9523 $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
9525 if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
9526 $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
9528 if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
9529 $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
9531 if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
9532 $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
9534 if (isset($pl['opt']['ff'])) {
9535 if (is_array($pl['opt']['ff'])) {
9536 // array of bit settings
9537 $flag = 0;
9538 foreach($pl['opt']['ff'] as $val) {
9539 $flag += 1 << ($val - 1);
9541 } else {
9542 $flag = intval($pl['opt']['ff']);
9544 $annots .= ' /Ff '.$flag;
9546 if (isset($pl['opt']['maxlen'])) {
9547 $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
9549 if (isset($pl['opt']['v'])) {
9550 $annots .= ' /V';
9551 if (is_array($pl['opt']['v'])) {
9552 foreach ($pl['opt']['v'] AS $optval) {
9553 if (is_float($optval)) {
9554 $optval = sprintf('%.2F', $optval);
9556 $annots .= ' '.$optval;
9558 } else {
9559 $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
9562 if (isset($pl['opt']['dv'])) {
9563 $annots .= ' /DV';
9564 if (is_array($pl['opt']['dv'])) {
9565 foreach ($pl['opt']['dv'] AS $optval) {
9566 if (is_float($optval)) {
9567 $optval = sprintf('%.2F', $optval);
9569 $annots .= ' '.$optval;
9571 } else {
9572 $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
9575 if (isset($pl['opt']['rv'])) {
9576 $annots .= ' /RV';
9577 if (is_array($pl['opt']['rv'])) {
9578 foreach ($pl['opt']['rv'] AS $optval) {
9579 if (is_float($optval)) {
9580 $optval = sprintf('%.2F', $optval);
9582 $annots .= ' '.$optval;
9584 } else {
9585 $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
9588 if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
9589 $annots .= ' /A << '.$pl['opt']['a'].' >>';
9591 if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
9592 $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
9594 if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
9595 $annots .= ' /DA ('.$pl['opt']['da'].')';
9597 if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
9598 $annots .= ' /Q '.intval($pl['opt']['q']);
9600 if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
9601 $annots .= ' /Opt [';
9602 foreach($pl['opt']['opt'] AS $copt) {
9603 if (is_array($copt)) {
9604 $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
9605 } else {
9606 $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
9609 $annots .= ']';
9611 if (isset($pl['opt']['ti'])) {
9612 $annots .= ' /TI '.intval($pl['opt']['ti']);
9614 if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
9615 $annots .= ' /I [';
9616 foreach($pl['opt']['i'] AS $copt) {
9617 $annots .= intval($copt).' ';
9619 $annots .= ']';
9621 break;
9623 case 'screen': {
9624 break;
9626 case 'printermark': {
9627 break;
9629 case 'trapnet': {
9630 break;
9632 case 'watermark': {
9633 break;
9635 case '3d': {
9636 break;
9638 default: {
9639 break;
9642 $annots .= '>>';
9643 // create new annotation object
9644 $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
9645 if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
9646 // store reference of form object
9647 $this->form_obj_id[] = $annot_obj_id;
9651 } // end for each page
9655 * Put appearance streams XObject used to define annotation's appearance states.
9656 * @param $w (int) annotation width
9657 * @param $h (int) annotation height
9658 * @param $stream (string) appearance stream
9659 * @return int object ID
9660 * @protected
9661 * @since 4.8.001 (2009-09-09)
9663 protected function _putAPXObject($w=0, $h=0, $stream='') {
9664 $stream = trim($stream);
9665 $out = $this->_getobj()."\n";
9666 $this->xobjects['AX'.$this->n] = array('n' => $this->n);
9667 $out .= '<<';
9668 $out .= ' /Type /XObject';
9669 $out .= ' /Subtype /Form';
9670 $out .= ' /FormType 1';
9671 if ($this->compress) {
9672 $stream = gzcompress($stream);
9673 $out .= ' /Filter /FlateDecode';
9675 $rect = sprintf('%.2F %.2F', $w, $h);
9676 $out .= ' /BBox [0 0 '.$rect.']';
9677 $out .= ' /Matrix [1 0 0 1 0 0]';
9678 $out .= ' /Resources 2 0 R';
9679 $stream = $this->_getrawstream($stream);
9680 $out .= ' /Length '.strlen($stream);
9681 $out .= ' >>';
9682 $out .= ' stream'."\n".$stream."\n".'endstream';
9683 $out .= "\n".'endobj';
9684 $this->_out($out);
9685 return $this->n;
9689 * Get ULONG from string (Big Endian 32-bit unsigned integer).
9690 * @param $str (string) string from where to extract value
9691 * @param $offset (int) point from where to read the data
9692 * @return int 32 bit value
9693 * @author Nicola Asuni
9694 * @protected
9695 * @since 5.2.000 (2010-06-02)
9697 protected function _getULONG($str, $offset) {
9698 $v = unpack('Ni', substr($str, $offset, 4));
9699 return $v['i'];
9703 * Get USHORT from string (Big Endian 16-bit unsigned integer).
9704 * @param $str (string) string from where to extract value
9705 * @param $offset (int) point from where to read the data
9706 * @return int 16 bit value
9707 * @author Nicola Asuni
9708 * @protected
9709 * @since 5.2.000 (2010-06-02)
9711 protected function _getUSHORT($str, $offset) {
9712 $v = unpack('ni', substr($str, $offset, 2));
9713 return $v['i'];
9717 * Get SHORT from string (Big Endian 16-bit signed integer).
9718 * @param $str (string) string from where to extract value
9719 * @param $offset (int) point from where to read the data
9720 * @return int 16 bit value
9721 * @author Nicola Asuni
9722 * @protected
9723 * @since 5.2.000 (2010-06-02)
9725 protected function _getSHORT($str, $offset) {
9726 $v = unpack('si', substr($str, $offset, 2));
9727 return $v['i'];
9731 * Get FWORD from string (Big Endian 16-bit signed integer).
9732 * @param $str (string) string from where to extract value
9733 * @param $offset (int) point from where to read the data
9734 * @return int 16 bit value
9735 * @author Nicola Asuni
9736 * @protected
9737 * @since 5.9.123 (2011-09-30)
9739 protected function _getFWORD($str, $offset) {
9740 $v = $this->_getUSHORT($str, $offset);
9741 if ($v > 0x7fff) {
9742 $v -= 0x10000;
9744 return $v;
9748 * Get UFWORD from string (Big Endian 16-bit unsigned integer).
9749 * @param $str (string) string from where to extract value
9750 * @param $offset (int) point from where to read the data
9751 * @return int 16 bit value
9752 * @author Nicola Asuni
9753 * @protected
9754 * @since 5.9.123 (2011-09-30)
9756 protected function _getUFWORD($str, $offset) {
9757 $v = $this->_getUSHORT($str, $offset);
9758 return $v;
9762 * Get FIXED from string (32-bit signed fixed-point number (16.16).
9763 * @param $str (string) string from where to extract value
9764 * @param $offset (int) point from where to read the data
9765 * @return int 16 bit value
9766 * @author Nicola Asuni
9767 * @protected
9768 * @since 5.9.123 (2011-09-30)
9770 protected function _getFIXED($str, $offset) {
9771 // mantissa
9772 $m = $this->_getFWORD($str, $offset);
9773 // fraction
9774 $f = $this->_getUSHORT($str, ($offset + 2));
9775 $v = floatval(''.$m.'.'.$f.'');
9776 return $v;
9780 * Get BYTE from string (8-bit unsigned integer).
9781 * @param $str (string) String from where to extract value.
9782 * @param $offset (int) Point from where to read the data.
9783 * @return int 8 bit value
9784 * @author Nicola Asuni
9785 * @protected
9786 * @since 5.2.000 (2010-06-02)
9788 protected function _getBYTE($str, $offset) {
9789 $v = unpack('Ci', substr($str, $offset, 1));
9790 return $v['i'];
9793 * Update the CIDToGIDMap string with a new value.
9794 * @param $map (string) CIDToGIDMap.
9795 * @param $cid (int) CID value.
9796 * @param $gid (int) GID value.
9797 * @return (string) CIDToGIDMap.
9798 * @author Nicola Asuni
9799 * @protected
9800 * @since 5.9.123 (2011-09-29)
9802 protected function updateCIDtoGIDmap($map, $cid, $gid) {
9803 if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
9804 if ($gid > 0xFFFF) {
9805 $gid -= 0x10000;
9807 $map{($cid * 2)} = chr($gid >> 8);
9808 $map{(($cid * 2) + 1)} = chr($gid & 0xFF);
9810 return $map;
9814 * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
9815 * @param $fontfile (string) TrueType or Type1 font file (full path).
9816 * @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
9817 * @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
9818 * @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
9819 * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
9820 * @return (string) TCPDF font name.
9821 * @author Nicola Asuni
9822 * @public
9823 * @since 5.9.123 (2010-09-30)
9825 public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='') {
9826 if (!file_exists($fontfile)) {
9827 $this->Error('Could not find file: '.$fontfile.'');
9829 // font metrics
9830 $fmetric = array();
9831 // build new font name for TCPDF compatibility
9832 $font_path_parts = pathinfo($fontfile);
9833 if (!isset($font_path_parts['filename'])) {
9834 $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
9836 $font_name = strtolower($font_path_parts['filename']);
9837 $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
9838 $search = array('bold', 'oblique', 'italic', 'regular');
9839 $replace = array('b', 'i', 'i', '');
9840 $font_name = str_replace($search, $replace, $font_name);
9841 if (empty($font_name)) {
9842 // set generic name
9843 $font_name = 'tcpdffont';
9845 // set output path
9846 if (empty($outpath)) {
9847 $outpath = $this->_getfontpath();
9849 // check if this font already exist
9850 if (file_exists($outpath.$font_name.'.php')) {
9851 // this font already exist (delete it from fonts folder to rebuild it)
9852 return $font_name;
9854 $fmetric['file'] = $font_name.'.z';
9855 $fmetric['ctg'] = $font_name.'.ctg.z';
9856 // get font data
9857 $font = file_get_contents($fontfile);
9858 $fmetric['originalsize'] = strlen($font);
9859 // autodetect font type
9860 if (empty($fonttype)) {
9861 if ($this->_getULONG($font, 0) == 0x10000) {
9862 // True Type (Unicode or not)
9863 $fonttype = 'TrueTypeUnicode';
9864 } elseif (substr($font, 0, 4) == 'OTTO') {
9865 // Open Type (Unicode or not)
9866 $this->Error('Unsupported font format: OpenType with CFF data.');
9867 } else {
9868 // Type 1
9869 $fonttype = 'Type1';
9872 // set font type
9873 switch ($fonttype) {
9874 case 'CID0CT':
9875 case 'CID0CS':
9876 case 'CID0KR':
9877 case 'CID0JP': {
9878 $fmetric['type'] = 'cidfont0';
9879 break;
9881 case 'Type1': {
9882 $fmetric['type'] = 'Type1';
9883 if (empty($enc) AND (($flags & 4) == 0)) {
9884 $enc = 'cp1252';
9886 break;
9888 case 'TrueType': {
9889 $fmetric['type'] = 'TrueType';
9890 break;
9892 case 'TrueTypeUnicode':
9893 default: {
9894 $fmetric['type'] = 'TrueTypeUnicode';
9895 break;
9898 // set encoding maps (if any)
9899 $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
9900 $fmetric['diff'] = '';
9901 if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
9902 if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
9903 // build differences from reference encoding
9904 $enc_ref = $this->encmaps->encmap['cp1252'];
9905 $enc_target = $this->encmaps->encmap[$enc];
9906 $last = 0;
9907 for ($i = 32; $i <= 255; ++$i) {
9908 if ($enc_target != $enc_ref[$i]) {
9909 if ($i != ($last + 1)) {
9910 $fmetric['diff'] .= $i.' ';
9912 $last = $i;
9913 $fmetric['diff'] .= '/'.$enc_target[$i].' ';
9918 // parse the font by type
9919 if ($fmetric['type'] == 'Type1') {
9920 // ---------- TYPE 1 ----------
9921 // read first segment
9922 $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
9923 if ($a['marker'] != 128) {
9924 $this->Error('Font file is not a valid binary Type1');
9926 $fmetric['size1'] = $a['size'];
9927 $data = substr($font, 6, $fmetric['size1']);
9928 // read second segment
9929 $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
9930 if ($a['marker'] != 128) {
9931 $this->Error('Font file is not a valid binary Type1');
9933 $fmetric['size2'] = $a['size'];
9934 $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
9935 $data .= $encrypted;
9936 // store compressed font
9937 $fp = fopen($outpath.$fmetric['file'], 'wb');
9938 fwrite($fp, gzcompress($data));
9939 fclose($fp);
9940 // get font info
9941 $fmetric['Flags'] = $flags;
9942 preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
9943 $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
9944 preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
9945 $fmetric['bbox'] = trim($matches[1]);
9946 $bv = explode(' ', $fmetric['bbox']);
9947 $fmetric['Ascent'] = intval($bv[3]);
9948 $fmetric['Descent'] = intval($bv[1]);
9949 preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
9950 $fmetric['italicAngle'] = intval($matches[1]);
9951 if ($fmetric['italicAngle'] != 0) {
9952 $fmetric['Flags'] |= 64;
9954 preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
9955 $fmetric['underlinePosition'] = intval($matches[1]);
9956 preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
9957 $fmetric['underlineThickness'] = intval($matches[1]);
9958 preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
9959 if ($matches[1] == 'true') {
9960 $fmetric['Flags'] |= 1;
9962 // get internal map
9963 $imap = array();
9964 if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
9965 foreach ($fmap as $v) {
9966 $imap[$v[2]] = $v[1];
9969 // decrypt eexec encrypted part
9970 $r = 55665; // eexec encryption constant
9971 $c1 = 52845;
9972 $c2 = 22719;
9973 $elen = strlen($encrypted);
9974 $eplain = '';
9975 for ($i = 0; $i < $elen; ++$i) {
9976 $chr = ord($encrypted{$i});
9977 $eplain .= chr($chr ^ ($r >> 8));
9978 $r = ((($chr + $r) * $c1 + $c2) % 65536);
9980 if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
9981 if ($matches[1] == 'true') {
9982 $fmetric['Flags'] |= 0x40000;
9985 if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
9986 $fmetric['StemV'] = intval($matches[1]);
9987 } else {
9988 $fmetric['StemV'] = 70;
9990 if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
9991 $fmetric['StemH'] = intval($matches[1]);
9992 } else {
9993 $fmetric['StemH'] = 30;
9995 if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
9996 $bv = explode(' ', $matches[1]);
9997 if (count($bv) >= 6) {
9998 $v1 = intval($bv[2]);
9999 $v2 = intval($bv[4]);
10000 if ($v1 <= $v2) {
10001 $fmetric['XHeight'] = $v1;
10002 $fmetric['CapHeight'] = $v2;
10003 } else {
10004 $fmetric['XHeight'] = $v2;
10005 $fmetric['CapHeight'] = $v1;
10007 } else {
10008 $fmetric['XHeight'] = 450;
10009 $fmetric['CapHeight'] = 700;
10011 } else {
10012 $fmetric['XHeight'] = 450;
10013 $fmetric['CapHeight'] = 700;
10015 // get the number of random bytes at the beginning of charstrings
10016 if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
10017 $lenIV = intval($matches[1]);
10018 } else {
10019 $lenIV = 4;
10021 $fmetric['Leading'] = 0;
10022 // get charstring data
10023 $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
10024 preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
10025 if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
10026 $enc_map = $this->encmaps->encmap[$enc];
10027 } else {
10028 $enc_map = false;
10030 $fmetric['cw'] = '';
10031 $fmetric['MaxWidth'] = 0;
10032 $cwidths = array();
10033 foreach ($matches as $k => $v) {
10034 $cid = 0;
10035 if (isset($imap[$v[1]])) {
10036 $cid = $imap[$v[1]];
10037 } elseif ($enc_map !== false) {
10038 $cid = array_search($v[1], $enc_map);
10039 if ($cid === false) {
10040 $cid = 0;
10041 } elseif ($cid > 1000) {
10042 $cid -= 1000;
10045 // decrypt charstring encrypted part
10046 $r = 4330; // charstring encryption constant
10047 $c1 = 52845;
10048 $c2 = 22719;
10049 $cd = $v[2];
10050 $clen = strlen($cd);
10051 $ccom = array();
10052 for ($i = 0; $i < $clen; ++$i) {
10053 $chr = ord($cd{$i});
10054 $ccom[] = ($chr ^ ($r >> 8));
10055 $r = ((($chr + $r) * $c1 + $c2) % 65536);
10057 // decode numbers
10058 $cdec = array();
10059 $ck = 0;
10060 $i = $lenIV;
10061 while ($i < $clen) {
10062 if ($ccom[$i] < 32) {
10063 $cdec[$ck] = $ccom[$i];
10064 if (($ck > 0) AND ($cdec[$ck] == 13)) {
10065 // hsbw command: update width
10066 $cwidths[$cid] = $cdec[($ck - 1)];
10068 ++$i;
10069 } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
10070 $cdec[$ck] = ($ccom[$i] - 139);
10071 ++$i;
10072 } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
10073 $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
10074 $i += 2;
10075 } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
10076 $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
10077 $i += 2;
10078 } elseif ($ccom[$i] == 255) {
10079 $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
10080 $vsval = unpack('li', $sval);
10081 $cdec[$ck] = $vsval['i'];
10082 $i += 5;
10084 ++$ck;
10086 } // end for each matches
10087 $fmetric['MissingWidth'] = $cwidths[0];
10088 $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
10089 $fmetric['AvgWidth'] = 0;
10090 // set chars widths
10091 for ($cid = 0; $cid <= 255; ++$cid) {
10092 if (isset($cwidths[$cid])) {
10093 if ($cwidths[$cid] > $fmetric['MaxWidth']) {
10094 $fmetric['MaxWidth'] = $cwidths[$cid];
10096 $fmetric['AvgWidth'] += $cwidths[$cid];
10097 $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
10098 } else {
10099 $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
10102 $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
10103 } else {
10104 // ---------- TRUE TYPE ----------
10105 if ($fmetric['type'] != 'cidfont0') {
10106 // store compressed font
10107 $fp = fopen($outpath.$fmetric['file'], 'wb');
10108 fwrite($fp, gzcompress($font));
10109 fclose($fp);
10111 $offset = 0; // offset position of the font data
10112 if ($this->_getULONG($font, $offset) != 0x10000) {
10113 // sfnt version must be 0x00010000 for TrueType version 1.0.
10114 return $font;
10116 $offset += 4;
10117 // get number of tables
10118 $numTables = $this->_getUSHORT($font, $offset);
10119 $offset += 2;
10120 // skip searchRange, entrySelector and rangeShift
10121 $offset += 6;
10122 // tables array
10123 $table = array();
10124 // ---------- get tables ----------
10125 for ($i = 0; $i < $numTables; ++$i) {
10126 // get table info
10127 $tag = substr($font, $offset, 4);
10128 $offset += 4;
10129 $table[$tag] = array();
10130 $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
10131 $offset += 4;
10132 $table[$tag]['offset'] = $this->_getULONG($font, $offset);
10133 $offset += 4;
10134 $table[$tag]['length'] = $this->_getULONG($font, $offset);
10135 $offset += 4;
10137 // check magicNumber
10138 $offset = $table['head']['offset'] + 12;
10139 if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
10140 // magicNumber must be 0x5F0F3CF5
10141 return $font;
10143 $offset += 4;
10144 $offset += 2; // skip flags
10145 // get FUnits
10146 $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
10147 $offset += 2;
10148 // units ratio constant
10149 $urk = (1000 / $fmetric['unitsPerEm']);
10150 $offset += 16; // skip created, modified
10151 $xMin = round($this->_getFWORD($font, $offset) * $urk);
10152 $offset += 2;
10153 $yMin = round($this->_getFWORD($font, $offset) * $urk);
10154 $offset += 2;
10155 $xMax = round($this->_getFWORD($font, $offset) * $urk);
10156 $offset += 2;
10157 $yMax = round($this->_getFWORD($font, $offset) * $urk);
10158 $offset += 2;
10159 $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
10160 $macStyle = $this->_getUSHORT($font, $offset);
10161 $offset += 2;
10162 // PDF font flags
10163 $fmetric['Flags'] = $flags;
10164 if (($macStyle & 2) == 2) {
10165 // italic flag
10166 $fmetric['Flags'] |= 64;
10168 // get offset mode (indexToLocFormat : 0 = short, 1 = long)
10169 $offset = $table['head']['offset'] + 50;
10170 $short_offset = ($this->_getSHORT($font, $offset) == 0);
10171 $offset += 2;
10172 // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
10173 $indexToLoc = array();
10174 $offset = $table['loca']['offset'];
10175 if ($short_offset) {
10176 // short version
10177 $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
10178 for ($i = 0; $i < $tot_num_glyphs; ++$i) {
10179 $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
10180 $offset += 2;
10182 } else {
10183 // long version
10184 $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
10185 for ($i = 0; $i < $tot_num_glyphs; ++$i) {
10186 $indexToLoc[$i] = $this->_getULONG($font, $offset);
10187 $offset += 4;
10190 // get glyphs indexes of chars from cmap table
10191 $offset = $table['cmap']['offset'] + 2;
10192 $numEncodingTables = $this->_getUSHORT($font, $offset);
10193 $offset += 2;
10194 $encodingTables = array();
10195 for ($i = 0; $i < $numEncodingTables; ++$i) {
10196 $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
10197 $offset += 2;
10198 $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
10199 $offset += 2;
10200 $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
10201 $offset += 4;
10203 // ---------- get os/2 metrics ----------
10204 $offset = $table['OS/2']['offset'];
10205 $offset += 2; // skip version
10206 // xAvgCharWidth
10207 $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
10208 $offset += 2;
10209 // usWeightClass
10210 $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
10211 // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
10212 $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
10213 $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
10214 $offset += 2;
10215 $offset += 2; // usWidthClass
10216 $fsType = $this->_getSHORT($font, $offset);
10217 $offset += 2;
10218 if ($fsType == 2) {
10219 $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
10221 // ---------- get font name ----------
10222 $fmetric['name'] = '';
10223 $offset = $table['name']['offset'];
10224 $offset += 2; // skip Format selector (=0).
10225 // Number of NameRecords that follow n.
10226 $numNameRecords = $this->_getUSHORT($font, $offset);
10227 $offset += 2;
10228 // Offset to start of string storage (from start of table).
10229 $stringStorageOffset = $this->_getUSHORT($font, $offset);
10230 $offset += 2;
10231 for ($i = 0; $i < $numNameRecords; ++$i) {
10232 $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
10233 // Name ID.
10234 $nameID = $this->_getUSHORT($font, $offset);
10235 $offset += 2;
10236 if ($nameID == 6) {
10237 // String length (in bytes).
10238 $stringLength = $this->_getUSHORT($font, $offset);
10239 $offset += 2;
10240 // String offset from start of storage area (in bytes).
10241 $stringOffset = $this->_getUSHORT($font, $offset);
10242 $offset += 2;
10243 $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
10244 $fmetric['name'] = substr($font, $offset, $stringLength);
10245 $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
10246 break;
10247 } else {
10248 $offset += 4; // skip String length, String offset
10251 if (empty($fmetric['name'])) {
10252 $fmetric['name'] = $font_name;
10254 // ---------- get post data ----------
10255 $offset = $table['post']['offset'];
10256 $offset += 4; // skip Format Type
10257 $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
10258 $offset += 4;
10259 $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
10260 $offset += 2;
10261 $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
10262 $offset += 2;
10263 $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
10264 $offset += 2;
10265 if ($isFixedPitch) {
10266 $fmetric['Flags'] |= 1;
10268 // ---------- get hhea data ----------
10269 $offset = $table['hhea']['offset'];
10270 $offset += 4; // skip Table version number
10271 // Ascender
10272 $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
10273 $offset += 2;
10274 // Descender
10275 $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
10276 $offset += 2;
10277 // LineGap
10278 $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
10279 $offset += 2;
10280 // advanceWidthMax
10281 $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
10282 $offset += 2;
10283 $offset += 22; // skip some values
10284 // get the number of hMetric entries in hmtx table
10285 $numberOfHMetrics = $this->_getUSHORT($font, $offset);
10286 // ---------- get maxp data ----------
10287 $offset = $table['maxp']['offset'];
10288 $offset += 4; // skip Table version number
10289 // get the the number of glyphs in the font.
10290 $numGlyphs = $this->_getUSHORT($font, $offset);
10291 // ---------- get CIDToGIDMap ----------
10292 $ctg = array();
10293 foreach ($encodingTables as $enctable) {
10294 if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
10295 $modesymbol = true;
10296 } else {
10297 $modesymbol = false;
10299 $offset = $table['cmap']['offset'] + $enctable['offset'];
10300 $format = $this->_getUSHORT($font, $offset);
10301 $offset += 2;
10302 switch ($format) {
10303 case 0: { // Format 0: Byte encoding table
10304 $offset += 4; // skip length and version/language
10305 for ($c = 0; $c < 256; ++$c) {
10306 $g = $this->_getBYTE($font, $offset);
10307 $ctg[$c] = $g;
10308 ++$offset;
10310 break;
10312 case 2: { // Format 2: High-byte mapping through table
10313 $offset += 4; // skip length and version/language
10314 $numSubHeaders = 0;
10315 for ($i = 0; $i < 256; ++$i) {
10316 // Array that maps high bytes to subHeaders: value is subHeader index * 8.
10317 $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
10318 $offset += 2;
10319 if ($numSubHeaders < $subHeaderKeys[$i]) {
10320 $numSubHeaders = $subHeaderKeys[$i];
10323 // the number of subHeaders is equal to the max of subHeaderKeys + 1
10324 ++$numSubHeaders;
10325 // read subHeader structures
10326 $subHeaders = array();
10327 $numGlyphIndexArray = 0;
10328 for ($k = 0; $k < $numSubHeaders; ++$k) {
10329 $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
10330 $offset += 2;
10331 $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
10332 $offset += 2;
10333 $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
10334 $offset += 2;
10335 $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
10336 $offset += 2;
10337 $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
10338 $subHeaders[$k]['idRangeOffset'] /= 2;
10339 $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
10341 for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
10342 $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
10343 $offset += 2;
10345 for ($i = 0; $i < 256; ++$i) {
10346 $k = $subHeaderKeys[$i];
10347 if ($k == 0) {
10348 // one byte code
10349 $c = $i;
10350 $g = $glyphIndexArray[0];
10351 $ctg[$c] = $g;
10352 } else {
10353 // two bytes code
10354 $start_byte = $subHeaders[$k]['firstCode'];
10355 $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
10356 for ($j = $start_byte; $j < $end_byte; ++$j) {
10357 // combine high and low bytes
10358 $c = (($i << 8) + $j);
10359 $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
10360 $g = $glyphIndexArray[$idRangeOffset];
10361 $g += ($idDelta[$k] - 65536);
10362 if ($g < 0) {
10363 $g = 0;
10365 $ctg[$c] = $g;
10369 break;
10371 case 4: { // Format 4: Segment mapping to delta values
10372 $length = $this->_getUSHORT($font, $offset);
10373 $offset += 2;
10374 $offset += 2; // skip version/language
10375 $segCount = ($this->_getUSHORT($font, $offset) / 2);
10376 $offset += 2;
10377 $offset += 6; // skip searchRange, entrySelector, rangeShift
10378 $endCount = array(); // array of end character codes for each segment
10379 for ($k = 0; $k < $segCount; ++$k) {
10380 $endCount[$k] = $this->_getUSHORT($font, $offset);
10381 $offset += 2;
10383 $offset += 2; // skip reservedPad
10384 $startCount = array(); // array of start character codes for each segment
10385 for ($k = 0; $k < $segCount; ++$k) {
10386 $startCount[$k] = $this->_getUSHORT($font, $offset);
10387 $offset += 2;
10389 $idDelta = array(); // delta for all character codes in segment
10390 for ($k = 0; $k < $segCount; ++$k) {
10391 $idDelta[$k] = $this->_getUSHORT($font, $offset);
10392 $offset += 2;
10394 $idRangeOffset = array(); // Offsets into glyphIdArray or 0
10395 for ($k = 0; $k < $segCount; ++$k) {
10396 $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
10397 $offset += 2;
10399 $gidlen = ($length / 2) - 8 - (4 * $segCount);
10400 $glyphIdArray = array(); // glyph index array
10401 for ($k = 0; $k < $gidlen; ++$k) {
10402 $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
10403 $offset += 2;
10405 for ($k = 0; $k < $segCount; ++$k) {
10406 for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
10407 if ($idRangeOffset[$k] == 0) {
10408 $g = $c;
10409 } else {
10410 $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
10411 $g = $glyphIdArray[$gid];
10413 $g += ($idDelta[$k] - 65536);
10414 if ($g < 0) {
10415 $g = 0;
10417 $ctg[$c] = $g;
10420 break;
10422 case 6: { // Format 6: Trimmed table mapping
10423 $offset += 4; // skip length and version/language
10424 $firstCode = $this->_getUSHORT($font, $offset);
10425 $offset += 2;
10426 $entryCount = $this->_getUSHORT($font, $offset);
10427 $offset += 2;
10428 for ($k = 0; $k < $entryCount; ++$k) {
10429 $c = ($k + $firstCode);
10430 $g = $this->_getUSHORT($font, $offset);
10431 $ctg[$c] = $g;
10432 $offset += 2;
10434 break;
10436 case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
10437 $offset += 10; // skip reserved, length and version/language
10438 for ($k = 0; $k < 8192; ++$k) {
10439 $is32[$k] = $this->_getBYTE($font, $offset);
10440 ++$offset;
10442 $nGroups = $this->_getULONG($font, $offset);
10443 $offset += 4;
10444 for ($i = 0; $i < $nGroups; ++$i) {
10445 $startCharCode = $this->_getULONG($font, $offset);
10446 $offset += 4;
10447 $endCharCode = $this->_getULONG($font, $offset);
10448 $offset += 4;
10449 $startGlyphID = $this->_getULONG($font, $offset);
10450 $offset += 4;
10451 for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
10452 $is32idx = floor($c / 8);
10453 if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
10454 $c = $k;
10455 } else {
10456 // 32 bit format
10457 // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
10458 //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
10459 //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
10460 $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
10462 $ctg[$c] = $g;
10463 ++$startGlyphID;
10466 break;
10468 case 10: { // Format 10: Trimmed array
10469 $offset += 10; // skip reserved, length and version/language
10470 $startCharCode = $this->_getULONG($font, $offset);
10471 $offset += 4;
10472 $numChars = $this->_getULONG($font, $offset);
10473 $offset += 4;
10474 for ($k = 0; $k < $numChars; ++$k) {
10475 $c = ($k + $startCharCode);
10476 $g = $this->_getUSHORT($font, $offset);
10477 $ctg[$c] = $g;
10478 $offset += 2;
10480 break;
10482 case 12: { // Format 12: Segmented coverage
10483 $offset += 10; // skip length and version/language
10484 $nGroups = $this->_getULONG($font, $offset);
10485 $offset += 4;
10486 for ($k = 0; $k < $nGroups; ++$k) {
10487 $startCharCode = $this->_getULONG($font, $offset);
10488 $offset += 4;
10489 $endCharCode = $this->_getULONG($font, $offset);
10490 $offset += 4;
10491 $startGlyphCode = $this->_getULONG($font, $offset);
10492 $offset += 4;
10493 for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
10494 $ctg[$c] = $startGlyphCode;
10495 ++$startGlyphCode;
10498 break;
10500 case 13: { // Format 13: Many-to-one range mappings
10501 // to be implemented ...
10502 break;
10504 case 14: { // Format 14: Unicode Variation Sequences
10505 // to be implemented ...
10506 break;
10510 if (!isset($ctg[0])) {
10511 $ctg[0] = 0;
10513 // get xHeight (height of x)
10514 $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
10515 $yMin = $this->_getFWORD($font, $offset);
10516 $offset += 4;
10517 $yMax = $this->_getFWORD($font, $offset);
10518 $offset += 2;
10519 $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
10520 // get CapHeight (height of H)
10521 $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
10522 $yMin = $this->_getFWORD($font, $offset);
10523 $offset += 4;
10524 $yMax = $this->_getFWORD($font, $offset);
10525 $offset += 2;
10526 $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
10527 // ceate widths array
10528 $cw = array();
10529 $offset = $table['hmtx']['offset'];
10530 for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
10531 $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
10532 $offset += 4; // skip lsb
10534 if ($numberOfHMetrics < $numGlyphs) {
10535 // fill missing widths with the last value
10536 $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
10538 $fmetric['MissingWidth'] = $cw[0];
10539 $fmetric['cw'] = '';
10540 for ($cid = 0; $cid <= 65535; ++$cid) {
10541 if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
10542 $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
10545 } // end of true type
10546 if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
10547 $fmetric['type'] == 'TrueType';
10549 // ---------- create php font file ----------
10550 $pfile = '<'.'?'.'php'."\n";
10551 $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
10552 $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
10553 $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
10554 $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
10555 $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
10556 if ($fmetric['MissingWidth'] > 0) {
10557 $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
10558 } else {
10559 $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
10561 $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
10562 if ($fmetric['type'] == 'Type1') {
10563 // Type 1
10564 $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
10565 $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
10566 $pfile .= '$size1='.$fmetric['size1'].';'."\n";
10567 $pfile .= '$size2='.$fmetric['size2'].';'."\n";
10568 } else {
10569 $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
10570 if ($fmetric['type'] == 'cidfont0') {
10571 // CID-0
10572 switch ($fonttype) {
10573 case 'CID0JP': {
10574 $pfile .= '// Japanese'."\n";
10575 $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
10576 $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
10577 $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
10578 break;
10580 case 'CID0KR': {
10581 $pfile .= '// Korean'."\n";
10582 $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
10583 $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
10584 $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
10585 break;
10587 case 'CID0CS': {
10588 $pfile .= '// Chinese Simplified'."\n";
10589 $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
10590 $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
10591 $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
10592 break;
10594 case 'CID0CT':
10595 default: {
10596 $pfile .= '// Chinese Traditional'."\n";
10597 $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
10598 $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
10599 $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
10600 break;
10603 } else {
10604 // TrueType
10605 $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
10606 $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
10607 $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
10608 // create CIDToGIDMap
10609 $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
10610 foreach ($ctg as $cid => $gid) {
10611 $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
10613 // store compressed CIDToGIDMap
10614 $fp = fopen($outpath.$fmetric['ctg'], 'wb');
10615 fwrite($fp, gzcompress($cidtogidmap));
10616 fclose($fp);
10619 $pfile .= '$desc=array(';
10620 $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
10621 $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
10622 $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
10623 $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
10624 $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
10625 $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
10626 $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
10627 $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
10628 $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
10629 $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
10630 $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
10631 $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
10632 $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
10633 $pfile .= ');'."\n";
10634 $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
10635 $pfile .= '// --- EOF ---'."\n";
10636 // store file
10637 $fp = fopen($outpath.$font_name.'.php', 'w');
10638 fwrite($fp, $pfile);
10639 fclose($fp);
10640 // return TCPDF font name
10641 return $font_name;
10645 * Returns a subset of the TrueType font data without the unused glyphs.
10646 * @param $font (string) TrueType font data.
10647 * @param $subsetchars (array) Array of used characters (the glyphs to keep).
10648 * @return (string) A subset of TrueType font data without the unused glyphs.
10649 * @author Nicola Asuni
10650 * @protected
10651 * @since 5.2.000 (2010-06-02)
10653 protected function _getTrueTypeFontSubset($font, $subsetchars) {
10654 ksort($subsetchars);
10655 $offset = 0; // offset position of the font data
10656 if ($this->_getULONG($font, $offset) != 0x10000) {
10657 // sfnt version must be 0x00010000 for TrueType version 1.0.
10658 return $font;
10660 $offset += 4;
10661 // get number of tables
10662 $numTables = $this->_getUSHORT($font, $offset);
10663 $offset += 2;
10664 // skip searchRange, entrySelector and rangeShift
10665 $offset += 6;
10666 // tables array
10667 $table = array();
10668 // for each table
10669 for ($i = 0; $i < $numTables; ++$i) {
10670 // get table info
10671 $tag = substr($font, $offset, 4);
10672 $offset += 4;
10673 $table[$tag] = array();
10674 $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
10675 $offset += 4;
10676 $table[$tag]['offset'] = $this->_getULONG($font, $offset);
10677 $offset += 4;
10678 $table[$tag]['length'] = $this->_getULONG($font, $offset);
10679 $offset += 4;
10681 // check magicNumber
10682 $offset = $table['head']['offset'] + 12;
10683 if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
10684 // magicNumber must be 0x5F0F3CF5
10685 return $font;
10687 $offset += 4;
10688 // get offset mode (indexToLocFormat : 0 = short, 1 = long)
10689 $offset = $table['head']['offset'] + 50;
10690 $short_offset = ($this->_getSHORT($font, $offset) == 0);
10691 $offset += 2;
10692 // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
10693 $indexToLoc = array();
10694 $offset = $table['loca']['offset'];
10695 if ($short_offset) {
10696 // short version
10697 $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
10698 for ($i = 0; $i < $tot_num_glyphs; ++$i) {
10699 $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
10700 $offset += 2;
10702 } else {
10703 // long version
10704 $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
10705 for ($i = 0; $i < $tot_num_glyphs; ++$i) {
10706 $indexToLoc[$i] = $this->_getULONG($font, $offset);
10707 $offset += 4;
10710 // get glyphs indexes of chars from cmap table
10711 $subsetglyphs = array(); // glyph IDs on key
10712 $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
10713 $offset = $table['cmap']['offset'] + 2;
10714 $numEncodingTables = $this->_getUSHORT($font, $offset);
10715 $offset += 2;
10716 $encodingTables = array();
10717 for ($i = 0; $i < $numEncodingTables; ++$i) {
10718 $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
10719 $offset += 2;
10720 $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
10721 $offset += 2;
10722 $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
10723 $offset += 4;
10725 foreach ($encodingTables as $enctable) {
10726 if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
10727 $modesymbol = true;
10728 } else {
10729 $modesymbol = false;
10731 $offset = $table['cmap']['offset'] + $enctable['offset'];
10732 $format = $this->_getUSHORT($font, $offset);
10733 $offset += 2;
10734 switch ($format) {
10735 case 0: { // Format 0: Byte encoding table
10736 $offset += 4; // skip length and version/language
10737 for ($c = 0; $c < 256; ++$c) {
10738 if (isset($subsetchars[$c])) {
10739 $g = $this->_getBYTE($font, $offset);
10740 $subsetglyphs[$g] = true;
10742 ++$offset;
10744 break;
10746 case 2: { // Format 2: High-byte mapping through table
10747 $offset += 4; // skip length and version/language
10748 $numSubHeaders = 0;
10749 for ($i = 0; $i < 256; ++$i) {
10750 // Array that maps high bytes to subHeaders: value is subHeader index * 8.
10751 $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
10752 $offset += 2;
10753 if ($numSubHeaders < $subHeaderKeys[$i]) {
10754 $numSubHeaders = $subHeaderKeys[$i];
10757 // the number of subHeaders is equal to the max of subHeaderKeys + 1
10758 ++$numSubHeaders;
10759 // read subHeader structures
10760 $subHeaders = array();
10761 $numGlyphIndexArray = 0;
10762 for ($k = 0; $k < $numSubHeaders; ++$k) {
10763 $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
10764 $offset += 2;
10765 $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
10766 $offset += 2;
10767 $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
10768 $offset += 2;
10769 $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
10770 $offset += 2;
10771 $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
10772 $subHeaders[$k]['idRangeOffset'] /= 2;
10773 $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
10775 for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
10776 $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
10777 $offset += 2;
10779 for ($i = 0; $i < 256; ++$i) {
10780 $k = $subHeaderKeys[$i];
10781 if ($k == 0) {
10782 // one byte code
10783 $c = $i;
10784 if (isset($subsetchars[$c])) {
10785 $g = $glyphIndexArray[0];
10786 $subsetglyphs[$g] = true;
10788 } else {
10789 // two bytes code
10790 $start_byte = $subHeaders[$k]['firstCode'];
10791 $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
10792 for ($j = $start_byte; $j < $end_byte; ++$j) {
10793 // combine high and low bytes
10794 $c = (($i << 8) + $j);
10795 if (isset($subsetchars[$c])) {
10796 $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
10797 $g = $glyphIndexArray[$idRangeOffset];
10798 $g += ($idDelta[$k] - 65536);
10799 if ($g < 0) {
10800 $g = 0;
10802 $subsetglyphs[$g] = true;
10807 break;
10809 case 4: { // Format 4: Segment mapping to delta values
10810 $length = $this->_getUSHORT($font, $offset);
10811 $offset += 2;
10812 $offset += 2; // skip version/language
10813 $segCount = ($this->_getUSHORT($font, $offset) / 2);
10814 $offset += 2;
10815 $offset += 6; // skip searchRange, entrySelector, rangeShift
10816 $endCount = array(); // array of end character codes for each segment
10817 for ($k = 0; $k < $segCount; ++$k) {
10818 $endCount[$k] = $this->_getUSHORT($font, $offset);
10819 $offset += 2;
10821 $offset += 2; // skip reservedPad
10822 $startCount = array(); // array of start character codes for each segment
10823 for ($k = 0; $k < $segCount; ++$k) {
10824 $startCount[$k] = $this->_getUSHORT($font, $offset);
10825 $offset += 2;
10827 $idDelta = array(); // delta for all character codes in segment
10828 for ($k = 0; $k < $segCount; ++$k) {
10829 $idDelta[$k] = $this->_getUSHORT($font, $offset);
10830 $offset += 2;
10832 $idRangeOffset = array(); // Offsets into glyphIdArray or 0
10833 for ($k = 0; $k < $segCount; ++$k) {
10834 $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
10835 $offset += 2;
10837 $gidlen = ($length / 2) - 8 - (4 * $segCount);
10838 $glyphIdArray = array(); // glyph index array
10839 for ($k = 0; $k < $gidlen; ++$k) {
10840 $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
10841 $offset += 2;
10843 for ($k = 0; $k < $segCount; ++$k) {
10844 for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
10845 if (isset($subsetchars[$c])) {
10846 if ($idRangeOffset[$k] == 0) {
10847 $g = $c;
10848 } else {
10849 $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
10850 $g = $glyphIdArray[$gid];
10852 $g += ($idDelta[$k] - 65536);
10853 if ($g < 0) {
10854 $g = 0;
10856 $subsetglyphs[$g] = true;
10860 break;
10862 case 6: { // Format 6: Trimmed table mapping
10863 $offset += 4; // skip length and version/language
10864 $firstCode = $this->_getUSHORT($font, $offset);
10865 $offset += 2;
10866 $entryCount = $this->_getUSHORT($font, $offset);
10867 $offset += 2;
10868 for ($k = 0; $k < $entryCount; ++$k) {
10869 $c = ($k + $firstCode);
10870 if (isset($subsetchars[$c])) {
10871 $g = $this->_getUSHORT($font, $offset);
10872 $subsetglyphs[$g] = true;
10874 $offset += 2;
10876 break;
10878 case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
10879 $offset += 10; // skip reserved, length and version/language
10880 for ($k = 0; $k < 8192; ++$k) {
10881 $is32[$k] = $this->_getBYTE($font, $offset);
10882 ++$offset;
10884 $nGroups = $this->_getULONG($font, $offset);
10885 $offset += 4;
10886 for ($i = 0; $i < $nGroups; ++$i) {
10887 $startCharCode = $this->_getULONG($font, $offset);
10888 $offset += 4;
10889 $endCharCode = $this->_getULONG($font, $offset);
10890 $offset += 4;
10891 $startGlyphID = $this->_getULONG($font, $offset);
10892 $offset += 4;
10893 for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
10894 $is32idx = floor($c / 8);
10895 if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
10896 $c = $k;
10897 } else {
10898 // 32 bit format
10899 // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
10900 //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
10901 //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
10902 $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
10904 if (isset($subsetchars[$c])) {
10905 $subsetglyphs[$startGlyphID] = true;
10907 ++$startGlyphID;
10910 break;
10912 case 10: { // Format 10: Trimmed array
10913 $offset += 10; // skip reserved, length and version/language
10914 $startCharCode = $this->_getULONG($font, $offset);
10915 $offset += 4;
10916 $numChars = $this->_getULONG($font, $offset);
10917 $offset += 4;
10918 for ($k = 0; $k < $numChars; ++$k) {
10919 $c = ($k + $startCharCode);
10920 if (isset($subsetchars[$c])) {
10921 $g = $this->_getUSHORT($font, $offset);
10922 $subsetglyphs[$g] = true;
10924 $offset += 2;
10926 break;
10928 case 12: { // Format 12: Segmented coverage
10929 $offset += 10; // skip length and version/language
10930 $nGroups = $this->_getULONG($font, $offset);
10931 $offset += 4;
10932 for ($k = 0; $k < $nGroups; ++$k) {
10933 $startCharCode = $this->_getULONG($font, $offset);
10934 $offset += 4;
10935 $endCharCode = $this->_getULONG($font, $offset);
10936 $offset += 4;
10937 $startGlyphCode = $this->_getULONG($font, $offset);
10938 $offset += 4;
10939 for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
10940 if (isset($subsetchars[$c])) {
10941 $subsetglyphs[$startGlyphCode] = true;
10943 ++$startGlyphCode;
10946 break;
10948 case 13: { // Format 13: Many-to-one range mappings
10949 // to be implemented ...
10950 break;
10952 case 14: { // Format 14: Unicode Variation Sequences
10953 // to be implemented ...
10954 break;
10958 // include all parts of composite glyphs
10959 $new_sga = $subsetglyphs;
10960 while (!empty($new_sga)) {
10961 $sga = $new_sga;
10962 $new_sga = array();
10963 foreach ($sga as $key => $val) {
10964 if (isset($indexToLoc[$key])) {
10965 $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
10966 $numberOfContours = $this->_getSHORT($font, $offset);
10967 $offset += 2;
10968 if ($numberOfContours < 0) { // composite glyph
10969 $offset += 8; // skip xMin, yMin, xMax, yMax
10970 do {
10971 $flags = $this->_getUSHORT($font, $offset);
10972 $offset += 2;
10973 $glyphIndex = $this->_getUSHORT($font, $offset);
10974 $offset += 2;
10975 if (!isset($subsetglyphs[$glyphIndex])) {
10976 // add missing glyphs
10977 $new_sga[$glyphIndex] = true;
10979 // skip some bytes by case
10980 if ($flags & 1) {
10981 $offset += 4;
10982 } else {
10983 $offset += 2;
10985 if ($flags & 8) {
10986 $offset += 2;
10987 } elseif ($flags & 64) {
10988 $offset += 4;
10989 } elseif ($flags & 128) {
10990 $offset += 8;
10992 } while ($flags & 32);
10996 $subsetglyphs += $new_sga;
10998 // sort glyphs by key (and remove duplicates)
10999 ksort($subsetglyphs);
11000 // build new glyf and loca tables
11001 $glyf = '';
11002 $loca = '';
11003 $offset = 0;
11004 $glyf_offset = $table['glyf']['offset'];
11005 for ($i = 0; $i < $tot_num_glyphs; ++$i) {
11006 if (isset($subsetglyphs[$i])) {
11007 $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
11008 $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
11009 } else {
11010 $length = 0;
11012 if ($short_offset) {
11013 $loca .= pack('n', ($offset / 2));
11014 } else {
11015 $loca .= pack('N', $offset);
11017 $offset += $length;
11019 // array of table names to preserve (loca and glyf tables will be added later)
11020 // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
11021 $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
11022 // get the tables to preserve
11023 $offset = 12;
11024 foreach ($table as $tag => $val) {
11025 if (in_array($tag, $table_names)) {
11026 $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
11027 if ($tag == 'head') {
11028 // set the checkSumAdjustment to 0
11029 $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
11031 $pad = 4 - ($table[$tag]['length'] % 4);
11032 if ($pad != 4) {
11033 // the length of a table must be a multiple of four bytes
11034 $table[$tag]['length'] += $pad;
11035 $table[$tag]['data'] .= str_repeat("\x0", $pad);
11037 $table[$tag]['offset'] = $offset;
11038 $offset += $table[$tag]['length'];
11039 // check sum is not changed (so keep the following line commented)
11040 //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
11041 } else {
11042 unset($table[$tag]);
11045 // add loca
11046 $table['loca']['data'] = $loca;
11047 $table['loca']['length'] = strlen($loca);
11048 $pad = 4 - ($table['loca']['length'] % 4);
11049 if ($pad != 4) {
11050 // the length of a table must be a multiple of four bytes
11051 $table['loca']['length'] += $pad;
11052 $table['loca']['data'] .= str_repeat("\x0", $pad);
11054 $table['loca']['offset'] = $offset;
11055 $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
11056 $offset += $table['loca']['length'];
11057 // add glyf
11058 $table['glyf']['data'] = $glyf;
11059 $table['glyf']['length'] = strlen($glyf);
11060 $pad = 4 - ($table['glyf']['length'] % 4);
11061 if ($pad != 4) {
11062 // the length of a table must be a multiple of four bytes
11063 $table['glyf']['length'] += $pad;
11064 $table['glyf']['data'] .= str_repeat("\x0", $pad);
11066 $table['glyf']['offset'] = $offset;
11067 $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
11068 // rebuild font
11069 $font = '';
11070 $font .= pack('N', 0x10000); // sfnt version
11071 $numTables = count($table);
11072 $font .= pack('n', $numTables); // numTables
11073 $entrySelector = floor(log($numTables, 2));
11074 $searchRange = pow(2, $entrySelector) * 16;
11075 $rangeShift = ($numTables * 16) - $searchRange;
11076 $font .= pack('n', $searchRange); // searchRange
11077 $font .= pack('n', $entrySelector); // entrySelector
11078 $font .= pack('n', $rangeShift); // rangeShift
11079 $offset = ($numTables * 16);
11080 foreach ($table as $tag => $data) {
11081 $font .= $tag; // tag
11082 $font .= pack('N', $data['checkSum']); // checkSum
11083 $font .= pack('N', ($data['offset'] + $offset)); // offset
11084 $font .= pack('N', $data['length']); // length
11086 foreach ($table as $data) {
11087 $font .= $data['data'];
11089 // set checkSumAdjustment on head table
11090 $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
11091 $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
11092 return $font;
11096 * Returs the checksum of a TTF table.
11097 * @param $table (string) table to check
11098 * @param $length (int) length of table in bytes
11099 * @return int checksum
11100 * @author Nicola Asuni
11101 * @protected
11102 * @since 5.2.000 (2010-06-02)
11104 protected function _getTTFtableChecksum($table, $length) {
11105 $sum = 0;
11106 $tlen = ($length / 4);
11107 $offset = 0;
11108 for ($i = 0; $i < $tlen; ++$i) {
11109 $v = unpack('Ni', substr($table, $offset, 4));
11110 $sum += $v['i'];
11111 $offset += 4;
11113 $sum = unpack('Ni', pack('N', $sum));
11114 return $sum['i'];
11118 * Outputs font widths
11119 * @param $font (array) font data
11120 * @param $cidoffset (int) offset for CID values
11121 * @return PDF command string for font widths
11122 * @author Nicola Asuni
11123 * @protected
11124 * @since 4.4.000 (2008-12-07)
11126 protected function _putfontwidths($font, $cidoffset=0) {
11127 ksort($font['cw']);
11128 $rangeid = 0;
11129 $range = array();
11130 $prevcid = -2;
11131 $prevwidth = -1;
11132 $interval = false;
11133 // for each character
11134 foreach ($font['cw'] as $cid => $width) {
11135 $cid -= $cidoffset;
11136 if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
11137 // ignore the unused characters (font subsetting)
11138 continue;
11140 if ($width != $font['dw']) {
11141 if ($cid == ($prevcid + 1)) {
11142 // consecutive CID
11143 if ($width == $prevwidth) {
11144 if ($width == $range[$rangeid][0]) {
11145 $range[$rangeid][] = $width;
11146 } else {
11147 array_pop($range[$rangeid]);
11148 // new range
11149 $rangeid = $prevcid;
11150 $range[$rangeid] = array();
11151 $range[$rangeid][] = $prevwidth;
11152 $range[$rangeid][] = $width;
11154 $interval = true;
11155 $range[$rangeid]['interval'] = true;
11156 } else {
11157 if ($interval) {
11158 // new range
11159 $rangeid = $cid;
11160 $range[$rangeid] = array();
11161 $range[$rangeid][] = $width;
11162 } else {
11163 $range[$rangeid][] = $width;
11165 $interval = false;
11167 } else {
11168 // new range
11169 $rangeid = $cid;
11170 $range[$rangeid] = array();
11171 $range[$rangeid][] = $width;
11172 $interval = false;
11174 $prevcid = $cid;
11175 $prevwidth = $width;
11178 // optimize ranges
11179 $prevk = -1;
11180 $nextk = -1;
11181 $prevint = false;
11182 foreach ($range as $k => $ws) {
11183 $cws = count($ws);
11184 if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
11185 if (isset($range[$k]['interval'])) {
11186 unset($range[$k]['interval']);
11188 $range[$prevk] = array_merge($range[$prevk], $range[$k]);
11189 unset($range[$k]);
11190 } else {
11191 $prevk = $k;
11193 $nextk = $k + $cws;
11194 if (isset($ws['interval'])) {
11195 if ($cws > 3) {
11196 $prevint = true;
11197 } else {
11198 $prevint = false;
11200 unset($range[$k]['interval']);
11201 --$nextk;
11202 } else {
11203 $prevint = false;
11206 // output data
11207 $w = '';
11208 foreach ($range as $k => $ws) {
11209 if (count(array_count_values($ws)) == 1) {
11210 // interval mode is more compact
11211 $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
11212 } else {
11213 // range mode
11214 $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
11217 return '/W ['.$w.' ]';
11221 * Output fonts.
11222 * @author Nicola Asuni
11223 * @protected
11225 protected function _putfonts() {
11226 $nf = $this->n;
11227 foreach ($this->diffs as $diff) {
11228 //Encodings
11229 $this->_newobj();
11230 $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
11232 $mqr = $this->get_mqr();
11233 $this->set_mqr(false);
11234 foreach ($this->FontFiles as $file => $info) {
11235 // search and get font file to embedd
11236 $fontdir = $info['fontdir'];
11237 $file = strtolower($file);
11238 $fontfile = '';
11239 // search files on various directories
11240 if (($fontdir !== false) AND file_exists($fontdir.$file)) {
11241 $fontfile = $fontdir.$file;
11242 } elseif (file_exists($this->_getfontpath().$file)) {
11243 $fontfile = $this->_getfontpath().$file;
11244 } elseif (file_exists($file)) {
11245 $fontfile = $file;
11247 if (!$this->empty_string($fontfile)) {
11248 $font = file_get_contents($fontfile);
11249 $compressed = (substr($file, -2) == '.z');
11250 if ((!$compressed) AND (isset($info['length2']))) {
11251 $header = (ord($font{0}) == 128);
11252 if ($header) {
11253 // strip first binary header
11254 $font = substr($font, 6);
11256 if ($header AND (ord($font{$info['length1']}) == 128)) {
11257 // strip second binary header
11258 $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
11260 } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
11261 if ($compressed) {
11262 // uncompress font
11263 $font = gzuncompress($font);
11265 // merge subset characters
11266 $subsetchars = array(); // used chars
11267 foreach ($info['fontkeys'] as $fontkey) {
11268 $fontinfo = $this->getFontBuffer($fontkey);
11269 $subsetchars += $fontinfo['subsetchars'];
11271 // rebuild a font subset
11272 $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
11273 // calculate new font length
11274 $info['length1'] = strlen($font);
11275 if ($compressed) {
11276 // recompress font
11277 $font = gzcompress($font);
11280 $this->_newobj();
11281 $this->FontFiles[$file]['n'] = $this->n;
11282 $stream = $this->_getrawstream($font);
11283 $out = '<< /Length '.strlen($stream);
11284 if ($compressed) {
11285 $out .= ' /Filter /FlateDecode';
11287 $out .= ' /Length1 '.$info['length1'];
11288 if (isset($info['length2'])) {
11289 $out .= ' /Length2 '.$info['length2'].' /Length3 0';
11291 $out .= ' >>';
11292 $out .= ' stream'."\n".$stream."\n".'endstream';
11293 $out .= "\n".'endobj';
11294 $this->_out($out);
11297 $this->set_mqr($mqr);
11298 foreach ($this->fontkeys as $k) {
11299 //Font objects
11300 $font = $this->getFontBuffer($k);
11301 $type = $font['type'];
11302 $name = $font['name'];
11303 if ($type == 'core') {
11304 // standard core font
11305 $out = $this->_getobj($this->font_obj_ids[$k])."\n";
11306 $out .= '<</Type /Font';
11307 $out .= ' /Subtype /Type1';
11308 $out .= ' /BaseFont /'.$name;
11309 $out .= ' /Name /F'.$font['i'];
11310 if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
11311 $out .= ' /Encoding /WinAnsiEncoding';
11313 if ($k == 'helvetica') {
11314 // add default font for annotations
11315 $this->annotation_fonts[$k] = $font['i'];
11317 $out .= ' >>';
11318 $out .= "\n".'endobj';
11319 $this->_out($out);
11320 } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
11321 // additional Type1 or TrueType font
11322 $out = $this->_getobj($this->font_obj_ids[$k])."\n";
11323 $out .= '<</Type /Font';
11324 $out .= ' /Subtype /'.$type;
11325 $out .= ' /BaseFont /'.$name;
11326 $out .= ' /Name /F'.$font['i'];
11327 $out .= ' /FirstChar 32 /LastChar 255';
11328 $out .= ' /Widths '.($this->n + 1).' 0 R';
11329 $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
11330 if ($font['enc']) {
11331 if (isset($font['diff'])) {
11332 $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
11333 } else {
11334 $out .= ' /Encoding /WinAnsiEncoding';
11337 $out .= ' >>';
11338 $out .= "\n".'endobj';
11339 $this->_out($out);
11340 // Widths
11341 $this->_newobj();
11342 $s = '[';
11343 for ($i = 32; $i < 256; ++$i) {
11344 $s .= $font['cw'][$i].' ';
11346 $s .= ']';
11347 $s .= "\n".'endobj';
11348 $this->_out($s);
11349 //Descriptor
11350 $this->_newobj();
11351 $s = '<</Type /FontDescriptor /FontName /'.$name;
11352 foreach ($font['desc'] as $fdk => $fdv) {
11353 if (is_float($fdv)) {
11354 $fdv = sprintf('%.3F', $fdv);
11356 $s .= ' /'.$fdk.' '.$fdv.'';
11358 if (!$this->empty_string($font['file'])) {
11359 $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
11361 $s .= '>>';
11362 $s .= "\n".'endobj';
11363 $this->_out($s);
11364 } else {
11365 // additional types
11366 $mtd = '_put'.strtolower($type);
11367 if (!method_exists($this, $mtd)) {
11368 $this->Error('Unsupported font type: '.$type);
11370 $this->$mtd($font);
11376 * Adds unicode fonts.<br>
11377 * Based on PDF Reference 1.3 (section 5)
11378 * @param $font (array) font data
11379 * @protected
11380 * @author Nicola Asuni
11381 * @since 1.52.0.TC005 (2005-01-05)
11383 protected function _puttruetypeunicode($font) {
11384 $fontname = '';
11385 if ($font['subset']) {
11386 // change name for font subsetting
11387 $subtag = sprintf('%06u', $font['i']);
11388 $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
11389 $fontname .= $subtag.'+';
11391 $fontname .= $font['name'];
11392 // Type0 Font
11393 // A composite font composed of other fonts, organized hierarchically
11394 $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
11395 $out .= '<< /Type /Font';
11396 $out .= ' /Subtype /Type0';
11397 $out .= ' /BaseFont /'.$fontname;
11398 $out .= ' /Name /F'.$font['i'];
11399 $out .= ' /Encoding /'.$font['enc'];
11400 $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
11401 $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
11402 $out .= ' >>';
11403 $out .= "\n".'endobj';
11404 $this->_out($out);
11405 // ToUnicode map for Identity-H
11406 $stream = "/CIDInit /ProcSet findresource begin\n";
11407 $stream .= "12 dict begin\n";
11408 $stream .= "begincmap\n";
11409 $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
11410 $stream .= "/CMapName /Adobe-Identity-UCS def\n";
11411 $stream .= "/CMapType 2 def\n";
11412 $stream .= "/WMode 0 def\n";
11413 $stream .= "1 begincodespacerange\n";
11414 $stream .= "<0000> <FFFF>\n";
11415 $stream .= "endcodespacerange\n";
11416 $stream .= "100 beginbfrange\n";
11417 $stream .= "<0000> <00ff> <0000>\n";
11418 $stream .= "<0100> <01ff> <0100>\n";
11419 $stream .= "<0200> <02ff> <0200>\n";
11420 $stream .= "<0300> <03ff> <0300>\n";
11421 $stream .= "<0400> <04ff> <0400>\n";
11422 $stream .= "<0500> <05ff> <0500>\n";
11423 $stream .= "<0600> <06ff> <0600>\n";
11424 $stream .= "<0700> <07ff> <0700>\n";
11425 $stream .= "<0800> <08ff> <0800>\n";
11426 $stream .= "<0900> <09ff> <0900>\n";
11427 $stream .= "<0a00> <0aff> <0a00>\n";
11428 $stream .= "<0b00> <0bff> <0b00>\n";
11429 $stream .= "<0c00> <0cff> <0c00>\n";
11430 $stream .= "<0d00> <0dff> <0d00>\n";
11431 $stream .= "<0e00> <0eff> <0e00>\n";
11432 $stream .= "<0f00> <0fff> <0f00>\n";
11433 $stream .= "<1000> <10ff> <1000>\n";
11434 $stream .= "<1100> <11ff> <1100>\n";
11435 $stream .= "<1200> <12ff> <1200>\n";
11436 $stream .= "<1300> <13ff> <1300>\n";
11437 $stream .= "<1400> <14ff> <1400>\n";
11438 $stream .= "<1500> <15ff> <1500>\n";
11439 $stream .= "<1600> <16ff> <1600>\n";
11440 $stream .= "<1700> <17ff> <1700>\n";
11441 $stream .= "<1800> <18ff> <1800>\n";
11442 $stream .= "<1900> <19ff> <1900>\n";
11443 $stream .= "<1a00> <1aff> <1a00>\n";
11444 $stream .= "<1b00> <1bff> <1b00>\n";
11445 $stream .= "<1c00> <1cff> <1c00>\n";
11446 $stream .= "<1d00> <1dff> <1d00>\n";
11447 $stream .= "<1e00> <1eff> <1e00>\n";
11448 $stream .= "<1f00> <1fff> <1f00>\n";
11449 $stream .= "<2000> <20ff> <2000>\n";
11450 $stream .= "<2100> <21ff> <2100>\n";
11451 $stream .= "<2200> <22ff> <2200>\n";
11452 $stream .= "<2300> <23ff> <2300>\n";
11453 $stream .= "<2400> <24ff> <2400>\n";
11454 $stream .= "<2500> <25ff> <2500>\n";
11455 $stream .= "<2600> <26ff> <2600>\n";
11456 $stream .= "<2700> <27ff> <2700>\n";
11457 $stream .= "<2800> <28ff> <2800>\n";
11458 $stream .= "<2900> <29ff> <2900>\n";
11459 $stream .= "<2a00> <2aff> <2a00>\n";
11460 $stream .= "<2b00> <2bff> <2b00>\n";
11461 $stream .= "<2c00> <2cff> <2c00>\n";
11462 $stream .= "<2d00> <2dff> <2d00>\n";
11463 $stream .= "<2e00> <2eff> <2e00>\n";
11464 $stream .= "<2f00> <2fff> <2f00>\n";
11465 $stream .= "<3000> <30ff> <3000>\n";
11466 $stream .= "<3100> <31ff> <3100>\n";
11467 $stream .= "<3200> <32ff> <3200>\n";
11468 $stream .= "<3300> <33ff> <3300>\n";
11469 $stream .= "<3400> <34ff> <3400>\n";
11470 $stream .= "<3500> <35ff> <3500>\n";
11471 $stream .= "<3600> <36ff> <3600>\n";
11472 $stream .= "<3700> <37ff> <3700>\n";
11473 $stream .= "<3800> <38ff> <3800>\n";
11474 $stream .= "<3900> <39ff> <3900>\n";
11475 $stream .= "<3a00> <3aff> <3a00>\n";
11476 $stream .= "<3b00> <3bff> <3b00>\n";
11477 $stream .= "<3c00> <3cff> <3c00>\n";
11478 $stream .= "<3d00> <3dff> <3d00>\n";
11479 $stream .= "<3e00> <3eff> <3e00>\n";
11480 $stream .= "<3f00> <3fff> <3f00>\n";
11481 $stream .= "<4000> <40ff> <4000>\n";
11482 $stream .= "<4100> <41ff> <4100>\n";
11483 $stream .= "<4200> <42ff> <4200>\n";
11484 $stream .= "<4300> <43ff> <4300>\n";
11485 $stream .= "<4400> <44ff> <4400>\n";
11486 $stream .= "<4500> <45ff> <4500>\n";
11487 $stream .= "<4600> <46ff> <4600>\n";
11488 $stream .= "<4700> <47ff> <4700>\n";
11489 $stream .= "<4800> <48ff> <4800>\n";
11490 $stream .= "<4900> <49ff> <4900>\n";
11491 $stream .= "<4a00> <4aff> <4a00>\n";
11492 $stream .= "<4b00> <4bff> <4b00>\n";
11493 $stream .= "<4c00> <4cff> <4c00>\n";
11494 $stream .= "<4d00> <4dff> <4d00>\n";
11495 $stream .= "<4e00> <4eff> <4e00>\n";
11496 $stream .= "<4f00> <4fff> <4f00>\n";
11497 $stream .= "<5000> <50ff> <5000>\n";
11498 $stream .= "<5100> <51ff> <5100>\n";
11499 $stream .= "<5200> <52ff> <5200>\n";
11500 $stream .= "<5300> <53ff> <5300>\n";
11501 $stream .= "<5400> <54ff> <5400>\n";
11502 $stream .= "<5500> <55ff> <5500>\n";
11503 $stream .= "<5600> <56ff> <5600>\n";
11504 $stream .= "<5700> <57ff> <5700>\n";
11505 $stream .= "<5800> <58ff> <5800>\n";
11506 $stream .= "<5900> <59ff> <5900>\n";
11507 $stream .= "<5a00> <5aff> <5a00>\n";
11508 $stream .= "<5b00> <5bff> <5b00>\n";
11509 $stream .= "<5c00> <5cff> <5c00>\n";
11510 $stream .= "<5d00> <5dff> <5d00>\n";
11511 $stream .= "<5e00> <5eff> <5e00>\n";
11512 $stream .= "<5f00> <5fff> <5f00>\n";
11513 $stream .= "<6000> <60ff> <6000>\n";
11514 $stream .= "<6100> <61ff> <6100>\n";
11515 $stream .= "<6200> <62ff> <6200>\n";
11516 $stream .= "<6300> <63ff> <6300>\n";
11517 $stream .= "endbfrange\n";
11518 $stream .= "100 beginbfrange\n";
11519 $stream .= "<6400> <64ff> <6400>\n";
11520 $stream .= "<6500> <65ff> <6500>\n";
11521 $stream .= "<6600> <66ff> <6600>\n";
11522 $stream .= "<6700> <67ff> <6700>\n";
11523 $stream .= "<6800> <68ff> <6800>\n";
11524 $stream .= "<6900> <69ff> <6900>\n";
11525 $stream .= "<6a00> <6aff> <6a00>\n";
11526 $stream .= "<6b00> <6bff> <6b00>\n";
11527 $stream .= "<6c00> <6cff> <6c00>\n";
11528 $stream .= "<6d00> <6dff> <6d00>\n";
11529 $stream .= "<6e00> <6eff> <6e00>\n";
11530 $stream .= "<6f00> <6fff> <6f00>\n";
11531 $stream .= "<7000> <70ff> <7000>\n";
11532 $stream .= "<7100> <71ff> <7100>\n";
11533 $stream .= "<7200> <72ff> <7200>\n";
11534 $stream .= "<7300> <73ff> <7300>\n";
11535 $stream .= "<7400> <74ff> <7400>\n";
11536 $stream .= "<7500> <75ff> <7500>\n";
11537 $stream .= "<7600> <76ff> <7600>\n";
11538 $stream .= "<7700> <77ff> <7700>\n";
11539 $stream .= "<7800> <78ff> <7800>\n";
11540 $stream .= "<7900> <79ff> <7900>\n";
11541 $stream .= "<7a00> <7aff> <7a00>\n";
11542 $stream .= "<7b00> <7bff> <7b00>\n";
11543 $stream .= "<7c00> <7cff> <7c00>\n";
11544 $stream .= "<7d00> <7dff> <7d00>\n";
11545 $stream .= "<7e00> <7eff> <7e00>\n";
11546 $stream .= "<7f00> <7fff> <7f00>\n";
11547 $stream .= "<8000> <80ff> <8000>\n";
11548 $stream .= "<8100> <81ff> <8100>\n";
11549 $stream .= "<8200> <82ff> <8200>\n";
11550 $stream .= "<8300> <83ff> <8300>\n";
11551 $stream .= "<8400> <84ff> <8400>\n";
11552 $stream .= "<8500> <85ff> <8500>\n";
11553 $stream .= "<8600> <86ff> <8600>\n";
11554 $stream .= "<8700> <87ff> <8700>\n";
11555 $stream .= "<8800> <88ff> <8800>\n";
11556 $stream .= "<8900> <89ff> <8900>\n";
11557 $stream .= "<8a00> <8aff> <8a00>\n";
11558 $stream .= "<8b00> <8bff> <8b00>\n";
11559 $stream .= "<8c00> <8cff> <8c00>\n";
11560 $stream .= "<8d00> <8dff> <8d00>\n";
11561 $stream .= "<8e00> <8eff> <8e00>\n";
11562 $stream .= "<8f00> <8fff> <8f00>\n";
11563 $stream .= "<9000> <90ff> <9000>\n";
11564 $stream .= "<9100> <91ff> <9100>\n";
11565 $stream .= "<9200> <92ff> <9200>\n";
11566 $stream .= "<9300> <93ff> <9300>\n";
11567 $stream .= "<9400> <94ff> <9400>\n";
11568 $stream .= "<9500> <95ff> <9500>\n";
11569 $stream .= "<9600> <96ff> <9600>\n";
11570 $stream .= "<9700> <97ff> <9700>\n";
11571 $stream .= "<9800> <98ff> <9800>\n";
11572 $stream .= "<9900> <99ff> <9900>\n";
11573 $stream .= "<9a00> <9aff> <9a00>\n";
11574 $stream .= "<9b00> <9bff> <9b00>\n";
11575 $stream .= "<9c00> <9cff> <9c00>\n";
11576 $stream .= "<9d00> <9dff> <9d00>\n";
11577 $stream .= "<9e00> <9eff> <9e00>\n";
11578 $stream .= "<9f00> <9fff> <9f00>\n";
11579 $stream .= "<a000> <a0ff> <a000>\n";
11580 $stream .= "<a100> <a1ff> <a100>\n";
11581 $stream .= "<a200> <a2ff> <a200>\n";
11582 $stream .= "<a300> <a3ff> <a300>\n";
11583 $stream .= "<a400> <a4ff> <a400>\n";
11584 $stream .= "<a500> <a5ff> <a500>\n";
11585 $stream .= "<a600> <a6ff> <a600>\n";
11586 $stream .= "<a700> <a7ff> <a700>\n";
11587 $stream .= "<a800> <a8ff> <a800>\n";
11588 $stream .= "<a900> <a9ff> <a900>\n";
11589 $stream .= "<aa00> <aaff> <aa00>\n";
11590 $stream .= "<ab00> <abff> <ab00>\n";
11591 $stream .= "<ac00> <acff> <ac00>\n";
11592 $stream .= "<ad00> <adff> <ad00>\n";
11593 $stream .= "<ae00> <aeff> <ae00>\n";
11594 $stream .= "<af00> <afff> <af00>\n";
11595 $stream .= "<b000> <b0ff> <b000>\n";
11596 $stream .= "<b100> <b1ff> <b100>\n";
11597 $stream .= "<b200> <b2ff> <b200>\n";
11598 $stream .= "<b300> <b3ff> <b300>\n";
11599 $stream .= "<b400> <b4ff> <b400>\n";
11600 $stream .= "<b500> <b5ff> <b500>\n";
11601 $stream .= "<b600> <b6ff> <b600>\n";
11602 $stream .= "<b700> <b7ff> <b700>\n";
11603 $stream .= "<b800> <b8ff> <b800>\n";
11604 $stream .= "<b900> <b9ff> <b900>\n";
11605 $stream .= "<ba00> <baff> <ba00>\n";
11606 $stream .= "<bb00> <bbff> <bb00>\n";
11607 $stream .= "<bc00> <bcff> <bc00>\n";
11608 $stream .= "<bd00> <bdff> <bd00>\n";
11609 $stream .= "<be00> <beff> <be00>\n";
11610 $stream .= "<bf00> <bfff> <bf00>\n";
11611 $stream .= "<c000> <c0ff> <c000>\n";
11612 $stream .= "<c100> <c1ff> <c100>\n";
11613 $stream .= "<c200> <c2ff> <c200>\n";
11614 $stream .= "<c300> <c3ff> <c300>\n";
11615 $stream .= "<c400> <c4ff> <c400>\n";
11616 $stream .= "<c500> <c5ff> <c500>\n";
11617 $stream .= "<c600> <c6ff> <c600>\n";
11618 $stream .= "<c700> <c7ff> <c700>\n";
11619 $stream .= "endbfrange\n";
11620 $stream .= "56 beginbfrange\n";
11621 $stream .= "<c800> <c8ff> <c800>\n";
11622 $stream .= "<c900> <c9ff> <c900>\n";
11623 $stream .= "<ca00> <caff> <ca00>\n";
11624 $stream .= "<cb00> <cbff> <cb00>\n";
11625 $stream .= "<cc00> <ccff> <cc00>\n";
11626 $stream .= "<cd00> <cdff> <cd00>\n";
11627 $stream .= "<ce00> <ceff> <ce00>\n";
11628 $stream .= "<cf00> <cfff> <cf00>\n";
11629 $stream .= "<d000> <d0ff> <d000>\n";
11630 $stream .= "<d100> <d1ff> <d100>\n";
11631 $stream .= "<d200> <d2ff> <d200>\n";
11632 $stream .= "<d300> <d3ff> <d300>\n";
11633 $stream .= "<d400> <d4ff> <d400>\n";
11634 $stream .= "<d500> <d5ff> <d500>\n";
11635 $stream .= "<d600> <d6ff> <d600>\n";
11636 $stream .= "<d700> <d7ff> <d700>\n";
11637 $stream .= "<d800> <d8ff> <d800>\n";
11638 $stream .= "<d900> <d9ff> <d900>\n";
11639 $stream .= "<da00> <daff> <da00>\n";
11640 $stream .= "<db00> <dbff> <db00>\n";
11641 $stream .= "<dc00> <dcff> <dc00>\n";
11642 $stream .= "<dd00> <ddff> <dd00>\n";
11643 $stream .= "<de00> <deff> <de00>\n";
11644 $stream .= "<df00> <dfff> <df00>\n";
11645 $stream .= "<e000> <e0ff> <e000>\n";
11646 $stream .= "<e100> <e1ff> <e100>\n";
11647 $stream .= "<e200> <e2ff> <e200>\n";
11648 $stream .= "<e300> <e3ff> <e300>\n";
11649 $stream .= "<e400> <e4ff> <e400>\n";
11650 $stream .= "<e500> <e5ff> <e500>\n";
11651 $stream .= "<e600> <e6ff> <e600>\n";
11652 $stream .= "<e700> <e7ff> <e700>\n";
11653 $stream .= "<e800> <e8ff> <e800>\n";
11654 $stream .= "<e900> <e9ff> <e900>\n";
11655 $stream .= "<ea00> <eaff> <ea00>\n";
11656 $stream .= "<eb00> <ebff> <eb00>\n";
11657 $stream .= "<ec00> <ecff> <ec00>\n";
11658 $stream .= "<ed00> <edff> <ed00>\n";
11659 $stream .= "<ee00> <eeff> <ee00>\n";
11660 $stream .= "<ef00> <efff> <ef00>\n";
11661 $stream .= "<f000> <f0ff> <f000>\n";
11662 $stream .= "<f100> <f1ff> <f100>\n";
11663 $stream .= "<f200> <f2ff> <f200>\n";
11664 $stream .= "<f300> <f3ff> <f300>\n";
11665 $stream .= "<f400> <f4ff> <f400>\n";
11666 $stream .= "<f500> <f5ff> <f500>\n";
11667 $stream .= "<f600> <f6ff> <f600>\n";
11668 $stream .= "<f700> <f7ff> <f700>\n";
11669 $stream .= "<f800> <f8ff> <f800>\n";
11670 $stream .= "<f900> <f9ff> <f900>\n";
11671 $stream .= "<fa00> <faff> <fa00>\n";
11672 $stream .= "<fb00> <fbff> <fb00>\n";
11673 $stream .= "<fc00> <fcff> <fc00>\n";
11674 $stream .= "<fd00> <fdff> <fd00>\n";
11675 $stream .= "<fe00> <feff> <fe00>\n";
11676 $stream .= "<ff00> <ffff> <ff00>\n";
11677 $stream .= "endbfrange\n";
11678 $stream .= "endcmap\n";
11679 $stream .= "CMapName currentdict /CMap defineresource pop\n";
11680 $stream .= "end\n";
11681 $stream .= "end";
11682 // ToUnicode Object
11683 $this->_newobj();
11684 $stream = ($this->compress) ? gzcompress($stream) : $stream;
11685 $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
11686 $stream = $this->_getrawstream($stream);
11687 $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
11688 // CIDFontType2
11689 // A CIDFont whose glyph descriptions are based on TrueType font technology
11690 $oid = $this->_newobj();
11691 $out = '<< /Type /Font';
11692 $out .= ' /Subtype /CIDFontType2';
11693 $out .= ' /BaseFont /'.$fontname;
11694 // A dictionary containing entries that define the character collection of the CIDFont.
11695 $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
11696 $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
11697 $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
11698 $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
11699 $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
11700 $out .= ' /DW '.$font['dw']; // default width
11701 $out .= "\n".$this->_putfontwidths($font, 0);
11702 if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
11703 $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
11705 $out .= ' >>';
11706 $out .= "\n".'endobj';
11707 $this->_out($out);
11708 // Font descriptor
11709 // A font descriptor describing the CIDFont default metrics other than its glyph widths
11710 $this->_newobj();
11711 $out = '<< /Type /FontDescriptor';
11712 $out .= ' /FontName /'.$fontname;
11713 foreach ($font['desc'] as $key => $value) {
11714 if (is_float($value)) {
11715 $value = sprintf('%.3F', $value);
11717 $out .= ' /'.$key.' '.$value;
11719 $fontdir = false;
11720 if (!$this->empty_string($font['file'])) {
11721 // A stream containing a TrueType font
11722 $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
11723 $fontdir = $this->FontFiles[$font['file']]['fontdir'];
11725 $out .= ' >>';
11726 $out .= "\n".'endobj';
11727 $this->_out($out);
11728 if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
11729 $this->_newobj();
11730 // Embed CIDToGIDMap
11731 // A specification of the mapping from CIDs to glyph indices
11732 // search and get CTG font file to embedd
11733 $ctgfile = strtolower($font['ctg']);
11734 // search and get ctg font file to embedd
11735 $fontfile = '';
11736 // search files on various directories
11737 if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
11738 $fontfile = $fontdir.$ctgfile;
11739 } elseif (file_exists($this->_getfontpath().$ctgfile)) {
11740 $fontfile = $this->_getfontpath().$ctgfile;
11741 } elseif (file_exists($ctgfile)) {
11742 $fontfile = $ctgfile;
11744 if ($this->empty_string($fontfile)) {
11745 $this->Error('Font file not found: '.$ctgfile);
11747 $stream = $this->_getrawstream(file_get_contents($fontfile));
11748 $out = '<< /Length '.strlen($stream).'';
11749 if (substr($fontfile, -2) == '.z') { // check file extension
11750 // Decompresses data encoded using the public-domain
11751 // zlib/deflate compression method, reproducing the
11752 // original text or binary data
11753 $out .= ' /Filter /FlateDecode';
11755 $out .= ' >>';
11756 $out .= ' stream'."\n".$stream."\n".'endstream';
11757 $out .= "\n".'endobj';
11758 $this->_out($out);
11763 * Output CID-0 fonts.
11764 * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
11765 * @param $font (array) font data
11766 * @protected
11767 * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
11768 * @since 3.2.000 (2008-06-23)
11770 protected function _putcidfont0($font) {
11771 $cidoffset = 0;
11772 if (!isset($font['cw'][1])) {
11773 $cidoffset = 31;
11775 if (isset($font['cidinfo']['uni2cid'])) {
11776 // convert unicode to cid.
11777 $uni2cid = $font['cidinfo']['uni2cid'];
11778 $cw = array();
11779 foreach ($font['cw'] as $uni => $width) {
11780 if (isset($uni2cid[$uni])) {
11781 $cw[($uni2cid[$uni] + $cidoffset)] = $width;
11782 } elseif ($uni < 256) {
11783 $cw[$uni] = $width;
11784 } // else unknown character
11786 $font = array_merge($font, array('cw' => $cw));
11788 $name = $font['name'];
11789 $enc = $font['enc'];
11790 if ($enc) {
11791 $longname = $name.'-'.$enc;
11792 } else {
11793 $longname = $name;
11795 $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
11796 $out .= '<</Type /Font';
11797 $out .= ' /Subtype /Type0';
11798 $out .= ' /BaseFont /'.$longname;
11799 $out .= ' /Name /F'.$font['i'];
11800 if ($enc) {
11801 $out .= ' /Encoding /'.$enc;
11803 $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
11804 $out .= ' >>';
11805 $out .= "\n".'endobj';
11806 $this->_out($out);
11807 $oid = $this->_newobj();
11808 $out = '<</Type /Font';
11809 $out .= ' /Subtype /CIDFontType0';
11810 $out .= ' /BaseFont /'.$name;
11811 $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
11812 $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
11813 $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
11814 $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
11815 $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
11816 $out .= ' /DW '.$font['dw'];
11817 $out .= "\n".$this->_putfontwidths($font, $cidoffset);
11818 $out .= ' >>';
11819 $out .= "\n".'endobj';
11820 $this->_out($out);
11821 $this->_newobj();
11822 $s = '<</Type /FontDescriptor /FontName /'.$name;
11823 foreach ($font['desc'] as $k => $v) {
11824 if ($k != 'Style') {
11825 if (is_float($v)) {
11826 $v = sprintf('%.3F', $v);
11828 $s .= ' /'.$k.' '.$v.'';
11831 $s .= '>>';
11832 $s .= "\n".'endobj';
11833 $this->_out($s);
11837 * Output images.
11838 * @protected
11840 protected function _putimages() {
11841 $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
11842 foreach ($this->imagekeys as $file) {
11843 $info = $this->getImageBuffer($file);
11844 // set object for alternate images array
11845 if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
11846 $altoid = $this->_newobj();
11847 $out = '[';
11848 foreach ($info['altimgs'] as $altimage) {
11849 if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
11850 $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
11851 $out .= ' /DefaultForPrinting';
11852 if ($altimage[1] === true) {
11853 $out .= ' true';
11854 } else {
11855 $out .= ' false';
11857 $out .= ' >>';
11860 $out .= ' ]';
11861 $out .= "\n".'endobj';
11862 $this->_out($out);
11864 // set image object
11865 $oid = $this->_newobj();
11866 $this->xobjects['I'.$info['i']] = array('n' => $oid);
11867 $this->setImageSubBuffer($file, 'n', $this->n);
11868 $out = '<</Type /XObject';
11869 $out .= ' /Subtype /Image';
11870 $out .= ' /Width '.$info['w'];
11871 $out .= ' /Height '.$info['h'];
11872 if (array_key_exists('masked', $info)) {
11873 $out .= ' /SMask '.($this->n - 1).' 0 R';
11875 // set color space
11876 $icc = false;
11877 if (isset($info['icc']) AND ($info['icc'] !== false)) {
11878 // ICC Colour Space
11879 $icc = true;
11880 $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
11881 } elseif ($info['cs'] == 'Indexed') {
11882 // Indexed Colour Space
11883 $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
11884 } else {
11885 // Device Colour Space
11886 $out .= ' /ColorSpace /'.$info['cs'];
11888 if ($info['cs'] == 'DeviceCMYK') {
11889 $out .= ' /Decode [1 0 1 0 1 0 1 0]';
11891 $out .= ' /BitsPerComponent '.$info['bpc'];
11892 if (isset($altoid) AND ($altoid > 0)) {
11893 // reference to alternate images dictionary
11894 $out .= ' /Alternates '.$altoid.' 0 R';
11896 if (isset($info['exurl']) AND !empty($info['exurl'])) {
11897 // external stream
11898 $out .= ' /Length 0';
11899 $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
11900 if (isset($info['f'])) {
11901 $out .= ' /FFilter /'.$info['f'];
11903 $out .= ' >>';
11904 $out .= ' stream'."\n".'endstream';
11905 } else {
11906 if (isset($info['f'])) {
11907 $out .= ' /Filter /'.$info['f'];
11909 if (isset($info['parms'])) {
11910 $out .= ' '.$info['parms'];
11912 if (isset($info['trns']) AND is_array($info['trns'])) {
11913 $trns = '';
11914 $count_info = count($info['trns']);
11915 for ($i=0; $i < $count_info; ++$i) {
11916 $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
11918 $out .= ' /Mask ['.$trns.']';
11920 $stream = $this->_getrawstream($info['data']);
11921 $out .= ' /Length '.strlen($stream).' >>';
11922 $out .= ' stream'."\n".$stream."\n".'endstream';
11924 $out .= "\n".'endobj';
11925 $this->_out($out);
11926 if ($icc) {
11927 // ICC colour profile
11928 $this->_newobj();
11929 $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
11930 $icc = $this->_getrawstream($icc);
11931 $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
11932 } elseif ($info['cs'] == 'Indexed') {
11933 // colour palette
11934 $this->_newobj();
11935 $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
11936 $pal = $this->_getrawstream($pal);
11937 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
11943 * Output Form XObjects Templates.
11944 * @author Nicola Asuni
11945 * @since 5.8.017 (2010-08-24)
11946 * @protected
11947 * @see startTemplate(), endTemplate(), printTemplate()
11949 protected function _putxobjects() {
11950 foreach ($this->xobjects as $key => $data) {
11951 if (isset($data['outdata'])) {
11952 $stream = trim($data['outdata']);
11953 $out = $this->_getobj($data['n'])."\n";
11954 $out .= '<<';
11955 $out .= ' /Type /XObject';
11956 $out .= ' /Subtype /Form';
11957 $out .= ' /FormType 1';
11958 if ($this->compress) {
11959 $stream = gzcompress($stream);
11960 $out .= ' /Filter /FlateDecode';
11962 $out .= sprintf(' /BBox [%.2F %.2F %.2F %.2F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
11963 $out .= ' /Matrix [1 0 0 1 0 0]';
11964 $out .= ' /Resources <<';
11965 $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
11966 if (!$this->pdfa_mode) {
11967 // transparency
11968 if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
11969 $out .= ' /ExtGState <<';
11970 foreach ($data['extgstates'] as $k => $extgstate) {
11971 if (isset($this->extgstates[$k]['name'])) {
11972 $out .= ' /'.$this->extgstates[$k]['name'];
11973 } else {
11974 $out .= ' /GS'.$k;
11976 $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
11978 $out .= ' >>';
11980 if (isset($data['gradients']) AND !empty($data['gradients'])) {
11981 $gp = '';
11982 $gs = '';
11983 foreach ($data['gradients'] as $id => $grad) {
11984 // gradient patterns
11985 $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
11986 // gradient shadings
11987 $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
11989 $out .= ' /Pattern <<'.$gp.' >>';
11990 $out .= ' /Shading <<'.$gs.' >>';
11993 // spot colors
11994 if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
11995 $out .= ' /ColorSpace <<';
11996 foreach ($data['spot_colors'] as $name => $color) {
11997 $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
11999 $out .= ' >>';
12001 // fonts
12002 if (!empty($data['fonts'])) {
12003 $out .= ' /Font <<';
12004 foreach ($data['fonts'] as $fontkey => $fontid) {
12005 $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
12007 $out .= ' >>';
12009 // images or nested xobjects
12010 if (!empty($data['images']) OR !empty($data['xobjects'])) {
12011 $out .= ' /XObject <<';
12012 foreach ($data['images'] as $imgid) {
12013 $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
12015 foreach ($data['xobjects'] as $sub_id => $sub_objid) {
12016 $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
12018 $out .= ' >>';
12020 $out .= ' >>'; //end resources
12021 if (isset($data['group']) AND ($data['group'] !== false)) {
12022 // set transparency group
12023 $out .= ' /Group << /Type /Group /S /Transparency';
12024 if (is_array($data['group'])) {
12025 if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
12026 $out .= ' /CS /'.$data['group']['CS'];
12028 if (isset($data['group']['I'])) {
12029 $out .= ' /I /'.($data['group']['I']===true?'true':'false');
12031 if (isset($data['group']['K'])) {
12032 $out .= ' /K /'.($data['group']['K']===true?'true':'false');
12035 $out .= ' >>';
12037 $stream = $this->_getrawstream($stream, $data['n']);
12038 $out .= ' /Length '.strlen($stream);
12039 $out .= ' >>';
12040 $out .= ' stream'."\n".$stream."\n".'endstream';
12041 $out .= "\n".'endobj';
12042 $this->_out($out);
12048 * Output Spot Colors Resources.
12049 * @protected
12050 * @since 4.0.024 (2008-09-12)
12052 protected function _putspotcolors() {
12053 foreach ($this->spot_colors as $name => $color) {
12054 $this->_newobj();
12055 $this->spot_colors[$name]['n'] = $this->n;
12056 $out = '[/Separation /'.str_replace(' ', '#20', $name);
12057 $out .= ' /DeviceCMYK <<';
12058 $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
12059 $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['c'] / 100), ($color['m'] / 100), ($color['y'] / 100), ($color['k'] / 100));
12060 $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
12061 $out .= "\n".'endobj';
12062 $this->_out($out);
12067 * Return XObjects Dictionary.
12068 * @return string XObjects dictionary
12069 * @protected
12070 * @since 5.8.014 (2010-08-23)
12072 protected function _getxobjectdict() {
12073 $out = '';
12074 foreach ($this->xobjects as $id => $objid) {
12075 $out .= ' /'.$id.' '.$objid['n'].' 0 R';
12077 return $out;
12081 * Output Resources Dictionary.
12082 * @protected
12084 protected function _putresourcedict() {
12085 $out = $this->_getobj(2)."\n";
12086 $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
12087 $out .= ' /Font <<';
12088 foreach ($this->fontkeys as $fontkey) {
12089 $font = $this->getFontBuffer($fontkey);
12090 $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
12092 $out .= ' >>';
12093 $out .= ' /XObject <<';
12094 $out .= $this->_getxobjectdict();
12095 $out .= ' >>';
12096 // layers
12097 if (!empty($this->pdflayers)) {
12098 $out .= ' /Properties <<';
12099 foreach ($this->pdflayers as $layer) {
12100 $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
12102 $out .= ' >>';
12104 if (!$this->pdfa_mode) {
12105 // transparency
12106 if (isset($this->extgstates) AND !empty($this->extgstates)) {
12107 $out .= ' /ExtGState <<';
12108 foreach ($this->extgstates as $k => $extgstate) {
12109 if (isset($extgstate['name'])) {
12110 $out .= ' /'.$extgstate['name'];
12111 } else {
12112 $out .= ' /GS'.$k;
12114 $out .= ' '.$extgstate['n'].' 0 R';
12116 $out .= ' >>';
12118 if (isset($this->gradients) AND !empty($this->gradients)) {
12119 $gp = '';
12120 $gs = '';
12121 foreach ($this->gradients as $id => $grad) {
12122 // gradient patterns
12123 $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
12124 // gradient shadings
12125 $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
12127 $out .= ' /Pattern <<'.$gp.' >>';
12128 $out .= ' /Shading <<'.$gs.' >>';
12131 // spot colors
12132 if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
12133 $out .= ' /ColorSpace <<';
12134 foreach ($this->spot_colors as $color) {
12135 $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
12137 $out .= ' >>';
12139 $out .= ' >>';
12140 $out .= "\n".'endobj';
12141 $this->_out($out);
12145 * Output Resources.
12146 * @protected
12148 protected function _putresources() {
12149 $this->_putextgstates();
12150 $this->_putocg();
12151 $this->_putfonts();
12152 $this->_putimages();
12153 $this->_putspotcolors();
12154 $this->_putshaders();
12155 $this->_putxobjects();
12156 $this->_putresourcedict();
12157 $this->_putdests();
12158 $this->_putbookmarks();
12159 $this->_putEmbeddedFiles();
12160 $this->_putannotsobjs();
12161 $this->_putjavascript();
12162 $this->_putencryption();
12166 * Adds some Metadata information (Document Information Dictionary)
12167 * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
12168 * @return int object id
12169 * @protected
12171 protected function _putinfo() {
12172 $oid = $this->_newobj();
12173 $out = '<<';
12174 // store current isunicode value
12175 $prev_isunicode = $this->isunicode;
12176 if ($this->docinfounicode) {
12177 $this->isunicode = true;
12179 if (!$this->empty_string($this->title)) {
12180 // The document's title.
12181 $out .= ' /Title '.$this->_textstring($this->title, $oid);
12183 if (!$this->empty_string($this->author)) {
12184 // The name of the person who created the document.
12185 $out .= ' /Author '.$this->_textstring($this->author, $oid);
12187 if (!$this->empty_string($this->subject)) {
12188 // The subject of the document.
12189 $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
12191 if (!$this->empty_string($this->keywords)) {
12192 // Keywords associated with the document.
12193 $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
12195 if (!$this->empty_string($this->creator)) {
12196 // If the document was converted to PDF from another format, the name of the conforming product that created the original document from which it was converted.
12197 $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
12199 // restore previous isunicode value
12200 $this->isunicode = $prev_isunicode;
12201 // default producer
12202 $out .= ' /Producer '.$this->_textstring("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29", $oid);
12203 // The date and time the document was created, in human-readable form
12204 $out .= ' /CreationDate '.$this->_datestring();
12205 // The date and time the document was most recently modified, in human-readable form
12206 $out .= ' /ModDate '.$this->_datestring();
12207 // A name object indicating whether the document has been modified to include trapping information
12208 $out .= ' /Trapped /False';
12209 $out .= ' >>';
12210 $out .= "\n".'endobj';
12211 $this->_out($out);
12212 return $oid;
12216 * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
12217 * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
12218 * @param $xmp (string) Custom XMP data.
12219 * @since 5.9.128 (2011-10-06)
12220 * @public
12222 public function setExtraXMP($xmp) {
12223 $this->custom_xmp = $xmp;
12227 * Put XMP data object and return ID.
12228 * @return (int) The object ID.
12229 * @since 5.9.121 (2011-09-28)
12230 * @protected
12232 protected function _putXMP() {
12233 $oid = $this->_newobj();
12234 // store current isunicode value
12235 $prev_isunicode = $this->isunicode;
12236 $this->isunicode = true;
12237 $prev_encrypted = $this->encrypted;
12238 $this->encrypted = false;
12239 // set XMP data
12240 $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
12241 $xmp .= '<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04">'."\n";
12242 $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
12243 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
12244 $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
12245 $xmp .= "\t\t\t".'<dc:title>'."\n";
12246 $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
12247 $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
12248 $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
12249 $xmp .= "\t\t\t".'</dc:title>'."\n";
12250 $xmp .= "\t\t\t".'<dc:creator>'."\n";
12251 $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
12252 $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
12253 $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
12254 $xmp .= "\t\t\t".'</dc:creator>'."\n";
12255 $xmp .= "\t\t\t".'<dc:description>'."\n";
12256 $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
12257 $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
12258 $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
12259 $xmp .= "\t\t\t".'</dc:description>'."\n";
12260 $xmp .= "\t\t\t".'<dc:subject>'."\n";
12261 $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
12262 $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
12263 $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
12264 $xmp .= "\t\t\t".'</dc:subject>'."\n";
12265 $xmp .= "\t\t".'</rdf:Description>'."\n";
12266 // convert date format
12267 $docdate = substr($this->doc_date, 0, 4).'-'.substr($this->doc_date, 4, 2).'-'.substr($this->doc_date, 6, 2);
12268 $docdate .= 'T'.substr($this->doc_date, 8, 2).':'.substr($this->doc_date, 10, 2).':'.substr($this->doc_date, 12, 2);
12269 $docdate .= '+'.substr($this->doc_date, 15, 2).':'.substr($this->doc_date, 18, 2);
12270 $docdate = $this->_escapeXML($docdate);
12271 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
12272 $xmp .= "\t\t\t".'<xmp:CreateDate>'.$docdate.'</xmp:CreateDate>'."\n";
12273 $xmp .= "\t\t\t".'<xmp:CreatorTool>TCPDF</xmp:CreatorTool>'."\n";
12274 $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docdate.'</xmp:ModifyDate>'."\n";
12275 $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$docdate.'</xmp:MetadataDate>'."\n";
12276 $xmp .= "\t\t".'</rdf:Description>'."\n";
12277 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
12278 $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
12279 $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29").'</pdf:Producer>'."\n";
12280 $xmp .= "\t\t".'</rdf:Description>'."\n";
12281 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
12282 $uuid = 'uuid:'.substr($this->file_id, 0, 8).'-'.substr($this->file_id, 8, 4).'-'.substr($this->file_id, 12, 4).'-'.substr($this->file_id, 16, 4).'-'.substr($this->file_id, 20, 12);
12283 $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
12284 $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
12285 $xmp .= "\t\t".'</rdf:Description>'."\n";
12286 if ($this->pdfa_mode) {
12287 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
12288 $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
12289 $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
12290 $xmp .= "\t\t".'</rdf:Description>'."\n";
12292 // XMP extension schemas
12293 $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">'."\n";
12294 $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
12295 $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
12296 $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12297 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
12298 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
12299 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
12300 $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
12301 $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12302 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
12303 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
12304 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
12305 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
12306 $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
12307 $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12308 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
12309 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
12310 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
12311 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
12312 $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
12313 $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
12314 $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
12315 $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
12316 $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12317 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
12318 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
12319 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
12320 $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
12321 $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
12322 $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12323 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
12324 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
12325 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
12326 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
12327 $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
12328 $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12329 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
12330 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
12331 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
12332 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
12333 $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
12334 $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
12335 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
12336 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
12337 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
12338 $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
12339 $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
12340 $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
12341 $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
12342 $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
12343 $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
12344 $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
12345 $xmp .= "\t\t".'</rdf:Description>'."\n";
12346 $xmp .= "\t".'</rdf:RDF>'."\n";
12347 $xmp .= $this->custom_xmp;
12348 $xmp .= '</x:xmpmeta>'."\n";
12349 $xmp .= '<?xpacket end="w"?>';
12350 $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
12351 // restore previous isunicode value
12352 $this->isunicode = $prev_isunicode;
12353 $this->encrypted = $prev_encrypted;
12354 $this->_out($out);
12355 return $oid;
12359 * Output Catalog.
12360 * @return int object id
12361 * @protected
12363 protected function _putcatalog() {
12364 // put XMP
12365 $xmpobj = $this->_putXMP();
12366 // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
12367 if ($this->pdfa_mode OR $this->force_srgb) {
12368 $iccobj = $this->_newobj();
12369 $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
12370 $filter = '';
12371 if ($this->compress) {
12372 $filter = ' /Filter /FlateDecode';
12373 $icc = gzcompress($icc);
12375 $icc = $this->_getrawstream($icc);
12376 $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
12378 // start catalog
12379 $oid = $this->_newobj();
12380 $out = '<< /Type /Catalog';
12381 $out .= ' /Version /'.$this->PDFVersion;
12382 //$out .= ' /Extensions <<>>';
12383 $out .= ' /Pages 1 0 R';
12384 //$out .= ' /PageLabels ' //...;
12385 $out .= ' /Names <<';
12386 if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
12387 $out .= ' /JavaScript '.($this->n_js).' 0 R';
12389 $out .= ' >>';
12390 if (!empty($this->dests)) {
12391 $out .= ' /Dests '.$this->n_dests.' 0 R';
12393 $out .= $this->_putviewerpreferences();
12394 if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
12395 $out .= ' /PageLayout /'.$this->LayoutMode;
12397 if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
12398 $out .= ' /PageMode /'.$this->PageMode;
12400 if (count($this->outlines) > 0) {
12401 $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
12402 $out .= ' /PageMode /UseOutlines';
12404 //$out .= ' /Threads []';
12405 if ($this->ZoomMode == 'fullpage') {
12406 $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
12407 } elseif ($this->ZoomMode == 'fullwidth') {
12408 $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
12409 } elseif ($this->ZoomMode == 'real') {
12410 $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
12411 } elseif (!is_string($this->ZoomMode)) {
12412 $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
12414 //$out .= ' /AA <<>>';
12415 //$out .= ' /URI <<>>';
12416 $out .= ' /Metadata '.$xmpobj.' 0 R';
12417 //$out .= ' /StructTreeRoot <<>>';
12418 //$out .= ' /MarkInfo <<>>';
12419 if (isset($this->l['a_meta_language'])) {
12420 $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
12422 //$out .= ' /SpiderInfo <<>>';
12423 // set OutputIntent to sRGB IEC61966-2.1 if required
12424 if ($this->pdfa_mode OR $this->force_srgb) {
12425 $out .= ' /OutputIntents [<<';
12426 $out .= ' /Type /OutputIntent';
12427 $out .= ' /S /GTS_PDFA1';
12428 $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
12429 $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
12430 $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
12431 $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
12432 $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
12433 $out .= ' >>]';
12435 //$out .= ' /PieceInfo <<>>';
12436 if (!empty($this->pdflayers)) {
12437 $lyrobjs = '';
12438 $lyrobjs_print = '';
12439 $lyrobjs_view = '';
12440 foreach ($this->pdflayers as $layer) {
12441 $lyrobjs .= ' '.$layer['objid'].' 0 R';
12442 if ($layer['print']) {
12443 $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
12445 if ($layer['view']) {
12446 $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
12449 $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
12450 $out .= ' /D <<';
12451 $out .= ' /Name '.$this->_textstring('Layers', $oid);
12452 $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
12453 $out .= ' /BaseState /ON';
12454 $out .= ' /ON ['.$lyrobjs_print.']';
12455 $out .= ' /OFF ['.$lyrobjs_view.']';
12456 $out .= ' /Intent /View';
12457 $out .= ' /AS [';
12458 $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
12459 $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
12460 $out .= ' ]';
12461 $out .= ' /Order ['.$lyrobjs.']';
12462 $out .= ' /ListMode /AllPages';
12463 //$out .= ' /RBGroups ['..']';
12464 //$out .= ' /Locked ['..']';
12465 $out .= ' >>';
12466 $out .= ' >>';
12468 // AcroForm
12469 if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
12470 $out .= ' /AcroForm <<';
12471 $objrefs = '';
12472 if ($this->sign AND isset($this->signature_data['cert_type'])) {
12473 // set reference for signature object
12474 $objrefs .= $this->sig_obj_id.' 0 R';
12476 if (!empty($this->empty_signature_appearance)) {
12477 foreach ($this->empty_signature_appearance as $esa) {
12478 // set reference for empty signature objects
12479 $objrefs .= ' '.$esa['objid'].' 0 R';
12482 if (!empty($this->form_obj_id)) {
12483 foreach($this->form_obj_id as $objid) {
12484 $objrefs .= ' '.$objid.' 0 R';
12487 $out .= ' /Fields ['.$objrefs.']';
12488 // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
12489 $out .= ' /NeedAppearances false';
12490 if ($this->sign AND isset($this->signature_data['cert_type'])) {
12491 if ($this->signature_data['cert_type'] > 0) {
12492 $out .= ' /SigFlags 3';
12493 } else {
12494 $out .= ' /SigFlags 1';
12497 //$out .= ' /CO ';
12498 if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
12499 $out .= ' /DR <<';
12500 $out .= ' /Font <<';
12501 foreach ($this->annotation_fonts as $fontkey => $fontid) {
12502 $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
12504 $out .= ' >> >>';
12506 $font = $this->getFontBuffer('helvetica');
12507 $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
12508 $out .= ' /Q '.(($this->rtl)?'2':'0');
12509 //$out .= ' /XFA ';
12510 $out .= ' >>';
12511 // signatures
12512 if ($this->sign AND isset($this->signature_data['cert_type'])) {
12513 if ($this->signature_data['cert_type'] > 0) {
12514 $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
12515 } else {
12516 $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
12520 //$out .= ' /Legal <<>>';
12521 //$out .= ' /Requirements []';
12522 //$out .= ' /Collection <<>>';
12523 //$out .= ' /NeedsRendering true';
12524 $out .= ' >>';
12525 $out .= "\n".'endobj';
12526 $this->_out($out);
12527 return $oid;
12531 * Output viewer preferences.
12532 * @return string for viewer preferences
12533 * @author Nicola asuni
12534 * @since 3.1.000 (2008-06-09)
12535 * @protected
12537 protected function _putviewerpreferences() {
12538 $out = ' /ViewerPreferences <<';
12539 if ($this->rtl) {
12540 $out .= ' /Direction /R2L';
12541 } else {
12542 $out .= ' /Direction /L2R';
12544 if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
12545 $out .= ' /HideToolbar true';
12547 if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
12548 $out .= ' /HideMenubar true';
12550 if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
12551 $out .= ' /HideWindowUI true';
12553 if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
12554 $out .= ' /FitWindow true';
12556 if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
12557 $out .= ' /CenterWindow true';
12559 if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
12560 $out .= ' /DisplayDocTitle true';
12562 if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
12563 $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
12565 if (isset($this->viewer_preferences['ViewArea'])) {
12566 $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
12568 if (isset($this->viewer_preferences['ViewClip'])) {
12569 $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
12571 if (isset($this->viewer_preferences['PrintArea'])) {
12572 $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
12574 if (isset($this->viewer_preferences['PrintClip'])) {
12575 $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
12577 if (isset($this->viewer_preferences['PrintScaling'])) {
12578 $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
12580 if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
12581 $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
12583 if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
12584 if ($this->viewer_preferences['PickTrayByPDFSize']) {
12585 $out .= ' /PickTrayByPDFSize true';
12586 } else {
12587 $out .= ' /PickTrayByPDFSize false';
12590 if (isset($this->viewer_preferences['PrintPageRange'])) {
12591 $PrintPageRangeNum = '';
12592 foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
12593 $PrintPageRangeNum .= ' '.($v - 1).'';
12595 $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
12597 if (isset($this->viewer_preferences['NumCopies'])) {
12598 $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
12600 $out .= ' >>';
12601 return $out;
12605 * Output PDF File Header (7.5.2).
12606 * @protected
12608 protected function _putheader() {
12609 $this->_out('%PDF-'.$this->PDFVersion);
12610 $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
12614 * Output end of document (EOF).
12615 * @protected
12617 protected function _enddoc() {
12618 $this->state = 1;
12619 $this->_putheader();
12620 $this->_putpages();
12621 $this->_putresources();
12622 // empty signature fields
12623 if (!empty($this->empty_signature_appearance)) {
12624 foreach ($this->empty_signature_appearance as $key => $esa) {
12625 // widget annotation for empty signature
12626 $out = $this->_getobj($esa['objid'])."\n";
12627 $out .= '<< /Type /Annot';
12628 $out .= ' /Subtype /Widget';
12629 $out .= ' /Rect ['.$esa['rect'].']';
12630 $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
12631 $out .= ' /F 4';
12632 $out .= ' /FT /Sig';
12633 $signame = sprintf('Signature_%03d', ($key + 1));
12634 $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
12635 $out .= ' /Ff 0';
12636 $out .= ' >>';
12637 $out .= "\n".'endobj';
12638 $this->_out($out);
12641 // Signature
12642 if ($this->sign AND isset($this->signature_data['cert_type'])) {
12643 // widget annotation for signature
12644 $out = $this->_getobj($this->sig_obj_id)."\n";
12645 $out .= '<< /Type /Annot';
12646 $out .= ' /Subtype /Widget';
12647 $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
12648 $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
12649 $out .= ' /F 4';
12650 $out .= ' /FT /Sig';
12651 $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
12652 $out .= ' /Ff 0';
12653 $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
12654 $out .= ' >>';
12655 $out .= "\n".'endobj';
12656 $this->_out($out);
12657 // signature
12658 $this->_putsignature();
12660 // Info
12661 $objid_info = $this->_putinfo();
12662 // Catalog
12663 $objid_catalog = $this->_putcatalog();
12664 // Cross-ref
12665 $o = $this->bufferlen;
12666 // XREF section
12667 $this->_out('xref');
12668 $this->_out('0 '.($this->n + 1));
12669 $this->_out('0000000000 65535 f ');
12670 for ($i=1; $i <= $this->n; ++$i) {
12671 if (!isset($this->offsets[$i]) AND ($i > 1)) {
12672 $this->offsets[$i] = $this->offsets[($i - 1)];
12674 $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
12676 // TRAILER
12677 $out = 'trailer'."\n";
12678 $out .= '<<';
12679 $out .= ' /Size '.($this->n + 1);
12680 $out .= ' /Root '.$objid_catalog.' 0 R';
12681 $out .= ' /Info '.$objid_info.' 0 R';
12682 if ($this->encrypted) {
12683 $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
12685 $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
12686 $out .= ' >>';
12687 $this->_out($out);
12688 $this->_out('startxref');
12689 $this->_out($o);
12690 $this->_out('%%EOF');
12691 $this->state = 3; // end-of-doc
12692 if ($this->diskcache) {
12693 // remove temporary files used for images
12694 foreach ($this->imagekeys as $key) {
12695 // remove temporary files
12696 unlink($this->images[$key]);
12698 foreach ($this->fontkeys as $key) {
12699 // remove temporary files
12700 unlink($this->fonts[$key]);
12706 * Initialize a new page.
12707 * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
12708 * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
12709 * @protected
12710 * @see getPageSizeFromFormat(), setPageFormat()
12712 protected function _beginpage($orientation='', $format='') {
12713 ++$this->page;
12714 $this->setPageBuffer($this->page, '');
12715 // initialize array for graphics tranformation positions inside a page buffer
12716 $this->transfmrk[$this->page] = array();
12717 $this->state = 2;
12718 if ($this->empty_string($orientation)) {
12719 if (isset($this->CurOrientation)) {
12720 $orientation = $this->CurOrientation;
12721 } elseif ($this->fwPt > $this->fhPt) {
12722 // landscape
12723 $orientation = 'L';
12724 } else {
12725 // portrait
12726 $orientation = 'P';
12729 if ($this->empty_string($format)) {
12730 $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
12731 $this->setPageOrientation($orientation);
12732 } else {
12733 $this->setPageFormat($format, $orientation);
12735 if ($this->rtl) {
12736 $this->x = $this->w - $this->rMargin;
12737 } else {
12738 $this->x = $this->lMargin;
12740 $this->y = $this->tMargin;
12741 if (isset($this->newpagegroup[$this->page])) {
12742 // start a new group
12743 $this->currpagegroup = $this->newpagegroup[$this->page];
12744 $this->pagegroups[$this->currpagegroup] = 1;
12745 } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
12746 ++$this->pagegroups[$this->currpagegroup];
12751 * Mark end of page.
12752 * @protected
12754 protected function _endpage() {
12755 $this->setVisibility('all');
12756 $this->state = 1;
12760 * Begin a new object and return the object number.
12761 * @return int object number
12762 * @protected
12764 protected function _newobj() {
12765 $this->_out($this->_getobj());
12766 return $this->n;
12770 * Return the starting object string for the selected object ID.
12771 * @param $objid (int) Object ID (leave empty to get a new ID).
12772 * @return string the starting object string
12773 * @protected
12774 * @since 5.8.009 (2010-08-20)
12776 protected function _getobj($objid='') {
12777 if ($objid === '') {
12778 ++$this->n;
12779 $objid = $this->n;
12781 $this->offsets[$objid] = $this->bufferlen;
12782 return $objid.' 0 obj';
12786 * Underline text.
12787 * @param $x (int) X coordinate
12788 * @param $y (int) Y coordinate
12789 * @param $txt (string) text to underline
12790 * @protected
12792 protected function _dounderline($x, $y, $txt) {
12793 $w = $this->GetStringWidth($txt);
12794 return $this->_dounderlinew($x, $y, $w);
12798 * Underline for rectangular text area.
12799 * @param $x (int) X coordinate
12800 * @param $y (int) Y coordinate
12801 * @param $w (int) width to underline
12802 * @protected
12803 * @since 4.8.008 (2009-09-29)
12805 protected function _dounderlinew($x, $y, $w) {
12806 $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
12807 return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
12811 * Line through text.
12812 * @param $x (int) X coordinate
12813 * @param $y (int) Y coordinate
12814 * @param $txt (string) text to linethrough
12815 * @protected
12817 protected function _dolinethrough($x, $y, $txt) {
12818 $w = $this->GetStringWidth($txt);
12819 return $this->_dolinethroughw($x, $y, $w);
12823 * Line through for rectangular text area.
12824 * @param $x (int) X coordinate
12825 * @param $y (int) Y coordinate
12826 * @param $w (int) line length (width)
12827 * @protected
12828 * @since 4.9.008 (2009-09-29)
12830 protected function _dolinethroughw($x, $y, $w) {
12831 $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
12832 return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
12836 * Overline text.
12837 * @param $x (int) X coordinate
12838 * @param $y (int) Y coordinate
12839 * @param $txt (string) text to overline
12840 * @protected
12841 * @since 4.9.015 (2010-04-19)
12843 protected function _dooverline($x, $y, $txt) {
12844 $w = $this->GetStringWidth($txt);
12845 return $this->_dooverlinew($x, $y, $w);
12849 * Overline for rectangular text area.
12850 * @param $x (int) X coordinate
12851 * @param $y (int) Y coordinate
12852 * @param $w (int) width to overline
12853 * @protected
12854 * @since 4.9.015 (2010-04-19)
12856 protected function _dooverlinew($x, $y, $w) {
12857 $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
12858 return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
12863 * Read a 4-byte (32 bit) integer from file.
12864 * @param $f (string) file name.
12865 * @return 4-byte integer
12866 * @protected
12868 protected function _freadint($f) {
12869 $a = unpack('Ni', fread($f, 4));
12870 return $a['i'];
12874 * Add "\" before "\", "(" and ")"
12875 * @param $s (string) string to escape.
12876 * @return string escaped string.
12877 * @protected
12879 protected function _escape($s) {
12880 // the chr(13) substitution fixes the Bugs item #1421290.
12881 return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
12885 * Format a data string for meta information
12886 * @param $s (string) data string to escape.
12887 * @param $n (int) object ID
12888 * @return string escaped string.
12889 * @protected
12891 protected function _datastring($s, $n=0) {
12892 if ($n == 0) {
12893 $n = $this->n;
12895 $s = $this->_encrypt_data($n, $s);
12896 return '('. $this->_escape($s).')';
12900 * Returns a formatted date for meta information
12901 * @param $n (int) object ID
12902 * @return string escaped date string.
12903 * @protected
12904 * @since 4.6.028 (2009-08-25)
12906 protected function _datestring($n=0) {
12907 return $this->_datastring('D:'.$this->doc_date, $n);
12911 * Format a text string for meta information
12912 * @param $s (string) string to escape.
12913 * @param $n (int) object ID
12914 * @return string escaped string.
12915 * @protected
12917 protected function _textstring($s, $n=0) {
12918 if ($this->isunicode) {
12919 //Convert string to UTF-16BE
12920 $s = $this->UTF8ToUTF16BE($s, true);
12922 return $this->_datastring($s, $n);
12926 * THIS METHOD IS DEPRECATED
12927 * Format a text string
12928 * @param $s (string) string to escape.
12929 * @return string escaped string.
12930 * @protected
12931 * @deprecated
12933 protected function _escapetext($s) {
12934 if ($this->isunicode) {
12935 if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
12936 $s = $this->UTF8ToLatin1($s);
12937 } else {
12938 //Convert string to UTF-16BE and reverse RTL language
12939 $s = $this->utf8StrRev($s, false, $this->tmprtl);
12942 return $this->_escape($s);
12946 * Escape some special characters (&lt; &gt; &amp;) for XML output.
12947 * @param $str (string) Input string to convert.
12948 * @return converted string
12949 * @since 5.9.121 (2011-09-28)
12950 * @protected
12952 protected function _escapeXML($str) {
12953 $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
12954 $str = strtr($str, $replaceTable);
12955 return $str;
12959 * get raw output stream.
12960 * @param $s (string) string to output.
12961 * @param $n (int) object reference for encryption mode
12962 * @protected
12963 * @author Nicola Asuni
12964 * @since 5.5.000 (2010-06-22)
12966 protected function _getrawstream($s, $n=0) {
12967 if ($n <= 0) {
12968 // default to current object
12969 $n = $this->n;
12971 return $this->_encrypt_data($n, $s);
12975 * Format output stream (DEPRECATED).
12976 * @param $s (string) string to output.
12977 * @param $n (int) object reference for encryption mode
12978 * @protected
12979 * @deprecated
12981 protected function _getstream($s, $n=0) {
12982 return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
12986 * Output a stream (DEPRECATED).
12987 * @param $s (string) string to output.
12988 * @param $n (int) object reference for encryption mode
12989 * @protected
12990 * @deprecated
12992 protected function _putstream($s, $n=0) {
12993 $this->_out($this->_getstream($s, $n));
12997 * Output a string to the document.
12998 * @param $s (string) string to output.
12999 * @protected
13001 protected function _out($s) {
13002 if ($this->state == 2) {
13003 if ($this->inxobj) {
13004 // we are inside an XObject template
13005 $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
13006 } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
13007 // puts data before page footer
13008 $pagebuff = $this->getPageBuffer($this->page);
13009 $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
13010 $footer = substr($pagebuff, -$this->footerlen[$this->page]);
13011 $this->setPageBuffer($this->page, $page.$s."\n".$footer);
13012 // update footer position
13013 $this->footerpos[$this->page] += strlen($s."\n");
13014 } else {
13015 $this->setPageBuffer($this->page, $s."\n", true);
13017 } else {
13018 $this->setBuffer($s."\n");
13023 * Converts UTF-8 strings to codepoints array.<br>
13024 * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
13025 * Based on: http://www.faqs.org/rfcs/rfc3629.html
13026 * <pre>
13027 * Char. number range | UTF-8 octet sequence
13028 * (hexadecimal) | (binary)
13029 * --------------------+-----------------------------------------------
13030 * 0000 0000-0000 007F | 0xxxxxxx
13031 * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
13032 * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
13033 * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
13034 * ---------------------------------------------------------------------
13036 * ABFN notation:
13037 * ---------------------------------------------------------------------
13038 * UTF8-octets = *( UTF8-char )
13039 * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
13040 * UTF8-1 = %x00-7F
13041 * UTF8-2 = %xC2-DF UTF8-tail
13043 * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
13044 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
13045 * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
13046 * %xF4 %x80-8F 2( UTF8-tail )
13047 * UTF8-tail = %x80-BF
13048 * ---------------------------------------------------------------------
13049 * </pre>
13050 * @param $str (string) string to process.
13051 * @return array containing codepoints (UTF-8 characters values)
13052 * @protected
13053 * @author Nicola Asuni
13054 * @since 1.53.0.TC005 (2005-01-05)
13056 protected function UTF8StringToArray($str) {
13057 // build a unique string key
13058 $strkey = md5($str);
13059 if (isset($this->cache_UTF8StringToArray[$strkey])) {
13060 // return cached value
13061 $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
13062 if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
13063 if ($this->isunicode) {
13064 foreach ($chrarray as $chr) {
13065 // store this char for font subsetting
13066 $this->CurrentFont['subsetchars'][$chr] = true;
13068 // update font subsetchars
13069 $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
13071 $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
13073 return $chrarray;
13075 // check cache size
13076 if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
13077 // remove first element
13078 array_shift($this->cache_UTF8StringToArray);
13080 // new cache array for selected string
13081 $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
13082 ++$this->cache_size_UTF8StringToArray;
13083 if (!$this->isunicode) {
13084 // split string into array of equivalent codes
13085 $strarr = array();
13086 $strlen = strlen($str);
13087 for ($i=0; $i < $strlen; ++$i) {
13088 $strarr[] = ord($str{$i});
13090 // insert new value on cache
13091 $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
13092 $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
13093 return $strarr;
13095 $unichar = -1; // last unicode char
13096 $unicode = array(); // array containing unicode values
13097 $bytes = array(); // array containing single character byte sequences
13098 $numbytes = 1; // number of octetc needed to represent the UTF-8 character
13099 $str .= ''; // force $str to be a string
13100 $length = strlen($str);
13101 for ($i = 0; $i < $length; ++$i) {
13102 $char = ord($str{$i}); // get one string character at time
13103 if (count($bytes) == 0) { // get starting octect
13104 if ($char <= 0x7F) {
13105 $unichar = $char; // use the character "as is" because is ASCII
13106 $numbytes = 1;
13107 } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
13108 $bytes[] = ($char - 0xC0) << 0x06;
13109 $numbytes = 2;
13110 } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
13111 $bytes[] = ($char - 0xE0) << 0x0C;
13112 $numbytes = 3;
13113 } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
13114 $bytes[] = ($char - 0xF0) << 0x12;
13115 $numbytes = 4;
13116 } else {
13117 // use replacement character for other invalid sequences
13118 $unichar = 0xFFFD;
13119 $bytes = array();
13120 $numbytes = 1;
13122 } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
13123 $bytes[] = $char - 0x80;
13124 if (count($bytes) == $numbytes) {
13125 // compose UTF-8 bytes to a single unicode value
13126 $char = $bytes[0];
13127 for ($j = 1; $j < $numbytes; ++$j) {
13128 $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
13130 if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
13131 /* The definition of UTF-8 prohibits encoding character numbers between
13132 U+D800 and U+DFFF, which are reserved for use with the UTF-16
13133 encoding form (as surrogate pairs) and do not directly represent
13134 characters. */
13135 $unichar = 0xFFFD; // use replacement character
13136 } else {
13137 $unichar = $char; // add char to array
13139 // reset data for next char
13140 $bytes = array();
13141 $numbytes = 1;
13143 } else {
13144 // use replacement character for other invalid sequences
13145 $unichar = 0xFFFD;
13146 $bytes = array();
13147 $numbytes = 1;
13149 if ($unichar >= 0) {
13150 // insert unicode value into array
13151 $unicode[] = $unichar;
13152 // store this char for font subsetting
13153 $this->CurrentFont['subsetchars'][$unichar] = true;
13154 $unichar = -1;
13157 // update font subsetchars
13158 $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
13159 // insert new value on cache
13160 $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
13161 $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
13162 return $unicode;
13166 * Converts UTF-8 strings to UTF16-BE.<br>
13167 * @param $str (string) string to process.
13168 * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
13169 * @return string
13170 * @author Nicola Asuni
13171 * @since 1.53.0.TC005 (2005-01-05)
13172 * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
13173 * @protected
13175 protected function UTF8ToUTF16BE($str, $setbom=false) {
13176 if (!$this->isunicode) {
13177 return $str; // string is not in unicode
13179 $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
13180 return $this->arrUTF8ToUTF16BE($unicode, $setbom);
13184 * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
13185 * @param $str (string) string to process.
13186 * @return string
13187 * @author Andrew Whitehead, Nicola Asuni
13188 * @protected
13189 * @since 3.2.000 (2008-06-23)
13191 protected function UTF8ToLatin1($str) {
13192 if (!$this->isunicode) {
13193 return $str; // string is not in unicode
13195 $outstr = ''; // string to be returned
13196 $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
13197 foreach ($unicode as $char) {
13198 if ($char < 256) {
13199 $outstr .= chr($char);
13200 } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
13201 // map from UTF-8
13202 $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
13203 } elseif ($char == 0xFFFD) {
13204 // skip
13205 } else {
13206 $outstr .= '?';
13209 return $outstr;
13213 * Converts UTF-8 characters array to array of Latin1 characters<br>
13214 * @param $unicode (array) array containing UTF-8 unicode values
13215 * @return array
13216 * @author Nicola Asuni
13217 * @protected
13218 * @since 4.8.023 (2010-01-15)
13220 protected function UTF8ArrToLatin1($unicode) {
13221 if ((!$this->isunicode) OR $this->isUnicodeFont()) {
13222 return $unicode;
13224 $outarr = array(); // array to be returned
13225 foreach ($unicode as $char) {
13226 if ($char < 256) {
13227 $outarr[] = $char;
13228 } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
13229 // map from UTF-8
13230 $outarr[] = $this->unicode->uni_utf8tolatin[$char];
13231 } elseif ($char == 0xFFFD) {
13232 // skip
13233 } else {
13234 $outarr[] = 63; // '?' character
13237 return $outarr;
13241 * Converts array of UTF-8 characters to UTF16-BE string.<br>
13242 * Based on: http://www.faqs.org/rfcs/rfc2781.html
13243 * <pre>
13244 * Encoding UTF-16:
13246 * Encoding of a single character from an ISO 10646 character value to
13247 * UTF-16 proceeds as follows. Let U be the character number, no greater
13248 * than 0x10FFFF.
13250 * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
13251 * terminate.
13253 * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
13254 * U' must be less than or equal to 0xFFFFF. That is, U' can be
13255 * represented in 20 bits.
13257 * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
13258 * 0xDC00, respectively. These integers each have 10 bits free to
13259 * encode the character value, for a total of 20 bits.
13261 * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
13262 * bits of W1 and the 10 low-order bits of U' to the 10 low-order
13263 * bits of W2. Terminate.
13265 * Graphically, steps 2 through 4 look like:
13266 * U' = yyyyyyyyyyxxxxxxxxxx
13267 * W1 = 110110yyyyyyyyyy
13268 * W2 = 110111xxxxxxxxxx
13269 * </pre>
13270 * @param $unicode (array) array containing UTF-8 unicode values
13271 * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
13272 * @return string
13273 * @protected
13274 * @author Nicola Asuni
13275 * @since 2.1.000 (2008-01-08)
13276 * @see UTF8ToUTF16BE()
13278 protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
13279 $outstr = ''; // string to be returned
13280 if ($setbom) {
13281 $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
13283 foreach ($unicode as $char) {
13284 if ($char == 0x200b) {
13285 // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
13286 } elseif ($char == 0xFFFD) {
13287 $outstr .= "\xFF\xFD"; // replacement character
13288 } elseif ($char < 0x10000) {
13289 $outstr .= chr($char >> 0x08);
13290 $outstr .= chr($char & 0xFF);
13291 } else {
13292 $char -= 0x10000;
13293 $w1 = 0xD800 | ($char >> 0x0a);
13294 $w2 = 0xDC00 | ($char & 0x3FF);
13295 $outstr .= chr($w1 >> 0x08);
13296 $outstr .= chr($w1 & 0xFF);
13297 $outstr .= chr($w2 >> 0x08);
13298 $outstr .= chr($w2 & 0xFF);
13301 return $outstr;
13303 // ====================================================
13306 * Set header font.
13307 * @param $font (array) font
13308 * @public
13309 * @since 1.1
13311 public function setHeaderFont($font) {
13312 $this->header_font = $font;
13316 * Get header font.
13317 * @return array()
13318 * @public
13319 * @since 4.0.012 (2008-07-24)
13321 public function getHeaderFont() {
13322 return $this->header_font;
13326 * Set footer font.
13327 * @param $font (array) font
13328 * @public
13329 * @since 1.1
13331 public function setFooterFont($font) {
13332 $this->footer_font = $font;
13336 * Get Footer font.
13337 * @return array()
13338 * @public
13339 * @since 4.0.012 (2008-07-24)
13341 public function getFooterFont() {
13342 return $this->footer_font;
13346 * Set language array.
13347 * @param $language (array)
13348 * @public
13349 * @since 1.1
13351 public function setLanguageArray($language) {
13352 $this->l = $language;
13353 if (isset($this->l['a_meta_dir'])) {
13354 $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
13355 } else {
13356 $this->rtl = false;
13361 * Returns the PDF data.
13362 * @public
13364 public function getPDFData() {
13365 if ($this->state < 3) {
13366 $this->Close();
13368 return $this->buffer;
13372 * Output anchor link.
13373 * @param $url (string) link URL or internal link (i.e.: &lt;a href="#23,4.5"&gt;link to page 23 at 4.5 Y position&lt;/a&gt;)
13374 * @param $name (string) link name
13375 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
13376 * @param $firstline (boolean) if true prints only the first line and return the remaining string.
13377 * @param $color (array) array of RGB text color
13378 * @param $style (string) font style (U, D, B, I)
13379 * @param $firstblock (boolean) if true the string is the starting of a line.
13380 * @return the number of cells used or the remaining text if $firstline = true;
13381 * @public
13383 public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
13384 if (!$this->empty_string($url) AND ($url{0} == '#')) {
13385 // convert url to internal link
13386 $lnkdata = explode(',', $url);
13387 if (isset($lnkdata[0])) {
13388 $page = intval(substr($lnkdata[0], 1));
13389 if (empty($page) OR ($page <= 0)) {
13390 $page = $this->page;
13392 if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
13393 $lnky = floatval($lnkdata[1]);
13394 } else {
13395 $lnky = 0;
13397 $url = $this->AddLink();
13398 $this->SetLink($url, $lnky, $page);
13401 // store current settings
13402 $prevcolor = $this->fgcolor;
13403 $prevstyle = $this->FontStyle;
13404 if (empty($color)) {
13405 $this->SetTextColorArray($this->htmlLinkColorArray);
13406 } else {
13407 $this->SetTextColorArray($color);
13409 if ($style == -1) {
13410 $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
13411 } else {
13412 $this->SetFont('', $this->FontStyle.$style);
13414 $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
13415 // restore settings
13416 $this->SetFont('', $prevstyle);
13417 $this->SetTextColorArray($prevcolor);
13418 return $ret;
13422 * Returns an array (RGB or CMYK) from an html color name or a six-digit (i.e. #3FE5AA) or three-digit (i.e. #7FF) hexadecimal color representation.
13423 * @param $hcolor (string) HTML color.
13424 * @param $defcol (array) Color to return in case of error.
13425 * @return array RGB or CMYK color, or false in case of error.
13426 * @public
13428 public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array(128,128,128)) {
13429 $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
13430 $color = strtolower($color);
13431 if (($dotpos = strpos($color, '.')) !== false) {
13432 // remove class parent (i.e.: color.red)
13433 $color = substr($color, ($dotpos + 1));
13435 if (strlen($color) == 0) {
13436 return $defcol;
13438 // RGB ARRAY
13439 if (substr($color, 0, 3) == 'rgb') {
13440 $codes = substr($color, 4);
13441 $codes = str_replace(')', '', $codes);
13442 $returncolor = explode(',', $codes);
13443 foreach ($returncolor as $key => $val) {
13444 if (strpos($val, '%') > 0) {
13445 // percentage
13446 $returncolor[$key] = (255 * intval($val) / 100);
13447 } else {
13448 $returncolor[$key] = intval($val);
13450 // normalize value
13451 $returncolor[$key] = max(0, min(255, $returncolor[$key]));
13453 return $returncolor;
13455 // CMYK ARRAY
13456 if (substr($color, 0, 4) == 'cmyk') {
13457 $codes = substr($color, 5);
13458 $codes = str_replace(')', '', $codes);
13459 $returncolor = explode(',', $codes);
13460 foreach ($returncolor as $key => $val) {
13461 if (strpos($val, '%') !== false) {
13462 // percentage
13463 $returncolor[$key] = (100 * intval($val) / 100);
13464 } else {
13465 $returncolor[$key] = intval($val);
13467 // normalize value
13468 $returncolor[$key] = max(0, min(100, $returncolor[$key]));
13470 return $returncolor;
13472 if ($color{0} != '#') {
13473 // COLOR NAME
13474 if (isset($this->webcolor[$color])) {
13475 // web color
13476 $color_code = $this->webcolor[$color];
13477 } else {
13478 // spot color
13479 $returncolor = $this->getSpotColor($color);
13480 if ($returncolor === false) {
13481 $returncolor = $defcol;
13483 return $returncolor;
13485 } else {
13486 $color_code = substr($color, 1);
13488 // HEXADECIMAL REPRESENTATION
13489 switch (strlen($color_code)) {
13490 case 3: {
13491 // 3-digit RGB hexadecimal representation
13492 $r = substr($color_code, 0, 1);
13493 $g = substr($color_code, 1, 1);
13494 $b = substr($color_code, 2, 1);
13495 $returncolor = array();
13496 $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
13497 $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
13498 $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
13499 break;
13501 case 6: {
13502 // 6-digit RGB hexadecimal representation
13503 $returncolor = array();
13504 $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
13505 $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
13506 $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
13507 break;
13509 case 8: {
13510 // 8-digit CMYK hexadecimal representation
13511 $returncolor = array();
13512 $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
13513 $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
13514 $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
13515 $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
13516 break;
13518 default: {
13519 $returncolor = $defcol;
13520 break;
13523 return $returncolor;
13527 * Converts pixels to User's Units.
13528 * @param $px (int) pixels
13529 * @return float value in user's unit
13530 * @public
13531 * @see setImageScale(), getImageScale()
13533 public function pixelsToUnits($px) {
13534 return ($px / ($this->imgscale * $this->k));
13538 * Reverse function for htmlentities.
13539 * Convert entities in UTF-8.
13540 * @param $text_to_convert (string) Text to convert.
13541 * @return string converted text string
13542 * @public
13544 public function unhtmlentities($text_to_convert) {
13545 return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
13548 // ENCRYPTION METHODS ----------------------------------
13551 * Returns a string containing random data to be used as a seed for encryption methods.
13552 * @param $seed (string) starting seed value
13553 * @return string containing random data
13554 * @author Nicola Asuni
13555 * @since 5.9.006 (2010-10-19)
13556 * @protected
13558 protected function getRandomSeed($seed='') {
13559 $seed .= microtime();
13560 if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
13561 // this is not used on windows systems because it is very slow for a know bug
13562 $seed .= openssl_random_pseudo_bytes(512);
13563 } else {
13564 for ($i = 0; $i < 23; ++$i) {
13565 $seed .= uniqid('', true);
13568 $seed .= uniqid('', true);
13569 $seed .= rand();
13570 $seed .= getmypid();
13571 $seed .= __FILE__;
13572 $seed .= $this->bufferlen;
13573 if (isset($_SERVER['REMOTE_ADDR'])) {
13574 $seed .= $_SERVER['REMOTE_ADDR'];
13576 if (isset($_SERVER['HTTP_USER_AGENT'])) {
13577 $seed .= $_SERVER['HTTP_USER_AGENT'];
13579 if (isset($_SERVER['HTTP_ACCEPT'])) {
13580 $seed .= $_SERVER['HTTP_ACCEPT'];
13582 if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
13583 $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
13585 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
13586 $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
13588 if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
13589 $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
13591 $seed .= rand();
13592 $seed .= uniqid('', true);
13593 $seed .= microtime();
13594 return $seed;
13598 * Compute encryption key depending on object number where the encrypted data is stored.
13599 * This is used for all strings and streams without crypt filter specifier.
13600 * @param $n (int) object number
13601 * @return int object key
13602 * @protected
13603 * @author Nicola Asuni
13604 * @since 2.0.000 (2008-01-02)
13606 protected function _objectkey($n) {
13607 $objkey = $this->encryptdata['key'].pack('VXxx', $n);
13608 if ($this->encryptdata['mode'] == 2) { // AES-128
13609 // AES padding
13610 $objkey .= "\x73\x41\x6C\x54"; // sAlT
13612 $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
13613 $objkey = substr($objkey, 0, 16);
13614 return $objkey;
13618 * Encrypt the input string.
13619 * @param $n (int) object number
13620 * @param $s (string) data string to encrypt
13621 * @return encrypted string
13622 * @protected
13623 * @author Nicola Asuni
13624 * @since 5.0.005 (2010-05-11)
13626 protected function _encrypt_data($n, $s) {
13627 if (!$this->encrypted) {
13628 return $s;
13630 switch ($this->encryptdata['mode']) {
13631 case 0: // RC4-40
13632 case 1: { // RC4-128
13633 $s = $this->_RC4($this->_objectkey($n), $s);
13634 break;
13636 case 2: { // AES-128
13637 $s = $this->_AES($this->_objectkey($n), $s);
13638 break;
13640 case 3: { // AES-256
13641 $s = $this->_AES($this->encryptdata['key'], $s);
13642 break;
13645 return $s;
13649 * Put encryption on PDF document.
13650 * @protected
13651 * @author Nicola Asuni
13652 * @since 2.0.000 (2008-01-02)
13654 protected function _putencryption() {
13655 if (!$this->encrypted) {
13656 return;
13658 $this->encryptdata['objid'] = $this->_newobj();
13659 $out = '<<';
13660 if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
13661 $this->encryptdata['Filter'] = 'Standard';
13663 $out .= ' /Filter /'.$this->encryptdata['Filter'];
13664 if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
13665 $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
13667 if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
13668 $this->encryptdata['V'] = 1;
13670 // V is a code specifying the algorithm to be used in encrypting and decrypting the document
13671 $out .= ' /V '.$this->encryptdata['V'];
13672 if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
13673 // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
13674 $out .= ' /Length '.$this->encryptdata['Length'];
13675 } else {
13676 $out .= ' /Length 40';
13678 if ($this->encryptdata['V'] >= 4) {
13679 if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
13680 $this->encryptdata['StmF'] = 'Identity';
13682 if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
13683 // The name of the crypt filter that shall be used when decrypting all strings in the document.
13684 $this->encryptdata['StrF'] = 'Identity';
13686 // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
13687 if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
13688 $out .= ' /CF <<';
13689 $out .= ' /'.$this->encryptdata['StmF'].' <<';
13690 $out .= ' /Type /CryptFilter';
13691 if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
13692 // The method used
13693 $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
13694 if ($this->encryptdata['pubkey']) {
13695 $out .= ' /Recipients [';
13696 foreach ($this->encryptdata['Recipients'] as $rec) {
13697 $out .= ' <'.$rec.'>';
13699 $out .= ' ]';
13700 if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
13701 $out .= ' /EncryptMetadata false';
13702 } else {
13703 $out .= ' /EncryptMetadata true';
13706 } else {
13707 $out .= ' /CFM /None';
13709 if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
13710 // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
13711 $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
13712 } else {
13713 $out .= ' /AuthEvent /DocOpen';
13715 if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
13716 // The bit length of the encryption key.
13717 $out .= ' /Length '.$this->encryptdata['CF']['Length'];
13719 $out .= ' >> >>';
13721 // The name of the crypt filter that shall be used by default when decrypting streams.
13722 $out .= ' /StmF /'.$this->encryptdata['StmF'];
13723 // The name of the crypt filter that shall be used when decrypting all strings in the document.
13724 $out .= ' /StrF /'.$this->encryptdata['StrF'];
13725 if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
13726 // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
13727 $out .= ' /EFF /'.$this->encryptdata[''];
13730 // Additional encryption dictionary entries for the standard security handler
13731 if ($this->encryptdata['pubkey']) {
13732 if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
13733 $out .= ' /Recipients [';
13734 foreach ($this->encryptdata['Recipients'] as $rec) {
13735 $out .= ' <'.$rec.'>';
13737 $out .= ' ]';
13739 } else {
13740 $out .= ' /R';
13741 if ($this->encryptdata['V'] == 5) { // AES-256
13742 $out .= ' 5';
13743 $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
13744 $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
13745 $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
13746 } elseif ($this->encryptdata['V'] == 4) { // AES-128
13747 $out .= ' 4';
13748 } elseif ($this->encryptdata['V'] < 2) { // RC-40
13749 $out .= ' 2';
13750 } else { // RC-128
13751 $out .= ' 3';
13753 $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
13754 $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
13755 $out .= ' /P '.$this->encryptdata['P'];
13756 if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
13757 $out .= ' /EncryptMetadata false';
13758 } else {
13759 $out .= ' /EncryptMetadata true';
13762 $out .= ' >>';
13763 $out .= "\n".'endobj';
13764 $this->_out($out);
13768 * Returns the input text encrypted using RC4 algorithm and the specified key.
13769 * RC4 is the standard encryption algorithm used in PDF format
13770 * @param $key (string) encryption key
13771 * @param $text (String) input text to be encrypted
13772 * @return String encrypted text
13773 * @protected
13774 * @since 2.0.000 (2008-01-02)
13775 * @author Klemen Vodopivec, Nicola Asuni
13777 protected function _RC4($key, $text) {
13778 if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
13779 // try to use mcrypt function if exist
13780 return $out;
13782 if ($this->last_enc_key != $key) {
13783 $k = str_repeat($key, ((256 / strlen($key)) + 1));
13784 $rc4 = range(0, 255);
13785 $j = 0;
13786 for ($i = 0; $i < 256; ++$i) {
13787 $t = $rc4[$i];
13788 $j = ($j + $t + ord($k{$i})) % 256;
13789 $rc4[$i] = $rc4[$j];
13790 $rc4[$j] = $t;
13792 $this->last_enc_key = $key;
13793 $this->last_enc_key_c = $rc4;
13794 } else {
13795 $rc4 = $this->last_enc_key_c;
13797 $len = strlen($text);
13798 $a = 0;
13799 $b = 0;
13800 $out = '';
13801 for ($i = 0; $i < $len; ++$i) {
13802 $a = ($a + 1) % 256;
13803 $t = $rc4[$a];
13804 $b = ($b + $t) % 256;
13805 $rc4[$a] = $rc4[$b];
13806 $rc4[$b] = $t;
13807 $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
13808 $out .= chr(ord($text{$i}) ^ $k);
13810 return $out;
13814 * Returns the input text exrypted using AES algorithm and the specified key.
13815 * This method requires mcrypt.
13816 * @param $key (string) encryption key
13817 * @param $text (String) input text to be encrypted
13818 * @return String encrypted text
13819 * @protected
13820 * @author Nicola Asuni
13821 * @since 5.0.005 (2010-05-11)
13823 protected function _AES($key, $text) {
13824 // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
13825 $padding = 16 - (strlen($text) % 16);
13826 $text .= str_repeat(chr($padding), $padding);
13827 $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
13828 $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
13829 $text = $iv.$text;
13830 return $text;
13834 * Encrypts a string using MD5 and returns it's value as a binary string.
13835 * @param $str (string) input string
13836 * @return String MD5 encrypted binary string
13837 * @protected
13838 * @since 2.0.000 (2008-01-02)
13839 * @author Klemen Vodopivec
13841 protected function _md5_16($str) {
13842 return pack('H*', md5($str));
13846 * Compute U value (used for encryption)
13847 * @return string U value
13848 * @protected
13849 * @since 2.0.000 (2008-01-02)
13850 * @author Nicola Asuni
13852 protected function _Uvalue() {
13853 if ($this->encryptdata['mode'] == 0) { // RC4-40
13854 return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
13855 } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
13856 $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
13857 $enc = $this->_RC4($this->encryptdata['key'], $tmp);
13858 $len = strlen($tmp);
13859 for ($i = 1; $i <= 19; ++$i) {
13860 $ek = '';
13861 for ($j = 0; $j < $len; ++$j) {
13862 $ek .= chr(ord($this->encryptdata['key']{$j}) ^ $i);
13864 $enc = $this->_RC4($ek, $enc);
13866 $enc .= str_repeat("\x00", 16);
13867 return substr($enc, 0, 32);
13868 } elseif ($this->encryptdata['mode'] == 3) { // AES-256
13869 $seed = $this->_md5_16($this->getRandomSeed());
13870 // User Validation Salt
13871 $this->encryptdata['UVS'] = substr($seed, 0, 8);
13872 // User Key Salt
13873 $this->encryptdata['UKS'] = substr($seed, 8, 16);
13874 return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
13879 * Compute UE value (used for encryption)
13880 * @return string UE value
13881 * @protected
13882 * @since 5.9.006 (2010-10-19)
13883 * @author Nicola Asuni
13885 protected function _UEvalue() {
13886 $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
13887 $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
13888 return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
13892 * Compute O value (used for encryption)
13893 * @return string O value
13894 * @protected
13895 * @since 2.0.000 (2008-01-02)
13896 * @author Nicola Asuni
13898 protected function _Ovalue() {
13899 if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
13900 $tmp = $this->_md5_16($this->encryptdata['owner_password']);
13901 if ($this->encryptdata['mode'] > 0) {
13902 for ($i = 0; $i < 50; ++$i) {
13903 $tmp = $this->_md5_16($tmp);
13906 $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
13907 $enc = $this->_RC4($owner_key, $this->encryptdata['user_password']);
13908 if ($this->encryptdata['mode'] > 0) {
13909 $len = strlen($owner_key);
13910 for ($i = 1; $i <= 19; ++$i) {
13911 $ek = '';
13912 for ($j = 0; $j < $len; ++$j) {
13913 $ek .= chr(ord($owner_key{$j}) ^ $i);
13915 $enc = $this->_RC4($ek, $enc);
13918 return $enc;
13919 } elseif ($this->encryptdata['mode'] == 3) { // AES-256
13920 $seed = $this->_md5_16($this->getRandomSeed());
13921 // Owner Validation Salt
13922 $this->encryptdata['OVS'] = substr($seed, 0, 8);
13923 // Owner Key Salt
13924 $this->encryptdata['OKS'] = substr($seed, 8, 16);
13925 return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
13930 * Compute OE value (used for encryption)
13931 * @return string OE value
13932 * @protected
13933 * @since 5.9.006 (2010-10-19)
13934 * @author Nicola Asuni
13936 protected function _OEvalue() {
13937 $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
13938 $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
13939 return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
13943 * Convert password for AES-256 encryption mode
13944 * @param $password (string) password
13945 * @return string password
13946 * @protected
13947 * @since 5.9.006 (2010-10-19)
13948 * @author Nicola Asuni
13950 protected function _fixAES256Password($password) {
13951 $psw = ''; // password to be returned
13952 $psw_array = $this->utf8Bidi($this->UTF8StringToArray($password), $password, $this->rtl);
13953 foreach ($psw_array as $c) {
13954 $psw .= $this->unichr($c);
13956 return substr($psw, 0, 127);
13960 * Compute encryption key
13961 * @protected
13962 * @since 2.0.000 (2008-01-02)
13963 * @author Nicola Asuni
13965 protected function _generateencryptionkey() {
13966 $keybytelen = ($this->encryptdata['Length'] / 8);
13967 if (!$this->encryptdata['pubkey']) { // standard mode
13968 if ($this->encryptdata['mode'] == 3) { // AES-256
13969 // generate 256 bit random key
13970 $this->encryptdata['key'] = substr(hash('sha256', $this->getRandomSeed(), true), 0, $keybytelen);
13971 // truncate passwords
13972 $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
13973 $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
13974 // Compute U value
13975 $this->encryptdata['U'] = $this->_Uvalue();
13976 // Compute UE value
13977 $this->encryptdata['UE'] = $this->_UEvalue();
13978 // Compute O value
13979 $this->encryptdata['O'] = $this->_Ovalue();
13980 // Compute OE value
13981 $this->encryptdata['OE'] = $this->_OEvalue();
13982 // Compute P value
13983 $this->encryptdata['P'] = $this->encryptdata['protection'];
13984 // Computing the encryption dictionary's Perms (permissions) value
13985 $perms = $this->getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
13986 $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
13987 if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
13988 $perms .= 'F';
13989 } else {
13990 $perms .= 'T';
13992 $perms .= 'adb'; // bytes 9-11
13993 $perms .= 'nick'; // bytes 12-15
13994 $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
13995 $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
13996 } else { // RC4-40, RC4-128, AES-128
13997 // Pad passwords
13998 $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].$this->enc_padding, 0, 32);
13999 $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].$this->enc_padding, 0, 32);
14000 // Compute O value
14001 $this->encryptdata['O'] = $this->_Ovalue();
14002 // get default permissions (reverse byte order)
14003 $permissions = $this->getEncPermissionsString($this->encryptdata['protection']);
14004 // Compute encryption key
14005 $tmp = $this->_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
14006 if ($this->encryptdata['mode'] > 0) {
14007 for ($i = 0; $i < 50; ++$i) {
14008 $tmp = $this->_md5_16(substr($tmp, 0, $keybytelen));
14011 $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
14012 // Compute U value
14013 $this->encryptdata['U'] = $this->_Uvalue();
14014 // Compute P value
14015 $this->encryptdata['P'] = $this->encryptdata['protection'];
14017 } else { // Public-Key mode
14018 // random 20-byte seed
14019 $seed = sha1($this->getRandomSeed(), true);
14020 $recipient_bytes = '';
14021 foreach ($this->encryptdata['pubkeys'] as $pubkey) {
14022 // for each public certificate
14023 if (isset($pubkey['p'])) {
14024 $pkprotection = $this->getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
14025 } else {
14026 $pkprotection = $this->encryptdata['protection'];
14028 // get default permissions (reverse byte order)
14029 $pkpermissions = $this->getEncPermissionsString($pkprotection);
14030 // envelope data
14031 $envelope = $seed.$pkpermissions;
14032 // write the envelope data to a temporary file
14033 $tempkeyfile = tempnam(K_PATH_CACHE, 'tmpkey_');
14034 $f = fopen($tempkeyfile, 'wb');
14035 if (!$f) {
14036 $this->Error('Unable to create temporary key file: '.$tempkeyfile);
14038 $envelope_length = strlen($envelope);
14039 fwrite($f, $envelope, $envelope_length);
14040 fclose($f);
14041 $tempencfile = tempnam(K_PATH_CACHE, 'tmpenc_');
14042 if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_DETACHED | PKCS7_BINARY)) {
14043 $this->Error('Unable to encrypt the file: '.$tempkeyfile);
14045 unlink($tempkeyfile);
14046 // read encryption signature
14047 $signature = file_get_contents($tempencfile, false, null, $envelope_length);
14048 unlink($tempencfile);
14049 // extract signature
14050 $signature = substr($signature, strpos($signature, 'Content-Disposition'));
14051 $tmparr = explode("\n\n", $signature);
14052 $signature = trim($tmparr[1]);
14053 unset($tmparr);
14054 // decode signature
14055 $signature = base64_decode($signature);
14056 // convert signature to hex
14057 $hexsignature = current(unpack('H*', $signature));
14058 // store signature on recipients array
14059 $this->encryptdata['Recipients'][] = $hexsignature;
14060 // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
14061 $recipient_bytes .= $signature;
14063 // calculate encryption key
14064 if ($this->encryptdata['mode'] == 3) { // AES-256
14065 $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
14066 } else { // RC4-40, RC4-128, AES-128
14067 $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
14073 * Return the premission code used on encryption (P value).
14074 * @param $permissions (Array) the set of permissions (specify the ones you want to block).
14075 * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
14076 * @protected
14077 * @since 5.0.005 (2010-05-12)
14078 * @author Nicola Asuni
14080 protected function getUserPermissionCode($permissions, $mode=0) {
14081 $options = array(
14082 'owner' => 2, // bit 2 -- inverted logic: cleared by default
14083 'print' => 4, // bit 3
14084 'modify' => 8, // bit 4
14085 'copy' => 16, // bit 5
14086 'annot-forms' => 32, // bit 6
14087 'fill-forms' => 256, // bit 9
14088 'extract' => 512, // bit 10
14089 'assemble' => 1024,// bit 11
14090 'print-high' => 2048 // bit 12
14092 $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
14093 foreach ($permissions as $permission) {
14094 if (!isset($options[$permission])) {
14095 $this->Error('Incorrect permission: '.$permission);
14097 if (($mode > 0) OR ($options[$permission] <= 32)) {
14098 // set only valid permissions
14099 if ($options[$permission] == 2) {
14100 // the logic for bit 2 is inverted (cleared by default)
14101 $protection += $options[$permission];
14102 } else {
14103 $protection -= $options[$permission];
14107 return $protection;
14111 * Set document protection
14112 * Remark: the protection against modification is for people who have the full Acrobat product.
14113 * If you don't set any password, the document will open as usual. If you set a user password, the PDF viewer will ask for it before displaying the document. The master password, if different from the user one, can be used to get full access.
14114 * Note: protecting a document requires to encrypt it, which increases the processing time a lot. This can cause a PHP time-out in some cases, especially if the document contains images or fonts.
14115 * @param $permissions (Array) the set of permissions (specify the ones you want to block):<ul><li>print : Print the document;</li><li>modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';</li><li>copy : Copy or otherwise extract text and graphics from the document;</li><li>annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);</li><li>fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;</li><li>extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);</li><li>assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;</li><li>print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.</li><li>owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.</li></ul>
14116 * @param $user_pass (String) user password. Empty by default.
14117 * @param $owner_pass (String) owner password. If not specified, a random value is used.
14118 * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
14119 * @param $pubkeys (String) array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../tcpdf.crt', 'p' => array('print')))
14120 * @public
14121 * @since 2.0.000 (2008-01-02)
14122 * @author Nicola Asuni
14124 public function SetProtection($permissions=array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass='', $owner_pass=null, $mode=0, $pubkeys=null) {
14125 if ($this->pdfa_mode) {
14126 // encryption is not allowed in PDF/A mode
14127 return;
14129 $this->encryptdata['protection'] = $this->getUserPermissionCode($permissions, $mode);
14130 if (($pubkeys !== null) AND (is_array($pubkeys))) {
14131 // public-key mode
14132 $this->encryptdata['pubkeys'] = $pubkeys;
14133 if ($mode == 0) {
14134 // public-Key Security requires at least 128 bit
14135 $mode = 1;
14137 if (!function_exists('openssl_pkcs7_encrypt')) {
14138 $this->Error('Public-Key Security requires openssl library.');
14140 // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
14141 $this->encryptdata['pubkey'] = true;
14142 $this->encryptdata['Filter'] = 'Adobe.PubSec';
14143 $this->encryptdata['StmF'] = 'DefaultCryptFilter';
14144 $this->encryptdata['StrF'] = 'DefaultCryptFilter';
14145 } else {
14146 // standard mode (password mode)
14147 $this->encryptdata['pubkey'] = false;
14148 $this->encryptdata['Filter'] = 'Standard';
14149 $this->encryptdata['StmF'] = 'StdCF';
14150 $this->encryptdata['StrF'] = 'StdCF';
14152 if ($mode > 1) { // AES
14153 if (!extension_loaded('mcrypt')) {
14154 $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
14156 if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
14157 $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
14159 if (($mode == 3) AND !function_exists('hash')) {
14160 // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
14161 $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
14164 if ($owner_pass === null) {
14165 $owner_pass = md5($this->getRandomSeed());
14167 $this->encryptdata['user_password'] = $user_pass;
14168 $this->encryptdata['owner_password'] = $owner_pass;
14169 $this->encryptdata['mode'] = $mode;
14170 switch ($mode) {
14171 case 0: { // RC4 40 bit
14172 $this->encryptdata['V'] = 1;
14173 $this->encryptdata['Length'] = 40;
14174 $this->encryptdata['CF']['CFM'] = 'V2';
14175 break;
14177 case 1: { // RC4 128 bit
14178 $this->encryptdata['V'] = 2;
14179 $this->encryptdata['Length'] = 128;
14180 $this->encryptdata['CF']['CFM'] = 'V2';
14181 if ($this->encryptdata['pubkey']) {
14182 $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
14183 $this->encryptdata['Recipients'] = array();
14185 break;
14187 case 2: { // AES 128 bit
14188 $this->encryptdata['V'] = 4;
14189 $this->encryptdata['Length'] = 128;
14190 $this->encryptdata['CF']['CFM'] = 'AESV2';
14191 $this->encryptdata['CF']['Length'] = 128;
14192 if ($this->encryptdata['pubkey']) {
14193 $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
14194 $this->encryptdata['Recipients'] = array();
14196 break;
14198 case 3: { // AES 256 bit
14199 $this->encryptdata['V'] = 5;
14200 $this->encryptdata['Length'] = 256;
14201 $this->encryptdata['CF']['CFM'] = 'AESV3';
14202 $this->encryptdata['CF']['Length'] = 256;
14203 if ($this->encryptdata['pubkey']) {
14204 $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
14205 $this->encryptdata['Recipients'] = array();
14207 break;
14210 $this->encrypted = true;
14211 $this->encryptdata['fileid'] = $this->convertHexStringToString($this->file_id);
14212 $this->_generateencryptionkey();
14216 * Convert hexadecimal string to string
14217 * @param $bs (string) byte-string to convert
14218 * @return String
14219 * @protected
14220 * @since 5.0.005 (2010-05-12)
14221 * @author Nicola Asuni
14223 protected function convertHexStringToString($bs) {
14224 $string = ''; // string to be returned
14225 $bslength = strlen($bs);
14226 if (($bslength % 2) != 0) {
14227 // padding
14228 $bs .= '0';
14229 ++$bslength;
14231 for ($i = 0; $i < $bslength; $i += 2) {
14232 $string .= chr(hexdec($bs{$i}.$bs{($i + 1)}));
14234 return $string;
14238 * Convert string to hexadecimal string (byte string)
14239 * @param $s (string) string to convert
14240 * @return byte string
14241 * @protected
14242 * @since 5.0.010 (2010-05-17)
14243 * @author Nicola Asuni
14245 protected function convertStringToHexString($s) {
14246 $bs = '';
14247 $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
14248 foreach ($chars as $c) {
14249 $bs .= sprintf('%02s', dechex(ord($c)));
14251 return $bs;
14255 * Convert encryption P value to a string of bytes, low-order byte first.
14256 * @param $protection (string) 32bit encryption permission value (P value)
14257 * @return String
14258 * @protected
14259 * @since 5.0.005 (2010-05-12)
14260 * @author Nicola Asuni
14262 protected function getEncPermissionsString($protection) {
14263 $binprot = sprintf('%032b', $protection);
14264 $str = chr(bindec(substr($binprot, 24, 8)));
14265 $str .= chr(bindec(substr($binprot, 16, 8)));
14266 $str .= chr(bindec(substr($binprot, 8, 8)));
14267 $str .= chr(bindec(substr($binprot, 0, 8)));
14268 return $str;
14271 // END OF ENCRYPTION FUNCTIONS -------------------------
14273 // START TRANSFORMATIONS SECTION -----------------------
14276 * Starts a 2D tranformation saving current graphic state.
14277 * This function must be called before scaling, mirroring, translation, rotation and skewing.
14278 * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
14279 * @public
14280 * @since 2.1.000 (2008-01-07)
14281 * @see StartTransform(), StopTransform()
14283 public function StartTransform() {
14284 $this->_out('q');
14285 if ($this->inxobj) {
14286 // we are inside an XObject template
14287 $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
14288 } else {
14289 $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
14291 ++$this->transfmatrix_key;
14292 $this->transfmatrix[$this->transfmatrix_key] = array();
14296 * Stops a 2D tranformation restoring previous graphic state.
14297 * This function must be called after scaling, mirroring, translation, rotation and skewing.
14298 * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
14299 * @public
14300 * @since 2.1.000 (2008-01-07)
14301 * @see StartTransform(), StopTransform()
14303 public function StopTransform() {
14304 $this->_out('Q');
14305 if (isset($this->transfmatrix[$this->transfmatrix_key])) {
14306 array_pop($this->transfmatrix[$this->transfmatrix_key]);
14307 --$this->transfmatrix_key;
14309 if ($this->inxobj) {
14310 // we are inside an XObject template
14311 array_pop($this->xobjects[$this->xobjid]['transfmrk']);
14312 } else {
14313 array_pop($this->transfmrk[$this->page]);
14317 * Horizontal Scaling.
14318 * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
14319 * @param $x (int) abscissa of the scaling center. Default is current x position
14320 * @param $y (int) ordinate of the scaling center. Default is current y position
14321 * @public
14322 * @since 2.1.000 (2008-01-07)
14323 * @see StartTransform(), StopTransform()
14325 public function ScaleX($s_x, $x='', $y='') {
14326 $this->Scale($s_x, 100, $x, $y);
14330 * Vertical Scaling.
14331 * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
14332 * @param $x (int) abscissa of the scaling center. Default is current x position
14333 * @param $y (int) ordinate of the scaling center. Default is current y position
14334 * @public
14335 * @since 2.1.000 (2008-01-07)
14336 * @see StartTransform(), StopTransform()
14338 public function ScaleY($s_y, $x='', $y='') {
14339 $this->Scale(100, $s_y, $x, $y);
14343 * Vertical and horizontal proportional Scaling.
14344 * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
14345 * @param $x (int) abscissa of the scaling center. Default is current x position
14346 * @param $y (int) ordinate of the scaling center. Default is current y position
14347 * @public
14348 * @since 2.1.000 (2008-01-07)
14349 * @see StartTransform(), StopTransform()
14351 public function ScaleXY($s, $x='', $y='') {
14352 $this->Scale($s, $s, $x, $y);
14356 * Vertical and horizontal non-proportional Scaling.
14357 * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
14358 * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
14359 * @param $x (int) abscissa of the scaling center. Default is current x position
14360 * @param $y (int) ordinate of the scaling center. Default is current y position
14361 * @public
14362 * @since 2.1.000 (2008-01-07)
14363 * @see StartTransform(), StopTransform()
14365 public function Scale($s_x, $s_y, $x='', $y='') {
14366 if ($x === '') {
14367 $x = $this->x;
14369 if ($y === '') {
14370 $y = $this->y;
14372 if (($s_x == 0) OR ($s_y == 0)) {
14373 $this->Error('Please do not use values equal to zero for scaling');
14375 $y = ($this->h - $y) * $this->k;
14376 $x *= $this->k;
14377 //calculate elements of transformation matrix
14378 $s_x /= 100;
14379 $s_y /= 100;
14380 $tm = array();
14381 $tm[0] = $s_x;
14382 $tm[1] = 0;
14383 $tm[2] = 0;
14384 $tm[3] = $s_y;
14385 $tm[4] = $x * (1 - $s_x);
14386 $tm[5] = $y * (1 - $s_y);
14387 //scale the coordinate system
14388 $this->Transform($tm);
14392 * Horizontal Mirroring.
14393 * @param $x (int) abscissa of the point. Default is current x position
14394 * @public
14395 * @since 2.1.000 (2008-01-07)
14396 * @see StartTransform(), StopTransform()
14398 public function MirrorH($x='') {
14399 $this->Scale(-100, 100, $x);
14403 * Verical Mirroring.
14404 * @param $y (int) ordinate of the point. Default is current y position
14405 * @public
14406 * @since 2.1.000 (2008-01-07)
14407 * @see StartTransform(), StopTransform()
14409 public function MirrorV($y='') {
14410 $this->Scale(100, -100, '', $y);
14414 * Point reflection mirroring.
14415 * @param $x (int) abscissa of the point. Default is current x position
14416 * @param $y (int) ordinate of the point. Default is current y position
14417 * @public
14418 * @since 2.1.000 (2008-01-07)
14419 * @see StartTransform(), StopTransform()
14421 public function MirrorP($x='',$y='') {
14422 $this->Scale(-100, -100, $x, $y);
14426 * Reflection against a straight line through point (x, y) with the gradient angle (angle).
14427 * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
14428 * @param $x (int) abscissa of the point. Default is current x position
14429 * @param $y (int) ordinate of the point. Default is current y position
14430 * @public
14431 * @since 2.1.000 (2008-01-07)
14432 * @see StartTransform(), StopTransform()
14434 public function MirrorL($angle=0, $x='',$y='') {
14435 $this->Scale(-100, 100, $x, $y);
14436 $this->Rotate(-2*($angle-90), $x, $y);
14440 * Translate graphic object horizontally.
14441 * @param $t_x (int) movement to the right (or left for RTL)
14442 * @public
14443 * @since 2.1.000 (2008-01-07)
14444 * @see StartTransform(), StopTransform()
14446 public function TranslateX($t_x) {
14447 $this->Translate($t_x, 0);
14451 * Translate graphic object vertically.
14452 * @param $t_y (int) movement to the bottom
14453 * @public
14454 * @since 2.1.000 (2008-01-07)
14455 * @see StartTransform(), StopTransform()
14457 public function TranslateY($t_y) {
14458 $this->Translate(0, $t_y);
14462 * Translate graphic object horizontally and vertically.
14463 * @param $t_x (int) movement to the right
14464 * @param $t_y (int) movement to the bottom
14465 * @public
14466 * @since 2.1.000 (2008-01-07)
14467 * @see StartTransform(), StopTransform()
14469 public function Translate($t_x, $t_y) {
14470 //calculate elements of transformation matrix
14471 $tm = array();
14472 $tm[0] = 1;
14473 $tm[1] = 0;
14474 $tm[2] = 0;
14475 $tm[3] = 1;
14476 $tm[4] = $t_x * $this->k;
14477 $tm[5] = -$t_y * $this->k;
14478 //translate the coordinate system
14479 $this->Transform($tm);
14483 * Rotate object.
14484 * @param $angle (float) angle in degrees for counter-clockwise rotation
14485 * @param $x (int) abscissa of the rotation center. Default is current x position
14486 * @param $y (int) ordinate of the rotation center. Default is current y position
14487 * @public
14488 * @since 2.1.000 (2008-01-07)
14489 * @see StartTransform(), StopTransform()
14491 public function Rotate($angle, $x='', $y='') {
14492 if ($x === '') {
14493 $x = $this->x;
14495 if ($y === '') {
14496 $y = $this->y;
14498 $y = ($this->h - $y) * $this->k;
14499 $x *= $this->k;
14500 //calculate elements of transformation matrix
14501 $tm = array();
14502 $tm[0] = cos(deg2rad($angle));
14503 $tm[1] = sin(deg2rad($angle));
14504 $tm[2] = -$tm[1];
14505 $tm[3] = $tm[0];
14506 $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
14507 $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
14508 //rotate the coordinate system around ($x,$y)
14509 $this->Transform($tm);
14513 * Skew horizontally.
14514 * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
14515 * @param $x (int) abscissa of the skewing center. default is current x position
14516 * @param $y (int) ordinate of the skewing center. default is current y position
14517 * @public
14518 * @since 2.1.000 (2008-01-07)
14519 * @see StartTransform(), StopTransform()
14521 public function SkewX($angle_x, $x='', $y='') {
14522 $this->Skew($angle_x, 0, $x, $y);
14526 * Skew vertically.
14527 * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
14528 * @param $x (int) abscissa of the skewing center. default is current x position
14529 * @param $y (int) ordinate of the skewing center. default is current y position
14530 * @public
14531 * @since 2.1.000 (2008-01-07)
14532 * @see StartTransform(), StopTransform()
14534 public function SkewY($angle_y, $x='', $y='') {
14535 $this->Skew(0, $angle_y, $x, $y);
14539 * Skew.
14540 * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
14541 * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
14542 * @param $x (int) abscissa of the skewing center. default is current x position
14543 * @param $y (int) ordinate of the skewing center. default is current y position
14544 * @public
14545 * @since 2.1.000 (2008-01-07)
14546 * @see StartTransform(), StopTransform()
14548 public function Skew($angle_x, $angle_y, $x='', $y='') {
14549 if ($x === '') {
14550 $x = $this->x;
14552 if ($y === '') {
14553 $y = $this->y;
14555 if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
14556 $this->Error('Please use values between -90 and +90 degrees for Skewing.');
14558 $x *= $this->k;
14559 $y = ($this->h - $y) * $this->k;
14560 //calculate elements of transformation matrix
14561 $tm = array();
14562 $tm[0] = 1;
14563 $tm[1] = tan(deg2rad($angle_y));
14564 $tm[2] = tan(deg2rad($angle_x));
14565 $tm[3] = 1;
14566 $tm[4] = -$tm[2] * $y;
14567 $tm[5] = -$tm[1] * $x;
14568 //skew the coordinate system
14569 $this->Transform($tm);
14573 * Apply graphic transformations.
14574 * @param $tm (array) transformation matrix
14575 * @protected
14576 * @since 2.1.000 (2008-01-07)
14577 * @see StartTransform(), StopTransform()
14579 protected function Transform($tm) {
14580 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
14581 // add tranformation matrix
14582 $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
14583 // update transformation mark
14584 if ($this->inxobj) {
14585 // we are inside an XObject template
14586 if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
14587 $key = key($this->xobjects[$this->xobjid]['transfmrk']);
14588 $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
14590 } elseif (end($this->transfmrk[$this->page]) !== false) {
14591 $key = key($this->transfmrk[$this->page]);
14592 $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
14596 // END TRANSFORMATIONS SECTION -------------------------
14598 // START GRAPHIC FUNCTIONS SECTION ---------------------
14599 // The following section is based on the code provided by David Hernandez Sanz
14602 * Defines the line width. By default, the value equals 0.2 mm. The method can be called before the first page is created and the value is retained from page to page.
14603 * @param $width (float) The width.
14604 * @public
14605 * @since 1.0
14606 * @see Line(), Rect(), Cell(), MultiCell()
14608 public function SetLineWidth($width) {
14609 //Set line width
14610 $this->LineWidth = $width;
14611 $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
14612 if ($this->page > 0) {
14613 $this->_out($this->linestyleWidth);
14618 * Returns the current the line width.
14619 * @return int Line width
14620 * @public
14621 * @since 2.1.000 (2008-01-07)
14622 * @see Line(), SetLineWidth()
14624 public function GetLineWidth() {
14625 return $this->LineWidth;
14629 * Set line style.
14630 * @param $style (array) Line style. Array with keys among the following:
14631 * <ul>
14632 * <li>width (float): Width of the line in user units.</li>
14633 * <li>cap (string): Type of cap to put on the line. Possible values are:
14634 * butt, round, square. The difference between "square" and "butt" is that
14635 * "square" projects a flat end past the end of the line.</li>
14636 * <li>join (string): Type of join. Possible values are: miter, round,
14637 * bevel.</li>
14638 * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
14639 * series of length values, which are the lengths of the on and off dashes.
14640 * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
14641 * 1 off, 2 on, 1 off, ...</li>
14642 * <li>phase (integer): Modifier on the dash pattern which is used to shift
14643 * the point at which the pattern starts.</li>
14644 * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
14645 * </ul>
14646 * @param $ret (boolean) if true do not send the command.
14647 * @return string the PDF command
14648 * @public
14649 * @since 2.1.000 (2008-01-08)
14651 public function SetLineStyle($style, $ret=false) {
14652 $s = ''; // string to be returned
14653 if (!is_array($style)) {
14654 return;
14656 if (isset($style['width'])) {
14657 $this->LineWidth = $style['width'];
14658 $this->linestyleWidth = sprintf('%.2F w', ($style['width'] * $this->k));
14659 $s .= $this->linestyleWidth.' ';
14661 if (isset($style['cap'])) {
14662 $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
14663 if (isset($ca[$style['cap']])) {
14664 $this->linestyleCap = $ca[$style['cap']].' J';
14665 $s .= $this->linestyleCap.' ';
14668 if (isset($style['join'])) {
14669 $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
14670 if (isset($ja[$style['join']])) {
14671 $this->linestyleJoin = $ja[$style['join']].' j';
14672 $s .= $this->linestyleJoin.' ';
14675 if (isset($style['dash'])) {
14676 $dash_string = '';
14677 if ($style['dash']) {
14678 if (preg_match('/^.+,/', $style['dash']) > 0) {
14679 $tab = explode(',', $style['dash']);
14680 } else {
14681 $tab = array($style['dash']);
14683 $dash_string = '';
14684 foreach ($tab as $i => $v) {
14685 if ($i) {
14686 $dash_string .= ' ';
14688 $dash_string .= sprintf('%.2F', $v);
14691 if (!isset($style['phase']) OR !$style['dash']) {
14692 $style['phase'] = 0;
14694 $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $style['phase']);
14695 $s .= $this->linestyleDash.' ';
14697 if (isset($style['color'])) {
14698 $s .= $this->SetDrawColorArray($style['color'], true).' ';
14700 if (!$ret) {
14701 $this->_out($s);
14703 return $s;
14707 * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
14708 * @param $x (float) Abscissa of point.
14709 * @param $y (float) Ordinate of point.
14710 * @protected
14711 * @since 2.1.000 (2008-01-08)
14713 protected function _outPoint($x, $y) {
14714 $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
14718 * Append a straight line segment from the current point to the point (x, y).
14719 * The new current point shall be (x, y).
14720 * @param $x (float) Abscissa of end point.
14721 * @param $y (float) Ordinate of end point.
14722 * @protected
14723 * @since 2.1.000 (2008-01-08)
14725 protected function _outLine($x, $y) {
14726 $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
14730 * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
14731 * @param $x (float) Abscissa of upper-left corner.
14732 * @param $y (float) Ordinate of upper-left corner.
14733 * @param $w (float) Width.
14734 * @param $h (float) Height.
14735 * @param $op (string) options
14736 * @protected
14737 * @since 2.1.000 (2008-01-08)
14739 protected function _outRect($x, $y, $w, $h, $op) {
14740 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
14744 * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bézier control points.
14745 * The new current point shall be (x3, y3).
14746 * @param $x1 (float) Abscissa of control point 1.
14747 * @param $y1 (float) Ordinate of control point 1.
14748 * @param $x2 (float) Abscissa of control point 2.
14749 * @param $y2 (float) Ordinate of control point 2.
14750 * @param $x3 (float) Abscissa of end point.
14751 * @param $y3 (float) Ordinate of end point.
14752 * @protected
14753 * @since 2.1.000 (2008-01-08)
14755 protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
14756 $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
14760 * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the Bézier control points.
14761 * The new current point shall be (x3, y3).
14762 * @param $x2 (float) Abscissa of control point 2.
14763 * @param $y2 (float) Ordinate of control point 2.
14764 * @param $x3 (float) Abscissa of end point.
14765 * @param $y3 (float) Ordinate of end point.
14766 * @protected
14767 * @since 4.9.019 (2010-04-26)
14769 protected function _outCurveV($x2, $y2, $x3, $y3) {
14770 $this->_out(sprintf('%.2F %.2F %.2F %.2F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
14774 * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the Bézier control points.
14775 * The new current point shall be (x3, y3).
14776 * @param $x1 (float) Abscissa of control point 1.
14777 * @param $y1 (float) Ordinate of control point 1.
14778 * @param $x3 (float) Abscissa of end point.
14779 * @param $y3 (float) Ordinate of end point.
14780 * @protected
14781 * @since 2.1.000 (2008-01-08)
14783 protected function _outCurveY($x1, $y1, $x3, $y3) {
14784 $this->_out(sprintf('%.2F %.2F %.2F %.2F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
14788 * Draws a line between two points.
14789 * @param $x1 (float) Abscissa of first point.
14790 * @param $y1 (float) Ordinate of first point.
14791 * @param $x2 (float) Abscissa of second point.
14792 * @param $y2 (float) Ordinate of second point.
14793 * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
14794 * @public
14795 * @since 1.0
14796 * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
14798 public function Line($x1, $y1, $x2, $y2, $style=array()) {
14799 if (is_array($style)) {
14800 $this->SetLineStyle($style);
14802 $this->_outPoint($x1, $y1);
14803 $this->_outLine($x2, $y2);
14804 $this->_out('S');
14808 * Draws a rectangle.
14809 * @param $x (float) Abscissa of upper-left corner.
14810 * @param $y (float) Ordinate of upper-left corner.
14811 * @param $w (float) Width.
14812 * @param $h (float) Height.
14813 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
14814 * @param $border_style (array) Border style of rectangle. Array with keys among the following:
14815 * <ul>
14816 * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
14817 * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
14818 * </ul>
14819 * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
14820 * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
14821 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
14822 * @public
14823 * @since 1.0
14824 * @see SetLineStyle()
14826 public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
14827 if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
14828 $this->SetFillColorArray($fill_color);
14830 $op = $this->getPathPaintOperator($style);
14831 if ((!$border_style) OR (isset($border_style['all']))) {
14832 if (isset($border_style['all']) AND $border_style['all']) {
14833 $this->SetLineStyle($border_style['all']);
14834 $border_style = array();
14837 $this->_outRect($x, $y, $w, $h, $op);
14838 if ($border_style) {
14839 $border_style2 = array();
14840 foreach ($border_style as $line => $value) {
14841 $length = strlen($line);
14842 for ($i = 0; $i < $length; ++$i) {
14843 $border_style2[$line[$i]] = $value;
14846 $border_style = $border_style2;
14847 if (isset($border_style['L']) AND $border_style['L']) {
14848 $this->Line($x, $y, $x, $y + $h, $border_style['L']);
14850 if (isset($border_style['T']) AND $border_style['T']) {
14851 $this->Line($x, $y, $x + $w, $y, $border_style['T']);
14853 if (isset($border_style['R']) AND $border_style['R']) {
14854 $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
14856 if (isset($border_style['B']) AND $border_style['B']) {
14857 $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
14863 * Draws a Bezier curve.
14864 * The Bezier curve is a tangent to the line between the control points at
14865 * either end of the curve.
14866 * @param $x0 (float) Abscissa of start point.
14867 * @param $y0 (float) Ordinate of start point.
14868 * @param $x1 (float) Abscissa of control point 1.
14869 * @param $y1 (float) Ordinate of control point 1.
14870 * @param $x2 (float) Abscissa of control point 2.
14871 * @param $y2 (float) Ordinate of control point 2.
14872 * @param $x3 (float) Abscissa of end point.
14873 * @param $y3 (float) Ordinate of end point.
14874 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
14875 * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
14876 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
14877 * @public
14878 * @see SetLineStyle()
14879 * @since 2.1.000 (2008-01-08)
14881 public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
14882 if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
14883 $this->SetFillColorArray($fill_color);
14885 $op = $this->getPathPaintOperator($style);
14886 if ($line_style) {
14887 $this->SetLineStyle($line_style);
14889 $this->_outPoint($x0, $y0);
14890 $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
14891 $this->_out($op);
14895 * Draws a poly-Bezier curve.
14896 * Each Bezier curve segment is a tangent to the line between the control points at
14897 * either end of the curve.
14898 * @param $x0 (float) Abscissa of start point.
14899 * @param $y0 (float) Ordinate of start point.
14900 * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
14901 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
14902 * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
14903 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
14904 * @public
14905 * @see SetLineStyle()
14906 * @since 3.0008 (2008-05-12)
14908 public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
14909 if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
14910 $this->SetFillColorArray($fill_color);
14912 $op = $this->getPathPaintOperator($style);
14913 if ($op == 'f') {
14914 $line_style = array();
14916 if ($line_style) {
14917 $this->SetLineStyle($line_style);
14919 $this->_outPoint($x0, $y0);
14920 foreach ($segments as $segment) {
14921 list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
14922 $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
14924 $this->_out($op);
14928 * Draws an ellipse.
14929 * An ellipse is formed from n Bezier curves.
14930 * @param $x0 (float) Abscissa of center point.
14931 * @param $y0 (float) Ordinate of center point.
14932 * @param $rx (float) Horizontal radius.
14933 * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
14934 * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
14935 * @param $astart: (float) Angle start of draw line. Default value: 0.
14936 * @param $afinish: (float) Angle finish of draw line. Default value: 360.
14937 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
14938 * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
14939 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
14940 * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
14941 * @author Nicola Asuni
14942 * @public
14943 * @since 2.1.000 (2008-01-08)
14945 public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
14946 if ($this->empty_string($ry) OR ($ry == 0)) {
14947 $ry = $rx;
14949 if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
14950 $this->SetFillColorArray($fill_color);
14952 $op = $this->getPathPaintOperator($style);
14953 if ($op == 'f') {
14954 $line_style = array();
14956 if ($line_style) {
14957 $this->SetLineStyle($line_style);
14959 $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
14960 $this->_out($op);
14964 * Append an elliptical arc to the current path.
14965 * An ellipse is formed from n Bezier curves.
14966 * @param $xc (float) Abscissa of center point.
14967 * @param $yc (float) Ordinate of center point.
14968 * @param $rx (float) Horizontal radius.
14969 * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
14970 * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
14971 * @param $angs: (float) Angle start of draw line. Default value: 0.
14972 * @param $angf: (float) Angle finish of draw line. Default value: 360.
14973 * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
14974 * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
14975 * @param $startpoint (boolean) if true output a starting point.
14976 * @param $ccw (boolean) if true draws in counter-clockwise.
14977 * @param $svg (boolean) if true the angles are in svg mode (already calculated).
14978 * @return array bounding box coordinates (x min, y min, x max, y max)
14979 * @author Nicola Asuni
14980 * @protected
14981 * @since 4.9.019 (2010-04-26)
14983 protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
14984 $k = $this->k;
14985 if ($nc < 2) {
14986 $nc = 2;
14988 $xmin = 2147483647;
14989 $ymin = 2147483647;
14990 $xmax = 0;
14991 $ymax = 0;
14992 if ($pie) {
14993 // center of the arc
14994 $this->_outPoint($xc, $yc);
14996 $xang = deg2rad((float) $xang);
14997 $angs = deg2rad((float) $angs);
14998 $angf = deg2rad((float) $angf);
14999 if ($svg) {
15000 $as = $angs;
15001 $af = $angf;
15002 } else {
15003 $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
15004 $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
15006 if ($as < 0) {
15007 $as += (2 * M_PI);
15009 if ($af < 0) {
15010 $af += (2 * M_PI);
15012 if ($ccw AND ($as > $af)) {
15013 // reverse rotation
15014 $as -= (2 * M_PI);
15015 } elseif (!$ccw AND ($as < $af)) {
15016 // reverse rotation
15017 $af -= (2 * M_PI);
15019 $total_angle = ($af - $as);
15020 if ($nc < 2) {
15021 $nc = 2;
15023 // total arcs to draw
15024 $nc *= (2 * abs($total_angle) / M_PI);
15025 $nc = round($nc) + 1;
15026 // angle of each arc
15027 $arcang = ($total_angle / $nc);
15028 // center point in PDF coordinates
15029 $x0 = $xc;
15030 $y0 = ($this->h - $yc);
15031 // starting angle
15032 $ang = $as;
15033 $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
15034 $cos_xang = cos($xang);
15035 $sin_xang = sin($xang);
15036 $cos_ang = cos($ang);
15037 $sin_ang = sin($ang);
15038 // first arc point
15039 $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
15040 $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
15041 // first Bezier control point
15042 $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
15043 $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
15044 if ($pie) {
15045 // line from center to arc starting point
15046 $this->_outLine($px1, $this->h - $py1);
15047 } elseif ($startpoint) {
15048 // arc starting point
15049 $this->_outPoint($px1, $this->h - $py1);
15051 // draw arcs
15052 for ($i = 1; $i <= $nc; ++$i) {
15053 // starting angle
15054 $ang = $as + ($i * $arcang);
15055 if ($i == $nc) {
15056 $ang = $af;
15058 $cos_ang = cos($ang);
15059 $sin_ang = sin($ang);
15060 // second arc point
15061 $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
15062 $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
15063 // second Bezier control point
15064 $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
15065 $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
15066 // draw arc
15067 $cx1 = ($px1 + $qx1);
15068 $cy1 = ($this->h - ($py1 + $qy1));
15069 $cx2 = ($px2 - $qx2);
15070 $cy2 = ($this->h - ($py2 - $qy2));
15071 $cx3 = $px2;
15072 $cy3 = ($this->h - $py2);
15073 $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
15074 // get bounding box coordinates
15075 $xmin = min($xmin, $cx1, $cx2, $cx3);
15076 $ymin = min($ymin, $cy1, $cy2, $cy3);
15077 $xmax = max($xmax, $cx1, $cx2, $cx3);
15078 $ymax = max($ymax, $cy1, $cy2, $cy3);
15079 // move to next point
15080 $px1 = $px2;
15081 $py1 = $py2;
15082 $qx1 = $qx2;
15083 $qy1 = $qy2;
15085 if ($pie) {
15086 $this->_outLine($xc, $yc);
15087 // get bounding box coordinates
15088 $xmin = min($xmin, $xc);
15089 $ymin = min($ymin, $yc);
15090 $xmax = max($xmax, $xc);
15091 $ymax = max($ymax, $yc);
15093 return array($xmin, $ymin, $xmax, $ymax);
15097 * Draws a circle.
15098 * A circle is formed from n Bezier curves.
15099 * @param $x0 (float) Abscissa of center point.
15100 * @param $y0 (float) Ordinate of center point.
15101 * @param $r (float) Radius.
15102 * @param $angstr: (float) Angle start of draw line. Default value: 0.
15103 * @param $angend: (float) Angle finish of draw line. Default value: 360.
15104 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15105 * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
15106 * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
15107 * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
15108 * @public
15109 * @since 2.1.000 (2008-01-08)
15111 public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
15112 $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
15116 * Draws a polygonal line
15117 * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
15118 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15119 * @param $line_style (array) Line style of polygon. Array with keys among the following:
15120 * <ul>
15121 * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
15122 * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
15123 * </ul>
15124 * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
15125 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
15126 * @since 4.8.003 (2009-09-15)
15127 * @public
15129 public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
15130 $this->Polygon($p, $style, $line_style, $fill_color, false);
15134 * Draws a polygon.
15135 * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
15136 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15137 * @param $line_style (array) Line style of polygon. Array with keys among the following:
15138 * <ul>
15139 * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
15140 * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
15141 * </ul>
15142 * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
15143 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
15144 * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
15145 * @public
15146 * @since 2.1.000 (2008-01-08)
15148 public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
15149 $nc = count($p); // number of coordinates
15150 $np = $nc / 2; // number of points
15151 if ($closed) {
15152 // close polygon by adding the first 2 points at the end (one line)
15153 for ($i = 0; $i < 4; ++$i) {
15154 $p[$nc + $i] = $p[$i];
15156 // copy style for the last added line
15157 if (isset($line_style[0])) {
15158 $line_style[$np] = $line_style[0];
15160 $nc += 4;
15162 if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
15163 $this->SetFillColorArray($fill_color);
15165 $op = $this->getPathPaintOperator($style);
15166 if ($op == 'f') {
15167 $line_style = array();
15169 $draw = true;
15170 if ($line_style) {
15171 if (isset($line_style['all'])) {
15172 $this->SetLineStyle($line_style['all']);
15173 } else {
15174 $draw = false;
15175 if ($op == 'B') {
15176 // draw fill
15177 $op = 'f';
15178 $this->_outPoint($p[0], $p[1]);
15179 for ($i = 2; $i < $nc; $i = $i + 2) {
15180 $this->_outLine($p[$i], $p[$i + 1]);
15182 $this->_out($op);
15184 // draw outline
15185 $this->_outPoint($p[0], $p[1]);
15186 for ($i = 2; $i < $nc; $i = $i + 2) {
15187 $line_num = ($i / 2) - 1;
15188 if (isset($line_style[$line_num])) {
15189 if ($line_style[$line_num] != 0) {
15190 if (is_array($line_style[$line_num])) {
15191 $this->_out('S');
15192 $this->SetLineStyle($line_style[$line_num]);
15193 $this->_outPoint($p[$i - 2], $p[$i - 1]);
15194 $this->_outLine($p[$i], $p[$i + 1]);
15195 $this->_out('S');
15196 $this->_outPoint($p[$i], $p[$i + 1]);
15197 } else {
15198 $this->_outLine($p[$i], $p[$i + 1]);
15201 } else {
15202 $this->_outLine($p[$i], $p[$i + 1]);
15205 $this->_out($op);
15208 if ($draw) {
15209 $this->_outPoint($p[0], $p[1]);
15210 for ($i = 2; $i < $nc; $i = $i + 2) {
15211 $this->_outLine($p[$i], $p[$i + 1]);
15213 $this->_out($op);
15218 * Draws a regular polygon.
15219 * @param $x0 (float) Abscissa of center point.
15220 * @param $y0 (float) Ordinate of center point.
15221 * @param $r: (float) Radius of inscribed circle.
15222 * @param $ns (integer) Number of sides.
15223 * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
15224 * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
15225 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15226 * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
15227 * <ul>
15228 * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
15229 * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
15230 * </ul>
15231 * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
15232 * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
15233 * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
15234 * <ul>
15235 * <li>D or empty string: Draw (default).</li>
15236 * <li>F: Fill.</li>
15237 * <li>DF or FD: Draw and fill.</li>
15238 * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
15239 * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
15240 * </ul>
15241 * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
15242 * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
15243 * @public
15244 * @since 2.1.000 (2008-01-08)
15246 public function RegularPolygon($x0, $y0, $r, $ns, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
15247 if (3 > $ns) {
15248 $ns = 3;
15250 if ($draw_circle) {
15251 $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
15253 $p = array();
15254 for ($i = 0; $i < $ns; ++$i) {
15255 $a = $angle + ($i * 360 / $ns);
15256 $a_rad = deg2rad((float) $a);
15257 $p[] = $x0 + ($r * sin($a_rad));
15258 $p[] = $y0 + ($r * cos($a_rad));
15260 $this->Polygon($p, $style, $line_style, $fill_color);
15264 * Draws a star polygon
15265 * @param $x0 (float) Abscissa of center point.
15266 * @param $y0 (float) Ordinate of center point.
15267 * @param $r (float) Radius of inscribed circle.
15268 * @param $nv (integer) Number of vertices.
15269 * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
15270 * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
15271 * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
15272 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15273 * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
15274 * <ul>
15275 * <li>all: Line style of all sides. Array like for
15276 * SetLineStyle().</li>
15277 * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
15278 * </ul>
15279 * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
15280 * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
15281 * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
15282 * <ul>
15283 * <li>D or empty string: Draw (default).</li>
15284 * <li>F: Fill.</li>
15285 * <li>DF or FD: Draw and fill.</li>
15286 * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
15287 * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
15288 * </ul>
15289 * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
15290 * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
15291 * @public
15292 * @since 2.1.000 (2008-01-08)
15294 public function StarPolygon($x0, $y0, $r, $nv, $ng, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
15295 if ($nv < 2) {
15296 $nv = 2;
15298 if ($draw_circle) {
15299 $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
15301 $p2 = array();
15302 $visited = array();
15303 for ($i = 0; $i < $nv; ++$i) {
15304 $a = $angle + ($i * 360 / $nv);
15305 $a_rad = deg2rad((float) $a);
15306 $p2[] = $x0 + ($r * sin($a_rad));
15307 $p2[] = $y0 + ($r * cos($a_rad));
15308 $visited[] = false;
15310 $p = array();
15311 $i = 0;
15312 do {
15313 $p[] = $p2[$i * 2];
15314 $p[] = $p2[($i * 2) + 1];
15315 $visited[$i] = true;
15316 $i += $ng;
15317 $i %= $nv;
15318 } while (!$visited[$i]);
15319 $this->Polygon($p, $style, $line_style, $fill_color);
15323 * Draws a rounded rectangle.
15324 * @param $x (float) Abscissa of upper-left corner.
15325 * @param $y (float) Ordinate of upper-left corner.
15326 * @param $w (float) Width.
15327 * @param $h (float) Height.
15328 * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
15329 * @param $round_corner (string) Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
15330 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15331 * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
15332 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
15333 * @public
15334 * @since 2.1.000 (2008-01-08)
15336 public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
15337 $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
15341 * Draws a rounded rectangle.
15342 * @param $x (float) Abscissa of upper-left corner.
15343 * @param $y (float) Ordinate of upper-left corner.
15344 * @param $w (float) Width.
15345 * @param $h (float) Height.
15346 * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
15347 * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
15348 * @param $round_corner (string) Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
15349 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
15350 * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
15351 * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
15352 * @public
15353 * @since 4.9.019 (2010-04-22)
15355 public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
15356 if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
15357 // Not rounded
15358 $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
15359 return;
15361 // Rounded
15362 if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
15363 $this->SetFillColorArray($fill_color);
15365 $op = $this->getPathPaintOperator($style);
15366 if ($op == 'f') {
15367 $border_style = array();
15369 if ($border_style) {
15370 $this->SetLineStyle($border_style);
15372 $MyArc = 4 / 3 * (sqrt(2) - 1);
15373 $this->_outPoint($x + $rx, $y);
15374 $xc = $x + $w - $rx;
15375 $yc = $y + $ry;
15376 $this->_outLine($xc, $y);
15377 if ($round_corner[0]) {
15378 $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
15379 } else {
15380 $this->_outLine($x + $w, $y);
15382 $xc = $x + $w - $rx;
15383 $yc = $y + $h - $ry;
15384 $this->_outLine($x + $w, $yc);
15385 if ($round_corner[1]) {
15386 $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
15387 } else {
15388 $this->_outLine($x + $w, $y + $h);
15390 $xc = $x + $rx;
15391 $yc = $y + $h - $ry;
15392 $this->_outLine($xc, $y + $h);
15393 if ($round_corner[2]) {
15394 $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
15395 } else {
15396 $this->_outLine($x, $y + $h);
15398 $xc = $x + $rx;
15399 $yc = $y + $ry;
15400 $this->_outLine($x, $yc);
15401 if ($round_corner[3]) {
15402 $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
15403 } else {
15404 $this->_outLine($x, $y);
15405 $this->_outLine($x + $rx, $y);
15407 $this->_out($op);
15411 * Draws a grahic arrow.
15412 * @param $x0 (float) Abscissa of first point.
15413 * @param $y0 (float) Ordinate of first point.
15414 * @param $x1 (float) Abscissa of second point.
15415 * @param $y1 (float) Ordinate of second point.
15416 * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
15417 * @param $arm_size (float) length of arrowhead arms
15418 * @param $arm_angle (int) angle between an arm and the shaft
15419 * @author Piotr Galecki, Nicola Asuni, Andy Meier
15420 * @since 4.6.018 (2009-07-10)
15422 public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
15423 // getting arrow direction angle
15424 // 0 deg angle is when both arms go along X axis. angle grows clockwise.
15425 $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
15426 if ($dir_angle < 0) {
15427 $dir_angle += (2 * M_PI);
15429 $arm_angle = deg2rad($arm_angle);
15430 $sx1 = $x1;
15431 $sy1 = $y1;
15432 if ($head_style > 0) {
15433 // calculate the stopping point for the arrow shaft
15434 $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
15435 $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
15437 // main arrow line / shaft
15438 $this->Line($x0, $y0, $sx1, $sy1);
15439 // left arrowhead arm tip
15440 $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
15441 $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
15442 // right arrowhead arm tip
15443 $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
15444 $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
15445 $mode = 'D';
15446 $style = array();
15447 switch ($head_style) {
15448 case 0: {
15449 // draw only arrowhead arms
15450 $mode = 'D';
15451 $style = array(1, 1, 0);
15452 break;
15454 case 1: {
15455 // draw closed arrowhead, but no fill
15456 $mode = 'D';
15457 break;
15459 case 2: {
15460 // closed and filled arrowhead
15461 $mode = 'DF';
15462 break;
15464 case 3: {
15465 // filled arrowhead
15466 $mode = 'F';
15467 break;
15470 $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
15473 // END GRAPHIC FUNCTIONS SECTION -----------------------
15475 // BIDIRECTIONAL TEXT SECTION --------------------------
15478 * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
15479 * @param $str (string) string to manipulate.
15480 * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
15481 * @param $forcertl (bool) if true forces RTL text direction
15482 * @return string
15483 * @protected
15484 * @author Nicola Asuni
15485 * @since 2.1.000 (2008-01-08)
15487 protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
15488 return $this->utf8StrArrRev($this->UTF8StringToArray($str), $str, $setbom, $forcertl);
15492 * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
15493 * @param $arr (array) array of unicode values.
15494 * @param $str (string) string to manipulate (or empty value).
15495 * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
15496 * @param $forcertl (bool) if true forces RTL text direction
15497 * @return string
15498 * @protected
15499 * @author Nicola Asuni
15500 * @since 4.9.000 (2010-03-27)
15502 protected function utf8StrArrRev($arr, $str='', $setbom=false, $forcertl=false) {
15503 return $this->arrUTF8ToUTF16BE($this->utf8Bidi($arr, $str, $forcertl), $setbom);
15507 * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
15508 * @param $ta (array) array of characters composing the string.
15509 * @param $str (string) string to process
15510 * @param $forcertl (bool) if 'R' forces RTL, if 'L' forces LTR
15511 * @return array of unicode chars
15512 * @author Nicola Asuni
15513 * @protected
15514 * @since 2.4.000 (2008-03-06)
15516 protected function utf8Bidi($ta, $str='', $forcertl=false) {
15517 // paragraph embedding level
15518 $pel = 0;
15519 // max level
15520 $maxlevel = 0;
15521 if ($this->empty_string($str)) {
15522 // create string from array
15523 $str = $this->UTF8ArrSubString($ta);
15525 // check if string contains arabic text
15526 if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $str)) {
15527 $arabic = true;
15528 } else {
15529 $arabic = false;
15531 // check if string contains RTL text
15532 if (!($forcertl OR $arabic OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $str))) {
15533 return $ta;
15536 // get number of chars
15537 $numchars = count($ta);
15539 if ($forcertl == 'R') {
15540 $pel = 1;
15541 } elseif ($forcertl == 'L') {
15542 $pel = 0;
15543 } else {
15544 // P2. In each paragraph, find the first character of type L, AL, or R.
15545 // P3. If a character is found in P2 and it is of type AL or R, then set the paragraph embedding level to one; otherwise, set it to zero.
15546 for ($i=0; $i < $numchars; ++$i) {
15547 $type = $this->unicode->uni_type[$ta[$i]];
15548 if ($type == 'L') {
15549 $pel = 0;
15550 break;
15551 } elseif (($type == 'AL') OR ($type == 'R')) {
15552 $pel = 1;
15553 break;
15558 // Current Embedding Level
15559 $cel = $pel;
15560 // directional override status
15561 $dos = 'N';
15562 $remember = array();
15563 // start-of-level-run
15564 $sor = $pel % 2 ? 'R' : 'L';
15565 $eor = $sor;
15567 // Array of characters data
15568 $chardata = Array();
15570 // X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
15571 // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
15572 for ($i=0; $i < $numchars; ++$i) {
15573 if ($ta[$i] == $this->unicode->uni_RLE) {
15574 // X2. With each RLE, compute the least greater odd embedding level.
15575 // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
15576 // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
15577 $next_level = $cel + ($cel % 2) + 1;
15578 if ($next_level < 62) {
15579 $remember[] = array('num' => $this->unicode->uni_RLE, 'cel' => $cel, 'dos' => $dos);
15580 $cel = $next_level;
15581 $dos = 'N';
15582 $sor = $eor;
15583 $eor = $cel % 2 ? 'R' : 'L';
15585 } elseif ($ta[$i] == $this->unicode->uni_LRE) {
15586 // X3. With each LRE, compute the least greater even embedding level.
15587 // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
15588 // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
15589 $next_level = $cel + 2 - ($cel % 2);
15590 if ( $next_level < 62 ) {
15591 $remember[] = array('num' => $this->unicode->uni_LRE, 'cel' => $cel, 'dos' => $dos);
15592 $cel = $next_level;
15593 $dos = 'N';
15594 $sor = $eor;
15595 $eor = $cel % 2 ? 'R' : 'L';
15597 } elseif ($ta[$i] == $this->unicode->uni_RLO) {
15598 // X4. With each RLO, compute the least greater odd embedding level.
15599 // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
15600 // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
15601 $next_level = $cel + ($cel % 2) + 1;
15602 if ($next_level < 62) {
15603 $remember[] = array('num' => $this->unicode->uni_RLO, 'cel' => $cel, 'dos' => $dos);
15604 $cel = $next_level;
15605 $dos = 'R';
15606 $sor = $eor;
15607 $eor = $cel % 2 ? 'R' : 'L';
15609 } elseif ($ta[$i] == $this->unicode->uni_LRO) {
15610 // X5. With each LRO, compute the least greater even embedding level.
15611 // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
15612 // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
15613 $next_level = $cel + 2 - ($cel % 2);
15614 if ( $next_level < 62 ) {
15615 $remember[] = array('num' => $this->unicode->uni_LRO, 'cel' => $cel, 'dos' => $dos);
15616 $cel = $next_level;
15617 $dos = 'L';
15618 $sor = $eor;
15619 $eor = $cel % 2 ? 'R' : 'L';
15621 } elseif ($ta[$i] == $this->unicode->uni_PDF) {
15622 // X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
15623 if (count($remember)) {
15624 $last = count($remember ) - 1;
15625 if (($remember[$last]['num'] == $this->unicode->uni_RLE) OR
15626 ($remember[$last]['num'] == $this->unicode->uni_LRE) OR
15627 ($remember[$last]['num'] == $this->unicode->uni_RLO) OR
15628 ($remember[$last]['num'] == $this->unicode->uni_LRO)) {
15629 $match = array_pop($remember);
15630 $cel = $match['cel'];
15631 $dos = $match['dos'];
15632 $sor = $eor;
15633 $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
15636 } elseif (($ta[$i] != $this->unicode->uni_RLE) AND
15637 ($ta[$i] != $this->unicode->uni_LRE) AND
15638 ($ta[$i] != $this->unicode->uni_RLO) AND
15639 ($ta[$i] != $this->unicode->uni_LRO) AND
15640 ($ta[$i] != $this->unicode->uni_PDF)) {
15641 // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
15642 // a. Set the level of the current character to the current embedding level.
15643 // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
15644 if ($dos != 'N') {
15645 $chardir = $dos;
15646 } else {
15647 if (isset($this->unicode->uni_type[$ta[$i]])) {
15648 $chardir = $this->unicode->uni_type[$ta[$i]];
15649 } else {
15650 $chardir = 'L';
15653 // stores string characters and other information
15654 $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
15656 } // end for each char
15658 // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
15659 // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
15660 // X10. The remaining rules are applied to each run of characters at the same level. For each run, determine the start-of-level-run (sor) and end-of-level-run (eor) type, either L or R. This depends on the higher of the two levels on either side of the boundary (at the start or end of the paragraph, the level of the 'other' run is the base embedding level). If the higher level is odd, the type is R; otherwise, it is L.
15662 // 3.3.3 Resolving Weak Types
15663 // Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
15664 // Nonspacing marks are now resolved based on the previous characters.
15665 $numchars = count($chardata);
15667 // W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
15668 $prevlevel = -1; // track level changes
15669 $levcount = 0; // counts consecutive chars at the same level
15670 for ($i=0; $i < $numchars; ++$i) {
15671 if ($chardata[$i]['type'] == 'NSM') {
15672 if ($levcount) {
15673 $chardata[$i]['type'] = $chardata[$i]['sor'];
15674 } elseif ($i > 0) {
15675 $chardata[$i]['type'] = $chardata[($i-1)]['type'];
15678 if ($chardata[$i]['level'] != $prevlevel) {
15679 $levcount = 0;
15680 } else {
15681 ++$levcount;
15683 $prevlevel = $chardata[$i]['level'];
15686 // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
15687 $prevlevel = -1;
15688 $levcount = 0;
15689 for ($i=0; $i < $numchars; ++$i) {
15690 if ($chardata[$i]['char'] == 'EN') {
15691 for ($j=$levcount; $j >= 0; $j--) {
15692 if ($chardata[$j]['type'] == 'AL') {
15693 $chardata[$i]['type'] = 'AN';
15694 } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
15695 break;
15699 if ($chardata[$i]['level'] != $prevlevel) {
15700 $levcount = 0;
15701 } else {
15702 ++$levcount;
15704 $prevlevel = $chardata[$i]['level'];
15707 // W3. Change all ALs to R.
15708 for ($i=0; $i < $numchars; ++$i) {
15709 if ($chardata[$i]['type'] == 'AL') {
15710 $chardata[$i]['type'] = 'R';
15714 // W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
15715 $prevlevel = -1;
15716 $levcount = 0;
15717 for ($i=0; $i < $numchars; ++$i) {
15718 if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
15719 if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
15720 $chardata[$i]['type'] = 'EN';
15721 } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
15722 $chardata[$i]['type'] = 'EN';
15723 } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
15724 $chardata[$i]['type'] = 'AN';
15727 if ($chardata[$i]['level'] != $prevlevel) {
15728 $levcount = 0;
15729 } else {
15730 ++$levcount;
15732 $prevlevel = $chardata[$i]['level'];
15735 // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
15736 $prevlevel = -1;
15737 $levcount = 0;
15738 for ($i=0; $i < $numchars; ++$i) {
15739 if ($chardata[$i]['type'] == 'ET') {
15740 if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
15741 $chardata[$i]['type'] = 'EN';
15742 } else {
15743 $j = $i+1;
15744 while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
15745 if ($chardata[$j]['type'] == 'EN') {
15746 $chardata[$i]['type'] = 'EN';
15747 break;
15748 } elseif ($chardata[$j]['type'] != 'ET') {
15749 break;
15751 ++$j;
15755 if ($chardata[$i]['level'] != $prevlevel) {
15756 $levcount = 0;
15757 } else {
15758 ++$levcount;
15760 $prevlevel = $chardata[$i]['level'];
15763 // W6. Otherwise, separators and terminators change to Other Neutral.
15764 $prevlevel = -1;
15765 $levcount = 0;
15766 for ($i=0; $i < $numchars; ++$i) {
15767 if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
15768 $chardata[$i]['type'] = 'ON';
15770 if ($chardata[$i]['level'] != $prevlevel) {
15771 $levcount = 0;
15772 } else {
15773 ++$levcount;
15775 $prevlevel = $chardata[$i]['level'];
15778 //W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
15779 $prevlevel = -1;
15780 $levcount = 0;
15781 for ($i=0; $i < $numchars; ++$i) {
15782 if ($chardata[$i]['char'] == 'EN') {
15783 for ($j=$levcount; $j >= 0; $j--) {
15784 if ($chardata[$j]['type'] == 'L') {
15785 $chardata[$i]['type'] = 'L';
15786 } elseif ($chardata[$j]['type'] == 'R') {
15787 break;
15791 if ($chardata[$i]['level'] != $prevlevel) {
15792 $levcount = 0;
15793 } else {
15794 ++$levcount;
15796 $prevlevel = $chardata[$i]['level'];
15799 // N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
15800 $prevlevel = -1;
15801 $levcount = 0;
15802 for ($i=0; $i < $numchars; ++$i) {
15803 if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
15804 if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
15805 $chardata[$i]['type'] = 'L';
15806 } elseif (($chardata[$i]['type'] == 'N') AND
15807 (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
15808 (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
15809 $chardata[$i]['type'] = 'R';
15810 } elseif ($chardata[$i]['type'] == 'N') {
15811 // N2. Any remaining neutrals take the embedding direction
15812 $chardata[$i]['type'] = $chardata[$i]['sor'];
15814 } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
15815 // first char
15816 if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
15817 $chardata[$i]['type'] = 'L';
15818 } elseif (($chardata[$i]['type'] == 'N') AND
15819 (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
15820 (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
15821 $chardata[$i]['type'] = 'R';
15822 } elseif ($chardata[$i]['type'] == 'N') {
15823 // N2. Any remaining neutrals take the embedding direction
15824 $chardata[$i]['type'] = $chardata[$i]['sor'];
15826 } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
15827 //last char
15828 if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
15829 $chardata[$i]['type'] = 'L';
15830 } elseif (($chardata[$i]['type'] == 'N') AND
15831 (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
15832 (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
15833 $chardata[$i]['type'] = 'R';
15834 } elseif ($chardata[$i]['type'] == 'N') {
15835 // N2. Any remaining neutrals take the embedding direction
15836 $chardata[$i]['type'] = $chardata[$i]['sor'];
15838 } elseif ($chardata[$i]['type'] == 'N') {
15839 // N2. Any remaining neutrals take the embedding direction
15840 $chardata[$i]['type'] = $chardata[$i]['sor'];
15842 if ($chardata[$i]['level'] != $prevlevel) {
15843 $levcount = 0;
15844 } else {
15845 ++$levcount;
15847 $prevlevel = $chardata[$i]['level'];
15850 // I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
15851 // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
15852 for ($i=0; $i < $numchars; ++$i) {
15853 $odd = $chardata[$i]['level'] % 2;
15854 if ($odd) {
15855 if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
15856 $chardata[$i]['level'] += 1;
15858 } else {
15859 if ($chardata[$i]['type'] == 'R') {
15860 $chardata[$i]['level'] += 1;
15861 } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
15862 $chardata[$i]['level'] += 2;
15865 $maxlevel = max($chardata[$i]['level'],$maxlevel);
15868 // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
15869 // 1. Segment separators,
15870 // 2. Paragraph separators,
15871 // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
15872 // 4. Any sequence of white space characters at the end of the line.
15873 for ($i=0; $i < $numchars; ++$i) {
15874 if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
15875 $chardata[$i]['level'] = $pel;
15876 } elseif ($chardata[$i]['type'] == 'WS') {
15877 $j = $i+1;
15878 while ($j < $numchars) {
15879 if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
15880 (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
15881 $chardata[$i]['level'] = $pel;
15882 break;
15883 } elseif ($chardata[$j]['type'] != 'WS') {
15884 break;
15886 ++$j;
15891 // Arabic Shaping
15892 // Cursively connected scripts, such as Arabic or Syriac, require the selection of positional character shapes that depend on adjacent characters. Shaping is logically applied after the Bidirectional Algorithm is used and is limited to characters within the same directional run.
15893 if ($arabic) {
15894 $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
15895 $alfletter = array(1570,1571,1573,1575);
15896 $chardata2 = $chardata;
15897 $laaletter = false;
15898 $charAL = array();
15899 $x = 0;
15900 for ($i=0; $i < $numchars; ++$i) {
15901 if (($this->unicode->uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
15902 $charAL[$x] = $chardata[$i];
15903 $charAL[$x]['i'] = $i;
15904 $chardata[$i]['x'] = $x;
15905 ++$x;
15908 $numAL = $x;
15909 for ($i=0; $i < $numchars; ++$i) {
15910 $thischar = $chardata[$i];
15911 if ($i > 0) {
15912 $prevchar = $chardata[($i-1)];
15913 } else {
15914 $prevchar = false;
15916 if (($i+1) < $numchars) {
15917 $nextchar = $chardata[($i+1)];
15918 } else {
15919 $nextchar = false;
15921 if ($this->unicode->uni_type[$thischar['char']] == 'AL') {
15922 $x = $thischar['x'];
15923 if ($x > 0) {
15924 $prevchar = $charAL[($x-1)];
15925 } else {
15926 $prevchar = false;
15928 if (($x+1) < $numAL) {
15929 $nextchar = $charAL[($x+1)];
15930 } else {
15931 $nextchar = false;
15933 // if laa letter
15934 if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
15935 $arabicarr = $this->unicode->uni_laa_array;
15936 $laaletter = true;
15937 if ($x > 1) {
15938 $prevchar = $charAL[($x-2)];
15939 } else {
15940 $prevchar = false;
15942 } else {
15943 $arabicarr = $this->unicode->uni_arabicsubst;
15944 $laaletter = false;
15946 if (($prevchar !== false) AND ($nextchar !== false) AND
15947 (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
15948 (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
15949 ($prevchar['type'] == $thischar['type']) AND
15950 ($nextchar['type'] == $thischar['type']) AND
15951 ($nextchar['char'] != 1567)) {
15952 if (in_array($prevchar['char'], $endedletter)) {
15953 if (isset($arabicarr[$thischar['char']][2])) {
15954 // initial
15955 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
15957 } else {
15958 if (isset($arabicarr[$thischar['char']][3])) {
15959 // medial
15960 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
15963 } elseif (($nextchar !== false) AND
15964 (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
15965 ($nextchar['type'] == $thischar['type']) AND
15966 ($nextchar['char'] != 1567)) {
15967 if (isset($arabicarr[$chardata[$i]['char']][2])) {
15968 // initial
15969 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
15971 } elseif ((($prevchar !== false) AND
15972 (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
15973 ($prevchar['type'] == $thischar['type'])) OR
15974 (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
15975 // final
15976 if (($i > 1) AND ($thischar['char'] == 1607) AND
15977 ($chardata[$i-1]['char'] == 1604) AND
15978 ($chardata[$i-2]['char'] == 1604)) {
15979 //Allah Word
15980 // mark characters to delete with false
15981 $chardata2[$i-2]['char'] = false;
15982 $chardata2[$i-1]['char'] = false;
15983 $chardata2[$i]['char'] = 65010;
15984 } else {
15985 if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
15986 if (isset($arabicarr[$thischar['char']][0])) {
15987 // isolated
15988 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
15990 } else {
15991 if (isset($arabicarr[$thischar['char']][1])) {
15992 // final
15993 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
15997 } elseif (isset($arabicarr[$thischar['char']][0])) {
15998 // isolated
15999 $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
16001 // if laa letter
16002 if ($laaletter) {
16003 // mark characters to delete with false
16004 $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
16006 } // end if AL (Arabic Letter)
16007 } // end for each char
16009 * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
16010 * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
16012 for ($i = 0; $i < ($numchars-1); ++$i) {
16013 if (($chardata2[$i]['char'] == 1617) AND (isset($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])]))) {
16014 // check if the subtitution font is defined on current font
16015 if (isset($this->CurrentFont['cw'][($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])])])) {
16016 $chardata2[$i]['char'] = false;
16017 $chardata2[$i+1]['char'] = $this->unicode->uni_diacritics[($chardata2[$i+1]['char'])];
16021 // remove marked characters
16022 foreach ($chardata2 as $key => $value) {
16023 if ($value['char'] === false) {
16024 unset($chardata2[$key]);
16027 $chardata = array_values($chardata2);
16028 $numchars = count($chardata);
16029 unset($chardata2);
16030 unset($arabicarr);
16031 unset($laaletter);
16032 unset($charAL);
16035 // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
16036 for ($j=$maxlevel; $j > 0; $j--) {
16037 $ordarray = Array();
16038 $revarr = Array();
16039 $onlevel = false;
16040 for ($i=0; $i < $numchars; ++$i) {
16041 if ($chardata[$i]['level'] >= $j) {
16042 $onlevel = true;
16043 if (isset($this->unicode->uni_mirror[$chardata[$i]['char']])) {
16044 // L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true.
16045 $chardata[$i]['char'] = $this->unicode->uni_mirror[$chardata[$i]['char']];
16047 $revarr[] = $chardata[$i];
16048 } else {
16049 if ($onlevel) {
16050 $revarr = array_reverse($revarr);
16051 $ordarray = array_merge($ordarray, $revarr);
16052 $revarr = Array();
16053 $onlevel = false;
16055 $ordarray[] = $chardata[$i];
16058 if ($onlevel) {
16059 $revarr = array_reverse($revarr);
16060 $ordarray = array_merge($ordarray, $revarr);
16062 $chardata = $ordarray;
16065 $ordarray = array();
16066 for ($i=0; $i < $numchars; ++$i) {
16067 $ordarray[] = $chardata[$i]['char'];
16068 // store char values for subsetting
16069 $this->CurrentFont['subsetchars'][$chardata[$i]['char']] = true;
16071 // update font subsetchars
16072 $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
16073 return $ordarray;
16076 // END OF BIDIRECTIONAL TEXT SECTION -------------------
16079 * Encode a name object.
16080 * @param $name (string) Name object to encode.
16081 * @return (string) Encoded name object.
16082 * @protected
16083 * @author Nicola Asuni
16084 * @since 5.9.097 (2011-06-23)
16086 protected function encodeNameObject($name) {
16087 $escname = '';
16088 $length = strlen($name);
16089 for ($i = 0; $i < $length; ++$i) {
16090 $chr = $name{$i};
16091 if (preg_match('/[0-9a-zA-Z]/', $chr) == 1) {
16092 $escname .= $chr;
16093 } else {
16094 $escname .= sprintf('#%02X', ord($chr));
16097 return $escname;
16101 * Add a Named Destination.
16102 * NOTE: destination names are unique, so only last entry will be saved.
16103 * @param $name (string) Destination name.
16104 * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
16105 * @param $page (int) Target page number (leave empty for current page).
16106 * @return (string) Stripped named destination identifier or false in case of error.
16107 * @public
16108 * @author Christian Deligant, Nicola Asuni
16109 * @since 5.9.097 (2011-06-23)
16111 public function setDestination($name, $y=-1, $page='') {
16112 // remove unsupported characters
16113 $name = $this->encodeNameObject($name);
16114 if ($this->empty_string($name)) {
16115 return false;
16117 if ($y == -1) {
16118 $y = $this->GetY();
16120 if (empty($page)) {
16121 $page = $this->PageNo();
16122 if (empty($page)) {
16123 return;
16126 $this->dests[$name] = array('y' => $y, 'p' => $page);
16127 return $name;
16131 * Return the Named Destination array.
16132 * @return (array) Named Destination array.
16133 * @public
16134 * @author Nicola Asuni
16135 * @since 5.9.097 (2011-06-23)
16137 public function getDestination() {
16138 return $this->dests;
16142 * Create a javascript PDF string.
16143 * @protected
16144 * @author Johannes Güntert, Nicola Asuni
16145 * @since 5.9.098 (2011-06-23)
16147 protected function _putdests() {
16148 if (empty($this->dests)) {
16149 return;
16151 $this->n_dests = $this->_newobj();
16152 $out = ' <<';
16153 foreach($this->dests as $name => $o) {
16154 $out .= ' /'.$name.' '.sprintf('[%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($o['p'])], ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
16156 $out .= ' >>';
16157 $out .= "\n".'endobj';
16158 $this->_out($out);
16162 * Adds a bookmark - alias for Bookmark().
16163 * @param $txt (string) Bookmark description.
16164 * @param $level (int) Bookmark level (minimum value is 0).
16165 * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
16166 * @param $page (int) Target page number (leave empty for current page).
16167 * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
16168 * @param $color (array) RGB color array (values from 0 to 255).
16169 * @public
16171 public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
16172 $this->Bookmark($txt, $level, $y, $page, $style, $color);
16176 * Adds a bookmark.
16177 * @param $txt (string) Bookmark description.
16178 * @param $level (int) Bookmark level (minimum value is 0).
16179 * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
16180 * @param $page (int) Target page number (leave empty for current page).
16181 * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
16182 * @param $color (array) RGB color array (values from 0 to 255).
16183 * @public
16184 * @author Olivier Plathey, Nicola Asuni
16185 * @since 2.1.002 (2008-02-12)
16187 public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
16188 if ($level < 0) {
16189 $level = 0;
16191 if (isset($this->outlines[0])) {
16192 $lastoutline = end($this->outlines);
16193 $maxlevel = $lastoutline['l'] + 1;
16194 } else {
16195 $maxlevel = 0;
16197 if ($level > $maxlevel) {
16198 $level = $maxlevel;
16200 if ($y == -1) {
16201 $y = $this->GetY();
16203 if (empty($page)) {
16204 $page = $this->PageNo();
16205 if (empty($page)) {
16206 return;
16209 $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color);
16213 * Sort bookmarks for page and key.
16214 * @protected
16215 * @since 5.9.119 (2011-09-19)
16217 protected function sortBookmarks() {
16218 // get sorting columns
16219 $outline_p = array();
16220 $outline_y = array();
16221 foreach ($this->outlines as $key => $row) {
16222 $outline_p[$key] = $row['p'];
16223 $outline_k[$key] = $key;
16225 // sort outlines by page and original position
16226 array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
16230 * Create a bookmark PDF string.
16231 * @protected
16232 * @author Olivier Plathey, Nicola Asuni
16233 * @since 2.1.002 (2008-02-12)
16235 protected function _putbookmarks() {
16236 $nb = count($this->outlines);
16237 if ($nb == 0) {
16238 return;
16240 // sort bookmarks
16241 $this->sortBookmarks();
16242 $lru = array();
16243 $level = 0;
16244 foreach ($this->outlines as $i => $o) {
16245 if ($o['l'] > 0) {
16246 $parent = $lru[($o['l'] - 1)];
16247 //Set parent and last pointers
16248 $this->outlines[$i]['parent'] = $parent;
16249 $this->outlines[$parent]['last'] = $i;
16250 if ($o['l'] > $level) {
16251 //Level increasing: set first pointer
16252 $this->outlines[$parent]['first'] = $i;
16254 } else {
16255 $this->outlines[$i]['parent'] = $nb;
16257 if (($o['l'] <= $level) AND ($i > 0)) {
16258 //Set prev and next pointers
16259 $prev = $lru[$o['l']];
16260 $this->outlines[$prev]['next'] = $i;
16261 $this->outlines[$i]['prev'] = $prev;
16263 $lru[$o['l']] = $i;
16264 $level = $o['l'];
16266 //Outline items
16267 $n = $this->n + 1;
16268 $nltags = '/<br[\s]?\/>|<\/(blockquote|dd|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|p|pre|ul|tcpdf|table|tr|td)>/si';
16269 foreach ($this->outlines as $i => $o) {
16270 $oid = $this->_newobj();
16271 // covert HTML title to string
16272 $title = preg_replace($nltags, "\n", $o['t']);
16273 $title = preg_replace("/[\r]+/si", '', $title);
16274 $title = preg_replace("/[\n]+/si", "\n", $title);
16275 $title = strip_tags($title);
16276 $title = $this->stringTrim($title);
16277 $out = '<</Title '.$this->_textstring($title, $oid);
16278 $out .= ' /Parent '.($n + $o['parent']).' 0 R';
16279 if (isset($o['prev'])) {
16280 $out .= ' /Prev '.($n + $o['prev']).' 0 R';
16282 if (isset($o['next'])) {
16283 $out .= ' /Next '.($n + $o['next']).' 0 R';
16285 if (isset($o['first'])) {
16286 $out .= ' /First '.($n + $o['first']).' 0 R';
16288 if (isset($o['last'])) {
16289 $out .= ' /Last '.($n + $o['last']).' 0 R';
16291 if (isset($this->page_obj_id[($o['p'])])) {
16292 $out .= ' '.sprintf('/Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($o['p'])], ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
16294 // set font style
16295 $style = 0;
16296 if (!empty($o['s'])) {
16297 // bold
16298 if (strpos($o['s'], 'B') !== false) {
16299 $style |= 2;
16301 // oblique
16302 if (strpos($o['s'], 'I') !== false) {
16303 $style |= 1;
16306 $out .= sprintf(' /F %d', $style);
16307 // set bookmark color
16308 if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
16309 $color = array_values($o['c']);
16310 $out .= sprintf(' /C [%.3F %.3F %.3F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
16311 } else {
16312 // black
16313 $out .= ' /C [0.0 0.0 0.0]';
16315 $out .= ' /Count 0'; // normally closed item
16316 $out .= ' >>';
16317 $out .= "\n".'endobj';
16318 $this->_out($out);
16320 //Outline root
16321 $this->OutlineRoot = $this->_newobj();
16322 $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
16325 // --- JAVASCRIPT ------------------------------------------------------
16328 * Adds a javascript
16329 * @param $script (string) Javascript code
16330 * @public
16331 * @author Johannes Güntert, Nicola Asuni
16332 * @since 2.1.002 (2008-02-12)
16334 public function IncludeJS($script) {
16335 $this->javascript .= $script;
16339 * Adds a javascript object and return object ID
16340 * @param $script (string) Javascript code
16341 * @param $onload (boolean) if true executes this object when opening the document
16342 * @return int internal object ID
16343 * @public
16344 * @author Nicola Asuni
16345 * @since 4.8.000 (2009-09-07)
16347 public function addJavascriptObject($script, $onload=false) {
16348 if ($this->pdfa_mode) {
16349 // javascript is not allowed in PDF/A mode
16350 return false;
16352 ++$this->n;
16353 $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
16354 return $this->n;
16358 * Create a javascript PDF string.
16359 * @protected
16360 * @author Johannes Güntert, Nicola Asuni
16361 * @since 2.1.002 (2008-02-12)
16363 protected function _putjavascript() {
16364 if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
16365 return;
16367 if (strpos($this->javascript, 'this.addField') > 0) {
16368 if (!$this->ur['enabled']) {
16369 //$this->setUserRights();
16371 // the following two lines are used to avoid form fields duplication after saving
16372 // The addField method only works when releasing user rights (UR3)
16373 $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
16374 $jsb = "getField('tcpdfdocsaved').value='saved';";
16375 $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
16377 $this->n_js = $this->_newobj();
16378 $out = ' << /Names [';
16379 if (!empty($this->javascript)) {
16380 $out .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
16382 if (!empty($this->js_objects)) {
16383 foreach ($this->js_objects as $key => $val) {
16384 if ($val['onload']) {
16385 $out .= ' (JS'.$key.') '.$key.' 0 R';
16389 $out .= ' ] >>';
16390 $out .= "\n".'endobj';
16391 $this->_out($out);
16392 // default Javascript object
16393 if (!empty($this->javascript)) {
16394 $obj_id = $this->_newobj();
16395 $out = '<< /S /JavaScript';
16396 $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
16397 $out .= ' >>';
16398 $out .= "\n".'endobj';
16399 $this->_out($out);
16401 // additional Javascript objects
16402 if (!empty($this->js_objects)) {
16403 foreach ($this->js_objects as $key => $val) {
16404 $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
16405 $this->_out($out);
16411 * Convert color to javascript color.
16412 * @param $color (string) color name or "#RRGGBB"
16413 * @protected
16414 * @author Denis Van Nuffelen, Nicola Asuni
16415 * @since 2.1.002 (2008-02-12)
16417 protected function _JScolor($color) {
16418 static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
16419 if (substr($color,0,1) == '#') {
16420 return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
16422 if (!in_array($color,$aColors)) {
16423 $this->Error('Invalid color: '.$color);
16425 return 'color.'.$color;
16429 * Adds a javascript form field.
16430 * @param $type (string) field type
16431 * @param $name (string) field name
16432 * @param $x (int) horizontal position
16433 * @param $y (int) vertical position
16434 * @param $w (int) width
16435 * @param $h (int) height
16436 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16437 * @protected
16438 * @author Denis Van Nuffelen, Nicola Asuni
16439 * @since 2.1.002 (2008-02-12)
16441 protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
16442 if ($this->rtl) {
16443 $x = $x - $w;
16445 // the followind avoid fields duplication after saving the document
16446 $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
16447 $k = $this->k;
16448 $this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%u,[%.2F,%.2F,%.2F,%.2F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
16449 $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
16450 while (list($key, $val) = each($prop)) {
16451 if (strcmp(substr($key, -5), 'Color') == 0) {
16452 $val = $this->_JScolor($val);
16453 } else {
16454 $val = "'".$val."'";
16456 $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
16458 if ($this->rtl) {
16459 $this->x -= $w;
16460 } else {
16461 $this->x += $w;
16463 $this->javascript .= '}';
16466 // --- FORM FIELDS -----------------------------------------------------
16469 * Convert JavaScript form fields properties array to Annotation Properties array.
16470 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16471 * @return array of annotation properties
16472 * @protected
16473 * @author Nicola Asuni
16474 * @since 4.8.000 (2009-09-06)
16476 protected function getAnnotOptFromJSProp($prop) {
16477 if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
16478 // the annotation options area lready defined
16479 return $prop['aopt'];
16481 $opt = array(); // value to be returned
16482 // alignment: Controls how the text is laid out within the text field.
16483 if (isset($prop['alignment'])) {
16484 switch ($prop['alignment']) {
16485 case 'left': {
16486 $opt['q'] = 0;
16487 break;
16489 case 'center': {
16490 $opt['q'] = 1;
16491 break;
16493 case 'right': {
16494 $opt['q'] = 2;
16495 break;
16497 default: {
16498 $opt['q'] = ($this->rtl)?2:0;
16499 break;
16503 // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
16504 if (isset($prop['lineWidth'])) {
16505 $linewidth = intval($prop['lineWidth']);
16506 } else {
16507 $linewidth = 1;
16509 // borderStyle: The border style for a field.
16510 if (isset($prop['borderStyle'])) {
16511 switch ($prop['borderStyle']) {
16512 case 'border.d':
16513 case 'dashed': {
16514 $opt['border'] = array(0, 0, $linewidth, array(3, 2));
16515 $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
16516 break;
16518 case 'border.b':
16519 case 'beveled': {
16520 $opt['border'] = array(0, 0, $linewidth);
16521 $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
16522 break;
16524 case 'border.i':
16525 case 'inset': {
16526 $opt['border'] = array(0, 0, $linewidth);
16527 $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
16528 break;
16530 case 'border.u':
16531 case 'underline': {
16532 $opt['border'] = array(0, 0, $linewidth);
16533 $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
16534 break;
16536 case 'border.s':
16537 case 'solid': {
16538 $opt['border'] = array(0, 0, $linewidth);
16539 $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
16540 break;
16542 default: {
16543 break;
16547 if (isset($prop['border']) AND is_array($prop['border'])) {
16548 $opt['border'] = $prop['border'];
16550 if (!isset($opt['mk'])) {
16551 $opt['mk'] = array();
16553 if (!isset($opt['mk']['if'])) {
16554 $opt['mk']['if'] = array();
16556 $opt['mk']['if']['a'] = array(0.5, 0.5);
16557 // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
16558 if (isset($prop['buttonAlignX'])) {
16559 $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
16561 // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
16562 if (isset($prop['buttonAlignY'])) {
16563 $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
16565 // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
16566 if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
16567 $opt['mk']['if']['fb'] = true;
16569 // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
16570 if (isset($prop['buttonScaleHow'])) {
16571 switch ($prop['buttonScaleHow']) {
16572 case 'scaleHow.proportional': {
16573 $opt['mk']['if']['s'] = 'P';
16574 break;
16576 case 'scaleHow.anamorphic': {
16577 $opt['mk']['if']['s'] = 'A';
16578 break;
16582 // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
16583 if (isset($prop['buttonScaleWhen'])) {
16584 switch ($prop['buttonScaleWhen']) {
16585 case 'scaleWhen.always': {
16586 $opt['mk']['if']['sw'] = 'A';
16587 break;
16589 case 'scaleWhen.never': {
16590 $opt['mk']['if']['sw'] = 'N';
16591 break;
16593 case 'scaleWhen.tooBig': {
16594 $opt['mk']['if']['sw'] = 'B';
16595 break;
16597 case 'scaleWhen.tooSmall': {
16598 $opt['mk']['if']['sw'] = 'S';
16599 break;
16603 // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
16604 if (isset($prop['buttonPosition'])) {
16605 switch ($prop['buttonPosition']) {
16606 case 0:
16607 case 'position.textOnly': {
16608 $opt['mk']['tp'] = 0;
16609 break;
16611 case 1:
16612 case 'position.iconOnly': {
16613 $opt['mk']['tp'] = 1;
16614 break;
16616 case 2:
16617 case 'position.iconTextV': {
16618 $opt['mk']['tp'] = 2;
16619 break;
16621 case 3:
16622 case 'position.textIconV': {
16623 $opt['mk']['tp'] = 3;
16624 break;
16626 case 4:
16627 case 'position.iconTextH': {
16628 $opt['mk']['tp'] = 4;
16629 break;
16631 case 5:
16632 case 'position.textIconH': {
16633 $opt['mk']['tp'] = 5;
16634 break;
16636 case 6:
16637 case 'position.overlay': {
16638 $opt['mk']['tp'] = 6;
16639 break;
16643 // fillColor: Specifies the background color for a field.
16644 if (isset($prop['fillColor'])) {
16645 if (is_array($prop['fillColor'])) {
16646 $opt['mk']['bg'] = $prop['fillColor'];
16647 } else {
16648 $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
16651 // strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
16652 if (isset($prop['strokeColor'])) {
16653 if (is_array($prop['strokeColor'])) {
16654 $opt['mk']['bc'] = $prop['strokeColor'];
16655 } else {
16656 $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
16659 // rotation: The rotation of a widget in counterclockwise increments.
16660 if (isset($prop['rotation'])) {
16661 $opt['mk']['r'] = $prop['rotation'];
16663 // charLimit: Limits the number of characters that a user can type into a text field.
16664 if (isset($prop['charLimit'])) {
16665 $opt['maxlen'] = intval($prop['charLimit']);
16667 if (!isset($ff)) {
16668 $ff = 0; // default value
16670 // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
16671 if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
16672 $ff += 1 << 0;
16674 // required: Specifies whether a field requires a value.
16675 if (isset($prop['required']) AND ($prop['required'] == 'true')) {
16676 $ff += 1 << 1;
16678 // multiline: Controls how text is wrapped within the field.
16679 if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
16680 $ff += 1 << 12;
16682 // password: Specifies whether the field should display asterisks when data is entered in the field.
16683 if (isset($prop['password']) AND ($prop['password'] == 'true')) {
16684 $ff += 1 << 13;
16686 // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
16687 if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
16688 $ff += 1 << 14;
16690 // Radio: If set, the field is a set of radio buttons.
16691 if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
16692 $ff += 1 << 15;
16694 // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
16695 if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
16696 $ff += 1 << 16;
16698 // Combo: If set, the field is a combo box; if clear, the field is a list box.
16699 if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
16700 $ff += 1 << 17;
16702 // editable: Controls whether a combo box is editable.
16703 if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
16704 $ff += 1 << 18;
16706 // Sort: If set, the field's option items shall be sorted alphabetically.
16707 if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
16708 $ff += 1 << 19;
16710 // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
16711 if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
16712 $ff += 1 << 20;
16714 // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
16715 if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
16716 $ff += 1 << 21;
16718 // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
16719 if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
16720 $ff += 1 << 22;
16722 // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
16723 if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
16724 $ff += 1 << 23;
16726 // comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
16727 if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
16728 $ff += 1 << 24;
16730 // radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
16731 if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
16732 $ff += 1 << 25;
16734 // richText: If true, the field allows rich text formatting.
16735 if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
16736 $ff += 1 << 25;
16738 // commitOnSelChange: Controls whether a field value is committed after a selection change.
16739 if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
16740 $ff += 1 << 26;
16742 $opt['ff'] = $ff;
16743 // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
16744 if (isset($prop['defaultValue'])) {
16745 $opt['dv'] = $prop['defaultValue'];
16747 $f = 4; // default value for annotation flags
16748 // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
16749 if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
16750 $f += 1 << 6;
16752 // display: Controls whether the field is hidden or visible on screen and in print.
16753 if (isset($prop['display'])) {
16754 if ($prop['display'] == 'display.visible') {
16756 } elseif ($prop['display'] == 'display.hidden') {
16757 $f += 1 << 1;
16758 } elseif ($prop['display'] == 'display.noPrint') {
16759 $f -= 1 << 2;
16760 } elseif ($prop['display'] == 'display.noView') {
16761 $f += 1 << 5;
16764 $opt['f'] = $f;
16765 // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
16766 if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
16767 $opt['i'] = $prop['currentValueIndices'];
16769 // value: The value of the field data that the user has entered.
16770 if (isset($prop['value'])) {
16771 if (is_array($prop['value'])) {
16772 $opt['opt'] = array();
16773 foreach ($prop['value'] AS $key => $optval) {
16774 // exportValues: An array of strings representing the export values for the field.
16775 if (isset($prop['exportValues'][$key])) {
16776 $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
16777 } else {
16778 $opt['opt'][$key] = $prop['value'][$key];
16781 } else {
16782 $opt['v'] = $prop['value'];
16785 // richValue: This property specifies the text contents and formatting of a rich text field.
16786 if (isset($prop['richValue'])) {
16787 $opt['rv'] = $prop['richValue'];
16789 // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
16790 if (isset($prop['submitName'])) {
16791 $opt['tm'] = $prop['submitName'];
16793 // name: Fully qualified field name.
16794 if (isset($prop['name'])) {
16795 $opt['t'] = $prop['name'];
16797 // userName: The user name (short description string) of the field.
16798 if (isset($prop['userName'])) {
16799 $opt['tu'] = $prop['userName'];
16801 // highlight: Defines how a button reacts when a user clicks it.
16802 if (isset($prop['highlight'])) {
16803 switch ($prop['highlight']) {
16804 case 'none':
16805 case 'highlight.n': {
16806 $opt['h'] = 'N';
16807 break;
16809 case 'invert':
16810 case 'highlight.i': {
16811 $opt['h'] = 'i';
16812 break;
16814 case 'push':
16815 case 'highlight.p': {
16816 $opt['h'] = 'P';
16817 break;
16819 case 'outline':
16820 case 'highlight.o': {
16821 $opt['h'] = 'O';
16822 break;
16826 // Unsupported options:
16827 // - calcOrderIndex: Changes the calculation order of fields in the document.
16828 // - delay: Delays the redrawing of a field's appearance.
16829 // - defaultStyle: This property defines the default style attributes for the form field.
16830 // - style: Allows the user to set the glyph style of a check box or radio button.
16831 // - textColor, textFont, textSize
16832 return $opt;
16836 * Set default properties for form fields.
16837 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16838 * @public
16839 * @author Nicola Asuni
16840 * @since 4.8.000 (2009-09-06)
16842 public function setFormDefaultProp($prop=array()) {
16843 $this->default_form_prop = $prop;
16847 * Return the default properties for form fields.
16848 * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16849 * @public
16850 * @author Nicola Asuni
16851 * @since 4.8.000 (2009-09-06)
16853 public function getFormDefaultProp() {
16854 return $this->default_form_prop;
16858 * Creates a text field
16859 * @param $name (string) field name
16860 * @param $w (float) Width of the rectangle
16861 * @param $h (float) Height of the rectangle
16862 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16863 * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
16864 * @param $x (float) Abscissa of the upper-left corner of the rectangle
16865 * @param $y (float) Ordinate of the upper-left corner of the rectangle
16866 * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
16867 * @public
16868 * @author Nicola Asuni
16869 * @since 4.8.000 (2009-09-07)
16871 public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
16872 if ($x === '') {
16873 $x = $this->x;
16875 if ($y === '') {
16876 $y = $this->y;
16878 // check page for no-write regions and adapt page margins if necessary
16879 list($x, $y) = $this->checkPageRegions($h, $x, $y);
16880 if ($js) {
16881 $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
16882 return;
16884 // get default style
16885 $prop = array_merge($this->getFormDefaultProp(), $prop);
16886 // get annotation data
16887 $popt = $this->getAnnotOptFromJSProp($prop);
16888 // set default appearance stream
16889 $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
16890 $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
16891 $popt['da'] = $fontstyle;
16892 // build appearance stream
16893 $popt['ap'] = array();
16894 $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
16895 $text = '';
16896 if (isset($prop['value']) AND !empty($prop['value'])) {
16897 $text = $prop['value'];
16898 } elseif (isset($opt['v']) AND !empty($opt['v'])) {
16899 $text = $opt['v'];
16901 $tmpid = $this->startTemplate($w, $h, false);
16902 $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
16903 $this->endTemplate();
16904 --$this->n;
16905 $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
16906 unset($this->xobjects[$tmpid]);
16907 $popt['ap']['n'] .= 'Q EMC';
16908 // merge options
16909 $opt = array_merge($popt, $opt);
16910 // remove some conflicting options
16911 unset($opt['bs']);
16912 // set remaining annotation data
16913 $opt['Subtype'] = 'Widget';
16914 $opt['ft'] = 'Tx';
16915 $opt['t'] = $name;
16916 // Additional annotation's parameters (check _putannotsobj() method):
16917 //$opt['f']
16918 //$opt['as']
16919 //$opt['bs']
16920 //$opt['be']
16921 //$opt['c']
16922 //$opt['border']
16923 //$opt['h']
16924 //$opt['mk'];
16925 //$opt['mk']['r']
16926 //$opt['mk']['bc'];
16927 //$opt['mk']['bg'];
16928 unset($opt['mk']['ca']);
16929 unset($opt['mk']['rc']);
16930 unset($opt['mk']['ac']);
16931 unset($opt['mk']['i']);
16932 unset($opt['mk']['ri']);
16933 unset($opt['mk']['ix']);
16934 unset($opt['mk']['if']);
16935 //$opt['mk']['if']['sw'];
16936 //$opt['mk']['if']['s'];
16937 //$opt['mk']['if']['a'];
16938 //$opt['mk']['if']['fb'];
16939 unset($opt['mk']['tp']);
16940 //$opt['tu']
16941 //$opt['tm']
16942 //$opt['ff']
16943 //$opt['v']
16944 //$opt['dv']
16945 //$opt['a']
16946 //$opt['aa']
16947 //$opt['q']
16948 $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
16949 if ($this->rtl) {
16950 $this->x -= $w;
16951 } else {
16952 $this->x += $w;
16957 * Creates a RadioButton field.
16958 * @param $name (string) Field name.
16959 * @param $w (int) Width or the radio button.
16960 * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
16961 * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
16962 * @param $onvalue (string) Value to be returned if selected.
16963 * @param $checked (boolean) Define the initial state.
16964 * @param $x (float) Abscissa of the upper-left corner of the rectangle
16965 * @param $y (float) Ordinate of the upper-left corner of the rectangle
16966 * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
16967 * @public
16968 * @author Nicola Asuni
16969 * @since 4.8.000 (2009-09-07)
16971 public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
16972 if ($x === '') {
16973 $x = $this->x;
16975 if ($y === '') {
16976 $y = $this->y;
16978 // check page for no-write regions and adapt page margins if necessary
16979 list($x, $y) = $this->checkPageRegions($w, $x, $y);
16980 if ($js) {
16981 $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
16982 return;
16984 if ($this->empty_string($onvalue)) {
16985 $onvalue = 'On';
16987 if ($checked) {
16988 $defval = $onvalue;
16989 } else {
16990 $defval = 'Off';
16992 // set font
16993 $font = 'zapfdingbats';
16994 $this->AddFont($font);
16995 $tmpfont = $this->getFontBuffer($font);
16996 // set data for parent group
16997 if (!isset($this->radiobutton_groups[$this->page])) {
16998 $this->radiobutton_groups[$this->page] = array();
17000 if (!isset($this->radiobutton_groups[$this->page][$name])) {
17001 $this->radiobutton_groups[$this->page][$name] = array();
17002 ++$this->n;
17003 $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
17004 $this->radio_groups[] = $this->n;
17006 $kid = ($this->n + 1);
17007 // save object ID to be added on Kids entry on parent object
17008 $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
17009 // get default style
17010 $prop = array_merge($this->getFormDefaultProp(), $prop);
17011 $prop['NoToggleToOff'] = 'true';
17012 $prop['Radio'] = 'true';
17013 $prop['borderStyle'] = 'inset';
17014 // get annotation data
17015 $popt = $this->getAnnotOptFromJSProp($prop);
17016 // set additional default options
17017 $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
17018 $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
17019 $popt['da'] = $fontstyle;
17020 // build appearance stream
17021 $popt['ap'] = array();
17022 $popt['ap']['n'] = array();
17023 $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
17024 $popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(108).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
17025 $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(109).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
17026 if (!isset($popt['mk'])) {
17027 $popt['mk'] = array();
17029 $popt['mk']['ca'] = '(l)';
17030 // merge options
17031 $opt = array_merge($popt, $opt);
17032 // set remaining annotation data
17033 $opt['Subtype'] = 'Widget';
17034 $opt['ft'] = 'Btn';
17035 if ($checked) {
17036 $opt['v'] = array('/'.$onvalue);
17037 $opt['as'] = $onvalue;
17038 } else {
17039 $opt['as'] = 'Off';
17041 // store readonly flag
17042 if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
17043 $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
17045 $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
17046 $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
17047 if ($this->rtl) {
17048 $this->x -= $w;
17049 } else {
17050 $this->x += $w;
17055 * Creates a List-box field
17056 * @param $name (string) field name
17057 * @param $w (int) width
17058 * @param $h (int) height
17059 * @param $values (array) array containing the list of values.
17060 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
17061 * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
17062 * @param $x (float) Abscissa of the upper-left corner of the rectangle
17063 * @param $y (float) Ordinate of the upper-left corner of the rectangle
17064 * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
17065 * @public
17066 * @author Nicola Asuni
17067 * @since 4.8.000 (2009-09-07)
17069 public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
17070 if ($x === '') {
17071 $x = $this->x;
17073 if ($y === '') {
17074 $y = $this->y;
17076 // check page for no-write regions and adapt page margins if necessary
17077 list($x, $y) = $this->checkPageRegions($h, $x, $y);
17078 if ($js) {
17079 $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
17080 $s = '';
17081 foreach ($values as $value) {
17082 $s .= '\''.addslashes($value).'\',';
17084 $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
17085 return;
17087 // get default style
17088 $prop = array_merge($this->getFormDefaultProp(), $prop);
17089 // get annotation data
17090 $popt = $this->getAnnotOptFromJSProp($prop);
17091 // set additional default values
17092 $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
17093 $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
17094 $popt['da'] = $fontstyle;
17095 // build appearance stream
17096 $popt['ap'] = array();
17097 $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
17098 $text = '';
17099 foreach($values as $item) {
17100 $text .= $item."\n";
17102 $tmpid = $this->startTemplate($w, $h, false);
17103 $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
17104 $this->endTemplate();
17105 --$this->n;
17106 $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
17107 unset($this->xobjects[$tmpid]);
17108 $popt['ap']['n'] .= 'Q EMC';
17109 // merge options
17110 $opt = array_merge($popt, $opt);
17111 // set remaining annotation data
17112 $opt['Subtype'] = 'Widget';
17113 $opt['ft'] = 'Ch';
17114 $opt['t'] = $name;
17115 $opt['opt'] = $values;
17116 unset($opt['mk']['ca']);
17117 unset($opt['mk']['rc']);
17118 unset($opt['mk']['ac']);
17119 unset($opt['mk']['i']);
17120 unset($opt['mk']['ri']);
17121 unset($opt['mk']['ix']);
17122 unset($opt['mk']['if']);
17123 unset($opt['mk']['tp']);
17124 $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
17125 if ($this->rtl) {
17126 $this->x -= $w;
17127 } else {
17128 $this->x += $w;
17133 * Creates a Combo-box field
17134 * @param $name (string) field name
17135 * @param $w (int) width
17136 * @param $h (int) height
17137 * @param $values (array) array containing the list of values.
17138 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
17139 * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
17140 * @param $x (float) Abscissa of the upper-left corner of the rectangle
17141 * @param $y (float) Ordinate of the upper-left corner of the rectangle
17142 * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
17143 * @public
17144 * @author Nicola Asuni
17145 * @since 4.8.000 (2009-09-07)
17147 public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
17148 if ($x === '') {
17149 $x = $this->x;
17151 if ($y === '') {
17152 $y = $this->y;
17154 // check page for no-write regions and adapt page margins if necessary
17155 list($x, $y) = $this->checkPageRegions($h, $x, $y);
17156 if ($js) {
17157 $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
17158 $s = '';
17159 foreach ($values as $value) {
17160 $s .= "'".addslashes($value)."',";
17162 $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
17163 return;
17165 // get default style
17166 $prop = array_merge($this->getFormDefaultProp(), $prop);
17167 $prop['Combo'] = true;
17168 // get annotation data
17169 $popt = $this->getAnnotOptFromJSProp($prop);
17170 // set additional default options
17171 $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
17172 $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
17173 $popt['da'] = $fontstyle;
17174 // build appearance stream
17175 $popt['ap'] = array();
17176 $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
17177 $text = '';
17178 foreach($values as $item) {
17179 $text .= $item[1]."\n";
17181 $tmpid = $this->startTemplate($w, $h, false);
17182 $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
17183 $this->endTemplate();
17184 --$this->n;
17185 $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
17186 unset($this->xobjects[$tmpid]);
17187 $popt['ap']['n'] .= 'Q EMC';
17188 // merge options
17189 $opt = array_merge($popt, $opt);
17190 // set remaining annotation data
17191 $opt['Subtype'] = 'Widget';
17192 $opt['ft'] = 'Ch';
17193 $opt['t'] = $name;
17194 $opt['opt'] = $values;
17195 unset($opt['mk']['ca']);
17196 unset($opt['mk']['rc']);
17197 unset($opt['mk']['ac']);
17198 unset($opt['mk']['i']);
17199 unset($opt['mk']['ri']);
17200 unset($opt['mk']['ix']);
17201 unset($opt['mk']['if']);
17202 unset($opt['mk']['tp']);
17203 $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
17204 if ($this->rtl) {
17205 $this->x -= $w;
17206 } else {
17207 $this->x += $w;
17212 * Creates a CheckBox field
17213 * @param $name (string) field name
17214 * @param $w (int) width
17215 * @param $checked (boolean) define the initial state.
17216 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
17217 * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
17218 * @param $onvalue (string) value to be returned if selected.
17219 * @param $x (float) Abscissa of the upper-left corner of the rectangle
17220 * @param $y (float) Ordinate of the upper-left corner of the rectangle
17221 * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
17222 * @public
17223 * @author Nicola Asuni
17224 * @since 4.8.000 (2009-09-07)
17226 public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
17227 if ($x === '') {
17228 $x = $this->x;
17230 if ($y === '') {
17231 $y = $this->y;
17233 // check page for no-write regions and adapt page margins if necessary
17234 list($x, $y) = $this->checkPageRegions($w, $x, $y);
17235 if ($js) {
17236 $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
17237 return;
17239 if (!isset($prop['value'])) {
17240 $prop['value'] = array('Yes');
17242 // get default style
17243 $prop = array_merge($this->getFormDefaultProp(), $prop);
17244 $prop['borderStyle'] = 'inset';
17245 // get annotation data
17246 $popt = $this->getAnnotOptFromJSProp($prop);
17247 // set additional default options
17248 $font = 'zapfdingbats';
17249 $this->AddFont($font);
17250 $tmpfont = $this->getFontBuffer($font);
17251 $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
17252 $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
17253 $popt['da'] = $fontstyle;
17254 // build appearance stream
17255 $popt['ap'] = array();
17256 $popt['ap']['n'] = array();
17257 $fy = ((($tmpfont['desc']['Ascent'] + $tmpfont['desc']['Descent']) * $this->FontSizePt) / (1000 * $this->k));
17258 $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
17259 $popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(110).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
17260 $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(111).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
17261 // merge options
17262 $opt = array_merge($popt, $opt);
17263 // set remaining annotation data
17264 $opt['Subtype'] = 'Widget';
17265 $opt['ft'] = 'Btn';
17266 $opt['t'] = $name;
17267 $opt['opt'] = array($onvalue);
17268 if ($checked) {
17269 $opt['v'] = array('/Yes');
17270 $opt['as'] = 'Yes';
17271 } else {
17272 $opt['v'] = array('/Off');
17273 $opt['as'] = 'Off';
17275 $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
17276 if ($this->rtl) {
17277 $this->x -= $w;
17278 } else {
17279 $this->x += $w;
17284 * Creates a button field
17285 * @param $name (string) field name
17286 * @param $w (int) width
17287 * @param $h (int) height
17288 * @param $caption (string) caption.
17289 * @param $action (mixed) action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
17290 * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
17291 * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
17292 * @param $x (float) Abscissa of the upper-left corner of the rectangle
17293 * @param $y (float) Ordinate of the upper-left corner of the rectangle
17294 * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
17295 * @public
17296 * @author Nicola Asuni
17297 * @since 4.8.000 (2009-09-07)
17299 public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
17300 if ($x === '') {
17301 $x = $this->x;
17303 if ($y === '') {
17304 $y = $this->y;
17306 // check page for no-write regions and adapt page margins if necessary
17307 list($x, $y) = $this->checkPageRegions($h, $x, $y);
17308 if ($js) {
17309 $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
17310 $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
17311 $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
17312 $this->javascript .= 'f'.$name.".highlight='push';\n";
17313 $this->javascript .= 'f'.$name.".print=false;\n";
17314 return;
17316 // get default style
17317 $prop = array_merge($this->getFormDefaultProp(), $prop);
17318 $prop['Pushbutton'] = 'true';
17319 $prop['highlight'] = 'push';
17320 $prop['display'] = 'display.noPrint';
17321 // get annotation data
17322 $popt = $this->getAnnotOptFromJSProp($prop);
17323 $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
17324 $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
17325 $popt['da'] = $fontstyle;
17326 // build appearance stream
17327 $popt['ap'] = array();
17328 $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
17329 $tmpid = $this->startTemplate($w, $h, false);
17330 $bw = (2 / $this->k); // border width
17331 $border = array(
17332 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
17333 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
17334 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
17335 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
17336 $this->SetFillColor(204);
17337 $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
17338 $this->endTemplate();
17339 --$this->n;
17340 $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
17341 unset($this->xobjects[$tmpid]);
17342 $popt['ap']['n'] .= 'Q EMC';
17343 // set additional default options
17344 if (!isset($popt['mk'])) {
17345 $popt['mk'] = array();
17347 $ann_obj_id = ($this->n + 1);
17348 if (!empty($action) AND !is_array($action)) {
17349 $ann_obj_id = ($this->n + 2);
17351 $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
17352 $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
17353 $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
17354 // merge options
17355 $opt = array_merge($popt, $opt);
17356 // set remaining annotation data
17357 $opt['Subtype'] = 'Widget';
17358 $opt['ft'] = 'Btn';
17359 $opt['t'] = $caption;
17360 $opt['v'] = $name;
17361 if (!empty($action)) {
17362 if (is_array($action)) {
17363 // form action options as on section 12.7.5 of PDF32000_2008.
17364 $opt['aa'] = '/D <<';
17365 $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
17366 foreach ($action AS $key => $val) {
17367 if (($key == 'S') AND in_array($val, $bmode)) {
17368 $opt['aa'] .= ' /S /'.$val;
17369 } elseif (($key == 'F') AND (!empty($val))) {
17370 $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
17371 } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
17372 $opt['aa'] .= ' /Fields [';
17373 foreach ($val AS $field) {
17374 $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
17376 $opt['aa'] .= ']';
17377 } elseif (($key == 'Flags')) {
17378 $ff = 0;
17379 if (is_array($val)) {
17380 foreach ($val AS $flag) {
17381 switch ($flag) {
17382 case 'Include/Exclude': {
17383 $ff += 1 << 0;
17384 break;
17386 case 'IncludeNoValueFields': {
17387 $ff += 1 << 1;
17388 break;
17390 case 'ExportFormat': {
17391 $ff += 1 << 2;
17392 break;
17394 case 'GetMethod': {
17395 $ff += 1 << 3;
17396 break;
17398 case 'SubmitCoordinates': {
17399 $ff += 1 << 4;
17400 break;
17402 case 'XFDF': {
17403 $ff += 1 << 5;
17404 break;
17406 case 'IncludeAppendSaves': {
17407 $ff += 1 << 6;
17408 break;
17410 case 'IncludeAnnotations': {
17411 $ff += 1 << 7;
17412 break;
17414 case 'SubmitPDF': {
17415 $ff += 1 << 8;
17416 break;
17418 case 'CanonicalFormat': {
17419 $ff += 1 << 9;
17420 break;
17422 case 'ExclNonUserAnnots': {
17423 $ff += 1 << 10;
17424 break;
17426 case 'ExclFKey': {
17427 $ff += 1 << 11;
17428 break;
17430 case 'EmbedForm': {
17431 $ff += 1 << 13;
17432 break;
17436 } else {
17437 $ff = intval($val);
17439 $opt['aa'] .= ' /Flags '.$ff;
17442 $opt['aa'] .= ' >>';
17443 } else {
17444 // Javascript action or raw action command
17445 $js_obj_id = $this->addJavascriptObject($action);
17446 $opt['aa'] = '/D '.$js_obj_id.' 0 R';
17449 $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
17450 if ($this->rtl) {
17451 $this->x -= $w;
17452 } else {
17453 $this->x += $w;
17457 // --- END FORMS FIELDS ------------------------------------------------
17460 * Add certification signature (DocMDP or UR3)
17461 * You can set only one signature type
17462 * @protected
17463 * @author Nicola Asuni
17464 * @since 4.6.008 (2009-05-07)
17466 protected function _putsignature() {
17467 if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
17468 return;
17470 $sigobjid = ($this->sig_obj_id + 1);
17471 $out = $this->_getobj($sigobjid)."\n";
17472 $out .= '<< /Type /Sig';
17473 $out .= ' /Filter /Adobe.PPKLite';
17474 $out .= ' /SubFilter /adbe.pkcs7.detached';
17475 $out .= ' '.$this->byterange_string;
17476 $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
17477 $out .= ' /Reference ['; // array of signature reference dictionaries
17478 $out .= ' << /Type /SigRef';
17479 if ($this->signature_data['cert_type'] > 0) {
17480 $out .= ' /TransformMethod /DocMDP';
17481 $out .= ' /TransformParams <<';
17482 $out .= ' /Type /TransformParams';
17483 $out .= ' /P '.$this->signature_data['cert_type'];
17484 $out .= ' /V /1.2';
17485 } else {
17486 $out .= ' /TransformMethod /UR3';
17487 $out .= ' /TransformParams <<';
17488 $out .= ' /Type /TransformParams';
17489 $out .= ' /V /2.2';
17490 if (!$this->empty_string($this->ur['document'])) {
17491 $out .= ' /Document['.$this->ur['document'].']';
17493 if (!$this->empty_string($this->ur['form'])) {
17494 $out .= ' /Form['.$this->ur['form'].']';
17496 if (!$this->empty_string($this->ur['signature'])) {
17497 $out .= ' /Signature['.$this->ur['signature'].']';
17499 if (!$this->empty_string($this->ur['annots'])) {
17500 $out .= ' /Annots['.$this->ur['annots'].']';
17502 if (!$this->empty_string($this->ur['ef'])) {
17503 $out .= ' /EF['.$this->ur['ef'].']';
17505 if (!$this->empty_string($this->ur['formex'])) {
17506 $out .= ' /FormEX['.$this->ur['formex'].']';
17509 $out .= ' >>'; // close TransformParams
17510 // optional digest data (values must be calculated and replaced later)
17511 //$out .= ' /Data ********** 0 R';
17512 //$out .= ' /DigestMethod/MD5';
17513 //$out .= ' /DigestLocation[********** 34]';
17514 //$out .= ' /DigestValue<********************************>';
17515 $out .= ' >>';
17516 $out .= ' ]'; // end of reference
17517 if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
17518 $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
17520 if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
17521 $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
17523 if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
17524 $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
17526 if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
17527 $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
17529 $out .= ' /M '.$this->_datestring($sigobjid);
17530 $out .= ' >>';
17531 $out .= "\n".'endobj';
17532 $this->_out($out);
17536 * Set User's Rights for PDF Reader
17537 * WARNING: This is experimental and currently do not work.
17538 * Check the PDF Reference 8.7.1 Transform Methods,
17539 * Table 8.105 Entries in the UR transform parameters dictionary
17540 * @param $enable (boolean) if true enable user's rights on PDF reader
17541 * @param $document (string) Names specifying additional document-wide usage rights for the document. The only defined value is "/FullSave", which permits a user to save the document along with modified form and/or annotation data.
17542 * @param $annots (string) Names specifying additional annotation-related usage rights for the document. Valid names in PDF 1.5 and later are /Create/Delete/Modify/Copy/Import/Export, which permit the user to perform the named operation on annotations.
17543 * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
17544 * @param $signature (string) Names specifying additional signature-related usage rights for the document. The only defined value is /Modify, which permits a user to apply a digital signature to an existing signature form field or clear a signed signature form field.
17545 * @param $ef (string) Names specifying additional usage rights for named embedded files in the document. Valid names are /Create/Delete/Modify/Import, which permit the user to perform the named operation on named embedded files
17546 Names specifying additional embedded-files-related usage rights for the document.
17547 * @param $formex (string) Names specifying additional form-field-related usage rights. The only valid name is BarcodePlaintext, which permits text form field data to be encoded as a plaintext two-dimensional barcode.
17548 * @public
17549 * @author Nicola Asuni
17550 * @since 2.9.000 (2008-03-26)
17552 public function setUserRights(
17553 $enable=true,
17554 $document='/FullSave',
17555 $annots='/Create/Delete/Modify/Copy/Import/Export',
17556 $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
17557 $signature='/Modify',
17558 $ef='/Create/Delete/Modify/Import',
17559 $formex='') {
17560 $this->ur['enabled'] = $enable;
17561 $this->ur['document'] = $document;
17562 $this->ur['annots'] = $annots;
17563 $this->ur['form'] = $form;
17564 $this->ur['signature'] = $signature;
17565 $this->ur['ef'] = $ef;
17566 $this->ur['formex'] = $formex;
17567 if (!$this->sign) {
17568 $this->setSignature('', '', '', '', 0, array());
17573 * Enable document signature (requires the OpenSSL Library).
17574 * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
17575 * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
17576 * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
17577 * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
17578 * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
17579 * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
17580 * @param $private_key_password (string) password
17581 * @param $extracerts (string) specifies the name of a file containing a bunch of extra certificates to include in the signature which can for example be used to help the recipient to verify the certificate that you used.
17582 * @param $cert_type (int) The access permissions granted for this document. Valid values shall be: 1 = No changes to the document shall be permitted; any change to the document shall invalidate the signature; 2 = Permitted changes shall be filling in forms, instantiating page templates, and signing; other changes shall invalidate the signature; 3 = Permitted changes shall be the same as for 2, as well as annotation creation, deletion, and modification; other changes shall invalidate the signature.
17583 * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
17584 * @public
17585 * @author Nicola Asuni
17586 * @since 4.6.005 (2009-04-24)
17588 public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
17589 // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
17590 // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
17591 // to convert pfx certificate to pem: openssl
17592 // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
17593 $this->sign = true;
17594 ++$this->n;
17595 $this->sig_obj_id = $this->n; // signature widget
17596 ++$this->n; // signature object ($this->sig_obj_id + 1)
17597 $this->signature_data = array();
17598 if (strlen($signing_cert) == 0) {
17599 $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
17600 $private_key_password = 'tcpdfdemo';
17602 if (strlen($private_key) == 0) {
17603 $private_key = $signing_cert;
17605 $this->signature_data['signcert'] = $signing_cert;
17606 $this->signature_data['privkey'] = $private_key;
17607 $this->signature_data['password'] = $private_key_password;
17608 $this->signature_data['extracerts'] = $extracerts;
17609 $this->signature_data['cert_type'] = $cert_type;
17610 $this->signature_data['info'] = $info;
17614 * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
17615 * @param $x (float) Abscissa of the upper-left corner.
17616 * @param $y (float) Ordinate of the upper-left corner.
17617 * @param $w (float) Width of the signature area.
17618 * @param $h (float) Height of the signature area.
17619 * @param $page (int) option page number (if < 0 the current page is used).
17620 * @public
17621 * @author Nicola Asuni
17622 * @since 5.3.011 (2010-06-17)
17624 public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
17625 $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
17629 * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
17630 * @param $x (float) Abscissa of the upper-left corner.
17631 * @param $y (float) Ordinate of the upper-left corner.
17632 * @param $w (float) Width of the signature area.
17633 * @param $h (float) Height of the signature area.
17634 * @param $page (int) option page number (if < 0 the current page is used).
17635 * @public
17636 * @author Nicola Asuni
17637 * @since 5.9.101 (2011-07-06)
17639 public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
17640 ++$this->n;
17641 $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
17645 * Get the array that defines the signature appearance (page and rectangle coordinates).
17646 * @param $x (float) Abscissa of the upper-left corner.
17647 * @param $y (float) Ordinate of the upper-left corner.
17648 * @param $w (float) Width of the signature area.
17649 * @param $h (float) Height of the signature area.
17650 * @param $page (int) option page number (if < 0 the current page is used).
17651 * @return (array) Array defining page and rectangle coordinates of signature appearance.
17652 * @protected
17653 * @author Nicola Asuni
17654 * @since 5.9.101 (2011-07-06)
17656 protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1) {
17657 $sigapp = array();
17658 if (($page < 1) OR ($page > $this->numpages)) {
17659 $sigapp['page'] = $this->page;
17660 } else {
17661 $sigapp['page'] = intval($page);
17663 $a = $x * $this->k;
17664 $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
17665 $c = $w * $this->k;
17666 $d = $h * $this->k;
17667 $sigapp['rect'] = sprintf('%.2F %.2F %.2F %.2F', $a, $b, ($a + $c), ($b + $d));
17668 return $sigapp;
17672 * Create a new page group.
17673 * NOTE: call this function before calling AddPage()
17674 * @param $page (int) starting group page (leave empty for next page).
17675 * @public
17676 * @since 3.0.000 (2008-03-27)
17678 public function startPageGroup($page='') {
17679 if (empty($page)) {
17680 $page = $this->page + 1;
17682 $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
17686 * This method is DEPRECATED and doesn't have any effect.
17687 * Please remove any reference to this method.
17688 * @param $s (string) Empty parameter.
17689 * @deprecated deprecated since version 5.9.089 (2011-06-13)
17690 * @public
17692 public function AliasNbPages($s='') {}
17695 * This method is DEPRECATED and doesn't have any effect.
17696 * Please remove any reference to this method.
17697 * @param $s (string) Empty parameter.
17698 * @deprecated deprecated since version 5.9.089 (2011-06-13)
17699 * @public
17701 public function AliasNumPage($s='') {}
17704 * Set the starting page number.
17705 * @param $num (int) Starting page number.
17706 * @since 5.9.093 (2011-06-16)
17707 * @public
17709 public function setStartingPageNumber($num=1) {
17710 $this->starting_page_number = max(0, intval($num));
17714 * Returns the string alias used right align page numbers.
17715 * If the current font is unicode type, the returned string wil contain an additional open curly brace.
17716 * @return string
17717 * @since 5.9.099 (2011-06-27)
17718 * @public
17720 public function getAliasRightShift() {
17721 // calculate aproximatively the ratio between widths of aliases and replacements.
17722 $ref = '{'.$this->alias_right_shift.'}{'.$this->alias_tot_pages.'}{'.$this->alias_num_page.'}';
17723 $rep = str_repeat(' ', $this->GetNumChars($ref));
17724 $wdiff = max(1, ($this->GetStringWidth($ref) / $this->GetStringWidth($rep)));
17725 $sdiff = sprintf('%.3F', $wdiff);
17726 $alias = $this->alias_right_shift.$sdiff.'}';
17727 if ($this->isUnicodeFont()) {
17728 $alias = '{'.$alias;
17730 return $alias;
17734 * Returns the string alias used for the total number of pages.
17735 * If the current font is unicode type, the returned string is surrounded by additional curly braces.
17736 * This alias will be replaced by the total number of pages in the document.
17737 * @return string
17738 * @since 4.0.018 (2008-08-08)
17739 * @public
17741 public function getAliasNbPages() {
17742 if ($this->isUnicodeFont()) {
17743 return '{'.$this->alias_tot_pages.'}';
17745 return $this->alias_tot_pages;
17749 * Returns the string alias used for the page number.
17750 * If the current font is unicode type, the returned string is surrounded by additional curly braces.
17751 * This alias will be replaced by the page number.
17752 * @return string
17753 * @since 4.5.000 (2009-01-02)
17754 * @public
17756 public function getAliasNumPage() {
17757 if ($this->isUnicodeFont()) {
17758 return '{'.$this->alias_num_page.'}';
17760 return $this->alias_num_page;
17764 * Return the alias for the total number of pages in the current page group.
17765 * If the current font is unicode type, the returned string is surrounded by additional curly braces.
17766 * This alias will be replaced by the total number of pages in this group.
17767 * @return alias of the current page group
17768 * @public
17769 * @since 3.0.000 (2008-03-27)
17771 public function getPageGroupAlias() {
17772 if ($this->isUnicodeFont()) {
17773 return '{'.$this->alias_group_tot_pages.'}';
17775 return $this->alias_group_tot_pages;
17779 * Return the alias for the page number on the current page group.
17780 * If the current font is unicode type, the returned string is surrounded by additional curly braces.
17781 * This alias will be replaced by the page number (relative to the belonging group).
17782 * @return alias of the current page group
17783 * @public
17784 * @since 4.5.000 (2009-01-02)
17786 public function getPageNumGroupAlias() {
17787 if ($this->isUnicodeFont()) {
17788 return '{'.$this->alias_group_num_page.'}';
17790 return $this->alias_group_num_page;
17794 * Return the current page in the group.
17795 * @return current page in the group
17796 * @public
17797 * @since 3.0.000 (2008-03-27)
17799 public function getGroupPageNo() {
17800 return $this->pagegroups[$this->currpagegroup];
17804 * Returns the current group page number formatted as a string.
17805 * @public
17806 * @since 4.3.003 (2008-11-18)
17807 * @see PaneNo(), formatPageNumber()
17809 public function getGroupPageNoFormatted() {
17810 return $this->formatPageNumber($this->getGroupPageNo());
17814 * Format the page numbers.
17815 * This method can be overriden for custom formats.
17816 * @param $num (int) page number
17817 * @protected
17818 * @since 4.2.005 (2008-11-06)
17820 protected function formatPageNumber($num) {
17821 return number_format((float)$num, 0, '', '.');
17825 * Format the page numbers on the Table Of Content.
17826 * This method can be overriden for custom formats.
17827 * @param $num (int) page number
17828 * @protected
17829 * @since 4.5.001 (2009-01-04)
17830 * @see addTOC(), addHTMLTOC()
17832 protected function formatTOCPageNumber($num) {
17833 return number_format((float)$num, 0, '', '.');
17837 * Returns the current page number formatted as a string.
17838 * @public
17839 * @since 4.2.005 (2008-11-06)
17840 * @see PaneNo(), formatPageNumber()
17842 public function PageNoFormatted() {
17843 return $this->formatPageNumber($this->PageNo());
17847 * Put pdf layers.
17848 * @protected
17849 * @since 3.0.000 (2008-03-27)
17851 protected function _putocg() {
17852 if (empty($this->pdflayers)) {
17853 return;
17855 foreach ($this->pdflayers as $key => $layer) {
17856 $this->pdflayers[$key]['objid'] = $this->_newobj();
17857 $out = '<< /Type /OCG';
17858 $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
17859 $out .= ' /Usage <<';
17860 $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
17861 $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
17862 $out .= ' >> >>';
17863 $out .= "\n".'endobj';
17864 $this->_out($out);
17869 * Start a new pdf layer.
17870 * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
17871 * @param $print (boolean) Set to true to print this layer.
17872 * @param $view (boolean) Set to true to view this layer.
17873 * @public
17874 * @since 5.9.102 (2011-07-13)
17876 public function startLayer($name='', $print=true, $view=true) {
17877 $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
17878 if (empty($name)) {
17879 $name = $layer;
17880 } else {
17881 $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
17883 $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
17884 $this->openMarkedContent = true;
17885 $this->_out('/OC /'.$layer.' BDC');
17889 * End the current PDF layer.
17890 * @public
17891 * @since 5.9.102 (2011-07-13)
17893 public function endLayer() {
17894 if ($this->openMarkedContent) {
17895 // close existing open marked-content layer
17896 $this->_out('EMC');
17897 $this->openMarkedContent = false;
17902 * Set the visibility of the successive elements.
17903 * This can be useful, for instance, to put a background
17904 * image or color that will show on screen but won't print.
17905 * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
17906 * @public
17907 * @since 3.0.000 (2008-03-27)
17909 public function setVisibility($v) {
17910 $this->endLayer();
17911 switch($v) {
17912 case 'print': {
17913 $this->startLayer('Print', true, false);
17914 break;
17916 case 'view':
17917 case 'screen': {
17918 $this->startLayer('View', false, true);
17919 break;
17921 case 'all': {
17922 $this->_out('');
17923 break;
17925 default: {
17926 $this->Error('Incorrect visibility: '.$v);
17927 break;
17933 * Add transparency parameters to the current extgstate
17934 * @param $parms (array) parameters
17935 * @return the number of extgstates
17936 * @protected
17937 * @since 3.0.000 (2008-03-27)
17939 protected function addExtGState($parms) {
17940 if ($this->pdfa_mode) {
17941 // transparencies are not allowed in PDF/A mode
17942 return;
17944 // check if this ExtGState already exist
17945 foreach ($this->extgstates as $i => $ext) {
17946 if ($ext['parms'] == $parms) {
17947 if ($this->inxobj) {
17948 // we are inside an XObject template
17949 $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
17951 // return reference to existing ExtGState
17952 return $i;
17955 $n = (count($this->extgstates) + 1);
17956 $this->extgstates[$n] = array('parms' => $parms);
17957 if ($this->inxobj) {
17958 // we are inside an XObject template
17959 $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
17961 return $n;
17965 * Add an extgstate
17966 * @param $gs (array) extgstate
17967 * @protected
17968 * @since 3.0.000 (2008-03-27)
17970 protected function setExtGState($gs) {
17971 if ($this->pdfa_mode) {
17972 // transparency is not allowed in PDF/A mode
17973 return;
17975 $this->_out(sprintf('/GS%d gs', $gs));
17979 * Put extgstates for object transparency
17980 * @protected
17981 * @since 3.0.000 (2008-03-27)
17983 protected function _putextgstates() {
17984 if ($this->pdfa_mode) {
17985 // transparencies are not allowed in PDF/A mode
17986 return;
17988 foreach ($this->extgstates as $i => $ext) {
17989 $this->extgstates[$i]['n'] = $this->_newobj();
17990 $out = '<< /Type /ExtGState';
17991 foreach ($ext['parms'] as $k => $v) {
17992 if (is_float($v)) {
17993 $v = sprintf('%.2F', $v);
17995 $out .= ' /'.$k.' '.$v;
17997 $out .= ' >>';
17998 $out .= "\n".'endobj';
17999 $this->_out($out);
18004 * Set alpha for stroking (CA) and non-stroking (ca) operations.
18005 * @param $alpha (float) real value from 0 (transparent) to 1 (opaque)
18006 * @param $bm (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
18007 * @public
18008 * @since 3.0.000 (2008-03-27)
18010 public function setAlpha($alpha, $bm='Normal') {
18011 if ($this->pdfa_mode) {
18012 // transparency is not allowed in PDF/A mode
18013 return;
18015 $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm, 'AIS' => 'false'));
18016 $this->setExtGState($gs);
18020 * Set the default JPEG compression quality (1-100)
18021 * @param $quality (int) JPEG quality, integer between 1 and 100
18022 * @public
18023 * @since 3.0.000 (2008-03-27)
18025 public function setJPEGQuality($quality) {
18026 if (($quality < 1) OR ($quality > 100)) {
18027 $quality = 75;
18029 $this->jpeg_quality = intval($quality);
18033 * Set the default number of columns in a row for HTML tables.
18034 * @param $cols (int) number of columns
18035 * @public
18036 * @since 3.0.014 (2008-06-04)
18038 public function setDefaultTableColumns($cols=4) {
18039 $this->default_table_columns = intval($cols);
18043 * Set the height of the cell (line height) respect the font height.
18044 * @param $h (int) cell proportion respect font height (typical value = 1.25).
18045 * @public
18046 * @since 3.0.014 (2008-06-04)
18048 public function setCellHeightRatio($h) {
18049 $this->cell_height_ratio = $h;
18053 * return the height of cell repect font height.
18054 * @public
18055 * @since 4.0.012 (2008-07-24)
18057 public function getCellHeightRatio() {
18058 return $this->cell_height_ratio;
18062 * Set the PDF version (check PDF reference for valid values).
18063 * @param $version (string) PDF document version.
18064 * @public
18065 * @since 3.1.000 (2008-06-09)
18067 public function setPDFVersion($version='1.7') {
18068 if ($this->pdfa_mode) {
18069 // PDF/A mode
18070 $this->PDFVersion = '1.4';
18071 } else {
18072 $this->PDFVersion = $version;
18077 * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
18078 * (see Section 8.1 of PDF reference, "Viewer Preferences").
18079 * <ul><li>HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application's tool bars when the document is active. Default value: false.</li><li>HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application's menu bar when the document is active. Default value: false.</li><li>HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default value: false.</li><li>FitWindow boolean (Optional) A flag specifying whether to resize the document's window to fit the size of the first displayed page. Default value: false.</li><li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li><li>DisplayDocTitle boolean (Optional; PDF 1.4) A flag specifying whether the window's title bar should display the document title taken from the Title entry of the document information dictionary (see Section 10.2.1, "Document Information Dictionary"). If false, the title bar should instead display the name of the PDF file containing the document. Default value: false.</li><li>NonFullScreenPageMode name (Optional) The document's page mode, specifying how to display the document on exiting full-screen mode:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>UseOC Optional content group panel visible</li></ul>This entry is meaningful only if the value of the PageMode entry in the catalog dictionary (see Section 3.6.1, "Document Catalog") is FullScreen; it is ignored otherwise. Default value: UseNone.</li><li>ViewArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>ViewClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be rendered when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintScaling name (Optional; PDF 1.6) The page scaling option to be selected when a print dialog is displayed for this document. Valid values are: <ul><li>None, which indicates that the print dialog should reflect no page scaling</li><li>AppDefault (default), which indicates that applications should use the current print scaling</li></ul></li><li>Duplex name (Optional; PDF 1.7) The paper handling option to use when printing the file from the print dialog. The following values are valid:<ul><li>Simplex - Print single-sided</li><li>DuplexFlipShortEdge - Duplex and flip on the short edge of the sheet</li><li>DuplexFlipLongEdge - Duplex and flip on the long edge of the sheet</li></ul>Default value: none</li><li>PickTrayByPDFSize boolean (Optional; PDF 1.7) A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked. Note: This setting has no effect on Mac OS systems, which do not provide the ability to pick the input tray by size.</li><li>PrintPageRange array (Optional; PDF 1.7) The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. An odd number of integers causes this entry to be ignored. Negative numbers cause the entire array to be ignored. Default value: as defined by PDF viewer application</li><li>NumCopies integer (Optional; PDF 1.7) The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are ignored. Default value: as defined by PDF viewer application, but typically 1</li></ul>
18080 * @param $preferences (array) array of options.
18081 * @author Nicola Asuni
18082 * @public
18083 * @since 3.1.000 (2008-06-09)
18085 public function setViewerPreferences($preferences) {
18086 $this->viewer_preferences = $preferences;
18090 * Paints color transition registration bars
18091 * @param $x (float) abscissa of the top left corner of the rectangle.
18092 * @param $y (float) ordinate of the top left corner of the rectangle.
18093 * @param $w (float) width of the rectangle.
18094 * @param $h (float) height of the rectangle.
18095 * @param $transition (boolean) if true prints tcolor transitions to white.
18096 * @param $vertical (boolean) if true prints bar vertically.
18097 * @param $colors (string) colors to print, one letter per color separated by comma (for example 'A,W,R,G,B,C,M,Y,K'): A=black, W=white, R=red, G=green, B=blue, C=cyan, M=magenta, Y=yellow, K=black.
18098 * @author Nicola Asuni
18099 * @since 4.9.000 (2010-03-26)
18100 * @public
18102 public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
18103 $bars = explode(',', $colors);
18104 $numbars = count($bars); // number of bars to print
18105 // set bar measures
18106 if ($vertical) {
18107 $coords = array(0, 0, 0, 1);
18108 $wb = $w / $numbars; // bar width
18109 $hb = $h; // bar height
18110 $xd = $wb; // delta x
18111 $yd = 0; // delta y
18112 } else {
18113 $coords = array(1, 0, 0, 0);
18114 $wb = $w; // bar width
18115 $hb = $h / $numbars; // bar height
18116 $xd = 0; // delta x
18117 $yd = $hb; // delta y
18119 $xb = $x;
18120 $yb = $y;
18121 foreach ($bars as $col) {
18122 switch ($col) {
18123 // set transition colors
18124 case 'A': { // BLACK
18125 $col_a = array(255);
18126 $col_b = array(0);
18127 break;
18129 case 'W': { // WHITE
18130 $col_a = array(0);
18131 $col_b = array(255);
18132 break;
18134 case 'R': { // R
18135 $col_a = array(255,255,255);
18136 $col_b = array(255,0,0);
18137 break;
18139 case 'G': { // G
18140 $col_a = array(255,255,255);
18141 $col_b = array(0,255,0);
18142 break;
18144 case 'B': { // B
18145 $col_a = array(255,255,255);
18146 $col_b = array(0,0,255);
18147 break;
18149 case 'C': { // C
18150 $col_a = array(0,0,0,0);
18151 $col_b = array(100,0,0,0);
18152 break;
18154 case 'M': { // M
18155 $col_a = array(0,0,0,0);
18156 $col_b = array(0,100,0,0);
18157 break;
18159 case 'Y': { // Y
18160 $col_a = array(0,0,0,0);
18161 $col_b = array(0,0,100,0);
18162 break;
18164 case 'K': { // K
18165 $col_a = array(0,0,0,0);
18166 $col_b = array(0,0,0,100);
18167 break;
18169 default: { // GRAY
18170 $col_a = array(255);
18171 $col_b = array(0);
18172 break;
18175 if ($transition) {
18176 // color gradient
18177 $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
18178 } else {
18179 // color rectangle
18180 $this->SetFillColorArray($col_b);
18181 $this->Rect($xb, $yb, $wb, $hb, 'F', array());
18183 $xb += $xd;
18184 $yb += $yd;
18189 * Paints crop mark
18190 * @param $x (float) abscissa of the crop mark center.
18191 * @param $y (float) ordinate of the crop mark center.
18192 * @param $w (float) width of the crop mark.
18193 * @param $h (float) height of the crop mark.
18194 * @param $type (string) type of crop mark, one sybol per type separated by comma: A = top left, B = top right, C = bottom left, D = bottom right.
18195 * @param $color (array) crop mark color (default black).
18196 * @author Nicola Asuni
18197 * @since 4.9.000 (2010-03-26)
18198 * @public
18200 public function cropMark($x, $y, $w, $h, $type='A,B,C,D', $color=array(0,0,0)) {
18201 $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
18202 $crops = explode(',', $type);
18203 $numcrops = count($crops); // number of crop marks to print
18204 $dw = $w / 4; // horizontal space to leave before the intersection point
18205 $dh = $h / 4; // vertical space to leave before the intersection point
18206 foreach ($crops as $crop) {
18207 switch ($crop) {
18208 case 'A': {
18209 $x1 = $x;
18210 $y1 = $y - $h;
18211 $x2 = $x;
18212 $y2 = $y - $dh;
18213 $x3 = $x - $w;
18214 $y3 = $y;
18215 $x4 = $x - $dw;
18216 $y4 = $y;
18217 break;
18219 case 'B': {
18220 $x1 = $x;
18221 $y1 = $y - $h;
18222 $x2 = $x;
18223 $y2 = $y - $dh;
18224 $x3 = $x + $dw;
18225 $y3 = $y;
18226 $x4 = $x + $w;
18227 $y4 = $y;
18228 break;
18230 case 'C': {
18231 $x1 = $x - $w;
18232 $y1 = $y;
18233 $x2 = $x - $dw;
18234 $y2 = $y;
18235 $x3 = $x;
18236 $y3 = $y + $dh;
18237 $x4 = $x;
18238 $y4 = $y + $h;
18239 break;
18241 case 'D': {
18242 $x1 = $x + $dw;
18243 $y1 = $y;
18244 $x2 = $x + $w;
18245 $y2 = $y;
18246 $x3 = $x;
18247 $y3 = $y + $dh;
18248 $x4 = $x;
18249 $y4 = $y + $h;
18250 break;
18253 $this->Line($x1, $y1, $x2, $y2);
18254 $this->Line($x3, $y3, $x4, $y4);
18259 * Paints a registration mark
18260 * @param $x (float) abscissa of the registration mark center.
18261 * @param $y (float) ordinate of the registration mark center.
18262 * @param $r (float) radius of the crop mark.
18263 * @param $double (boolean) if true print two concentric crop marks.
18264 * @param $cola (array) crop mark color (default black).
18265 * @param $colb (array) second crop mark color.
18266 * @author Nicola Asuni
18267 * @since 4.9.000 (2010-03-26)
18268 * @public
18270 public function registrationMark($x, $y, $r, $double=false, $cola=array(0,0,0), $colb=array(255,255,255)) {
18271 $line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
18272 $this->SetFillColorArray($cola);
18273 $this->PieSector($x, $y, $r, 90, 180, 'F');
18274 $this->PieSector($x, $y, $r, 270, 360, 'F');
18275 $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
18276 if ($double) {
18277 $r2 = $r * 0.5;
18278 $this->SetFillColorArray($colb);
18279 $this->PieSector($x, $y, $r2, 90, 180, 'F');
18280 $this->PieSector($x, $y, $r2, 270, 360, 'F');
18281 $this->SetFillColorArray($cola);
18282 $this->PieSector($x, $y, $r2, 0, 90, 'F');
18283 $this->PieSector($x, $y, $r2, 180, 270, 'F');
18284 $this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
18289 * Paints a linear colour gradient.
18290 * @param $x (float) abscissa of the top left corner of the rectangle.
18291 * @param $y (float) ordinate of the top left corner of the rectangle.
18292 * @param $w (float) width of the rectangle.
18293 * @param $h (float) height of the rectangle.
18294 * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
18295 * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
18296 * @param $coords (array) array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg). The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
18297 * @author Andreas Würmser, Nicola Asuni
18298 * @since 3.1.000 (2008-06-09)
18299 * @public
18301 public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
18302 $this->Clip($x, $y, $w, $h);
18303 $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
18307 * Paints a radial colour gradient.
18308 * @param $x (float) abscissa of the top left corner of the rectangle.
18309 * @param $y (float) ordinate of the top left corner of the rectangle.
18310 * @param $w (float) width of the rectangle.
18311 * @param $h (float) height of the rectangle.
18312 * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
18313 * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
18314 * @param $coords (array) array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1, (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg). (fx, fy) should be inside the circle, otherwise some areas will not be defined.
18315 * @author Andreas Würmser, Nicola Asuni
18316 * @since 3.1.000 (2008-06-09)
18317 * @public
18319 public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
18320 $this->Clip($x, $y, $w, $h);
18321 $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
18325 * Paints a coons patch mesh.
18326 * @param $x (float) abscissa of the top left corner of the rectangle.
18327 * @param $y (float) ordinate of the top left corner of the rectangle.
18328 * @param $w (float) width of the rectangle.
18329 * @param $h (float) height of the rectangle.
18330 * @param $col1 (array) first color (lower left corner) (RGB components).
18331 * @param $col2 (array) second color (lower right corner) (RGB components).
18332 * @param $col3 (array) third color (upper right corner) (RGB components).
18333 * @param $col4 (array) fourth color (upper left corner) (RGB components).
18334 * @param $coords (array) <ul><li>for one patch mesh: array(float x1, float y1, .... float x12, float y12): 12 pairs of coordinates (normally from 0 to 1) which specify the Bezier control points that define the patch. First pair is the lower left edge point, next is its right control point (control point 2). Then the other points are defined in the order: control point 1, edge point, control point 2 going counter-clockwise around the patch. Last (x12, y12) is the first edge point's left control point (control point 1).</li><li>for two or more patch meshes: array[number of patches]: arrays with the following keys for each patch: f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left of precedent patch - I didn't figure this out completely - just try and error ;-) points: 12 pairs of coordinates of the Bezier control points as above for the first patch, 8 pairs of coordinates for the following patches, ignoring the coordinates already defined by the precedent patch (I also didn't figure out the order of these - also: try and see what's happening) colors: must be 4 colors for the first patch, 2 colors for the following patches</li></ul>
18335 * @param $coords_min (array) minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
18336 * @param $coords_max (array) maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
18337 * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
18338 * @author Andreas Würmser, Nicola Asuni
18339 * @since 3.1.000 (2008-06-09)
18340 * @public
18342 public function CoonsPatchMesh($x, $y, $w, $h, $col1=array(), $col2=array(), $col3=array(), $col4=array(), $coords=array(0.00,0.0,0.33,0.00,0.67,0.00,1.00,0.00,1.00,0.33,1.00,0.67,1.00,1.00,0.67,1.00,0.33,1.00,0.00,1.00,0.00,0.67,0.00,0.33), $coords_min=0, $coords_max=1, $antialias=false) {
18343 if ($this->pdfa_mode) {
18344 return;
18346 $this->Clip($x, $y, $w, $h);
18347 $n = count($this->gradients) + 1;
18348 $this->gradients[$n] = array();
18349 $this->gradients[$n]['type'] = 6; //coons patch mesh
18350 $this->gradients[$n]['coords'] = array();
18351 $this->gradients[$n]['antialias'] = $antialias;
18352 $this->gradients[$n]['colors'] = array();
18353 $this->gradients[$n]['transparency'] = false;
18354 //check the coords array if it is the simple array or the multi patch array
18355 if (!isset($coords[0]['f'])) {
18356 //simple array -> convert to multi patch array
18357 if (!isset($col1[1])) {
18358 $col1[1] = $col1[2] = $col1[0];
18360 if (!isset($col2[1])) {
18361 $col2[1] = $col2[2] = $col2[0];
18363 if (!isset($col3[1])) {
18364 $col3[1] = $col3[2] = $col3[0];
18366 if (!isset($col4[1])) {
18367 $col4[1] = $col4[2] = $col4[0];
18369 $patch_array[0]['f'] = 0;
18370 $patch_array[0]['points'] = $coords;
18371 $patch_array[0]['colors'][0]['r'] = $col1[0];
18372 $patch_array[0]['colors'][0]['g'] = $col1[1];
18373 $patch_array[0]['colors'][0]['b'] = $col1[2];
18374 $patch_array[0]['colors'][1]['r'] = $col2[0];
18375 $patch_array[0]['colors'][1]['g'] = $col2[1];
18376 $patch_array[0]['colors'][1]['b'] = $col2[2];
18377 $patch_array[0]['colors'][2]['r'] = $col3[0];
18378 $patch_array[0]['colors'][2]['g'] = $col3[1];
18379 $patch_array[0]['colors'][2]['b'] = $col3[2];
18380 $patch_array[0]['colors'][3]['r'] = $col4[0];
18381 $patch_array[0]['colors'][3]['g'] = $col4[1];
18382 $patch_array[0]['colors'][3]['b'] = $col4[2];
18383 } else {
18384 //multi patch array
18385 $patch_array = $coords;
18387 $bpcd = 65535; //16 bits per coordinate
18388 //build the data stream
18389 $this->gradients[$n]['stream'] = '';
18390 $count_patch = count($patch_array);
18391 for ($i=0; $i < $count_patch; ++$i) {
18392 $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
18393 $count_points = count($patch_array[$i]['points']);
18394 for ($j=0; $j < $count_points; ++$j) {
18395 //each point as 16 bit
18396 $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
18397 if ($patch_array[$i]['points'][$j] < 0) {
18398 $patch_array[$i]['points'][$j] = 0;
18400 if ($patch_array[$i]['points'][$j] > $bpcd) {
18401 $patch_array[$i]['points'][$j] = $bpcd;
18403 $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
18404 $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
18406 $count_cols = count($patch_array[$i]['colors']);
18407 for ($j=0; $j < $count_cols; ++$j) {
18408 //each color component as 8 bit
18409 $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
18410 $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
18411 $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
18414 //paint the gradient
18415 $this->_out('/Sh'.$n.' sh');
18416 //restore previous Graphic State
18417 $this->_out('Q');
18418 if ($this->inxobj) {
18419 // we are inside an XObject template
18420 $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
18425 * Set a rectangular clipping area.
18426 * @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
18427 * @param $y (float) ordinate of the top left corner of the rectangle.
18428 * @param $w (float) width of the rectangle.
18429 * @param $h (float) height of the rectangle.
18430 * @author Andreas Würmser, Nicola Asuni
18431 * @since 3.1.000 (2008-06-09)
18432 * @protected
18434 protected function Clip($x, $y, $w, $h) {
18435 if ($this->rtl) {
18436 $x = $this->w - $x - $w;
18438 //save current Graphic State
18439 $s = 'q';
18440 //set clipping area
18441 $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
18442 //set up transformation matrix for gradient
18443 $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
18444 $this->_out($s);
18448 * Output gradient.
18449 * @param $type (int) type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
18450 * @param $coords (array) array of coordinates.
18451 * @param $stops (array) array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
18452 * @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
18453 * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
18454 * @author Nicola Asuni
18455 * @since 3.1.000 (2008-06-09)
18456 * @public
18458 public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
18459 if ($this->pdfa_mode) {
18460 return;
18462 $n = count($this->gradients) + 1;
18463 $this->gradients[$n] = array();
18464 $this->gradients[$n]['type'] = $type;
18465 $this->gradients[$n]['coords'] = $coords;
18466 $this->gradients[$n]['antialias'] = $antialias;
18467 $this->gradients[$n]['colors'] = array();
18468 $this->gradients[$n]['transparency'] = false;
18469 // color space
18470 $numcolspace = count($stops[0]['color']);
18471 $bcolor = array_values($background);
18472 switch($numcolspace) {
18473 case 4: { // CMYK
18474 $this->gradients[$n]['colspace'] = 'DeviceCMYK';
18475 if (!empty($background)) {
18476 $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F %.3F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
18478 break;
18480 case 3: { // RGB
18481 $this->gradients[$n]['colspace'] = 'DeviceRGB';
18482 if (!empty($background)) {
18483 $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
18485 break;
18487 case 1: { // Gray scale
18488 $this->gradients[$n]['colspace'] = 'DeviceGray';
18489 if (!empty($background)) {
18490 $this->gradients[$n]['background'] = sprintf('%.3F', $bcolor[0]/255);
18492 break;
18495 $num_stops = count($stops);
18496 $last_stop_id = $num_stops - 1;
18497 foreach ($stops as $key => $stop) {
18498 $this->gradients[$n]['colors'][$key] = array();
18499 // offset represents a location along the gradient vector
18500 if (isset($stop['offset'])) {
18501 $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
18502 } else {
18503 if ($key == 0) {
18504 $this->gradients[$n]['colors'][$key]['offset'] = 0;
18505 } elseif ($key == $last_stop_id) {
18506 $this->gradients[$n]['colors'][$key]['offset'] = 1;
18507 } else {
18508 $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
18509 $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
18512 if (isset($stop['opacity'])) {
18513 $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
18514 if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
18515 $this->gradients[$n]['transparency'] = true;
18517 } else {
18518 $this->gradients[$n]['colors'][$key]['opacity'] = 1;
18520 // exponent for the exponential interpolation function
18521 if (isset($stop['exponent'])) {
18522 $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
18523 } else {
18524 $this->gradients[$n]['colors'][$key]['exponent'] = 1;
18526 // set colors
18527 $color = array_values($stop['color']);
18528 switch($numcolspace) {
18529 case 4: { // CMYK
18530 $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F %.3F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
18531 break;
18533 case 3: { // RGB
18534 $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
18535 break;
18537 case 1: { // Gray scale
18538 $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F', $color[0]/255);
18539 break;
18543 if ($this->gradients[$n]['transparency']) {
18544 // paint luminosity gradient
18545 $this->_out('/TGS'.$n.' gs');
18547 //paint the gradient
18548 $this->_out('/Sh'.$n.' sh');
18549 //restore previous Graphic State
18550 $this->_out('Q');
18551 if ($this->inxobj) {
18552 // we are inside an XObject template
18553 $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
18558 * Output gradient shaders.
18559 * @author Nicola Asuni
18560 * @since 3.1.000 (2008-06-09)
18561 * @protected
18563 function _putshaders() {
18564 if ($this->pdfa_mode) {
18565 return;
18567 $idt = count($this->gradients); //index for transparency gradients
18568 foreach ($this->gradients as $id => $grad) {
18569 if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
18570 $fc = $this->_newobj();
18571 $out = '<<';
18572 $out .= ' /FunctionType 3';
18573 $out .= ' /Domain [0 1]';
18574 $functions = '';
18575 $bounds = '';
18576 $encode = '';
18577 $i = 1;
18578 $num_cols = count($grad['colors']);
18579 $lastcols = $num_cols - 1;
18580 for ($i = 1; $i < $num_cols; ++$i) {
18581 $functions .= ($fc + $i).' 0 R ';
18582 if ($i < $lastcols) {
18583 $bounds .= sprintf('%.3F ', $grad['colors'][$i]['offset']);
18585 $encode .= '0 1 ';
18587 $out .= ' /Functions ['.trim($functions).']';
18588 $out .= ' /Bounds ['.trim($bounds).']';
18589 $out .= ' /Encode ['.trim($encode).']';
18590 $out .= ' >>';
18591 $out .= "\n".'endobj';
18592 $this->_out($out);
18593 for ($i = 1; $i < $num_cols; ++$i) {
18594 $this->_newobj();
18595 $out = '<<';
18596 $out .= ' /FunctionType 2';
18597 $out .= ' /Domain [0 1]';
18598 $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
18599 $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
18600 $out .= ' /N '.$grad['colors'][$i]['exponent'];
18601 $out .= ' >>';
18602 $out .= "\n".'endobj';
18603 $this->_out($out);
18605 // set transparency fuctions
18606 if ($grad['transparency']) {
18607 $ft = $this->_newobj();
18608 $out = '<<';
18609 $out .= ' /FunctionType 3';
18610 $out .= ' /Domain [0 1]';
18611 $functions = '';
18612 $i = 1;
18613 $num_cols = count($grad['colors']);
18614 for ($i = 1; $i < $num_cols; ++$i) {
18615 $functions .= ($ft + $i).' 0 R ';
18617 $out .= ' /Functions ['.trim($functions).']';
18618 $out .= ' /Bounds ['.trim($bounds).']';
18619 $out .= ' /Encode ['.trim($encode).']';
18620 $out .= ' >>';
18621 $out .= "\n".'endobj';
18622 $this->_out($out);
18623 for ($i = 1; $i < $num_cols; ++$i) {
18624 $this->_newobj();
18625 $out = '<<';
18626 $out .= ' /FunctionType 2';
18627 $out .= ' /Domain [0 1]';
18628 $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
18629 $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
18630 $out .= ' /N '.$grad['colors'][$i]['exponent'];
18631 $out .= ' >>';
18632 $out .= "\n".'endobj';
18633 $this->_out($out);
18637 // set shading object
18638 $this->_newobj();
18639 $out = '<< /ShadingType '.$grad['type'];
18640 if (isset($grad['colspace'])) {
18641 $out .= ' /ColorSpace /'.$grad['colspace'];
18642 } else {
18643 $out .= ' /ColorSpace /DeviceRGB';
18645 if (isset($grad['background']) AND !empty($grad['background'])) {
18646 $out .= ' /Background ['.$grad['background'].']';
18648 if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
18649 $out .= ' /AntiAlias true';
18651 if ($grad['type'] == 2) {
18652 $out .= ' '.sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
18653 $out .= ' /Domain [0 1]';
18654 $out .= ' /Function '.$fc.' 0 R';
18655 $out .= ' /Extend [true true]';
18656 $out .= ' >>';
18657 } elseif ($grad['type'] == 3) {
18658 //x0, y0, r0, x1, y1, r1
18659 //at this this time radius of inner circle is 0
18660 $out .= ' '.sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
18661 $out .= ' /Domain [0 1]';
18662 $out .= ' /Function '.$fc.' 0 R';
18663 $out .= ' /Extend [true true]';
18664 $out .= ' >>';
18665 } elseif ($grad['type'] == 6) {
18666 $out .= ' /BitsPerCoordinate 16';
18667 $out .= ' /BitsPerComponent 8';
18668 $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
18669 $out .= ' /BitsPerFlag 8';
18670 $stream = $this->_getrawstream($grad['stream']);
18671 $out .= ' /Length '.strlen($stream);
18672 $out .= ' >>';
18673 $out .= ' stream'."\n".$stream."\n".'endstream';
18675 $out .= "\n".'endobj';
18676 $this->_out($out);
18677 if ($grad['transparency']) {
18678 $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
18679 $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
18681 $this->gradients[$id]['id'] = $this->n;
18682 // set pattern object
18683 $this->_newobj();
18684 $out = '<< /Type /Pattern /PatternType 2';
18685 $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
18686 $out .= ' >>';
18687 $out .= "\n".'endobj';
18688 $this->_out($out);
18689 $this->gradients[$id]['pattern'] = $this->n;
18690 // set shading and pattern for transparency mask
18691 if ($grad['transparency']) {
18692 // luminosity pattern
18693 $idgs = $id + $idt;
18694 $this->_newobj();
18695 $this->_out($shading_transparency);
18696 $this->gradients[$idgs]['id'] = $this->n;
18697 $this->_newobj();
18698 $out = '<< /Type /Pattern /PatternType 2';
18699 $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
18700 $out .= ' >>';
18701 $out .= "\n".'endobj';
18702 $this->_out($out);
18703 $this->gradients[$idgs]['pattern'] = $this->n;
18704 // luminosity XObject
18705 $oid = $this->_newobj();
18706 $this->xobjects['LX'.$oid] = array('n' => $oid);
18707 $filter = '';
18708 $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
18709 if ($this->compress) {
18710 $filter = ' /Filter /FlateDecode';
18711 $stream = gzcompress($stream);
18713 $stream = $this->_getrawstream($stream);
18714 $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
18715 $out .= ' /Length '.strlen($stream);
18716 $rect = sprintf('%.2F %.2F', $this->wPt, $this->hPt);
18717 $out .= ' /BBox [0 0 '.$rect.']';
18718 $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
18719 $out .= ' /Resources <<';
18720 $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
18721 $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
18722 $out .= ' >>';
18723 $out .= ' >> ';
18724 $out .= ' stream'."\n".$stream."\n".'endstream';
18725 $out .= "\n".'endobj';
18726 $this->_out($out);
18727 // SMask
18728 $this->_newobj();
18729 $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
18730 $this->_out($out);
18731 // ExtGState
18732 $this->_newobj();
18733 $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
18734 $this->_out($out);
18735 $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
18741 * Draw the sector of a circle.
18742 * It can be used for instance to render pie charts.
18743 * @param $xc (float) abscissa of the center.
18744 * @param $yc (float) ordinate of the center.
18745 * @param $r (float) radius.
18746 * @param $a (float) start angle (in degrees).
18747 * @param $b (float) end angle (in degrees).
18748 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
18749 * @param $cw: (float) indicates whether to go clockwise (default: true).
18750 * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
18751 * @author Maxime Delorme, Nicola Asuni
18752 * @since 3.1.000 (2008-06-09)
18753 * @public
18755 public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
18756 $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
18760 * Draw the sector of an ellipse.
18761 * It can be used for instance to render pie charts.
18762 * @param $xc (float) abscissa of the center.
18763 * @param $yc (float) ordinate of the center.
18764 * @param $rx (float) the x-axis radius.
18765 * @param $ry (float) the y-axis radius.
18766 * @param $a (float) start angle (in degrees).
18767 * @param $b (float) end angle (in degrees).
18768 * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
18769 * @param $cw: (float) indicates whether to go clockwise.
18770 * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
18771 * @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
18772 * @author Maxime Delorme, Nicola Asuni
18773 * @since 3.1.000 (2008-06-09)
18774 * @public
18776 public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
18777 if ($this->rtl) {
18778 $xc = $this->w - $xc;
18780 $op = $this->getPathPaintOperator($style);
18781 if ($op == 'f') {
18782 $line_style = array();
18784 if ($cw) {
18785 $d = $b;
18786 $b = 360 - $a + $o;
18787 $a = 360 - $d + $o;
18788 } else {
18789 $b += $o;
18790 $a += $o;
18792 $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
18793 $this->_out($op);
18797 * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
18798 * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
18799 * Only vector drawing is supported, not text or bitmap.
18800 * Although the script was successfully tested with various AI format versions, best results are probably achieved with files that were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
18801 * @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
18802 * @param $x (float) Abscissa of the upper-left corner.
18803 * @param $y (float) Ordinate of the upper-left corner.
18804 * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
18805 * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
18806 * @param $link (mixed) URL or identifier returned by AddLink().
18807 * @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
18808 * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
18809 * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
18810 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
18811 * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
18812 * @param $fixoutvals (boolean) if true remove values outside the bounding box.
18813 * @author Valentin Schmidt, Nicola Asuni
18814 * @since 3.1.000 (2008-06-09)
18815 * @public
18817 public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
18818 if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
18819 // convert EPS to raster image using GD or ImageMagick libraries
18820 return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
18822 if ($x === '') {
18823 $x = $this->x;
18825 if ($y === '') {
18826 $y = $this->y;
18828 // check page for no-write regions and adapt page margins if necessary
18829 list($x, $y) = $this->checkPageRegions($h, $x, $y);
18830 $k = $this->k;
18831 if ($file{0} === '@') { // image from string
18832 $data = substr($file, 1);
18833 } else { // EPS/AI file
18834 $data = file_get_contents($file);
18836 if ($data === false) {
18837 $this->Error('EPS file not found: '.$file);
18839 $regs = array();
18840 // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
18841 preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
18842 if (count($regs) > 1) {
18843 $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
18844 if (strpos($version_str, 'Adobe Illustrator') !== false) {
18845 $versexp = explode(' ', $version_str);
18846 $version = (float)array_pop($versexp);
18847 if ($version >= 9) {
18848 $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
18852 // strip binary bytes in front of PS-header
18853 $start = strpos($data, '%!PS-Adobe');
18854 if ($start > 0) {
18855 $data = substr($data, $start);
18857 // find BoundingBox params
18858 preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
18859 if (count($regs) > 1) {
18860 list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
18861 } else {
18862 $this->Error('No BoundingBox found in EPS/AI file: '.$file);
18864 $start = strpos($data, '%%EndSetup');
18865 if ($start === false) {
18866 $start = strpos($data, '%%EndProlog');
18868 if ($start === false) {
18869 $start = strpos($data, '%%BoundingBox');
18871 $data = substr($data, $start);
18872 $end = strpos($data, '%%PageTrailer');
18873 if ($end===false) {
18874 $end = strpos($data, 'showpage');
18876 if ($end) {
18877 $data = substr($data, 0, $end);
18879 // calculate image width and height on document
18880 if (($w <= 0) AND ($h <= 0)) {
18881 $w = ($x2 - $x1) / $k;
18882 $h = ($y2 - $y1) / $k;
18883 } elseif ($w <= 0) {
18884 $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
18885 } elseif ($h <= 0) {
18886 $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
18888 // fit the image on available space
18889 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
18890 if ($this->rasterize_vector_images) {
18891 // convert EPS to raster image using GD or ImageMagick libraries
18892 return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
18894 // set scaling factors
18895 $scale_x = $w / (($x2 - $x1) / $k);
18896 $scale_y = $h / (($y2 - $y1) / $k);
18897 // set alignment
18898 $this->img_rb_y = $y + $h;
18899 // set alignment
18900 if ($this->rtl) {
18901 if ($palign == 'L') {
18902 $ximg = $this->lMargin;
18903 } elseif ($palign == 'C') {
18904 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
18905 } elseif ($palign == 'R') {
18906 $ximg = $this->w - $this->rMargin - $w;
18907 } else {
18908 $ximg = $x - $w;
18910 $this->img_rb_x = $ximg;
18911 } else {
18912 if ($palign == 'L') {
18913 $ximg = $this->lMargin;
18914 } elseif ($palign == 'C') {
18915 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
18916 } elseif ($palign == 'R') {
18917 $ximg = $this->w - $this->rMargin - $w;
18918 } else {
18919 $ximg = $x;
18921 $this->img_rb_x = $ximg + $w;
18923 if ($useBoundingBox) {
18924 $dx = $ximg * $k - $x1;
18925 $dy = $y * $k - $y1;
18926 } else {
18927 $dx = $ximg * $k;
18928 $dy = $y * $k;
18930 // save the current graphic state
18931 $this->_out('q'.$this->epsmarker);
18932 // translate
18933 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
18934 // scale
18935 if (isset($scale_x)) {
18936 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
18938 // handle pc/unix/mac line endings
18939 $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
18940 $u=0;
18941 $cnt = count($lines);
18942 for ($i=0; $i < $cnt; ++$i) {
18943 $line = $lines[$i];
18944 if (($line == '') OR ($line{0} == '%')) {
18945 continue;
18947 $len = strlen($line);
18948 // check for spot color names
18949 $color_name = '';
18950 if (strcasecmp('x', substr(trim($line), -1)) == 0) {
18951 if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
18952 // extract spot color name
18953 $color_name = $matches[0];
18954 // remove color name from string
18955 $line = str_replace(' '.$color_name, '', $line);
18956 // remove pharentesis from color name
18957 $color_name = substr($color_name, 1, -1);
18960 $chunks = explode(' ', $line);
18961 $cmd = trim(array_pop($chunks));
18962 // RGB
18963 if (($cmd == 'Xa') OR ($cmd == 'XA')) {
18964 $b = array_pop($chunks);
18965 $g = array_pop($chunks);
18966 $r = array_pop($chunks);
18967 $this->_out(''.$r.' '.$g.' '.$b.' '.($cmd=='Xa'?'rg':'RG')); //substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
18968 continue;
18970 $skip = false;
18971 if ($fixoutvals) {
18972 // check for values outside the bounding box
18973 switch ($cmd) {
18974 case 'm':
18975 case 'l':
18976 case 'L': {
18977 // skip values outside bounding box
18978 foreach ($chunks as $key => $val) {
18979 if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
18980 $skip = true;
18981 } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
18982 $skip = true;
18988 switch ($cmd) {
18989 case 'm':
18990 case 'l':
18991 case 'v':
18992 case 'y':
18993 case 'c':
18994 case 'k':
18995 case 'K':
18996 case 'g':
18997 case 'G':
18998 case 's':
18999 case 'S':
19000 case 'J':
19001 case 'j':
19002 case 'w':
19003 case 'M':
19004 case 'd':
19005 case 'n': {
19006 if ($skip) {
19007 break;
19009 $this->_out($line);
19010 break;
19012 case 'x': {// custom fill color
19013 if (empty($color_name)) {
19014 // CMYK color
19015 list($col_c, $col_m, $col_y, $col_k) = $chunks;
19016 $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
19017 } else {
19018 // Spot Color (CMYK + tint)
19019 list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
19020 $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
19021 $color_cmd = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
19022 $this->_out($color_cmd);
19024 break;
19026 case 'X': { // custom stroke color
19027 if (empty($color_name)) {
19028 // CMYK color
19029 list($col_c, $col_m, $col_y, $col_k) = $chunks;
19030 $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
19031 } else {
19032 // Spot Color (CMYK + tint)
19033 list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
19034 $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
19035 $color_cmd = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
19036 $this->_out($color_cmd);
19038 break;
19040 case 'Y':
19041 case 'N':
19042 case 'V':
19043 case 'L':
19044 case 'C': {
19045 if ($skip) {
19046 break;
19048 $line{$len-1} = strtolower($cmd);
19049 $this->_out($line);
19050 break;
19052 case 'b':
19053 case 'B': {
19054 $this->_out($cmd . '*');
19055 break;
19057 case 'f':
19058 case 'F': {
19059 if ($u > 0) {
19060 $isU = false;
19061 $max = min(($i + 5), $cnt);
19062 for ($j = ($i + 1); $j < $max; ++$j) {
19063 $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
19065 if ($isU) {
19066 $this->_out('f*');
19068 } else {
19069 $this->_out('f*');
19071 break;
19073 case '*u': {
19074 ++$u;
19075 break;
19077 case '*U': {
19078 --$u;
19079 break;
19083 // restore previous graphic state
19084 $this->_out($this->epsmarker.'Q');
19085 if (!empty($border)) {
19086 $bx = $this->x;
19087 $by = $this->y;
19088 $this->x = $ximg;
19089 if ($this->rtl) {
19090 $this->x += $w;
19092 $this->y = $y;
19093 $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
19094 $this->x = $bx;
19095 $this->y = $by;
19097 if ($link) {
19098 $this->Link($ximg, $y, $w, $h, $link, 0);
19100 // set pointer to align the next text/objects
19101 switch($align) {
19102 case 'T':{
19103 $this->y = $y;
19104 $this->x = $this->img_rb_x;
19105 break;
19107 case 'M':{
19108 $this->y = $y + round($h/2);
19109 $this->x = $this->img_rb_x;
19110 break;
19112 case 'B':{
19113 $this->y = $this->img_rb_y;
19114 $this->x = $this->img_rb_x;
19115 break;
19117 case 'N':{
19118 $this->SetY($this->img_rb_y);
19119 break;
19121 default:{
19122 break;
19125 $this->endlinex = $this->img_rb_x;
19129 * Set document barcode.
19130 * @param $bc (string) barcode
19131 * @public
19133 public function setBarcode($bc='') {
19134 $this->barcode = $bc;
19138 * Get current barcode.
19139 * @return string
19140 * @public
19141 * @since 4.0.012 (2008-07-24)
19143 public function getBarcode() {
19144 return $this->barcode;
19148 * Print a Linear Barcode.
19149 * @param $code (string) code to print
19150 * @param $type (string) type of barcode (see barcodes.php for supported formats).
19151 * @param $x (int) x position in user units (empty string = current x position)
19152 * @param $y (int) y position in user units (empty string = current y position)
19153 * @param $w (int) width in user units (empty string = remaining page width)
19154 * @param $h (int) height in user units (empty string = remaining page height)
19155 * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
19156 * @param $style (array) array of options:<ul>
19157 * <li>boolean $style['border'] if true prints a border</li>
19158 * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
19159 * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
19160 * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
19161 * <li>array $style['fgcolor'] color array for bars and text</li>
19162 * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
19163 * <li>boolean $style['text'] if true prints text below the barcode</li>
19164 * <li>string $style['label'] override default label</li>
19165 * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
19166 * <li>int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing.</li>
19167 * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
19168 * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
19169 * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
19170 * <li>string $style['fitwidth'] if true reduce the width to fit the barcode width + padding. When this option is enabled the 'stretch' option is automatically disabled.</li>
19171 * <li>string $style['cellfitalign'] this option works only when 'fitwidth' is true and 'position' is unset or empty. Set the horizontal position of the containing barcode cell inside the specified rectangle: L = left; C = center; R = right.</li></ul>
19172 * @param $align (string) Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
19173 * @author Nicola Asuni
19174 * @since 3.1.000 (2008-06-09)
19175 * @public
19177 public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres='', $style='', $align='') {
19178 if ($this->empty_string(trim($code))) {
19179 return;
19181 require_once(dirname(__FILE__).'/barcodes.php');
19182 // save current graphic settings
19183 $gvars = $this->getGraphicVars();
19184 // create new barcode object
19185 $barcodeobj = new TCPDFBarcode($code, $type);
19186 $arrcode = $barcodeobj->getBarcodeArray();
19187 if ($arrcode === false) {
19188 $this->Error('Error in 1D barcode string');
19190 // set default values
19191 if (!isset($style['position'])) {
19192 $style['position'] = '';
19193 } elseif ($style['position'] == 'S') {
19194 // keep this for backward compatibility
19195 $style['position'] = '';
19196 $style['stretch'] = true;
19198 if (!isset($style['fitwidth'])) {
19199 if (!isset($style['stretch'])) {
19200 $style['fitwidth'] = true;
19201 } else {
19202 $style['fitwidth'] = false;
19205 if ($style['fitwidth']) {
19206 // disable stretch
19207 $style['stretch'] = false;
19209 if (!isset($style['stretch'])) {
19210 if (($w === '') OR ($w <= 0)) {
19211 $style['stretch'] = false;
19212 } else {
19213 $style['stretch'] = true;
19216 if (!isset($style['fgcolor'])) {
19217 $style['fgcolor'] = array(0,0,0); // default black
19219 if (!isset($style['bgcolor'])) {
19220 $style['bgcolor'] = false; // default transparent
19222 if (!isset($style['border'])) {
19223 $style['border'] = false;
19225 $fontsize = 0;
19226 if (!isset($style['text'])) {
19227 $style['text'] = false;
19229 if ($style['text'] AND isset($style['font'])) {
19230 if (isset($style['fontsize'])) {
19231 $fontsize = $style['fontsize'];
19233 $this->SetFont($style['font'], '', $fontsize);
19235 if (!isset($style['stretchtext'])) {
19236 $style['stretchtext'] = 4;
19238 if ($x === '') {
19239 $x = $this->x;
19241 if ($y === '') {
19242 $y = $this->y;
19244 // check page for no-write regions and adapt page margins if necessary
19245 list($x, $y) = $this->checkPageRegions($h, $x, $y);
19246 if (($w === '') OR ($w <= 0)) {
19247 if ($this->rtl) {
19248 $w = $x - $this->lMargin;
19249 } else {
19250 $w = $this->w - $this->rMargin - $x;
19253 // padding
19254 if (!isset($style['padding'])) {
19255 $padding = 0;
19256 } elseif ($style['padding'] === 'auto') {
19257 $padding = 10 * ($w / ($arrcode['maxw'] + 20));
19258 } else {
19259 $padding = floatval($style['padding']);
19261 // horizontal padding
19262 if (!isset($style['hpadding'])) {
19263 $hpadding = $padding;
19264 } elseif ($style['hpadding'] === 'auto') {
19265 $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
19266 } else {
19267 $hpadding = floatval($style['hpadding']);
19269 // vertical padding
19270 if (!isset($style['vpadding'])) {
19271 $vpadding = $padding;
19272 } elseif ($style['vpadding'] === 'auto') {
19273 $vpadding = ($hpadding / 2);
19274 } else {
19275 $vpadding = floatval($style['vpadding']);
19277 // calculate xres (single bar width)
19278 $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
19279 if ($style['stretch']) {
19280 $xres = $max_xres;
19281 } else {
19282 if ($this->empty_string($xres)) {
19283 $xres = (0.141 * $this->k); // default bar width = 0.4 mm
19285 if ($xres > $max_xres) {
19286 // correct xres to fit on $w
19287 $xres = $max_xres;
19289 if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
19290 OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
19291 $hpadding = 10 * $xres;
19292 if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
19293 $vpadding = ($hpadding / 2);
19297 if ($style['fitwidth']) {
19298 $wold = $w;
19299 $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
19300 if (isset($style['cellfitalign'])) {
19301 switch ($style['cellfitalign']) {
19302 case 'L': {
19303 if ($this->rtl) {
19304 $x -= ($wold - $w);
19306 break;
19308 case 'R': {
19309 if (!$this->rtl) {
19310 $x += ($wold - $w);
19312 break;
19314 case 'C': {
19315 if ($this->rtl) {
19316 $x -= (($wold - $w) / 2);
19317 } else {
19318 $x += (($wold - $w) / 2);
19320 break;
19322 default : {
19323 break;
19328 $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
19329 // height
19330 if (($h === '') OR ($h <= 0)) {
19331 // set default height
19332 $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
19334 $barh = $h - $text_height - (2 * $vpadding);
19335 if ($barh <=0) {
19336 // try to reduce font or padding to fit barcode on available height
19337 if ($text_height > $h) {
19338 $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
19339 $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
19340 $this->SetFont($style['font'], '', $fontsize);
19342 if ($vpadding > 0) {
19343 $vpadding = (($h - $text_height) / 4);
19345 $barh = $h - $text_height - (2 * $vpadding);
19347 // fit the barcode on available space
19348 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
19349 // set alignment
19350 $this->img_rb_y = $y + $h;
19351 // set alignment
19352 if ($this->rtl) {
19353 if ($style['position'] == 'L') {
19354 $xpos = $this->lMargin;
19355 } elseif ($style['position'] == 'C') {
19356 $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
19357 } elseif ($style['position'] == 'R') {
19358 $xpos = $this->w - $this->rMargin - $w;
19359 } else {
19360 $xpos = $x - $w;
19362 $this->img_rb_x = $xpos;
19363 } else {
19364 if ($style['position'] == 'L') {
19365 $xpos = $this->lMargin;
19366 } elseif ($style['position'] == 'C') {
19367 $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
19368 } elseif ($style['position'] == 'R') {
19369 $xpos = $this->w - $this->rMargin - $w;
19370 } else {
19371 $xpos = $x;
19373 $this->img_rb_x = $xpos + $w;
19375 $xpos_rect = $xpos;
19376 if (!isset($style['align'])) {
19377 $style['align'] = 'C';
19379 switch ($style['align']) {
19380 case 'L': {
19381 $xpos = $xpos_rect + $hpadding;
19382 break;
19384 case 'R': {
19385 $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
19386 break;
19388 case 'C':
19389 default : {
19390 $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
19391 break;
19394 $xpos_text = $xpos;
19395 // barcode is always printed in LTR direction
19396 $tempRTL = $this->rtl;
19397 $this->rtl = false;
19398 // print background color
19399 if ($style['bgcolor']) {
19400 $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
19401 } elseif ($style['border']) {
19402 $this->Rect($xpos_rect, $y, $w, $h, 'D');
19404 // set foreground color
19405 $this->SetDrawColorArray($style['fgcolor']);
19406 $this->SetTextColorArray($style['fgcolor']);
19407 // print bars
19408 foreach ($arrcode['bcode'] as $k => $v) {
19409 $bw = ($v['w'] * $xres);
19410 if ($v['t']) {
19411 // draw a vertical bar
19412 $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
19413 $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
19415 $xpos += $bw;
19417 // print text
19418 if ($style['text']) {
19419 if (isset($style['label']) AND !$this->empty_string($style['label'])) {
19420 $label = $style['label'];
19421 } else {
19422 $label = $code;
19424 $txtwidth = ($arrcode['maxw'] * $xres);
19425 if ($this->GetStringWidth($label) > $txtwidth) {
19426 $style['stretchtext'] = 2;
19428 // print text
19429 $this->x = $xpos_text;
19430 $this->y = $y + $vpadding + $barh;
19431 $cellpadding = $this->cell_padding;
19432 $this->SetCellPadding(0);
19433 $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
19434 $this->cell_padding = $cellpadding;
19436 // restore original direction
19437 $this->rtl = $tempRTL;
19438 // restore previous settings
19439 $this->setGraphicVars($gvars);
19440 // set pointer to align the next text/objects
19441 switch($align) {
19442 case 'T':{
19443 $this->y = $y;
19444 $this->x = $this->img_rb_x;
19445 break;
19447 case 'M':{
19448 $this->y = $y + round($h / 2);
19449 $this->x = $this->img_rb_x;
19450 break;
19452 case 'B':{
19453 $this->y = $this->img_rb_y;
19454 $this->x = $this->img_rb_x;
19455 break;
19457 case 'N':{
19458 $this->SetY($this->img_rb_y);
19459 break;
19461 default:{
19462 break;
19465 $this->endlinex = $this->img_rb_x;
19469 * This function is DEPRECATED, please use the new write1DBarcode() function.
19470 * @param $x (int) x position in user units
19471 * @param $y (int) y position in user units
19472 * @param $w (int) width in user units
19473 * @param $h (int) height position in user units
19474 * @param $type (string) type of barcode
19475 * @param $style (string) barcode style
19476 * @param $font (string) font for text
19477 * @param $xres (int) x resolution
19478 * @param $code (string) code to print
19479 * @deprecated deprecated since version 3.1.000 (2008-06-10)
19480 * @public
19481 * @see write1DBarcode()
19483 public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
19484 // convert old settings for the new write1DBarcode() function.
19485 $xres = 1 / $xres;
19486 $newstyle = array(
19487 'position' => '',
19488 'align' => '',
19489 'stretch' => false,
19490 'fitwidth' => false,
19491 'cellfitalign' => '',
19492 'border' => false,
19493 'padding' => 0,
19494 'fgcolor' => array(0,0,0),
19495 'bgcolor' => false,
19496 'text' => true,
19497 'font' => $font,
19498 'fontsize' => 8,
19499 'stretchtext' => 4
19501 if ($style & 1) {
19502 $newstyle['border'] = true;
19504 if ($style & 2) {
19505 $newstyle['bgcolor'] = false;
19507 if ($style & 4) {
19508 $newstyle['position'] = 'C';
19509 } elseif ($style & 8) {
19510 $newstyle['position'] = 'L';
19511 } elseif ($style & 16) {
19512 $newstyle['position'] = 'R';
19514 if ($style & 128) {
19515 $newstyle['text'] = true;
19517 if ($style & 256) {
19518 $newstyle['stretchtext'] = 4;
19520 $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
19524 * Print 2D Barcode.
19525 * @param $code (string) code to print
19526 * @param $type (string) type of barcode (see 2dbarcodes.php for supported formats).
19527 * @param $x (int) x position in user units
19528 * @param $y (int) y position in user units
19529 * @param $w (int) width in user units
19530 * @param $h (int) height in user units
19531 * @param $style (array) array of options:<ul>
19532 * <li>boolean $style['border'] if true prints a border around the barcode</li>
19533 * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
19534 * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
19535 * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
19536 * <li>int $style['module_width'] width of a single module in points</li>
19537 * <li>int $style['module_height'] height of a single module in points</li>
19538 * <li>array $style['fgcolor'] color array for bars and text</li>
19539 * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
19540 * <li>string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch</li><li>$style['module_width'] width of a single module in points</li>
19541 * <li>$style['module_height'] height of a single module in points</li></ul>
19542 * @param $align (string) Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
19543 * @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
19544 * @author Nicola Asuni
19545 * @since 4.5.037 (2009-04-07)
19546 * @public
19548 public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='', $distort=false) {
19549 if ($this->empty_string(trim($code))) {
19550 return;
19552 require_once(dirname(__FILE__).'/2dbarcodes.php');
19553 // save current graphic settings
19554 $gvars = $this->getGraphicVars();
19555 // create new barcode object
19556 $barcodeobj = new TCPDF2DBarcode($code, $type);
19557 $arrcode = $barcodeobj->getBarcodeArray();
19558 if (($arrcode === false) OR empty($arrcode)) {
19559 $this->Error('Error in 2D barcode string');
19561 // set default values
19562 if (!isset($style['position'])) {
19563 $style['position'] = '';
19565 if (!isset($style['fgcolor'])) {
19566 $style['fgcolor'] = array(0,0,0); // default black
19568 if (!isset($style['bgcolor'])) {
19569 $style['bgcolor'] = false; // default transparent
19571 if (!isset($style['border'])) {
19572 $style['border'] = false;
19574 // padding
19575 if (!isset($style['padding'])) {
19576 $style['padding'] = 0;
19577 } elseif ($style['padding'] === 'auto') {
19578 $style['padding'] = 4;
19580 if (!isset($style['hpadding'])) {
19581 $style['hpadding'] = $style['padding'];
19582 } elseif ($style['hpadding'] === 'auto') {
19583 $style['hpadding'] = 4;
19585 if (!isset($style['vpadding'])) {
19586 $style['vpadding'] = $style['padding'];
19587 } elseif ($style['vpadding'] === 'auto') {
19588 $style['vpadding'] = 4;
19590 $hpad = (2 * $style['hpadding']);
19591 $vpad = (2 * $style['vpadding']);
19592 // cell (module) dimension
19593 if (!isset($style['module_width'])) {
19594 $style['module_width'] = 1; // width of a single module in points
19596 if (!isset($style['module_height'])) {
19597 $style['module_height'] = 1; // height of a single module in points
19599 if ($x === '') {
19600 $x = $this->x;
19602 if ($y === '') {
19603 $y = $this->y;
19605 // check page for no-write regions and adapt page margins if necessary
19606 list($x, $y) = $this->checkPageRegions($h, $x, $y);
19607 // number of barcode columns and rows
19608 $rows = $arrcode['num_rows'];
19609 $cols = $arrcode['num_cols'];
19610 // module width and height
19611 $mw = $style['module_width'];
19612 $mh = $style['module_height'];
19613 // get max dimensions
19614 if ($this->rtl) {
19615 $maxw = $x - $this->lMargin;
19616 } else {
19617 $maxw = $this->w - $this->rMargin - $x;
19619 $maxh = ($this->h - $this->tMargin - $this->bMargin);
19620 $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
19621 $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
19622 if (!$distort) {
19623 if (($maxw * $ratioHW) > $maxh) {
19624 $maxw = $maxh * $ratioWH;
19626 if (($maxh * $ratioWH) > $maxw) {
19627 $maxh = $maxw * $ratioHW;
19630 // set maximum dimesions
19631 if ($w > $maxw) {
19632 $w = $maxw;
19634 if ($h > $maxh) {
19635 $h = $maxh;
19637 // set dimensions
19638 if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
19639 $w = ($cols + $hpad) * ($mw / $this->k);
19640 $h = ($rows + $vpad) * ($mh / $this->k);
19641 } elseif (($w === '') OR ($w <= 0)) {
19642 $w = $h * $ratioWH;
19643 } elseif (($h === '') OR ($h <= 0)) {
19644 $h = $w * $ratioHW;
19646 // barcode size (excluding padding)
19647 $bw = ($w * $cols) / ($cols + $hpad);
19648 $bh = ($h * $rows) / ($rows + $vpad);
19649 // dimension of single barcode cell unit
19650 $cw = $bw / $cols;
19651 $ch = $bh / $rows;
19652 if (!$distort) {
19653 if (($cw / $ch) > ($mw / $mh)) {
19654 // correct horizontal distortion
19655 $cw = $ch * $mw / $mh;
19656 $bw = $cw * $cols;
19657 $style['hpadding'] = ($w - $bw) / (2 * $cw);
19658 } else {
19659 // correct vertical distortion
19660 $ch = $cw * $mh / $mw;
19661 $bh = $ch * $rows;
19662 $style['vpadding'] = ($h - $bh) / (2 * $ch);
19665 // fit the barcode on available space
19666 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
19667 // set alignment
19668 $this->img_rb_y = $y + $h;
19669 // set alignment
19670 if ($this->rtl) {
19671 if ($style['position'] == 'L') {
19672 $xpos = $this->lMargin;
19673 } elseif ($style['position'] == 'C') {
19674 $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
19675 } elseif ($style['position'] == 'R') {
19676 $xpos = $this->w - $this->rMargin - $w;
19677 } else {
19678 $xpos = $x - $w;
19680 $this->img_rb_x = $xpos;
19681 } else {
19682 if ($style['position'] == 'L') {
19683 $xpos = $this->lMargin;
19684 } elseif ($style['position'] == 'C') {
19685 $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
19686 } elseif ($style['position'] == 'R') {
19687 $xpos = $this->w - $this->rMargin - $w;
19688 } else {
19689 $xpos = $x;
19691 $this->img_rb_x = $xpos + $w;
19693 $xstart = $xpos + ($style['hpadding'] * $cw);
19694 $ystart = $y + ($style['vpadding'] * $ch);
19695 // barcode is always printed in LTR direction
19696 $tempRTL = $this->rtl;
19697 $this->rtl = false;
19698 // print background color
19699 if ($style['bgcolor']) {
19700 $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
19701 } elseif ($style['border']) {
19702 $this->Rect($xpos, $y, $w, $h, 'D');
19704 // set foreground color
19705 $this->SetDrawColorArray($style['fgcolor']);
19706 // print barcode cells
19707 // for each row
19708 for ($r = 0; $r < $rows; ++$r) {
19709 $xr = $xstart;
19710 // for each column
19711 for ($c = 0; $c < $cols; ++$c) {
19712 if ($arrcode['bcode'][$r][$c] == 1) {
19713 // draw a single barcode cell
19714 $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
19716 $xr += $cw;
19718 $ystart += $ch;
19720 // restore original direction
19721 $this->rtl = $tempRTL;
19722 // restore previous settings
19723 $this->setGraphicVars($gvars);
19724 // set pointer to align the next text/objects
19725 switch($align) {
19726 case 'T':{
19727 $this->y = $y;
19728 $this->x = $this->img_rb_x;
19729 break;
19731 case 'M':{
19732 $this->y = $y + round($h/2);
19733 $this->x = $this->img_rb_x;
19734 break;
19736 case 'B':{
19737 $this->y = $this->img_rb_y;
19738 $this->x = $this->img_rb_x;
19739 break;
19741 case 'N':{
19742 $this->SetY($this->img_rb_y);
19743 break;
19745 default:{
19746 break;
19749 $this->endlinex = $this->img_rb_x;
19753 * Returns an array containing current margins:
19754 * <ul>
19755 <li>$ret['left'] = left margin</li>
19756 <li>$ret['right'] = right margin</li>
19757 <li>$ret['top'] = top margin</li>
19758 <li>$ret['bottom'] = bottom margin</li>
19759 <li>$ret['header'] = header margin</li>
19760 <li>$ret['footer'] = footer margin</li>
19761 <li>$ret['cell'] = cell padding array</li>
19762 <li>$ret['padding_left'] = cell left padding</li>
19763 <li>$ret['padding_top'] = cell top padding</li>
19764 <li>$ret['padding_right'] = cell right padding</li>
19765 <li>$ret['padding_bottom'] = cell bottom padding</li>
19766 * </ul>
19767 * @return array containing all margins measures
19768 * @public
19769 * @since 3.2.000 (2008-06-23)
19771 public function getMargins() {
19772 $ret = array(
19773 'left' => $this->lMargin,
19774 'right' => $this->rMargin,
19775 'top' => $this->tMargin,
19776 'bottom' => $this->bMargin,
19777 'header' => $this->header_margin,
19778 'footer' => $this->footer_margin,
19779 'cell' => $this->cell_padding,
19780 'padding_left' => $this->cell_padding['L'],
19781 'padding_top' => $this->cell_padding['T'],
19782 'padding_right' => $this->cell_padding['R'],
19783 'padding_bottom' => $this->cell_padding['B']
19785 return $ret;
19789 * Returns an array containing original margins:
19790 * <ul>
19791 <li>$ret['left'] = left margin</li>
19792 <li>$ret['right'] = right margin</li>
19793 * </ul>
19794 * @return array containing all margins measures
19795 * @public
19796 * @since 4.0.012 (2008-07-24)
19798 public function getOriginalMargins() {
19799 $ret = array(
19800 'left' => $this->original_lMargin,
19801 'right' => $this->original_rMargin
19803 return $ret;
19807 * Returns the current font size.
19808 * @return current font size
19809 * @public
19810 * @since 3.2.000 (2008-06-23)
19812 public function getFontSize() {
19813 return $this->FontSize;
19817 * Returns the current font size in points unit.
19818 * @return current font size in points unit
19819 * @public
19820 * @since 3.2.000 (2008-06-23)
19822 public function getFontSizePt() {
19823 return $this->FontSizePt;
19827 * Returns the current font family name.
19828 * @return string current font family name
19829 * @public
19830 * @since 4.3.008 (2008-12-05)
19832 public function getFontFamily() {
19833 return $this->FontFamily;
19837 * Returns the current font style.
19838 * @return string current font style
19839 * @public
19840 * @since 4.3.008 (2008-12-05)
19842 public function getFontStyle() {
19843 return $this->FontStyle;
19847 * Cleanup HTML code (requires HTML Tidy library).
19848 * @param $html (string) htmlcode to fix
19849 * @param $default_css (string) CSS commands to add
19850 * @param $tagvs (array) parameters for setHtmlVSpace method
19851 * @param $tidy_options (array) options for tidy_parse_string function
19852 * @return string XHTML code cleaned up
19853 * @author Nicola Asuni
19854 * @public
19855 * @since 5.9.017 (2010-11-16)
19856 * @see setHtmlVSpace()
19858 public function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='') {
19859 // configure parameters for HTML Tidy
19860 if ($tidy_options === '') {
19861 $tidy_options = array (
19862 'clean' => 1,
19863 'drop-empty-paras' => 0,
19864 'drop-proprietary-attributes' => 1,
19865 'fix-backslash' => 1,
19866 'hide-comments' => 1,
19867 'join-styles' => 1,
19868 'lower-literals' => 1,
19869 'merge-divs' => 1,
19870 'merge-spans' => 1,
19871 'output-xhtml' => 1,
19872 'word-2000' => 1,
19873 'wrap' => 0,
19874 'output-bom' => 0,
19875 //'char-encoding' => 'utf8',
19876 //'input-encoding' => 'utf8',
19877 //'output-encoding' => 'utf8'
19880 // clean up the HTML code
19881 $tidy = tidy_parse_string($html, $tidy_options);
19882 // fix the HTML
19883 $tidy->cleanRepair();
19884 // get the CSS part
19885 $tidy_head = tidy_get_head($tidy);
19886 $css = $tidy_head->value;
19887 $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
19888 $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
19889 $css = str_replace('/*<![CDATA[*/', '', $css);
19890 $css = str_replace('/*]]>*/', '', $css);
19891 preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
19892 if (isset($matches[1])) {
19893 $css = strtolower($matches[1]);
19894 } else {
19895 $css = '';
19897 // include default css
19898 $css = '<style>'.$default_css.$css.'</style>';
19899 // get the body part
19900 $tidy_body = tidy_get_body($tidy);
19901 $html = $tidy_body->value;
19902 // fix some self-closing tags
19903 $html = str_replace('<br>', '<br />', $html);
19904 // remove some empty tag blocks
19905 $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
19906 $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
19907 if ($tagvs !== '') {
19908 // set vertical space for some XHTML tags
19909 $this->setHtmlVSpace($tagvs);
19911 // return the cleaned XHTML code + CSS
19912 return $css.$html;
19916 * Extracts the CSS properties from a CSS string.
19917 * @param $cssdata (string) string containing CSS definitions.
19918 * @return An array where the keys are the CSS selectors and the values are the CSS properties.
19919 * @author Nicola Asuni
19920 * @since 5.1.000 (2010-05-25)
19921 * @protected
19923 protected function extractCSSproperties($cssdata) {
19924 if (empty($cssdata)) {
19925 return array();
19927 // remove comments
19928 $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
19929 // remove newlines and multiple spaces
19930 $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
19931 // remove some spaces
19932 $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
19933 // remove empty blocks
19934 $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
19935 // replace media type parenthesis
19936 $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1§', $cssdata);
19937 $cssdata = preg_replace('/\}\}/si', '}§', $cssdata);
19938 // trim string
19939 $cssdata = trim($cssdata);
19940 // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
19941 $cssblocks = array();
19942 $matches = array();
19943 if (preg_match_all('/@media[\s]+([^\§]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
19944 foreach ($matches[1] as $key => $type) {
19945 $cssblocks[$type] = $matches[2][$key];
19947 // remove media blocks
19948 $cssdata = preg_replace('/@media[\s]+([^\§]*)§([^§]*)§/i', '', $cssdata);
19950 // keep 'all' and 'print' media, other media types are discarded
19951 if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
19952 $cssdata .= $cssblocks['all'];
19954 if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
19955 $cssdata .= $cssblocks['print'];
19957 // reset css blocks array
19958 $cssblocks = array();
19959 $matches = array();
19960 // explode css data string into array
19961 if (substr($cssdata, -1) == '}') {
19962 // remove last parethesis
19963 $cssdata = substr($cssdata, 0, -1);
19965 $matches = explode('}', $cssdata);
19966 foreach ($matches as $key => $block) {
19967 // index 0 contains the CSS selector, index 1 contains CSS properties
19968 $cssblocks[$key] = explode('{', $block);
19969 if (!isset($cssblocks[$key][1])) {
19970 // remove empty definitions
19971 unset($cssblocks[$key]);
19974 // split groups of selectors (comma-separated list of selectors)
19975 foreach ($cssblocks as $key => $block) {
19976 if (strpos($block[0], ',') > 0) {
19977 $selectors = explode(',', $block[0]);
19978 foreach ($selectors as $sel) {
19979 $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
19981 unset($cssblocks[$key]);
19984 // covert array to selector => properties
19985 $cssdata = array();
19986 foreach ($cssblocks as $block) {
19987 $selector = $block[0];
19988 // calculate selector's specificity
19989 $matches = array();
19990 $a = 0; // the declaration is not from is a 'style' attribute
19991 $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
19992 $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
19993 $c += intval(preg_match_all('/[\:]link|visited|hover|active|focus|target|lang|enabled|disabled|checked|indeterminate|root|nth|first|last|only|empty|contains|not/i', $selector, $matches)); // number of pseudo-classes
19994 $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
19995 $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
19996 $specificity = $a.$b.$c.$d;
19997 // add specificity to the beginning of the selector
19998 $cssdata[$specificity.' '.$selector] = $block[1];
20000 // sort selectors alphabetically to account for specificity
20001 ksort($cssdata, SORT_STRING);
20002 // return array
20003 return $cssdata;
20007 * Returns true if the CSS selector is valid for the selected HTML tag
20008 * @param $dom (array) array of HTML tags and properties
20009 * @param $key (int) key of the current HTML tag
20010 * @param $selector (string) CSS selector string
20011 * @return true if the selector is valid, false otherwise
20012 * @protected
20013 * @since 5.1.000 (2010-05-25)
20015 protected function isValidCSSSelectorForTag($dom, $key, $selector) {
20016 $valid = false; // value to be returned
20017 $tag = $dom[$key]['value'];
20018 $class = array();
20019 if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
20020 $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
20022 $id = '';
20023 if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
20024 $id = strtolower($dom[$key]['attribute']['id']);
20026 $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
20027 $matches = array();
20028 if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
20029 $parentop = array_pop($matches[1]);
20030 $operator = $parentop[0];
20031 $offset = $parentop[1];
20032 $lasttag = array_pop($matches[2]);
20033 $lasttag = strtolower(trim($lasttag[0]));
20034 if (($lasttag == '*') OR ($lasttag == $tag)) {
20035 // the last element on selector is our tag or 'any tag'
20036 $attrib = array_pop($matches[3]);
20037 $attrib = strtolower(trim($attrib[0]));
20038 if (!empty($attrib)) {
20039 // check if matches class, id, attribute, pseudo-class or pseudo-element
20040 switch ($attrib{0}) {
20041 case '.': { // class
20042 if (in_array(substr($attrib, 1), $class)) {
20043 $valid = true;
20045 break;
20047 case '#': { // ID
20048 if (substr($attrib, 1) == $id) {
20049 $valid = true;
20051 break;
20053 case '[': { // attribute
20054 $attrmatch = array();
20055 if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
20056 $att = strtolower($attrmatch[1]);
20057 $val = $attrmatch[3];
20058 if (isset($dom[$key]['attribute'][$att])) {
20059 switch ($attrmatch[2]) {
20060 case '=': {
20061 if ($dom[$key]['attribute'][$att] == $val) {
20062 $valid = true;
20064 break;
20066 case '~=': {
20067 if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
20068 $valid = true;
20070 break;
20072 case '^=': {
20073 if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
20074 $valid = true;
20076 break;
20078 case '$=': {
20079 if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
20080 $valid = true;
20082 break;
20084 case '*=': {
20085 if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
20086 $valid = true;
20088 break;
20090 case '|=': {
20091 if ($dom[$key]['attribute'][$att] == $val) {
20092 $valid = true;
20093 } elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
20094 $valid = true;
20096 break;
20098 default: {
20099 $valid = true;
20104 break;
20106 case ':': { // pseudo-class or pseudo-element
20107 if ($attrib{1} == ':') { // pseudo-element
20108 // pseudo-elements are not supported!
20109 // (::first-line, ::first-letter, ::before, ::after)
20110 } else { // pseudo-class
20111 // pseudo-classes are not supported!
20112 // (:root, :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n), :first-child, :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, :link, :visited, :active, :hover, :focus, :target, :lang(fr), :enabled, :disabled, :checked)
20114 break;
20116 } // end of switch
20117 } else {
20118 $valid = true;
20120 if ($valid AND ($offset > 0)) {
20121 $valid = false;
20122 // check remaining selector part
20123 $selector = substr($selector, 0, $offset);
20124 switch ($operator) {
20125 case ' ': { // descendant of an element
20126 while ($dom[$key]['parent'] > 0) {
20127 if ($this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
20128 $valid = true;
20129 break;
20130 } else {
20131 $key = $dom[$key]['parent'];
20134 break;
20136 case '>': { // child of an element
20137 $valid = $this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
20138 break;
20140 case '+': { // immediately preceded by an element
20141 for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
20142 if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
20143 $valid = $this->isValidCSSSelectorForTag($dom, $i, $selector);
20144 break;
20147 break;
20149 case '~': { // preceded by an element
20150 for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
20151 if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
20152 if ($this->isValidCSSSelectorForTag($dom, $i, $selector)) {
20153 break;
20157 break;
20163 return $valid;
20167 * Returns the styles array that apply for the selected HTML tag.
20168 * @param $dom (array) array of HTML tags and properties
20169 * @param $key (int) key of the current HTML tag
20170 * @param $css (array) array of CSS properties
20171 * @return array containing CSS properties
20172 * @protected
20173 * @since 5.1.000 (2010-05-25)
20175 protected function getCSSdataArray($dom, $key, $css) {
20176 $cssarray = array(); // style to be returned
20177 // get parent CSS selectors
20178 $selectors = array();
20179 if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
20180 $selectors = $dom[($dom[$key]['parent'])]['csssel'];
20182 // get all styles that apply
20183 foreach($css as $selector => $style) {
20184 $pos = strpos($selector, ' ');
20185 // get specificity
20186 $specificity = substr($selector, 0, $pos);
20187 // remove specificity
20188 $selector = substr($selector, $pos);
20189 // check if this selector apply to current tag
20190 if ($this->isValidCSSSelectorForTag($dom, $key, $selector)) {
20191 if (!in_array($selector, $selectors)) {
20192 // add style if not already added on parent selector
20193 $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
20194 $selectors[] = $selector;
20198 if (isset($dom[$key]['attribute']['style'])) {
20199 // attach inline style (latest properties have high priority)
20200 $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
20202 // order the css array to account for specificity
20203 $cssordered = array();
20204 foreach ($cssarray as $key => $val) {
20205 $skey = sprintf('%04d', $key);
20206 $cssordered[$val['s'].'_'.$skey] = $val;
20208 // sort selectors alphabetically to account for specificity
20209 ksort($cssordered, SORT_STRING);
20210 return array($selectors, $cssordered);
20214 * Compact CSS data array into single string.
20215 * @param $css (array) array of CSS properties
20216 * @return string containing merged CSS properties
20217 * @protected
20218 * @since 5.9.070 (2011-04-19)
20220 protected function getTagStyleFromCSSarray($css) {
20221 $tagstyle = ''; // value to be returned
20222 foreach ($css as $style) {
20223 // split single css commands
20224 $csscmds = explode(';', $style['c']);
20225 foreach ($csscmds as $cmd) {
20226 if (!empty($cmd)) {
20227 $pos = strpos($cmd, ':');
20228 if ($pos !== false) {
20229 $cmd = substr($cmd, 0, ($pos + 1));
20230 if (strpos($tagstyle, $cmd) !== false) {
20231 // remove duplicate commands (last commands have high priority)
20232 $tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
20237 $tagstyle .= ';'.$style['c'];
20239 // remove multiple semicolons
20240 $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
20241 return $tagstyle;
20245 * Returns the border width from CSS property
20246 * @param $width (string) border width
20247 * @return int with in user units
20248 * @protected
20249 * @since 5.7.000 (2010-08-02)
20251 protected function getCSSBorderWidth($width) {
20252 if ($width == 'thin') {
20253 $width = (2 / $this->k);
20254 } elseif ($width == 'medium') {
20255 $width = (4 / $this->k);
20256 } elseif ($width == 'thick') {
20257 $width = (6 / $this->k);
20258 } else {
20259 $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
20261 return $width;
20265 * Returns the border dash style from CSS property
20266 * @param $style (string) border style to convert
20267 * @return int sash style (return -1 in case of none or hidden border)
20268 * @protected
20269 * @since 5.7.000 (2010-08-02)
20271 protected function getCSSBorderDashStyle($style) {
20272 switch (strtolower($style)) {
20273 case 'none':
20274 case 'hidden': {
20275 $dash = -1;
20276 break;
20278 case 'dotted': {
20279 $dash = 1;
20280 break;
20282 case 'dashed': {
20283 $dash = 3;
20284 break;
20286 case 'double':
20287 case 'groove':
20288 case 'ridge':
20289 case 'inset':
20290 case 'outset':
20291 case 'solid':
20292 default: {
20293 $dash = 0;
20294 break;
20297 return $dash;
20301 * Returns the border style array from CSS border properties
20302 * @param $cssborder (string) border properties
20303 * @return array containing border properties
20304 * @protected
20305 * @since 5.7.000 (2010-08-02)
20307 protected function getCSSBorderStyle($cssborder) {
20308 $bprop = preg_split('/[\s]+/', trim($cssborder));
20309 $border = array(); // value to be returned
20310 switch (count($bprop)) {
20311 case 3: {
20312 $width = $bprop[0];
20313 $style = $bprop[1];
20314 $color = $bprop[2];
20315 break;
20317 case 2: {
20318 $width = 'medium';
20319 $style = $bprop[0];
20320 $color = $bprop[1];
20321 break;
20323 case 1: {
20324 $width = 'medium';
20325 $style = $bprop[0];
20326 $color = 'black';
20327 break;
20329 default: {
20330 $width = 'medium';
20331 $style = 'solid';
20332 $color = 'black';
20333 break;
20336 if ($style == 'none') {
20337 return array();
20339 $border['cap'] = 'square';
20340 $border['join'] = 'miter';
20341 $border['dash'] = $this->getCSSBorderDashStyle($style);
20342 if ($border['dash'] < 0) {
20343 return array();
20345 $border['width'] = $this->getCSSBorderWidth($width);
20346 $border['color'] = $this->convertHTMLColorToDec($color);
20347 return $border;
20351 * Get the internal Cell padding from CSS attribute.
20352 * @param $csspadding (string) padding properties
20353 * @param $width (float) width of the containing element
20354 * @return array of cell paddings
20355 * @public
20356 * @since 5.9.000 (2010-10-04)
20358 public function getCSSPadding($csspadding, $width=0) {
20359 $padding = preg_split('/[\s]+/', trim($csspadding));
20360 $cell_padding = array(); // value to be returned
20361 switch (count($padding)) {
20362 case 4: {
20363 $cell_padding['T'] = $padding[0];
20364 $cell_padding['R'] = $padding[1];
20365 $cell_padding['B'] = $padding[2];
20366 $cell_padding['L'] = $padding[3];
20367 break;
20369 case 3: {
20370 $cell_padding['T'] = $padding[0];
20371 $cell_padding['R'] = $padding[1];
20372 $cell_padding['B'] = $padding[2];
20373 $cell_padding['L'] = $padding[1];
20374 break;
20376 case 2: {
20377 $cell_padding['T'] = $padding[0];
20378 $cell_padding['R'] = $padding[1];
20379 $cell_padding['B'] = $padding[0];
20380 $cell_padding['L'] = $padding[1];
20381 break;
20383 case 1: {
20384 $cell_padding['T'] = $padding[0];
20385 $cell_padding['R'] = $padding[0];
20386 $cell_padding['B'] = $padding[0];
20387 $cell_padding['L'] = $padding[0];
20388 break;
20390 default: {
20391 return $this->cell_padding;
20394 if ($width == 0) {
20395 $width = $this->w - $this->lMargin - $this->rMargin;
20397 $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
20398 $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
20399 $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
20400 $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
20401 return $cell_padding;
20405 * Get the internal Cell margin from CSS attribute.
20406 * @param $cssmargin (string) margin properties
20407 * @param $width (float) width of the containing element
20408 * @return array of cell margins
20409 * @public
20410 * @since 5.9.000 (2010-10-04)
20412 public function getCSSMargin($cssmargin, $width=0) {
20413 $margin = preg_split('/[\s]+/', trim($cssmargin));
20414 $cell_margin = array(); // value to be returned
20415 switch (count($margin)) {
20416 case 4: {
20417 $cell_margin['T'] = $margin[0];
20418 $cell_margin['R'] = $margin[1];
20419 $cell_margin['B'] = $margin[2];
20420 $cell_margin['L'] = $margin[3];
20421 break;
20423 case 3: {
20424 $cell_margin['T'] = $margin[0];
20425 $cell_margin['R'] = $margin[1];
20426 $cell_margin['B'] = $margin[2];
20427 $cell_margin['L'] = $margin[1];
20428 break;
20430 case 2: {
20431 $cell_margin['T'] = $margin[0];
20432 $cell_margin['R'] = $margin[1];
20433 $cell_margin['B'] = $margin[0];
20434 $cell_margin['L'] = $margin[1];
20435 break;
20437 case 1: {
20438 $cell_margin['T'] = $margin[0];
20439 $cell_margin['R'] = $margin[0];
20440 $cell_margin['B'] = $margin[0];
20441 $cell_margin['L'] = $margin[0];
20442 break;
20444 default: {
20445 return $this->cell_margin;
20448 if ($width == 0) {
20449 $width = $this->w - $this->lMargin - $this->rMargin;
20451 $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
20452 $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
20453 $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
20454 $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
20455 return $cell_margin;
20459 * Get the border-spacing from CSS attribute.
20460 * @param $cssbspace (string) border-spacing CSS properties
20461 * @param $width (float) width of the containing element
20462 * @return array of border spacings
20463 * @public
20464 * @since 5.9.010 (2010-10-27)
20466 public function getCSSBorderMargin($cssbspace, $width=0) {
20467 $space = preg_split('/[\s]+/', trim($cssbspace));
20468 $border_spacing = array(); // value to be returned
20469 switch (count($space)) {
20470 case 2: {
20471 $border_spacing['H'] = $space[0];
20472 $border_spacing['V'] = $space[1];
20473 break;
20475 case 1: {
20476 $border_spacing['H'] = $space[0];
20477 $border_spacing['V'] = $space[0];
20478 break;
20480 default: {
20481 return array('H' => 0, 'V' => 0);
20484 if ($width == 0) {
20485 $width = $this->w - $this->lMargin - $this->rMargin;
20487 $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
20488 $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
20489 return $border_spacing;
20493 * Returns the letter-spacing value from CSS value
20494 * @param $spacing (string) letter-spacing value
20495 * @param $parent (float) font spacing (tracking/kerning) value of the parent element
20496 * @return float quantity to increases or decreases the space between characters in a text.
20497 * @protected
20498 * @since 5.9.000 (2010-10-02)
20500 protected function getCSSFontSpacing($spacing, $parent=0) {
20501 $val = 0; // value to be returned
20502 $spacing = trim($spacing);
20503 switch ($spacing) {
20504 case 'normal': {
20505 $val = 0;
20506 break;
20508 case 'inherit': {
20509 if ($parent == 'normal') {
20510 $val = 0;
20511 } else {
20512 $val = $parent;
20514 break;
20516 default: {
20517 $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
20520 return $val;
20524 * Returns the percentage of font stretching from CSS value
20525 * @param $stretch (string) stretch mode
20526 * @param $parent (float) stretch value of the parent element
20527 * @return float font stretching percentage
20528 * @protected
20529 * @since 5.9.000 (2010-10-02)
20531 protected function getCSSFontStretching($stretch, $parent=100) {
20532 $val = 100; // value to be returned
20533 $stretch = trim($stretch);
20534 switch ($stretch) {
20535 case 'ultra-condensed': {
20536 $val = 40;
20537 break;
20539 case 'extra-condensed': {
20540 $val = 55;
20541 break;
20543 case 'condensed': {
20544 $val = 70;
20545 break;
20547 case 'semi-condensed': {
20548 $val = 85;
20549 break;
20551 case 'normal': {
20552 $val = 100;
20553 break;
20555 case 'semi-expanded': {
20556 $val = 115;
20557 break;
20559 case 'expanded': {
20560 $val = 130;
20561 break;
20563 case 'extra-expanded': {
20564 $val = 145;
20565 break;
20567 case 'ultra-expanded': {
20568 $val = 160;
20569 break;
20571 case 'wider': {
20572 $val = $parent + 10;
20573 break;
20575 case 'narrower': {
20576 $val = $parent - 10;
20577 break;
20579 case 'inherit': {
20580 if ($parent == 'normal') {
20581 $val = 100;
20582 } else {
20583 $val = $parent;
20585 break;
20587 default: {
20588 $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
20591 return $val;
20595 * Returns the HTML DOM array.
20596 * @param $html (string) html code
20597 * @return array
20598 * @protected
20599 * @since 3.2.000 (2008-06-20)
20601 protected function getHtmlDomArray($html) {
20602 // array of CSS styles ( selector => properties).
20603 $css = array();
20604 // get CSS array defined at previous call
20605 $matches = array();
20606 if (preg_match_all('/<cssarray>([^\<]*)<\/cssarray>/isU', $html, $matches) > 0) {
20607 if (isset($matches[1][0])) {
20608 $css = array_merge($css, unserialize($this->unhtmlentities($matches[1][0])));
20610 $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/isU', '', $html);
20612 // extract external CSS files
20613 $matches = array();
20614 if (preg_match_all('/<link([^\>]*)>/isU', $html, $matches) > 0) {
20615 foreach ($matches[1] as $key => $link) {
20616 $type = array();
20617 if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
20618 $type = array();
20619 preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
20620 // get 'all' and 'print' media, other media types are discarded
20621 // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
20622 if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
20623 $type = array();
20624 if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
20625 // read CSS data file
20626 $cssdata = file_get_contents(trim($type[1]));
20627 $css = array_merge($css, $this->extractCSSproperties($cssdata));
20633 // extract style tags
20634 $matches = array();
20635 if (preg_match_all('/<style([^\>]*)>([^\<]*)<\/style>/isU', $html, $matches) > 0) {
20636 foreach ($matches[1] as $key => $media) {
20637 $type = array();
20638 preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
20639 // get 'all' and 'print' media, other media types are discarded
20640 // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
20641 if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
20642 $cssdata = $matches[2][$key];
20643 $css = array_merge($css, $this->extractCSSproperties($cssdata));
20647 // create a special tag to contain the CSS array (used for table content)
20648 $csstagarray = '<cssarray>'.htmlentities(serialize($css)).'</cssarray>';
20649 // remove head and style blocks
20650 $html = preg_replace('/<head([^\>]*)>(.*?)<\/head>/siU', '', $html);
20651 $html = preg_replace('/<style([^\>]*)>([^\<]*)<\/style>/isU', '', $html);
20652 // define block tags
20653 $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
20654 // define self-closing tags
20655 $selfclosingtags = array('area','base','basefont','br','hr','input','img','link','meta');
20656 // remove all unsupported tags (the line below lists all supported tags)
20657 $html = strip_tags($html, '<marker/><a><b><blockquote><body><br><br/><dd><del><div><dl><dt><em><font><form><h1><h2><h3><h4><h5><h6><hr><hr/><i><img><input><label><li><ol><option><p><pre><s><select><small><span><strike><strong><sub><sup><table><tablehead><tcpdf><td><textarea><th><thead><tr><tt><u><ul>');
20658 //replace some blank characters
20659 $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
20660 $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dl|dt|form|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
20661 $html = preg_replace('@(\r\n|\r)@', "\n", $html);
20662 $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
20663 $html = strtr($html, $repTable);
20664 $offset = 0;
20665 while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
20666 $html_a = substr($html, 0, $offset);
20667 $html_b = substr($html, $offset, ($pos - $offset + 6));
20668 while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
20669 // preserve newlines on <pre> tag
20670 $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
20672 while (preg_match("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], $html_b)) {
20673 // preserve spaces on <pre> tag
20674 $html_b = preg_replace("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
20676 $html = $html_a.$html_b.substr($html, $pos + 6);
20677 $offset = strlen($html_a.$html_b);
20679 $offset = 0;
20680 while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
20681 $html_a = substr($html, 0, $offset);
20682 $html_b = substr($html, $offset, ($pos - $offset + 11));
20683 while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
20684 // preserve newlines on <textarea> tag
20685 $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
20686 $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
20688 $html = $html_a.$html_b.substr($html, $pos + 11);
20689 $offset = strlen($html_a.$html_b);
20691 $html = preg_replace('/([\s]*)<option/si', '<option', $html);
20692 $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
20693 $offset = 0;
20694 while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
20695 $html_a = substr($html, 0, $offset);
20696 $html_b = substr($html, $offset, ($pos - $offset + 9));
20697 while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
20698 $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
20699 $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
20701 $html = $html_a.$html_b.substr($html, $pos + 9);
20702 $offset = strlen($html_a.$html_b);
20704 if (preg_match("'</select'si", $html)) {
20705 $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
20706 $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
20708 $html = str_replace("\n", ' ', $html);
20709 // restore textarea newlines
20710 $html = str_replace('<TBR>', "\n", $html);
20711 // remove extra spaces from code
20712 $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
20713 $html = preg_replace('/'.$this->re_space['p'].'+<\/(td|th|li|dt|dd)>/'.$this->re_space['m'], '</\\1>', $html);
20714 $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
20715 $html = preg_replace('/'.$this->re_space['p'].'+<(ul|ol|dl|br)/'.$this->re_space['m'], '<\\1', $html);
20716 $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|dt|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
20717 $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
20718 $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
20719 $html = preg_replace('/'.$this->re_space['p'].'+<img/'.$this->re_space['m'], chr(32).'<img', $html);
20720 $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
20721 $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
20722 $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
20723 $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
20724 $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
20725 $html = preg_replace('/<li([^\>]*)>'.$this->re_space['p'].'*<img/'.$this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
20726 $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
20727 $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
20728 $html = preg_replace('/'.$this->re_space['p'].'+/'.$this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
20729 // trim string
20730 $html = $this->stringTrim($html);
20731 // fix first image tag alignment
20732 $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
20733 // pattern for generic tag
20734 $tagpattern = '/(<[^>]+>)/';
20735 // explodes the string
20736 $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
20737 // count elements
20738 $maxel = count($a);
20739 $elkey = 0;
20740 $key = 0;
20741 // create an array of elements
20742 $dom = array();
20743 $dom[$key] = array();
20744 // set inheritable properties fot the first void element
20745 // possible inheritable properties are: azimuth, border-collapse, border-spacing, caption-side, color, cursor, direction, empty-cells, font, font-family, font-stretch, font-size, font-size-adjust, font-style, font-variant, font-weight, letter-spacing, line-height, list-style, list-style-image, list-style-position, list-style-type, orphans, page, page-break-inside, quotes, speak, speak-header, text-align, text-indent, text-transform, volume, white-space, widows, word-spacing
20746 $dom[$key]['tag'] = false;
20747 $dom[$key]['block'] = false;
20748 $dom[$key]['value'] = '';
20749 $dom[$key]['parent'] = 0;
20750 $dom[$key]['hide'] = false;
20751 $dom[$key]['fontname'] = $this->FontFamily;
20752 $dom[$key]['fontstyle'] = $this->FontStyle;
20753 $dom[$key]['fontsize'] = $this->FontSizePt;
20754 $dom[$key]['font-stretch'] = $this->font_stretching;
20755 $dom[$key]['letter-spacing'] = $this->font_spacing;
20756 $dom[$key]['stroke'] = $this->textstrokewidth;
20757 $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
20758 $dom[$key]['clip'] = ($this->textrendermode > 3);
20759 $dom[$key]['line-height'] = $this->cell_height_ratio;
20760 $dom[$key]['bgcolor'] = false;
20761 $dom[$key]['fgcolor'] = $this->fgcolor; // color
20762 $dom[$key]['strokecolor'] = $this->strokecolor;
20763 $dom[$key]['align'] = '';
20764 $dom[$key]['listtype'] = '';
20765 $dom[$key]['text-indent'] = 0;
20766 $dom[$key]['border'] = array();
20767 $dom[$key]['dir'] = $this->rtl?'rtl':'ltr';
20768 $thead = false; // true when we are inside the THEAD tag
20769 ++$key;
20770 $level = array();
20771 array_push($level, 0); // root
20772 while ($elkey < $maxel) {
20773 $dom[$key] = array();
20774 $element = $a[$elkey];
20775 $dom[$key]['elkey'] = $elkey;
20776 if (preg_match($tagpattern, $element)) {
20777 // html tag
20778 $element = substr($element, 1, -1);
20779 // get tag name
20780 preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
20781 $tagname = strtolower($tag[1]);
20782 // check if we are inside a table header
20783 if ($tagname == 'thead') {
20784 if ($element{0} == '/') {
20785 $thead = false;
20786 } else {
20787 $thead = true;
20789 ++$elkey;
20790 continue;
20792 $dom[$key]['tag'] = true;
20793 $dom[$key]['value'] = $tagname;
20794 if (in_array($dom[$key]['value'], $blocktags)) {
20795 $dom[$key]['block'] = true;
20796 } else {
20797 $dom[$key]['block'] = false;
20799 if ($element{0} == '/') {
20800 // *** closing html tag
20801 $dom[$key]['opening'] = false;
20802 $dom[$key]['parent'] = end($level);
20803 array_pop($level);
20804 $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
20805 $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
20806 $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
20807 $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
20808 $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
20809 $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
20810 $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
20811 $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
20812 $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
20813 $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
20814 $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
20815 $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
20816 $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
20817 $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
20818 $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
20819 if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
20820 $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
20822 // set the number of columns in table tag
20823 if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
20824 $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
20826 if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
20827 $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
20828 for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
20829 $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
20831 $key = $i;
20832 // mark nested tables
20833 $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
20834 // remove thead sections from nested tables
20835 $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
20836 $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
20838 // store header rows on a new table
20839 if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
20840 if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
20841 $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray.$a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
20843 for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
20844 $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
20846 if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
20847 $dom[($dom[$key]['parent'])]['attribute'] = array();
20849 // header elements must be always contained in a single page
20850 $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
20852 if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
20853 // remove the nobr attributes from the table header
20854 $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
20855 $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
20857 } else {
20858 // *** opening or self-closing html tag
20859 $dom[$key]['opening'] = true;
20860 $dom[$key]['parent'] = end($level);
20861 if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
20862 // self-closing tag
20863 $dom[$key]['self'] = true;
20864 } else {
20865 // opening tag
20866 array_push($level, $key);
20867 $dom[$key]['self'] = false;
20869 // copy some values from parent
20870 $parentkey = 0;
20871 if ($key > 0) {
20872 $parentkey = $dom[$key]['parent'];
20873 $dom[$key]['hide'] = $dom[$parentkey]['hide'];
20874 $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
20875 $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
20876 $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
20877 $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
20878 $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
20879 $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
20880 $dom[$key]['fill'] = $dom[$parentkey]['fill'];
20881 $dom[$key]['clip'] = $dom[$parentkey]['clip'];
20882 $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
20883 $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
20884 $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
20885 $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
20886 $dom[$key]['align'] = $dom[$parentkey]['align'];
20887 $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
20888 $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
20889 $dom[$key]['border'] = array();
20890 $dom[$key]['dir'] = $dom[$parentkey]['dir'];
20892 // get attributes
20893 preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
20894 $dom[$key]['attribute'] = array(); // reset attribute array
20895 while (list($id, $name) = each($attr_array[1])) {
20896 $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
20898 if (!empty($css)) {
20899 // merge CSS style to current style
20900 list($dom[$key]['csssel'], $dom[$key]['cssdata']) = $this->getCSSdataArray($dom, $key, $css);
20901 $dom[$key]['attribute']['style'] = $this->getTagStyleFromCSSarray($dom[$key]['cssdata']);
20903 // split style attributes
20904 if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
20905 // get style attributes
20906 preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
20907 $dom[$key]['style'] = array(); // reset style attribute array
20908 while (list($id, $name) = each($style_array[1])) {
20909 // in case of duplicate attribute the last replace the previous
20910 $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
20912 // --- get some style attributes ---
20913 // text direction
20914 if (isset($dom[$key]['style']['direction'])) {
20915 $dom[$key]['dir'] = $dom[$key]['style']['direction'];
20917 // display
20918 if (isset($dom[$key]['style']['display'])) {
20919 $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
20921 // font family
20922 if (isset($dom[$key]['style']['font-family'])) {
20923 $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
20925 // list-style-type
20926 if (isset($dom[$key]['style']['list-style-type'])) {
20927 $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
20928 if ($dom[$key]['listtype'] == 'inherit') {
20929 $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
20932 // text-indent
20933 if (isset($dom[$key]['style']['text-indent'])) {
20934 $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
20935 if ($dom[$key]['text-indent'] == 'inherit') {
20936 $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
20939 // font size
20940 if (isset($dom[$key]['style']['font-size'])) {
20941 $fsize = trim($dom[$key]['style']['font-size']);
20942 switch ($fsize) {
20943 // absolute-size
20944 case 'xx-small': {
20945 $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
20946 break;
20948 case 'x-small': {
20949 $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
20950 break;
20952 case 'small': {
20953 $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
20954 break;
20956 case 'medium': {
20957 $dom[$key]['fontsize'] = $dom[0]['fontsize'];
20958 break;
20960 case 'large': {
20961 $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
20962 break;
20964 case 'x-large': {
20965 $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
20966 break;
20968 case 'xx-large': {
20969 $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
20970 break;
20972 // relative-size
20973 case 'smaller': {
20974 $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
20975 break;
20977 case 'larger': {
20978 $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
20979 break;
20981 default: {
20982 $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
20986 // font-stretch
20987 if (isset($dom[$key]['style']['font-stretch'])) {
20988 $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
20990 // letter-spacing
20991 if (isset($dom[$key]['style']['letter-spacing'])) {
20992 $dom[$key]['letter-spacing'] = $this->getCSSFontSpacing($dom[$key]['style']['letter-spacing'], $dom[$parentkey]['letter-spacing']);
20994 // line-height
20995 if (isset($dom[$key]['style']['line-height'])) {
20996 $lineheight = trim($dom[$key]['style']['line-height']);
20997 switch ($lineheight) {
20998 // A normal line height. This is default
20999 case 'normal': {
21000 $dom[$key]['line-height'] = $dom[0]['line-height'];
21001 break;
21003 default: {
21004 if (is_numeric($lineheight)) {
21005 $lineheight = $lineheight * 100;
21007 $dom[$key]['line-height'] = $this->getHTMLUnitToUnits($lineheight, 1, '%', true);
21011 // font style
21012 if (isset($dom[$key]['style']['font-weight'])) {
21013 if (strtolower($dom[$key]['style']['font-weight']{0}) == 'n') {
21014 if (strpos($dom[$key]['fontstyle'], 'B') !== false) {
21015 $dom[$key]['fontstyle'] = str_replace('B', '', $dom[$key]['fontstyle']);
21017 } elseif (strtolower($dom[$key]['style']['font-weight']{0}) == 'b') {
21018 $dom[$key]['fontstyle'] .= 'B';
21021 if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
21022 $dom[$key]['fontstyle'] .= 'I';
21024 // font color
21025 if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
21026 $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
21027 } elseif ($dom[$key]['value'] == 'a') {
21028 $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
21030 // background color
21031 if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
21032 $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
21034 // text-decoration
21035 if (isset($dom[$key]['style']['text-decoration'])) {
21036 $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
21037 foreach ($decors as $dec) {
21038 $dec = trim($dec);
21039 if (!$this->empty_string($dec)) {
21040 if ($dec{0} == 'u') {
21041 // underline
21042 $dom[$key]['fontstyle'] .= 'U';
21043 } elseif ($dec{0} == 'l') {
21044 // line-trough
21045 $dom[$key]['fontstyle'] .= 'D';
21046 } elseif ($dec{0} == 'o') {
21047 // overline
21048 $dom[$key]['fontstyle'] .= 'O';
21052 } elseif ($dom[$key]['value'] == 'a') {
21053 $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
21055 // check for width attribute
21056 if (isset($dom[$key]['style']['width'])) {
21057 $dom[$key]['width'] = $dom[$key]['style']['width'];
21059 // check for height attribute
21060 if (isset($dom[$key]['style']['height'])) {
21061 $dom[$key]['height'] = $dom[$key]['style']['height'];
21063 // check for text alignment
21064 if (isset($dom[$key]['style']['text-align'])) {
21065 $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
21067 // check for CSS border properties
21068 if (isset($dom[$key]['style']['border'])) {
21069 $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border']);
21070 if (!empty($borderstyle)) {
21071 $dom[$key]['border']['LTRB'] = $borderstyle;
21074 if (isset($dom[$key]['style']['border-color'])) {
21075 $brd_colors = preg_split('/[\s]+/', trim($dom[$key]['style']['border-color']));
21076 if (isset($brd_colors[3])) {
21077 $dom[$key]['border']['L']['color'] = $this->convertHTMLColorToDec($brd_colors[3]);
21079 if (isset($brd_colors[1])) {
21080 $dom[$key]['border']['R']['color'] = $this->convertHTMLColorToDec($brd_colors[1]);
21082 if (isset($brd_colors[0])) {
21083 $dom[$key]['border']['T']['color'] = $this->convertHTMLColorToDec($brd_colors[0]);
21085 if (isset($brd_colors[2])) {
21086 $dom[$key]['border']['B']['color'] = $this->convertHTMLColorToDec($brd_colors[2]);
21089 if (isset($dom[$key]['style']['border-width'])) {
21090 $brd_widths = preg_split('/[\s]+/', trim($dom[$key]['style']['border-width']));
21091 if (isset($brd_widths[3])) {
21092 $dom[$key]['border']['L']['width'] = $this->getCSSBorderWidth($brd_widths[3]);
21094 if (isset($brd_widths[1])) {
21095 $dom[$key]['border']['R']['width'] = $this->getCSSBorderWidth($brd_widths[1]);
21097 if (isset($brd_widths[0])) {
21098 $dom[$key]['border']['T']['width'] = $this->getCSSBorderWidth($brd_widths[0]);
21100 if (isset($brd_widths[2])) {
21101 $dom[$key]['border']['B']['width'] = $this->getCSSBorderWidth($brd_widths[2]);
21104 if (isset($dom[$key]['style']['border-style'])) {
21105 $brd_styles = preg_split('/[\s]+/', trim($dom[$key]['style']['border-style']));
21106 if (isset($brd_styles[3])) {
21107 $dom[$key]['border']['L']['cap'] = 'square';
21108 $dom[$key]['border']['L']['join'] = 'miter';
21109 $dom[$key]['border']['L']['dash'] = $this->getCSSBorderDashStyle($brd_styles[3]);
21110 if ($dom[$key]['border']['L']['dash'] < 0) {
21111 $dom[$key]['border']['L'] = array();
21114 if (isset($brd_styles[1])) {
21115 $dom[$key]['border']['R']['cap'] = 'square';
21116 $dom[$key]['border']['R']['join'] = 'miter';
21117 $dom[$key]['border']['R']['dash'] = $this->getCSSBorderDashStyle($brd_styles[1]);
21118 if ($dom[$key]['border']['R']['dash'] < 0) {
21119 $dom[$key]['border']['R'] = array();
21122 if (isset($brd_styles[0])) {
21123 $dom[$key]['border']['T']['cap'] = 'square';
21124 $dom[$key]['border']['T']['join'] = 'miter';
21125 $dom[$key]['border']['T']['dash'] = $this->getCSSBorderDashStyle($brd_styles[0]);
21126 if ($dom[$key]['border']['T']['dash'] < 0) {
21127 $dom[$key]['border']['T'] = array();
21130 if (isset($brd_styles[2])) {
21131 $dom[$key]['border']['B']['cap'] = 'square';
21132 $dom[$key]['border']['B']['join'] = 'miter';
21133 $dom[$key]['border']['B']['dash'] = $this->getCSSBorderDashStyle($brd_styles[2]);
21134 if ($dom[$key]['border']['B']['dash'] < 0) {
21135 $dom[$key]['border']['B'] = array();
21139 $cellside = array('L' => 'left', 'R' => 'right', 'T' => 'top', 'B' => 'bottom');
21140 foreach ($cellside as $bsk => $bsv) {
21141 if (isset($dom[$key]['style']['border-'.$bsv])) {
21142 $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border-'.$bsv]);
21143 if (!empty($borderstyle)) {
21144 $dom[$key]['border'][$bsk] = $borderstyle;
21147 if (isset($dom[$key]['style']['border-'.$bsv.'-color'])) {
21148 $dom[$key]['border'][$bsk]['color'] = $this->convertHTMLColorToDec($dom[$key]['style']['border-'.$bsv.'-color']);
21150 if (isset($dom[$key]['style']['border-'.$bsv.'-width'])) {
21151 $dom[$key]['border'][$bsk]['width'] = $this->getCSSBorderWidth($dom[$key]['style']['border-'.$bsv.'-width']);
21153 if (isset($dom[$key]['style']['border-'.$bsv.'-style'])) {
21154 $dom[$key]['border'][$bsk]['dash'] = $this->getCSSBorderDashStyle($dom[$key]['style']['border-'.$bsv.'-style']);
21155 if ($dom[$key]['border'][$bsk]['dash'] < 0) {
21156 $dom[$key]['border'][$bsk] = array();
21160 // check for CSS padding properties
21161 if (isset($dom[$key]['style']['padding'])) {
21162 $dom[$key]['padding'] = $this->getCSSPadding($dom[$key]['style']['padding']);
21163 } else {
21164 $dom[$key]['padding'] = $this->cell_padding;
21166 foreach ($cellside as $psk => $psv) {
21167 if (isset($dom[$key]['style']['padding-'.$psv])) {
21168 $dom[$key]['padding'][$psk] = $this->getHTMLUnitToUnits($dom[$key]['style']['padding-'.$psv], 0, 'px', false);
21171 // check for CSS margin properties
21172 if (isset($dom[$key]['style']['margin'])) {
21173 $dom[$key]['margin'] = $this->getCSSMargin($dom[$key]['style']['margin']);
21174 } else {
21175 $dom[$key]['margin'] = $this->cell_margin;
21177 foreach ($cellside as $psk => $psv) {
21178 if (isset($dom[$key]['style']['margin-'.$psv])) {
21179 $dom[$key]['margin'][$psk] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $dom[$key]['style']['margin-'.$psv]), 0, 'px', false);
21182 // check for CSS border-spacing properties
21183 if (isset($dom[$key]['style']['border-spacing'])) {
21184 $dom[$key]['border-spacing'] = $this->getCSSBorderMargin($dom[$key]['style']['border-spacing']);
21186 // page-break-inside
21187 if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
21188 $dom[$key]['attribute']['nobr'] = 'true';
21190 // page-break-before
21191 if (isset($dom[$key]['style']['page-break-before'])) {
21192 if ($dom[$key]['style']['page-break-before'] == 'always') {
21193 $dom[$key]['attribute']['pagebreak'] = 'true';
21194 } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
21195 $dom[$key]['attribute']['pagebreak'] = 'left';
21196 } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
21197 $dom[$key]['attribute']['pagebreak'] = 'right';
21200 // page-break-after
21201 if (isset($dom[$key]['style']['page-break-after'])) {
21202 if ($dom[$key]['style']['page-break-after'] == 'always') {
21203 $dom[$key]['attribute']['pagebreakafter'] = 'true';
21204 } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
21205 $dom[$key]['attribute']['pagebreakafter'] = 'left';
21206 } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
21207 $dom[$key]['attribute']['pagebreakafter'] = 'right';
21211 if (isset($dom[$key]['attribute']['display'])) {
21212 $dom[$key]['hide'] = (trim(strtolower($dom[$key]['attribute']['display'])) == 'none');
21214 if (isset($dom[$key]['attribute']['border']) AND ($dom[$key]['attribute']['border'] != 0)) {
21215 $borderstyle = $this->getCSSBorderStyle($dom[$key]['attribute']['border'].' solid black');
21216 if (!empty($borderstyle)) {
21217 $dom[$key]['border']['LTRB'] = $borderstyle;
21220 // check for font tag
21221 if ($dom[$key]['value'] == 'font') {
21222 // font family
21223 if (isset($dom[$key]['attribute']['face'])) {
21224 $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['attribute']['face']);
21226 // font size
21227 if (isset($dom[$key]['attribute']['size'])) {
21228 if ($key > 0) {
21229 if ($dom[$key]['attribute']['size']{0} == '+') {
21230 $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
21231 } elseif ($dom[$key]['attribute']['size']{0} == '-') {
21232 $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
21233 } else {
21234 $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
21236 } else {
21237 $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
21241 // force natural alignment for lists
21242 if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
21243 AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
21244 if ($this->rtl) {
21245 $dom[$key]['align'] = 'R';
21246 } else {
21247 $dom[$key]['align'] = 'L';
21250 if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
21251 if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
21252 $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
21255 if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
21256 $dom[$key]['fontstyle'] .= 'B';
21258 if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
21259 $dom[$key]['fontstyle'] .= 'I';
21261 if ($dom[$key]['value'] == 'u') {
21262 $dom[$key]['fontstyle'] .= 'U';
21264 if (($dom[$key]['value'] == 'del') OR ($dom[$key]['value'] == 's') OR ($dom[$key]['value'] == 'strike')) {
21265 $dom[$key]['fontstyle'] .= 'D';
21267 if (!isset($dom[$key]['style']['text-decoration']) AND ($dom[$key]['value'] == 'a')) {
21268 $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
21270 if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
21271 $dom[$key]['fontname'] = $this->default_monospaced_font;
21273 if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
21274 // headings h1, h2, h3, h4, h5, h6
21275 if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
21276 $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
21277 $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
21279 if (!isset($dom[$key]['style']['font-weight'])) {
21280 $dom[$key]['fontstyle'] .= 'B';
21283 if (($dom[$key]['value'] == 'table')) {
21284 $dom[$key]['rows'] = 0; // number of rows
21285 $dom[$key]['trids'] = array(); // IDs of TR elements
21286 $dom[$key]['thead'] = ''; // table header rows
21288 if (($dom[$key]['value'] == 'tr')) {
21289 $dom[$key]['cols'] = 0;
21290 if ($thead) {
21291 $dom[$key]['thead'] = true;
21292 // rows on thead block are printed as a separate table
21293 } else {
21294 $dom[$key]['thead'] = false;
21295 // store the number of rows on table element
21296 ++$dom[($dom[$key]['parent'])]['rows'];
21297 // store the TR elements IDs on table element
21298 array_push($dom[($dom[$key]['parent'])]['trids'], $key);
21301 if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
21302 if (isset($dom[$key]['attribute']['colspan'])) {
21303 $colspan = intval($dom[$key]['attribute']['colspan']);
21304 } else {
21305 $colspan = 1;
21307 $dom[$key]['attribute']['colspan'] = $colspan;
21308 $dom[($dom[$key]['parent'])]['cols'] += $colspan;
21310 // text direction
21311 if (isset($dom[$key]['attribute']['dir'])) {
21312 $dom[$key]['dir'] = $dom[$key]['attribute']['dir'];
21314 // set foreground color attribute
21315 if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
21316 $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
21317 } elseif (!isset($dom[$key]['style']['color']) AND ($dom[$key]['value'] == 'a')) {
21318 $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
21320 // set background color attribute
21321 if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
21322 $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
21324 // set stroke color attribute
21325 if (isset($dom[$key]['attribute']['strokecolor']) AND (!$this->empty_string($dom[$key]['attribute']['strokecolor']))) {
21326 $dom[$key]['strokecolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['strokecolor']);
21328 // check for width attribute
21329 if (isset($dom[$key]['attribute']['width'])) {
21330 $dom[$key]['width'] = $dom[$key]['attribute']['width'];
21332 // check for height attribute
21333 if (isset($dom[$key]['attribute']['height'])) {
21334 $dom[$key]['height'] = $dom[$key]['attribute']['height'];
21336 // check for text alignment
21337 if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
21338 $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
21340 // check for text rendering mode (the following attributes do not exist in HTML)
21341 if (isset($dom[$key]['attribute']['stroke'])) {
21342 // font stroke width
21343 $dom[$key]['stroke'] = $this->getHTMLUnitToUnits($dom[$key]['attribute']['stroke'], $dom[$key]['fontsize'], 'pt', true);
21345 if (isset($dom[$key]['attribute']['fill'])) {
21346 // font fill
21347 if ($dom[$key]['attribute']['fill'] == 'true') {
21348 $dom[$key]['fill'] = true;
21349 } else {
21350 $dom[$key]['fill'] = false;
21353 if (isset($dom[$key]['attribute']['clip'])) {
21354 // clipping mode
21355 if ($dom[$key]['attribute']['clip'] == 'true') {
21356 $dom[$key]['clip'] = true;
21357 } else {
21358 $dom[$key]['clip'] = false;
21361 } // end opening tag
21362 } else {
21363 // text
21364 $dom[$key]['tag'] = false;
21365 $dom[$key]['block'] = false;
21366 //$element = str_replace('&nbsp;', $this->unichr(160), $element);
21367 $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
21368 $dom[$key]['parent'] = end($level);
21369 $dom[$key]['dir'] = $dom[$dom[$key]['parent']]['dir'];
21371 ++$elkey;
21372 ++$key;
21374 return $dom;
21378 * Returns the string used to find spaces
21379 * @return string
21380 * @protected
21381 * @author Nicola Asuni
21382 * @since 4.8.024 (2010-01-15)
21384 protected function getSpaceString() {
21385 $spacestr = chr(32);
21386 if ($this->isUnicodeFont()) {
21387 $spacestr = chr(0).chr(32);
21389 return $spacestr;
21393 * Prints a cell (rectangular area) with optional borders, background color and html text string.
21394 * The upper-left corner of the cell corresponds to the current position. After the call, the current position moves to the right or to the next line.<br />
21395 * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
21396 * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
21397 * @param $h (float) Cell minimum height. The cell extends automatically if needed.
21398 * @param $x (float) upper-left corner X coordinate
21399 * @param $y (float) upper-left corner Y coordinate
21400 * @param $html (string) html text to print. Default value: empty string.
21401 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
21402 * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL language)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>
21403 Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
21404 * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
21405 * @param $reseth (boolean) if true reset the last cell height (default true).
21406 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
21407 * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
21408 * @see Multicell(), writeHTML()
21409 * @public
21411 public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
21412 return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
21416 * Allows to preserve some HTML formatting (limited support).<br />
21417 * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
21418 * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul
21419 * @param $html (string) text to display
21420 * @param $ln (boolean) if true add a new line after text (default = true)
21421 * @param $fill (boolean) Indicates if the background must be painted (true) or transparent (false).
21422 * @param $reseth (boolean) if true reset the last cell height (default false).
21423 * @param $cell (boolean) if true add the current left (or right for RTL) padding to each Write (default false).
21424 * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
21425 * @public
21427 public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
21428 $gvars = $this->getGraphicVars();
21429 // store current values
21430 $prev_cell_margin = $this->cell_margin;
21431 $prev_cell_padding = $this->cell_padding;
21432 $prevPage = $this->page;
21433 $prevlMargin = $this->lMargin;
21434 $prevrMargin = $this->rMargin;
21435 $curfontname = $this->FontFamily;
21436 $curfontstyle = $this->FontStyle;
21437 $curfontsize = $this->FontSizePt;
21438 $curfontascent = $this->getFontAscent($curfontname, $curfontstyle, $curfontsize);
21439 $curfontdescent = $this->getFontDescent($curfontname, $curfontstyle, $curfontsize);
21440 $curfontstretcing = $this->font_stretching;
21441 $curfontkerning = $this->font_spacing;
21442 $this->newline = true;
21443 $newline = true;
21444 $startlinepage = $this->page;
21445 $minstartliney = $this->y;
21446 $maxbottomliney = 0;
21447 $startlinex = $this->x;
21448 $startliney = $this->y;
21449 $yshift = 0;
21450 $loop = 0;
21451 $curpos = 0;
21452 $this_method_vars = array();
21453 $undo = false;
21454 $fontaligned = false;
21455 $reverse_dir = false; // true when the text direction is reversed
21456 $this->premode = false;
21457 if ($this->inxobj) {
21458 // we are inside an XObject template
21459 $pask = count($this->xobjects[$this->xobjid]['annotations']);
21460 } elseif (isset($this->PageAnnots[$this->page])) {
21461 $pask = count($this->PageAnnots[$this->page]);
21462 } else {
21463 $pask = 0;
21465 if ($this->inxobj) {
21466 // we are inside an XObject template
21467 $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
21468 } elseif (!$this->InFooter) {
21469 if (isset($this->footerlen[$this->page])) {
21470 $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
21471 } else {
21472 $this->footerpos[$this->page] = $this->pagelen[$this->page];
21474 $startlinepos = $this->footerpos[$this->page];
21475 } else {
21476 // we are inside the footer
21477 $startlinepos = $this->pagelen[$this->page];
21479 $lalign = $align;
21480 $plalign = $align;
21481 if ($this->rtl) {
21482 $w = $this->x - $this->lMargin;
21483 } else {
21484 $w = $this->w - $this->rMargin - $this->x;
21486 $w -= ($this->cell_padding['L'] + $this->cell_padding['R']);
21487 if ($cell) {
21488 if ($this->rtl) {
21489 $this->x -= $this->cell_padding['R'];
21490 $this->lMargin += $this->cell_padding['R'];
21491 } else {
21492 $this->x += $this->cell_padding['L'];
21493 $this->rMargin += $this->cell_padding['L'];
21496 if ($this->customlistindent >= 0) {
21497 $this->listindent = $this->customlistindent;
21498 } else {
21499 $this->listindent = $this->GetStringWidth('000000');
21501 $this->listindentlevel = 0;
21502 // save previous states
21503 $prev_cell_height_ratio = $this->cell_height_ratio;
21504 $prev_listnum = $this->listnum;
21505 $prev_listordered = $this->listordered;
21506 $prev_listcount = $this->listcount;
21507 $prev_lispacer = $this->lispacer;
21508 $this->listnum = 0;
21509 $this->listordered = array();
21510 $this->listcount = array();
21511 $this->lispacer = '';
21512 if (($this->empty_string($this->lasth)) OR ($reseth)) {
21513 // reset row height
21514 $this->resetLastH();
21516 $dom = $this->getHtmlDomArray($html);
21517 $maxel = count($dom);
21518 $key = 0;
21519 $hidden_node_key = -1;
21520 while ($key < $maxel) {
21521 if ($dom[$key]['tag']) {
21522 if ($dom[$key]['opening']) {
21523 if (($hidden_node_key <= 0) AND $dom[$key]['hide']) {
21524 // store the node key
21525 $hidden_node_key = $key;
21527 } elseif (($hidden_node_key > 0) AND ($dom[$key]['parent'] == $hidden_node_key)) {
21528 // we have reached the closing tag of the hidden node
21529 $hidden_node_key = 0;
21532 if ($hidden_node_key >= 0) {
21533 // skip this node
21534 ++$key;
21535 if ($hidden_node_key == 0) {
21536 // reset hidden mode
21537 $hidden_node_key = -1;
21539 continue;
21541 if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
21542 // check for pagebreak
21543 if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
21544 // add a page (or trig AcceptPageBreak() for multicolumn mode)
21545 $this->checkPageBreak($this->PageBreakTrigger + 1);
21547 if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
21548 OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
21549 // add a page (or trig AcceptPageBreak() for multicolumn mode)
21550 $this->checkPageBreak($this->PageBreakTrigger + 1);
21553 if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
21554 if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
21555 $dom[$key]['attribute']['nobr'] = false;
21556 } else {
21557 // store current object
21558 $this->startTransaction();
21559 // save this method vars
21560 $this_method_vars['html'] = $html;
21561 $this_method_vars['ln'] = $ln;
21562 $this_method_vars['fill'] = $fill;
21563 $this_method_vars['reseth'] = $reseth;
21564 $this_method_vars['cell'] = $cell;
21565 $this_method_vars['align'] = $align;
21566 $this_method_vars['gvars'] = $gvars;
21567 $this_method_vars['prevPage'] = $prevPage;
21568 $this_method_vars['prev_cell_margin'] = $prev_cell_margin;
21569 $this_method_vars['prev_cell_padding'] = $prev_cell_padding;
21570 $this_method_vars['prevlMargin'] = $prevlMargin;
21571 $this_method_vars['prevrMargin'] = $prevrMargin;
21572 $this_method_vars['curfontname'] = $curfontname;
21573 $this_method_vars['curfontstyle'] = $curfontstyle;
21574 $this_method_vars['curfontsize'] = $curfontsize;
21575 $this_method_vars['curfontascent'] = $curfontascent;
21576 $this_method_vars['curfontdescent'] = $curfontdescent;
21577 $this_method_vars['curfontstretcing'] = $curfontstretcing;
21578 $this_method_vars['curfontkerning'] = $curfontkerning;
21579 $this_method_vars['minstartliney'] = $minstartliney;
21580 $this_method_vars['maxbottomliney'] = $maxbottomliney;
21581 $this_method_vars['yshift'] = $yshift;
21582 $this_method_vars['startlinepage'] = $startlinepage;
21583 $this_method_vars['startlinepos'] = $startlinepos;
21584 $this_method_vars['startlinex'] = $startlinex;
21585 $this_method_vars['startliney'] = $startliney;
21586 $this_method_vars['newline'] = $newline;
21587 $this_method_vars['loop'] = $loop;
21588 $this_method_vars['curpos'] = $curpos;
21589 $this_method_vars['pask'] = $pask;
21590 $this_method_vars['lalign'] = $lalign;
21591 $this_method_vars['plalign'] = $plalign;
21592 $this_method_vars['w'] = $w;
21593 $this_method_vars['prev_cell_height_ratio'] = $prev_cell_height_ratio;
21594 $this_method_vars['prev_listnum'] = $prev_listnum;
21595 $this_method_vars['prev_listordered'] = $prev_listordered;
21596 $this_method_vars['prev_listcount'] = $prev_listcount;
21597 $this_method_vars['prev_lispacer'] = $prev_lispacer;
21598 $this_method_vars['fontaligned'] = $fontaligned;
21599 $this_method_vars['key'] = $key;
21600 $this_method_vars['dom'] = $dom;
21603 // print THEAD block
21604 if (($dom[$key]['value'] == 'tr') AND isset($dom[$key]['thead']) AND $dom[$key]['thead']) {
21605 if (isset($dom[$key]['parent']) AND isset($dom[$dom[$key]['parent']]['thead']) AND !$this->empty_string($dom[$dom[$key]['parent']]['thead'])) {
21606 $this->inthead = true;
21607 // print table header (thead)
21608 $this->writeHTML($this->thead, false, false, false, false, '');
21609 // check if we are on a new page or on a new column
21610 if (($this->y < $this->start_transaction_y) OR ($this->checkPageBreak($this->lasth, '', false))) {
21611 // we are on a new page or on a new column and the total object height is less than the available vertical space.
21612 // restore previous object
21613 $this->rollbackTransaction(true);
21614 // restore previous values
21615 foreach ($this_method_vars as $vkey => $vval) {
21616 $$vkey = $vval;
21618 // disable table header
21619 $tmp_thead = $this->thead;
21620 $this->thead = '';
21621 // add a page (or trig AcceptPageBreak() for multicolumn mode)
21622 $pre_y = $this->y;
21623 if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
21624 // fix for multicolumn mode
21625 $startliney = $this->y;
21627 $this->start_transaction_page = $this->page;
21628 $this->start_transaction_y = $this->y;
21629 // restore table header
21630 $this->thead = $tmp_thead;
21631 // fix table border properties
21632 if (isset($dom[$dom[$key]['parent']]['attribute']['cellspacing'])) {
21633 $tmp_cellspacing = $this->getHTMLUnitToUnits($dom[$dom[$key]['parent']]['attribute']['cellspacing'], 1, 'px');
21634 } elseif (isset($dom[$dom[$key]['parent']]['border-spacing'])) {
21635 $tmp_cellspacing = $dom[$dom[$key]['parent']]['border-spacing']['V'];
21636 } else {
21637 $tmp_cellspacing = 0;
21639 $dom[$dom[$key]['parent']]['borderposition']['page'] = $this->page;
21640 $dom[$dom[$key]['parent']]['borderposition']['column'] = $this->current_column;
21641 $dom[$dom[$key]['parent']]['borderposition']['y'] = $this->y + $tmp_cellspacing;
21642 $xoffset = ($this->x - $dom[$dom[$key]['parent']]['borderposition']['x']);
21643 $dom[$dom[$key]['parent']]['borderposition']['x'] += $xoffset;
21644 $dom[$dom[$key]['parent']]['borderposition']['xmax'] += $xoffset;
21645 // print table header (thead)
21646 $this->writeHTML($this->thead, false, false, false, false, '');
21649 // move $key index forward to skip THEAD block
21650 while ( ($key < $maxel) AND (!(
21651 ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'tr') AND (!isset($dom[$key]['thead']) OR !$dom[$key]['thead']))
21652 OR ($dom[$key]['tag'] AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == 'table'))) )) {
21653 ++$key;
21656 if ($dom[$key]['tag'] OR ($key == 0)) {
21657 if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
21658 $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
21660 // vertically align image in line
21661 if ((!$this->newline) AND ($dom[$key]['value'] == 'img') AND (isset($dom[$key]['height'])) AND ($dom[$key]['height'] > 0)) {
21662 // get image height
21663 $imgh = $this->getHTMLUnitToUnits($dom[$key]['height'], $this->lasth, 'px');
21664 $autolinebreak = false;
21665 if (isset($dom[$key]['width']) AND ($dom[$key]['width'] > 0)) {
21666 $imgw = $this->getHTMLUnitToUnits($dom[$key]['width'], 1, 'px', false);
21667 if (($imgw <= ($this->w - $this->lMargin - $this->rMargin - $this->cell_padding['L'] - $this->cell_padding['R']))
21668 AND ((($this->rtl) AND (($this->x - $imgw) < ($this->lMargin + $this->cell_padding['L'])))
21669 OR ((!$this->rtl) AND (($this->x + $imgw) > ($this->w - $this->rMargin - $this->cell_padding['R']))))) {
21670 // add automatic line break
21671 $autolinebreak = true;
21672 $this->Ln('', $cell);
21673 if ((!$dom[($key-1)]['tag']) AND ($dom[($key-1)]['value'] == ' ')) {
21674 // go back to evaluate this line break
21675 --$key;
21679 if (!$autolinebreak) {
21680 if ($this->inPageBody()) {
21681 $pre_y = $this->y;
21682 // check for page break
21683 if ((!$this->checkPageBreak($imgh)) AND ($this->y < $pre_y)) {
21684 // fix for multicolumn mode
21685 $startliney = $this->y;
21688 if ($this->page > $startlinepage) {
21689 // fix line splitted over two pages
21690 if (isset($this->footerlen[$startlinepage])) {
21691 $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
21693 // line to be moved one page forward
21694 $pagebuff = $this->getPageBuffer($startlinepage);
21695 $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
21696 $tstart = substr($pagebuff, 0, $startlinepos);
21697 $tend = substr($this->getPageBuffer($startlinepage), $curpos);
21698 // remove line from previous page
21699 $this->setPageBuffer($startlinepage, $tstart.''.$tend);
21700 $pagebuff = $this->getPageBuffer($this->page);
21701 $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
21702 $tend = substr($pagebuff, $this->cntmrk[$this->page]);
21703 // add line start to current page
21704 $yshift = ($minstartliney - $this->y);
21705 if ($fontaligned) {
21706 $yshift += ($curfontsize / $this->k);
21708 $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
21709 $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
21710 // shift the annotations and links
21711 if (isset($this->PageAnnots[$this->page])) {
21712 $next_pask = count($this->PageAnnots[$this->page]);
21713 } else {
21714 $next_pask = 0;
21716 if (isset($this->PageAnnots[$startlinepage])) {
21717 foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
21718 if ($pak >= $pask) {
21719 $this->PageAnnots[$this->page][] = $pac;
21720 unset($this->PageAnnots[$startlinepage][$pak]);
21721 $npak = count($this->PageAnnots[$this->page]) - 1;
21722 $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
21726 $pask = $next_pask;
21727 $startlinepos = $this->cntmrk[$this->page];
21728 $startlinepage = $this->page;
21729 $startliney = $this->y;
21730 $this->newline = false;
21732 $this->y += ((($curfontsize * $this->cell_height_ratio / $this->k) + $curfontascent - $curfontdescent) / 2) - $imgh;
21733 $minstartliney = min($this->y, $minstartliney);
21734 $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
21736 } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize']) OR isset($dom[$key]['line-height'])) {
21737 // account for different font size
21738 $pfontname = $curfontname;
21739 $pfontstyle = $curfontstyle;
21740 $pfontsize = $curfontsize;
21741 $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
21742 $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
21743 $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
21744 $fontascent = $this->getFontAscent($fontname, $fontstyle, $fontsize);
21745 $fontdescent = $this->getFontDescent($fontname, $fontstyle, $fontsize);
21746 if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)
21747 OR ($this->cell_height_ratio != $dom[$key]['line-height'])
21748 OR ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li')) ) {
21749 if (($key < ($maxel - 1)) AND (
21750 ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li'))
21751 OR ($this->cell_height_ratio != $dom[$key]['line-height'])
21752 OR (!$this->newline AND is_numeric($fontsize) AND is_numeric($curfontsize) AND ($fontsize >= 0) AND ($curfontsize >= 0) AND ($fontsize != $curfontsize))
21753 )) {
21754 if ($this->page > $startlinepage) {
21755 // fix lines splitted over two pages
21756 if (isset($this->footerlen[$startlinepage])) {
21757 $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
21759 // line to be moved one page forward
21760 $pagebuff = $this->getPageBuffer($startlinepage);
21761 $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
21762 $tstart = substr($pagebuff, 0, $startlinepos);
21763 $tend = substr($this->getPageBuffer($startlinepage), $curpos);
21764 // remove line start from previous page
21765 $this->setPageBuffer($startlinepage, $tstart.''.$tend);
21766 $pagebuff = $this->getPageBuffer($this->page);
21767 $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
21768 $tend = substr($pagebuff, $this->cntmrk[$this->page]);
21769 // add line start to current page
21770 $yshift = ($minstartliney - $this->y);
21771 $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
21772 $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
21773 // shift the annotations and links
21774 if (isset($this->PageAnnots[$this->page])) {
21775 $next_pask = count($this->PageAnnots[$this->page]);
21776 } else {
21777 $next_pask = 0;
21779 if (isset($this->PageAnnots[$startlinepage])) {
21780 foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
21781 if ($pak >= $pask) {
21782 $this->PageAnnots[$this->page][] = $pac;
21783 unset($this->PageAnnots[$startlinepage][$pak]);
21784 $npak = count($this->PageAnnots[$this->page]) - 1;
21785 $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
21789 $pask = $next_pask;
21790 $startlinepos = $this->cntmrk[$this->page];
21791 $startlinepage = $this->page;
21792 $startliney = $this->y;
21794 if (!isset($dom[$key]['line-height'])) {
21795 $dom[$key]['line-height'] = $this->cell_height_ratio;
21797 if (!$dom[$key]['block']) {
21798 if (!(isset($dom[($key + 1)]) AND $dom[($key + 1)]['tag'] AND (!$dom[($key + 1)]['opening']) AND ($dom[($key + 1)]['value'] != 'li') AND $dom[$key]['tag'] AND (!$dom[$key]['opening']))) {
21799 $this->y += (((($curfontsize * $this->cell_height_ratio) - ($fontsize * $dom[$key]['line-height'])) / $this->k) + $curfontascent - $fontascent - $curfontdescent + $fontdescent) / 2;
21801 if (($dom[$key]['value'] != 'sup') AND ($dom[$key]['value'] != 'sub')) {
21802 $current_line_align_data = array($key, $minstartliney, $maxbottomliney);
21803 if (isset($line_align_data) AND (($line_align_data[0] == ($key - 1)) OR (($line_align_data[0] == ($key - 2)) AND (isset($dom[($key - 1)])) AND (preg_match('/^([\s]+)$/', $dom[($key - 1)]['value']) > 0)))) {
21804 $minstartliney = min($this->y, $line_align_data[1]);
21805 $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $line_align_data[2]);
21806 } else {
21807 $minstartliney = min($this->y, $minstartliney);
21808 $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
21810 $line_align_data = $current_line_align_data;
21813 $this->cell_height_ratio = $dom[$key]['line-height'];
21814 $fontaligned = true;
21816 $this->SetFont($fontname, $fontstyle, $fontsize);
21817 // reset row height
21818 $this->resetLastH();
21819 $curfontname = $fontname;
21820 $curfontstyle = $fontstyle;
21821 $curfontsize = $fontsize;
21822 $curfontascent = $fontascent;
21823 $curfontdescent = $fontdescent;
21826 // set text rendering mode
21827 $textstroke = isset($dom[$key]['stroke']) ? $dom[$key]['stroke'] : $this->textstrokewidth;
21828 $textfill = isset($dom[$key]['fill']) ? $dom[$key]['fill'] : (($this->textrendermode % 2) == 0);
21829 $textclip = isset($dom[$key]['clip']) ? $dom[$key]['clip'] : ($this->textrendermode > 3);
21830 $this->setTextRenderingMode($textstroke, $textfill, $textclip);
21831 if (isset($dom[$key]['font-stretch']) AND ($dom[$key]['font-stretch'] !== false)) {
21832 $this->setFontStretching($dom[$key]['font-stretch']);
21834 if (isset($dom[$key]['letter-spacing']) AND ($dom[$key]['letter-spacing'] !== false)) {
21835 $this->setFontSpacing($dom[$key]['letter-spacing']);
21837 if (($plalign == 'J') AND $dom[$key]['block']) {
21838 $plalign = '';
21840 // get current position on page buffer
21841 $curpos = $this->pagelen[$startlinepage];
21842 if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
21843 $this->SetFillColorArray($dom[$key]['bgcolor']);
21844 $wfill = true;
21845 } else {
21846 $wfill = $fill | false;
21848 if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
21849 $this->SetTextColorArray($dom[$key]['fgcolor']);
21851 if (isset($dom[$key]['strokecolor']) AND ($dom[$key]['strokecolor'] !== false)) {
21852 $this->SetDrawColorArray($dom[$key]['strokecolor']);
21854 if (isset($dom[$key]['align'])) {
21855 $lalign = $dom[$key]['align'];
21857 if ($this->empty_string($lalign)) {
21858 $lalign = $align;
21861 // align lines
21862 if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
21863 $newline = true;
21864 $fontaligned = false;
21865 // we are at the beginning of a new line
21866 if (isset($startlinex)) {
21867 $yshift = ($minstartliney - $startliney);
21868 if (($yshift > 0) OR ($this->page > $startlinepage)) {
21869 $yshift = 0;
21871 $t_x = 0;
21872 // the last line must be shifted to be aligned as requested
21873 $linew = abs($this->endlinex - $startlinex);
21874 if ($this->inxobj) {
21875 // we are inside an XObject template
21876 $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
21877 if (isset($opentagpos)) {
21878 $midpos = $opentagpos;
21879 } else {
21880 $midpos = 0;
21882 if ($midpos > 0) {
21883 $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
21884 $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
21885 } else {
21886 $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
21887 $pend = '';
21889 } else {
21890 $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
21891 if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
21892 $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
21893 $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
21894 } elseif (isset($opentagpos)) {
21895 $midpos = $opentagpos;
21896 } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
21897 $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
21898 $midpos = $this->footerpos[$startlinepage];
21899 } else {
21900 $midpos = 0;
21902 if ($midpos > 0) {
21903 $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
21904 $pend = substr($this->getPageBuffer($startlinepage), $midpos);
21905 } else {
21906 $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
21907 $pend = '';
21910 if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
21911 // calculate shifting amount
21912 $tw = $w;
21913 if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
21914 $tw += $this->cell_padding['R'];
21916 if ($this->lMargin != $prevlMargin) {
21917 $tw += ($prevlMargin - $this->lMargin);
21919 if ($this->rMargin != $prevrMargin) {
21920 $tw += ($prevrMargin - $this->rMargin);
21922 $one_space_width = $this->GetStringWidth(chr(32));
21923 $no = 0; // number of spaces on a line contained on a single block
21924 if ($this->isRTLTextDir()) { // RTL
21925 // remove left space if exist
21926 $pos1 = $this->revstrpos($pmid, '[(');
21927 if ($pos1 > 0) {
21928 $pos1 = intval($pos1);
21929 if ($this->isUnicodeFont()) {
21930 $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
21931 $spacelen = 2;
21932 } else {
21933 $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
21934 $spacelen = 1;
21936 if ($pos1 == $pos2) {
21937 $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
21938 if (substr($pmid, $pos1, 4) == '[()]') {
21939 $linew -= $one_space_width;
21940 } elseif ($pos1 == strpos($pmid, '[(')) {
21941 $no = 1;
21945 } else { // LTR
21946 // remove right space if exist
21947 $pos1 = $this->revstrpos($pmid, ')]');
21948 if ($pos1 > 0) {
21949 $pos1 = intval($pos1);
21950 if ($this->isUnicodeFont()) {
21951 $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
21952 $spacelen = 2;
21953 } else {
21954 $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
21955 $spacelen = 1;
21957 if ($pos1 == $pos2) {
21958 $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
21959 $linew -= $one_space_width;
21963 $mdiff = ($tw - $linew);
21964 if ($plalign == 'C') {
21965 if ($this->rtl) {
21966 $t_x = -($mdiff / 2);
21967 } else {
21968 $t_x = ($mdiff / 2);
21970 } elseif ($plalign == 'R') {
21971 // right alignment on LTR document
21972 $t_x = $mdiff;
21973 } elseif ($plalign == 'L') {
21974 // left alignment on RTL document
21975 $t_x = -$mdiff;
21976 } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
21977 // Justification
21978 if ($this->isRTLTextDir()) {
21979 // align text on the left
21980 $t_x = -$mdiff;
21982 $ns = 0; // number of spaces
21983 $pmidtemp = $pmid;
21984 // escape special characters
21985 $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
21986 $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
21987 // search spaces
21988 if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
21989 $spacestr = $this->getSpaceString();
21990 $maxkk = count($lnstring[1]) - 1;
21991 for ($kk=0; $kk <= $maxkk; ++$kk) {
21992 // restore special characters
21993 $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
21994 $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
21995 // store number of spaces on the strings
21996 $lnstring[2][$kk] = substr_count($lnstring[1][$kk], $spacestr);
21997 // count total spaces on line
21998 $ns += $lnstring[2][$kk];
21999 $lnstring[3][$kk] = $ns;
22001 if ($ns == 0) {
22002 $ns = 1;
22004 // calculate additional space to add to each existing space
22005 $spacewidth = ($mdiff / ($ns - $no)) * $this->k;
22006 $spacewidthu = -1000 * ($mdiff + (($ns + $no) * $one_space_width)) / $ns / $this->FontSize;
22007 if ($this->font_spacing != 0) {
22008 // fixed spacing mode
22009 $osw = -1000 * $this->font_spacing / $this->FontSize;
22010 $spacewidthu += $osw;
22012 $nsmax = $ns;
22013 $ns = 0;
22014 reset($lnstring);
22015 $offset = 0;
22016 $strcount = 0;
22017 $prev_epsposbeg = 0;
22018 $textpos = 0;
22019 if ($this->isRTLTextDir()) {
22020 $textpos = $this->wPt;
22022 global $spacew;
22023 while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
22024 // check if we are inside a string section '[( ... )]'
22025 $stroffset = strpos($pmid, '[(', $offset);
22026 if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
22027 // set offset to the end of string section
22028 $offset = strpos($pmid, ')]', $stroffset);
22029 while (($offset !== false) AND ($pmid{($offset - 1)} == '\\')) {
22030 $offset = strpos($pmid, ')]', ($offset + 1));
22032 if ($offset === false) {
22033 $this->Error('HTML Justification: malformed PDF code.');
22035 continue;
22037 if ($this->isRTLTextDir()) {
22038 $spacew = ($spacewidth * ($nsmax - $ns));
22039 } else {
22040 $spacew = ($spacewidth * $ns);
22042 $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
22043 $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
22044 $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
22045 if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
22046 OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
22047 // shift EPS images
22048 $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
22049 $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
22050 $pmid_b = substr($pmid, 0, $epsposbeg);
22051 $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
22052 $pmid_e = substr($pmid, $epsposend);
22053 $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
22054 $offset = $epsposend;
22055 continue;
22058 $prev_epsposbeg = $epsposbeg;
22059 $currentxpos = 0;
22060 // shift blocks of code
22061 switch ($strpiece[2][0]) {
22062 case 'Td':
22063 case 'cm':
22064 case 'm':
22065 case 'l': {
22066 // get current X position
22067 preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
22068 $currentxpos = $xmatches[1];
22069 $textpos = $currentxpos;
22070 if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
22071 $ns = $lnstring[3][$strcount];
22072 if ($this->isRTLTextDir()) {
22073 $spacew = ($spacewidth * ($nsmax - $ns));
22075 ++$strcount;
22077 // justify block
22078 $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
22079 create_function('$matches', 'global $spacew;
22080 $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
22081 return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
22082 break;
22084 case 're': {
22085 // justify block
22086 if (!$this->empty_string($this->lispacer)) {
22087 $this->lispacer = '';
22088 continue;
22090 preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
22091 $currentxpos = $xmatches[1];
22092 global $x_diff, $w_diff;
22093 $x_diff = 0;
22094 $w_diff = 0;
22095 if ($this->isRTLTextDir()) { // RTL
22096 if ($currentxpos < $textpos) {
22097 $x_diff = ($spacewidth * ($nsmax - $lnstring[3][$strcount]));
22098 $w_diff = ($spacewidth * $lnstring[2][$strcount]);
22099 } else {
22100 if ($strcount > 0) {
22101 $x_diff = ($spacewidth * ($nsmax - $lnstring[3][($strcount - 1)]));
22102 $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
22105 } else { // LTR
22106 if ($currentxpos > $textpos) {
22107 if ($strcount > 0) {
22108 $x_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
22110 $w_diff = ($spacewidth * $lnstring[2][$strcount]);
22111 } else {
22112 if ($strcount > 1) {
22113 $x_diff = ($spacewidth * $lnstring[3][($strcount - 2)]);
22115 if ($strcount > 0) {
22116 $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
22120 $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x',
22121 create_function('$matches', 'global $x_diff, $w_diff;
22122 $newx = sprintf("%.2F",(floatval($matches[1]) + $x_diff));
22123 $neww = sprintf("%.2F",(floatval($matches[3]) + $w_diff));
22124 return "".$newx." ".$matches[2]." ".$neww." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
22125 break;
22127 case 'c': {
22128 // get current X position
22129 preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x', $pmid, $xmatches);
22130 $currentxpos = $xmatches[1];
22131 // justify block
22132 $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$xmatches[4].')[\s]('.$xmatches[5].')[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x',
22133 create_function('$matches', 'global $spacew;
22134 $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
22135 $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
22136 $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
22137 return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
22138 break;
22141 // shift the annotations and links
22142 $cxpos = ($currentxpos / $this->k);
22143 $lmpos = ($this->lMargin + $this->cell_padding['L'] + $this->feps);
22144 if ($this->inxobj) {
22145 // we are inside an XObject template
22146 foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
22147 if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
22148 if ($cxpos > $lmpos) {
22149 $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += ($spacew / $this->k);
22150 $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
22151 } else {
22152 $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
22154 break;
22157 } elseif (isset($this->PageAnnots[$this->page])) {
22158 foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
22159 if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
22160 if ($cxpos > $lmpos) {
22161 $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
22162 $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
22163 } else {
22164 $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
22166 break;
22170 } // end of while
22171 // remove markers
22172 $pmid = str_replace('x*#!#*x', '', $pmid);
22173 if ($this->isUnicodeFont()) {
22174 // multibyte characters
22175 $spacew = $spacewidthu;
22176 if ($this->font_stretching != 100) {
22177 // word spacing is affected by stretching
22178 $spacew /= ($this->font_stretching / 100);
22180 $pmidtemp = $pmid;
22181 // escape special characters
22182 $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
22183 $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
22184 $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
22185 create_function('$matches', 'global $spacew;
22186 $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
22187 $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
22188 return "[(".str_replace(chr(0).chr(32), ") ".sprintf("%.3F", $spacew)." (", $matches[1]).")]";'), $pmidtemp);
22189 if ($this->inxobj) {
22190 // we are inside an XObject template
22191 $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\n".$pend;
22192 } else {
22193 $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
22195 $endlinepos = strlen($pstart."\n".$pmid."\n");
22196 } else {
22197 // non-unicode (single-byte characters)
22198 if ($this->font_stretching != 100) {
22199 // word spacing (Tw) is affected by stretching
22200 $spacewidth /= ($this->font_stretching / 100);
22202 $rs = sprintf('%.3F Tw', $spacewidth);
22203 $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
22204 if ($this->inxobj) {
22205 // we are inside an XObject template
22206 $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend;
22207 } else {
22208 $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
22210 $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
22213 } // end of J
22214 } // end if $startlinex
22215 if (($t_x != 0) OR ($yshift < 0)) {
22216 // shift the line
22217 $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
22218 $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
22219 $endlinepos = strlen($pstart);
22220 if ($this->inxobj) {
22221 // we are inside an XObject template
22222 $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
22223 foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
22224 if ($pak >= $pask) {
22225 $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
22226 $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
22229 } else {
22230 $this->setPageBuffer($startlinepage, $pstart.$pend);
22231 // shift the annotations and links
22232 if (isset($this->PageAnnots[$this->page])) {
22233 foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
22234 if ($pak >= $pask) {
22235 $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
22236 $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
22241 $this->y -= $yshift;
22244 $pbrk = $this->checkPageBreak($this->lasth);
22245 $this->newline = false;
22246 $startlinex = $this->x;
22247 $startliney = $this->y;
22248 if ($dom[$dom[$key]['parent']]['value'] == 'sup') {
22249 $startliney -= ((0.3 * $this->FontSizePt) / $this->k);
22250 } elseif ($dom[$dom[$key]['parent']]['value'] == 'sub') {
22251 $startliney -= (($this->FontSizePt / 0.7) / $this->k);
22252 } else {
22253 $minstartliney = $startliney;
22254 $maxbottomliney = ($this->y + (($fontsize * $this->cell_height_ratio) / $this->k));
22256 $startlinepage = $this->page;
22257 if (isset($endlinepos) AND (!$pbrk)) {
22258 $startlinepos = $endlinepos;
22259 } else {
22260 if ($this->inxobj) {
22261 // we are inside an XObject template
22262 $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
22263 } elseif (!$this->InFooter) {
22264 if (isset($this->footerlen[$this->page])) {
22265 $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
22266 } else {
22267 $this->footerpos[$this->page] = $this->pagelen[$this->page];
22269 $startlinepos = $this->footerpos[$this->page];
22270 } else {
22271 $startlinepos = $this->pagelen[$this->page];
22274 unset($endlinepos);
22275 $plalign = $lalign;
22276 if (isset($this->PageAnnots[$this->page])) {
22277 $pask = count($this->PageAnnots[$this->page]);
22278 } else {
22279 $pask = 0;
22281 if (!($dom[$key]['tag'] AND !$dom[$key]['opening'] AND ($dom[$key]['value'] == 'table')
22282 AND (isset($this->emptypagemrk[$this->page]))
22283 AND ($this->emptypagemrk[$this->page] == $this->pagelen[$this->page]))) {
22284 $this->SetFont($fontname, $fontstyle, $fontsize);
22285 if ($wfill) {
22286 $this->SetFillColorArray($this->bgcolor);
22289 } // end newline
22290 if (isset($opentagpos)) {
22291 unset($opentagpos);
22293 if ($dom[$key]['tag']) {
22294 if ($dom[$key]['opening']) {
22295 // get text indentation (if any)
22296 if (isset($dom[$key]['text-indent']) AND $dom[$key]['block']) {
22297 $this->textindent = $dom[$key]['text-indent'];
22298 $this->newline = true;
22300 // table
22301 if ($dom[$key]['value'] == 'table') {
22302 // available page width
22303 if ($this->rtl) {
22304 $wtmp = $this->x - $this->lMargin;
22305 } else {
22306 $wtmp = $this->w - $this->rMargin - $this->x;
22308 // get cell spacing
22309 if (isset($dom[$key]['attribute']['cellspacing'])) {
22310 $clsp = $this->getHTMLUnitToUnits($dom[$key]['attribute']['cellspacing'], 1, 'px');
22311 $cellspacing = array('H' => $clsp, 'V' => $clsp);
22312 } elseif (isset($dom[$key]['border-spacing'])) {
22313 $cellspacing = $dom[$key]['border-spacing'];
22314 } else {
22315 $cellspacing = array('H' => 0, 'V' => 0);
22317 // table width
22318 if (isset($dom[$key]['width'])) {
22319 $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
22320 } else {
22321 $table_width = $wtmp;
22323 $table_width -= (2 * $cellspacing['H']);
22324 if (!$this->inthead) {
22325 $this->y += $cellspacing['V'];
22327 if ($this->rtl) {
22328 $cellspacingx = -$cellspacing['H'];
22329 } else {
22330 $cellspacingx = $cellspacing['H'];
22332 // total table width without cellspaces
22333 $table_columns_width = ($table_width - ($cellspacing['H'] * ($dom[$key]['cols'] - 1)));
22334 // minimum column width
22335 $table_min_column_width = ($table_columns_width / $dom[$key]['cols']);
22336 // array of custom column widths
22337 $table_colwidths = array_fill(0, $dom[$key]['cols'], $table_min_column_width);
22339 // table row
22340 if ($dom[$key]['value'] == 'tr') {
22341 // reset column counter
22342 $colid = 0;
22344 // table cell
22345 if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
22346 $trid = $dom[$key]['parent'];
22347 $table_el = $dom[$trid]['parent'];
22348 if (!isset($dom[$table_el]['cols'])) {
22349 $dom[$table_el]['cols'] = $dom[$trid]['cols'];
22351 // store border info
22352 $tdborder = 0;
22353 if (isset($dom[$key]['border']) AND !empty($dom[$key]['border'])) {
22354 $tdborder = $dom[$key]['border'];
22356 $colspan = $dom[$key]['attribute']['colspan'];
22357 $old_cell_padding = $this->cell_padding;
22358 if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
22359 $crclpd = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
22360 $current_cell_padding = array('L' => $crclpd, 'T' => $crclpd, 'R' => $crclpd, 'B' => $crclpd);
22361 } elseif (isset($dom[($dom[$trid]['parent'])]['padding'])) {
22362 $current_cell_padding = $dom[($dom[$trid]['parent'])]['padding'];
22363 } else {
22364 $current_cell_padding = array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0);
22366 $this->cell_padding = $current_cell_padding;
22367 if (isset($dom[$key]['height'])) {
22368 // minimum cell height
22369 $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
22370 } else {
22371 $cellh = 0;
22373 if (isset($dom[$key]['content'])) {
22374 $cell_content = stripslashes($dom[$key]['content']);
22375 } else {
22376 $cell_content = '&nbsp;';
22378 $tagtype = $dom[$key]['value'];
22379 $parentid = $key;
22380 while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
22381 // move $key index forward
22382 ++$key;
22384 if (!isset($dom[$trid]['startpage'])) {
22385 $dom[$trid]['startpage'] = $this->page;
22386 } else {
22387 $this->setPage($dom[$trid]['startpage']);
22389 if (!isset($dom[$trid]['startcolumn'])) {
22390 $dom[$trid]['startcolumn'] = $this->current_column;
22391 } elseif ($this->current_column != $dom[$trid]['startcolumn']) {
22392 $tmpx = $this->x;
22393 $this->selectColumn($dom[$trid]['startcolumn']);
22394 $this->x = $tmpx;
22396 if (!isset($dom[$trid]['starty'])) {
22397 $dom[$trid]['starty'] = $this->y;
22398 } else {
22399 $this->y = $dom[$trid]['starty'];
22401 if (!isset($dom[$trid]['startx'])) {
22402 $dom[$trid]['startx'] = $this->x;
22403 $this->x += $cellspacingx;
22404 } else {
22405 $this->x += ($cellspacingx / 2);
22407 if (isset($dom[$parentid]['attribute']['rowspan'])) {
22408 $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
22409 } else {
22410 $rowspan = 1;
22412 // skip row-spanned cells started on the previous rows
22413 if (isset($dom[$table_el]['rowspans'])) {
22414 $rsk = 0;
22415 $rskmax = count($dom[$table_el]['rowspans']);
22416 while ($rsk < $rskmax) {
22417 $trwsp = $dom[$table_el]['rowspans'][$rsk];
22418 $rsstartx = $trwsp['startx'];
22419 $rsendx = $trwsp['endx'];
22420 // account for margin changes
22421 if ($trwsp['startpage'] < $this->page) {
22422 if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
22423 $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
22424 $rsstartx -= $dl;
22425 $rsendx -= $dl;
22426 } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
22427 $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
22428 $rsstartx += $dl;
22429 $rsendx += $dl;
22432 if (($trwsp['rowspan'] > 0)
22433 AND ($rsstartx > ($this->x - $cellspacing['H'] - $current_cell_padding['L'] - $this->feps))
22434 AND ($rsstartx < ($this->x + $cellspacing['H'] + $current_cell_padding['R'] + $this->feps))
22435 AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page) OR ($trwsp['startcolumn'] < $this->current_column))) {
22436 // set the starting X position of the current cell
22437 $this->x = $rsendx + $cellspacingx;
22438 // increment column indicator
22439 $colid += $trwsp['colspan'];
22440 if (($trwsp['rowspan'] == 1)
22441 AND (isset($dom[$trid]['endy']))
22442 AND (isset($dom[$trid]['endpage']))
22443 AND (isset($dom[$trid]['endcolumn']))
22444 AND ($trwsp['endpage'] == $dom[$trid]['endpage'])
22445 AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
22446 // set ending Y position for row
22447 $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
22448 $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
22450 $rsk = 0;
22451 } else {
22452 ++$rsk;
22456 if (isset($dom[$parentid]['width'])) {
22457 // user specified width
22458 $cellw = $this->getHTMLUnitToUnits($dom[$parentid]['width'], $table_columns_width, 'px');
22459 $tmpcw = ($cellw / $colspan);
22460 for ($i = 0; $i < $colspan; ++$i) {
22461 $table_colwidths[($colid + $i)] = $tmpcw;
22463 } else {
22464 // inherit column width
22465 $cellw = 0;
22466 for ($i = 0; $i < $colspan; ++$i) {
22467 $cellw += $table_colwidths[($colid + $i)];
22470 $cellw += (($colspan - 1) * $cellspacing['H']);
22471 // increment column indicator
22472 $colid += $colspan;
22473 // add rowspan information to table element
22474 if ($rowspan > 1) {
22475 $trsid = array_push($dom[$table_el]['rowspans'], array('trid' => $trid, 'rowspan' => $rowspan, 'mrowspan' => $rowspan, 'colspan' => $colspan, 'startpage' => $this->page, 'startcolumn' => $this->current_column, 'startx' => $this->x, 'starty' => $this->y));
22477 $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
22478 if ($rowspan > 1) {
22479 $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
22481 // push background colors
22482 if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
22483 $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
22485 // store border info
22486 if (isset($tdborder) AND !empty($tdborder)) {
22487 $dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
22489 $prevLastH = $this->lasth;
22490 // store some info for multicolumn mode
22491 if ($this->rtl) {
22492 $this->colxshift['x'] = $this->w - $this->x - $this->rMargin;
22493 } else {
22494 $this->colxshift['x'] = $this->x - $this->lMargin;
22496 $this->colxshift['s'] = $cellspacing;
22497 $this->colxshift['p'] = $current_cell_padding;
22498 // ****** write the cell content ******
22499 $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true, true, 0, 'T', false);
22500 // restore some values
22501 $this->colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
22502 $this->lasth = $prevLastH;
22503 $this->cell_padding = $old_cell_padding;
22504 $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
22505 // update the end of row position
22506 if ($rowspan <= 1) {
22507 if (isset($dom[$trid]['endy'])) {
22508 if (($this->page == $dom[$trid]['endpage']) AND ($this->current_column == $dom[$trid]['endcolumn'])) {
22509 $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
22510 } elseif (($this->page > $dom[$trid]['endpage']) OR ($this->current_column > $dom[$trid]['endcolumn'])) {
22511 $dom[$trid]['endy'] = $this->y;
22513 } else {
22514 $dom[$trid]['endy'] = $this->y;
22516 if (isset($dom[$trid]['endpage'])) {
22517 $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
22518 } else {
22519 $dom[$trid]['endpage'] = $this->page;
22521 if (isset($dom[$trid]['endcolumn'])) {
22522 $dom[$trid]['endcolumn'] = max($this->current_column, $dom[$trid]['endcolumn']);
22523 } else {
22524 $dom[$trid]['endcolumn'] = $this->current_column;
22526 } else {
22527 // account for row-spanned cells
22528 $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
22529 $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
22530 $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
22531 $dom[$table_el]['rowspans'][($trsid - 1)]['endcolumn'] = $this->current_column;
22533 if (isset($dom[$table_el]['rowspans'])) {
22534 // update endy and endpage on rowspanned cells
22535 foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
22536 if ($trwsp['rowspan'] > 0) {
22537 if (isset($dom[$trid]['endpage'])) {
22538 if (($trwsp['endpage'] == $dom[$trid]['endpage']) AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
22539 $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
22540 } elseif (($trwsp['endpage'] < $dom[$trid]['endpage']) OR ($trwsp['endcolumn'] < $dom[$trid]['endcolumn'])) {
22541 $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
22542 $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
22543 $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[$trid]['endcolumn'];
22544 } else {
22545 $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
22551 $this->x += ($cellspacingx / 2);
22552 } else {
22553 // opening tag (or self-closing tag)
22554 if (!isset($opentagpos)) {
22555 if ($this->inxobj) {
22556 // we are inside an XObject template
22557 $opentagpos = strlen($this->xobjects[$this->xobjid]['outdata']);
22558 } elseif (!$this->InFooter) {
22559 if (isset($this->footerlen[$this->page])) {
22560 $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
22561 } else {
22562 $this->footerpos[$this->page] = $this->pagelen[$this->page];
22564 $opentagpos = $this->footerpos[$this->page];
22567 $dom = $this->openHTMLTagHandler($dom, $key, $cell);
22569 } else { // closing tag
22570 $prev_numpages = $this->numpages;
22571 $old_bordermrk = $this->bordermrk[$this->page];
22572 $dom = $this->closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney);
22573 if ($this->bordermrk[$this->page] > $old_bordermrk) {
22574 $startlinepos += ($this->bordermrk[$this->page] - $old_bordermrk);
22576 if ($prev_numpages > $this->numpages) {
22577 $startlinepage = $this->page;
22580 } elseif (strlen($dom[$key]['value']) > 0) {
22581 // print list-item
22582 if (!$this->empty_string($this->lispacer) AND ($this->lispacer != '^')) {
22583 $this->SetFont($pfontname, $pfontstyle, $pfontsize);
22584 $this->resetLastH();
22585 $minstartliney = $this->y;
22586 $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
22587 $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
22588 $this->SetFont($curfontname, $curfontstyle, $curfontsize);
22589 $this->resetLastH();
22590 if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
22591 $pfontascent = $this->getFontAscent($pfontname, $pfontstyle, $pfontsize);
22592 $pfontdescent = $this->getFontDescent($pfontname, $pfontstyle, $pfontsize);
22593 $this->y += ((($pfontsize - $curfontsize) * $this->cell_height_ratio / $this->k) + $pfontascent - $curfontascent - $pfontdescent + $curfontdescent) / 2;
22594 $minstartliney = min($this->y, $minstartliney);
22595 $maxbottomliney = max(($this->y + (($pfontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
22598 // text
22599 $this->htmlvspace = 0;
22600 if ((!$this->premode) AND $this->isRTLTextDir()) {
22601 // reverse spaces order
22602 $lsp = ''; // left spaces
22603 $rsp = ''; // right spaces
22604 if (preg_match('/^('.$this->re_space['p'].'+)/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
22605 $lsp = $matches[1];
22607 if (preg_match('/('.$this->re_space['p'].'+)$/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
22608 $rsp = $matches[1];
22610 $dom[$key]['value'] = $rsp.$this->stringTrim($dom[$key]['value']).$lsp;
22612 if ($newline) {
22613 if (!$this->premode) {
22614 $prelen = strlen($dom[$key]['value']);
22615 if ($this->isRTLTextDir()) {
22616 // right trim except non-breaking space
22617 $dom[$key]['value'] = $this->stringRightTrim($dom[$key]['value']);
22618 } else {
22619 // left trim except non-breaking space
22620 $dom[$key]['value'] = $this->stringLeftTrim($dom[$key]['value']);
22622 $postlen = strlen($dom[$key]['value']);
22623 if (($postlen == 0) AND ($prelen > 0)) {
22624 $dom[$key]['trimmed_space'] = true;
22627 $newline = false;
22628 $firstblock = true;
22629 } else {
22630 $firstblock = false;
22631 // replace empty multiple spaces string with a single space
22632 $dom[$key]['value'] = preg_replace('/^'.$this->re_space['p'].'+$/'.$this->re_space['m'], chr(32), $dom[$key]['value']);
22634 $strrest = '';
22635 if ($this->rtl) {
22636 $this->x -= $this->textindent;
22637 } else {
22638 $this->x += $this->textindent;
22640 if (!isset($dom[$key]['trimmed_space']) OR !$dom[$key]['trimmed_space']) {
22641 $strlinelen = $this->GetStringWidth($dom[$key]['value']);
22642 if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
22643 // HTML <a> Link
22644 $hrefcolor = '';
22645 if (isset($dom[($dom[$key]['parent'])]['fgcolor']) AND ($dom[($dom[$key]['parent'])]['fgcolor'] !== false)) {
22646 $hrefcolor = $dom[($dom[$key]['parent'])]['fgcolor'];
22648 $hrefstyle = -1;
22649 if (isset($dom[($dom[$key]['parent'])]['fontstyle']) AND ($dom[($dom[$key]['parent'])]['fontstyle'] !== false)) {
22650 $hrefstyle = $dom[($dom[$key]['parent'])]['fontstyle'];
22652 $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $hrefcolor, $hrefstyle, true);
22653 } else {
22654 $wadj = 0; // space to leave for block continuity
22655 if ($this->rtl) {
22656 $cwa = $this->x - $this->lMargin;
22657 } else {
22658 $cwa = $this->w - $this->rMargin - $this->x;
22660 if (($strlinelen < $cwa) AND (isset($dom[($key + 1)])) AND ($dom[($key + 1)]['tag']) AND (!$dom[($key + 1)]['block'])) {
22661 // check the next text blocks for continuity
22662 $nkey = ($key + 1);
22663 $write_block = true;
22664 $same_textdir = true;
22665 $tmp_fontname = $this->FontFamily;
22666 $tmp_fontstyle = $this->FontStyle;
22667 $tmp_fontsize = $this->FontSizePt;
22668 while ($write_block AND isset($dom[$nkey])) {
22669 if ($dom[$nkey]['tag']) {
22670 if ($dom[$nkey]['block']) {
22671 // end of block
22672 $write_block = false;
22674 $tmp_fontname = isset($dom[$nkey]['fontname']) ? $dom[$nkey]['fontname'] : $this->FontFamily;
22675 $tmp_fontstyle = isset($dom[$nkey]['fontstyle']) ? $dom[$nkey]['fontstyle'] : $this->FontStyle;
22676 $tmp_fontsize = isset($dom[$nkey]['fontsize']) ? $dom[$nkey]['fontsize'] : $this->FontSizePt;
22677 $same_textdir = ($dom[$nkey]['dir'] == $dom[$key]['dir']);
22678 } else {
22679 $nextstr = preg_split('/'.$this->re_space['p'].'+/'.$this->re_space['m'], $dom[$nkey]['value']);
22680 if (isset($nextstr[0]) AND $same_textdir) {
22681 $wadj += $this->GetStringWidth($nextstr[0], $tmp_fontname, $tmp_fontstyle, $tmp_fontsize);
22683 if (isset($nextstr[1])) {
22684 $write_block = false;
22687 ++$nkey;
22690 if (($wadj > 0) AND (($strlinelen + $wadj) >= $cwa)) {
22691 $wadj = 0;
22692 $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $dom[$key]['value']);
22693 $numblks = count($nextstr);
22694 if ($numblks > 1) {
22695 // try to split on blank spaces
22696 $wadj = ($cwa - $strlinelen + $this->GetStringWidth($nextstr[($numblks - 1)]));
22699 // check for reversed text direction
22700 if (($wadj > 0) AND (($this->rtl AND ($this->tmprtl === 'L')) OR (!$this->rtl AND ($this->tmprtl === 'R')))) {
22701 // LTR text on RTL direction or RTL text on LTR direction
22702 $reverse_dir = true;
22703 $this->rtl = !$this->rtl;
22704 $revshift = ($strlinelen + $wadj + 0.000001); // add little quantity for rounding problems
22705 if ($this->rtl) {
22706 $this->x += $revshift;
22707 } else {
22708 $this->x -= $revshift;
22710 $xws = $this->x;
22712 // ****** write only until the end of the line and get the rest ******
22713 $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
22714 // restore default direction
22715 if ($reverse_dir AND ($wadj == 0)) {
22716 $this->x = $xws;
22717 $this->rtl = !$this->rtl;
22718 $reverse_dir = false;
22722 $this->textindent = 0;
22723 if (strlen($strrest) > 0) {
22724 // store the remaining string on the previous $key position
22725 $this->newline = true;
22726 if ($strrest == $dom[$key]['value']) {
22727 // used to avoid infinite loop
22728 ++$loop;
22729 } else {
22730 $loop = 0;
22732 $dom[$key]['value'] = $strrest;
22733 if ($cell) {
22734 if ($this->rtl) {
22735 $this->x -= $this->cell_padding['R'];
22736 } else {
22737 $this->x += $this->cell_padding['L'];
22740 if ($loop < 3) {
22741 --$key;
22743 } else {
22744 $loop = 0;
22747 ++$key;
22748 if (isset($dom[$key]['tag']) AND $dom[$key]['tag'] AND (!isset($dom[$key]['opening']) OR !$dom[$key]['opening']) AND isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
22749 // check if we are on a new page or on a new column
22750 if ((!$undo) AND (($this->y < $this->start_transaction_y) OR (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['endy'] < $this->start_transaction_y)))) {
22751 // we are on a new page or on a new column and the total object height is less than the available vertical space.
22752 // restore previous object
22753 $this->rollbackTransaction(true);
22754 // restore previous values
22755 foreach ($this_method_vars as $vkey => $vval) {
22756 $$vkey = $vval;
22758 // add a page (or trig AcceptPageBreak() for multicolumn mode)
22759 $pre_y = $this->y;
22760 if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
22761 $startliney = $this->y;
22763 $undo = true; // avoid infinite loop
22764 } else {
22765 $undo = false;
22768 } // end for each $key
22769 // align the last line
22770 if (isset($startlinex)) {
22771 $yshift = ($minstartliney - $startliney);
22772 if (($yshift > 0) OR ($this->page > $startlinepage)) {
22773 $yshift = 0;
22775 $t_x = 0;
22776 // the last line must be shifted to be aligned as requested
22777 $linew = abs($this->endlinex - $startlinex);
22778 if ($this->inxobj) {
22779 // we are inside an XObject template
22780 $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
22781 if (isset($opentagpos)) {
22782 $midpos = $opentagpos;
22783 } else {
22784 $midpos = 0;
22786 if ($midpos > 0) {
22787 $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
22788 $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
22789 } else {
22790 $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
22791 $pend = '';
22793 } else {
22794 $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
22795 if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
22796 $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
22797 $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
22798 } elseif (isset($opentagpos)) {
22799 $midpos = $opentagpos;
22800 } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
22801 $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
22802 $midpos = $this->footerpos[$startlinepage];
22803 } else {
22804 $midpos = 0;
22806 if ($midpos > 0) {
22807 $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
22808 $pend = substr($this->getPageBuffer($startlinepage), $midpos);
22809 } else {
22810 $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
22811 $pend = '';
22814 if ((isset($plalign) AND ((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
22815 // calculate shifting amount
22816 $tw = $w;
22817 if ($this->lMargin != $prevlMargin) {
22818 $tw += ($prevlMargin - $this->lMargin);
22820 if ($this->rMargin != $prevrMargin) {
22821 $tw += ($prevrMargin - $this->rMargin);
22823 $one_space_width = $this->GetStringWidth(chr(32));
22824 $no = 0; // number of spaces on a line contained on a single block
22825 if ($this->isRTLTextDir()) { // RTL
22826 // remove left space if exist
22827 $pos1 = $this->revstrpos($pmid, '[(');
22828 if ($pos1 > 0) {
22829 $pos1 = intval($pos1);
22830 if ($this->isUnicodeFont()) {
22831 $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
22832 $spacelen = 2;
22833 } else {
22834 $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
22835 $spacelen = 1;
22837 if ($pos1 == $pos2) {
22838 $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
22839 if (substr($pmid, $pos1, 4) == '[()]') {
22840 $linew -= $one_space_width;
22841 } elseif ($pos1 == strpos($pmid, '[(')) {
22842 $no = 1;
22846 } else { // LTR
22847 // remove right space if exist
22848 $pos1 = $this->revstrpos($pmid, ')]');
22849 if ($pos1 > 0) {
22850 $pos1 = intval($pos1);
22851 if ($this->isUnicodeFont()) {
22852 $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
22853 $spacelen = 2;
22854 } else {
22855 $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
22856 $spacelen = 1;
22858 if ($pos1 == $pos2) {
22859 $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
22860 $linew -= $one_space_width;
22864 $mdiff = ($tw - $linew);
22865 if ($plalign == 'C') {
22866 if ($this->rtl) {
22867 $t_x = -($mdiff / 2);
22868 } else {
22869 $t_x = ($mdiff / 2);
22871 } elseif ($plalign == 'R') {
22872 // right alignment on LTR document
22873 $t_x = $mdiff;
22874 } elseif ($plalign == 'L') {
22875 // left alignment on RTL document
22876 $t_x = -$mdiff;
22878 } // end if startlinex
22879 if (($t_x != 0) OR ($yshift < 0)) {
22880 // shift the line
22881 $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
22882 $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
22883 $endlinepos = strlen($pstart);
22884 if ($this->inxobj) {
22885 // we are inside an XObject template
22886 $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
22887 foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
22888 if ($pak >= $pask) {
22889 $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
22890 $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
22893 } else {
22894 $this->setPageBuffer($startlinepage, $pstart.$pend);
22895 // shift the annotations and links
22896 if (isset($this->PageAnnots[$this->page])) {
22897 foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
22898 if ($pak >= $pask) {
22899 $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
22900 $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
22905 $this->y -= $yshift;
22906 $yshift = 0;
22909 // restore previous values
22910 $this->setGraphicVars($gvars);
22911 if ($this->num_columns > 1) {
22912 $this->selectColumn();
22913 } elseif ($this->page > $prevPage) {
22914 $this->lMargin = $this->pagedim[$this->page]['olm'];
22915 $this->rMargin = $this->pagedim[$this->page]['orm'];
22917 // restore previous list state
22918 $this->cell_height_ratio = $prev_cell_height_ratio;
22919 $this->listnum = $prev_listnum;
22920 $this->listordered = $prev_listordered;
22921 $this->listcount = $prev_listcount;
22922 $this->lispacer = $prev_lispacer;
22923 if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
22924 $this->Ln($this->lasth);
22925 if ($this->y < $maxbottomliney) {
22926 $this->y = $maxbottomliney;
22929 unset($dom);
22933 * Process opening tags.
22934 * @param $dom (array) html dom array
22935 * @param $key (int) current element id
22936 * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
22937 * @return $dom array
22938 * @protected
22940 protected function openHTMLTagHandler($dom, $key, $cell) {
22941 $tag = $dom[$key];
22942 $parent = $dom[($dom[$key]['parent'])];
22943 $firsttag = ($key == 1);
22944 // check for text direction attribute
22945 if (isset($tag['dir'])) {
22946 $this->setTempRTL($tag['dir']);
22947 } else {
22948 $this->tmprtl = false;
22950 if ($tag['block']) {
22951 $hbz = 0; // distance from y to line bottom
22952 $hb = 0; // vertical space between block tags
22953 // calculate vertical space for block tags
22954 if (isset($this->tagvspaces[$tag['value']][0]['h']) AND ($this->tagvspaces[$tag['value']][0]['h'] >= 0)) {
22955 $cur_h = $this->tagvspaces[$tag['value']][0]['h'];
22956 } elseif (isset($tag['fontsize'])) {
22957 $cur_h = ($tag['fontsize'] / $this->k) * $this->cell_height_ratio;
22958 } else {
22959 $cur_h = $this->FontSize * $this->cell_height_ratio;
22961 if (isset($this->tagvspaces[$tag['value']][0]['n'])) {
22962 $n = $this->tagvspaces[$tag['value']][0]['n'];
22963 } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
22964 $n = 0.6;
22965 } else {
22966 $n = 1;
22968 if ((!isset($this->tagvspaces[$tag['value']])) AND (in_array($tag['value'], array('div', 'dt', 'dd', 'li', 'br')))) {
22969 $hb = 0;
22970 } else {
22971 $hb = ($n * $cur_h);
22973 if (($this->htmlvspace <= 0) AND ($n > 0)) {
22974 if (isset($parent['fontsize'])) {
22975 $hbz = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
22976 } else {
22977 $hbz = $this->FontSize * $this->cell_height_ratio;
22981 // Opening tag
22982 switch($tag['value']) {
22983 case 'table': {
22984 $cp = 0;
22985 $cs = 0;
22986 $dom[$key]['rowspans'] = array();
22987 if (!isset($dom[$key]['attribute']['nested']) OR ($dom[$key]['attribute']['nested'] != 'true')) {
22988 // set table header
22989 if (!$this->empty_string($dom[$key]['thead'])) {
22990 // set table header
22991 $this->thead = $dom[$key]['thead'];
22992 if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
22993 $this->theadMargins = array();
22994 $this->theadMargins['cell_padding'] = $this->cell_padding;
22995 $this->theadMargins['lmargin'] = $this->lMargin;
22996 $this->theadMargins['rmargin'] = $this->rMargin;
22997 $this->theadMargins['page'] = $this->page;
22998 $this->theadMargins['cell'] = $cell;
23002 // store current margins and page
23003 $dom[$key]['old_cell_padding'] = $this->cell_padding;
23004 if (isset($tag['attribute']['cellpadding'])) {
23005 $pad = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
23006 $this->SetCellPadding($pad);
23007 } elseif (isset($tag['padding'])) {
23008 $this->cell_padding = $tag['padding'];
23010 if (isset($tag['attribute']['cellspacing'])) {
23011 $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
23012 } elseif (isset($tag['border-spacing'])) {
23013 $cs = $tag['border-spacing']['V'];
23015 $prev_y = $this->y;
23016 if ($this->checkPageBreak(((2 * $cp) + (2 * $cs) + $this->lasth), '', false) OR ($this->y < $prev_y)) {
23017 $this->inthead = true;
23018 // add a page (or trig AcceptPageBreak() for multicolumn mode)
23019 $this->checkPageBreak($this->PageBreakTrigger + 1);
23021 break;
23023 case 'tr': {
23024 // array of columns positions
23025 $dom[$key]['cellpos'] = array();
23026 break;
23028 case 'hr': {
23029 if ((isset($tag['height'])) AND ($tag['height'] != '')) {
23030 $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
23031 } else {
23032 $hrHeight = $this->GetLineWidth();
23034 $this->addHTMLVertSpace($hbz, ($hrHeight / 2), $cell, $firsttag);
23035 $x = $this->GetX();
23036 $y = $this->GetY();
23037 $wtmp = $this->w - $this->lMargin - $this->rMargin;
23038 if ($cell) {
23039 $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
23041 if ((isset($tag['width'])) AND ($tag['width'] != '')) {
23042 $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
23043 } else {
23044 $hrWidth = $wtmp;
23046 $prevlinewidth = $this->GetLineWidth();
23047 $this->SetLineWidth($hrHeight);
23048 $this->Line($x, $y, $x + $hrWidth, $y);
23049 $this->SetLineWidth($prevlinewidth);
23050 $this->addHTMLVertSpace(($hrHeight / 2), 0, $cell, !isset($dom[($key + 1)]));
23051 break;
23053 case 'a': {
23054 if (array_key_exists('href', $tag['attribute'])) {
23055 $this->HREF['url'] = $tag['attribute']['href'];
23057 break;
23059 case 'img': {
23060 if (isset($tag['attribute']['src'])) {
23061 if ($tag['attribute']['src']{0} === '@') {
23062 // data stream
23063 $tag['attribute']['src'] = '@'.base64_decode(substr($tag['attribute']['src'], 1));
23064 $type = '';
23065 } else {
23066 // check for images without protocol
23067 if (preg_match('%^/{2}%', $tag['attribute']['src'])) {
23068 $tag['attribute']['src'] = 'http:'.$tag['attribute']['src'];
23070 // replace relative path with real server path
23071 if (($tag['attribute']['src'][0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
23072 $findroot = strpos($tag['attribute']['src'], $_SERVER['DOCUMENT_ROOT']);
23073 if (($findroot === false) OR ($findroot > 1)) {
23074 if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
23075 $tag['attribute']['src'] = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$tag['attribute']['src'];
23076 } else {
23077 $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
23081 $tag['attribute']['src'] = htmlspecialchars_decode(urldecode($tag['attribute']['src']));
23082 $type = $this->getImageFileType($tag['attribute']['src']);
23083 $testscrtype = @parse_url($tag['attribute']['src']);
23084 if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
23085 // convert URL to server path
23086 $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
23089 if (!isset($tag['width'])) {
23090 $tag['width'] = 0;
23092 if (!isset($tag['height'])) {
23093 $tag['height'] = 0;
23095 //if (!isset($tag['attribute']['align'])) {
23096 // the only alignment supported is "bottom"
23097 // further development is required for other modes.
23098 $tag['attribute']['align'] = 'bottom';
23100 switch($tag['attribute']['align']) {
23101 case 'top': {
23102 $align = 'T';
23103 break;
23105 case 'middle': {
23106 $align = 'M';
23107 break;
23109 case 'bottom': {
23110 $align = 'B';
23111 break;
23113 default: {
23114 $align = 'B';
23115 break;
23118 $prevy = $this->y;
23119 $xpos = $this->x;
23120 $imglink = '';
23121 if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
23122 $imglink = $this->HREF['url'];
23123 if ($imglink{0} == '#') {
23124 // convert url to internal link
23125 $lnkdata = explode(',', $imglink);
23126 if (isset($lnkdata[0])) {
23127 $page = intval(substr($lnkdata[0], 1));
23128 if (empty($page) OR ($page <= 0)) {
23129 $page = $this->page;
23131 if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
23132 $lnky = floatval($lnkdata[1]);
23133 } else {
23134 $lnky = 0;
23136 $imglink = $this->AddLink();
23137 $this->SetLink($imglink, $lnky, $page);
23141 $border = 0;
23142 if (isset($tag['border']) AND !empty($tag['border'])) {
23143 // currently only support 1 (frame) or a combination of 'LTRB'
23144 $border = $tag['border'];
23146 $iw = '';
23147 if (isset($tag['width'])) {
23148 $iw = $this->getHTMLUnitToUnits($tag['width'], 1, 'px', false);
23150 $ih = '';
23151 if (isset($tag['height'])) {
23152 $ih = $this->getHTMLUnitToUnits($tag['height'], 1, 'px', false);
23154 if (($type == 'eps') OR ($type == 'ai')) {
23155 $this->ImageEps($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, true, $align, '', $border, true);
23156 } elseif ($type == 'svg') {
23157 $this->ImageSVG($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, $align, '', $border, true);
23158 } else {
23159 $this->Image($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border, false, false, true);
23161 switch($align) {
23162 case 'T': {
23163 $this->y = $prevy;
23164 break;
23166 case 'M': {
23167 $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
23168 break;
23170 case 'B': {
23171 $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
23172 break;
23176 break;
23178 case 'dl': {
23179 ++$this->listnum;
23180 if ($this->listnum == 1) {
23181 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23182 } else {
23183 $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
23185 break;
23187 case 'dt': {
23188 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23189 break;
23191 case 'dd': {
23192 if ($this->rtl) {
23193 $this->rMargin += $this->listindent;
23194 } else {
23195 $this->lMargin += $this->listindent;
23197 ++$this->listindentlevel;
23198 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23199 break;
23201 case 'ul':
23202 case 'ol': {
23203 ++$this->listnum;
23204 if ($tag['value'] == 'ol') {
23205 $this->listordered[$this->listnum] = true;
23206 } else {
23207 $this->listordered[$this->listnum] = false;
23209 if (isset($tag['attribute']['start'])) {
23210 $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
23211 } else {
23212 $this->listcount[$this->listnum] = 0;
23214 if ($this->rtl) {
23215 $this->rMargin += $this->listindent;
23216 $this->x -= $this->listindent;
23217 } else {
23218 $this->lMargin += $this->listindent;
23219 $this->x += $this->listindent;
23221 ++$this->listindentlevel;
23222 if ($this->listnum == 1) {
23223 if ($key > 1) {
23224 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23226 } else {
23227 $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
23229 break;
23231 case 'li': {
23232 if ($key > 2) {
23233 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23235 if ($this->listordered[$this->listnum]) {
23236 // ordered item
23237 if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
23238 $this->lispacer = $parent['attribute']['type'];
23239 } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
23240 $this->lispacer = $parent['listtype'];
23241 } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
23242 $this->lispacer = $this->lisymbol;
23243 } else {
23244 $this->lispacer = '#';
23246 ++$this->listcount[$this->listnum];
23247 if (isset($tag['attribute']['value'])) {
23248 $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
23250 } else {
23251 // unordered item
23252 if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
23253 $this->lispacer = $parent['attribute']['type'];
23254 } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
23255 $this->lispacer = $parent['listtype'];
23256 } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
23257 $this->lispacer = $this->lisymbol;
23258 } else {
23259 $this->lispacer = '!';
23262 break;
23264 case 'blockquote': {
23265 if ($this->rtl) {
23266 $this->rMargin += $this->listindent;
23267 } else {
23268 $this->lMargin += $this->listindent;
23270 ++$this->listindentlevel;
23271 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23272 break;
23274 case 'br': {
23275 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23276 break;
23278 case 'div': {
23279 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23280 break;
23282 case 'p': {
23283 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23284 break;
23286 case 'pre': {
23287 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23288 $this->premode = true;
23289 break;
23291 case 'sup': {
23292 $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
23293 break;
23295 case 'sub': {
23296 $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
23297 break;
23299 case 'h1':
23300 case 'h2':
23301 case 'h3':
23302 case 'h4':
23303 case 'h5':
23304 case 'h6': {
23305 $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
23306 break;
23308 // Form fields (since 4.8.000 - 2009-09-07)
23309 case 'form': {
23310 if (isset($tag['attribute']['action'])) {
23311 $this->form_action = $tag['attribute']['action'];
23312 } else {
23313 $this->form_action = K_PATH_URL.$_SERVER['SCRIPT_NAME'];
23315 if (isset($tag['attribute']['enctype'])) {
23316 $this->form_enctype = $tag['attribute']['enctype'];
23317 } else {
23318 $this->form_enctype = 'application/x-www-form-urlencoded';
23320 if (isset($tag['attribute']['method'])) {
23321 $this->form_mode = $tag['attribute']['method'];
23322 } else {
23323 $this->form_mode = 'post';
23325 break;
23327 case 'input': {
23328 if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
23329 $name = $tag['attribute']['name'];
23330 } else {
23331 break;
23333 $prop = array();
23334 $opt = array();
23335 if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
23336 $prop['readonly'] = true;
23338 if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
23339 $value = $tag['attribute']['value'];
23341 if (isset($tag['attribute']['maxlength']) AND !$this->empty_string($tag['attribute']['maxlength'])) {
23342 $opt['maxlen'] = intval($tag['attribute']['value']);
23344 $h = $this->FontSize * $this->cell_height_ratio;
23345 if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
23346 $w = intval($tag['attribute']['size']) * $this->GetStringWidth(chr(32)) * 2;
23347 } else {
23348 $w = $h;
23350 if (isset($tag['attribute']['checked']) AND (($tag['attribute']['checked'] == 'checked') OR ($tag['attribute']['checked'] == 'true'))) {
23351 $checked = true;
23352 } else {
23353 $checked = false;
23355 if (isset($tag['align'])) {
23356 switch ($tag['align']) {
23357 case 'C': {
23358 $opt['q'] = 1;
23359 break;
23361 case 'R': {
23362 $opt['q'] = 2;
23363 break;
23365 case 'L':
23366 default: {
23367 break;
23371 switch ($tag['attribute']['type']) {
23372 case 'text': {
23373 if (isset($value)) {
23374 $opt['v'] = $value;
23376 $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
23377 break;
23379 case 'password': {
23380 if (isset($value)) {
23381 $opt['v'] = $value;
23383 $prop['password'] = 'true';
23384 $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
23385 break;
23387 case 'checkbox': {
23388 $this->CheckBox($name, $w, $checked, $prop, $opt, $value, '', '', false);
23389 break;
23391 case 'radio': {
23392 $this->RadioButton($name, $w, $prop, $opt, $value, $checked, '', '', false);
23393 break;
23395 case 'submit': {
23396 $w = $this->GetStringWidth($value) * 1.5;
23397 $h *= 1.6;
23398 $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
23399 $action = array();
23400 $action['S'] = 'SubmitForm';
23401 $action['F'] = $this->form_action;
23402 if ($this->form_enctype != 'FDF') {
23403 $action['Flags'] = array('ExportFormat');
23405 if ($this->form_mode == 'get') {
23406 $action['Flags'] = array('GetMethod');
23408 $this->Button($name, $w, $h, $value, $action, $prop, $opt, '', '', false);
23409 break;
23411 case 'reset': {
23412 $w = $this->GetStringWidth($value) * 1.5;
23413 $h *= 1.6;
23414 $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
23415 $this->Button($name, $w, $h, $value, array('S'=>'ResetForm'), $prop, $opt, '', '', false);
23416 break;
23418 case 'file': {
23419 $prop['fileSelect'] = 'true';
23420 $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
23421 if (!isset($value)) {
23422 $value = '*';
23424 $w = $this->GetStringWidth($value) * 2;
23425 $h *= 1.2;
23426 $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
23427 $jsaction = 'var f=this.getField(\''.$name.'\'); f.browseForFileToSubmit();';
23428 $this->Button('FB_'.$name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
23429 break;
23431 case 'hidden': {
23432 if (isset($value)) {
23433 $opt['v'] = $value;
23435 $opt['f'] = array('invisible', 'hidden');
23436 $this->TextField($name, 0, 0, $prop, $opt, '', '', false);
23437 break;
23439 case 'image': {
23440 // THIS TYPE MUST BE FIXED
23441 if (isset($tag['attribute']['src']) AND !$this->empty_string($tag['attribute']['src'])) {
23442 $img = $tag['attribute']['src'];
23443 } else {
23444 break;
23446 $value = 'img';
23447 //$opt['mk'] = array('i'=>$img, 'tp'=>1, 'if'=>array('sw'=>'A', 's'=>'A', 'fb'=>false));
23448 if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
23449 $jsaction = $tag['attribute']['onclick'];
23450 } else {
23451 $jsaction = '';
23453 $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
23454 break;
23456 case 'button': {
23457 $w = $this->GetStringWidth($value) * 1.5;
23458 $h *= 1.6;
23459 $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
23460 if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
23461 $jsaction = $tag['attribute']['onclick'];
23462 } else {
23463 $jsaction = '';
23465 $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
23466 break;
23469 break;
23471 case 'textarea': {
23472 $prop = array();
23473 $opt = array();
23474 if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
23475 $prop['readonly'] = true;
23477 if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
23478 $name = $tag['attribute']['name'];
23479 } else {
23480 break;
23482 if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
23483 $opt['v'] = $tag['attribute']['value'];
23485 if (isset($tag['attribute']['cols']) AND !$this->empty_string($tag['attribute']['cols'])) {
23486 $w = intval($tag['attribute']['cols']) * $this->GetStringWidth(chr(32)) * 2;
23487 } else {
23488 $w = 40;
23490 if (isset($tag['attribute']['rows']) AND !$this->empty_string($tag['attribute']['rows'])) {
23491 $h = intval($tag['attribute']['rows']) * $this->FontSize * $this->cell_height_ratio;
23492 } else {
23493 $h = 10;
23495 $prop['multiline'] = 'true';
23496 $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
23497 break;
23499 case 'select': {
23500 $h = $this->FontSize * $this->cell_height_ratio;
23501 if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
23502 $h *= ($tag['attribute']['size'] + 1);
23504 $prop = array();
23505 $opt = array();
23506 if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
23507 $name = $tag['attribute']['name'];
23508 } else {
23509 break;
23511 $w = 0;
23512 if (isset($tag['attribute']['opt']) AND !$this->empty_string($tag['attribute']['opt'])) {
23513 $options = explode('#!NwL!#', $tag['attribute']['opt']);
23514 $values = array();
23515 foreach ($options as $val) {
23516 if (strpos($val, '#!TaB!#') !== false) {
23517 $opts = explode('#!TaB!#', $val);
23518 $values[] = $opts;
23519 $w = max($w, $this->GetStringWidth($opts[1]));
23520 } else {
23521 $values[] = $val;
23522 $w = max($w, $this->GetStringWidth($val));
23525 } else {
23526 break;
23528 $w *= 2;
23529 if (isset($tag['attribute']['multiple']) AND ($tag['attribute']['multiple']='multiple')) {
23530 $prop['multipleSelection'] = 'true';
23531 $this->ListBox($name, $w, $h, $values, $prop, $opt, '', '', false);
23532 } else {
23533 $this->ComboBox($name, $w, $h, $values, $prop, $opt, '', '', false);
23535 break;
23537 case 'tcpdf': {
23538 if (defined('K_TCPDF_CALLS_IN_HTML') AND (K_TCPDF_CALLS_IN_HTML === true)) {
23539 // Special tag used to call TCPDF methods
23540 if (isset($tag['attribute']['method'])) {
23541 $tcpdf_method = $tag['attribute']['method'];
23542 if (method_exists($this, $tcpdf_method)) {
23543 if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
23544 $params = unserialize(urldecode($tag['attribute']['params']));
23545 call_user_func_array(array($this, $tcpdf_method), $params);
23546 } else {
23547 $this->$tcpdf_method();
23549 $this->newline = true;
23553 break;
23555 default: {
23556 break;
23559 // define tags that support borders and background colors
23560 $bordertags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table');
23561 if (in_array($tag['value'], $bordertags)) {
23562 // set border
23563 $dom[$key]['borderposition'] = $this->getBorderStartPosition();
23565 if ($dom[$key]['self'] AND isset($dom[$key]['attribute']['pagebreakafter'])) {
23566 $pba = $dom[$key]['attribute']['pagebreakafter'];
23567 // check for pagebreak
23568 if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
23569 // add a page (or trig AcceptPageBreak() for multicolumn mode)
23570 $this->checkPageBreak($this->PageBreakTrigger + 1);
23572 if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
23573 OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
23574 // add a page (or trig AcceptPageBreak() for multicolumn mode)
23575 $this->checkPageBreak($this->PageBreakTrigger + 1);
23578 return $dom;
23582 * Process closing tags.
23583 * @param $dom (array) html dom array
23584 * @param $key (int) current element id
23585 * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
23586 * @param $maxbottomliney (int) maximum y value of current line
23587 * @return $dom array
23588 * @protected
23590 protected function closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney=0) {
23591 $tag = $dom[$key];
23592 $parent = $dom[($dom[$key]['parent'])];
23593 $lasttag = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
23594 $in_table_head = false;
23595 // maximum x position (used to draw borders)
23596 if ($this->rtl) {
23597 $xmax = $this->w;
23598 } else {
23599 $xmax = 0;
23601 if ($tag['block']) {
23602 $hbz = 0; // distance from y to line bottom
23603 $hb = 0; // vertical space between block tags
23604 // calculate vertical space for block tags
23605 if (isset($this->tagvspaces[$tag['value']][1]['h']) AND ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
23606 $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
23607 } elseif (isset($parent['fontsize'])) {
23608 $pre_h = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
23609 } else {
23610 $pre_h = $this->FontSize * $this->cell_height_ratio;
23612 if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
23613 $n = $this->tagvspaces[$tag['value']][1]['n'];
23614 } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
23615 $n = 0.6;
23616 } else {
23617 $n = 1;
23619 if ((!isset($this->tagvspaces[$tag['value']])) AND ($tag['value'] == 'div')) {
23620 $hb = 0;
23621 } else {
23622 $hb = ($n * $pre_h);
23624 if ($maxbottomliney > $this->PageBreakTrigger) {
23625 $hbz = ($this->FontSize * $this->cell_height_ratio);
23626 } elseif ($this->y < $maxbottomliney) {
23627 $hbz = ($maxbottomliney - $this->y);
23630 // Closing tag
23631 switch($tag['value']) {
23632 case 'tr': {
23633 $table_el = $dom[($dom[$key]['parent'])]['parent'];
23634 if (!isset($parent['endy'])) {
23635 $dom[($dom[$key]['parent'])]['endy'] = $this->y;
23636 $parent['endy'] = $this->y;
23638 if (!isset($parent['endpage'])) {
23639 $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
23640 $parent['endpage'] = $this->page;
23642 if (!isset($parent['endcolumn'])) {
23643 $dom[($dom[$key]['parent'])]['endcolumn'] = $this->current_column;
23644 $parent['endcolumn'] = $this->current_column;
23646 // update row-spanned cells
23647 if (isset($dom[$table_el]['rowspans'])) {
23648 foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
23649 $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
23650 if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
23651 if (($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) AND ($dom[$table_el]['rowspans'][$k]['endcolumn'] == $parent['endcolumn'])) {
23652 $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
23653 } elseif (($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) OR ($dom[$table_el]['rowspans'][$k]['endcolumn'] > $parent['endcolumn'])) {
23654 $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
23655 $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
23656 $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
23660 // report new endy and endpage to the rowspanned cells
23661 foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
23662 if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
23663 $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
23664 $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
23665 $dom[$table_el]['rowspans'][$k]['endcolumn'] = max($dom[$table_el]['rowspans'][$k]['endcolumn'], $dom[($dom[$key]['parent'])]['endcolumn']);
23666 $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
23667 $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
23668 $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
23671 // update remaining rowspanned cells
23672 foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
23673 if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
23674 $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
23675 $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[($dom[$key]['parent'])]['endcolumn'];
23676 $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
23680 $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
23681 if ($this->num_columns > 1) {
23682 $this->selectColumn($dom[($dom[$key]['parent'])]['endcolumn']);
23684 $this->y = $dom[($dom[$key]['parent'])]['endy'];
23685 if (isset($dom[$table_el]['attribute']['cellspacing'])) {
23686 $this->y += $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
23687 } elseif (isset($dom[$table_el]['border-spacing'])) {
23688 $this->y += $dom[$table_el]['border-spacing']['V'];
23690 $this->Ln(0, $cell);
23691 if ($this->current_column == $parent['startcolumn']) {
23692 $this->x = $parent['startx'];
23694 // account for booklet mode
23695 if ($this->page > $parent['startpage']) {
23696 if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
23697 $this->x -= ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
23698 } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
23699 $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
23702 break;
23704 case 'tablehead':
23705 // closing tag used for the thead part
23706 $in_table_head = true;
23707 $this->inthead = false;
23708 case 'table': {
23709 $table_el = $parent;
23710 // set default border
23711 if (isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0)) {
23712 // set default border
23713 $border = array('LTRB' => array('width' => $this->getCSSBorderWidth($table_el['attribute']['border']), 'cap'=>'square', 'join'=>'miter', 'dash'=> 0, 'color'=>array(0,0,0)));
23714 } else {
23715 $border = 0;
23717 $default_border = $border;
23718 // fix bottom line alignment of last line before page break
23719 foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
23720 // update row-spanned cells
23721 if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
23722 foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
23723 if ($trwsp['trid'] == $trkey) {
23724 $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
23726 if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0)) {
23727 $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
23731 if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
23732 $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
23733 $dom[$prevtrkey]['endy'] = $pgendy;
23734 // update row-spanned cells
23735 if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
23736 foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
23737 if (($trwsp['trid'] == $trkey) AND ($trwsp['mrowspan'] > 1) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
23738 $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
23739 $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
23744 $prevtrkey = $trkey;
23745 $table_el = $dom[($dom[$key]['parent'])];
23747 // for each row
23748 if (count($table_el['trids']) > 0) {
23749 unset($xmax);
23751 foreach ($table_el['trids'] as $j => $trkey) {
23752 $parent = $dom[$trkey];
23753 if (!isset($xmax)) {
23754 $xmax = $parent['cellpos'][(count($parent['cellpos']) - 1)]['endx'];
23756 // for each cell on the row
23757 foreach ($parent['cellpos'] as $k => $cellpos) {
23758 if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
23759 $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
23760 $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
23761 $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
23762 $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
23763 $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
23764 $startcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['startcolumn'];
23765 $endcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['endcolumn'];
23766 } else {
23767 $endy = $parent['endy'];
23768 $startpage = $parent['startpage'];
23769 $endpage = $parent['endpage'];
23770 $startcolumn = $parent['startcolumn'];
23771 $endcolumn = $parent['endcolumn'];
23773 if ($this->num_columns == 0) {
23774 $this->num_columns = 1;
23776 if (isset($cellpos['border'])) {
23777 $border = $cellpos['border'];
23779 if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
23780 $this->SetFillColorArray($cellpos['bgcolor']);
23781 $fill = true;
23782 } else {
23783 $fill = false;
23785 $x = $cellpos['startx'];
23786 $y = $parent['starty'];
23787 $starty = $y;
23788 $w = abs($cellpos['endx'] - $cellpos['startx']);
23789 // get border modes
23790 $border_start = $this->getBorderMode($border, $position='start');
23791 $border_end = $this->getBorderMode($border, $position='end');
23792 $border_middle = $this->getBorderMode($border, $position='middle');
23793 // design borders around HTML cells.
23794 for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
23795 $ccode = '';
23796 $this->setPage($page);
23797 if ($this->num_columns < 2) {
23798 // single-column mode
23799 $this->x = $x;
23800 $this->y = $this->tMargin;
23802 // account for margin changes
23803 if ($page > $startpage) {
23804 if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
23805 $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
23806 } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
23807 $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
23810 if ($startpage == $endpage) { // single page
23811 $deltacol = 0;
23812 $deltath = 0;
23813 for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
23814 $this->selectColumn($column);
23815 if ($startcolumn == $endcolumn) { // single column
23816 $cborder = $border;
23817 $h = $endy - $parent['starty'];
23818 $this->y = $y;
23819 $this->x = $x;
23820 } elseif ($column == $startcolumn) { // first column
23821 $cborder = $border_start;
23822 $this->y = $starty;
23823 $this->x = $x;
23824 $h = $this->h - $this->y - $this->bMargin;
23825 if ($this->rtl) {
23826 $deltacol = $this->x + $this->rMargin - $this->w;
23827 } else {
23828 $deltacol = $this->x - $this->lMargin;
23830 } elseif ($column == $endcolumn) { // end column
23831 $cborder = $border_end;
23832 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23833 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23835 $this->x += $deltacol;
23836 $h = $endy - $this->y;
23837 } else { // middle column
23838 $cborder = $border_middle;
23839 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23840 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23842 $this->x += $deltacol;
23843 $h = $this->h - $this->y - $this->bMargin;
23845 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
23846 } // end for each column
23847 } elseif ($page == $startpage) { // first page
23848 $deltacol = 0;
23849 $deltath = 0;
23850 for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
23851 $this->selectColumn($column);
23852 if ($column == $startcolumn) { // first column
23853 $cborder = $border_start;
23854 $this->y = $starty;
23855 $this->x = $x;
23856 $h = $this->h - $this->y - $this->bMargin;
23857 if ($this->rtl) {
23858 $deltacol = $this->x + $this->rMargin - $this->w;
23859 } else {
23860 $deltacol = $this->x - $this->lMargin;
23862 } else { // middle column
23863 $cborder = $border_middle;
23864 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23865 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23867 $this->x += $deltacol;
23868 $h = $this->h - $this->y - $this->bMargin;
23870 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
23871 } // end for each column
23872 } elseif ($page == $endpage) { // last page
23873 $deltacol = 0;
23874 $deltath = 0;
23875 for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
23876 $this->selectColumn($column);
23877 if ($column == $endcolumn) { // end column
23878 $cborder = $border_end;
23879 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23880 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23882 $this->x += $deltacol;
23883 $h = $endy - $this->y;
23884 } else { // middle column
23885 $cborder = $border_middle;
23886 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23887 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23889 $this->x += $deltacol;
23890 $h = $this->h - $this->y - $this->bMargin;
23892 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
23893 } // end for each column
23894 } else { // middle page
23895 $deltacol = 0;
23896 $deltath = 0;
23897 for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
23898 $this->selectColumn($column);
23899 $cborder = $border_middle;
23900 if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
23901 $this->y = $this->columns[$column]['th']['\''.$page.'\''];
23903 $this->x += $deltacol;
23904 $h = $this->h - $this->y - $this->bMargin;
23905 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
23906 } // end for each column
23908 if ($cborder OR $fill) {
23909 $offsetlen = strlen($ccode);
23910 // draw border and fill
23911 if ($this->inxobj) {
23912 // we are inside an XObject template
23913 if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
23914 $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
23915 $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
23916 $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
23917 } else {
23918 $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
23919 $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
23921 $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
23922 $pstart = substr($pagebuff, 0, $pagemark);
23923 $pend = substr($pagebuff, $pagemark);
23924 $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
23925 } else {
23926 // draw border and fill
23927 if (end($this->transfmrk[$this->page]) !== false) {
23928 $pagemarkkey = key($this->transfmrk[$this->page]);
23929 $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
23930 $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
23931 } elseif ($this->InFooter) {
23932 $pagemark = $this->footerpos[$this->page];
23933 $this->footerpos[$this->page] += $offsetlen;
23934 } else {
23935 $pagemark = $this->intmrk[$this->page];
23936 $this->intmrk[$this->page] += $offsetlen;
23938 $pagebuff = $this->getPageBuffer($this->page);
23939 $pstart = substr($pagebuff, 0, $pagemark);
23940 $pend = substr($pagebuff, $pagemark);
23941 $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
23944 } // end for each page
23945 // restore default border
23946 $border = $default_border;
23947 } // end for each cell on the row
23948 if (isset($table_el['attribute']['cellspacing'])) {
23949 $this->y += $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
23950 } elseif (isset($table_el['border-spacing'])) {
23951 $this->y += $table_el['border-spacing']['V'];
23953 $this->Ln(0, $cell);
23954 $this->x = $parent['startx'];
23955 if ($endpage > $startpage) {
23956 if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
23957 $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
23958 } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
23959 $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
23963 if (!$in_table_head) { // we are not inside a thead section
23964 $this->cell_padding = $table_el['old_cell_padding'];
23965 // reset row height
23966 $this->resetLastH();
23967 if (($this->page == ($this->numpages - 1)) AND ($this->pageopen[$this->numpages])) {
23968 $plendiff = ($this->pagelen[$this->numpages] - $this->emptypagemrk[$this->numpages]);
23969 if (($plendiff > 0) AND ($plendiff < 60)) {
23970 $pagediff = substr($this->getPageBuffer($this->numpages), $this->emptypagemrk[$this->numpages], $plendiff);
23971 if (substr($pagediff, 0, 5) == 'BT /F') {
23972 // the difference is only a font setting
23973 $plendiff = 0;
23976 if ($plendiff == 0) {
23977 // remove last blank page
23978 $this->deletePage($this->numpages);
23981 if (isset($this->theadMargins['top'])) {
23982 // restore top margin
23983 $this->tMargin = $this->theadMargins['top'];
23985 if (!isset($table_el['attribute']['nested']) OR ($table_el['attribute']['nested'] != 'true')) {
23986 // reset main table header
23987 $this->thead = '';
23988 $this->theadMargins = array();
23989 $this->pagedim[$this->page]['tm'] = $this->tMargin;
23992 $parent = $table_el;
23993 break;
23995 case 'a': {
23996 $this->HREF = '';
23997 break;
23999 case 'sup': {
24000 $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
24001 break;
24003 case 'sub': {
24004 $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize'])/$this->k));
24005 break;
24007 case 'div': {
24008 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24009 break;
24011 case 'blockquote': {
24012 if ($this->rtl) {
24013 $this->rMargin -= $this->listindent;
24014 } else {
24015 $this->lMargin -= $this->listindent;
24017 --$this->listindentlevel;
24018 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24019 break;
24021 case 'p': {
24022 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24023 break;
24025 case 'pre': {
24026 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24027 $this->premode = false;
24028 break;
24030 case 'dl': {
24031 --$this->listnum;
24032 if ($this->listnum <= 0) {
24033 $this->listnum = 0;
24034 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24035 } else {
24036 $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
24038 $this->resetLastH();
24039 break;
24041 case 'dt': {
24042 $this->lispacer = '';
24043 $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
24044 break;
24046 case 'dd': {
24047 $this->lispacer = '';
24048 if ($this->rtl) {
24049 $this->rMargin -= $this->listindent;
24050 } else {
24051 $this->lMargin -= $this->listindent;
24053 --$this->listindentlevel;
24054 $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
24055 break;
24057 case 'ul':
24058 case 'ol': {
24059 --$this->listnum;
24060 $this->lispacer = '';
24061 if ($this->rtl) {
24062 $this->rMargin -= $this->listindent;
24063 } else {
24064 $this->lMargin -= $this->listindent;
24066 --$this->listindentlevel;
24067 if ($this->listnum <= 0) {
24068 $this->listnum = 0;
24069 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24070 } else {
24071 $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
24073 $this->resetLastH();
24074 break;
24076 case 'li': {
24077 $this->lispacer = '';
24078 $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
24079 break;
24081 case 'h1':
24082 case 'h2':
24083 case 'h3':
24084 case 'h4':
24085 case 'h5':
24086 case 'h6': {
24087 $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
24088 break;
24090 // Form fields (since 4.8.000 - 2009-09-07)
24091 case 'form': {
24092 $this->form_action = '';
24093 $this->form_enctype = 'application/x-www-form-urlencoded';
24094 break;
24096 default : {
24097 break;
24100 // draw border and background (if any)
24101 $this->drawHTMLTagBorder($parent, $xmax);
24102 if (isset($dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'])) {
24103 $pba = $dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'];
24104 // check for pagebreak
24105 if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
24106 // add a page (or trig AcceptPageBreak() for multicolumn mode)
24107 $this->checkPageBreak($this->PageBreakTrigger + 1);
24109 if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
24110 OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
24111 // add a page (or trig AcceptPageBreak() for multicolumn mode)
24112 $this->checkPageBreak($this->PageBreakTrigger + 1);
24115 $this->tmprtl = false;
24116 return $dom;
24120 * Add vertical spaces if needed.
24121 * @param $hbz (string) Distance between current y and line bottom.
24122 * @param $hb (string) The height of the break.
24123 * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
24124 * @param $firsttag (boolean) set to true when the tag is the first.
24125 * @param $lasttag (boolean) set to true when the tag is the last.
24126 * @protected
24128 protected function addHTMLVertSpace($hbz=0, $hb=0, $cell=false, $firsttag=false, $lasttag=false) {
24129 if ($firsttag) {
24130 $this->Ln(0, $cell);
24131 $this->htmlvspace = 0;
24132 return;
24134 if ($lasttag) {
24135 $this->Ln($hbz, $cell);
24136 $this->htmlvspace = 0;
24137 return;
24139 if ($hb < $this->htmlvspace) {
24140 $hd = 0;
24141 } else {
24142 $hd = $hb - $this->htmlvspace;
24143 $this->htmlvspace = $hb;
24145 $this->Ln(($hbz + $hd), $cell);
24149 * Return the starting coordinates to draw an html border
24150 * @return array containing top-left border coordinates
24151 * @protected
24152 * @since 5.7.000 (2010-08-03)
24154 protected function getBorderStartPosition() {
24155 if ($this->rtl) {
24156 $xmax = $this->lMargin;
24157 } else {
24158 $xmax = $this->w - $this->rMargin;
24160 return array('page' => $this->page, 'column' => $this->current_column, 'x' => $this->x, 'y' => $this->y, 'xmax' => $xmax);
24164 * Draw an HTML block border and fill
24165 * @param $tag (array) array of tag properties.
24166 * @param $xmax (int) end X coordinate for border.
24167 * @protected
24168 * @since 5.7.000 (2010-08-03)
24170 protected function drawHTMLTagBorder($tag, $xmax) {
24171 if (!isset($tag['borderposition'])) {
24172 // nothing to draw
24173 return;
24175 $prev_x = $this->x;
24176 $prev_y = $this->y;
24177 $prev_lasth = $this->lasth;
24178 $border = 0;
24179 $fill = false;
24180 $this->lasth = 0;
24181 if (isset($tag['border']) AND !empty($tag['border'])) {
24182 // get border style
24183 $border = $tag['border'];
24184 if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
24185 // border for table header
24186 $border = $this->getBorderMode($border, $position='middle');
24189 if (isset($tag['bgcolor']) AND ($tag['bgcolor'] !== false)) {
24190 // get background color
24191 $old_bgcolor = $this->bgcolor;
24192 $this->SetFillColorArray($tag['bgcolor']);
24193 $fill = true;
24195 if (!$border AND !$fill) {
24196 // nothing to draw
24197 return;
24199 if (isset($tag['attribute']['cellspacing'])) {
24200 $clsp = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
24201 $cellspacing = array('H' => $clsp, 'V' => $clsp);
24202 } elseif (isset($tag['border-spacing'])) {
24203 $cellspacing = $tag['border-spacing'];
24204 } else {
24205 $cellspacing = array('H' => 0, 'V' => 0);
24207 if (($tag['value'] != 'table') AND (is_array($border)) AND (!empty($border))) {
24208 // draw the border externally respect the sqare edge.
24209 $border['mode'] = 'ext';
24211 if ($this->rtl) {
24212 if ($xmax >= $tag['borderposition']['x']) {
24213 $xmax = $tag['borderposition']['xmax'];
24215 $w = ($tag['borderposition']['x'] - $xmax);
24216 } else {
24217 if ($xmax <= $tag['borderposition']['x']) {
24218 $xmax = $tag['borderposition']['xmax'];
24220 $w = ($xmax - $tag['borderposition']['x']);
24222 if ($w <= 0) {
24223 return;
24225 $w += $cellspacing['H'];
24226 $startpage = $tag['borderposition']['page'];
24227 $startcolumn = $tag['borderposition']['column'];
24228 $x = $tag['borderposition']['x'];
24229 $y = $tag['borderposition']['y'];
24230 $endpage = $this->page;
24231 $starty = $tag['borderposition']['y'] - $cellspacing['V'];
24232 $currentY = $this->y;
24233 $this->x = $x;
24234 // get latest column
24235 $endcolumn = $this->current_column;
24236 if ($this->num_columns == 0) {
24237 $this->num_columns = 1;
24239 // get border modes
24240 $border_start = $this->getBorderMode($border, $position='start');
24241 $border_end = $this->getBorderMode($border, $position='end');
24242 $border_middle = $this->getBorderMode($border, $position='middle');
24243 // temporary disable page regions
24244 $temp_page_regions = $this->page_regions;
24245 $this->page_regions = array();
24246 // design borders around HTML cells.
24247 for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
24248 $ccode = '';
24249 $this->setPage($page);
24250 if ($this->num_columns < 2) {
24251 // single-column mode
24252 $this->x = $x;
24253 $this->y = $this->tMargin;
24255 // account for margin changes
24256 if ($page > $startpage) {
24257 if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
24258 $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
24259 } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
24260 $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
24263 if ($startpage == $endpage) {
24264 // single page
24265 for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
24266 $this->selectColumn($column);
24267 if ($startcolumn == $endcolumn) { // single column
24268 $cborder = $border;
24269 $h = ($currentY - $y) + $cellspacing['V'];
24270 $this->y = $starty;
24271 } elseif ($column == $startcolumn) { // first column
24272 $cborder = $border_start;
24273 $this->y = $starty;
24274 $h = $this->h - $this->y - $this->bMargin;
24275 } elseif ($column == $endcolumn) { // end column
24276 $cborder = $border_end;
24277 $h = $currentY - $this->y;
24278 } else { // middle column
24279 $cborder = $border_middle;
24280 $h = $this->h - $this->y - $this->bMargin;
24282 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
24283 } // end for each column
24284 } elseif ($page == $startpage) { // first page
24285 for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
24286 $this->selectColumn($column);
24287 if ($column == $startcolumn) { // first column
24288 $cborder = $border_start;
24289 $this->y = $starty;
24290 $h = $this->h - $this->y - $this->bMargin;
24291 } else { // middle column
24292 $cborder = $border_middle;
24293 $h = $this->h - $this->y - $this->bMargin;
24295 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
24296 } // end for each column
24297 } elseif ($page == $endpage) { // last page
24298 for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
24299 $this->selectColumn($column);
24300 if ($column == $endcolumn) {
24301 // end column
24302 $cborder = $border_end;
24303 $h = $currentY - $this->y;
24304 } else {
24305 // middle column
24306 $cborder = $border_middle;
24307 $h = $this->h - $this->y - $this->bMargin;
24309 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
24310 } // end for each column
24311 } else { // middle page
24312 for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
24313 $this->selectColumn($column);
24314 $cborder = $border_middle;
24315 $h = $this->h - $this->y - $this->bMargin;
24316 $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
24317 } // end for each column
24319 if ($cborder OR $fill) {
24320 $offsetlen = strlen($ccode);
24321 // draw border and fill
24322 if ($this->inxobj) {
24323 // we are inside an XObject template
24324 if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
24325 $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
24326 $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
24327 $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
24328 } else {
24329 $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
24330 $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
24332 $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
24333 $pstart = substr($pagebuff, 0, $pagemark);
24334 $pend = substr($pagebuff, $pagemark);
24335 $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
24336 } else {
24337 if (end($this->transfmrk[$this->page]) !== false) {
24338 $pagemarkkey = key($this->transfmrk[$this->page]);
24339 $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
24340 $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
24341 } elseif ($this->InFooter) {
24342 $pagemark = $this->footerpos[$this->page];
24343 $this->footerpos[$this->page] += $offsetlen;
24344 } else {
24345 $pagemark = $this->intmrk[$this->page];
24346 $this->intmrk[$this->page] += $offsetlen;
24348 $pagebuff = $this->getPageBuffer($this->page);
24349 $pstart = substr($pagebuff, 0, $this->bordermrk[$this->page]);
24350 $pend = substr($pagebuff, $this->bordermrk[$this->page]);
24351 $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
24352 $this->bordermrk[$this->page] += $offsetlen;
24353 $this->cntmrk[$this->page] += $offsetlen;
24356 } // end for each page
24357 // restore page regions
24358 $this->page_regions = $temp_page_regions;
24359 if (isset($old_bgcolor)) {
24360 // restore background color
24361 $this->SetFillColorArray($old_bgcolor);
24363 // restore pointer position
24364 $this->x = $prev_x;
24365 $this->y = $prev_y;
24366 $this->lasth = $prev_lasth;
24370 * Set the default bullet to be used as LI bullet symbol
24371 * @param $symbol (string) character or string to be used (legal values are: '' = automatic, '!' = auto bullet, '#' = auto numbering, 'disc', 'disc', 'circle', 'square', '1', 'decimal', 'decimal-leading-zero', 'i', 'lower-roman', 'I', 'upper-roman', 'a', 'lower-alpha', 'lower-latin', 'A', 'upper-alpha', 'upper-latin', 'lower-greek', 'img|type|width|height|image.ext')
24372 * @public
24373 * @since 4.0.028 (2008-09-26)
24375 public function setLIsymbol($symbol='!') {
24376 // check for custom image symbol
24377 if (substr($symbol, 0, 4) == 'img|') {
24378 $this->lisymbol = $symbol;
24379 return;
24381 $symbol = strtolower($symbol);
24382 switch ($symbol) {
24383 case '!' :
24384 case '#' :
24385 case 'disc' :
24386 case 'circle' :
24387 case 'square' :
24388 case '1':
24389 case 'decimal':
24390 case 'decimal-leading-zero':
24391 case 'i':
24392 case 'lower-roman':
24393 case 'I':
24394 case 'upper-roman':
24395 case 'a':
24396 case 'lower-alpha':
24397 case 'lower-latin':
24398 case 'A':
24399 case 'upper-alpha':
24400 case 'upper-latin':
24401 case 'lower-greek': {
24402 $this->lisymbol = $symbol;
24403 break;
24405 default : {
24406 $this->lisymbol = '';
24412 * Set the booklet mode for double-sided pages.
24413 * @param $booklet (boolean) true set the booklet mode on, false otherwise.
24414 * @param $inner (float) Inner page margin.
24415 * @param $outer (float) Outer page margin.
24416 * @public
24417 * @since 4.2.000 (2008-10-29)
24419 public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
24420 $this->booklet = $booklet;
24421 if ($inner >= 0) {
24422 $this->lMargin = $inner;
24424 if ($outer >= 0) {
24425 $this->rMargin = $outer;
24430 * Swap the left and right margins.
24431 * @param $reverse (boolean) if true swap left and right margins.
24432 * @protected
24433 * @since 4.2.000 (2008-10-29)
24435 protected function swapMargins($reverse=true) {
24436 if ($reverse) {
24437 // swap left and right margins
24438 $mtemp = $this->original_lMargin;
24439 $this->original_lMargin = $this->original_rMargin;
24440 $this->original_rMargin = $mtemp;
24441 $deltam = $this->original_lMargin - $this->original_rMargin;
24442 $this->lMargin += $deltam;
24443 $this->rMargin -= $deltam;
24448 * Set the vertical spaces for HTML tags.
24449 * The array must have the following structure (example):
24450 * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
24451 * The first array level contains the tag names,
24452 * the second level contains 0 for opening tags or 1 for closing tags,
24453 * the third level contains the vertical space unit (h) and the number spaces to add (n).
24454 * If the h parameter is not specified, default values are used.
24455 * @param $tagvs (array) array of tags and relative vertical spaces.
24456 * @public
24457 * @since 4.2.001 (2008-10-30)
24459 public function setHtmlVSpace($tagvs) {
24460 $this->tagvspaces = $tagvs;
24464 * Set custom width for list indentation.
24465 * @param $width (float) width of the indentation. Use negative value to disable it.
24466 * @public
24467 * @since 4.2.007 (2008-11-12)
24469 public function setListIndentWidth($width) {
24470 return $this->customlistindent = floatval($width);
24474 * Set the top/bottom cell sides to be open or closed when the cell cross the page.
24475 * @param $isopen (boolean) if true keeps the top/bottom border open for the cell sides that cross the page.
24476 * @public
24477 * @since 4.2.010 (2008-11-14)
24479 public function setOpenCell($isopen) {
24480 $this->opencell = $isopen;
24484 * Set the color and font style for HTML links.
24485 * @param $color (array) RGB array of colors
24486 * @param $fontstyle (string) additional font styles to add
24487 * @public
24488 * @since 4.4.003 (2008-12-09)
24490 public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
24491 $this->htmlLinkColorArray = $color;
24492 $this->htmlLinkFontStyle = $fontstyle;
24496 * Convert HTML string containing value and unit of measure to user's units or points.
24497 * @param $htmlval (string) string containing values and unit
24498 * @param $refsize (string) reference value in points
24499 * @param $defaultunit (string) default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
24500 * @param $points (boolean) if true returns points, otherwise returns value in user's units
24501 * @return float value in user's unit or point if $points=true
24502 * @public
24503 * @since 4.4.004 (2008-12-10)
24505 public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
24506 $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
24507 $retval = 0;
24508 $value = 0;
24509 $unit = 'px';
24510 $k = $this->k;
24511 if ($points) {
24512 $k = 1;
24514 if (in_array($defaultunit, $supportedunits)) {
24515 $unit = $defaultunit;
24517 if (is_numeric($htmlval)) {
24518 $value = floatval($htmlval);
24519 } elseif (preg_match('/([0-9\.\-\+]+)/', $htmlval, $mnum)) {
24520 $value = floatval($mnum[1]);
24521 if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
24522 if (in_array($munit[1], $supportedunits)) {
24523 $unit = $munit[1];
24527 switch ($unit) {
24528 // percentage
24529 case '%': {
24530 $retval = (($value * $refsize) / 100);
24531 break;
24533 // relative-size
24534 case 'em': {
24535 $retval = ($value * $refsize);
24536 break;
24538 // height of lower case 'x' (about half the font-size)
24539 case 'ex': {
24540 $retval = $value * ($refsize / 2);
24541 break;
24543 // absolute-size
24544 case 'in': {
24545 $retval = ($value * $this->dpi) / $k;
24546 break;
24548 // centimeters
24549 case 'cm': {
24550 $retval = ($value / 2.54 * $this->dpi) / $k;
24551 break;
24553 // millimeters
24554 case 'mm': {
24555 $retval = ($value / 25.4 * $this->dpi) / $k;
24556 break;
24558 // one pica is 12 points
24559 case 'pc': {
24560 $retval = ($value * 12) / $k;
24561 break;
24563 // points
24564 case 'pt': {
24565 $retval = $value / $k;
24566 break;
24568 // pixels
24569 case 'px': {
24570 $retval = $this->pixelsToUnits($value);
24571 break;
24574 return $retval;
24578 * Returns the Roman representation of an integer number
24579 * @param $number (int) number to convert
24580 * @return string roman representation of the specified number
24581 * @since 4.4.004 (2008-12-10)
24582 * @public
24584 public function intToRoman($number) {
24585 $roman = '';
24586 while ($number >= 1000) {
24587 $roman .= 'M';
24588 $number -= 1000;
24590 while ($number >= 900) {
24591 $roman .= 'CM';
24592 $number -= 900;
24594 while ($number >= 500) {
24595 $roman .= 'D';
24596 $number -= 500;
24598 while ($number >= 400) {
24599 $roman .= 'CD';
24600 $number -= 400;
24602 while ($number >= 100) {
24603 $roman .= 'C';
24604 $number -= 100;
24606 while ($number >= 90) {
24607 $roman .= 'XC';
24608 $number -= 90;
24610 while ($number >= 50) {
24611 $roman .= 'L';
24612 $number -= 50;
24614 while ($number >= 40) {
24615 $roman .= 'XL';
24616 $number -= 40;
24618 while ($number >= 10) {
24619 $roman .= 'X';
24620 $number -= 10;
24622 while ($number >= 9) {
24623 $roman .= 'IX';
24624 $number -= 9;
24626 while ($number >= 5) {
24627 $roman .= 'V';
24628 $number -= 5;
24630 while ($number >= 4) {
24631 $roman .= 'IV';
24632 $number -= 4;
24634 while ($number >= 1) {
24635 $roman .= 'I';
24636 --$number;
24638 return $roman;
24642 * Output an HTML list bullet or ordered item symbol
24643 * @param $listdepth (int) list nesting level
24644 * @param $listtype (string) type of list
24645 * @param $size (float) current font size
24646 * @protected
24647 * @since 4.4.004 (2008-12-10)
24649 protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
24650 $size /= $this->k;
24651 $fill = '';
24652 $bgcolor = $this->bgcolor;
24653 $color = $this->fgcolor;
24654 $strokecolor = $this->strokecolor;
24655 $width = 0;
24656 $textitem = '';
24657 $tmpx = $this->x;
24658 $lspace = $this->GetStringWidth(' ');
24659 if ($listtype == '^') {
24660 // special symbol used for avoid justification of rect bullet
24661 $this->lispacer = '';
24662 return;
24663 } elseif ($listtype == '!') {
24664 // set default list type for unordered list
24665 $deftypes = array('disc', 'circle', 'square');
24666 $listtype = $deftypes[($listdepth - 1) % 3];
24667 } elseif ($listtype == '#') {
24668 // set default list type for ordered list
24669 $listtype = 'decimal';
24670 } elseif (substr($listtype, 0, 4) == 'img|') {
24671 // custom image type ('img|type|width|height|image.ext')
24672 $img = explode('|', $listtype);
24673 $listtype = 'img';
24675 switch ($listtype) {
24676 // unordered types
24677 case 'none': {
24678 break;
24680 case 'disc': {
24681 $r = $size / 6;
24682 $lspace += (2 * $r);
24683 if ($this->rtl) {
24684 $this->x += $lspace;
24685 } else {
24686 $this->x -= $lspace;
24688 $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, 'F', array(), $color, 8);
24689 break;
24691 case 'circle': {
24692 $r = $size / 6;
24693 $lspace += (2 * $r);
24694 if ($this->rtl) {
24695 $this->x += $lspace;
24696 } else {
24697 $this->x -= $lspace;
24699 $prev_line_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor;
24700 $new_line_style = array('width' => ($r / 3), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color'=>$color);
24701 $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), ($r * (1 - (1/6))), 0, 360, 'D', $new_line_style, array(), 8);
24702 $this->_out($prev_line_style); // restore line settings
24703 break;
24705 case 'square': {
24706 $l = $size / 3;
24707 $lspace += $l;
24708 if ($this->rtl) {;
24709 $this->x += $lspace;
24710 } else {
24711 $this->x -= $lspace;
24713 $this->Rect($this->x, ($this->y + (($this->lasth - $l) / 2)), $l, $l, 'F', array(), $color);
24714 break;
24716 case 'img': {
24717 // 1=>type, 2=>width, 3=>height, 4=>image.ext
24718 $lspace += $img[2];
24719 if ($this->rtl) {;
24720 $this->x += $lspace;
24721 } else {
24722 $this->x -= $lspace;
24724 $imgtype = strtolower($img[1]);
24725 $prev_y = $this->y;
24726 switch ($imgtype) {
24727 case 'svg': {
24728 $this->ImageSVG($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', 'T', '', 0, false);
24729 break;
24731 case 'ai':
24732 case 'eps': {
24733 $this->ImageEps($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', true, 'T', '', 0, false);
24734 break;
24736 default: {
24737 $this->Image($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], $img[1], '', 'T', false, 300, '', false, false, 0, false, false, false);
24738 break;
24741 $this->y = $prev_y;
24742 break;
24744 // ordered types
24745 // $this->listcount[$this->listnum];
24746 // $textitem
24747 case '1':
24748 case 'decimal': {
24749 $textitem = $this->listcount[$this->listnum];
24750 break;
24752 case 'decimal-leading-zero': {
24753 $textitem = sprintf('%02d', $this->listcount[$this->listnum]);
24754 break;
24756 case 'i':
24757 case 'lower-roman': {
24758 $textitem = strtolower($this->intToRoman($this->listcount[$this->listnum]));
24759 break;
24761 case 'I':
24762 case 'upper-roman': {
24763 $textitem = $this->intToRoman($this->listcount[$this->listnum]);
24764 break;
24766 case 'a':
24767 case 'lower-alpha':
24768 case 'lower-latin': {
24769 $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
24770 break;
24772 case 'A':
24773 case 'upper-alpha':
24774 case 'upper-latin': {
24775 $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
24776 break;
24778 case 'lower-greek': {
24779 $textitem = $this->unichr(945 + $this->listcount[$this->listnum] - 1);
24780 break;
24783 // Types to be implemented (special handling)
24784 case 'hebrew': {
24785 break;
24787 case 'armenian': {
24788 break;
24790 case 'georgian': {
24791 break;
24793 case 'cjk-ideographic': {
24794 break;
24796 case 'hiragana': {
24797 break;
24799 case 'katakana': {
24800 break;
24802 case 'hiragana-iroha': {
24803 break;
24805 case 'katakana-iroha': {
24806 break;
24809 default: {
24810 $textitem = $this->listcount[$this->listnum];
24813 if (!$this->empty_string($textitem)) {
24814 // Check whether we need a new page or new column
24815 $prev_y = $this->y;
24816 $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
24817 if ($this->checkPageBreak($h) OR ($this->y < $prev_y)) {
24818 $tmpx = $this->x;
24820 // print ordered item
24821 if ($this->rtl) {
24822 $textitem = '.'.$textitem;
24823 } else {
24824 $textitem = $textitem.'.';
24826 $lspace += $this->GetStringWidth($textitem);
24827 if ($this->rtl) {
24828 $this->x += $lspace;
24829 } else {
24830 $this->x -= $lspace;
24832 $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
24834 $this->x = $tmpx;
24835 $this->lispacer = '^';
24836 // restore colors
24837 $this->SetFillColorArray($bgcolor);
24838 $this->SetDrawColorArray($strokecolor);
24839 $this->SettextColorArray($color);
24843 * Returns current graphic variables as array.
24844 * @return array of graphic variables
24845 * @protected
24846 * @since 4.2.010 (2008-11-14)
24848 protected function getGraphicVars() {
24849 $grapvars = array(
24850 'FontFamily' => $this->FontFamily,
24851 'FontStyle' => $this->FontStyle,
24852 'FontSizePt' => $this->FontSizePt,
24853 'rMargin' => $this->rMargin,
24854 'lMargin' => $this->lMargin,
24855 'cell_padding' => $this->cell_padding,
24856 'cell_margin' => $this->cell_margin,
24857 'LineWidth' => $this->LineWidth,
24858 'linestyleWidth' => $this->linestyleWidth,
24859 'linestyleCap' => $this->linestyleCap,
24860 'linestyleJoin' => $this->linestyleJoin,
24861 'linestyleDash' => $this->linestyleDash,
24862 'textrendermode' => $this->textrendermode,
24863 'textstrokewidth' => $this->textstrokewidth,
24864 'DrawColor' => $this->DrawColor,
24865 'FillColor' => $this->FillColor,
24866 'TextColor' => $this->TextColor,
24867 'ColorFlag' => $this->ColorFlag,
24868 'bgcolor' => $this->bgcolor,
24869 'fgcolor' => $this->fgcolor,
24870 'htmlvspace' => $this->htmlvspace,
24871 'listindent' => $this->listindent,
24872 'listindentlevel' => $this->listindentlevel,
24873 'listnum' => $this->listnum,
24874 'listordered' => $this->listordered,
24875 'listcount' => $this->listcount,
24876 'lispacer' => $this->lispacer,
24877 'cell_height_ratio' => $this->cell_height_ratio,
24878 'font_stretching' => $this->font_stretching,
24879 'font_spacing' => $this->font_spacing,
24880 // extended
24881 'lasth' => $this->lasth,
24882 'tMargin' => $this->tMargin,
24883 'bMargin' => $this->bMargin,
24884 'AutoPageBreak' => $this->AutoPageBreak,
24885 'PageBreakTrigger' => $this->PageBreakTrigger,
24886 'x' => $this->x,
24887 'y' => $this->y,
24888 'w' => $this->w,
24889 'h' => $this->h,
24890 'wPt' => $this->wPt,
24891 'hPt' => $this->hPt,
24892 'fwPt' => $this->fwPt,
24893 'fhPt' => $this->fhPt,
24894 'page' => $this->page,
24895 'current_column' => $this->current_column,
24896 'num_columns' => $this->num_columns
24898 return $grapvars;
24902 * Set graphic variables.
24903 * @param $gvars (array) array of graphic variablesto restore
24904 * @param $extended (boolean) if true restore extended graphic variables
24905 * @protected
24906 * @since 4.2.010 (2008-11-14)
24908 protected function setGraphicVars($gvars, $extended=false) {
24909 $this->FontFamily = $gvars['FontFamily'];
24910 $this->FontStyle = $gvars['FontStyle'];
24911 $this->FontSizePt = $gvars['FontSizePt'];
24912 $this->rMargin = $gvars['rMargin'];
24913 $this->lMargin = $gvars['lMargin'];
24914 $this->cell_padding = $gvars['cell_padding'];
24915 $this->cell_margin = $gvars['cell_margin'];
24916 $this->LineWidth = $gvars['LineWidth'];
24917 $this->linestyleWidth = $gvars['linestyleWidth'];
24918 $this->linestyleCap = $gvars['linestyleCap'];
24919 $this->linestyleJoin = $gvars['linestyleJoin'];
24920 $this->linestyleDash = $gvars['linestyleDash'];
24921 $this->textrendermode = $gvars['textrendermode'];
24922 $this->textstrokewidth = $gvars['textstrokewidth'];
24923 $this->DrawColor = $gvars['DrawColor'];
24924 $this->FillColor = $gvars['FillColor'];
24925 $this->TextColor = $gvars['TextColor'];
24926 $this->ColorFlag = $gvars['ColorFlag'];
24927 $this->bgcolor = $gvars['bgcolor'];
24928 $this->fgcolor = $gvars['fgcolor'];
24929 $this->htmlvspace = $gvars['htmlvspace'];
24930 $this->listindent = $gvars['listindent'];
24931 $this->listindentlevel = $gvars['listindentlevel'];
24932 $this->listnum = $gvars['listnum'];
24933 $this->listordered = $gvars['listordered'];
24934 $this->listcount = $gvars['listcount'];
24935 $this->lispacer = $gvars['lispacer'];
24936 $this->cell_height_ratio = $gvars['cell_height_ratio'];
24937 $this->font_stretching = $gvars['font_stretching'];
24938 $this->font_spacing = $gvars['font_spacing'];
24939 if ($extended) {
24940 // restore extended values
24941 $this->lasth = $gvars['lasth'];
24942 $this->tMargin = $gvars['tMargin'];
24943 $this->bMargin = $gvars['bMargin'];
24944 $this->AutoPageBreak = $gvars['AutoPageBreak'];
24945 $this->PageBreakTrigger = $gvars['PageBreakTrigger'];
24946 $this->x = $gvars['x'];
24947 $this->y = $gvars['y'];
24948 $this->w = $gvars['w'];
24949 $this->h = $gvars['h'];
24950 $this->wPt = $gvars['wPt'];
24951 $this->hPt = $gvars['hPt'];
24952 $this->fwPt = $gvars['fwPt'];
24953 $this->fhPt = $gvars['fhPt'];
24954 $this->page = $gvars['page'];
24955 $this->current_column = $gvars['current_column'];
24956 $this->num_columns = $gvars['num_columns'];
24958 $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
24959 if (!$this->empty_string($this->FontFamily)) {
24960 $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
24965 * Returns a temporary filename for caching object on filesystem.
24966 * @param $name (string) prefix to add to filename
24967 * @return string filename.
24968 * @since 4.5.000 (2008-12-31)
24969 * @protected
24971 protected function getObjFilename($name) {
24972 return tempnam(K_PATH_CACHE, $name.'_');
24976 * Writes data to a temporary file on filesystem.
24977 * @param $filename (string) file name
24978 * @param $data (mixed) data to write on file
24979 * @param $append (boolean) if true append data, false replace.
24980 * @since 4.5.000 (2008-12-31)
24981 * @protected
24983 protected function writeDiskCache($filename, $data, $append=false) {
24984 if ($append) {
24985 $fmode = 'ab+';
24986 } else {
24987 $fmode = 'wb+';
24989 $f = @fopen($filename, $fmode);
24990 if (!$f) {
24991 $this->Error('Unable to write cache file: '.$filename);
24992 } else {
24993 fwrite($f, $data);
24994 fclose($f);
24996 // update file length (needed for transactions)
24997 if (!isset($this->cache_file_length['_'.$filename])) {
24998 $this->cache_file_length['_'.$filename] = strlen($data);
24999 } else {
25000 $this->cache_file_length['_'.$filename] += strlen($data);
25005 * Read data from a temporary file on filesystem.
25006 * @param $filename (string) file name
25007 * @return mixed retrieved data
25008 * @since 4.5.000 (2008-12-31)
25009 * @protected
25011 protected function readDiskCache($filename) {
25012 return file_get_contents($filename);
25016 * Set buffer content (always append data).
25017 * @param $data (string) data
25018 * @protected
25019 * @since 4.5.000 (2009-01-02)
25021 protected function setBuffer($data) {
25022 $this->bufferlen += strlen($data);
25023 if ($this->diskcache) {
25024 if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
25025 $this->buffer = $this->getObjFilename('buffer');
25027 $this->writeDiskCache($this->buffer, $data, true);
25028 } else {
25029 $this->buffer .= $data;
25034 * Replace the buffer content
25035 * @param $data (string) data
25036 * @protected
25037 * @since 5.5.000 (2010-06-22)
25039 protected function replaceBuffer($data) {
25040 $this->bufferlen = strlen($data);
25041 if ($this->diskcache) {
25042 if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
25043 $this->buffer = $this->getObjFilename('buffer');
25045 $this->writeDiskCache($this->buffer, $data, false);
25046 } else {
25047 $this->buffer = $data;
25052 * Get buffer content.
25053 * @return string buffer content
25054 * @protected
25055 * @since 4.5.000 (2009-01-02)
25057 protected function getBuffer() {
25058 if ($this->diskcache) {
25059 return $this->readDiskCache($this->buffer);
25060 } else {
25061 return $this->buffer;
25066 * Set page buffer content.
25067 * @param $page (int) page number
25068 * @param $data (string) page data
25069 * @param $append (boolean) if true append data, false replace.
25070 * @protected
25071 * @since 4.5.000 (2008-12-31)
25073 protected function setPageBuffer($page, $data, $append=false) {
25074 if ($this->diskcache) {
25075 if (!isset($this->pages[$page])) {
25076 $this->pages[$page] = $this->getObjFilename('page'.$page);
25078 $this->writeDiskCache($this->pages[$page], $data, $append);
25079 } else {
25080 if ($append) {
25081 $this->pages[$page] .= $data;
25082 } else {
25083 $this->pages[$page] = $data;
25086 if ($append AND isset($this->pagelen[$page])) {
25087 $this->pagelen[$page] += strlen($data);
25088 } else {
25089 $this->pagelen[$page] = strlen($data);
25094 * Get page buffer content.
25095 * @param $page (int) page number
25096 * @return string page buffer content or false in case of error
25097 * @protected
25098 * @since 4.5.000 (2008-12-31)
25100 protected function getPageBuffer($page) {
25101 if ($this->diskcache) {
25102 return $this->readDiskCache($this->pages[$page]);
25103 } elseif (isset($this->pages[$page])) {
25104 return $this->pages[$page];
25106 return false;
25110 * Set image buffer content.
25111 * @param $image (string) image key
25112 * @param $data (array) image data
25113 * @protected
25114 * @since 4.5.000 (2008-12-31)
25116 protected function setImageBuffer($image, $data) {
25117 if ($this->diskcache) {
25118 if (!isset($this->images[$image])) {
25119 $this->images[$image] = $this->getObjFilename('image'.$image);
25121 $this->writeDiskCache($this->images[$image], serialize($data));
25122 } else {
25123 $this->images[$image] = $data;
25125 if (!in_array($image, $this->imagekeys)) {
25126 $this->imagekeys[] = $image;
25127 ++$this->numimages;
25132 * Set image buffer content for a specified sub-key.
25133 * @param $image (string) image key
25134 * @param $key (string) image sub-key
25135 * @param $data (array) image data
25136 * @protected
25137 * @since 4.5.000 (2008-12-31)
25139 protected function setImageSubBuffer($image, $key, $data) {
25140 if (!isset($this->images[$image])) {
25141 $this->setImageBuffer($image, array());
25143 if ($this->diskcache) {
25144 $tmpimg = $this->getImageBuffer($image);
25145 $tmpimg[$key] = $data;
25146 $this->writeDiskCache($this->images[$image], serialize($tmpimg));
25147 } else {
25148 $this->images[$image][$key] = $data;
25153 * Get image buffer content.
25154 * @param $image (string) image key
25155 * @return string image buffer content or false in case of error
25156 * @protected
25157 * @since 4.5.000 (2008-12-31)
25159 protected function getImageBuffer($image) {
25160 if ($this->diskcache AND isset($this->images[$image])) {
25161 return unserialize($this->readDiskCache($this->images[$image]));
25162 } elseif (isset($this->images[$image])) {
25163 return $this->images[$image];
25165 return false;
25169 * Set font buffer content.
25170 * @param $font (string) font key
25171 * @param $data (array) font data
25172 * @protected
25173 * @since 4.5.000 (2009-01-02)
25175 protected function setFontBuffer($font, $data) {
25176 if ($this->diskcache) {
25177 if (!isset($this->fonts[$font])) {
25178 $this->fonts[$font] = $this->getObjFilename('font');
25180 $this->writeDiskCache($this->fonts[$font], serialize($data));
25181 } else {
25182 $this->fonts[$font] = $data;
25184 if (!in_array($font, $this->fontkeys)) {
25185 $this->fontkeys[] = $font;
25186 // store object ID for current font
25187 ++$this->n;
25188 $this->font_obj_ids[$font] = $this->n;
25189 $this->setFontSubBuffer($font, 'n', $this->n);
25194 * Set font buffer content.
25195 * @param $font (string) font key
25196 * @param $key (string) font sub-key
25197 * @param $data (array) font data
25198 * @protected
25199 * @since 4.5.000 (2009-01-02)
25201 protected function setFontSubBuffer($font, $key, $data) {
25202 if (!isset($this->fonts[$font])) {
25203 $this->setFontBuffer($font, array());
25205 if ($this->diskcache) {
25206 $tmpfont = $this->getFontBuffer($font);
25207 $tmpfont[$key] = $data;
25208 $this->writeDiskCache($this->fonts[$font], serialize($tmpfont));
25209 } else {
25210 $this->fonts[$font][$key] = $data;
25215 * Get font buffer content.
25216 * @param $font (string) font key
25217 * @return string font buffer content or false in case of error
25218 * @protected
25219 * @since 4.5.000 (2009-01-02)
25221 protected function getFontBuffer($font) {
25222 if ($this->diskcache AND isset($this->fonts[$font])) {
25223 return unserialize($this->readDiskCache($this->fonts[$font]));
25224 } elseif (isset($this->fonts[$font])) {
25225 return $this->fonts[$font];
25227 return false;
25231 * Move a page to a previous position.
25232 * @param $frompage (int) number of the source page
25233 * @param $topage (int) number of the destination page (must be less than $frompage)
25234 * @return true in case of success, false in case of error.
25235 * @public
25236 * @since 4.5.000 (2009-01-02)
25238 public function movePage($frompage, $topage) {
25239 if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
25240 return false;
25242 if ($frompage == $this->page) {
25243 // close the page before moving it
25244 $this->endPage();
25246 // move all page-related states
25247 $tmppage = $this->getPageBuffer($frompage);
25248 $tmppagedim = $this->pagedim[$frompage];
25249 $tmppagelen = $this->pagelen[$frompage];
25250 $tmpintmrk = $this->intmrk[$frompage];
25251 $tmpbordermrk = $this->bordermrk[$frompage];
25252 $tmpcntmrk = $this->cntmrk[$frompage];
25253 if (isset($this->footerpos[$frompage])) {
25254 $tmpfooterpos = $this->footerpos[$frompage];
25256 if (isset($this->footerlen[$frompage])) {
25257 $tmpfooterlen = $this->footerlen[$frompage];
25259 if (isset($this->transfmrk[$frompage])) {
25260 $tmptransfmrk = $this->transfmrk[$frompage];
25262 if (isset($this->PageAnnots[$frompage])) {
25263 $tmpannots = $this->PageAnnots[$frompage];
25265 if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
25266 for ($i = $frompage; $i > $topage; --$i) {
25267 if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $frompage)) {
25268 --$this->pagegroups[$this->newpagegroup[$i]];
25269 break;
25272 for ($i = $topage; $i > 0; --$i) {
25273 if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $topage)) {
25274 ++$this->pagegroups[$this->newpagegroup[$i]];
25275 break;
25279 for ($i = $frompage; $i > $topage; --$i) {
25280 $j = $i - 1;
25281 // shift pages down
25282 $this->setPageBuffer($i, $this->getPageBuffer($j));
25283 $this->pagedim[$i] = $this->pagedim[$j];
25284 $this->pagelen[$i] = $this->pagelen[$j];
25285 $this->intmrk[$i] = $this->intmrk[$j];
25286 $this->bordermrk[$i] = $this->bordermrk[$j];
25287 $this->cntmrk[$i] = $this->cntmrk[$j];
25288 if (isset($this->footerpos[$j])) {
25289 $this->footerpos[$i] = $this->footerpos[$j];
25290 } elseif (isset($this->footerpos[$i])) {
25291 unset($this->footerpos[$i]);
25293 if (isset($this->footerlen[$j])) {
25294 $this->footerlen[$i] = $this->footerlen[$j];
25295 } elseif (isset($this->footerlen[$i])) {
25296 unset($this->footerlen[$i]);
25298 if (isset($this->transfmrk[$j])) {
25299 $this->transfmrk[$i] = $this->transfmrk[$j];
25300 } elseif (isset($this->transfmrk[$i])) {
25301 unset($this->transfmrk[$i]);
25303 if (isset($this->PageAnnots[$j])) {
25304 $this->PageAnnots[$i] = $this->PageAnnots[$j];
25305 } elseif (isset($this->PageAnnots[$i])) {
25306 unset($this->PageAnnots[$i]);
25308 if (isset($this->newpagegroup[$j])) {
25309 $this->newpagegroup[$i] = $this->newpagegroup[$j];
25310 unset($this->newpagegroup[$j]);
25312 if ($this->currpagegroup == $j) {
25313 $this->currpagegroup = $i;
25316 $this->setPageBuffer($topage, $tmppage);
25317 $this->pagedim[$topage] = $tmppagedim;
25318 $this->pagelen[$topage] = $tmppagelen;
25319 $this->intmrk[$topage] = $tmpintmrk;
25320 $this->bordermrk[$topage] = $tmpbordermrk;
25321 $this->cntmrk[$topage] = $tmpcntmrk;
25322 if (isset($tmpfooterpos)) {
25323 $this->footerpos[$topage] = $tmpfooterpos;
25324 } elseif (isset($this->footerpos[$topage])) {
25325 unset($this->footerpos[$topage]);
25327 if (isset($tmpfooterlen)) {
25328 $this->footerlen[$topage] = $tmpfooterlen;
25329 } elseif (isset($this->footerlen[$topage])) {
25330 unset($this->footerlen[$topage]);
25332 if (isset($tmptransfmrk)) {
25333 $this->transfmrk[$topage] = $tmptransfmrk;
25334 } elseif (isset($this->transfmrk[$topage])) {
25335 unset($this->transfmrk[$topage]);
25337 if (isset($tmpannots)) {
25338 $this->PageAnnots[$topage] = $tmpannots;
25339 } elseif (isset($this->PageAnnots[$topage])) {
25340 unset($this->PageAnnots[$topage]);
25342 // adjust outlines
25343 $tmpoutlines = $this->outlines;
25344 foreach ($tmpoutlines as $key => $outline) {
25345 if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
25346 $this->outlines[$key]['p'] = ($outline['p'] + 1);
25347 } elseif ($outline['p'] == $frompage) {
25348 $this->outlines[$key]['p'] = $topage;
25351 // adjust dests
25352 $tmpdests = $this->dests;
25353 foreach ($tmpdests as $key => $dest) {
25354 if (($dest['p'] >= $topage) AND ($dest['p'] < $frompage)) {
25355 $this->dests[$key]['p'] = ($dest['p'] + 1);
25356 } elseif ($dest['p'] == $frompage) {
25357 $this->dests[$key]['p'] = $topage;
25360 // adjust links
25361 $tmplinks = $this->links;
25362 foreach ($tmplinks as $key => $link) {
25363 if (($link[0] >= $topage) AND ($link[0] < $frompage)) {
25364 $this->links[$key][0] = ($link[0] + 1);
25365 } elseif ($link[0] == $frompage) {
25366 $this->links[$key][0] = $topage;
25369 // adjust javascript
25370 $tmpjavascript = $this->javascript;
25371 global $jfrompage, $jtopage;
25372 $jfrompage = $frompage;
25373 $jtopage = $topage;
25374 $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
25375 create_function('$matches', 'global $jfrompage, $jtopage;
25376 $pagenum = intval($matches[3]) + 1;
25377 if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
25378 $newpage = ($pagenum + 1);
25379 } elseif ($pagenum == $jfrompage) {
25380 $newpage = $jtopage;
25381 } else {
25382 $newpage = $pagenum;
25384 --$newpage;
25385 return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
25386 // return to last page
25387 $this->lastPage(true);
25388 return true;
25392 * Remove the specified page.
25393 * @param $page (int) page to remove
25394 * @return true in case of success, false in case of error.
25395 * @public
25396 * @since 4.6.004 (2009-04-23)
25398 public function deletePage($page) {
25399 if (($page < 1) OR ($page > $this->numpages)) {
25400 return false;
25402 // delete current page
25403 unset($this->pages[$page]);
25404 unset($this->pagedim[$page]);
25405 unset($this->pagelen[$page]);
25406 unset($this->intmrk[$page]);
25407 unset($this->bordermrk[$page]);
25408 unset($this->cntmrk[$page]);
25409 if (isset($this->footerpos[$page])) {
25410 unset($this->footerpos[$page]);
25412 if (isset($this->footerlen[$page])) {
25413 unset($this->footerlen[$page]);
25415 if (isset($this->transfmrk[$page])) {
25416 unset($this->transfmrk[$page]);
25418 if (isset($this->PageAnnots[$page])) {
25419 unset($this->PageAnnots[$page]);
25421 if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
25422 for ($i = $page; $i > 0; --$i) {
25423 if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $page)) {
25424 --$this->pagegroups[$this->newpagegroup[$i]];
25425 break;
25429 if (isset($this->pageopen[$page])) {
25430 unset($this->pageopen[$page]);
25432 if ($page < $this->numpages) {
25433 // update remaining pages
25434 for ($i = $page; $i < $this->numpages; ++$i) {
25435 $j = $i + 1;
25436 // shift pages
25437 $this->setPageBuffer($i, $this->getPageBuffer($j));
25438 $this->pagedim[$i] = $this->pagedim[$j];
25439 $this->pagelen[$i] = $this->pagelen[$j];
25440 $this->intmrk[$i] = $this->intmrk[$j];
25441 $this->bordermrk[$i] = $this->bordermrk[$j];
25442 $this->cntmrk[$i] = $this->cntmrk[$j];
25443 if (isset($this->footerpos[$j])) {
25444 $this->footerpos[$i] = $this->footerpos[$j];
25445 } elseif (isset($this->footerpos[$i])) {
25446 unset($this->footerpos[$i]);
25448 if (isset($this->footerlen[$j])) {
25449 $this->footerlen[$i] = $this->footerlen[$j];
25450 } elseif (isset($this->footerlen[$i])) {
25451 unset($this->footerlen[$i]);
25453 if (isset($this->transfmrk[$j])) {
25454 $this->transfmrk[$i] = $this->transfmrk[$j];
25455 } elseif (isset($this->transfmrk[$i])) {
25456 unset($this->transfmrk[$i]);
25458 if (isset($this->PageAnnots[$j])) {
25459 $this->PageAnnots[$i] = $this->PageAnnots[$j];
25460 } elseif (isset($this->PageAnnots[$i])) {
25461 unset($this->PageAnnots[$i]);
25463 if (isset($this->newpagegroup[$j])) {
25464 $this->newpagegroup[$i] = $this->newpagegroup[$j];
25465 unset($this->newpagegroup[$j]);
25467 if ($this->currpagegroup == $j) {
25468 $this->currpagegroup = $i;
25470 if (isset($this->pageopen[$j])) {
25471 $this->pageopen[$i] = $this->pageopen[$j];
25472 } elseif (isset($this->pageopen[$i])) {
25473 unset($this->pageopen[$i]);
25476 // remove last page
25477 unset($this->pages[$this->numpages]);
25478 unset($this->pagedim[$this->numpages]);
25479 unset($this->pagelen[$this->numpages]);
25480 unset($this->intmrk[$this->numpages]);
25481 unset($this->bordermrk[$this->numpages]);
25482 unset($this->cntmrk[$this->numpages]);
25483 if (isset($this->footerpos[$this->numpages])) {
25484 unset($this->footerpos[$this->numpages]);
25486 if (isset($this->footerlen[$this->numpages])) {
25487 unset($this->footerlen[$this->numpages]);
25489 if (isset($this->transfmrk[$this->numpages])) {
25490 unset($this->transfmrk[$this->numpages]);
25492 if (isset($this->PageAnnots[$this->numpages])) {
25493 unset($this->PageAnnots[$this->numpages]);
25495 if (isset($this->newpagegroup[$this->numpages])) {
25496 unset($this->newpagegroup[$this->numpages]);
25498 if ($this->currpagegroup == $this->numpages) {
25499 $this->currpagegroup = ($this->numpages - 1);
25501 if (isset($this->pagegroups[$this->numpages])) {
25502 unset($this->pagegroups[$this->numpages]);
25504 if (isset($this->pageopen[$this->numpages])) {
25505 unset($this->pageopen[$this->numpages]);
25508 --$this->numpages;
25509 $this->page = $this->numpages;
25510 // adjust outlines
25511 $tmpoutlines = $this->outlines;
25512 foreach ($tmpoutlines as $key => $outline) {
25513 if ($outline['p'] > $page) {
25514 $this->outlines[$key]['p'] = $outline['p'] - 1;
25515 } elseif ($outline['p'] == $page) {
25516 unset($this->outlines[$key]);
25519 // adjust dests
25520 $tmpdests = $this->dests;
25521 foreach ($tmpdests as $key => $dest) {
25522 if ($dest['p'] > $page) {
25523 $this->dests[$key]['p'] = $dest['p'] - 1;
25524 } elseif ($dest['p'] == $page) {
25525 unset($this->dests[$key]);
25528 // adjust links
25529 $tmplinks = $this->links;
25530 foreach ($tmplinks as $key => $link) {
25531 if ($link[0] > $page) {
25532 $this->links[$key][0] = $link[0] - 1;
25533 } elseif ($link[0] == $page) {
25534 unset($this->links[$key]);
25537 // adjust javascript
25538 $tmpjavascript = $this->javascript;
25539 global $jpage;
25540 $jpage = $page;
25541 $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
25542 create_function('$matches', 'global $jpage;
25543 $pagenum = intval($matches[3]) + 1;
25544 if ($pagenum >= $jpage) {
25545 $newpage = ($pagenum - 1);
25546 } elseif ($pagenum == $jpage) {
25547 $newpage = 1;
25548 } else {
25549 $newpage = $pagenum;
25551 --$newpage;
25552 return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
25553 // return to last page
25554 $this->lastPage(true);
25555 return true;
25559 * Clone the specified page to a new page.
25560 * @param $page (int) number of page to copy (0 = current page)
25561 * @return true in case of success, false in case of error.
25562 * @public
25563 * @since 4.9.015 (2010-04-20)
25565 public function copyPage($page=0) {
25566 if ($page == 0) {
25567 // default value
25568 $page = $this->page;
25570 if (($page < 1) OR ($page > $this->numpages)) {
25571 return false;
25573 // close the last page
25574 $this->endPage();
25575 // copy all page-related states
25576 ++$this->numpages;
25577 $this->page = $this->numpages;
25578 $this->setPageBuffer($this->page, $this->getPageBuffer($page));
25579 $this->pagedim[$this->page] = $this->pagedim[$page];
25580 $this->pagelen[$this->page] = $this->pagelen[$page];
25581 $this->intmrk[$this->page] = $this->intmrk[$page];
25582 $this->bordermrk[$this->page] = $this->bordermrk[$page];
25583 $this->cntmrk[$this->page] = $this->cntmrk[$page];
25584 $this->pageopen[$this->page] = false;
25585 if (isset($this->footerpos[$page])) {
25586 $this->footerpos[$this->page] = $this->footerpos[$page];
25588 if (isset($this->footerlen[$page])) {
25589 $this->footerlen[$this->page] = $this->footerlen[$page];
25591 if (isset($this->transfmrk[$page])) {
25592 $this->transfmrk[$this->page] = $this->transfmrk[$page];
25594 if (isset($this->PageAnnots[$page])) {
25595 $this->PageAnnots[$this->page] = $this->PageAnnots[$page];
25597 if (isset($this->newpagegroup[$page])) {
25598 // start a new group
25599 $this->newpagegroup[$this->page] = sizeof($this->newpagegroup) + 1;
25600 $this->currpagegroup = $this->newpagegroup[$this->page];
25601 $this->pagegroups[$this->currpagegroup] = 1;
25602 } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
25603 ++$this->pagegroups[$this->currpagegroup];
25605 // copy outlines
25606 $tmpoutlines = $this->outlines;
25607 foreach ($tmpoutlines as $key => $outline) {
25608 if ($outline['p'] == $page) {
25609 $this->outlines[] = array('t' => $outline['t'], 'l' => $outline['l'], 'y' => $outline['y'], 'p' => $this->page, 's' => $outline['s'], 'c' => $outline['c']);
25612 // copy links
25613 $tmplinks = $this->links;
25614 foreach ($tmplinks as $key => $link) {
25615 if ($link[0] == $page) {
25616 $this->links[] = array($this->page, $link[1]);
25619 // return to last page
25620 $this->lastPage(true);
25621 return true;
25625 * Output a Table of Content Index (TOC).
25626 * This method must be called after all Bookmarks were set.
25627 * Before calling this method you have to open the page using the addTOCPage() method.
25628 * After calling this method you have to call endTOCPage() to close the TOC page.
25629 * You can override this method to achieve different styles.
25630 * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
25631 * @param $numbersfont (string) set the font for page numbers (please use monospaced font for better alignment).
25632 * @param $filler (string) string used to fill the space between text and page number.
25633 * @param $toc_name (string) name to use for TOC bookmark.
25634 * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
25635 * @param $color (array) RGB color array for bookmark title (values from 0 to 255).
25636 * @public
25637 * @author Nicola Asuni
25638 * @since 4.5.000 (2009-01-02)
25639 * @see addTOCPage(), endTOCPage(), addHTMLTOC()
25641 public function addTOC($page='', $numbersfont='', $filler='.', $toc_name='TOC', $style='', $color=array(0,0,0)) {
25642 $fontsize = $this->FontSizePt;
25643 $fontfamily = $this->FontFamily;
25644 $fontstyle = $this->FontStyle;
25645 $w = $this->w - $this->lMargin - $this->rMargin;
25646 $spacer = $this->GetStringWidth(chr(32)) * 4;
25647 $page_first = $this->getPage();
25648 $lmargin = $this->lMargin;
25649 $rmargin = $this->rMargin;
25650 $x_start = $this->GetX();
25651 $current_page = $this->page;
25652 $current_column = $this->current_column;
25653 if ($this->empty_string($numbersfont)) {
25654 $numbersfont = $this->default_monospaced_font;
25656 if ($this->empty_string($filler)) {
25657 $filler = ' ';
25659 if ($this->empty_string($page)) {
25660 $gap = ' ';
25661 } else {
25662 $gap = '';
25663 if ($page < 1) {
25664 $page = 1;
25667 $this->SetFont($numbersfont, $fontstyle, $fontsize);
25668 $numwidth = $this->GetStringWidth('00000');
25669 $maxpage = 0; //used for pages on attached documents
25670 foreach ($this->outlines as $key => $outline) {
25671 // check for extra pages (used for attachments)
25672 if (($this->page > $page_first) AND ($outline['p'] >= $this->numpages)) {
25673 $outline['p'] += ($this->page - $page_first);
25675 if ($this->rtl) {
25676 $aligntext = 'R';
25677 $alignnum = 'L';
25678 } else {
25679 $aligntext = 'L';
25680 $alignnum = 'R';
25682 if ($outline['l'] == 0) {
25683 $this->SetFont($fontfamily, $fontstyle.'B', $fontsize);
25684 } else {
25685 $this->SetFont($fontfamily, $fontstyle, $fontsize - $outline['l']);
25687 // check for page break
25688 $this->checkPageBreak((2 * $this->FontSize * $this->cell_height_ratio));
25689 // set margins and X position
25690 if (($this->page == $current_page) AND ($this->current_column == $current_column)) {
25691 $this->lMargin = $lmargin;
25692 $this->rMargin = $rmargin;
25693 } else {
25694 if ($this->current_column != $current_column) {
25695 if ($this->rtl) {
25696 $x_start = $this->w - $this->columns[$this->current_column]['x'];
25697 } else {
25698 $x_start = $this->columns[$this->current_column]['x'];
25701 $lmargin = $this->lMargin;
25702 $rmargin = $this->rMargin;
25703 $current_page = $this->page;
25704 $current_column = $this->current_column;
25706 $this->SetX($x_start);
25707 $indent = ($spacer * $outline['l']);
25708 if ($this->rtl) {
25709 $this->x -= $indent;
25710 $this->rMargin = $this->w - $this->x;
25711 } else {
25712 $this->x += $indent;
25713 $this->lMargin = $this->x;
25715 $link = $this->AddLink();
25716 $this->SetLink($link, $outline['y'], $outline['p']);
25717 // write the text
25718 if ($this->rtl) {
25719 $txt = ' '.$outline['t'];
25720 } else {
25721 $txt = $outline['t'].' ';
25723 $this->Write(0, $txt, $link, false, $aligntext, false, 0, false, false, 0, $numwidth, '');
25724 if ($this->rtl) {
25725 $tw = $this->x - $this->lMargin;
25726 } else {
25727 $tw = $this->w - $this->rMargin - $this->x;
25729 $this->SetFont($numbersfont, $fontstyle, $fontsize);
25730 if ($this->empty_string($page)) {
25731 $pagenum = $outline['p'];
25732 } else {
25733 // placemark to be replaced with the correct number
25734 $pagenum = '{#'.($outline['p']).'}';
25735 if ($this->isUnicodeFont()) {
25736 $pagenum = '{'.$pagenum.'}';
25738 $maxpage = max($maxpage, $outline['p']);
25740 $fw = ($tw - $this->GetStringWidth($pagenum.$filler));
25741 $numfills = floor($fw / $this->GetStringWidth($filler));
25742 if ($numfills > 0) {
25743 $rowfill = str_repeat($filler, $numfills);
25744 } else {
25745 $rowfill = '';
25747 if ($this->rtl) {
25748 $pagenum = $pagenum.$gap.$rowfill;
25749 } else {
25750 $pagenum = $rowfill.$gap.$pagenum;
25752 // write the number
25753 $this->Cell($tw, 0, $pagenum, 0, 1, $alignnum, 0, $link, 0);
25755 $page_last = $this->getPage();
25756 $maxpage = max($maxpage, $page_last);
25757 $numpages = $page_last - $page_first + 1;
25758 if (!$this->empty_string($page)) {
25759 for ($p = $page_first; $p <= $page_last; ++$p) {
25760 // get page data
25761 $temppage = $this->getPageBuffer($p);
25762 for ($n = 1; $n <= $maxpage; ++$n) {
25763 // update page numbers
25764 $a = '{#'.$n.'}';
25765 // get page number aliases
25766 $pnalias = $this->getInternalPageNumberAliases($a);
25767 // calculate replacement number
25768 if (($n >= $page) AND ($n <= $this->numpages)) {
25769 $np = $n + $numpages;
25770 } else {
25771 $np = $n;
25773 $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
25774 $nu = $this->UTF8ToUTF16BE($na, false);
25775 // replace aliases with numbers
25776 foreach ($pnalias['u'] as $u) {
25777 $sfill = str_repeat($filler, max(0, (strlen($u) - strlen($nu.' '))));
25778 if ($this->rtl) {
25779 $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
25780 } else {
25781 $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
25783 $temppage = str_replace($u, $nr, $temppage);
25785 foreach ($pnalias['a'] as $a) {
25786 $sfill = str_repeat($filler, max(0, (strlen($a) - strlen($na.' '))));
25787 if ($this->rtl) {
25788 $nr = $na.' '.$sfill;
25789 } else {
25790 $nr = $sfill.' '.$na;
25792 $temppage = str_replace($a, $nr, $temppage);
25795 // save changes
25796 $this->setPageBuffer($p, $temppage);
25798 // move pages
25799 $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
25800 for ($i = 0; $i < $numpages; ++$i) {
25801 $this->movePage($page_last, $page);
25807 * Output a Table Of Content Index (TOC) using HTML templates.
25808 * This method must be called after all Bookmarks were set.
25809 * Before calling this method you have to open the page using the addTOCPage() method.
25810 * After calling this method you have to call endTOCPage() to close the TOC page.
25811 * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
25812 * @param $toc_name (string) name to use for TOC bookmark.
25813 * @param $templates (array) array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
25814 * @param $correct_align (boolean) if true correct the number alignment (numbers must be in monospaced font like courier and right aligned on LTR, or left aligned on RTL)
25815 * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
25816 * @param $color (array) RGB color array for title (values from 0 to 255).
25817 * @public
25818 * @author Nicola Asuni
25819 * @since 5.0.001 (2010-05-06)
25820 * @see addTOCPage(), endTOCPage(), addTOC()
25822 public function addHTMLTOC($page='', $toc_name='TOC', $templates=array(), $correct_align=true, $style='', $color=array(0,0,0)) {
25823 $filler = ' ';
25824 $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
25825 $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
25826 // set new style for link
25827 $this->htmlLinkColorArray = array();
25828 $this->htmlLinkFontStyle = '';
25829 $page_first = $this->getPage();
25830 // get the font type used for numbers in each template
25831 $current_font = $this->FontFamily;
25832 foreach ($templates as $level => $html) {
25833 $dom = $this->getHtmlDomArray($html);
25834 foreach ($dom as $key => $value) {
25835 if ($value['value'] == '#TOC_PAGE_NUMBER#') {
25836 $this->SetFont($dom[($key - 1)]['fontname']);
25837 $templates['F'.$level] = $this->isUnicodeFont();
25841 $this->SetFont($current_font);
25842 $maxpage = 0; //used for pages on attached documents
25843 foreach ($this->outlines as $key => $outline) {
25844 // get HTML template
25845 $row = $templates[$outline['l']];
25846 if ($this->empty_string($page)) {
25847 $pagenum = $outline['p'];
25848 } else {
25849 // placemark to be replaced with the correct number
25850 $pagenum = '{#'.($outline['p']).'}';
25851 if ($templates['F'.$outline['l']]) {
25852 $pagenum = '{'.$pagenum.'}';
25854 $maxpage = max($maxpage, $outline['p']);
25856 // replace templates with current values
25857 $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
25858 $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
25859 // add link to page
25860 $row = '<a href="#'.$outline['p'].','.$outline['y'].'">'.$row.'</a>';
25861 // write bookmark entry
25862 $this->writeHTML($row, false, false, true, false, '');
25864 // restore link styles
25865 $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
25866 $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
25867 // move TOC page and replace numbers
25868 $page_last = $this->getPage();
25869 $maxpage = max($maxpage, $page_last);
25870 $numpages = $page_last - $page_first + 1;
25871 if (!$this->empty_string($page)) {
25872 for ($p = $page_first; $p <= $page_last; ++$p) {
25873 // get page data
25874 $temppage = $this->getPageBuffer($p);
25875 for ($n = 1; $n <= $maxpage; ++$n) {
25876 // update page numbers
25877 $a = '{#'.$n.'}';
25878 // get page number aliases
25879 $pnalias = $this->getInternalPageNumberAliases($a);
25880 // calculate replacement number
25881 if ($n >= $page) {
25882 $np = $n + $numpages;
25883 } else {
25884 $np = $n;
25886 $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
25887 $nu = $this->UTF8ToUTF16BE($na, false);
25888 // replace aliases with numbers
25889 foreach ($pnalias['u'] as $u) {
25890 if ($correct_align) {
25891 $sfill = str_repeat($filler, (strlen($u) - strlen($nu.' ')));
25892 if ($this->rtl) {
25893 $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
25894 } else {
25895 $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
25897 } else {
25898 $nr = $nu;
25900 $temppage = str_replace($u, $nr, $temppage);
25902 foreach ($pnalias['a'] as $a) {
25903 if ($correct_align) {
25904 $sfill = str_repeat($filler, (strlen($a) - strlen($na.' ')));
25905 if ($this->rtl) {
25906 $nr = $na.' '.$sfill;
25907 } else {
25908 $nr = $sfill.' '.$na;
25910 } else {
25911 $nr = $na;
25913 $temppage = str_replace($a, $nr, $temppage);
25916 // save changes
25917 $this->setPageBuffer($p, $temppage);
25919 // move pages
25920 $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
25921 for ($i = 0; $i < $numpages; ++$i) {
25922 $this->movePage($page_last, $page);
25928 * Stores a copy of the current TCPDF object used for undo operation.
25929 * @public
25930 * @since 4.5.029 (2009-03-19)
25932 public function startTransaction() {
25933 if (isset($this->objcopy)) {
25934 // remove previous copy
25935 $this->commitTransaction();
25937 // record current page number and Y position
25938 $this->start_transaction_page = $this->page;
25939 $this->start_transaction_y = $this->y;
25940 // clone current object
25941 $this->objcopy = $this->objclone($this);
25945 * Delete the copy of the current TCPDF object used for undo operation.
25946 * @public
25947 * @since 4.5.029 (2009-03-19)
25949 public function commitTransaction() {
25950 if (isset($this->objcopy)) {
25951 $this->objcopy->_destroy(true, true);
25952 unset($this->objcopy);
25957 * This method allows to undo the latest transaction by returning the latest saved TCPDF object with startTransaction().
25958 * @param $self (boolean) if true restores current class object to previous state without the need of reassignment via the returned value.
25959 * @return TCPDF object.
25960 * @public
25961 * @since 4.5.029 (2009-03-19)
25963 public function rollbackTransaction($self=false) {
25964 if (isset($this->objcopy)) {
25965 if (isset($this->objcopy->diskcache) AND $this->objcopy->diskcache) {
25966 // truncate files to previous values
25967 foreach ($this->objcopy->cache_file_length as $file => $length) {
25968 $file = substr($file, 1);
25969 $handle = fopen($file, 'r+');
25970 ftruncate($handle, $length);
25973 $this->_destroy(true, true);
25974 if ($self) {
25975 $objvars = get_object_vars($this->objcopy);
25976 foreach ($objvars as $key => $value) {
25977 $this->$key = $value;
25980 return $this->objcopy;
25982 return $this;
25986 * Creates a copy of a class object
25987 * @param $object (object) class object to be cloned
25988 * @return cloned object
25989 * @public
25990 * @since 4.5.029 (2009-03-19)
25992 public function objclone($object) {
25993 return @clone($object);
25997 * Determine whether a string is empty.
25998 * @param $str (string) string to be checked
25999 * @return boolean true if string is empty
26000 * @public
26001 * @since 4.5.044 (2009-04-16)
26003 public function empty_string($str) {
26004 return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
26008 * Find position of last occurrence of a substring in a string
26009 * @param $haystack (string) The string to search in.
26010 * @param $needle (string) substring to search.
26011 * @param $offset (int) May be specified to begin searching an arbitrary number of characters into the string.
26012 * @return Returns the position where the needle exists. Returns FALSE if the needle was not found.
26013 * @public
26014 * @since 4.8.038 (2010-03-13)
26016 public function revstrpos($haystack, $needle, $offset = 0) {
26017 $length = strlen($haystack);
26018 $offset = ($offset > 0)?($length - $offset):abs($offset);
26019 $pos = strpos(strrev($haystack), strrev($needle), $offset);
26020 return ($pos === false)?false:($length - $pos - strlen($needle));
26023 // --- MULTI COLUMNS METHODS -----------------------
26026 * Set multiple columns of the same size
26027 * @param $numcols (int) number of columns (set to zero to disable columns mode)
26028 * @param $width (int) column width
26029 * @param $y (int) column starting Y position (leave empty for current Y position)
26030 * @public
26031 * @since 4.9.001 (2010-03-28)
26033 public function setEqualColumns($numcols=0, $width=0, $y='') {
26034 $this->columns = array();
26035 if ($numcols < 2) {
26036 $numcols = 0;
26037 $this->columns = array();
26038 } else {
26039 // maximum column width
26040 $maxwidth = ($this->w - $this->original_lMargin - $this->original_rMargin) / $numcols;
26041 if (($width == 0) OR ($width > $maxwidth)) {
26042 $width = $maxwidth;
26044 if ($this->empty_string($y)) {
26045 $y = $this->y;
26047 // space between columns
26048 $space = (($this->w - $this->original_lMargin - $this->original_rMargin - ($numcols * $width)) / ($numcols - 1));
26049 // fill the columns array (with, space, starting Y position)
26050 for ($i = 0; $i < $numcols; ++$i) {
26051 $this->columns[$i] = array('w' => $width, 's' => $space, 'y' => $y);
26054 $this->num_columns = $numcols;
26055 $this->current_column = 0;
26056 $this->column_start_page = $this->page;
26057 $this->selectColumn(0);
26061 * Remove columns and reset page margins.
26062 * @public
26063 * @since 5.9.072 (2011-04-26)
26065 public function resetColumns() {
26066 $this->lMargin = $this->original_lMargin;
26067 $this->rMargin = $this->original_rMargin;
26068 $this->setEqualColumns();
26072 * Set columns array.
26073 * Each column is represented by an array of arrays with the following keys: (w = width, s = space between columns, y = column top position).
26074 * @param $columns (array)
26075 * @public
26076 * @since 4.9.001 (2010-03-28)
26078 public function setColumnsArray($columns) {
26079 $this->columns = $columns;
26080 $this->num_columns = count($columns);
26081 $this->current_column = 0;
26082 $this->column_start_page = $this->page;
26083 $this->selectColumn(0);
26087 * Set position at a given column
26088 * @param $col (int) column number (from 0 to getNumberOfColumns()-1); empty string = current column.
26089 * @public
26090 * @since 4.9.001 (2010-03-28)
26092 public function selectColumn($col='') {
26093 if (is_string($col)) {
26094 $col = $this->current_column;
26095 } elseif ($col >= $this->num_columns) {
26096 $col = 0;
26098 $xshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
26099 $enable_thead = false;
26100 if ($this->num_columns > 1) {
26101 if ($col != $this->current_column) {
26102 // move Y pointer at the top of the column
26103 if ($this->column_start_page == $this->page) {
26104 $this->y = $this->columns[$col]['y'];
26105 } else {
26106 $this->y = $this->tMargin;
26108 // Avoid to write table headers more than once
26109 if (($this->page > $this->maxselcol['page']) OR (($this->page == $this->maxselcol['page']) AND ($col > $this->maxselcol['column']))) {
26110 $enable_thead = true;
26111 $this->maxselcol['page'] = $this->page;
26112 $this->maxselcol['column'] = $col;
26115 $xshift = $this->colxshift;
26116 // set X position of the current column by case
26117 $listindent = ($this->listindentlevel * $this->listindent);
26118 // calculate column X position
26119 $colpos = 0;
26120 for ($i = 0; $i < $col; ++$i) {
26121 $colpos += ($this->columns[$i]['w'] + $this->columns[$i]['s']);
26123 if ($this->rtl) {
26124 $x = $this->w - $this->original_rMargin - $colpos;
26125 $this->rMargin = ($this->w - $x + $listindent);
26126 $this->lMargin = ($x - $this->columns[$col]['w']);
26127 $this->x = $x - $listindent;
26128 } else {
26129 $x = $this->original_lMargin + $colpos;
26130 $this->lMargin = ($x + $listindent);
26131 $this->rMargin = ($this->w - $x - $this->columns[$col]['w']);
26132 $this->x = $x + $listindent;
26134 $this->columns[$col]['x'] = $x;
26136 $this->current_column = $col;
26137 // fix for HTML mode
26138 $this->newline = true;
26139 // print HTML table header (if any)
26140 if ((!$this->empty_string($this->thead)) AND (!$this->inthead)) {
26141 if ($enable_thead) {
26142 // print table header
26143 $this->writeHTML($this->thead, false, false, false, false, '');
26144 $this->y += $xshift['s']['V'];
26145 // store end of header position
26146 if (!isset($this->columns[$col]['th'])) {
26147 $this->columns[$col]['th'] = array();
26149 $this->columns[$col]['th']['\''.$this->page.'\''] = $this->y;
26150 $this->lasth = 0;
26151 } elseif (isset($this->columns[$col]['th']['\''.$this->page.'\''])) {
26152 $this->y = $this->columns[$col]['th']['\''.$this->page.'\''];
26155 // account for an html table cell over multiple columns
26156 if ($this->rtl) {
26157 $this->rMargin += $xshift['x'];
26158 $this->x -= ($xshift['x'] + $xshift['p']['R']);
26159 } else {
26160 $this->lMargin += $xshift['x'];
26161 $this->x += $xshift['x'] + $xshift['p']['L'];
26166 * Return the current column number
26167 * @return int current column number
26168 * @public
26169 * @since 5.5.011 (2010-07-08)
26171 public function getColumn() {
26172 return $this->current_column;
26176 * Return the current number of columns.
26177 * @return int number of columns
26178 * @public
26179 * @since 5.8.018 (2010-08-25)
26181 public function getNumberOfColumns() {
26182 return $this->num_columns;
26186 * Serialize an array of parameters to be used with TCPDF tag in HTML code.
26187 * @param $pararray (array) parameters array
26188 * @return sting containing serialized data
26189 * @public
26190 * @since 4.9.006 (2010-04-02)
26192 public function serializeTCPDFtagParameters($pararray) {
26193 return urlencode(serialize($pararray));
26197 * Set Text rendering mode.
26198 * @param $stroke (int) outline size in user units (0 = disable).
26199 * @param $fill (boolean) if true fills the text (default).
26200 * @param $clip (boolean) if true activate clipping mode
26201 * @public
26202 * @since 4.9.008 (2009-04-02)
26204 public function setTextRenderingMode($stroke=0, $fill=true, $clip=false) {
26205 // Ref.: PDF 32000-1:2008 - 9.3.6 Text Rendering Mode
26206 // convert text rendering parameters
26207 if ($stroke < 0) {
26208 $stroke = 0;
26210 if ($fill === true) {
26211 if ($stroke > 0) {
26212 if ($clip === true) {
26213 // Fill, then stroke text and add to path for clipping
26214 $textrendermode = 6;
26215 } else {
26216 // Fill, then stroke text
26217 $textrendermode = 2;
26219 $textstrokewidth = $stroke;
26220 } else {
26221 if ($clip === true) {
26222 // Fill text and add to path for clipping
26223 $textrendermode = 4;
26224 } else {
26225 // Fill text
26226 $textrendermode = 0;
26229 } else {
26230 if ($stroke > 0) {
26231 if ($clip === true) {
26232 // Stroke text and add to path for clipping
26233 $textrendermode = 5;
26234 } else {
26235 // Stroke text
26236 $textrendermode = 1;
26238 $textstrokewidth = $stroke;
26239 } else {
26240 if ($clip === true) {
26241 // Add text to path for clipping
26242 $textrendermode = 7;
26243 } else {
26244 // Neither fill nor stroke text (invisible)
26245 $textrendermode = 3;
26249 $this->textrendermode = $textrendermode;
26250 $this->textstrokewidth = $stroke * $this->k;
26254 * Returns an array of chars containing soft hyphens.
26255 * @param $word (array) array of chars
26256 * @param $patterns (array) Array of hypenation patterns.
26257 * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
26258 * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
26259 * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
26260 * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
26261 * @param $charmax (int) Maximum length of broken piece of word.
26262 * @return array text with soft hyphens
26263 * @author Nicola Asuni
26264 * @since 4.9.012 (2010-04-12)
26265 * @protected
26267 protected function hyphenateWord($word, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
26268 $hyphenword = array(); // hyphens positions
26269 $numchars = count($word);
26270 if ($numchars <= $charmin) {
26271 return $word;
26273 $word_string = $this->UTF8ArrSubString($word);
26274 // some words will be returned as-is
26275 $pattern = '/^([a-zA-Z0-9_\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/';
26276 if (preg_match($pattern, $word_string) > 0) {
26277 // email
26278 return $word;
26280 $pattern = '/(([a-zA-Z0-9\-]+\.)?)((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/';
26281 if (preg_match($pattern, $word_string) > 0) {
26282 // URL
26283 return $word;
26285 if (isset($dictionary[$word_string])) {
26286 return $this->UTF8StringToArray($dictionary[$word_string]);
26288 // suround word with '_' characters
26289 $tmpword = array_merge(array(95), $word, array(95));
26290 $tmpnumchars = $numchars + 2;
26291 $maxpos = $tmpnumchars - $charmin;
26292 for ($pos = 0; $pos < $maxpos; ++$pos) {
26293 $imax = min(($tmpnumchars - $pos), $charmax);
26294 for ($i = $charmin; $i <= $imax; ++$i) {
26295 $subword = strtolower($this->UTF8ArrSubString($tmpword, $pos, $pos + $i));
26296 if (isset($patterns[$subword])) {
26297 $pattern = $this->UTF8StringToArray($patterns[$subword]);
26298 $pattern_length = count($pattern);
26299 $digits = 1;
26300 for ($j = 0; $j < $pattern_length; ++$j) {
26301 // check if $pattern[$j] is a number
26302 if (($pattern[$j] >= 48) AND ($pattern[$j] <= 57)) {
26303 if ($j == 0) {
26304 $zero = $pos - 1;
26305 } else {
26306 $zero = $pos + $j - $digits;
26308 if (!isset($hyphenword[$zero]) OR ($hyphenword[$zero] != $pattern[$j])) {
26309 $hyphenword[$zero] = $this->unichr($pattern[$j]);
26311 ++$digits;
26317 $inserted = 0;
26318 $maxpos = $numchars - $rightmin;
26319 for ($i = $leftmin; $i <= $maxpos; ++$i) {
26320 if (isset($hyphenword[$i]) AND (($hyphenword[$i] % 2) != 0)) {
26321 // 173 = soft hyphen character
26322 array_splice($word, $i + $inserted, 0, 173);
26323 ++$inserted;
26326 return $word;
26330 * Returns an array of hyphenation patterns.
26331 * @param $file (string) TEX file containing hypenation patterns. TEX pattrns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
26332 * @return array of hyphenation patterns
26333 * @author Nicola Asuni
26334 * @since 4.9.012 (2010-04-12)
26335 * @public
26337 public function getHyphenPatternsFromTEX($file) {
26338 // TEX patterns are available at:
26339 // http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
26340 $data = file_get_contents($file);
26341 $patterns = array();
26342 // remove comments
26343 $data = preg_replace('/\%[^\n]*/', '', $data);
26344 // extract the patterns part
26345 preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
26346 $data = trim(substr($matches[0], 10, -1));
26347 // extract each pattern
26348 $patterns_array = preg_split('/[\s]+/', $data);
26349 // create new language array of patterns
26350 $patterns = array();
26351 foreach($patterns_array as $val) {
26352 if (!$this->empty_string($val)) {
26353 $val = trim($val);
26354 $val = str_replace('\'', '\\\'', $val);
26355 $key = preg_replace('/[0-9]+/', '', $val);
26356 $patterns[$key] = $val;
26359 return $patterns;
26363 * Returns text with soft hyphens.
26364 * @param $text (string) text to process
26365 * @param $patterns (mixed) Array of hypenation patterns or a TEX file containing hypenation patterns. TEX patterns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
26366 * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
26367 * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
26368 * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
26369 * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
26370 * @param $charmax (int) Maximum length of broken piece of word.
26371 * @return array text with soft hyphens
26372 * @author Nicola Asuni
26373 * @since 4.9.012 (2010-04-12)
26374 * @public
26376 public function hyphenateText($text, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
26377 $text = $this->unhtmlentities($text);
26378 $word = array(); // last word
26379 $txtarr = array(); // text to be returned
26380 $intag = false; // true if we are inside an HTML tag
26381 if (!is_array($patterns)) {
26382 $patterns = $this->getHyphenPatternsFromTEX($patterns);
26384 // get array of characters
26385 $unichars = $this->UTF8StringToArray($text);
26386 // for each char
26387 foreach ($unichars as $char) {
26388 if ((!$intag) AND $this->unicode->uni_type[$char] == 'L') {
26389 // letter character
26390 $word[] = $char;
26391 } else {
26392 // other type of character
26393 if (!$this->empty_string($word)) {
26394 // hypenate the word
26395 $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
26396 $word = array();
26398 $txtarr[] = $char;
26399 if (chr($char) == '<') {
26400 // we are inside an HTML tag
26401 $intag = true;
26402 } elseif ($intag AND (chr($char) == '>')) {
26403 // end of HTML tag
26404 $intag = false;
26408 if (!$this->empty_string($word)) {
26409 // hypenate the word
26410 $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
26412 // convert char array to string and return
26413 return $this->UTF8ArrSubString($txtarr);
26417 * Enable/disable rasterization of vector images using ImageMagick library.
26418 * @param $mode (boolean) if true enable rasterization, false otherwise.
26419 * @public
26420 * @since 5.0.000 (2010-04-27)
26422 public function setRasterizeVectorImages($mode) {
26423 $this->rasterize_vector_images = $mode;
26427 * Get the Path-Painting Operators.
26428 * @param $style (string) Style of rendering. Possible values are:
26429 * <ul>
26430 * <li>S or D: Stroke the path.</li>
26431 * <li>s or d: Close and stroke the path.</li>
26432 * <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
26433 * <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
26434 * <li>B or FD or DF: Fill and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
26435 * <li>B* or F*D or DF*: Fill and then stroke the path, using the even-odd rule to determine the region to fill.</li>
26436 * <li>b or fd or df: Close, fill, and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
26437 * <li>b or f*d or df*: Close, fill, and then stroke the path, using the even-odd rule to determine the region to fill.</li>
26438 * <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
26439 * <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
26440 * <li>n: End the path object without filling or stroking it.</li>
26441 * </ul>
26442 * @param $default (string) default style
26443 * @author Nicola Asuni
26444 * @since 5.0.000 (2010-04-30)
26445 * @protected
26447 protected function getPathPaintOperator($style, $default='S') {
26448 $op = '';
26449 switch($style) {
26450 case 'S':
26451 case 'D': {
26452 $op = 'S';
26453 break;
26455 case 's':
26456 case 'd': {
26457 $op = 's';
26458 break;
26460 case 'f':
26461 case 'F': {
26462 $op = 'f';
26463 break;
26465 case 'f*':
26466 case 'F*': {
26467 $op = 'f*';
26468 break;
26470 case 'B':
26471 case 'FD':
26472 case 'DF': {
26473 $op = 'B';
26474 break;
26476 case 'B*':
26477 case 'F*D':
26478 case 'DF*': {
26479 $op = 'B*';
26480 break;
26482 case 'b':
26483 case 'fd':
26484 case 'df': {
26485 $op = 'b';
26486 break;
26488 case 'b*':
26489 case 'f*d':
26490 case 'df*': {
26491 $op = 'b*';
26492 break;
26494 case 'CNZ': {
26495 $op = 'W n';
26496 break;
26498 case 'CEO': {
26499 $op = 'W* n';
26500 break;
26502 case 'n': {
26503 $op = 'n';
26504 break;
26506 default: {
26507 if (!empty($default)) {
26508 $op = $this->getPathPaintOperator($default, '');
26509 } else {
26510 $op = '';
26514 return $op;
26518 * Enable or disable default option for font subsetting.
26519 * @param $enable (boolean) if true enable font subsetting by default.
26520 * @author Nicola Asuni
26521 * @public
26522 * @since 5.3.002 (2010-06-07)
26524 public function setFontSubsetting($enable=true) {
26525 if ($this->pdfa_mode) {
26526 $this->font_subsetting = false;
26527 } else {
26528 $this->font_subsetting = $enable ? true : false;
26533 * Return the default option for font subsetting.
26534 * @return boolean default font subsetting state.
26535 * @author Nicola Asuni
26536 * @public
26537 * @since 5.3.002 (2010-06-07)
26539 public function getFontSubsetting() {
26540 return $this->font_subsetting;
26544 * Left trim the input string
26545 * @param $str (string) string to trim
26546 * @param $replace (string) string that replace spaces.
26547 * @return left trimmed string
26548 * @author Nicola Asuni
26549 * @public
26550 * @since 5.8.000 (2010-08-11)
26552 public function stringLeftTrim($str, $replace='') {
26553 return preg_replace('/^'.$this->re_space['p'].'+/'.$this->re_space['m'], $replace, $str);
26557 * Right trim the input string
26558 * @param $str (string) string to trim
26559 * @param $replace (string) string that replace spaces.
26560 * @return right trimmed string
26561 * @author Nicola Asuni
26562 * @public
26563 * @since 5.8.000 (2010-08-11)
26565 public function stringRightTrim($str, $replace='') {
26566 return preg_replace('/'.$this->re_space['p'].'+$/'.$this->re_space['m'], $replace, $str);
26570 * Trim the input string
26571 * @param $str (string) string to trim
26572 * @param $replace (string) string that replace spaces.
26573 * @return trimmed string
26574 * @author Nicola Asuni
26575 * @public
26576 * @since 5.8.000 (2010-08-11)
26578 public function stringTrim($str, $replace='') {
26579 $str = $this->stringLeftTrim($str, $replace);
26580 $str = $this->stringRightTrim($str, $replace);
26581 return $str;
26585 * Return true if the current font is unicode type.
26586 * @return true for unicode font, false otherwise.
26587 * @author Nicola Asuni
26588 * @public
26589 * @since 5.8.002 (2010-08-14)
26591 public function isUnicodeFont() {
26592 return (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0'));
26596 * Return normalized font name
26597 * @param $fontfamily (string) property string containing font family names
26598 * @return string normalized font name
26599 * @author Nicola Asuni
26600 * @public
26601 * @since 5.8.004 (2010-08-17)
26603 public function getFontFamilyName($fontfamily) {
26604 // remove spaces and symbols
26605 $fontfamily = preg_replace('/[^a-z0-9\,]/', '', strtolower($fontfamily));
26606 // extract all font names
26607 $fontslist = preg_split('/[,]/', $fontfamily);
26608 // find first valid font name
26609 foreach ($fontslist as $font) {
26610 // replace font variations
26611 $font = preg_replace('/italic$/', 'I', $font);
26612 $font = preg_replace('/oblique$/', 'I', $font);
26613 $font = preg_replace('/bold([I]?)$/', 'B\\1', $font);
26614 // replace common family names and core fonts
26615 $pattern = array();
26616 $replacement = array();
26617 $pattern[] = '/^serif|^cursive|^fantasy|^timesnewroman/';
26618 $replacement[] = 'times';
26619 $pattern[] = '/^sansserif/';
26620 $replacement[] = 'helvetica';
26621 $pattern[] = '/^monospace/';
26622 $replacement[] = 'courier';
26623 $font = preg_replace($pattern, $replacement, $font);
26624 if (in_array(strtolower($font), $this->fontlist) OR in_array($font, $this->fontkeys)) {
26625 return $font;
26628 // return current font as default
26629 return $this->CurrentFont['fontkey'];
26633 * Start a new XObject Template.
26634 * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
26635 * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
26636 * Note: X,Y coordinates will be reset to 0,0.
26637 * @param $w (int) Template width in user units (empty string or zero = page width less margins).
26638 * @param $h (int) Template height in user units (empty string or zero = page height less margins).
26639 * @param $group (mixed) Set transparency group. Can be a boolean value or an array specifying optional parameters: 'CS' (solour space name), 'I' (boolean flag to indicate isolated group) and 'K' (boolean flag to indicate knockout group).
26640 * @return int the XObject Template ID in case of success or false in case of error.
26641 * @author Nicola Asuni
26642 * @public
26643 * @since 5.8.017 (2010-08-24)
26644 * @see endTemplate(), printTemplate()
26646 public function startTemplate($w=0, $h=0, $group=false) {
26647 if ($this->inxobj) {
26648 // we are already inside an XObject template
26649 return false;
26651 $this->inxobj = true;
26652 ++$this->n;
26653 // XObject ID
26654 $this->xobjid = 'XT'.$this->n;
26655 // object ID
26656 $this->xobjects[$this->xobjid] = array('n' => $this->n);
26657 // store current graphic state
26658 $this->xobjects[$this->xobjid]['gvars'] = $this->getGraphicVars();
26659 // initialize data
26660 $this->xobjects[$this->xobjid]['intmrk'] = 0;
26661 $this->xobjects[$this->xobjid]['transfmrk'] = array();
26662 $this->xobjects[$this->xobjid]['outdata'] = '';
26663 $this->xobjects[$this->xobjid]['xobjects'] = array();
26664 $this->xobjects[$this->xobjid]['images'] = array();
26665 $this->xobjects[$this->xobjid]['fonts'] = array();
26666 $this->xobjects[$this->xobjid]['annotations'] = array();
26667 $this->xobjects[$this->xobjid]['extgstates'] = array();
26668 $this->xobjects[$this->xobjid]['gradients'] = array();
26669 $this->xobjects[$this->xobjid]['spot_colors'] = array();
26670 // set new environment
26671 $this->num_columns = 1;
26672 $this->current_column = 0;
26673 $this->SetAutoPageBreak(false);
26674 if (($w === '') OR ($w <= 0)) {
26675 $w = $this->w - $this->lMargin - $this->rMargin;
26677 if (($h === '') OR ($h <= 0)) {
26678 $h = $this->h - $this->tMargin - $this->bMargin;
26680 $this->xobjects[$this->xobjid]['x'] = 0;
26681 $this->xobjects[$this->xobjid]['y'] = 0;
26682 $this->xobjects[$this->xobjid]['w'] = $w;
26683 $this->xobjects[$this->xobjid]['h'] = $h;
26684 $this->w = $w;
26685 $this->h = $h;
26686 $this->wPt = $this->w * $this->k;
26687 $this->hPt = $this->h * $this->k;
26688 $this->fwPt = $this->wPt;
26689 $this->fhPt = $this->hPt;
26690 $this->x = 0;
26691 $this->y = 0;
26692 $this->lMargin = 0;
26693 $this->rMargin = 0;
26694 $this->tMargin = 0;
26695 $this->bMargin = 0;
26696 // set group mode
26697 $this->xobjects[$this->xobjid]['group'] = $group;
26698 return $this->xobjid;
26702 * End the current XObject Template started with startTemplate() and restore the previous graphic state.
26703 * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
26704 * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
26705 * @return int the XObject Template ID in case of success or false in case of error.
26706 * @author Nicola Asuni
26707 * @public
26708 * @since 5.8.017 (2010-08-24)
26709 * @see startTemplate(), printTemplate()
26711 public function endTemplate() {
26712 if (!$this->inxobj) {
26713 // we are not inside a template
26714 return false;
26716 $this->inxobj = false;
26717 // restore previous graphic state
26718 $this->setGraphicVars($this->xobjects[$this->xobjid]['gvars'], true);
26719 return $this->xobjid;
26723 * Print an XObject Template.
26724 * You can print an XObject Template inside the currently opened Template.
26725 * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
26726 * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
26727 * @param $id (string) The ID of XObject Template to print.
26728 * @param $x (int) X position in user units (empty string = current x position)
26729 * @param $y (int) Y position in user units (empty string = current y position)
26730 * @param $w (int) Width in user units (zero = remaining page width)
26731 * @param $h (int) Height in user units (zero = remaining page height)
26732 * @param $align (string) Indicates the alignment of the pointer next to template insertion relative to template height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
26733 * @param $palign (string) Allows to center or align the template on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
26734 * @param $fitonpage (boolean) If true the template is resized to not exceed page dimensions.
26735 * @author Nicola Asuni
26736 * @public
26737 * @since 5.8.017 (2010-08-24)
26738 * @see startTemplate(), endTemplate()
26740 public function printTemplate($id, $x='', $y='', $w=0, $h=0, $align='', $palign='', $fitonpage=false) {
26741 if (!isset($this->xobjects[$id])) {
26742 $this->Error('The XObject Template \''.$id.'\' doesn\'t exist!');
26744 if ($this->inxobj) {
26745 if ($id == $this->xobjid) {
26746 // close current template
26747 $this->endTemplate();
26748 } else {
26749 // use the template as resource for the template currently opened
26750 $this->xobjects[$this->xobjid]['xobjects'][$id] = $this->xobjects[$id];
26753 // set default values
26754 if ($x === '') {
26755 $x = $this->x;
26757 if ($y === '') {
26758 $y = $this->y;
26760 // check page for no-write regions and adapt page margins if necessary
26761 list($x, $y) = $this->checkPageRegions($h, $x, $y);
26762 $ow = $this->xobjects[$id]['w'];
26763 $oh = $this->xobjects[$id]['h'];
26764 // calculate template width and height on document
26765 if (($w <= 0) AND ($h <= 0)) {
26766 $w = $ow;
26767 $h = $oh;
26768 } elseif ($w <= 0) {
26769 $w = $h * $ow / $oh;
26770 } elseif ($h <= 0) {
26771 $h = $w * $oh / $ow;
26773 // fit the template on available space
26774 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
26775 // set page alignment
26776 $rb_y = $y + $h;
26777 // set alignment
26778 if ($this->rtl) {
26779 if ($palign == 'L') {
26780 $xt = $this->lMargin;
26781 } elseif ($palign == 'C') {
26782 $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
26783 } elseif ($palign == 'R') {
26784 $xt = $this->w - $this->rMargin - $w;
26785 } else {
26786 $xt = $x - $w;
26788 $rb_x = $xt;
26789 } else {
26790 if ($palign == 'L') {
26791 $xt = $this->lMargin;
26792 } elseif ($palign == 'C') {
26793 $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
26794 } elseif ($palign == 'R') {
26795 $xt = $this->w - $this->rMargin - $w;
26796 } else {
26797 $xt = $x;
26799 $rb_x = $xt + $w;
26801 // print XObject Template + Transformation matrix
26802 $this->StartTransform();
26803 // translate and scale
26804 $sx = ($w / $this->xobjects[$id]['w']);
26805 $sy = ($h / $this->xobjects[$id]['h']);
26806 $tm = array();
26807 $tm[0] = $sx;
26808 $tm[1] = 0;
26809 $tm[2] = 0;
26810 $tm[3] = $sy;
26811 $tm[4] = $xt * $this->k;
26812 $tm[5] = ($this->h - $h - $y) * $this->k;
26813 $this->Transform($tm);
26814 // set object
26815 $this->_out('/'.$id.' Do');
26816 $this->StopTransform();
26817 // add annotations
26818 if (!empty($this->xobjects[$id]['annotations'])) {
26819 foreach ($this->xobjects[$id]['annotations'] as $annot) {
26820 // transform original coordinates
26821 $coordlt = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, ($annot['x'] * $this->k), (-$annot['y'] * $this->k)));
26822 $ax = ($coordlt[4] / $this->k);
26823 $ay = ($this->h - $h - ($coordlt[5] / $this->k));
26824 $coordrb = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, (($annot['x'] + $annot['w']) * $this->k), ((-$annot['y'] - $annot['h']) * $this->k)));
26825 $aw = ($coordrb[4] / $this->k) - $ax;
26826 $ah = ($this->h - $h - ($coordrb[5] / $this->k)) - $ay;
26827 $this->Annotation($ax, $ay, $aw, $ah, $annot['text'], $annot['opt'], $annot['spaces']);
26830 // set pointer to align the next text/objects
26831 switch($align) {
26832 case 'T': {
26833 $this->y = $y;
26834 $this->x = $rb_x;
26835 break;
26837 case 'M': {
26838 $this->y = $y + round($h/2);
26839 $this->x = $rb_x;
26840 break;
26842 case 'B': {
26843 $this->y = $rb_y;
26844 $this->x = $rb_x;
26845 break;
26847 case 'N': {
26848 $this->SetY($rb_y);
26849 break;
26851 default:{
26852 break;
26858 * Set the percentage of character stretching.
26859 * @param $perc (int) percentage of stretching (100 = no stretching)
26860 * @author Nicola Asuni
26861 * @public
26862 * @since 5.9.000 (2010-09-29)
26864 public function setFontStretching($perc=100) {
26865 $this->font_stretching = $perc;
26869 * Get the percentage of character stretching.
26870 * @return float stretching value
26871 * @author Nicola Asuni
26872 * @public
26873 * @since 5.9.000 (2010-09-29)
26875 public function getFontStretching() {
26876 return $this->font_stretching;
26880 * Set the amount to increase or decrease the space between characters in a text.
26881 * @param $spacing (float) amount to increase or decrease the space between characters in a text (0 = default spacing)
26882 * @author Nicola Asuni
26883 * @public
26884 * @since 5.9.000 (2010-09-29)
26886 public function setFontSpacing($spacing=0) {
26887 $this->font_spacing = $spacing;
26891 * Get the amount to increase or decrease the space between characters in a text.
26892 * @return int font spacing (tracking/kerning) value
26893 * @author Nicola Asuni
26894 * @public
26895 * @since 5.9.000 (2010-09-29)
26897 public function getFontSpacing() {
26898 return $this->font_spacing;
26902 * Return an array of no-write page regions
26903 * @return array of no-write page regions
26904 * @author Nicola Asuni
26905 * @public
26906 * @since 5.9.003 (2010-10-13)
26907 * @see setPageRegions(), addPageRegion()
26909 public function getPageRegions() {
26910 return $this->page_regions;
26914 * Set no-write regions on page.
26915 * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
26916 * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
26917 * You can set multiple regions for the same page.
26918 * @param $regions (array) array of no-write regions. For each region you can define an array as follow: ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right). Omit this parameter to remove all regions.
26919 * @author Nicola Asuni
26920 * @public
26921 * @since 5.9.003 (2010-10-13)
26922 * @see addPageRegion(), getPageRegions()
26924 public function setPageRegions($regions=array()) {
26925 // empty current regions array
26926 $this->page_regions = array();
26927 // add regions
26928 foreach ($regions as $data) {
26929 $this->addPageRegion($data);
26934 * Add a single no-write region on selected page.
26935 * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
26936 * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
26937 * You can set multiple regions for the same page.
26938 * @param $region (array) array of a single no-write region array: ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right).
26939 * @author Nicola Asuni
26940 * @public
26941 * @since 5.9.003 (2010-10-13)
26942 * @see setPageRegions(), getPageRegions()
26944 public function addPageRegion($region) {
26945 if (!isset($region['page']) OR empty($region['page'])) {
26946 $region['page'] = $this->page;
26948 if (isset($region['xt']) AND isset($region['xb']) AND ($region['xt'] > 0) AND ($region['xb'] > 0)
26949 AND isset($region['yt']) AND isset($region['yb']) AND ($region['yt'] >= 0) AND ($region['yt'] < $region['yb'])
26950 AND isset($region['side']) AND (($region['side'] == 'L') OR ($region['side'] == 'R'))) {
26951 $this->page_regions[] = $region;
26956 * Remove a single no-write region.
26957 * @param $key (int) region key
26958 * @author Nicola Asuni
26959 * @public
26960 * @since 5.9.003 (2010-10-13)
26961 * @see setPageRegions(), getPageRegions()
26963 public function removePageRegion($key) {
26964 if (isset($this->page_regions[$key])) {
26965 unset($this->page_regions[$key]);
26970 * Check page for no-write regions and adapt current coordinates and page margins if necessary.
26971 * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
26972 * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
26973 * @param $h (float) height of the text/image/object to print in user units
26974 * @param $x (float) current X coordinate in user units
26975 * @param $y (float) current Y coordinate in user units
26976 * @return array($x, $y)
26977 * @author Nicola Asuni
26978 * @protected
26979 * @since 5.9.003 (2010-10-13)
26981 protected function checkPageRegions($h, $x, $y) {
26982 // set default values
26983 if ($x === '') {
26984 $x = $this->x;
26986 if ($y === '') {
26987 $y = $this->y;
26989 if (empty($this->page_regions)) {
26990 // no page regions defined
26991 return array($x, $y);
26993 if (empty($h)) {
26994 $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
26996 // check for page break
26997 if ($this->checkPageBreak($h, $y)) {
26998 // the content will be printed on a new page
26999 $x = $this->x;
27000 $y = $this->y;
27002 if ($this->num_columns > 1) {
27003 if ($this->rtl) {
27004 $this->lMargin = $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
27005 } else {
27006 $this->rMargin = $this->w - $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
27008 } else {
27009 if ($this->rtl) {
27010 $this->lMargin = $this->original_lMargin;
27011 } else {
27012 $this->rMargin = $this->original_rMargin;
27015 // adjust coordinates and page margins
27016 foreach ($this->page_regions as $regid => $regdata) {
27017 if ($regdata['page'] == $this->page) {
27018 // check region boundaries
27019 if (($y > ($regdata['yt'] - $h)) AND ($y <= $regdata['yb'])) {
27020 // Y is inside the region
27021 $minv = ($regdata['xb'] - $regdata['xt']) / ($regdata['yb'] - $regdata['yt']); // inverse of angular coefficient
27022 $yt = max($y, $regdata['yt']);
27023 $yb = min(($yt + $h), $regdata['yb']);
27024 $xt = (($yt - $regdata['yt']) * $minv) + $regdata['xt'];
27025 $xb = (($yb - $regdata['yt']) * $minv) + $regdata['xt'];
27026 if ($regdata['side'] == 'L') { // left side
27027 $new_margin = max($xt, $xb);
27028 if ($this->lMargin < $new_margin) {
27029 if ($this->rtl) {
27030 // adjust left page margin
27031 $this->lMargin = $new_margin;
27033 if ($x < $new_margin) {
27034 // adjust x position
27035 $x = $new_margin;
27036 if ($new_margin > ($this->w - $this->rMargin)) {
27037 // adjust y position
27038 $y = $regdata['yb'] - $h;
27042 } elseif ($regdata['side'] == 'R') { // right side
27043 $new_margin = min($xt, $xb);
27044 if (($this->w - $this->rMargin) > $new_margin) {
27045 if (!$this->rtl) {
27046 // adjust right page margin
27047 $this->rMargin = ($this->w - $new_margin);
27049 if ($x > $new_margin) {
27050 // adjust x position
27051 $x = $new_margin;
27052 if ($new_margin > $this->lMargin) {
27053 // adjust y position
27054 $y = $regdata['yb'] - $h;
27062 return array($x, $y);
27065 // --- SVG METHODS ---------------------------------------------------------
27068 * Embedd a Scalable Vector Graphics (SVG) image.
27069 * NOTE: SVG standard is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
27070 * @param $file (string) Name of the SVG file or a '@' character followed by the SVG data string.
27071 * @param $x (float) Abscissa of the upper-left corner.
27072 * @param $y (float) Ordinate of the upper-left corner.
27073 * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
27074 * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
27075 * @param $link (mixed) URL or identifier returned by AddLink().
27076 * @param $align (string) Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul> If the alignment is an empty string, then the pointer will be restored on the starting SVG position.
27077 * @param $palign (string) Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
27078 * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
27079 * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
27080 * @author Nicola Asuni
27081 * @since 5.0.000 (2010-05-02)
27082 * @public
27084 public function ImageSVG($file, $x='', $y='', $w=0, $h=0, $link='', $align='', $palign='', $border=0, $fitonpage=false) {
27085 if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
27086 // convert SVG to raster image using GD or ImageMagick libraries
27087 return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
27089 if ($file{0} === '@') { // image from string
27090 $this->svgdir = '';
27091 $svgdata = substr($file, 1);
27092 } else { // SVG file
27093 $this->svgdir = dirname($file);
27094 $svgdata = file_get_contents($file);
27096 if ($svgdata === false) {
27097 $this->Error('SVG file not found: '.$file);
27099 if ($x === '') {
27100 $x = $this->x;
27102 if ($y === '') {
27103 $y = $this->y;
27105 // check page for no-write regions and adapt page margins if necessary
27106 list($x, $y) = $this->checkPageRegions($h, $x, $y);
27107 $k = $this->k;
27108 $ox = 0;
27109 $oy = 0;
27110 $ow = $w;
27111 $oh = $h;
27112 $aspect_ratio_align = 'xMidYMid';
27113 $aspect_ratio_ms = 'meet';
27114 $regs = array();
27115 // get original image width and height
27116 preg_match('/<svg([^\>]*)>/si', $svgdata, $regs);
27117 if (isset($regs[1]) AND !empty($regs[1])) {
27118 $tmp = array();
27119 if (preg_match('/[\s]+x[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
27120 $ox = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
27122 $tmp = array();
27123 if (preg_match('/[\s]+y[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
27124 $oy = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
27126 $tmp = array();
27127 if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
27128 $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
27130 $tmp = array();
27131 if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
27132 $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
27134 $tmp = array();
27135 $view_box = array();
27136 if (preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.\-]+)[\s]+([0-9\.\-]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
27137 if (count($tmp) == 5) {
27138 array_shift($tmp);
27139 foreach ($tmp as $key => $val) {
27140 $view_box[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
27142 $ox = $view_box[0];
27143 $oy = $view_box[1];
27145 // get aspect ratio
27146 $tmp = array();
27147 if (preg_match('/[\s]+preserveAspectRatio[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
27148 $aspect_ratio = preg_split('/[\s]+/si', $tmp[1]);
27149 switch (count($aspect_ratio)) {
27150 case 3: {
27151 $aspect_ratio_align = $aspect_ratio[1];
27152 $aspect_ratio_ms = $aspect_ratio[2];
27153 break;
27155 case 2: {
27156 $aspect_ratio_align = $aspect_ratio[0];
27157 $aspect_ratio_ms = $aspect_ratio[1];
27158 break;
27160 case 1: {
27161 $aspect_ratio_align = $aspect_ratio[0];
27162 $aspect_ratio_ms = 'meet';
27163 break;
27169 // calculate image width and height on document
27170 if (($w <= 0) AND ($h <= 0)) {
27171 // convert image size to document unit
27172 $w = $ow;
27173 $h = $oh;
27174 } elseif ($w <= 0) {
27175 $w = $h * $ow / $oh;
27176 } elseif ($h <= 0) {
27177 $h = $w * $oh / $ow;
27179 // fit the image on available space
27180 list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
27181 if ($this->rasterize_vector_images) {
27182 // convert SVG to raster image using GD or ImageMagick libraries
27183 return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
27185 // set alignment
27186 $this->img_rb_y = $y + $h;
27187 // set alignment
27188 if ($this->rtl) {
27189 if ($palign == 'L') {
27190 $ximg = $this->lMargin;
27191 } elseif ($palign == 'C') {
27192 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
27193 } elseif ($palign == 'R') {
27194 $ximg = $this->w - $this->rMargin - $w;
27195 } else {
27196 $ximg = $x - $w;
27198 $this->img_rb_x = $ximg;
27199 } else {
27200 if ($palign == 'L') {
27201 $ximg = $this->lMargin;
27202 } elseif ($palign == 'C') {
27203 $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
27204 } elseif ($palign == 'R') {
27205 $ximg = $this->w - $this->rMargin - $w;
27206 } else {
27207 $ximg = $x;
27209 $this->img_rb_x = $ximg + $w;
27211 // store current graphic vars
27212 $gvars = $this->getGraphicVars();
27213 // store SVG position and scale factors
27214 $svgoffset_x = ($ximg - $ox) * $this->k;
27215 $svgoffset_y = -($y - $oy) * $this->k;
27216 if (isset($view_box[2]) AND ($view_box[2] > 0) AND ($view_box[3] > 0)) {
27217 $ow = $view_box[2];
27218 $oh = $view_box[3];
27219 } else {
27220 if ($ow <= 0) {
27221 $ow = $w;
27223 if ($oh <= 0) {
27224 $oh = $h;
27227 $svgscale_x = $w / $ow;
27228 $svgscale_y = $h / $oh;
27229 // scaling and alignment
27230 if ($aspect_ratio_align != 'none') {
27231 // store current scaling values
27232 $svgscale_old_x = $svgscale_x;
27233 $svgscale_old_y = $svgscale_y;
27234 // force uniform scaling
27235 if ($aspect_ratio_ms == 'slice') {
27236 // the entire viewport is covered by the viewBox
27237 if ($svgscale_x > $svgscale_y) {
27238 $svgscale_y = $svgscale_x;
27239 } elseif ($svgscale_x < $svgscale_y) {
27240 $svgscale_x = $svgscale_y;
27242 } else { // meet
27243 // the entire viewBox is visible within the viewport
27244 if ($svgscale_x < $svgscale_y) {
27245 $svgscale_y = $svgscale_x;
27246 } elseif ($svgscale_x > $svgscale_y) {
27247 $svgscale_x = $svgscale_y;
27250 // correct X alignment
27251 switch (substr($aspect_ratio_align, 1, 3)) {
27252 case 'Min': {
27253 // do nothing
27254 break;
27256 case 'Max': {
27257 $svgoffset_x += (($w * $this->k) - ($ow * $this->k * $svgscale_x));
27258 break;
27260 default:
27261 case 'Mid': {
27262 $svgoffset_x += ((($w * $this->k) - ($ow * $this->k * $svgscale_x)) / 2);
27263 break;
27266 // correct Y alignment
27267 switch (substr($aspect_ratio_align, 5)) {
27268 case 'Min': {
27269 // do nothing
27270 break;
27272 case 'Max': {
27273 $svgoffset_y -= (($h * $this->k) - ($oh * $this->k * $svgscale_y));
27274 break;
27276 default:
27277 case 'Mid': {
27278 $svgoffset_y -= ((($h * $this->k) - ($oh * $this->k * $svgscale_y)) / 2);
27279 break;
27283 // store current page break mode
27284 $page_break_mode = $this->AutoPageBreak;
27285 $page_break_margin = $this->getBreakMargin();
27286 $cell_padding = $this->cell_padding;
27287 $this->SetCellPadding(0);
27288 $this->SetAutoPageBreak(false);
27289 // save the current graphic state
27290 $this->_out('q'.$this->epsmarker);
27291 // set initial clipping mask
27292 $this->Rect($x, $y, $w, $h, 'CNZ', array(), array());
27293 // scale and translate
27294 $e = $ox * $this->k * (1 - $svgscale_x);
27295 $f = ($this->h - $oy) * $this->k * (1 - $svgscale_y);
27296 $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $svgscale_x, 0, 0, $svgscale_y, $e + $svgoffset_x, $f + $svgoffset_y));
27297 // creates a new XML parser to be used by the other XML functions
27298 $this->parser = xml_parser_create('UTF-8');
27299 // the following function allows to use parser inside object
27300 xml_set_object($this->parser, $this);
27301 // disable case-folding for this XML parser
27302 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
27303 // sets the element handler functions for the XML parser
27304 xml_set_element_handler($this->parser, 'startSVGElementHandler', 'endSVGElementHandler');
27305 // sets the character data handler function for the XML parser
27306 xml_set_character_data_handler($this->parser, 'segSVGContentHandler');
27307 // start parsing an XML document
27308 if (!xml_parse($this->parser, $svgdata)) {
27309 $error_message = sprintf('SVG Error: %s at line %d', xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser));
27310 $this->Error($error_message);
27312 // free this XML parser
27313 xml_parser_free($this->parser);
27314 // restore previous graphic state
27315 $this->_out($this->epsmarker.'Q');
27316 // restore graphic vars
27317 $this->setGraphicVars($gvars);
27318 $this->lasth = $gvars['lasth'];
27319 if (!empty($border)) {
27320 $bx = $this->x;
27321 $by = $this->y;
27322 $this->x = $ximg;
27323 if ($this->rtl) {
27324 $this->x += $w;
27326 $this->y = $y;
27327 $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
27328 $this->x = $bx;
27329 $this->y = $by;
27331 if ($link) {
27332 $this->Link($ximg, $y, $w, $h, $link, 0);
27334 // set pointer to align the next text/objects
27335 switch($align) {
27336 case 'T':{
27337 $this->y = $y;
27338 $this->x = $this->img_rb_x;
27339 break;
27341 case 'M':{
27342 $this->y = $y + round($h/2);
27343 $this->x = $this->img_rb_x;
27344 break;
27346 case 'B':{
27347 $this->y = $this->img_rb_y;
27348 $this->x = $this->img_rb_x;
27349 break;
27351 case 'N':{
27352 $this->SetY($this->img_rb_y);
27353 break;
27355 default:{
27356 // restore pointer to starting position
27357 $this->x = $gvars['x'];
27358 $this->y = $gvars['y'];
27359 $this->page = $gvars['page'];
27360 $this->current_column = $gvars['current_column'];
27361 $this->tMargin = $gvars['tMargin'];
27362 $this->bMargin = $gvars['bMargin'];
27363 $this->w = $gvars['w'];
27364 $this->h = $gvars['h'];
27365 $this->wPt = $gvars['wPt'];
27366 $this->hPt = $gvars['hPt'];
27367 $this->fwPt = $gvars['fwPt'];
27368 $this->fhPt = $gvars['fhPt'];
27369 break;
27372 $this->endlinex = $this->img_rb_x;
27373 // restore page break
27374 $this->SetAutoPageBreak($page_break_mode, $page_break_margin);
27375 $this->cell_padding = $cell_padding;
27379 * Get the tranformation matrix from SVG transform attribute
27380 * @param $attribute (string) transformation
27381 * @return array of transformations
27382 * @author Nicola Asuni
27383 * @since 5.0.000 (2010-05-02)
27384 * @protected
27386 protected function getSVGTransformMatrix($attribute) {
27387 // identity matrix
27388 $tm = array(1, 0, 0, 1, 0, 0);
27389 $transform = array();
27390 if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
27391 foreach ($transform as $key => $data) {
27392 if (!empty($data[2])) {
27393 $a = 1;
27394 $b = 0;
27395 $c = 0;
27396 $d = 1;
27397 $e = 0;
27398 $f = 0;
27399 $regs = array();
27400 switch ($data[1]) {
27401 case 'matrix': {
27402 if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27403 $a = $regs[1];
27404 $b = $regs[2];
27405 $c = $regs[3];
27406 $d = $regs[4];
27407 $e = $regs[5];
27408 $f = $regs[6];
27410 break;
27412 case 'translate': {
27413 if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27414 $e = $regs[1];
27415 $f = $regs[2];
27416 } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27417 $e = $regs[1];
27419 break;
27421 case 'scale': {
27422 if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27423 $a = $regs[1];
27424 $d = $regs[2];
27425 } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27426 $a = $regs[1];
27427 $d = $a;
27429 break;
27431 case 'rotate': {
27432 if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
27433 $ang = deg2rad($regs[1]);
27434 $x = $regs[2];
27435 $y = $regs[3];
27436 $a = cos($ang);
27437 $b = sin($ang);
27438 $c = -$b;
27439 $d = $a;
27440 $e = ($x * (1 - $a)) - ($y * $c);
27441 $f = ($y * (1 - $d)) - ($x * $b);
27442 } elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
27443 $ang = deg2rad($regs[1]);
27444 $a = cos($ang);
27445 $b = sin($ang);
27446 $c = -$b;
27447 $d = $a;
27448 $e = 0;
27449 $f = 0;
27451 break;
27453 case 'skewX': {
27454 if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
27455 $c = tan(deg2rad($regs[1]));
27457 break;
27459 case 'skewY': {
27460 if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
27461 $b = tan(deg2rad($regs[1]));
27463 break;
27466 $tm = $this->getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
27470 return $tm;
27474 * Get the product of two SVG tranformation matrices
27475 * @param $ta (array) first SVG tranformation matrix
27476 * @param $tb (array) second SVG tranformation matrix
27477 * @return transformation array
27478 * @author Nicola Asuni
27479 * @since 5.0.000 (2010-05-02)
27480 * @protected
27482 protected function getTransformationMatrixProduct($ta, $tb) {
27483 $tm = array();
27484 $tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
27485 $tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
27486 $tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
27487 $tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
27488 $tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
27489 $tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
27490 return $tm;
27494 * Convert SVG transformation matrix to PDF.
27495 * @param $tm (array) original SVG transformation matrix
27496 * @return array transformation matrix
27497 * @protected
27498 * @since 5.0.000 (2010-05-02)
27500 protected function convertSVGtMatrix($tm) {
27501 $a = $tm[0];
27502 $b = -$tm[1];
27503 $c = -$tm[2];
27504 $d = $tm[3];
27505 $e = $this->getHTMLUnitToUnits($tm[4], 1, $this->svgunit, false) * $this->k;
27506 $f = -$this->getHTMLUnitToUnits($tm[5], 1, $this->svgunit, false) * $this->k;
27507 $x = 0;
27508 $y = $this->h * $this->k;
27509 $e = ($x * (1 - $a)) - ($y * $c) + $e;
27510 $f = ($y * (1 - $d)) - ($x * $b) + $f;
27511 return array($a, $b, $c, $d, $e, $f);
27515 * Apply SVG graphic transformation matrix.
27516 * @param $tm (array) original SVG transformation matrix
27517 * @protected
27518 * @since 5.0.000 (2010-05-02)
27520 protected function SVGTransform($tm) {
27521 $this->Transform($this->convertSVGtMatrix($tm));
27525 * Apply the requested SVG styles (*** TO BE COMPLETED ***)
27526 * @param $svgstyle (array) array of SVG styles to apply
27527 * @param $prevsvgstyle (array) array of previous SVG style
27528 * @param $x (int) X origin of the bounding box
27529 * @param $y (int) Y origin of the bounding box
27530 * @param $w (int) width of the bounding box
27531 * @param $h (int) height of the bounding box
27532 * @param $clip_function (string) clip function
27533 * @param $clip_params (array) array of parameters for clipping function
27534 * @return object style
27535 * @author Nicola Asuni
27536 * @since 5.0.000 (2010-05-02)
27537 * @protected
27539 protected function setSVGStyles($svgstyle, $prevsvgstyle, $x=0, $y=0, $w=1, $h=1, $clip_function='', $clip_params=array()) {
27540 $objstyle = '';
27541 $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
27542 if (!isset($svgstyle['opacity'])) {
27543 return $objstyle;
27545 // clip-path
27546 $regs = array();
27547 if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['clip-path'], $regs)) {
27548 $clip_path = $this->svgclippaths[$regs[1]];
27549 foreach ($clip_path as $cp) {
27550 $this->startSVGElementHandler('clip-path', $cp['name'], $cp['attribs'], $cp['tm']);
27553 // opacity
27554 if ($svgstyle['opacity'] != 1) {
27555 $this->SetAlpha($svgstyle['opacity']);
27557 // color
27558 $fill_color = $this->convertHTMLColorToDec($svgstyle['color']);
27559 $this->SetFillColorArray($fill_color);
27560 // text color
27561 $text_color = $this->convertHTMLColorToDec($svgstyle['text-color']);
27562 $this->SetTextColorArray($text_color);
27563 // clip
27564 if (preg_match('/rect\(([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)\)/si', $svgstyle['clip'], $regs)) {
27565 $top = (isset($regs[1])?$this->getHTMLUnitToUnits($regs[1], 0, $this->svgunit, false):0);
27566 $right = (isset($regs[2])?$this->getHTMLUnitToUnits($regs[2], 0, $this->svgunit, false):0);
27567 $bottom = (isset($regs[3])?$this->getHTMLUnitToUnits($regs[3], 0, $this->svgunit, false):0);
27568 $left = (isset($regs[4])?$this->getHTMLUnitToUnits($regs[4], 0, $this->svgunit, false):0);
27569 $cx = $x + $left;
27570 $cy = $y + $top;
27571 $cw = $w - $left - $right;
27572 $ch = $h - $top - $bottom;
27573 if ($svgstyle['clip-rule'] == 'evenodd') {
27574 $clip_rule = 'CNZ';
27575 } else {
27576 $clip_rule = 'CEO';
27578 $this->Rect($cx, $cy, $cw, $ch, $clip_rule, array(), array());
27580 // fill
27581 $regs = array();
27582 if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['fill'], $regs)) {
27583 // gradient
27584 $gradient = $this->svggradients[$regs[1]];
27585 if (isset($gradient['xref'])) {
27586 // reference to another gradient definition
27587 $newgradient = $this->svggradients[$gradient['xref']];
27588 $newgradient['coords'] = $gradient['coords'];
27589 $newgradient['mode'] = $gradient['mode'];
27590 $newgradient['gradientUnits'] = $gradient['gradientUnits'];
27591 if (isset($gradient['gradientTransform'])) {
27592 $newgradient['gradientTransform'] = $gradient['gradientTransform'];
27594 $gradient = $newgradient;
27596 //save current Graphic State
27597 $this->_out('q');
27598 //set clipping area
27599 if (!empty($clip_function) AND method_exists($this, $clip_function)) {
27600 $bbox = call_user_func_array(array($this, $clip_function), $clip_params);
27601 if (is_array($bbox) AND (count($bbox) == 4)) {
27602 list($x, $y, $w, $h) = $bbox;
27605 if ($gradient['mode'] == 'measure') {
27606 if (isset($gradient['gradientTransform']) AND !empty($gradient['gradientTransform'])) {
27607 $gtm = $gradient['gradientTransform'];
27608 // apply transformation matrix
27609 $xa = ($gtm[0] * $gradient['coords'][0]) + ($gtm[2] * $gradient['coords'][1]) + $gtm[4];
27610 $ya = ($gtm[1] * $gradient['coords'][0]) + ($gtm[3] * $gradient['coords'][1]) + $gtm[5];
27611 $xb = ($gtm[0] * $gradient['coords'][2]) + ($gtm[2] * $gradient['coords'][3]) + $gtm[4];
27612 $yb = ($gtm[1] * $gradient['coords'][2]) + ($gtm[3] * $gradient['coords'][3]) + $gtm[5];
27613 if (isset($gradient['coords'][4])) {
27614 $gradient['coords'][4] = sqrt(pow(($gtm[0] * $gradient['coords'][4]), 2) + pow(($gtm[1] * $gradient['coords'][4]), 2));
27616 $gradient['coords'][0] = $xa;
27617 $gradient['coords'][1] = $ya;
27618 $gradient['coords'][2] = $xb;
27619 $gradient['coords'][3] = $yb;
27621 // convert SVG coordinates to user units
27622 $gradient['coords'][0] = $this->getHTMLUnitToUnits($gradient['coords'][0], 0, $this->svgunit, false);
27623 $gradient['coords'][1] = $this->getHTMLUnitToUnits($gradient['coords'][1], 0, $this->svgunit, false);
27624 $gradient['coords'][2] = $this->getHTMLUnitToUnits($gradient['coords'][2], 0, $this->svgunit, false);
27625 $gradient['coords'][3] = $this->getHTMLUnitToUnits($gradient['coords'][3], 0, $this->svgunit, false);
27626 if (isset($gradient['coords'][4])) {
27627 $gradient['coords'][4] = $this->getHTMLUnitToUnits($gradient['coords'][4], 0, $this->svgunit, false);
27629 // shift units
27630 if ($gradient['gradientUnits'] == 'objectBoundingBox') {
27631 // convert to SVG coordinate system
27632 $gradient['coords'][0] += $x;
27633 $gradient['coords'][1] += $y;
27634 $gradient['coords'][2] += $x;
27635 $gradient['coords'][3] += $y;
27637 if ($w <= $minlen) {
27638 $w = $minlen;
27640 if ($h <= $minlen) {
27641 $h = $minlen;
27643 // calculate percentages
27644 $gradient['coords'][0] = ($gradient['coords'][0] - $x) / $w;
27645 $gradient['coords'][1] = ($gradient['coords'][1] - $y) / $h;
27646 $gradient['coords'][2] = ($gradient['coords'][2] - $x) / $w;
27647 $gradient['coords'][3] = ($gradient['coords'][3] - $y) / $h;
27648 if (isset($gradient['coords'][4])) {
27649 $gradient['coords'][4] /= $w;
27651 } elseif ($gradient['mode'] == 'percentage') {
27652 foreach($gradient['coords'] as $key => $val) {
27653 $gradient['coords'][$key] = (intval($val) / 100);
27656 // fix values
27657 foreach($gradient['coords'] as $key => $val) {
27658 if ($val < 0) {
27659 $gradient['coords'][$key] = 0;
27660 } elseif ($val > 1) {
27661 $gradient['coords'][$key] = 1;
27664 if (($gradient['type'] == 2) AND ($gradient['coords'][0] == $gradient['coords'][2]) AND ($gradient['coords'][1] == $gradient['coords'][3])) {
27665 // single color (no shading)
27666 $gradient['coords'][0] = 1;
27667 $gradient['coords'][1] = 0;
27668 $gradient['coords'][2] = 0.999;
27669 $gradient['coords'][3] = 0;
27671 // swap Y coordinates
27672 $tmp = $gradient['coords'][1];
27673 $gradient['coords'][1] = $gradient['coords'][3];
27674 $gradient['coords'][3] = $tmp;
27675 // set transformation map for gradient
27676 if ($gradient['type'] == 3) {
27677 // gradient is always circular
27678 $cy = $this->h - $y - ($gradient['coords'][1] * ($w + $h));
27679 $this->_out(sprintf('%.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $w*$this->k, $x*$this->k, $cy*$this->k));
27680 } else {
27681 $this->_out(sprintf('%.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k));
27683 if (count($gradient['stops']) > 1) {
27684 $this->Gradient($gradient['type'], $gradient['coords'], $gradient['stops'], array(), false);
27686 } elseif ($svgstyle['fill'] != 'none') {
27687 $fill_color = $this->convertHTMLColorToDec($svgstyle['fill']);
27688 if ($svgstyle['fill-opacity'] != 1) {
27689 $this->SetAlpha($svgstyle['fill-opacity']);
27691 $this->SetFillColorArray($fill_color);
27692 if ($svgstyle['fill-rule'] == 'evenodd') {
27693 $objstyle .= 'F*';
27694 } else {
27695 $objstyle .= 'F';
27698 // stroke
27699 if ($svgstyle['stroke'] != 'none') {
27700 $stroke_style = array(
27701 'color' => $this->convertHTMLColorToDec($svgstyle['stroke']),
27702 'width' => $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false),
27703 'cap' => $svgstyle['stroke-linecap'],
27704 'join' => $svgstyle['stroke-linejoin']
27706 if (isset($svgstyle['stroke-dasharray']) AND !empty($svgstyle['stroke-dasharray']) AND ($svgstyle['stroke-dasharray'] != 'none')) {
27707 $stroke_style['dash'] = $svgstyle['stroke-dasharray'];
27709 $this->SetLineStyle($stroke_style);
27710 $objstyle .= 'D';
27712 // font
27713 $regs = array();
27714 if (!empty($svgstyle['font'])) {
27715 if (preg_match('/font-family[\s]*:[\s]*([^\;\"]*)/si', $svgstyle['font'], $regs)) {
27716 $font_family = $this->getFontFamilyName($regs[1]);
27717 } else {
27718 $font_family = $svgstyle['font-family'];
27720 if (preg_match('/font-size[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
27721 $font_size = trim($regs[1]);
27722 } else {
27723 $font_size = $svgstyle['font-size'];
27725 if (preg_match('/font-style[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
27726 $font_style = trim($regs[1]);
27727 } else {
27728 $font_style = $svgstyle['font-style'];
27730 if (preg_match('/font-weight[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
27731 $font_weight = trim($regs[1]);
27732 } else {
27733 $font_weight = $svgstyle['font-weight'];
27735 if (preg_match('/font-stretch[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
27736 $font_stretch = trim($regs[1]);
27737 } else {
27738 $font_stretch = $svgstyle['font-stretch'];
27740 if (preg_match('/letter-spacing[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
27741 $font_spacing = trim($regs[1]);
27742 } else {
27743 $font_spacing = $svgstyle['letter-spacing'];
27745 } else {
27746 $font_family = $this->getFontFamilyName($svgstyle['font-family']);
27747 $font_size = $svgstyle['font-size'];
27748 $font_style = $svgstyle['font-style'];
27749 $font_weight = $svgstyle['font-weight'];
27750 $font_stretch = $svgstyle['font-stretch'];
27751 $font_spacing = $svgstyle['letter-spacing'];
27753 $font_size = $this->getHTMLUnitToUnits($font_size, $prevsvgstyle['font-size'], $this->svgunit, false) * $this->k;
27754 $font_stretch = $this->getCSSFontStretching($font_stretch, $svgstyle['font-stretch']);
27755 $font_spacing = $this->getCSSFontSpacing($font_spacing, $svgstyle['letter-spacing']);
27756 switch ($font_style) {
27757 case 'italic': {
27758 $font_style = 'I';
27759 break;
27761 case 'oblique': {
27762 $font_style = 'I';
27763 break;
27765 default:
27766 case 'normal': {
27767 $font_style = '';
27768 break;
27771 switch ($font_weight) {
27772 case 'bold':
27773 case 'bolder': {
27774 $font_style .= 'B';
27775 break;
27778 switch ($svgstyle['text-decoration']) {
27779 case 'underline': {
27780 $font_style .= 'U';
27781 break;
27783 case 'overline': {
27784 $font_style .= 'O';
27785 break;
27787 case 'line-through': {
27788 $font_style .= 'D';
27789 break;
27791 default:
27792 case 'none': {
27793 break;
27796 $this->SetFont($font_family, $font_style, $font_size);
27797 $this->setFontStretching($font_stretch);
27798 $this->setFontSpacing($font_spacing);
27799 return $objstyle;
27803 * Draws an SVG path
27804 * @param $d (string) attribute d of the path SVG element
27805 * @param $style (string) Style of rendering. Possible values are:
27806 * <ul>
27807 * <li>D or empty string: Draw (default).</li>
27808 * <li>F: Fill.</li>
27809 * <li>F*: Fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
27810 * <li>DF or FD: Draw and fill.</li>
27811 * <li>DF* or FD*: Draw and fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
27812 * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
27813 * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
27814 * </ul>
27815 * @return array of container box measures (x, y, w, h)
27816 * @author Nicola Asuni
27817 * @since 5.0.000 (2010-05-02)
27818 * @protected
27820 protected function SVGPath($d, $style='') {
27821 // set fill/stroke style
27822 $op = $this->getPathPaintOperator($style, '');
27823 if (empty($op)) {
27824 return;
27826 $paths = array();
27827 $d = preg_replace('/([0-9ACHLMQSTVZ])([\-\+])/si', '\\1 \\2', $d);
27828 preg_match_all('/([ACHLMQSTVZ])[\s]*([^ACHLMQSTVZ\"]*)/si', $d, $paths, PREG_SET_ORDER);
27829 $x = 0;
27830 $y = 0;
27831 $x1 = 0;
27832 $y1 = 0;
27833 $x2 = 0;
27834 $y2 = 0;
27835 $xmin = 2147483647;
27836 $xmax = 0;
27837 $ymin = 2147483647;
27838 $ymax = 0;
27839 $relcoord = false;
27840 $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
27841 $firstcmd = true; // used to print first point
27842 // draw curve pieces
27843 foreach ($paths as $key => $val) {
27844 // get curve type
27845 $cmd = trim($val[1]);
27846 if (strtolower($cmd) == $cmd) {
27847 // use relative coordinated instead of absolute
27848 $relcoord = true;
27849 $xoffset = $x;
27850 $yoffset = $y;
27851 } else {
27852 $relcoord = false;
27853 $xoffset = 0;
27854 $yoffset = 0;
27856 $params = array();
27857 if (isset($val[2])) {
27858 // get curve parameters
27859 $rawparams = preg_split('/([\,\s]+)/si', trim($val[2]));
27860 $params = array();
27861 foreach ($rawparams as $ck => $cp) {
27862 $params[$ck] = $this->getHTMLUnitToUnits($cp, 0, $this->svgunit, false);
27863 if (abs($params[$ck]) < $minlen) {
27864 // aproximate little values to zero
27865 $params[$ck] = 0;
27869 // store current origin point
27870 $x0 = $x;
27871 $y0 = $y;
27872 switch (strtoupper($cmd)) {
27873 case 'M': { // moveto
27874 foreach ($params as $ck => $cp) {
27875 if (($ck % 2) == 0) {
27876 $x = $cp + $xoffset;
27877 } else {
27878 $y = $cp + $yoffset;
27879 if ($firstcmd OR (abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
27880 if ($ck == 1) {
27881 $this->_outPoint($x, $y);
27882 $firstcmd = false;
27883 } else {
27884 $this->_outLine($x, $y);
27887 $xmin = min($xmin, $x);
27888 $ymin = min($ymin, $y);
27889 $xmax = max($xmax, $x);
27890 $ymax = max($ymax, $y);
27891 if ($relcoord) {
27892 $xoffset = $x;
27893 $yoffset = $y;
27897 break;
27899 case 'L': { // lineto
27900 foreach ($params as $ck => $cp) {
27901 if (($ck % 2) == 0) {
27902 $x = $cp + $xoffset;
27903 } else {
27904 $y = $cp + $yoffset;
27905 if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
27906 $this->_outLine($x, $y);
27908 $xmin = min($xmin, $x);
27909 $ymin = min($ymin, $y);
27910 $xmax = max($xmax, $x);
27911 $ymax = max($ymax, $y);
27912 if ($relcoord) {
27913 $xoffset = $x;
27914 $yoffset = $y;
27918 break;
27920 case 'H': { // horizontal lineto
27921 foreach ($params as $ck => $cp) {
27922 $x = $cp + $xoffset;
27923 if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
27924 $this->_outLine($x, $y);
27926 $xmin = min($xmin, $x);
27927 $xmax = max($xmax, $x);
27928 if ($relcoord) {
27929 $xoffset = $x;
27932 break;
27934 case 'V': { // vertical lineto
27935 foreach ($params as $ck => $cp) {
27936 $y = $cp + $yoffset;
27937 if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
27938 $this->_outLine($x, $y);
27940 $ymin = min($ymin, $y);
27941 $ymax = max($ymax, $y);
27942 if ($relcoord) {
27943 $yoffset = $y;
27946 break;
27948 case 'C': { // curveto
27949 foreach ($params as $ck => $cp) {
27950 $params[$ck] = $cp;
27951 if ((($ck + 1) % 6) == 0) {
27952 $x1 = $params[($ck - 5)] + $xoffset;
27953 $y1 = $params[($ck - 4)] + $yoffset;
27954 $x2 = $params[($ck - 3)] + $xoffset;
27955 $y2 = $params[($ck - 2)] + $yoffset;
27956 $x = $params[($ck - 1)] + $xoffset;
27957 $y = $params[($ck)] + $yoffset;
27958 $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
27959 $xmin = min($xmin, $x, $x1, $x2);
27960 $ymin = min($ymin, $y, $y1, $y2);
27961 $xmax = max($xmax, $x, $x1, $x2);
27962 $ymax = max($ymax, $y, $y1, $y2);
27963 if ($relcoord) {
27964 $xoffset = $x;
27965 $yoffset = $y;
27969 break;
27971 case 'S': { // shorthand/smooth curveto
27972 foreach ($params as $ck => $cp) {
27973 $params[$ck] = $cp;
27974 if ((($ck + 1) % 4) == 0) {
27975 if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'C') OR (strtoupper($paths[($key - 1)][1]) == 'S'))) {
27976 $x1 = (2 * $x) - $x2;
27977 $y1 = (2 * $y) - $y2;
27978 } else {
27979 $x1 = $x;
27980 $y1 = $y;
27982 $x2 = $params[($ck - 3)] + $xoffset;
27983 $y2 = $params[($ck - 2)] + $yoffset;
27984 $x = $params[($ck - 1)] + $xoffset;
27985 $y = $params[($ck)] + $yoffset;
27986 $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
27987 $xmin = min($xmin, $x, $x1, $x2);
27988 $ymin = min($ymin, $y, $y1, $y2);
27989 $xmax = max($xmax, $x, $x1, $x2);
27990 $ymax = max($ymax, $y, $y1, $y2);
27991 if ($relcoord) {
27992 $xoffset = $x;
27993 $yoffset = $y;
27997 break;
27999 case 'Q': { // quadratic Bézier curveto
28000 foreach ($params as $ck => $cp) {
28001 $params[$ck] = $cp;
28002 if ((($ck + 1) % 4) == 0) {
28003 // convert quadratic points to cubic points
28004 $x1 = $params[($ck - 3)] + $xoffset;
28005 $y1 = $params[($ck - 2)] + $yoffset;
28006 $xa = ($x + (2 * $x1)) / 3;
28007 $ya = ($y + (2 * $y1)) / 3;
28008 $x = $params[($ck - 1)] + $xoffset;
28009 $y = $params[($ck)] + $yoffset;
28010 $xb = ($x + (2 * $x1)) / 3;
28011 $yb = ($y + (2 * $y1)) / 3;
28012 $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
28013 $xmin = min($xmin, $x, $xa, $xb);
28014 $ymin = min($ymin, $y, $ya, $yb);
28015 $xmax = max($xmax, $x, $xa, $xb);
28016 $ymax = max($ymax, $y, $ya, $yb);
28017 if ($relcoord) {
28018 $xoffset = $x;
28019 $yoffset = $y;
28023 break;
28025 case 'T': { // shorthand/smooth quadratic Bézier curveto
28026 foreach ($params as $ck => $cp) {
28027 $params[$ck] = $cp;
28028 if (($ck % 2) != 0) {
28029 if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'Q') OR (strtoupper($paths[($key - 1)][1]) == 'T'))) {
28030 $x1 = (2 * $x) - $x1;
28031 $y1 = (2 * $y) - $y1;
28032 } else {
28033 $x1 = $x;
28034 $y1 = $y;
28036 // convert quadratic points to cubic points
28037 $xa = ($x + (2 * $x1)) / 3;
28038 $ya = ($y + (2 * $y1)) / 3;
28039 $x = $params[($ck - 1)] + $xoffset;
28040 $y = $params[($ck)] + $yoffset;
28041 $xb = ($x + (2 * $x1)) / 3;
28042 $yb = ($y + (2 * $y1)) / 3;
28043 $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
28044 $xmin = min($xmin, $x, $xa, $xb);
28045 $ymin = min($ymin, $y, $ya, $yb);
28046 $xmax = max($xmax, $x, $xa, $xb);
28047 $ymax = max($ymax, $y, $ya, $yb);
28048 if ($relcoord) {
28049 $xoffset = $x;
28050 $yoffset = $y;
28054 break;
28056 case 'A': { // elliptical arc
28057 foreach ($params as $ck => $cp) {
28058 $params[$ck] = $cp;
28059 if ((($ck + 1) % 7) == 0) {
28060 $x0 = $x;
28061 $y0 = $y;
28062 $rx = abs($params[($ck - 6)]);
28063 $ry = abs($params[($ck - 5)]);
28064 $ang = -$rawparams[($ck - 4)];
28065 $angle = deg2rad($ang);
28066 $fa = $rawparams[($ck - 3)]; // large-arc-flag
28067 $fs = $rawparams[($ck - 2)]; // sweep-flag
28068 $x = $params[($ck - 1)] + $xoffset;
28069 $y = $params[$ck] + $yoffset;
28070 if ((abs($x0 - $x) < $minlen) AND (abs($y0 - $y) < $minlen)) {
28071 // endpoints are almost identical
28072 $xmin = min($xmin, $x);
28073 $ymin = min($ymin, $y);
28074 $xmax = max($xmax, $x);
28075 $ymax = max($ymax, $y);
28076 } else {
28077 $cos_ang = cos($angle);
28078 $sin_ang = sin($angle);
28079 $a = (($x0 - $x) / 2);
28080 $b = (($y0 - $y) / 2);
28081 $xa = ($a * $cos_ang) - ($b * $sin_ang);
28082 $ya = ($a * $sin_ang) + ($b * $cos_ang);
28083 $rx2 = $rx * $rx;
28084 $ry2 = $ry * $ry;
28085 $xa2 = $xa * $xa;
28086 $ya2 = $ya * $ya;
28087 $delta = ($xa2 / $rx2) + ($ya2 / $ry2);
28088 if ($delta > 1) {
28089 $rx *= sqrt($delta);
28090 $ry *= sqrt($delta);
28091 $rx2 = $rx * $rx;
28092 $ry2 = $ry * $ry;
28094 $numerator = (($rx2 * $ry2) - ($rx2 * $ya2) - ($ry2 * $xa2));
28095 if ($numerator < 0) {
28096 $root = 0;
28097 } else {
28098 $root = sqrt($numerator / (($rx2 * $ya2) + ($ry2 * $xa2)));
28100 if ($fa == $fs){
28101 $root *= -1;
28103 $cax = $root * (($rx * $ya) / $ry);
28104 $cay = -$root * (($ry * $xa) / $rx);
28105 // coordinates of ellipse center
28106 $cx = ($cax * $cos_ang) - ($cay * $sin_ang) + (($x0 + $x) / 2);
28107 $cy = ($cax * $sin_ang) + ($cay * $cos_ang) + (($y0 + $y) / 2);
28108 // get angles
28109 $angs = $this->getVectorsAngle(1, 0, (($xa - $cax) / $rx), (($cay - $ya) / $ry));
28110 $dang = $this->getVectorsAngle((($xa - $cax) / $rx), (($ya - $cay) / $ry), ((-$xa - $cax) / $rx), ((-$ya - $cay) / $ry));
28111 if (($fs == 0) AND ($dang > 0)) {
28112 $dang -= (2 * M_PI);
28113 } elseif (($fs == 1) AND ($dang < 0)) {
28114 $dang += (2 * M_PI);
28116 $angf = $angs - $dang;
28117 if ((($fs == 0) AND ($angs > $angf)) OR (($fs == 1) AND ($angs < $angf))) {
28118 // reverse angles
28119 $tmp = $angs;
28120 $angs = $angf;
28121 $angf = $tmp;
28123 $angs = round(rad2deg($angs), 6);
28124 $angf = round(rad2deg($angf), 6);
28125 // covent angles to positive values
28126 if (($angs < 0) AND ($angf < 0)) {
28127 $angs += 360;
28128 $angf += 360;
28130 $pie = false;
28131 if (($key == 0) AND (isset($paths[($key + 1)][1])) AND (trim($paths[($key + 1)][1]) == 'z')) {
28132 $pie = true;
28134 list($axmin, $aymin, $axmax, $aymax) = $this->_outellipticalarc($cx, $cy, $rx, $ry, $ang, $angs, $angf, $pie, 2, false, ($fs == 0), true);
28135 $xmin = min($xmin, $x, $axmin);
28136 $ymin = min($ymin, $y, $aymin);
28137 $xmax = max($xmax, $x, $axmax);
28138 $ymax = max($ymax, $y, $aymax);
28140 if ($relcoord) {
28141 $xoffset = $x;
28142 $yoffset = $y;
28146 break;
28148 case 'Z': {
28149 $this->_out('h');
28150 break;
28153 $firstcmd = false;
28154 } // end foreach
28155 if (!empty($op)) {
28156 $this->_out($op);
28158 return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
28162 * Returns the angle in radiants between two vectors
28163 * @param $x1 (int) X coordinate of first vector point
28164 * @param $y1 (int) Y coordinate of first vector point
28165 * @param $x2 (int) X coordinate of second vector point
28166 * @param $y2 (int) Y coordinate of second vector point
28167 * @author Nicola Asuni
28168 * @since 5.0.000 (2010-05-04)
28169 * @protected
28171 protected function getVectorsAngle($x1, $y1, $x2, $y2) {
28172 $dprod = ($x1 * $x2) + ($y1 * $y2);
28173 $dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
28174 $dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
28175 $angle = acos($dprod / ($dist1 * $dist2));
28176 if (is_nan($angle)) {
28177 $angle = M_PI;
28179 if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
28180 $angle *= -1;
28182 return $angle;
28186 * Sets the opening SVG element handler function for the XML parser. (*** TO BE COMPLETED ***)
28187 * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
28188 * @param $name (string) The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
28189 * @param $attribs (array) The third parameter, attribs, contains an associative array with the element's attributes (if any). The keys of this array are the attribute names, the values are the attribute values. Attribute names are case-folded on the same criteria as element names. Attribute values are not case-folded. The original order of the attributes can be retrieved by walking through attribs the normal way, using each(). The first key in the array was the first attribute, and so on.
28190 * @param $ctm (array) tranformation matrix for clipping mode (starting transformation matrix).
28191 * @author Nicola Asuni
28192 * @since 5.0.000 (2010-05-02)
28193 * @protected
28195 protected function startSVGElementHandler($parser, $name, $attribs, $ctm=array()) {
28196 // check if we are in clip mode
28197 if ($this->svgclipmode) {
28198 $this->svgclippaths[$this->svgclipid][] = array('name' => $name, 'attribs' => $attribs, 'tm' => $this->svgcliptm[$this->svgclipid]);
28199 return;
28201 if ($this->svgdefsmode AND !in_array($name, array('clipPath', 'linearGradient', 'radialGradient', 'stop'))) {
28202 if (!isset($attribs['id'])) {
28203 $attribs['id'] = 'DF_'.(count($this->svgdefs) + 1);
28205 $this->svgdefs[$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
28206 return;
28208 $clipping = false;
28209 if ($parser == 'clip-path') {
28210 // set clipping mode
28211 $clipping = true;
28213 // get styling properties
28214 $prev_svgstyle = $this->svgstyles[(count($this->svgstyles) - 1)]; // previous style
28215 $svgstyle = $this->svgstyles[0]; // set default style
28216 if (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
28217 // fix style for regular expression
28218 $attribs['style'] = ';'.$attribs['style'];
28220 foreach ($prev_svgstyle as $key => $val) {
28221 if (in_array($key, $this->svginheritprop)) {
28222 // inherit previous value
28223 $svgstyle[$key] = $val;
28225 if (isset($attribs[$key]) AND !$this->empty_string($attribs[$key])) {
28226 // specific attribute settings
28227 if ($attribs[$key] == 'inherit') {
28228 $svgstyle[$key] = $val;
28229 } else {
28230 $svgstyle[$key] = $attribs[$key];
28232 } elseif (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
28233 // CSS style syntax
28234 $attrval = array();
28235 if (preg_match('/[;\"\s]{1}'.$key.'[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval) AND isset($attrval[1])) {
28236 if ($attrval[1] == 'inherit') {
28237 $svgstyle[$key] = $val;
28238 } else {
28239 $svgstyle[$key] = $attrval[1];
28244 // transformation matrix
28245 if (!empty($ctm)) {
28246 $tm = $ctm;
28247 } else {
28248 $tm = $this->svgstyles[(count($this->svgstyles) - 1)]['transfmatrix'];
28250 if (isset($attribs['transform']) AND !empty($attribs['transform'])) {
28251 $tm = $this->getTransformationMatrixProduct($tm, $this->getSVGTransformMatrix($attribs['transform']));
28253 $svgstyle['transfmatrix'] = $tm;
28254 $invisible = false;
28255 if (($svgstyle['visibility'] == 'hidden') OR ($svgstyle['visibility'] == 'collapse') OR ($svgstyle['display'] == 'none')) {
28256 // the current graphics element is invisible (nothing is painted)
28257 $invisible = true;
28259 // process tag
28260 switch($name) {
28261 case 'defs': {
28262 $this->svgdefsmode = true;
28263 break;
28265 // clipPath
28266 case 'clipPath': {
28267 if ($invisible) {
28268 break;
28270 $this->svgclipmode = true;
28271 if (!isset($attribs['id'])) {
28272 $attribs['id'] = 'CP_'.(count($this->svgcliptm) + 1);
28274 $this->svgclipid = $attribs['id'];
28275 $this->svgclippaths[$this->svgclipid] = array();
28276 $this->svgcliptm[$this->svgclipid] = $tm;
28277 break;
28279 case 'svg': {
28280 // start of SVG object
28281 break;
28283 case 'g': {
28284 // group together related graphics elements
28285 array_push($this->svgstyles, $svgstyle);
28286 $this->StartTransform();
28287 $this->setSVGStyles($svgstyle, $prev_svgstyle);
28288 break;
28290 case 'linearGradient': {
28291 if ($this->pdfa_mode) {
28292 break;
28294 if (!isset($attribs['id'])) {
28295 $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
28297 $this->svggradientid = $attribs['id'];
28298 $this->svggradients[$this->svggradientid] = array();
28299 $this->svggradients[$this->svggradientid]['type'] = 2;
28300 $this->svggradients[$this->svggradientid]['stops'] = array();
28301 if (isset($attribs['gradientUnits'])) {
28302 $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
28303 } else {
28304 $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
28306 //$attribs['spreadMethod']
28307 $x1 = (isset($attribs['x1'])?$attribs['x1']:'0%');
28308 $y1 = (isset($attribs['y1'])?$attribs['y1']:'0%');
28309 $x2 = (isset($attribs['x2'])?$attribs['x2']:'100%');
28310 $y2 = (isset($attribs['y2'])?$attribs['y2']:'0%');
28311 if (substr($x1, -1) != '%') {
28312 $this->svggradients[$this->svggradientid]['mode'] = 'measure';
28313 } else {
28314 $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
28316 if (isset($attribs['gradientTransform'])) {
28317 $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
28319 $this->svggradients[$this->svggradientid]['coords'] = array($x1, $y1, $x2, $y2);
28320 if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
28321 // gradient is defined on another place
28322 $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
28324 break;
28326 case 'radialGradient': {
28327 if ($this->pdfa_mode) {
28328 break;
28330 if (!isset($attribs['id'])) {
28331 $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
28333 $this->svggradientid = $attribs['id'];
28334 $this->svggradients[$this->svggradientid] = array();
28335 $this->svggradients[$this->svggradientid]['type'] = 3;
28336 $this->svggradients[$this->svggradientid]['stops'] = array();
28337 if (isset($attribs['gradientUnits'])) {
28338 $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
28339 } else {
28340 $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
28342 //$attribs['spreadMethod']
28343 $cx = (isset($attribs['cx'])?$attribs['cx']:0.5);
28344 $cy = (isset($attribs['cy'])?$attribs['cy']:0.5);
28345 $fx = (isset($attribs['fx'])?$attribs['fx']:$cx);
28346 $fy = (isset($attribs['fy'])?$attribs['fy']:$cy);
28347 $r = (isset($attribs['r'])?$attribs['r']:0.5);
28348 if (isset($attribs['cx']) AND (substr($attribs['cx'], -1) != '%')) {
28349 $this->svggradients[$this->svggradientid]['mode'] = 'measure';
28350 } else {
28351 $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
28353 if (isset($attribs['gradientTransform'])) {
28354 $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
28356 $this->svggradients[$this->svggradientid]['coords'] = array($cx, $cy, $fx, $fy, $r);
28357 if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
28358 // gradient is defined on another place
28359 $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
28361 break;
28363 case 'stop': {
28364 // gradient stops
28365 if (substr($attribs['offset'], -1) == '%') {
28366 $offset = floatval(substr($attribs['offset'], -1)) / 100;
28367 } else {
28368 $offset = floatval($attribs['offset']);
28369 if ($offset > 1) {
28370 $offset /= 100;
28373 $stop_color = isset($svgstyle['stop-color'])?$this->convertHTMLColorToDec($svgstyle['stop-color']):'black';
28374 $opacity = isset($svgstyle['stop-opacity'])?$svgstyle['stop-opacity']:1;
28375 $this->svggradients[$this->svggradientid]['stops'][] = array('offset' => $offset, 'color' => $stop_color, 'opacity' => $opacity);
28376 break;
28378 // paths
28379 case 'path': {
28380 if ($invisible) {
28381 break;
28383 if (isset($attribs['d'])) {
28384 $d = trim($attribs['d']);
28385 if (!empty($d)) {
28386 if ($clipping) {
28387 $this->SVGTransform($tm);
28388 $this->SVGPath($d, 'CNZ');
28389 } else {
28390 $this->StartTransform();
28391 $this->SVGTransform($tm);
28392 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, 0, 0, 1, 1, 'SVGPath', array($d, 'CNZ'));
28393 if (!empty($obstyle)) {
28394 $this->SVGPath($d, $obstyle);
28396 $this->StopTransform();
28400 break;
28402 // shapes
28403 case 'rect': {
28404 if ($invisible) {
28405 break;
28407 $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
28408 $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
28409 $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
28410 $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
28411 $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
28412 $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):$rx);
28413 if ($clipping) {
28414 $this->SVGTransform($tm);
28415 $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ', array(), array());
28416 } else {
28417 $this->StartTransform();
28418 $this->SVGTransform($tm);
28419 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'RoundedRectXY', array($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ'));
28420 if (!empty($obstyle)) {
28421 $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', $obstyle, array(), array());
28423 $this->StopTransform();
28425 break;
28427 case 'circle': {
28428 if ($invisible) {
28429 break;
28431 $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
28432 $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
28433 $r = (isset($attribs['r'])?$this->getHTMLUnitToUnits($attribs['r'], 0, $this->svgunit, false):0);
28434 $x = $cx - $r;
28435 $y = $cy - $r;
28436 $w = 2 * $r;
28437 $h = $w;
28438 if ($clipping) {
28439 $this->SVGTransform($tm);
28440 $this->Circle($cx, $cy, $r, 0, 360, 'CNZ', array(), array(), 8);
28441 } else {
28442 $this->StartTransform();
28443 $this->SVGTransform($tm);
28444 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Circle', array($cx, $cy, $r, 0, 360, 'CNZ'));
28445 if (!empty($obstyle)) {
28446 $this->Circle($cx, $cy, $r, 0, 360, $obstyle, array(), array(), 8);
28448 $this->StopTransform();
28450 break;
28452 case 'ellipse': {
28453 if ($invisible) {
28454 break;
28456 $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
28457 $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
28458 $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
28459 $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):0);
28460 $x = $cx - $rx;
28461 $y = $cy - $ry;
28462 $w = 2 * $rx;
28463 $h = 2 * $ry;
28464 if ($clipping) {
28465 $this->SVGTransform($tm);
28466 $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ', array(), array(), 8);
28467 } else {
28468 $this->StartTransform();
28469 $this->SVGTransform($tm);
28470 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Ellipse', array($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ'));
28471 if (!empty($obstyle)) {
28472 $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, $obstyle, array(), array(), 8);
28474 $this->StopTransform();
28476 break;
28478 case 'line': {
28479 if ($invisible) {
28480 break;
28482 $x1 = (isset($attribs['x1'])?$this->getHTMLUnitToUnits($attribs['x1'], 0, $this->svgunit, false):0);
28483 $y1 = (isset($attribs['y1'])?$this->getHTMLUnitToUnits($attribs['y1'], 0, $this->svgunit, false):0);
28484 $x2 = (isset($attribs['x2'])?$this->getHTMLUnitToUnits($attribs['x2'], 0, $this->svgunit, false):0);
28485 $y2 = (isset($attribs['y2'])?$this->getHTMLUnitToUnits($attribs['y2'], 0, $this->svgunit, false):0);
28486 $x = $x1;
28487 $y = $y1;
28488 $w = abs($x2 - $x1);
28489 $h = abs($y2 - $y1);
28490 if (!$clipping) {
28491 $this->StartTransform();
28492 $this->SVGTransform($tm);
28493 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Line', array($x1, $y1, $x2, $y2));
28494 $this->Line($x1, $y1, $x2, $y2);
28495 $this->StopTransform();
28497 break;
28499 case 'polyline':
28500 case 'polygon': {
28501 if ($invisible) {
28502 break;
28504 $points = (isset($attribs['points'])?$attribs['points']:'0 0');
28505 $points = trim($points);
28506 // note that point may use a complex syntax not covered here
28507 $points = preg_split('/[\,\s]+/si', $points);
28508 if (count($points) < 4) {
28509 break;
28511 $p = array();
28512 $xmin = 2147483647;
28513 $xmax = 0;
28514 $ymin = 2147483647;
28515 $ymax = 0;
28516 foreach ($points as $key => $val) {
28517 $p[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
28518 if (($key % 2) == 0) {
28519 // X coordinate
28520 $xmin = min($xmin, $p[$key]);
28521 $xmax = max($xmax, $p[$key]);
28522 } else {
28523 // Y coordinate
28524 $ymin = min($ymin, $p[$key]);
28525 $ymax = max($ymax, $p[$key]);
28528 $x = $xmin;
28529 $y = $ymin;
28530 $w = ($xmax - $xmin);
28531 $h = ($ymax - $ymin);
28532 if ($name == 'polyline') {
28533 $this->StartTransform();
28534 $this->SVGTransform($tm);
28535 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'PolyLine', array($p, 'CNZ'));
28536 $this->PolyLine($p, 'D', array(), array());
28537 $this->StopTransform();
28538 } else { // polygon
28539 if ($clipping) {
28540 $this->SVGTransform($tm);
28541 $this->Polygon($p, 'CNZ', array(), array(), true);
28542 } else {
28543 $this->StartTransform();
28544 $this->SVGTransform($tm);
28545 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Polygon', array($p, 'CNZ'));
28546 if (!empty($obstyle)) {
28547 $this->Polygon($p, $obstyle, array(), array(), true);
28549 $this->StopTransform();
28552 break;
28554 // image
28555 case 'image': {
28556 if ($invisible) {
28557 break;
28559 if (!isset($attribs['xlink:href']) OR empty($attribs['xlink:href'])) {
28560 break;
28562 $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
28563 $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
28564 $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
28565 $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
28566 $img = $attribs['xlink:href'];
28567 if (!$clipping) {
28568 $this->StartTransform();
28569 $this->SVGTransform($tm);
28570 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h);
28571 // fix image path
28572 if (!$this->empty_string($this->svgdir) AND (($img{0} == '.') OR (basename($img) == $img))) {
28573 // replace relative path with full server path
28574 $img = $this->svgdir.'/'.$img;
28576 if (($img[0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
28577 $findroot = strpos($img, $_SERVER['DOCUMENT_ROOT']);
28578 if (($findroot === false) OR ($findroot > 1)) {
28579 if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
28580 $img = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$img;
28581 } else {
28582 $img = $_SERVER['DOCUMENT_ROOT'].$img;
28586 $img = urldecode($img);
28587 $testscrtype = @parse_url($img);
28588 if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
28589 // convert URL to server path
28590 $img = str_replace(K_PATH_URL, K_PATH_MAIN, $img);
28592 $this->Image($img, $x, $y, $w, $h);
28593 $this->StopTransform();
28595 break;
28597 // text
28598 case 'text':
28599 case 'tspan': {
28600 $this->svgtextmode['invisible'] = $invisible;
28601 if ($invisible) {
28602 break;
28604 array_push($this->svgstyles, $svgstyle);
28605 // only basic support - advanced features must be implemented
28606 $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):$this->x);
28607 $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):$this->y);
28608 $svgstyle['text-color'] = $svgstyle['fill'];
28609 $this->svgtext = '';
28610 if (isset($svgstyle['text-anchor'])) {
28611 $this->svgtextmode['text-anchor'] = $svgstyle['text-anchor'];
28612 } else {
28613 $this->svgtextmode['text-anchor'] = 'start';
28615 if (isset($svgstyle['direction'])) {
28616 if ($svgstyle['direction'] == 'rtl') {
28617 $this->svgtextmode['rtl'] = true;
28618 } else {
28619 $this->svgtextmode['rtl'] = false;
28621 } else {
28622 $this->svgtextmode['rtl'] = false;
28624 if (isset($svgstyle['stroke']) AND ($svgstyle['stroke'] != 'none') AND isset($svgstyle['stroke-width']) AND ($svgstyle['stroke-width'] > 0)) {
28625 $this->svgtextmode['stroke'] = $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false);
28626 } else {
28627 $this->svgtextmode['stroke'] = false;
28629 $this->StartTransform();
28630 $this->SVGTransform($tm);
28631 $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, 1, 1);
28632 $this->x = $x;
28633 $this->y = $y;
28634 break;
28636 // use
28637 case 'use': {
28638 if (isset($attribs['xlink:href'])) {
28639 $use = $this->svgdefs[substr($attribs['xlink:href'], 1)];
28640 if (isset($attribs['xlink:href'])) {
28641 unset($attribs['xlink:href']);
28643 if (isset($attribs['id'])) {
28644 unset($attribs['id']);
28646 $attribs = array_merge($use['attribs'], $attribs);
28647 $this->startSVGElementHandler($parser, $use['name'], $use['attribs']);
28649 break;
28651 default: {
28652 break;
28654 } // end of switch
28658 * Sets the closing SVG element handler function for the XML parser.
28659 * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
28660 * @param $name (string) The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
28661 * @author Nicola Asuni
28662 * @since 5.0.000 (2010-05-02)
28663 * @protected
28665 protected function endSVGElementHandler($parser, $name) {
28666 switch($name) {
28667 case 'defs': {
28668 $this->svgdefsmode = false;
28669 break;
28671 // clipPath
28672 case 'clipPath': {
28673 $this->svgclipmode = false;
28674 break;
28676 case 'g': {
28677 // ungroup: remove last style from array
28678 array_pop($this->svgstyles);
28679 $this->StopTransform();
28680 break;
28682 case 'text':
28683 case 'tspan': {
28684 if ($this->svgtextmode['invisible']) {
28685 // This implementation must be fixed to following the rule:
28686 // If the 'visibility' property is set to hidden on a 'tspan', 'tref' or 'altGlyph' element, then the text is invisible but still takes up space in text layout calculations.
28687 break;
28689 // print text
28690 $text = $this->stringTrim($this->svgtext);
28691 if ($this->svgtextmode['text-anchor'] != 'start') {
28692 $textlen = $this->GetStringWidth($text);
28693 // check if string is RTL text
28694 if ($this->svgtextmode['text-anchor'] == 'end') {
28695 if ($this->svgtextmode['rtl']) {
28696 $this->x += $textlen;
28697 } else {
28698 $this->x -= $textlen;
28700 } elseif ($this->svgtextmode['text-anchor'] == 'middle') {
28701 if ($this->svgtextmode['rtl']) {
28702 $this->x += ($textlen / 2);
28703 } else {
28704 $this->x -= ($textlen / 2);
28708 $textrendermode = $this->textrendermode;
28709 $textstrokewidth = $this->textstrokewidth;
28710 $this->setTextRenderingMode($this->svgtextmode['stroke'], true, false);
28711 $this->Cell(0, 0, $text, 0, 0, '', false, '', 0, false, 'L', 'T');
28712 // restore previous rendering mode
28713 $this->textrendermode = $textrendermode;
28714 $this->textstrokewidth = $textstrokewidth;
28715 $this->svgtext = '';
28716 $this->StopTransform();
28717 array_pop($this->svgstyles);
28718 break;
28720 default: {
28721 break;
28727 * Sets the character data handler function for the XML parser.
28728 * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
28729 * @param $data (string) The second parameter, data, contains the character data as a string.
28730 * @author Nicola Asuni
28731 * @since 5.0.000 (2010-05-02)
28732 * @protected
28734 protected function segSVGContentHandler($parser, $data) {
28735 $this->svgtext .= $data;
28738 // --- END SVG METHODS -----------------------------------------------------
28740 } // END OF TCPDF CLASS
28742 //============================================================+
28743 // END OF FILE
28744 //============================================================+