Moved check for plugin import/export to the start rather than the end
[moodle-linuxchix.git] / lib / tcpdf / tcpdf.php
blob17aa548d930c989973caf86ff175f7dc66b9142a
1 <?php
2 //============================================================+
3 // File name : tcpdf.php
4 // Begin : 2002-08-03
5 // Last Update : 2006-08-05
6 // Author : Nicola Asuni
7 // Version : 1.53.0.TC023_PHP4
8 // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
9 //
10 // Description : This is a PHP4 class for generating PDF files
11 // on-the-fly without requiring external
12 // extensions.
14 // IMPORTANT:
15 // This class is an extension and improvement of the public Domain
16 // FPDF class by Olivier Plathey (http://www.fpdf.org).
18 // Main changes by Nicola Asuni:
19 // PHP4 porting;
20 // UTF-8 Unicode support;
21 // code refactoring;
22 // source code clean up;
23 // code style and formatting;
24 // source code documentation using phpDocumentor (www.phpdoc.org);
25 // All ISO page formats were included;
26 // image scale factor;
27 // includes methods to parse and printsome XHTML code, supporting the following elements: h1, h2, h3, h4, h5, h6, b, u, i, a, img, p, br, strong, em, font, blockquote, li, ul, ol, hr, td, th, tr, table, sup, sub, small;
28 // includes a method to print various barcode formats using an improved version of "Generic Barcode Render Class" by Karim Mribti (http://www.mribti.com/barcode/) (require GD library: http://www.boutell.com/gd/);
29 // defines standard Header() and Footer() methods.
30 //============================================================+
32 /**
33 * include configuration file
35 require_once(dirname(__FILE__).'/config/tcpdf_config.php');
38 /**
39 * TCPDF Class.
40 * @package com.tecnick.tcpdf
43 /**
44 * This is a PHP4 class for generating PDF files on-the-fly without requiring external extensions.<br>
45 * TCPDF project (http://tcpdf.sourceforge.net) is based on the public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
46 * <h3>TCPDF main changes from FPDF are:</h3><ul>
47 * <li>PHP4 porting</li>
48 * <li>UTF-8 Unicode support</li>
49 * <li>source code clean up</li>
50 * <li>code style and formatting</li>
51 * <li>source code documentation using phpDocumentor (www.phpdoc.org)</li>
52 * <li>All ISO page formats were included</li>
53 * <li>image scale factor</li>
54 * <li>includes methods to parse and printsome XHTML code, supporting the following elements: h1, h2, h3, h4, h5, h6, b, u, i, a, img, p, br, strong, em, font, blockquote, li, ul, ol, hr, td, th, tr, table, sup, sub, small;</li>
55 * <li>includes a method to print various barcode formats using an improved version of "Generic Barcode Render Class" by Karim Mribti (http://www.mribti.com/barcode/) (require GD library: http://www.boutell.com/gd/)</li>
56 * <li>defines standard Header() and Footer() methods.</li>
57 * </ul>
58 * Tools to encode your unicode fonts are on fonts/ttf2ufm directory.</p>
59 * @name TCPDF
60 * @package com.tecnick.tcpdf
61 * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
62 * @author Nicola Asuni
63 * @copyright 2004-2006 Tecnick.com S.r.l (www.tecnick.com) Via Ugo Foscolo n.19 - 09045 Quartu Sant'Elena (CA) - ITALY - www.tecnick.com - info@tecnick.com
64 * @link http://tcpdf.sourceforge.net
65 * @license http://www.gnu.org/copyleft/lesser.html LGPL
66 @version 1.53.0.TC023_PHP4
69 if(!class_exists('TCPDF')) {
70 /**
71 * define default PDF document producer
72 */
73 define('PDF_PRODUCER','TCPDF 1.53.0.TC023_PHP4 (http://tcpdf.sourceforge.net)');
75 /**
76 * This is a PHP4 class for generating PDF files on-the-fly without requiring external extensions.<br>
77 * This class is an extension and improvement of the FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
78 * This version contains some changes: [porting to PHP4, support for UTF-8 Unicode, code style and formatting, php documentation (www.phpdoc.org), ISO page formats, minor improvements, image scale factor]<br>
79 * TCPDF project (http://tcpdf.sourceforge.net) is based on the public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
80 * To add your own TTF fonts please read /fonts/README.TXT
81 * @name TCPDF
82 * @package com.tecnick.tcpdf
83 * @version 1.53.0.TC023
84 * @author Nicola Asuni
85 * @link http://tcpdf.sourceforge.net
86 * @license http://www.gnu.org/copyleft/lesser.html LGPL
88 class TCPDF {
89 //var properties
91 /**
92 * @var current page number
93 * @access protected
95 var $page;
97 /**
98 * @var current object number
99 * @access protected
101 var $n;
104 * @var array of object offsets
105 * @access protected
107 var $offsets;
110 * @var buffer holding in-memory PDF
111 * @access protected
113 var $buffer;
116 * @var array containing pages
117 * @access protected
119 var $pages;
122 * @var current document state
123 * @access protected
125 var $state;
128 * @var compression flag
129 * @access protected
131 var $compress;
134 * @var default orientation
135 * @access protected
137 var $DefOrientation;
140 * @var current orientation
141 * @access protected
143 var $CurOrientation;
146 * @var array indicating orientation changes
147 * @access protected
149 var $OrientationChanges;
152 * @var scale factor (number of points in user unit)
153 * @access protected
155 var $k;
158 * @var width of page format in points
159 * @access protected
161 var $fwPt;
164 * @var height of page format in points
165 * @access protected
167 var $fhPt;
170 * @var width of page format in user unit
171 * @access protected
173 var $fw;
176 * @var height of page format in user unit
177 * @access protected
179 var $fh;
182 * @var current width of page in points
183 * @access protected
185 var $wPt;
188 * @var current height of page in points
189 * @access protected
191 var $hPt;
194 * @var current width of page in user unit
195 * @access protected
197 var $w;
200 * @var current height of page in user unit
201 * @access protected
203 var $h;
206 * @var left margin
207 * @access protected
209 var $lMargin;
212 * @var top margin
213 * @access protected
215 var $tMargin;
218 * @var right margin
219 * @access protected
221 var $rMargin;
224 * @var page break margin
225 * @access protected
227 var $bMargin;
230 * @var cell margin
231 * @access protected
233 var $cMargin;
236 * @var current horizontal position in user unit for cell positioning
237 * @access protected
239 var $x;
242 * @var current vertical position in user unit for cell positioning
243 * @access protected
245 var $y;
248 * @var height of last cell printed
249 * @access protected
251 var $lasth;
254 * @var line width in user unit
255 * @access protected
257 var $LineWidth;
260 * @var array of standard font names
261 * @access protected
263 var $CoreFonts;
266 * @var array of used fonts
267 * @access protected
269 var $fonts;
272 * @var array of font files
273 * @access protected
275 var $FontFiles;
278 * @var array of encoding differences
279 * @access protected
281 var $diffs;
284 * @var array of used images
285 * @access protected
287 var $images;
290 * @var array of links in pages
291 * @access protected
293 var $PageLinks;
296 * @var array of internal links
297 * @access protected
299 var $links;
302 * @var current font family
303 * @access protected
305 var $FontFamily;
308 * @var current font style
309 * @access protected
311 var $FontStyle;
314 * @var underlining flag
315 * @access protected
317 var $underline;
320 * @var current font info
321 * @access protected
323 var $CurrentFont;
326 * @var current font size in points
327 * @access protected
329 var $FontSizePt;
332 * @var current font size in user unit
333 * @access protected
335 var $FontSize;
338 * @var commands for drawing color
339 * @access protected
341 var $DrawColor;
344 * @var commands for filling color
345 * @access protected
347 var $FillColor;
350 * @var commands for text color
351 * @access protected
353 var $TextColor;
356 * @var indicates whether fill and text colors are different
357 * @access protected
359 var $ColorFlag;
362 * @var word spacing
363 * @access protected
365 var $ws;
368 * @var automatic page breaking
369 * @access protected
371 var $AutoPageBreak;
374 * @var threshold used to trigger page breaks
375 * @access protected
377 var $PageBreakTrigger;
380 * @var flag set when processing footer
381 * @access protected
383 var $InFooter;
386 * @var zoom display mode
387 * @access protected
389 var $ZoomMode;
392 * @var layout display mode
393 * @access protected
395 var $LayoutMode;
398 * @var title
399 * @access protected
401 var $title;
404 * @var subject
405 * @access protected
407 var $subject;
410 * @var author
411 * @access protected
413 var $author;
416 * @var keywords
417 * @access protected
419 var $keywords;
422 * @var creator
423 * @access protected
425 var $creator;
428 * @var alias for total number of pages
429 * @access protected
431 var $AliasNbPages;
434 * @var right-bottom corner X coordinate of inserted image
435 * @since 2002-07-31
436 * @author Nicola Asuni
437 * @access protected
439 var $img_rb_x;
442 * @var right-bottom corner Y coordinate of inserted image
443 * @since 2002-07-31
444 * @author Nicola Asuni
445 * @access protected
447 var $img_rb_y;
450 * @var image scale factor
451 * @since 2004-06-14
452 * @author Nicola Asuni
453 * @access protected
455 var $imgscale = 1;
458 * @var boolean set to true when the input text is unicode (require unicode fonts)
459 * @since 2005-01-02
460 * @author Nicola Asuni
461 * @access protected
463 var $isunicode = false;
466 * @var PDF version
467 * @since 1.5.3
468 * @access protected
470 var $PDFVersion = "1.3";
473 // ----------------------
476 * @var Minimum distance between header and top page margin.
477 * @access private
479 var $header_margin;
482 * @var Minimum distance between footer and bottom page margin.
483 * @access private
485 var $footer_margin;
488 * @var original left margin value
489 * @access private
490 * @since 1.53.0.TC013
492 var $original_lMargin;
495 * @var original right margin value
496 * @access private
497 * @since 1.53.0.TC013
499 var $original_rMargin;
502 * @var Header font.
503 * @access private
505 var $header_font;
508 * @var Footer font.
509 * @access private
511 var $footer_font;
514 * @var Language templates.
515 * @access private
517 var $l;
520 * @var Barcode to print on page footer (only if set).
521 * @access private
523 var $barcode = false;
526 * @var If true prints header
527 * @access private
529 var $print_header = true;
532 * @var If true prints footer.
533 * @access private
535 var $print_footer = true;
538 * @var Header width (0 = full page width).
539 * @access private
541 var $header_width = 0;
544 * @var Header image logo.
545 * @access private
547 var $header_logo = "";
550 * @var Header image logo width in mm.
551 * @access private
553 var $header_logo_width = 30;
556 * @var String to print as title on document header.
557 * @access private
559 var $header_title = "";
562 * @var String to print on document header.
563 * @access private
565 var $header_string = "";
568 * @var Default number of columns for html table.
569 * @access private
571 var $default_table_columns = 4;
574 // variables for html parser
577 * @var HTML PARSER: store current link.
578 * @access private
580 var $HREF;
583 * @var HTML PARSER: store font list.
584 * @access private
586 var $fontList;
589 * @var HTML PARSER: true when font attribute is set.
590 * @access private
592 var $issetfont;
595 * @var HTML PARSER: true when color attribute is set.
596 * @access private
598 var $issetcolor;
601 * @var HTML PARSER: true in case of ordered list (OL), false otherwise.
602 * @access private
604 var $listordered = false;
607 * @var HTML PARSER: count list items.
608 * @access private
610 var $listcount = 0;
613 * @var HTML PARSER: size of table border.
614 * @access private
616 var $tableborder = 0;
619 * @var HTML PARSER: true at the beginning of table.
620 * @access private
622 var $tdbegin = false;
625 * @var HTML PARSER: table width.
626 * @access private
628 var $tdwidth = 0;
631 * @var HTML PARSER: table height.
632 * @access private
634 var $tdheight = 0;
637 * @var HTML PARSER: table align.
638 * @access private
640 var $tdalign = "L";
643 * @var HTML PARSER: table background color.
644 * @access private
646 var $tdbgcolor = false;
649 * @var Store temporary font size in points.
650 * @access private
652 var $tempfontsize = 10;
655 * @var Bold font style status.
656 * @access private
658 var $b;
661 * @var Underlined font style status.
662 * @access private
664 var $u;
667 * @var Italic font style status.
668 * @access private
670 var $i;
673 * @var spacer for LI tags.
674 * @access private
676 var $lispacer = "";
679 * @var default encoding
680 * @access private
681 * @since 1.53.0.TC010
683 var $encoding = "UTF-8";
686 * @var PHP internal encoding
687 * @access private
688 * @since 1.53.0.TC016
690 var $internal_encoding;
693 * @var store previous fill color as RGB array
694 * @access private
695 * @since 1.53.0.TC017
697 var $prevFillColor = array(255,255,255);
700 * @var store previous text color as RGB array
701 * @access private
702 * @since 1.53.0.TC017
704 var $prevTextColor = array(0,0,0);
707 * @var store previous font family
708 * @access private
709 * @since 1.53.0.TC017
711 var $prevFontFamily;
714 * @var store previous font style
715 * @access private
716 * @since 1.53.0.TC017
718 var $prevFontStyle;
720 //------------------------------------------------------------
721 // var methods
722 //------------------------------------------------------------
725 * This is the class constructor.
726 * It allows to set up the page format, the orientation and
727 * the measure unit used in all the methods (except for the font sizes).
728 * @since 1.0
729 * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
730 * @param string $unit 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.
731 * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
732 * @param boolean $unicode TRUE means that the input text is unicode (default = true)
733 * @param String $encoding charset encoding; default is UTF-8
735 function TCPDF($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding="UTF-8") {
737 /* Set internal character encoding to ASCII */
738 if (function_exists("mb_internal_encoding") AND mb_internal_encoding()) {
739 $this->internal_encoding = mb_internal_encoding();
740 mb_internal_encoding("ASCII");
743 //Some checks
744 $this->_dochecks();
745 //Initialization of properties
746 $this->isunicode=$unicode;
747 $this->page=0;
748 $this->n=2;
749 $this->buffer='';
750 $this->pages=array();
751 $this->OrientationChanges=array();
752 $this->state=0;
753 $this->fonts=array();
754 $this->FontFiles=array();
755 $this->diffs=array();
756 $this->images=array();
757 $this->links=array();
758 $this->InFooter=false;
759 $this->lasth=0;
760 $this->FontFamily='';
761 $this->FontStyle='';
762 $this->FontSizePt=12;
763 $this->underline=false;
764 $this->DrawColor='0 G';
765 $this->FillColor='0 g';
766 $this->TextColor='0 g';
767 $this->ColorFlag=false;
768 $this->ws=0;
769 //Standard Unicode fonts
770 $this->CoreFonts=array(
771 'courier'=>'Courier',
772 'courierB'=>'Courier-Bold',
773 'courierI'=>'Courier-Oblique',
774 'courierBI'=>'Courier-BoldOblique',
775 'helvetica'=>'Helvetica',
776 'helveticaB'=>'Helvetica-Bold',
777 'helveticaI'=>'Helvetica-Oblique',
778 'helveticaBI'=>'Helvetica-BoldOblique',
779 'times'=>'Times-Roman',
780 'timesB'=>'Times-Bold',
781 'timesI'=>'Times-Italic',
782 'timesBI'=>'Times-BoldItalic',
783 'symbol'=>'Symbol',
784 'zapfdingbats'=>'ZapfDingbats'
787 //Scale factor
788 // 2003-06-11 - Nicola Asuni : changed if/else with switch statement
789 switch (strtolower($unit)){
790 case 'pt': {$this->k=1; break;}
791 case 'mm': {$this->k=72/25.4; break;}
792 case 'cm': {$this->k=72/2.54; break;}
793 case 'in': {$this->k=72; break;}
794 default : {$this->Error('Incorrect unit: '.$unit); break;}
797 //Page format
798 if(is_string($format)) {
799 // 2002-07-24 - Nicola Asuni (info@tecnick.com)
800 // Added new page formats (45 standard ISO paper formats and 4 american common formats).
801 // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
802 switch (strtoupper($format)){
803 case '4A0': {$format = array(4767.87,6740.79); break;}
804 case '2A0': {$format = array(3370.39,4767.87); break;}
805 case 'A0': {$format = array(2383.94,3370.39); break;}
806 case 'A1': {$format = array(1683.78,2383.94); break;}
807 case 'A2': {$format = array(1190.55,1683.78); break;}
808 case 'A3': {$format = array(841.89,1190.55); break;}
809 case 'A4': default: {$format = array(595.28,841.89); break;}
810 case 'A5': {$format = array(419.53,595.28); break;}
811 case 'A6': {$format = array(297.64,419.53); break;}
812 case 'A7': {$format = array(209.76,297.64); break;}
813 case 'A8': {$format = array(147.40,209.76); break;}
814 case 'A9': {$format = array(104.88,147.40); break;}
815 case 'A10': {$format = array(73.70,104.88); break;}
816 case 'B0': {$format = array(2834.65,4008.19); break;}
817 case 'B1': {$format = array(2004.09,2834.65); break;}
818 case 'B2': {$format = array(1417.32,2004.09); break;}
819 case 'B3': {$format = array(1000.63,1417.32); break;}
820 case 'B4': {$format = array(708.66,1000.63); break;}
821 case 'B5': {$format = array(498.90,708.66); break;}
822 case 'B6': {$format = array(354.33,498.90); break;}
823 case 'B7': {$format = array(249.45,354.33); break;}
824 case 'B8': {$format = array(175.75,249.45); break;}
825 case 'B9': {$format = array(124.72,175.75); break;}
826 case 'B10': {$format = array(87.87,124.72); break;}
827 case 'C0': {$format = array(2599.37,3676.54); break;}
828 case 'C1': {$format = array(1836.85,2599.37); break;}
829 case 'C2': {$format = array(1298.27,1836.85); break;}
830 case 'C3': {$format = array(918.43,1298.27); break;}
831 case 'C4': {$format = array(649.13,918.43); break;}
832 case 'C5': {$format = array(459.21,649.13); break;}
833 case 'C6': {$format = array(323.15,459.21); break;}
834 case 'C7': {$format = array(229.61,323.15); break;}
835 case 'C8': {$format = array(161.57,229.61); break;}
836 case 'C9': {$format = array(113.39,161.57); break;}
837 case 'C10': {$format = array(79.37,113.39); break;}
838 case 'RA0': {$format = array(2437.80,3458.27); break;}
839 case 'RA1': {$format = array(1729.13,2437.80); break;}
840 case 'RA2': {$format = array(1218.90,1729.13); break;}
841 case 'RA3': {$format = array(864.57,1218.90); break;}
842 case 'RA4': {$format = array(609.45,864.57); break;}
843 case 'SRA0': {$format = array(2551.18,3628.35); break;}
844 case 'SRA1': {$format = array(1814.17,2551.18); break;}
845 case 'SRA2': {$format = array(1275.59,1814.17); break;}
846 case 'SRA3': {$format = array(907.09,1275.59); break;}
847 case 'SRA4': {$format = array(637.80,907.09); break;}
848 case 'LETTER': {$format = array(612.00,792.00); break;}
849 case 'LEGAL': {$format = array(612.00,1008.00); break;}
850 case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
851 case 'FOLIO': {$format = array(612.00,936.00); break;}
852 // default: {$this->Error('Unknown page format: '.$format); break;}
853 // END CHANGES Nicola Asuni
855 $this->fwPt=$format[0];
856 $this->fhPt=$format[1];
858 else {
859 $this->fwPt=$format[0]*$this->k;
860 $this->fhPt=$format[1]*$this->k;
863 $this->fw=$this->fwPt/$this->k;
864 $this->fh=$this->fhPt/$this->k;
866 //Page orientation
867 $orientation=strtolower($orientation);
868 if($orientation=='p' or $orientation=='portrait') {
869 $this->DefOrientation='P';
870 $this->wPt=$this->fwPt;
871 $this->hPt=$this->fhPt;
873 elseif($orientation=='l' or $orientation=='landscape') {
874 $this->DefOrientation='L';
875 $this->wPt=$this->fhPt;
876 $this->hPt=$this->fwPt;
878 else {
879 $this->Error('Incorrect orientation: '.$orientation);
882 $this->CurOrientation=$this->DefOrientation;
883 $this->w=$this->wPt/$this->k;
884 $this->h=$this->hPt/$this->k;
885 //Page margins (1 cm)
886 $margin=28.35/$this->k;
887 $this->SetMargins($margin,$margin);
888 //Interior cell margin (1 mm)
889 $this->cMargin=$margin/10;
890 //Line width (0.2 mm)
891 $this->LineWidth=.567/$this->k;
892 //Automatic page break
893 $this->SetAutoPageBreak(true,2*$margin);
894 //Full width display mode
895 $this->SetDisplayMode('fullwidth');
896 //Compression
897 $this->SetCompression(true);
898 //Set default PDF version number
899 $this->PDFVersion = "1.3";
901 $this->encoding = $encoding;
902 $this->b = 0;
903 $this->i = 0;
904 $this->u = 0;
905 $this->HREF = '';
906 $this->fontlist = array("arial", "times", "courier", "helvetica", "symbol");
907 $this->issetfont = false;
908 $this->issetcolor = false;
909 $this->tableborder = 0;
910 $this->tdbegin = false;
911 $this->tdwidth= 0;
912 $this->tdheight = 0;
913 $this->tdalign = "L";
914 $this->tdbgcolor = false;
916 $this->SetFillColor(200, 200, 200, true);
917 $this->SetTextColor(0, 0, 0, true);
921 * Set the image scale.
922 * @param float $scale image scale.
923 * @author Nicola Asuni
924 * @since 1.5.2
926 function setImageScale($scale) {
927 $this->imgscale=$scale;
931 * Returns the image scale.
932 * @return float image scale.
933 * @author Nicola Asuni
934 * @since 1.5.2
936 function getImageScale() {
937 return $this->imgscale;
941 * Returns the page width in units.
942 * @return int page width.
943 * @author Nicola Asuni
944 * @since 1.5.2
946 function getPageWidth() {
947 return $this->w;
951 * Returns the page height in units.
952 * @return int page height.
953 * @author Nicola Asuni
954 * @since 1.5.2
956 function getPageHeight() {
957 return $this->fh;
961 * Returns the page break margin.
962 * @return int page break margin.
963 * @author Nicola Asuni
964 * @since 1.5.2
966 function getBreakMargin() {
967 return $this->bMargin;
971 * Returns the scale factor (number of points in user unit).
972 * @return int scale factor.
973 * @author Nicola Asuni
974 * @since 1.5.2
976 function getScaleFactor() {
977 return $this->k;
981 * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
982 * @param float $left Left margin.
983 * @param float $top Top margin.
984 * @param float $right Right margin. Default value is the left one.
985 * @since 1.0
986 * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
988 function SetMargins($left, $top, $right=-1) {
989 //Set left, top and right margins
990 $this->lMargin=$left;
991 $this->tMargin=$top;
992 if($right==-1) {
993 $right=$left;
995 $this->rMargin=$right;
999 * 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.
1000 * @param float $margin The margin.
1001 * @since 1.4
1002 * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
1004 function SetLeftMargin($margin) {
1005 //Set left margin
1006 $this->lMargin=$margin;
1007 if(($this->page>0) and ($this->x<$margin)) {
1008 $this->x=$margin;
1013 * Defines the top margin. The method can be called before creating the first page.
1014 * @param float $margin The margin.
1015 * @since 1.5
1016 * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
1018 function SetTopMargin($margin) {
1019 //Set top margin
1020 $this->tMargin=$margin;
1024 * Defines the right margin. The method can be called before creating the first page.
1025 * @param float $margin The margin.
1026 * @since 1.5
1027 * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
1029 function SetRightMargin($margin) {
1030 //Set right margin
1031 $this->rMargin=$margin;
1035 * 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.
1036 * @param boolean $auto Boolean indicating if mode should be on or off.
1037 * @param float $margin Distance from the bottom of the page.
1038 * @since 1.0
1039 * @see Cell(), MultiCell(), AcceptPageBreak()
1041 function SetAutoPageBreak($auto, $margin=0) {
1042 //Set auto page break mode and triggering margin
1043 $this->AutoPageBreak=$auto;
1044 $this->bMargin=$margin;
1045 $this->PageBreakTrigger=$this->h-$margin;
1049 * Defines the way the document is to be displayed by the viewer. The zoom level can be set: pages can be displayed entirely on screen, occupy the full width of the window, use real size, be scaled by a specific zooming factor or use viewer default (configured in the Preferences menu of Acrobat). The page layout can be specified too: single at once, continuous display, two columns or viewer default. By default, documents use the full width mode with continuous display.
1050 * @param mixed $zoom 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>
1051 * @param string $layout The page layout. Possible values are:<ul><li>single: displays one page at once</li><li>continuous: displays pages continuously (default)</li><li>two: displays two pages on two columns</li><li>default: uses viewer default mode</li></ul>
1052 * @since 1.2
1054 function SetDisplayMode($zoom, $layout='continuous') {
1055 //Set display mode in viewer
1056 if($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom)) {
1057 $this->ZoomMode=$zoom;
1059 else {
1060 $this->Error('Incorrect zoom display mode: '.$zoom);
1062 if($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default') {
1063 $this->LayoutMode=$layout;
1065 else {
1066 $this->Error('Incorrect layout display mode: '.$layout);
1071 * 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.
1072 * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
1073 * @param boolean $compress Boolean indicating if compression must be enabled.
1074 * @since 1.4
1076 function SetCompression($compress) {
1077 //Set page compression
1078 if(function_exists('gzcompress')) {
1079 $this->compress=$compress;
1081 else {
1082 $this->compress=false;
1087 * Defines the title of the document.
1088 * @param string $title The title.
1089 * @since 1.2
1090 * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
1092 function SetTitle($title) {
1093 //Title of document
1094 $this->title=$title;
1098 * Defines the subject of the document.
1099 * @param string $subject The subject.
1100 * @since 1.2
1101 * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
1103 function SetSubject($subject) {
1104 //Subject of document
1105 $this->subject=$subject;
1109 * Defines the author of the document.
1110 * @param string $author The name of the author.
1111 * @since 1.2
1112 * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
1114 function SetAuthor($author) {
1115 //Author of document
1116 $this->author=$author;
1120 * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
1121 * @param string $keywords The list of keywords.
1122 * @since 1.2
1123 * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
1125 function SetKeywords($keywords) {
1126 //Keywords of document
1127 $this->keywords=$keywords;
1131 * Defines the creator of the document. This is typically the name of the application that generates the PDF.
1132 * @param string $creator The name of the creator.
1133 * @since 1.2
1134 * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
1136 function SetCreator($creator) {
1137 //Creator of document
1138 $this->creator=$creator;
1142 * Defines an alias for the total number of pages. It will be substituted as the document is closed.<br />
1143 * <b>Example:</b><br />
1144 * <pre>
1145 * class PDF extends TCPDF {
1146 * function Footer() {
1147 * //Go to 1.5 cm from bottom
1148 * $this->SetY(-15);
1149 * //Select Arial italic 8
1150 * $this->SetFont('Arial','I',8);
1151 * //Print current and total page numbers
1152 * $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
1155 * $pdf=new PDF();
1156 * $pdf->AliasNbPages();
1157 * </pre>
1158 * @param string $alias The alias. Default value: {nb}.
1159 * @since 1.4
1160 * @see PageNo(), Footer()
1162 function AliasNbPages($alias='{nb}') {
1163 //Define an alias for total number of pages
1164 $this->AliasNbPages = $this->_escapetext($alias);
1168 * 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.
1169 * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
1170 * @param string $msg The error message
1171 * @since 1.0
1173 function Error($msg) {
1174 //Fatal error
1175 die('<strong>TCPDF error: </strong>'.$msg);
1179 * This method begins the generation of the PDF document. It is not necessary to call it explicitly because AddPage() does it automatically.
1180 * Note: no page is created by this method
1181 * @since 1.0
1182 * @see AddPage(), Close()
1184 function Open() {
1185 //Begin document
1186 $this->state=1;
1190 * Terminates the PDF document. It is not necessary to call this method explicitly because Output() does it automatically. If the document contains no page, AddPage() is called to prevent from getting an invalid document.
1191 * @since 1.0
1192 * @see Open(), Output()
1194 function Close() {
1195 //Terminate document
1196 if($this->state==3) {
1197 return;
1199 if($this->page==0) {
1200 $this->AddPage();
1202 //Page footer
1203 $this->InFooter=true;
1204 $this->Footer();
1205 $this->InFooter=false;
1206 //Close page
1207 $this->_endpage();
1208 //Close document
1209 $this->_enddoc();
1213 * Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer. Then the page is added, the current position set to the top-left corner according to the left and top margins, and Header() is called to display the header.
1214 * The font which was set before calling is automatically restored. There is no need to call SetFont() again if you want to continue with the same font. The same is true for colors and line width.
1215 * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
1216 * @param string $orientation Page orientation. Possible values are (case insensitive):<ul><li>P or Portrait</li><li>L or Landscape</li></ul> The default value is the one passed to the constructor.
1217 * @since 1.0
1218 * @see TCPDF(), Header(), Footer(), SetMargins()
1220 function AddPage($orientation='') {
1221 //Start a new page
1222 if($this->state==0) {
1223 $this->Open();
1225 $family=$this->FontFamily;
1226 $style=$this->FontStyle.($this->underline ? 'U' : '');
1227 $size=$this->FontSizePt;
1228 $lw=$this->LineWidth;
1229 $dc=$this->DrawColor;
1230 $fc=$this->FillColor;
1231 $tc=$this->TextColor;
1232 $cf=$this->ColorFlag;
1233 if($this->page>0) {
1234 //Page footer
1235 $this->InFooter=true;
1236 $this->Footer();
1237 $this->InFooter=false;
1238 //Close page
1239 $this->_endpage();
1241 //Start new page
1242 $this->_beginpage($orientation);
1243 //Set line cap style to square
1244 $this->_out('2 J');
1245 //Set line width
1246 $this->LineWidth=$lw;
1247 $this->_out(sprintf('%.2f w',$lw*$this->k));
1248 //Set font
1249 if($family) {
1250 $this->SetFont($family,$style,$size);
1252 //Set colors
1253 $this->DrawColor=$dc;
1254 if($dc!='0 G') {
1255 $this->_out($dc);
1257 $this->FillColor=$fc;
1258 if($fc!='0 g') {
1259 $this->_out($fc);
1261 $this->TextColor=$tc;
1262 $this->ColorFlag=$cf;
1263 //Page header
1264 $this->Header();
1265 //Restore line width
1266 if($this->LineWidth!=$lw) {
1267 $this->LineWidth=$lw;
1268 $this->_out(sprintf('%.2f w',$lw*$this->k));
1270 //Restore font
1271 if($family) {
1272 $this->SetFont($family,$style,$size);
1274 //Restore colors
1275 if($this->DrawColor!=$dc) {
1276 $this->DrawColor=$dc;
1277 $this->_out($dc);
1279 if($this->FillColor!=$fc) {
1280 $this->FillColor=$fc;
1281 $this->_out($fc);
1283 $this->TextColor=$tc;
1284 $this->ColorFlag=$cf;
1290 * Set header data.
1291 * @param string $ln header image logo
1292 * @param string $lw header image logo width in mm
1293 * @param string $ht string to print as title on document header
1294 * @param string $hs string to print on document header
1296 function setHeaderData($ln="", $lw=0, $ht="", $hs="") {
1297 $this->header_logo = $ln;
1298 $this->header_logo_width = $lw;
1299 $this->header_title = $ht;
1300 $this->header_string = $hs;
1304 * Set header margin.
1305 * (minimum distance between header and top page margin)
1306 * @param int $hm distance in millimeters
1308 function setHeaderMargin($hm=10) {
1309 $this->header_margin = $hm;
1313 * Set footer margin.
1314 * (minimum distance between footer and bottom page margin)
1315 * @param int $fm distance in millimeters
1317 function setFooterMargin($fm=10) {
1318 $this->footer_margin = $fm;
1322 * This method is used to render the page header.
1323 * It is automatically called by AddPage() and could be overwritten in your own inherited class.
1325 function Header() {
1326 if ($this->print_header) {
1328 if (!isset($this->original_lMargin)) {
1329 $this->original_lMargin = $this->lMargin;
1331 if (!isset($this->original_rMargin)) {
1332 $this->original_rMargin = $this->rMargin;
1335 //set current position
1336 $this->SetXY($this->original_lMargin, $this->header_margin);
1338 if (($this->header_logo) AND ($this->header_logo != K_BLANK_IMAGE)) {
1339 $this->Image(K_PATH_IMAGES.$this->header_logo, $this->original_lMargin, $this->header_margin, $this->header_logo_width);
1341 else {
1342 $this->img_rb_y = $this->GetY();
1345 $cell_height = round((K_CELL_HEIGHT_RATIO * $this->header_font[2]) / $this->k, 2);
1347 $header_x = $this->original_lMargin + ($this->header_logo_width * 1.05); //set left margin for text data cell
1349 // header title
1350 $this->SetFont($this->header_font[0], 'B', $this->header_font[2] + 1);
1351 $this->SetX($header_x);
1352 $this->Cell($this->header_width, $cell_height, $this->header_title, 0, 1, 'L');
1354 // header string
1355 $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
1356 $this->SetX($header_x);
1357 $this->MultiCell($this->header_width, $cell_height, $this->header_string, 0, 'L', 0);
1359 // print an ending header line
1360 if (empty($this->header_width)) {
1361 //set style for cell border
1362 $this->SetLineWidth(0.3);
1363 $this->SetDrawColor(0, 0, 0);
1364 $this->SetY(1 + max($this->img_rb_y, $this->GetY()));
1365 $this->SetX($this->original_lMargin);
1366 $this->Cell(0, 0, '', 'T', 0, 'C');
1369 //restore position
1370 $this->SetXY($this->original_lMargin, $this->tMargin);
1375 * This method is used to render the page footer.
1376 * It is automatically called by AddPage() and could be overwritten in your own inherited class.
1378 function Footer() {
1379 if ($this->print_footer) {
1381 if (!isset($this->original_lMargin)) {
1382 $this->original_lMargin = $this->lMargin;
1384 if (!isset($this->original_rMargin)) {
1385 $this->original_rMargin = $this->rMargin;
1388 //set font
1389 $this->SetFont($this->footer_font[0], $this->footer_font[1] , $this->footer_font[2]);
1390 //set style for cell border
1391 $line_width = 0.3;
1392 $this->SetLineWidth($line_width);
1393 $this->SetDrawColor(0, 0, 0);
1395 $footer_height = round((K_CELL_HEIGHT_RATIO * $this->footer_font[2]) / $this->k, 2); //footer height
1396 //get footer y position
1397 $footer_y = $this->h - $this->footer_margin - $footer_height;
1398 //set current position
1399 $this->SetXY($this->original_lMargin, $footer_y);
1401 //print document barcode
1402 if ($this->barcode) {
1403 $this->Ln();
1404 $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin)); //max width
1405 $this->writeBarcode($this->original_lMargin, $footer_y + $line_width, $barcode_width, $footer_height - $line_width, "C128B", false, false, 2, $this->barcode);
1408 $this->SetXY($this->original_lMargin, $footer_y);
1410 //Print page number
1411 $this->Cell(0, $footer_height, $this->l['w_page']." ".$this->PageNo().' / {nb}', 'T', 0, 'R');
1416 * Returns the current page number.
1417 * @return int page number
1418 * @since 1.0
1419 * @see AliasNbPages()
1421 function PageNo() {
1422 //Get current page number
1423 return $this->page;
1427 * 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.
1428 * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1429 * @param int $g Green component (between 0 and 255)
1430 * @param int $b Blue component (between 0 and 255)
1431 * @since 1.3
1432 * @see SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
1434 function SetDrawColor($r, $g=-1, $b=-1) {
1435 //Set color for all stroking operations
1436 if(($r==0 and $g==0 and $b==0) or $g==-1) {
1437 $this->DrawColor=sprintf('%.3f G',$r/255);
1439 else {
1440 $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
1442 if($this->page>0) {
1443 $this->_out($this->DrawColor);
1448 * 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.
1449 * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1450 * @param int $g Green component (between 0 and 255)
1451 * @param int $b Blue component (between 0 and 255)
1452 * @param boolean $storeprev if true stores the RGB array on $prevFillColor variable.
1453 * @since 1.3
1454 * @see SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
1456 function SetFillColor($r, $g=-1, $b=-1, $storeprev=false) {
1457 //Set color for all filling operations
1458 if(($r==0 and $g==0 and $b==0) or $g==-1) {
1459 $this->FillColor=sprintf('%.3f g',$r/255);
1461 else {
1462 $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
1464 $this->ColorFlag=($this->FillColor!=$this->TextColor);
1465 if($this->page>0) {
1466 $this->_out($this->FillColor);
1468 if ($storeprev) {
1469 // store color as previous value
1470 $this->prevFillColor = array($r, $g, $b);
1475 * 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.
1476 * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
1477 * @param int $g Green component (between 0 and 255)
1478 * @param int $b Blue component (between 0 and 255)
1479 * @param boolean $storeprev if true stores the RGB array on $prevTextColor variable.
1480 * @since 1.3
1481 * @see SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
1483 function SetTextColor($r, $g=-1, $b=-1, $storeprev=false) {
1484 //Set color for text
1485 if(($r==0 and $g==0 and $b==0) or $g==-1) {
1486 $this->TextColor=sprintf('%.3f g',$r/255);
1488 else {
1489 $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
1491 $this->ColorFlag=($this->FillColor!=$this->TextColor);
1492 if ($storeprev) {
1493 // store color as previous value
1494 $this->prevTextColor = array($r, $g, $b);
1499 * Returns the length of a string in user unit. A font must be selected.<br>
1500 * Support UTF-8 Unicode [Nicola Asuni, 2005-01-02]
1501 * @param string $s The string whose length is to be computed
1502 * @return int
1503 * @since 1.2
1505 function GetStringWidth($s) {
1506 //Get width of a string in the current font
1507 $s = (string)$s;
1508 $cw = &$this->CurrentFont['cw'];
1509 $w = 0;
1510 if($this->isunicode) {
1511 $unicode = $this->UTF8StringToArray($s);
1512 foreach($unicode as $char) {
1513 if (isset($cw[$char])) {
1514 $w+=$cw[$char];
1515 } elseif(isset($cw[ord($char)])) {
1516 $w+=$cw[ord($char)];
1517 } elseif(isset($cw[chr($char)])) {
1518 $w+=$cw[chr($char)];
1519 } elseif(isset($this->CurrentFont['desc']['MissingWidth'])) {
1520 $w += $this->CurrentFont['desc']['MissingWidth']; // set default size
1521 } else {
1522 $w += 500;
1525 } else {
1526 $l = strlen($s);
1527 for($i=0; $i<$l; $i++) {
1528 if (isset($cw[$s{$i}])) {
1529 $w += $cw[$s{$i}];
1530 } else if (isset($cw[ord($s{$i})])) {
1531 $w += $cw[ord($s{$i})];
1535 return ($w * $this->FontSize / 1000);
1539 * 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.
1540 * @param float $width The width.
1541 * @since 1.0
1542 * @see Line(), Rect(), Cell(), MultiCell()
1544 function SetLineWidth($width) {
1545 //Set line width
1546 $this->LineWidth=$width;
1547 if($this->page>0) {
1548 $this->_out(sprintf('%.2f w',$width*$this->k));
1553 * Draws a line between two points.
1554 * @param float $x1 Abscissa of first point
1555 * @param float $y1 Ordinate of first point
1556 * @param float $x2 Abscissa of second point
1557 * @param float $y2 Ordinate of second point
1558 * @since 1.0
1559 * @see SetLineWidth(), SetDrawColor()
1561 function Line($x1, $y1, $x2, $y2) {
1562 //Draw a line
1563 $this->_out(sprintf('%.2f %.2f m %.2f %.2f l S', $x1*$this->k, ($this->h-$y1)*$this->k, $x2*$this->k, ($this->h-$y2)*$this->k));
1567 * Outputs a rectangle. It can be drawn (border only), filled (with no border) or both.
1568 * @param float $x Abscissa of upper-left corner
1569 * @param float $y Ordinate of upper-left corner
1570 * @param float $w Width
1571 * @param float $h Height
1572 * @param string $style Style of rendering. Possible values are:<ul><li>D or empty string: draw (default)</li><li>F: fill</li><li>DF or FD: draw and fill</li></ul>
1573 * @since 1.0
1574 * @see SetLineWidth(), SetDrawColor(), SetFillColor()
1576 function Rect($x, $y, $w, $h, $style='') {
1577 //Draw a rectangle
1578 if($style=='F') {
1579 $op='f';
1581 elseif($style=='FD' or $style=='DF') {
1582 $op='B';
1584 else {
1585 $op='S';
1587 $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
1591 * Imports a TrueType or Type1 font and makes it available. It is necessary to generate a font definition file first with the makefont.php utility. The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by FPDF_FONTPATH if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
1592 * Support UTF-8 Unicode [Nicola Asuni, 2005-01-02].
1593 * <b>Example</b>:<br />
1594 * <pre>
1595 * $pdf->AddFont('Comic','I');
1596 * // is equivalent to:
1597 * $pdf->AddFont('Comic','I','comici.php');
1598 * </pre>
1599 * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
1600 * @param string $style 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>
1601 * @param string $file The font definition file. By default, the name is built from the family and style, in lower case with no space.
1602 * @since 1.5
1603 * @see SetFont()
1605 function AddFont($family, $style='', $file='') {
1606 if(empty($family)) {
1607 return;
1610 //Add a TrueType or Type1 font
1611 $family = strtolower($family);
1612 if((!$this->isunicode) AND ($family == 'arial')) {
1613 $family = 'helvetica';
1616 $style=strtoupper($style);
1617 $style=str_replace('U','',$style);
1618 if($style == 'IB') {
1619 $style = 'BI';
1622 $fontkey = $family.$style;
1623 // check if the font has been already added
1624 if(isset($this->fonts[$fontkey])) {
1625 return;
1628 if($file=='') {
1629 $file = str_replace(' ', '', $family).strtolower($style).'.php';
1631 if(!file_exists($this->_getfontpath().$file)) {
1632 // try to load the basic file without styles
1633 $file = str_replace(' ', '', $family).'.php';
1636 include($this->_getfontpath().$file);
1638 if(!isset($name) AND !isset($fpdf_charwidths)) {
1639 $this->Error('Could not include font definition file');
1642 $i = count($this->fonts)+1;
1644 if($this->isunicode) {
1645 $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
1646 $fpdf_charwidths[$fontkey] = $cw;
1647 } else {
1648 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
1651 if(isset($diff) AND (!empty($diff))) {
1652 //Search existing encodings
1653 $d=0;
1654 $nb=count($this->diffs);
1655 for($i=1;$i<=$nb;$i++) {
1656 if($this->diffs[$i]==$diff) {
1657 $d=$i;
1658 break;
1661 if($d==0) {
1662 $d=$nb+1;
1663 $this->diffs[$d]=$diff;
1665 $this->fonts[$fontkey]['diff']=$d;
1667 if(!empty($file)) {
1668 if((strcasecmp($type,"TrueType") == 0) OR (strcasecmp($type,"TrueTypeUnicode") == 0)) {
1669 $this->FontFiles[$file]=array('length1'=>$originalsize);
1671 else {
1672 $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
1678 * Sets the font used to print character strings. It is mandatory to call this method at least once before printing text or the resulting document would not be valid.
1679 * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
1680 * The method can be called before the first page is created and the font is retained from page to page.
1681 If you just wish to change the current font size, it is simpler to call SetFontSize().
1682 * 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 FPDF_FONTPATH constant</li></ul><br />
1683 * Example for the last case (note the trailing slash):<br />
1684 * <pre>
1685 * define('FPDF_FONTPATH','/home/www/font/');
1686 * require('tcpdf.php');
1688 * //Times regular 12
1689 * $pdf->SetFont('Times');
1690 * //Arial bold 14
1691 * $pdf->SetFont('Arial','B',14);
1692 * //Removes bold
1693 * $pdf->SetFont('');
1694 * //Times bold, italic and underlined 14
1695 * $pdf->SetFont('Times','BIU');
1696 * </pre><br />
1697 * If the file corresponding to the requested font is not found, the error "Could not include font metric file" is generated.
1698 * @param string $family Family font. It can be either a name defined by AddFont() or one of the standard families (case insensitive):<ul><li>Courier (fixed-width)</li><li>Helvetica or Arial (synonymous; sans serif)</li><li>Times (serif)</li><li>Symbol (symbolic)</li><li>ZapfDingbats (symbolic)</li></ul>It is also possible to pass an empty string. In that case, the current family is retained.
1699 * @param string $style 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></ul>or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats
1700 * @param float $size 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
1701 * @since 1.0
1702 * @see AddFont(), SetFontSize(), Cell(), MultiCell(), Write()
1704 function SetFont($family, $style='', $size=0) {
1705 // save previous values
1706 $this->prevFontFamily = $this->FontFamily;
1707 $this->prevFontStyle = $this->FontStyle;
1709 //Select a font; size given in points
1710 global $fpdf_charwidths;
1712 $family=strtolower($family);
1713 if($family=='') {
1714 $family=$this->FontFamily;
1716 if((!$this->isunicode) AND ($family == 'arial')) {
1717 $family = 'helvetica';
1719 elseif(($family=="symbol") OR ($family=="zapfdingbats")) {
1720 $style='';
1722 $style=strtoupper($style);
1724 if(strpos($style,'U')!==false) {
1725 $this->underline=true;
1726 $style=str_replace('U','',$style);
1728 else {
1729 $this->underline=false;
1731 if($style=='IB') {
1732 $style='BI';
1734 if($size==0) {
1735 $size=$this->FontSizePt;
1738 // try to add font (if not already added)
1739 if($this->isunicode) {
1740 $this->AddFont($family, $style);
1743 //Test if font is already selected
1744 if(($this->FontFamily == $family) AND ($this->FontStyle == $style) AND ($this->FontSizePt == $size)) {
1745 return;
1748 $fontkey = $family.$style;
1749 //if(!isset($this->fonts[$fontkey]) AND isset($this->fonts[$family])) {
1750 // $style='';
1753 //Test if used for the first time
1754 if(!isset($this->fonts[$fontkey])) {
1755 //Check if one of the standard fonts
1756 if(isset($this->CoreFonts[$fontkey])) {
1757 if(!isset($fpdf_charwidths[$fontkey])) {
1758 //Load metric file
1759 $file = $family;
1760 if(($family!='symbol') AND ($family!='zapfdingbats')) {
1761 $file .= strtolower($style);
1763 if(!file_exists($this->_getfontpath().$file.'.php')) {
1764 // try to load the basic file without styles
1765 $file = $family;
1766 $fontkey = $family;
1768 include($this->_getfontpath().$file.'.php');
1769 if (($this->isunicode AND !isset($ctg)) OR ((!$this->isunicode) AND (!isset($fpdf_charwidths[$fontkey]))) ) {
1770 $this->Error("Could not include font metric file [".$fontkey."]: ".$this->_getfontpath().$file.".php");
1773 $i = count($this->fonts) + 1;
1775 if($this->isunicode) {
1776 $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
1777 $fpdf_charwidths[$fontkey] = $cw;
1778 } else {
1779 $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
1782 else {
1783 $this->Error('Undefined font: '.$family.' '.$style);
1786 //Select it
1787 $this->FontFamily = $family;
1788 $this->FontStyle = $style;
1789 $this->FontSizePt = $size;
1790 $this->FontSize = $size / $this->k;
1791 $this->CurrentFont = &$this->fonts[$fontkey];
1792 if($this->page>0) {
1793 $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
1798 * Defines the size of the current font.
1799 * @param float $size The size (in points)
1800 * @since 1.0
1801 * @see SetFont()
1803 function SetFontSize($size) {
1804 //Set font size in points
1805 if($this->FontSizePt==$size) {
1806 return;
1808 $this->FontSizePt = $size;
1809 $this->FontSize = $size / $this->k;
1810 if($this->page > 0) {
1811 $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
1816 * 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 />
1817 * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
1818 * @since 1.5
1819 * @see Cell(), Write(), Image(), Link(), SetLink()
1821 function AddLink() {
1822 //Create a new internal link
1823 $n=count($this->links)+1;
1824 $this->links[$n]=array(0,0);
1825 return $n;
1829 * Defines the page and position a link points to
1830 * @param int $link The link identifier returned by AddLink()
1831 * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
1832 * @param int $page Number of target page; -1 indicates the current page. This is the default value
1833 * @since 1.5
1834 * @see AddLink()
1836 function SetLink($link, $y=0, $page=-1) {
1837 //Set destination of internal link
1838 if($y==-1) {
1839 $y=$this->y;
1841 if($page==-1) {
1842 $page=$this->page;
1844 $this->links[$link]=array($page,$y);
1848 * Puts a link on a rectangular area of the page. 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.
1849 * @param float $x Abscissa of the upper-left corner of the rectangle
1850 * @param float $y Ordinate of the upper-left corner of the rectangle
1851 * @param float $w Width of the rectangle
1852 * @param float $h Height of the rectangle
1853 * @param mixed $link URL or identifier returned by AddLink()
1854 * @since 1.5
1855 * @see AddLink(), Cell(), Write(), Image()
1857 function Link($x, $y, $w, $h, $link) {
1858 //Put a link on the page
1859 $this->PageLinks[$this->page][] = array($x * $this->k, $this->hPt - $y * $this->k, $w * $this->k, $h*$this->k, $link);
1863 * Prints a character string. The origin is on the left of the first charcter, on the baseline. This method allows to place a string precisely on the page, but it is usually easier to use Cell(), MultiCell() or Write() which are the standard methods to print text.
1864 * @param float $x Abscissa of the origin
1865 * @param float $y Ordinate of the origin
1866 * @param string $txt String to print
1867 * @since 1.0
1868 * @see SetFont(), SetTextColor(), Cell(), MultiCell(), Write()
1870 function Text($x, $y, $txt) {
1871 //Output a string
1872 $s=sprintf('BT %.2f %.2f Td (%s) Tj ET', $x * $this->k, ($this->h-$y) * $this->k, $this->_escapetext($txt));
1873 if($this->underline AND ($txt!='')) {
1874 $s .= ' '.$this->_dounderline($x,$y,$txt);
1876 if($this->ColorFlag) {
1877 $s='q '.$this->TextColor.' '.$s.' Q';
1879 $this->_out($s);
1883 * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value. The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
1884 * This method is called automatically and should not be called directly by the application.<br />
1885 * <b>Example:</b><br />
1886 * The method is overriden in an inherited class in order to obtain a 3 column layout:<br />
1887 * <pre>
1888 * class PDF extends TCPDF {
1889 * var $col=0;
1891 * function SetCol($col) {
1892 * //Move position to a column
1893 * $this->col=$col;
1894 * $x=10+$col*65;
1895 * $this->SetLeftMargin($x);
1896 * $this->SetX($x);
1899 * function AcceptPageBreak() {
1900 * if($this->col<2) {
1901 * //Go to next column
1902 * $this->SetCol($this->col+1);
1903 * $this->SetY(10);
1904 * return false;
1906 * else {
1907 * //Go back to first column and issue page break
1908 * $this->SetCol(0);
1909 * return true;
1914 * $pdf=new PDF();
1915 * $pdf->Open();
1916 * $pdf->AddPage();
1917 * $pdf->SetFont('Arial','',12);
1918 * for($i=1;$i<=300;$i++) {
1919 * $pdf->Cell(0,5,"Line $i",0,1);
1921 * $pdf->Output();
1922 * </pre>
1923 * @return boolean
1924 * @since 1.4
1925 * @see SetAutoPageBreak()
1927 function AcceptPageBreak() {
1928 //Accept automatic page break or not
1929 return $this->AutoPageBreak;
1933 * 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 />
1934 * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
1935 * @param float $w Cell width. If 0, the cell extends up to the right margin.
1936 * @param float $h Cell height. Default value: 0.
1937 * @param string $txt String to print. Default value: empty string.
1938 * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either 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>
1939 * @param int $ln 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</li><li>2: below</li></ul>
1940 Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
1941 * @param string $align 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></ul>
1942 * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
1943 * @param mixed $link URL or identifier returned by AddLink().
1944 * @since 1.0
1945 * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
1947 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='') {
1948 //Output a cell
1949 $k=$this->k;
1950 if(($this->y + $h) > $this->PageBreakTrigger AND empty($this->InFooter) AND $this->AcceptPageBreak()) {
1951 //Automatic page break
1952 $x = $this->x;
1953 $ws = $this->ws;
1954 if($ws > 0) {
1955 $this->ws = 0;
1956 $this->_out('0 Tw');
1958 $this->AddPage($this->CurOrientation);
1959 $this->x = $x;
1960 if($ws > 0) {
1961 $this->ws = $ws;
1962 $this->_out(sprintf('%.3f Tw',$ws * $k));
1965 if($w == 0) {
1966 $w = $this->w - $this->rMargin - $this->x;
1968 $s = '';
1969 if(($fill == 1) OR ($border == 1)) {
1970 if($fill == 1) {
1971 $op = ($border == 1) ? 'B' : 'f';
1973 else {
1974 $op = 'S';
1976 $s = sprintf('%.2f %.2f %.2f %.2f re %s ', $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
1978 if(is_string($border)) {
1979 $x=$this->x;
1980 $y=$this->y;
1981 if(strpos($border,'L')!==false) {
1982 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
1984 if(strpos($border,'T')!==false) {
1985 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
1987 if(strpos($border,'R')!==false) {
1988 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
1990 if(strpos($border,'B')!==false) {
1991 $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
1994 if($txt != '') {
1995 $width = $this->GetStringWidth($txt);
1996 if($align == 'R') {
1997 $dx = $w - $this->cMargin - $width;
1999 elseif($align=='C') {
2000 $dx = ($w - $width)/2;
2002 else {
2003 $dx = $this->cMargin;
2005 if($this->ColorFlag) {
2006 $s .= 'q '.$this->TextColor.' ';
2008 $txt2 = $this->_escapetext($txt);
2009 $s.=sprintf('BT %.2f %.2f Td (%s) Tj ET', ($this->x + $dx) * $k, ($this->h - ($this->y + 0.5 * $h + 0.3 * $this->FontSize)) * $k, $txt2);
2010 if($this->underline) {
2011 $s.=' '.$this->_dounderline($this->x + $dx, $this->y + 0.5 * $h + 0.3 * $this->FontSize, $txt);
2013 if($this->ColorFlag) {
2014 $s.=' Q';
2016 if($link) {
2017 $this->Link($this->x + $dx, $this->y + 0.5 * $h - 0.5 * $this->FontSize, $width, $this->FontSize, $link);
2020 if($s) {
2021 $this->_out($s);
2023 $this->lasth = $h;
2024 if($ln>0) {
2025 //Go to next line
2026 $this->y += $h;
2027 if($ln == 1) {
2028 $this->x = $this->lMargin;
2031 else {
2032 $this->x += $w;
2037 * This method allows printing text with line breaks. 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 />
2038 * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
2039 * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
2040 * @param float $h Height of cells.
2041 * @param string $txt String to print
2042 * @param mixed $border Indicates if borders must be drawn around the cell block. The value can be either 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>
2043 * @param int $ln 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</li><li>2: below</li></ul>
2044 Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
2045 * @param string $align 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)</li></ul>
2046 * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
2047 * @since 1.3
2048 * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
2050 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0) {
2051 //Output text with automatic or explicit line breaks
2052 $cw = &$this->CurrentFont['cw'];
2054 if($w == 0) {
2055 $w = $this->w - $this->rMargin - $this->x;
2058 $wmax = ($w - 2 * $this->cMargin);
2060 $s = str_replace("\r", '', $txt); // remove carriage returns
2061 $nb = strlen($s);
2063 $b=0;
2064 if($border) {
2065 if($border==1) {
2066 $border='LTRB';
2067 $b='LRT';
2068 $b2='LR';
2070 else {
2071 $b2='';
2072 if(strpos($border,'L')!==false) {
2073 $b2.='L';
2075 if(strpos($border,'R')!==false) {
2076 $b2.='R';
2078 $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
2081 $sep=-1;
2082 $i=0;
2083 $j=0;
2084 $l=0;
2085 $ns=0;
2086 $nl=1;
2087 while($i<$nb) {
2088 //Get next character
2089 $c = $s{$i};
2090 if(preg_match("/[\n]/u", $c)) {
2091 //Explicit line break
2092 if($this->ws > 0) {
2093 $this->ws = 0;
2094 $this->_out('0 Tw');
2096 $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
2097 $i++;
2098 $sep=-1;
2099 $j=$i;
2100 $l=0;
2101 $ns=0;
2102 $nl++;
2103 if($border and $nl==2) {
2104 $b = $b2;
2106 continue;
2108 if(preg_match("/[ ]/u", $c)) {
2109 $sep = $i;
2110 $ls = $l;
2111 $ns++;
2114 $l = $this->GetStringWidth(substr($s, $j, $i-$j));
2116 if($l > $wmax) {
2117 //Automatic line break
2118 if($sep == -1) {
2119 if($i == $j) {
2120 $i++;
2122 if($this->ws > 0) {
2123 $this->ws = 0;
2124 $this->_out('0 Tw');
2126 $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
2128 else {
2129 if($align=='J') {
2130 $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
2131 $this->_out(sprintf('%.3f Tw', $this->ws * $this->k));
2133 $this->Cell($w, $h, substr($s, $j, $sep-$j), $b, 2, $align, $fill);
2134 $i = $sep + 1;
2136 $sep=-1;
2137 $j=$i;
2138 $l=0;
2139 $ns=0;
2140 $nl++;
2141 if($border AND ($nl==2)) {
2142 $b=$b2;
2145 else {
2146 $i++;
2149 //Last chunk
2150 if($this->ws>0) {
2151 $this->ws=0;
2152 $this->_out('0 Tw');
2154 if($border and is_int(strpos($border,'B'))) {
2155 $b.='B';
2157 $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
2158 $this->x=$this->lMargin;
2162 * This method prints text from the current position. When the right margin is reached (or the \n character is met) a line break occurs and text continues from the left margin. Upon method exit, the current position is left just at the end of the text. It is possible to put a link on the text.<br />
2163 * <b>Example:</b><br />
2164 * <pre>
2165 * //Begin with regular font
2166 * $pdf->SetFont('Arial','',14);
2167 * $pdf->Write(5,'Visit ');
2168 * //Then put a blue underlined link
2169 * $pdf->SetTextColor(0,0,255);
2170 * $pdf->SetFont('','U');
2171 * $pdf->Write(5,'www.tecnick.com','http://www.tecnick.com');
2172 * </pre>
2173 * @param float $h Line height
2174 * @param string $txt String to print
2175 * @param mixed $link URL or identifier returned by AddLink()
2176 * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
2177 * @since 1.5
2178 * @see SetFont(), SetTextColor(), AddLink(), MultiCell(), SetAutoPageBreak()
2180 function Write($h, $txt, $link='', $fill=0) {
2182 //Output text in flowing mode
2183 $cw = &$this->CurrentFont['cw'];
2184 $w = $this->w - $this->rMargin - $this->x;
2185 $wmax = ($w - 2 * $this->cMargin);
2187 $s = str_replace("\r", '', $txt);
2188 $nb = strlen($s);
2190 // handle single space character
2191 if(($nb==1) AND preg_match("/[ ]/u", $s)) {
2192 $this->x += $this->GetStringWidth($s);
2193 return;
2196 $sep=-1;
2197 $i=0;
2198 $j=0;
2199 $l=0;
2200 $nl=1;
2201 while($i<$nb) {
2202 //Get next character
2203 $c=$s{$i};
2204 if(preg_match("/[\n]/u", $c)) {
2205 //Explicit line break
2206 $this->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, '', $fill, $link);
2207 $i++;
2208 $sep = -1;
2209 $j = $i;
2210 $l = 0;
2211 if($nl == 1) {
2212 $this->x = $this->lMargin;
2213 $w = $this->w - $this->rMargin - $this->x;
2214 $wmax = ($w - 2 * $this->cMargin);
2216 $nl++;
2217 continue;
2219 if(preg_match("/[ ]/u", $c)) {
2220 $sep= $i;
2223 $l = $this->GetStringWidth(substr($s, $j, $i-$j));
2225 if($l > $wmax) {
2226 //Automatic line break (word wrapping)
2227 if($sep == -1) {
2228 if($this->x > $this->lMargin) {
2229 //Move to next line
2230 $this->x = $this->lMargin;
2231 $this->y += $h;
2232 $w=$this->w - $this->rMargin - $this->x;
2233 $wmax=($w - 2 * $this->cMargin);
2234 $i++;
2235 $nl++;
2236 continue;
2238 if($i==$j) {
2239 $i++;
2241 $this->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, '', $fill, $link);
2243 else {
2244 $this->Cell($w, $h, substr($s, $j, $sep-$j), 0, 2, '', $fill, $link);
2245 $i=$sep+1;
2247 $sep = -1;
2248 $j = $i;
2249 $l = 0;
2250 if($nl==1) {
2251 $this->x = $this->lMargin;
2252 $w = $this->w - $this->rMargin - $this->x;
2253 $wmax = ($w - 2 * $this->cMargin);
2255 $nl++;
2257 else {
2258 $i++;
2262 //Last chunk
2263 if($i!=$j) {
2264 $this->Cell($this->GetStringWidth(substr($s, $j)." "), $h, substr($s, $j), 0, 0, '', $fill, $link);
2269 * Puts an image in the page. The upper-left corner must be given. The dimensions can be specified in different ways:<ul><li>explicit width and height (expressed in user unit)</li><li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li><li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
2270 * Supported formats are JPEG and PNG.
2271 * For JPEG, all flavors are allowed:<ul><li>gray scales</li><li>true colors (24 bits)</li><li>CMYK (32 bits)</li></ul>
2272 * For PNG, are allowed:<ul><li>gray scales on at most 8 bits (256 levels)</li><li>indexed colors</li><li>true colors (24 bits)</li></ul>
2273 * but are not supported:<ul><li>Interlacing</li><li>Alpha channel</li></ul>
2274 * If a transparent color is defined, it will be taken into account (but will be only interpreted by Acrobat 4 and above).<br />
2275 * The format can be specified explicitly or inferred from the file extension.<br />
2276 * It is possible to put a link on the image.<br />
2277 * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
2278 * @param string $file Name of the file containing the image.
2279 * @param float $x Abscissa of the upper-left corner.
2280 * @param float $y Ordinate of the upper-left corner.
2281 * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
2282 * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
2283 * @param string $type Image format. Possible values are (case insensitive): JPG, JPEG, PNG. If not specified, the type is inferred from the file extension.
2284 * @param mixed $link URL or identifier returned by AddLink().
2285 * @since 1.1
2286 * @see AddLink()
2288 function Image($file, $x, $y, $w=0, $h=0, $type='', $link='') {
2289 //Put an image on the page
2290 if(!isset($this->images[$file])) {
2291 //First use of image, get info
2292 if($type == '') {
2293 $pos = strrpos($file,'.');
2294 if(empty($pos)) {
2295 $this->Error('Image file has no extension and no type was specified: '.$file);
2297 $type = substr($file, $pos+1);
2299 $type = strtolower($type);
2300 $mqr = get_magic_quotes_runtime();
2301 set_magic_quotes_runtime(0);
2302 if($type == 'jpg' or $type == 'jpeg') {
2303 $info=$this->_parsejpg($file);
2305 elseif($type == 'png') {
2306 $info=$this->_parsepng($file);
2308 else {
2309 //Allow for additional formats
2310 $mtd='_parse'.$type;
2311 if(!method_exists($this,$mtd)) {
2312 $this->Error('Unsupported image type: '.$type);
2314 $info=$this->$mtd($file);
2316 set_magic_quotes_runtime($mqr);
2317 $info['i']=count($this->images)+1;
2318 $this->images[$file]=$info;
2320 else {
2321 $info=$this->images[$file];
2323 //Automatic width and height calculation if needed
2324 if(($w == 0) and ($h == 0)) {
2325 //Put image at 72 dpi
2326 // 2004-06-14 :: Nicola Asuni, scale factor where added
2327 $w = $info['w'] / ($this->imgscale * $this->k);
2328 $h = $info['h'] / ($this->imgscale * $this->k);
2330 if($w == 0) {
2331 $w = $h * $info['w'] / $info['h'];
2333 if($h == 0) {
2334 $h = $w * $info['h'] / $info['w'];
2336 $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k, $info['i']));
2337 if($link) {
2338 $this->Link($x, $y, $w, $h, $link);
2341 //2002-07-31 - Nicola Asuni
2342 // set right-bottom corner coordinates
2343 $this->img_rb_x = $x + $w;
2344 $this->img_rb_y = $y + $h;
2348 * Performs a line break. The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
2349 * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
2350 * @since 1.0
2351 * @see Cell()
2353 function Ln($h='') {
2354 //Line feed; default value is last cell height
2355 $this->x=$this->lMargin;
2356 if(is_string($h)) {
2357 $this->y+=$this->lasth;
2359 else {
2360 $this->y+=$h;
2365 * Returns the abscissa of the current position.
2366 * @return float
2367 * @since 1.2
2368 * @see SetX(), GetY(), SetY()
2370 function GetX() {
2371 //Get x position
2372 return $this->x;
2376 * Defines the abscissa of the current position. If the passed value is negative, it is relative to the right of the page.
2377 * @param float $x The value of the abscissa.
2378 * @since 1.2
2379 * @see GetX(), GetY(), SetY(), SetXY()
2381 function SetX($x) {
2382 //Set x position
2383 if($x>=0) {
2384 $this->x=$x;
2386 else {
2387 $this->x=$this->w+$x;
2392 * Returns the ordinate of the current position.
2393 * @return float
2394 * @since 1.0
2395 * @see SetY(), GetX(), SetX()
2397 function GetY() {
2398 //Get y position
2399 return $this->y;
2403 * Moves the current abscissa back to the left margin and sets the ordinate. If the passed value is negative, it is relative to the bottom of the page.
2404 * @param float $y The value of the ordinate.
2405 * @since 1.0
2406 * @see GetX(), GetY(), SetY(), SetXY()
2408 function SetY($y) {
2409 //Set y position and reset x
2410 $this->x=$this->lMargin;
2411 if($y>=0) {
2412 $this->y=$y;
2414 else {
2415 $this->y=$this->h+$y;
2420 * Defines the abscissa and ordinate of the current position. If the passed values are negative, they are relative respectively to the right and bottom of the page.
2421 * @param float $x The value of the abscissa
2422 * @param float $y The value of the ordinate
2423 * @since 1.2
2424 * @see SetX(), SetY()
2426 function SetXY($x, $y) {
2427 //Set x and y positions
2428 $this->SetY($y);
2429 $this->SetX($x);
2433 * Send the document to a given destination: string, local file or browser. In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
2434 * The method first calls Close() if necessary to terminate the document.
2435 * @param string $name The name of the file. If not given, the document will be sent to the browser (destination I) with the name doc.pdf.
2436 * @param string $dest Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser. 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 file with the name given by name.</li><li>S: return the document as a string. name is ignored.</li></ul>If the parameter is not specified but a name is given, destination is F. If no parameter is specified at all, destination is I.<br />Note: for compatibility with previous versions, a boolean value is also accepted (false for F and true for D).
2437 * @since 1.0
2438 * @see Close()
2440 function Output($name='',$dest='') {
2441 //Output PDF to some destination
2442 //Finish document if necessary
2443 if($this->state < 3) {
2444 $this->Close();
2446 //Normalize parameters
2447 if(is_bool($dest)) {
2448 $dest=$dest ? 'D' : 'F';
2450 $dest=strtoupper($dest);
2451 if($dest=='') {
2452 if($name=='') {
2453 $name='doc.pdf';
2454 $dest='I';
2455 } else {
2456 $dest='F';
2459 switch($dest) {
2460 case 'I': {
2461 //Send to standard output
2462 if(ob_get_contents()) {
2463 $this->Error('Some data has already been output, can\'t send PDF file');
2465 if(php_sapi_name()!='cli') {
2466 //We send to a browser
2467 header('Content-Type: application/pdf');
2468 if(headers_sent()) {
2469 $this->Error('Some data has already been output to browser, can\'t send PDF file');
2471 header('Content-Length: '.strlen($this->buffer));
2472 header('Content-disposition: inline; filename="'.$name.'"');
2474 echo $this->buffer;
2475 break;
2477 case 'D': {
2478 //Download file
2479 if(ob_get_contents()) {
2480 $this->Error('Some data has already been output, can\'t send PDF file');
2482 if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) {
2483 header('Content-Type: application/force-download');
2484 } else {
2485 header('Content-Type: application/octet-stream');
2487 if(headers_sent()) {
2488 $this->Error('Some data has already been output to browser, can\'t send PDF file');
2490 header('Content-Length: '.strlen($this->buffer));
2491 header('Content-disposition: attachment; filename="'.$name.'"');
2492 echo $this->buffer;
2493 break;
2495 case 'F': {
2496 //Save to local file
2497 $f=fopen($name,'wb');
2498 if(!$f) {
2499 $this->Error('Unable to create output file: '.$name);
2501 fwrite($f,$this->buffer,strlen($this->buffer));
2502 fclose($f);
2503 break;
2505 case 'S': {
2506 //Return as a string
2507 return $this->buffer;
2509 default: {
2510 $this->Error('Incorrect output destination: '.$dest);
2513 return '';
2516 // var methods
2519 * Check for locale-related bug
2520 * @access protected
2522 function _dochecks() {
2523 //Check for locale-related bug
2524 if(1.1==1) {
2525 $this->Error('Don\'t alter the locale before including class file');
2527 //Check for decimal separator
2528 if(sprintf('%.1f',1.0)!='1.0') {
2529 setlocale(LC_NUMERIC,'C');
2534 * Return fonts path
2535 * @access protected
2537 function _getfontpath() {
2538 if(!defined('FPDF_FONTPATH') AND is_dir(dirname(__FILE__).'/font')) {
2539 define('FPDF_FONTPATH', dirname(__FILE__).'/font/');
2541 return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
2545 * Start document
2546 * @access protected
2548 function _begindoc() {
2549 //Start document
2550 $this->state=1;
2551 $this->_out('%PDF-1.3');
2555 * _putpages
2556 * @access protected
2558 function _putpages() {
2559 $nb = $this->page;
2560 if(!empty($this->AliasNbPages)) {
2561 $nbstr = $this->UTF8ToUTF16BE($nb, false);
2562 //Replace number of pages
2563 for($n=1;$n<=$nb;$n++) {
2564 $this->pages[$n]=str_replace($this->AliasNbPages, $nbstr, $this->pages[$n]);
2567 if($this->DefOrientation=='P') {
2568 $wPt=$this->fwPt;
2569 $hPt=$this->fhPt;
2571 else {
2572 $wPt=$this->fhPt;
2573 $hPt=$this->fwPt;
2575 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
2576 for($n=1;$n<=$nb;$n++) {
2577 //Page
2578 $this->_newobj();
2579 $this->_out('<</Type /Page');
2580 $this->_out('/Parent 1 0 R');
2581 if(isset($this->OrientationChanges[$n])) {
2582 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
2584 $this->_out('/Resources 2 0 R');
2585 if(isset($this->PageLinks[$n])) {
2586 //Links
2587 $annots='/Annots [';
2588 foreach($this->PageLinks[$n] as $pl) {
2589 $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
2590 $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
2591 if(is_string($pl[4])) {
2592 $annots.='/A <</S /URI /URI ('.$this->_escape($pl[4]).')>>>>';
2594 else {
2595 $l=$this->links[$pl[4]];
2596 $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
2597 $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
2600 $this->_out($annots.']');
2602 $this->_out('/Contents '.($this->n+1).' 0 R>>');
2603 $this->_out('endobj');
2604 //Page content
2605 $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
2606 $this->_newobj();
2607 $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
2608 $this->_putstream($p);
2609 $this->_out('endobj');
2611 //Pages root
2612 $this->offsets[1]=strlen($this->buffer);
2613 $this->_out('1 0 obj');
2614 $this->_out('<</Type /Pages');
2615 $kids='/Kids [';
2616 for($i=0;$i<$nb;$i++) {
2617 $kids.=(3+2*$i).' 0 R ';
2619 $this->_out($kids.']');
2620 $this->_out('/Count '.$nb);
2621 $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
2622 $this->_out('>>');
2623 $this->_out('endobj');
2627 * Adds fonts
2628 * _putfonts
2629 * @access protected
2631 function _putfonts() {
2632 $nf=$this->n;
2633 foreach($this->diffs as $diff) {
2634 //Encodings
2635 $this->_newobj();
2636 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
2637 $this->_out('endobj');
2639 $mqr=get_magic_quotes_runtime();
2640 set_magic_quotes_runtime(0);
2641 foreach($this->FontFiles as $file=>$info) {
2642 //Font file embedding
2643 $this->_newobj();
2644 $this->FontFiles[$file]['n']=$this->n;
2645 $font='';
2646 $f = fopen($this->_getfontpath().$file,'rb',1);
2647 if(!$f) {
2648 $this->Error('Font file not found');
2650 while(!feof($f)) {
2651 $font .= fread($f, 8192);
2653 fclose($f);
2654 $compressed=(substr($file,-2)=='.z');
2655 if(!$compressed && isset($info['length2'])) {
2656 $header=(ord($font{0})==128);
2657 if($header) {
2658 //Strip first binary header
2659 $font=substr($font,6);
2661 if($header && ord($font{$info['length1']})==128) {
2662 //Strip second binary header
2663 $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
2666 $this->_out('<</Length '.strlen($font));
2667 if($compressed) {
2668 $this->_out('/Filter /FlateDecode');
2670 $this->_out('/Length1 '.$info['length1']);
2671 if(isset($info['length2'])) {
2672 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
2674 $this->_out('>>');
2675 $this->_putstream($font);
2676 $this->_out('endobj');
2678 set_magic_quotes_runtime($mqr);
2679 foreach($this->fonts as $k=>$font) {
2680 //Font objects
2681 $this->fonts[$k]['n']=$this->n+1;
2682 $type=$font['type'];
2683 $name=$font['name'];
2684 if($type=='core') {
2685 //Standard font
2686 $this->_newobj();
2687 $this->_out('<</Type /Font');
2688 $this->_out('/BaseFont /'.$name);
2689 $this->_out('/Subtype /Type1');
2690 if($name!='Symbol' && $name!='ZapfDingbats') {
2691 $this->_out('/Encoding /WinAnsiEncoding');
2693 $this->_out('>>');
2694 $this->_out('endobj');
2695 } elseif($type=='Type1' || $type=='TrueType') {
2696 //Additional Type1 or TrueType font
2697 $this->_newobj();
2698 $this->_out('<</Type /Font');
2699 $this->_out('/BaseFont /'.$name);
2700 $this->_out('/Subtype /'.$type);
2701 $this->_out('/FirstChar 32 /LastChar 255');
2702 $this->_out('/Widths '.($this->n+1).' 0 R');
2703 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
2704 if($font['enc']) {
2705 if(isset($font['diff'])) {
2706 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
2707 } else {
2708 $this->_out('/Encoding /WinAnsiEncoding');
2711 $this->_out('>>');
2712 $this->_out('endobj');
2713 //Widths
2714 $this->_newobj();
2715 $cw=&$font['cw'];
2716 $s='[';
2717 for($i=32;$i<=255;$i++) {
2718 $s.=$cw[chr($i)].' ';
2720 $this->_out($s.']');
2721 $this->_out('endobj');
2722 //Descriptor
2723 $this->_newobj();
2724 $s='<</Type /FontDescriptor /FontName /'.$name;
2725 foreach($font['desc'] as $k=>$v) {
2726 $s.=' /'.$k.' '.$v;
2728 $file = $font['file'];
2729 if($file) {
2730 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
2732 $this->_out($s.'>>');
2733 $this->_out('endobj');
2734 } else {
2735 //Allow for additional types
2736 $mtd='_put'.strtolower($type);
2737 if(!method_exists($this, $mtd)) {
2738 $this->Error('Unsupported font type: '.$type);
2740 $this->$mtd($font);
2746 * _putimages
2747 * @access protected
2749 function _putimages() {
2750 $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
2751 reset($this->images);
2752 while(list($file,$info)=each($this->images)) {
2753 $this->_newobj();
2754 $this->images[$file]['n']=$this->n;
2755 $this->_out('<</Type /XObject');
2756 $this->_out('/Subtype /Image');
2757 $this->_out('/Width '.$info['w']);
2758 $this->_out('/Height '.$info['h']);
2759 if($info['cs']=='Indexed') {
2760 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
2762 else {
2763 $this->_out('/ColorSpace /'.$info['cs']);
2764 if($info['cs']=='DeviceCMYK') {
2765 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
2768 $this->_out('/BitsPerComponent '.$info['bpc']);
2769 if(isset($info['f'])) {
2770 $this->_out('/Filter /'.$info['f']);
2772 if(isset($info['parms'])) {
2773 $this->_out($info['parms']);
2775 if(isset($info['trns']) and is_array($info['trns'])) {
2776 $trns='';
2777 for($i=0;$i<count($info['trns']);$i++) {
2778 $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
2780 $this->_out('/Mask ['.$trns.']');
2782 $this->_out('/Length '.strlen($info['data']).'>>');
2783 $this->_putstream($info['data']);
2784 unset($this->images[$file]['data']);
2785 $this->_out('endobj');
2786 //Palette
2787 if($info['cs']=='Indexed') {
2788 $this->_newobj();
2789 $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
2790 $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
2791 $this->_putstream($pal);
2792 $this->_out('endobj');
2798 * _putxobjectdict
2799 * @access protected
2801 function _putxobjectdict() {
2802 foreach($this->images as $image) {
2803 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
2808 * _putresourcedict
2809 * @access protected
2811 function _putresourcedict(){
2812 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
2813 $this->_out('/Font <<');
2814 foreach($this->fonts as $font) {
2815 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
2817 $this->_out('>>');
2818 $this->_out('/XObject <<');
2819 $this->_putxobjectdict();
2820 $this->_out('>>');
2824 * _putresources
2825 * @access protected
2827 function _putresources() {
2828 $this->_putfonts();
2829 $this->_putimages();
2830 //Resource dictionary
2831 $this->offsets[2]=strlen($this->buffer);
2832 $this->_out('2 0 obj');
2833 $this->_out('<<');
2834 $this->_putresourcedict();
2835 $this->_out('>>');
2836 $this->_out('endobj');
2840 * _putinfo
2841 * @access protected
2843 function _putinfo() {
2844 $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
2845 if(!empty($this->title)) {
2846 $this->_out('/Title '.$this->_textstring($this->title));
2848 if(!empty($this->subject)) {
2849 $this->_out('/Subject '.$this->_textstring($this->subject));
2851 if(!empty($this->author)) {
2852 $this->_out('/Author '.$this->_textstring($this->author));
2854 if(!empty($this->keywords)) {
2855 $this->_out('/Keywords '.$this->_textstring($this->keywords));
2857 if(!empty($this->creator)) {
2858 $this->_out('/Creator '.$this->_textstring($this->creator));
2860 $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
2864 * _putcatalog
2865 * @access protected
2867 function _putcatalog() {
2868 $this->_out('/Type /Catalog');
2869 $this->_out('/Pages 1 0 R');
2870 if($this->ZoomMode=='fullpage') {
2871 $this->_out('/OpenAction [3 0 R /Fit]');
2873 elseif($this->ZoomMode=='fullwidth') {
2874 $this->_out('/OpenAction [3 0 R /FitH null]');
2876 elseif($this->ZoomMode=='real') {
2877 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
2879 elseif(!is_string($this->ZoomMode)) {
2880 $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
2882 if($this->LayoutMode=='single') {
2883 $this->_out('/PageLayout /SinglePage');
2885 elseif($this->LayoutMode=='continuous') {
2886 $this->_out('/PageLayout /OneColumn');
2888 elseif($this->LayoutMode=='two') {
2889 $this->_out('/PageLayout /TwoColumnLeft');
2894 * _puttrailer
2895 * @access protected
2897 function _puttrailer() {
2898 $this->_out('/Size '.($this->n+1));
2899 $this->_out('/Root '.$this->n.' 0 R');
2900 $this->_out('/Info '.($this->n-1).' 0 R');
2904 * _putheader
2905 * @access protected
2907 function _putheader() {
2908 $this->_out('%PDF-'.$this->PDFVersion);
2912 * _enddoc
2913 * @access protected
2915 function _enddoc() {
2916 $this->_putheader();
2917 $this->_putpages();
2918 $this->_putresources();
2919 //Info
2920 $this->_newobj();
2921 $this->_out('<<');
2922 $this->_putinfo();
2923 $this->_out('>>');
2924 $this->_out('endobj');
2925 //Catalog
2926 $this->_newobj();
2927 $this->_out('<<');
2928 $this->_putcatalog();
2929 $this->_out('>>');
2930 $this->_out('endobj');
2931 //Cross-ref
2932 $o=strlen($this->buffer);
2933 $this->_out('xref');
2934 $this->_out('0 '.($this->n+1));
2935 $this->_out('0000000000 65535 f ');
2936 for($i=1;$i<=$this->n;$i++) {
2937 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
2939 //Trailer
2940 $this->_out('trailer');
2941 $this->_out('<<');
2942 $this->_puttrailer();
2943 $this->_out('>>');
2944 $this->_out('startxref');
2945 $this->_out($o);
2946 $this->_out('%%EOF');
2947 $this->state=3;
2951 * _beginpage
2952 * @access protected
2954 function _beginpage($orientation) {
2955 $this->page++;
2956 $this->pages[$this->page]='';
2957 $this->state=2;
2958 $this->x=$this->lMargin;
2959 $this->y=$this->tMargin;
2960 $this->FontFamily='';
2961 //Page orientation
2962 if(empty($orientation)) {
2963 $orientation=$this->DefOrientation;
2965 else {
2966 $orientation=strtoupper($orientation{0});
2967 if($orientation!=$this->DefOrientation) {
2968 $this->OrientationChanges[$this->page]=true;
2971 if($orientation!=$this->CurOrientation) {
2972 //Change orientation
2973 if($orientation=='P') {
2974 $this->wPt=$this->fwPt;
2975 $this->hPt=$this->fhPt;
2976 $this->w=$this->fw;
2977 $this->h=$this->fh;
2979 else {
2980 $this->wPt=$this->fhPt;
2981 $this->hPt=$this->fwPt;
2982 $this->w=$this->fh;
2983 $this->h=$this->fw;
2985 $this->PageBreakTrigger=$this->h-$this->bMargin;
2986 $this->CurOrientation=$orientation;
2991 * End of page contents
2992 * @access protected
2994 function _endpage() {
2995 $this->state=1;
2999 * Begin a new object
3000 * @access protected
3002 function _newobj() {
3003 $this->n++;
3004 $this->offsets[$this->n]=strlen($this->buffer);
3005 $this->_out($this->n.' 0 obj');
3009 * Underline text
3010 * @access protected
3012 function _dounderline($x,$y,$txt) {
3013 $up = $this->CurrentFont['up'];
3014 $ut = $this->CurrentFont['ut'];
3015 $w = $this->GetStringWidth($txt) + $this->ws * substr_count($txt,' ');
3016 return sprintf('%.2f %.2f %.2f %.2f re f', $x * $this->k, ($this->h - ($y - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt);
3020 * Extract info from a JPEG file
3021 * @access protected
3023 function _parsejpg($file) {
3024 $a=GetImageSize($file);
3025 if(empty($a)) {
3026 $this->Error('Missing or incorrect image file: '.$file);
3028 if($a[2]!=2) {
3029 $this->Error('Not a JPEG file: '.$file);
3031 if(!isset($a['channels']) or $a['channels']==3) {
3032 $colspace='DeviceRGB';
3034 elseif($a['channels']==4) {
3035 $colspace='DeviceCMYK';
3037 else {
3038 $colspace='DeviceGray';
3040 $bpc=isset($a['bits']) ? $a['bits'] : 8;
3041 //Read whole file
3042 $f=fopen($file,'rb');
3043 $data='';
3044 while(!feof($f)) {
3045 $data.=fread($f,4096);
3047 fclose($f);
3048 return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
3052 * Extract info from a PNG file
3053 * @access protected
3055 function _parsepng($file) {
3056 $f=fopen($file,'rb');
3057 if(empty($f)) {
3058 $this->Error('Can\'t open image file: '.$file);
3060 //Check signature
3061 if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
3062 $this->Error('Not a PNG file: '.$file);
3064 //Read header chunk
3065 fread($f,4);
3066 if(fread($f,4)!='IHDR') {
3067 $this->Error('Incorrect PNG file: '.$file);
3069 $w=$this->_freadint($f);
3070 $h=$this->_freadint($f);
3071 $bpc=ord(fread($f,1));
3072 if($bpc>8) {
3073 $this->Error('16-bit depth not supported: '.$file);
3075 $ct=ord(fread($f,1));
3076 if($ct==0) {
3077 $colspace='DeviceGray';
3079 elseif($ct==2) {
3080 $colspace='DeviceRGB';
3082 elseif($ct==3) {
3083 $colspace='Indexed';
3085 else {
3086 $this->Error('Alpha channel not supported: '.$file);
3088 if(ord(fread($f,1))!=0) {
3089 $this->Error('Unknown compression method: '.$file);
3091 if(ord(fread($f,1))!=0) {
3092 $this->Error('Unknown filter method: '.$file);
3094 if(ord(fread($f,1))!=0) {
3095 $this->Error('Interlacing not supported: '.$file);
3097 fread($f,4);
3098 $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
3099 //Scan chunks looking for palette, transparency and image data
3100 $pal='';
3101 $trns='';
3102 $data='';
3103 do {
3104 $n=$this->_freadint($f);
3105 $type=fread($f,4);
3106 if($type=='PLTE') {
3107 //Read palette
3108 $pal=fread($f,$n);
3109 fread($f,4);
3111 elseif($type=='tRNS') {
3112 //Read transparency info
3113 $t=fread($f,$n);
3114 if($ct==0) {
3115 $trns=array(ord(substr($t,1,1)));
3117 elseif($ct==2) {
3118 $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
3120 else {
3121 $pos=strpos($t,chr(0));
3122 if($pos!==false) {
3123 $trns=array($pos);
3126 fread($f,4);
3128 elseif($type=='IDAT') {
3129 //Read image data block
3130 $data.=fread($f,$n);
3131 fread($f,4);
3133 elseif($type=='IEND') {
3134 break;
3136 else {
3137 fread($f,$n+4);
3140 while($n);
3141 if($colspace=='Indexed' and empty($pal)) {
3142 $this->Error('Missing palette in '.$file);
3144 fclose($f);
3145 return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
3149 * Read a 4-byte integer from file
3150 * @access protected
3152 function _freadint($f) {
3153 //Read a 4-byte integer from file
3154 $a=unpack('Ni',fread($f,4));
3155 return $a['i'];
3159 * Format a text string
3160 * @access protected
3162 function _textstring($s) {
3163 if($this->isunicode) {
3164 //Convert string to UTF-16BE
3165 $s = $this->UTF8ToUTF16BE($s, true);
3167 return '('. $this->_escape($s).')';
3171 * Format a text string
3172 * @access protected
3174 function _escapetext($s) {
3175 if($this->isunicode) {
3176 //Convert string to UTF-16BE
3177 $s = $this->UTF8ToUTF16BE($s, false);
3179 return $this->_escape($s);
3183 * Add \ before \, ( and )
3184 * @access protected
3186 function _escape($s) {
3187 return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\'));
3192 * @access protected
3194 function _putstream($s) {
3195 $this->_out('stream');
3196 $this->_out($s);
3197 $this->_out('endstream');
3201 * Add a line to the document
3202 * @access protected
3204 function _out($s) {
3205 if($this->state==2) {
3206 $this->pages[$this->page] .= $s."\n";
3208 else {
3209 $this->buffer .= $s."\n";
3214 * Adds unicode fonts.<br>
3215 * Based on PDF Reference 1.3 (section 5)
3216 * @access protected
3217 * @author Nicola Asuni
3218 * @since 1.52.0.TC005 (2005-01-05)
3220 function _puttruetypeunicode($font) {
3221 // Type0 Font
3222 // A composite font—a font composed of other fonts, organized hierarchically
3223 $this->_newobj();
3224 $this->_out('<</Type /Font');
3225 $this->_out('/Subtype /Type0');
3226 $this->_out('/BaseFont /'.$font['name'].'');
3227 $this->_out('/Encoding /Identity-H'); //The horizontal identity mapping for 2-byte CIDs; may be used with CIDFonts using any Registry, Ordering, and Supplement values.
3228 $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
3229 $this->_out('>>');
3230 $this->_out('endobj');
3232 // CIDFontType2
3233 // A CIDFont whose glyph descriptions are based on TrueType font technology
3234 $this->_newobj();
3235 $this->_out('<</Type /Font');
3236 $this->_out('/Subtype /CIDFontType2');
3237 $this->_out('/BaseFont /'.$font['name'].'');
3238 $this->_out('/CIDSystemInfo '.($this->n + 1).' 0 R');
3239 $this->_out('/FontDescriptor '.($this->n + 2).' 0 R');
3240 if (isset($font['desc']['MissingWidth'])){
3241 $this->_out('/DW '.$font['desc']['MissingWidth'].''); // The default width for glyphs in the CIDFont MissingWidth
3243 $w = "";
3244 foreach ($font['cw'] as $cid => $width) {
3245 $w .= ''.$cid.' ['.$width.'] '; // define a specific width for each individual CID
3247 $this->_out('/W ['.$w.']'); // A description of the widths for the glyphs in the CIDFont
3248 $this->_out('/CIDToGIDMap '.($this->n + 3).' 0 R');
3249 $this->_out('>>');
3250 $this->_out('endobj');
3252 // CIDSystemInfo dictionary
3253 // A dictionary containing entries that define the character collection of the CIDFont.
3254 $this->_newobj();
3255 $this->_out('<</Registry (Adobe)'); // A string identifying an issuer of character collections
3256 $this->_out('/Ordering (UCS)'); // A string that uniquely names a character collection issued by a specific registry
3257 $this->_out('/Supplement 0'); // The supplement number of the character collection.
3258 $this->_out('>>');
3259 $this->_out('endobj');
3261 // Font descriptor
3262 // A font descriptor describing the CIDFont’s default metrics other than its glyph widths
3263 $this->_newobj();
3264 $this->_out('<</Type /FontDescriptor');
3265 $this->_out('/FontName /'.$font['name']);
3266 foreach ($font['desc'] as $key => $value) {
3267 $this->_out('/'.$key.' '.$value);
3269 if ($font['file']) {
3270 // A stream containing a TrueType font program
3271 $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
3273 $this->_out('>>');
3274 $this->_out('endobj');
3276 // Embed CIDToGIDMap
3277 // A specification of the mapping from CIDs to glyph indices
3278 $this->_newobj();
3279 $ctgfile = $this->_getfontpath().$font['ctg'];
3280 if(!file_exists($ctgfile)) {
3281 $this->Error('Font file not found: '.$ctgfile);
3283 $size = filesize($ctgfile);
3284 $this->_out('<</Length '.$size.'');
3285 if(substr($ctgfile, -2) == '.z') { // check file extension
3286 /* Decompresses data encoded using the public-domain
3287 zlib/deflate compression method, reproducing the
3288 original text or binary data */
3289 $this->_out('/Filter /FlateDecode');
3291 $this->_out('>>');
3292 $this->_putstream(file_get_contents($ctgfile));
3293 $this->_out('endobj');
3297 * Converts UTF-8 strings to codepoints array.<br>
3298 * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
3299 * Based on: http://www.faqs.org/rfcs/rfc3629.html
3300 * <pre>
3301 * Char. number range | UTF-8 octet sequence
3302 * (hexadecimal) | (binary)
3303 * --------------------+-----------------------------------------------
3304 * 0000 0000-0000 007F | 0xxxxxxx
3305 * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
3306 * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
3307 * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
3308 * ---------------------------------------------------------------------
3310 * ABFN notation:
3311 * ---------------------------------------------------------------------
3312 * UTF8-octets = *( UTF8-char )
3313 * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
3314 * UTF8-1 = %x00-7F
3315 * UTF8-2 = %xC2-DF UTF8-tail
3317 * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
3318 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
3319 * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
3320 * %xF4 %x80-8F 2( UTF8-tail )
3321 * UTF8-tail = %x80-BF
3322 * ---------------------------------------------------------------------
3323 * </pre>
3324 * @param string $str string to process.
3325 * @return array containing codepoints (UTF-8 characters values)
3326 * @access protected
3327 * @author Nicola Asuni
3328 * @since 1.53.0.TC005 (2005-01-05)
3330 function UTF8StringToArray($str) {
3331 if(!$this->isunicode) {
3332 return $str; // string is not in unicode
3334 $unicode = array(); // array containing unicode values
3335 $bytes = array(); // array containing single character byte sequences
3336 $numbytes = 1; // number of octetc needed to represent the UTF-8 character
3338 $str .= ""; // force $str to be a string
3339 $length = strlen($str);
3341 for($i = 0; $i < $length; $i++) {
3342 $char = ord($str{$i}); // get one string character at time
3343 if(count($bytes) == 0) { // get starting octect
3344 if ($char <= 0x7F) {
3345 $unicode[] = $char; // use the character "as is" because is ASCII
3346 $numbytes = 1;
3347 } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
3348 $bytes[] = ($char - 0xC0) << 0x06;
3349 $numbytes = 2;
3350 } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
3351 $bytes[] = ($char - 0xE0) << 0x0C;
3352 $numbytes = 3;
3353 } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
3354 $bytes[] = ($char - 0xF0) << 0x12;
3355 $numbytes = 4;
3356 } else {
3357 // use replacement character for other invalid sequences
3358 $unicode[] = 0xFFFD;
3359 $bytes = array();
3360 $numbytes = 1;
3362 } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
3363 $bytes[] = $char - 0x80;
3364 if (count($bytes) == $numbytes) {
3365 // compose UTF-8 bytes to a single unicode value
3366 $char = $bytes[0];
3367 for($j = 1; $j < $numbytes; $j++) {
3368 $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
3370 if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
3371 /* The definition of UTF-8 prohibits encoding character numbers between
3372 U+D800 and U+DFFF, which are reserved for use with the UTF-16
3373 encoding form (as surrogate pairs) and do not directly represent
3374 characters. */
3375 $unicode[] = 0xFFFD; // use replacement character
3377 else {
3378 $unicode[] = $char; // add char to array
3380 // reset data for next char
3381 $bytes = array();
3382 $numbytes = 1;
3384 } else {
3385 // use replacement character for other invalid sequences
3386 $unicode[] = 0xFFFD;
3387 $bytes = array();
3388 $numbytes = 1;
3391 return $unicode;
3395 * Converts UTF-8 strings to UTF16-BE.<br>
3396 * Based on: http://www.faqs.org/rfcs/rfc2781.html
3397 * <pre>
3398 * Encoding UTF-16:
3400 * Encoding of a single character from an ISO 10646 character value to
3401 * UTF-16 proceeds as follows. Let U be the character number, no greater
3402 * than 0x10FFFF.
3404 * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
3405 * terminate.
3407 * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
3408 * U' must be less than or equal to 0xFFFFF. That is, U' can be
3409 * represented in 20 bits.
3411 * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
3412 * 0xDC00, respectively. These integers each have 10 bits free to
3413 * encode the character value, for a total of 20 bits.
3415 * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
3416 * bits of W1 and the 10 low-order bits of U' to the 10 low-order
3417 * bits of W2. Terminate.
3419 * Graphically, steps 2 through 4 look like:
3420 * U' = yyyyyyyyyyxxxxxxxxxx
3421 * W1 = 110110yyyyyyyyyy
3422 * W2 = 110111xxxxxxxxxx
3423 * </pre>
3424 * @param string $str string to process.
3425 * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
3426 * @return string
3427 * @access protected
3428 * @author Nicola Asuni
3429 * @since 1.53.0.TC005 (2005-01-05)
3430 * @uses UTF8StringToArray
3432 function UTF8ToUTF16BE($str, $setbom=true) {
3433 if(!$this->isunicode) {
3434 return $str; // string is not in unicode
3436 $outstr = ""; // string to be returned
3437 $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
3438 $numitems = count($unicode);
3440 if ($setbom) {
3441 $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
3443 foreach($unicode as $char) {
3444 if($char == 0xFFFD) {
3445 $outstr .= "\xFF\xFD"; // replacement character
3446 } elseif ($char < 0x10000) {
3447 $outstr .= chr($char >> 0x08);
3448 $outstr .= chr($char & 0xFF);
3449 } else {
3450 $char -= 0x10000;
3451 $w1 = 0xD800 | ($char >> 0x10);
3452 $w2 = 0xDC00 | ($char & 0x3FF);
3453 $outstr .= chr($w1 >> 0x08);
3454 $outstr .= chr($w1 & 0xFF);
3455 $outstr .= chr($w2 >> 0x08);
3456 $outstr .= chr($w2 & 0xFF);
3459 return $outstr;
3462 // ====================================================
3465 * Set header font.
3466 * @param array $font font
3467 * @since 1.1
3469 function setHeaderFont($font) {
3470 $this->header_font = $font;
3474 * Set footer font.
3475 * @param array $font font
3476 * @since 1.1
3478 function setFooterFont($font) {
3479 $this->footer_font = $font;
3483 * Set language array.
3484 * @param array $language
3485 * @since 1.1
3487 function setLanguageArray($language) {
3488 $this->l = $language;
3492 * Set document barcode.
3493 * @param string $bc barcode
3495 function setBarcode($bc="") {
3496 $this->barcode = $bc;
3500 * Print Barcode.
3501 * @param int $x x position in user units
3502 * @param int $y y position in user units
3503 * @param int $w width in user units
3504 * @param int $h height position in user units
3505 * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
3506 * @param string $style barcode style
3507 * @param string $font font for text
3508 * @param int $xres x resolution
3509 * @param string $code code to print
3511 function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
3512 require_once(dirname(__FILE__)."/barcode/barcode.php");
3513 require_once(dirname(__FILE__)."/barcode/i25object.php");
3514 require_once(dirname(__FILE__)."/barcode/c39object.php");
3515 require_once(dirname(__FILE__)."/barcode/c128aobject.php");
3516 require_once(dirname(__FILE__)."/barcode/c128bobject.php");
3517 require_once(dirname(__FILE__)."/barcode/c128cobject.php");
3519 if (empty($code)) {
3520 return;
3523 if (empty($style)) {
3524 $style = BCS_ALIGN_LEFT;
3525 $style |= BCS_IMAGE_PNG;
3526 $style |= BCS_TRANSPARENT;
3527 //$style |= BCS_BORDER;
3528 //$style |= BCS_DRAW_TEXT;
3529 //$style |= BCS_STRETCH_TEXT;
3530 //$style |= BCS_REVERSE_COLOR;
3532 if (empty($font)) {$font = BCD_DEFAULT_FONT;}
3533 if (empty($xres)) {$xres = BCD_DEFAULT_XRES;}
3535 $scale_factor = 1.5 * $xres * $this->k;
3536 $bc_w = round($w * $scale_factor); //width in points
3537 $bc_h = round($h * $scale_factor); //height in points
3539 switch (strtoupper($type)) {
3540 case "I25": {
3541 $obj = new I25Object($bc_w, $bc_h, $style, $code);
3542 break;
3544 case "C128A": {
3545 $obj = new C128AObject($bc_w, $bc_h, $style, $code);
3546 break;
3548 default:
3549 case "C128B": {
3550 $obj = new C128BObject($bc_w, $bc_h, $style, $code);
3551 break;
3553 case "C128C": {
3554 $obj = new C128CObject($bc_w, $bc_h, $style, $code);
3555 break;
3557 case "C39": {
3558 $obj = new C39Object($bc_w, $bc_h, $style, $code);
3559 break;
3563 $obj->SetFont($font);
3564 $obj->DrawObject($xres);
3566 //use a temporary file....
3567 $tmpName = tempnam(K_PATH_CACHE,'img');
3568 imagepng($obj->getImage(), $tmpName);
3569 $this->Image($tmpName, $x, $y, $w, $h, 'png');
3570 $obj->DestroyObject();
3571 unset($obj);
3572 unlink($tmpName);
3576 * Returns the PDF data.
3578 function getPDFData() {
3579 if($this->state < 3) {
3580 $this->Close();
3582 return $this->buffer;
3585 // --- HTML PARSER FUNCTIONS ---
3588 * Allows to preserve some HTML formatting.<br />
3589 * Supports: h1, h2, h3, h4, h5, h6, b, u, i, a, img, p, br, strong, em, font, blockquote, li, ul, ol, hr, td, th, tr, table, sup, sub, small
3590 * @param string $html text to display
3591 * @param boolean $ln if true add a new line after text (default = true)
3592 * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
3594 function writeHTML($html, $ln=true, $fill=0) {
3596 // store some variables
3597 $html=strip_tags($html,"<h1><h2><h3><h4><h5><h6><b><u><i><a><img><p><br><strong><em><font><blockquote><li><ul><ol><hr><td><th><tr><table><sup><sub><small>"); //remove all unsupported tags
3598 //replace carriage returns, newlines and tabs
3599 $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
3600 $html = strtr($html, $repTable);
3601 $pattern = '/(<[^>]+>)/Uu';
3602 $a = preg_split($pattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); //explodes the string
3604 if (empty($this->lasth)) {
3605 //set row height
3606 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
3609 foreach($a as $key=>$element) {
3610 if (!preg_match($pattern, $element)) {
3611 //Text
3612 if($this->HREF) {
3613 $this->addHtmlLink($this->HREF, $element, $fill);
3615 elseif($this->tdbegin) {
3616 if((strlen(trim($element)) > 0) AND ($element != "&nbsp;")) {
3617 $this->Cell($this->tdwidth, $this->tdheight, $this->unhtmlentities($element), $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
3619 elseif($element == "&nbsp;") {
3620 $this->Cell($this->tdwidth, $this->tdheight, '', $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
3623 else {
3624 $this->Write($this->lasth, stripslashes($this->unhtmlentities($element)), '', $fill);
3627 else {
3628 $element = substr($element, 1, -1);
3629 //Tag
3630 if($element{0}=='/') {
3631 $this->closedHTMLTagHandler(strtolower(substr($element, 1)));
3633 else {
3634 //Extract attributes
3635 // get tag name
3636 preg_match('/([a-zA-Z0-9]*)/', $element, $tag);
3637 $tag = strtolower($tag[0]);
3638 // get attributes
3639 preg_match_all('/([^=\s]*)=["\']?([^"\']*)["\']?/', $element, $attr_array, PREG_PATTERN_ORDER);
3640 $attr = array(); // reset attribute array
3641 while(list($id,$name)=each($attr_array[1])) {
3642 $attr[strtolower($name)] = $attr_array[2][$id];
3644 $this->openHTMLTagHandler($tag, $attr, $fill);
3648 if ($ln) {
3649 $this->Ln($this->lasth);
3654 * Prints a cell (rectangular area) with optional borders, background color and html text string. 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 />
3655 * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
3656 * @param float $w Cell width. If 0, the cell extends up to the right margin.
3657 * @param float $h Cell minimum height. The cell extends automatically if needed.
3658 * @param float $x upper-left corner X coordinate
3659 * @param float $y upper-left corner Y coordinate
3660 * @param string $html html text to print. Default value: empty string.
3661 * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either 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>
3662 * @param int $ln 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</li><li>2: below</li></ul>
3663 Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
3664 * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
3665 * @see Cell()
3667 function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0) {
3669 if (empty($this->lasth)) {
3670 //set row height
3671 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
3674 if (empty($x)) {
3675 $x = $this->GetX();
3677 if (empty($y)) {
3678 $y = $this->GetY();
3681 // get current page number
3682 $pagenum = $this->page;
3684 $this->SetX($x);
3685 $this->SetY($y);
3687 if(empty($w)) {
3688 $w = $this->fw - $x - $this->rMargin;
3691 // store original margin values
3692 $lMargin = $this->lMargin;
3693 $rMargin = $this->rMargin;
3695 // set new margin values
3696 $this->SetLeftMargin($x);
3697 $this->SetRightMargin($this->fw - $x - $w);
3699 // calculate remaining vertical space on page
3700 $restspace = $this->getPageHeight() - $this->GetY() - $this->getBreakMargin();
3702 $this->writeHTML($html, true, $fill); // write html text
3704 $currentY = $this->GetY();
3706 // check if a new page has been created
3707 if ($this->page > $pagenum) {
3708 // design a cell around the text on first page
3709 $currentpage = $this->page;
3710 $this->page = $pagenum;
3711 $this->SetY($this->getPageHeight() - $restspace - $this->getBreakMargin());
3712 $h = $restspace - 1;
3713 $this->Cell($w, $h, "", $border, $ln, 'L', 0);
3714 // design a cell around the text on last page
3715 $this->page = $currentpage;
3716 $h = $currentY - $this->tMargin;
3717 $this->SetY($this->tMargin); // put cursor at the beginning of text
3718 $this->Cell($w, $h, "", $border, $ln, 'L', 0);
3719 } else {
3720 $h = max($h, ($currentY - $y));
3721 $this->SetY($y); // put cursor at the beginning of text
3722 // design a cell around the text
3723 $this->Cell($w, $h, "", $border, $ln, 'L', 0);
3726 // restore original margin values
3727 $this->SetLeftMargin($lMargin);
3728 $this->SetRightMargin($rMargin);
3730 if ($ln) {
3731 $this->Ln(0);
3736 * Process opening tags.
3737 * @param string $tag tag name (in uppercase)
3738 * @param string $attr tag attribute (in uppercase)
3739 * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
3740 * @access private
3742 function openHTMLTagHandler($tag, $attr, $fill=0) {
3743 //Opening tag
3744 switch($tag) {
3745 case 'table': {
3746 if ((isset($attr['border'])) AND ($attr['border'] != '')) {
3747 $this->tableborder = $attr['border'];
3749 else {
3750 $this->tableborder = 0;
3752 break;
3754 case 'tr': {
3755 break;
3757 case 'td':
3758 case 'th': {
3759 if ((isset($attr['width'])) AND ($attr['width'] != '')) {
3760 $this->tdwidth = ($attr['width']/4);
3762 else {
3763 $this->tdwidth = (($this->w - $this->lMargin - $this->rMargin) / $this->default_table_columns);
3765 if ((isset($attr['height'])) AND ($attr['height'] != '')) {
3766 $this->tdheight=($attr['height'] / $this->k);
3768 else {
3769 $this->tdheight = $this->lasth;
3771 if ((isset($attr['align'])) AND ($attr['align'] != '')) {
3772 switch ($attr['align']) {
3773 case 'center': {
3774 $this->tdalign = "C";
3775 break;
3777 case 'right': {
3778 $this->tdalign = "R";
3779 break;
3781 default:
3782 case 'left': {
3783 $this->tdalign = "L";
3784 break;
3788 if ((isset($attr['bgcolor'])) AND ($attr['bgcolor'] != '')) {
3789 $coul = $this->convertColorHexToDec($attr['bgcolor']);
3790 $this->SetFillColor($coul['R'], $coul['G'], $coul['B']);
3791 $this->tdbgcolor=true;
3793 $this->tdbegin=true;
3794 break;
3796 case 'hr': {
3797 $this->Ln();
3798 if ((isset($attr['width'])) AND ($attr['width'] != '')) {
3799 $hrWidth = $attr['width'];
3801 else {
3802 $hrWidth = $this->w - $this->lMargin - $this->rMargin;
3804 $x = $this->GetX();
3805 $y = $this->GetY();
3806 $this->SetLineWidth(0.2);
3807 $this->Line($x, $y, $x + $hrWidth, $y);
3808 $this->SetLineWidth(0.2);
3809 $this->Ln();
3810 break;
3812 case 'strong': {
3813 $this->setStyle('b', true);
3814 break;
3816 case 'em': {
3817 $this->setStyle('i', true);
3818 break;
3820 case 'b':
3821 case 'i':
3822 case 'u': {
3823 $this->setStyle($tag, true);
3824 break;
3826 case 'a': {
3827 $this->HREF = $attr['href'];
3828 break;
3830 case 'img': {
3831 if(isset($attr['src'])) {
3832 // replace relative path with real server path
3833 $attr['src'] = str_replace(K_PATH_URL_CACHE, K_PATH_CACHE, $attr['src']);
3834 if(!isset($attr['width'])) {
3835 $attr['width'] = 0;
3837 if(!isset($attr['height'])) {
3838 $attr['height'] = 0;
3841 $this->Image($attr['src'], $this->GetX(),$this->GetY(), $this->pixelsToMillimeters($attr['width']), $this->pixelsToMillimeters($attr['height']));
3842 //$this->SetX($this->img_rb_x);
3843 $this->SetY($this->img_rb_y);
3846 break;
3848 case 'ul': {
3849 $this->listordered = false;
3850 $this->listcount = 0;
3851 break;
3853 case 'ol': {
3854 $this->listordered = true;
3855 $this->listcount = 0;
3856 break;
3858 case 'li': {
3859 $this->Ln();
3860 if ($this->listordered) {
3861 $this->lispacer = " ".(++$this->listcount).". ";
3863 else {
3864 //unordered list simbol
3865 $this->lispacer = " - ";
3867 $this->Write($this->lasth, $this->lispacer, '', $fill);
3868 break;
3870 case 'tr':
3871 case 'blockquote':
3872 case 'br': {
3873 $this->Ln();
3874 if(strlen($this->lispacer) > 0) {
3875 $this->x += $this->GetStringWidth($this->lispacer);
3877 break;
3879 case 'p': {
3880 $this->Ln();
3881 $this->Ln();
3882 break;
3884 case 'sup': {
3885 $currentFontSize = $this->FontSize;
3886 $this->tempfontsize = $this->FontSizePt;
3887 $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
3888 $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
3889 break;
3891 case 'sub': {
3892 $currentFontSize = $this->FontSize;
3893 $this->tempfontsize = $this->FontSizePt;
3894 $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
3895 $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
3896 break;
3898 case 'small': {
3899 $currentFontSize = $this->FontSize;
3900 $this->tempfontsize = $this->FontSizePt;
3901 $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
3902 $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)/3));
3903 break;
3905 case 'font': {
3906 if (isset($attr['color']) AND $attr['color']!='') {
3907 $coul = $this->convertColorHexToDec($attr['color']);
3908 $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
3909 $this->issetcolor=true;
3911 if (isset($attr['face']) and in_array(strtolower($attr['face']), $this->fontlist)) {
3912 $this->SetFont(strtolower($attr['FACE']));
3913 $this->issetfont=true;
3915 if (isset($attr['size'])) {
3916 $headsize = intval($attr['size']);
3917 } else {
3918 $headsize = 0;
3920 $currentFontSize = $this->FontSize;
3921 $this->tempfontsize = $this->FontSizePt;
3922 $this->SetFontSize($this->FontSizePt + $headsize);
3923 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
3924 break;
3926 case 'h1':
3927 case 'h2':
3928 case 'h3':
3929 case 'h4':
3930 case 'h5':
3931 case 'h6': {
3932 $headsize = (4 - substr($tag, 1)) * 2;
3933 $currentFontSize = $this->FontSize;
3934 $this->tempfontsize = $this->FontSizePt;
3935 $this->SetFontSize($this->FontSizePt + $headsize);
3936 $this->setStyle('b', true);
3937 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
3938 break;
3944 * Process closing tags.
3945 * @param string $tag tag name (in uppercase)
3946 * @access private
3948 function closedHTMLTagHandler($tag) {
3949 //Closing tag
3950 switch($tag) {
3951 case 'td':
3952 case 'th': {
3953 $this->tdbegin = false;
3954 $this->tdwidth = 0;
3955 $this->tdheight = 0;
3956 $this->tdalign = "L";
3957 $this->tdbgcolor = false;
3958 $this->SetFillColor($this->prevFillColor[0], $this->prevFillColor[1], $this->prevFillColor[2]);
3959 break;
3961 case 'tr': {
3962 $this->Ln();
3963 break;
3965 case 'table': {
3966 $this->tableborder=0;
3967 break;
3969 case 'strong': {
3970 $this->setStyle('b', false);
3972 case 'em': {
3973 $this->setStyle('i', false);
3975 case 'b':
3976 case 'i':
3977 case 'u': {
3978 $this->setStyle($tag, false);
3979 break;
3981 case 'a': {
3982 $this->HREF = '';
3983 break;
3985 case 'sup': {
3986 $currentFontSize = $this->FontSize;
3987 $this->SetFontSize($this->tempfontsize);
3988 $this->tempfontsize = $this->FontSizePt;
3989 $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
3990 break;
3992 case 'sub': {
3993 $currentFontSize = $this->FontSize;
3994 $this->SetFontSize($this->tempfontsize);
3995 $this->tempfontsize = $this->FontSizePt;
3996 $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
3997 break;
3999 case 'small': {
4000 $currentFontSize = $this->FontSize;
4001 $this->SetFontSize($this->tempfontsize);
4002 $this->tempfontsize = $this->FontSizePt;
4003 $this->SetXY($this->GetX(), $this->GetY() - (($this->FontSize - $currentFontSize)/3));
4004 break;
4006 case 'font': {
4007 if ($this->issetcolor == true) {
4008 $this->SetTextColor($this->prevTextColor[0], $this->prevTextColor[1], $this->prevTextColor[2]);
4010 if ($this->issetfont) {
4011 $this->FontFamily = $this->prevFontFamily;
4012 $this->FontStyle = $this->prevFontStyle;
4013 $this->SetFont($this->FontFamily);
4014 $this->issetfont = false;
4016 $currentFontSize = $this->FontSize;
4017 $this->SetFontSize($this->tempfontsize);
4018 $this->tempfontsize = $this->FontSizePt;
4019 //$this->TextColor = $this->prevTextColor;
4020 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
4021 break;
4023 case 'ul': {
4024 $this->Ln();
4025 break;
4027 case 'ol': {
4028 $this->Ln();
4029 break;
4031 case 'li': {
4032 $this->lispacer = "";
4033 break;
4035 case 'h1':
4036 case 'h2':
4037 case 'h3':
4038 case 'h4':
4039 case 'h5':
4040 case 'h6': {
4041 $currentFontSize = $this->FontSize;
4042 $this->SetFontSize($this->tempfontsize);
4043 $this->tempfontsize = $this->FontSizePt;
4044 $this->setStyle('b', false);
4045 $this->Ln();
4046 $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
4047 break;
4049 default : {
4050 break;
4056 * Sets font style.
4057 * @param string $tag tag name (in lowercase)
4058 * @param boolean $enable
4059 * @access private
4061 function setStyle($tag, $enable) {
4062 //Modify style and select corresponding font
4063 $this->$tag += ($enable ? 1 : -1);
4064 $style='';
4065 foreach(array('b', 'i', 'u') as $s) {
4066 if($this->$s > 0) {
4067 $style .= $s;
4070 $this->SetFont('', $style);
4074 * Output anchor link.
4075 * @param string $url link URL
4076 * @param string $name link name
4077 * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
4078 * @access public
4080 function addHtmlLink($url, $name, $fill=0) {
4081 //Put a hyperlink
4082 $this->SetTextColor(0, 0, 255);
4083 $this->setStyle('u', true);
4084 $this->Write($this->lasth, $name, $url, $fill);
4085 $this->setStyle('u', false);
4086 $this->SetTextColor(0);
4090 * Returns an associative array (keys: R,G,B) from
4091 * a hex html code (e.g. #3FE5AA).
4092 * @param string $color hexadecimal html color [#rrggbb]
4093 * @return array
4094 * @access private
4096 function convertColorHexToDec($color = "#000000"){
4097 $tbl_color = array();
4098 $tbl_color['R'] = hexdec(substr($color, 1, 2));
4099 $tbl_color['G'] = hexdec(substr($color, 3, 2));
4100 $tbl_color['B'] = hexdec(substr($color, 5, 2));
4101 return $tbl_color;
4105 * Converts pixels to millimeters in 72 dpi.
4106 * @param int $px pixels
4107 * @return float millimeters
4108 * @access private
4110 function pixelsToMillimeters($px){
4111 return $px * 25.4 / 72;
4115 * Reverse function for htmlentities.
4116 * Convert entities in UTF-8.
4118 * @param $text_to_convert Text to convert.
4119 * @return string converted
4121 function unhtmlentities($text_to_convert) {
4122 require_once(dirname(__FILE__).'/html_entity_decode_php4.php');
4123 return html_entity_decode_php4($text_to_convert);
4125 } // END OF CLASS
4127 //Handle special IE contype request
4128 if(isset($_SERVER['HTTP_USER_AGENT']) AND ($_SERVER['HTTP_USER_AGENT']=='contype')) {
4129 header('Content-Type: application/pdf');
4130 exit;
4134 //============================================================+
4135 // END OF FILE
4136 //============================================================+