PageRenderTime 556ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 3ms

/lib/tcpdf/tcpdf.php

https://bitbucket.org/ceu/moodle_demo
PHP | 4137 lines | 2321 code | 305 blank | 1511 comment | 373 complexity | 4e978c04afa0fd8cfe9fc5ed9a91e829 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  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.
  13. //
  14. // IMPORTANT:
  15. // This class is an extension and improvement of the public Domain
  16. // FPDF class by Olivier Plathey (http://www.fpdf.org).
  17. //
  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. //============================================================+
  31. /**
  32. * include configuration file
  33. */
  34. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  35. /**
  36. * TCPDF Class.
  37. * @package com.tecnick.tcpdf
  38. */
  39. /**
  40. * This is a PHP4 class for generating PDF files on-the-fly without requiring external extensions.<br>
  41. * TCPDF project (http://tcpdf.sourceforge.net) is based on the public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
  42. * <h3>TCPDF main changes from FPDF are:</h3><ul>
  43. * <li>PHP4 porting</li>
  44. * <li>UTF-8 Unicode support</li>
  45. * <li>source code clean up</li>
  46. * <li>code style and formatting</li>
  47. * <li>source code documentation using phpDocumentor (www.phpdoc.org)</li>
  48. * <li>All ISO page formats were included</li>
  49. * <li>image scale factor</li>
  50. * <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>
  51. * <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>
  52. * <li>defines standard Header() and Footer() methods.</li>
  53. * </ul>
  54. * Tools to encode your unicode fonts are on fonts/ttf2ufm directory.</p>
  55. * @name TCPDF
  56. * @package com.tecnick.tcpdf
  57. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  58. * @author Nicola Asuni
  59. * @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
  60. * @link http://tcpdf.sourceforge.net
  61. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  62. @version 1.53.0.TC023_PHP4
  63. */
  64. if(!class_exists('TCPDF')) {
  65. /**
  66. * define default PDF document producer
  67. */
  68. define('PDF_PRODUCER','TCPDF 1.53.0.TC023_PHP4 (http://tcpdf.sourceforge.net)');
  69. /**
  70. * This is a PHP4 class for generating PDF files on-the-fly without requiring external extensions.<br>
  71. * This class is an extension and improvement of the FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
  72. * 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>
  73. * TCPDF project (http://tcpdf.sourceforge.net) is based on the public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
  74. * To add your own TTF fonts please read /fonts/README.TXT
  75. * @name TCPDF
  76. * @package com.tecnick.tcpdf
  77. * @version 1.53.0.TC023
  78. * @author Nicola Asuni
  79. * @link http://tcpdf.sourceforge.net
  80. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  81. */
  82. class TCPDF {
  83. //var properties
  84. /**
  85. * @var current page number
  86. * @access protected
  87. */
  88. var $page;
  89. /**
  90. * @var current object number
  91. * @access protected
  92. */
  93. var $n;
  94. /**
  95. * @var array of object offsets
  96. * @access protected
  97. */
  98. var $offsets;
  99. /**
  100. * @var buffer holding in-memory PDF
  101. * @access protected
  102. */
  103. var $buffer;
  104. /**
  105. * @var array containing pages
  106. * @access protected
  107. */
  108. var $pages;
  109. /**
  110. * @var current document state
  111. * @access protected
  112. */
  113. var $state;
  114. /**
  115. * @var compression flag
  116. * @access protected
  117. */
  118. var $compress;
  119. /**
  120. * @var default orientation
  121. * @access protected
  122. */
  123. var $DefOrientation;
  124. /**
  125. * @var current orientation
  126. * @access protected
  127. */
  128. var $CurOrientation;
  129. /**
  130. * @var array indicating orientation changes
  131. * @access protected
  132. */
  133. var $OrientationChanges;
  134. /**
  135. * @var scale factor (number of points in user unit)
  136. * @access protected
  137. */
  138. var $k;
  139. /**
  140. * @var width of page format in points
  141. * @access protected
  142. */
  143. var $fwPt;
  144. /**
  145. * @var height of page format in points
  146. * @access protected
  147. */
  148. var $fhPt;
  149. /**
  150. * @var width of page format in user unit
  151. * @access protected
  152. */
  153. var $fw;
  154. /**
  155. * @var height of page format in user unit
  156. * @access protected
  157. */
  158. var $fh;
  159. /**
  160. * @var current width of page in points
  161. * @access protected
  162. */
  163. var $wPt;
  164. /**
  165. * @var current height of page in points
  166. * @access protected
  167. */
  168. var $hPt;
  169. /**
  170. * @var current width of page in user unit
  171. * @access protected
  172. */
  173. var $w;
  174. /**
  175. * @var current height of page in user unit
  176. * @access protected
  177. */
  178. var $h;
  179. /**
  180. * @var left margin
  181. * @access protected
  182. */
  183. var $lMargin;
  184. /**
  185. * @var top margin
  186. * @access protected
  187. */
  188. var $tMargin;
  189. /**
  190. * @var right margin
  191. * @access protected
  192. */
  193. var $rMargin;
  194. /**
  195. * @var page break margin
  196. * @access protected
  197. */
  198. var $bMargin;
  199. /**
  200. * @var cell margin
  201. * @access protected
  202. */
  203. var $cMargin;
  204. /**
  205. * @var current horizontal position in user unit for cell positioning
  206. * @access protected
  207. */
  208. var $x;
  209. /**
  210. * @var current vertical position in user unit for cell positioning
  211. * @access protected
  212. */
  213. var $y;
  214. /**
  215. * @var height of last cell printed
  216. * @access protected
  217. */
  218. var $lasth;
  219. /**
  220. * @var line width in user unit
  221. * @access protected
  222. */
  223. var $LineWidth;
  224. /**
  225. * @var array of standard font names
  226. * @access protected
  227. */
  228. var $CoreFonts;
  229. /**
  230. * @var array of used fonts
  231. * @access protected
  232. */
  233. var $fonts;
  234. /**
  235. * @var array of font files
  236. * @access protected
  237. */
  238. var $FontFiles;
  239. /**
  240. * @var array of encoding differences
  241. * @access protected
  242. */
  243. var $diffs;
  244. /**
  245. * @var array of used images
  246. * @access protected
  247. */
  248. var $images;
  249. /**
  250. * @var array of links in pages
  251. * @access protected
  252. */
  253. var $PageLinks;
  254. /**
  255. * @var array of internal links
  256. * @access protected
  257. */
  258. var $links;
  259. /**
  260. * @var current font family
  261. * @access protected
  262. */
  263. var $FontFamily;
  264. /**
  265. * @var current font style
  266. * @access protected
  267. */
  268. var $FontStyle;
  269. /**
  270. * @var underlining flag
  271. * @access protected
  272. */
  273. var $underline;
  274. /**
  275. * @var current font info
  276. * @access protected
  277. */
  278. var $CurrentFont;
  279. /**
  280. * @var current font size in points
  281. * @access protected
  282. */
  283. var $FontSizePt;
  284. /**
  285. * @var current font size in user unit
  286. * @access protected
  287. */
  288. var $FontSize;
  289. /**
  290. * @var commands for drawing color
  291. * @access protected
  292. */
  293. var $DrawColor;
  294. /**
  295. * @var commands for filling color
  296. * @access protected
  297. */
  298. var $FillColor;
  299. /**
  300. * @var commands for text color
  301. * @access protected
  302. */
  303. var $TextColor;
  304. /**
  305. * @var indicates whether fill and text colors are different
  306. * @access protected
  307. */
  308. var $ColorFlag;
  309. /**
  310. * @var word spacing
  311. * @access protected
  312. */
  313. var $ws;
  314. /**
  315. * @var automatic page breaking
  316. * @access protected
  317. */
  318. var $AutoPageBreak;
  319. /**
  320. * @var threshold used to trigger page breaks
  321. * @access protected
  322. */
  323. var $PageBreakTrigger;
  324. /**
  325. * @var flag set when processing footer
  326. * @access protected
  327. */
  328. var $InFooter;
  329. /**
  330. * @var zoom display mode
  331. * @access protected
  332. */
  333. var $ZoomMode;
  334. /**
  335. * @var layout display mode
  336. * @access protected
  337. */
  338. var $LayoutMode;
  339. /**
  340. * @var title
  341. * @access protected
  342. */
  343. var $title;
  344. /**
  345. * @var subject
  346. * @access protected
  347. */
  348. var $subject;
  349. /**
  350. * @var author
  351. * @access protected
  352. */
  353. var $author;
  354. /**
  355. * @var keywords
  356. * @access protected
  357. */
  358. var $keywords;
  359. /**
  360. * @var creator
  361. * @access protected
  362. */
  363. var $creator;
  364. /**
  365. * @var alias for total number of pages
  366. * @access protected
  367. */
  368. var $AliasNbPages;
  369. /**
  370. * @var right-bottom corner X coordinate of inserted image
  371. * @since 2002-07-31
  372. * @author Nicola Asuni
  373. * @access protected
  374. */
  375. var $img_rb_x;
  376. /**
  377. * @var right-bottom corner Y coordinate of inserted image
  378. * @since 2002-07-31
  379. * @author Nicola Asuni
  380. * @access protected
  381. */
  382. var $img_rb_y;
  383. /**
  384. * @var image scale factor
  385. * @since 2004-06-14
  386. * @author Nicola Asuni
  387. * @access protected
  388. */
  389. var $imgscale = 1;
  390. /**
  391. * @var boolean set to true when the input text is unicode (require unicode fonts)
  392. * @since 2005-01-02
  393. * @author Nicola Asuni
  394. * @access protected
  395. */
  396. var $isunicode = false;
  397. /**
  398. * @var PDF version
  399. * @since 1.5.3
  400. * @access protected
  401. */
  402. var $PDFVersion = "1.3";
  403. // ----------------------
  404. /**
  405. * @var Minimum distance between header and top page margin.
  406. * @access private
  407. */
  408. var $header_margin;
  409. /**
  410. * @var Minimum distance between footer and bottom page margin.
  411. * @access private
  412. */
  413. var $footer_margin;
  414. /**
  415. * @var original left margin value
  416. * @access private
  417. * @since 1.53.0.TC013
  418. */
  419. var $original_lMargin;
  420. /**
  421. * @var original right margin value
  422. * @access private
  423. * @since 1.53.0.TC013
  424. */
  425. var $original_rMargin;
  426. /**
  427. * @var Header font.
  428. * @access private
  429. */
  430. var $header_font;
  431. /**
  432. * @var Footer font.
  433. * @access private
  434. */
  435. var $footer_font;
  436. /**
  437. * @var Language templates.
  438. * @access private
  439. */
  440. var $l;
  441. /**
  442. * @var Barcode to print on page footer (only if set).
  443. * @access private
  444. */
  445. var $barcode = false;
  446. /**
  447. * @var If true prints header
  448. * @access private
  449. */
  450. var $print_header = true;
  451. /**
  452. * @var If true prints footer.
  453. * @access private
  454. */
  455. var $print_footer = true;
  456. /**
  457. * @var Header width (0 = full page width).
  458. * @access private
  459. */
  460. var $header_width = 0;
  461. /**
  462. * @var Header image logo.
  463. * @access private
  464. */
  465. var $header_logo = "";
  466. /**
  467. * @var Header image logo width in mm.
  468. * @access private
  469. */
  470. var $header_logo_width = 30;
  471. /**
  472. * @var String to print as title on document header.
  473. * @access private
  474. */
  475. var $header_title = "";
  476. /**
  477. * @var String to print on document header.
  478. * @access private
  479. */
  480. var $header_string = "";
  481. /**
  482. * @var Default number of columns for html table.
  483. * @access private
  484. */
  485. var $default_table_columns = 4;
  486. // variables for html parser
  487. /**
  488. * @var HTML PARSER: store current link.
  489. * @access private
  490. */
  491. var $HREF;
  492. /**
  493. * @var HTML PARSER: store font list.
  494. * @access private
  495. */
  496. var $fontList;
  497. /**
  498. * @var HTML PARSER: true when font attribute is set.
  499. * @access private
  500. */
  501. var $issetfont;
  502. /**
  503. * @var HTML PARSER: true when color attribute is set.
  504. * @access private
  505. */
  506. var $issetcolor;
  507. /**
  508. * @var HTML PARSER: true in case of ordered list (OL), false otherwise.
  509. * @access private
  510. */
  511. var $listordered = false;
  512. /**
  513. * @var HTML PARSER: count list items.
  514. * @access private
  515. */
  516. var $listcount = 0;
  517. /**
  518. * @var HTML PARSER: size of table border.
  519. * @access private
  520. */
  521. var $tableborder = 0;
  522. /**
  523. * @var HTML PARSER: true at the beginning of table.
  524. * @access private
  525. */
  526. var $tdbegin = false;
  527. /**
  528. * @var HTML PARSER: table width.
  529. * @access private
  530. */
  531. var $tdwidth = 0;
  532. /**
  533. * @var HTML PARSER: table height.
  534. * @access private
  535. */
  536. var $tdheight = 0;
  537. /**
  538. * @var HTML PARSER: table align.
  539. * @access private
  540. */
  541. var $tdalign = "L";
  542. /**
  543. * @var HTML PARSER: table background color.
  544. * @access private
  545. */
  546. var $tdbgcolor = false;
  547. /**
  548. * @var Store temporary font size in points.
  549. * @access private
  550. */
  551. var $tempfontsize = 10;
  552. /**
  553. * @var Bold font style status.
  554. * @access private
  555. */
  556. var $b;
  557. /**
  558. * @var Underlined font style status.
  559. * @access private
  560. */
  561. var $u;
  562. /**
  563. * @var Italic font style status.
  564. * @access private
  565. */
  566. var $i;
  567. /**
  568. * @var spacer for LI tags.
  569. * @access private
  570. */
  571. var $lispacer = "";
  572. /**
  573. * @var default encoding
  574. * @access private
  575. * @since 1.53.0.TC010
  576. */
  577. var $encoding = "UTF-8";
  578. /**
  579. * @var PHP internal encoding
  580. * @access private
  581. * @since 1.53.0.TC016
  582. */
  583. var $internal_encoding;
  584. /**
  585. * @var store previous fill color as RGB array
  586. * @access private
  587. * @since 1.53.0.TC017
  588. */
  589. var $prevFillColor = array(255,255,255);
  590. /**
  591. * @var store previous text color as RGB array
  592. * @access private
  593. * @since 1.53.0.TC017
  594. */
  595. var $prevTextColor = array(0,0,0);
  596. /**
  597. * @var store previous font family
  598. * @access private
  599. * @since 1.53.0.TC017
  600. */
  601. var $prevFontFamily;
  602. /**
  603. * @var store previous font style
  604. * @access private
  605. * @since 1.53.0.TC017
  606. */
  607. var $prevFontStyle;
  608. //------------------------------------------------------------
  609. // var methods
  610. //------------------------------------------------------------
  611. /**
  612. * This is the class constructor.
  613. * It allows to set up the page format, the orientation and
  614. * the measure unit used in all the methods (except for the font sizes).
  615. * @since 1.0
  616. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  617. * @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.
  618. * @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>
  619. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  620. * @param String $encoding charset encoding; default is UTF-8
  621. */
  622. function TCPDF($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding="UTF-8") {
  623. /* Set internal character encoding to ASCII */
  624. if (function_exists("mb_internal_encoding") AND mb_internal_encoding()) {
  625. $this->internal_encoding = mb_internal_encoding();
  626. mb_internal_encoding("ASCII");
  627. }
  628. //Some checks
  629. $this->_dochecks();
  630. //Initialization of properties
  631. $this->isunicode=$unicode;
  632. $this->page=0;
  633. $this->n=2;
  634. $this->buffer='';
  635. $this->pages=array();
  636. $this->OrientationChanges=array();
  637. $this->state=0;
  638. $this->fonts=array();
  639. $this->FontFiles=array();
  640. $this->diffs=array();
  641. $this->images=array();
  642. $this->links=array();
  643. $this->InFooter=false;
  644. $this->lasth=0;
  645. $this->FontFamily='';
  646. $this->FontStyle='';
  647. $this->FontSizePt=12;
  648. $this->underline=false;
  649. $this->DrawColor='0 G';
  650. $this->FillColor='0 g';
  651. $this->TextColor='0 g';
  652. $this->ColorFlag=false;
  653. $this->ws=0;
  654. //Standard Unicode fonts
  655. $this->CoreFonts=array(
  656. 'courier'=>'Courier',
  657. 'courierB'=>'Courier-Bold',
  658. 'courierI'=>'Courier-Oblique',
  659. 'courierBI'=>'Courier-BoldOblique',
  660. 'helvetica'=>'Helvetica',
  661. 'helveticaB'=>'Helvetica-Bold',
  662. 'helveticaI'=>'Helvetica-Oblique',
  663. 'helveticaBI'=>'Helvetica-BoldOblique',
  664. 'times'=>'Times-Roman',
  665. 'timesB'=>'Times-Bold',
  666. 'timesI'=>'Times-Italic',
  667. 'timesBI'=>'Times-BoldItalic',
  668. 'symbol'=>'Symbol',
  669. 'zapfdingbats'=>'ZapfDingbats'
  670. );
  671. //Scale factor
  672. // 2003-06-11 - Nicola Asuni : changed if/else with switch statement
  673. switch (strtolower($unit)){
  674. case 'pt': {$this->k=1; break;}
  675. case 'mm': {$this->k=72/25.4; break;}
  676. case 'cm': {$this->k=72/2.54; break;}
  677. case 'in': {$this->k=72; break;}
  678. default : {$this->Error('Incorrect unit: '.$unit); break;}
  679. }
  680. //Page format
  681. if(is_string($format)) {
  682. // 2002-07-24 - Nicola Asuni (info@tecnick.com)
  683. // Added new page formats (45 standard ISO paper formats and 4 american common formats).
  684. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  685. switch (strtoupper($format)){
  686. case '4A0': {$format = array(4767.87,6740.79); break;}
  687. case '2A0': {$format = array(3370.39,4767.87); break;}
  688. case 'A0': {$format = array(2383.94,3370.39); break;}
  689. case 'A1': {$format = array(1683.78,2383.94); break;}
  690. case 'A2': {$format = array(1190.55,1683.78); break;}
  691. case 'A3': {$format = array(841.89,1190.55); break;}
  692. case 'A4': default: {$format = array(595.28,841.89); break;}
  693. case 'A5': {$format = array(419.53,595.28); break;}
  694. case 'A6': {$format = array(297.64,419.53); break;}
  695. case 'A7': {$format = array(209.76,297.64); break;}
  696. case 'A8': {$format = array(147.40,209.76); break;}
  697. case 'A9': {$format = array(104.88,147.40); break;}
  698. case 'A10': {$format = array(73.70,104.88); break;}
  699. case 'B0': {$format = array(2834.65,4008.19); break;}
  700. case 'B1': {$format = array(2004.09,2834.65); break;}
  701. case 'B2': {$format = array(1417.32,2004.09); break;}
  702. case 'B3': {$format = array(1000.63,1417.32); break;}
  703. case 'B4': {$format = array(708.66,1000.63); break;}
  704. case 'B5': {$format = array(498.90,708.66); break;}
  705. case 'B6': {$format = array(354.33,498.90); break;}
  706. case 'B7': {$format = array(249.45,354.33); break;}
  707. case 'B8': {$format = array(175.75,249.45); break;}
  708. case 'B9': {$format = array(124.72,175.75); break;}
  709. case 'B10': {$format = array(87.87,124.72); break;}
  710. case 'C0': {$format = array(2599.37,3676.54); break;}
  711. case 'C1': {$format = array(1836.85,2599.37); break;}
  712. case 'C2': {$format = array(1298.27,1836.85); break;}
  713. case 'C3': {$format = array(918.43,1298.27); break;}
  714. case 'C4': {$format = array(649.13,918.43); break;}
  715. case 'C5': {$format = array(459.21,649.13); break;}
  716. case 'C6': {$format = array(323.15,459.21); break;}
  717. case 'C7': {$format = array(229.61,323.15); break;}
  718. case 'C8': {$format = array(161.57,229.61); break;}
  719. case 'C9': {$format = array(113.39,161.57); break;}
  720. case 'C10': {$format = array(79.37,113.39); break;}
  721. case 'RA0': {$format = array(2437.80,3458.27); break;}
  722. case 'RA1': {$format = array(1729.13,2437.80); break;}
  723. case 'RA2': {$format = array(1218.90,1729.13); break;}
  724. case 'RA3': {$format = array(864.57,1218.90); break;}
  725. case 'RA4': {$format = array(609.45,864.57); break;}
  726. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  727. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  728. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  729. case 'SRA3': {$format = array(907.09,1275.59); break;}
  730. case 'SRA4': {$format = array(637.80,907.09); break;}
  731. case 'LETTER': {$format = array(612.00,792.00); break;}
  732. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  733. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  734. case 'FOLIO': {$format = array(612.00,936.00); break;}
  735. // default: {$this->Error('Unknown page format: '.$format); break;}
  736. // END CHANGES Nicola Asuni
  737. }
  738. $this->fwPt=$format[0];
  739. $this->fhPt=$format[1];
  740. }
  741. else {
  742. $this->fwPt=$format[0]*$this->k;
  743. $this->fhPt=$format[1]*$this->k;
  744. }
  745. $this->fw=$this->fwPt/$this->k;
  746. $this->fh=$this->fhPt/$this->k;
  747. //Page orientation
  748. $orientation=strtolower($orientation);
  749. if($orientation=='p' or $orientation=='portrait') {
  750. $this->DefOrientation='P';
  751. $this->wPt=$this->fwPt;
  752. $this->hPt=$this->fhPt;
  753. }
  754. elseif($orientation=='l' or $orientation=='landscape') {
  755. $this->DefOrientation='L';
  756. $this->wPt=$this->fhPt;
  757. $this->hPt=$this->fwPt;
  758. }
  759. else {
  760. $this->Error('Incorrect orientation: '.$orientation);
  761. }
  762. $this->CurOrientation=$this->DefOrientation;
  763. $this->w=$this->wPt/$this->k;
  764. $this->h=$this->hPt/$this->k;
  765. //Page margins (1 cm)
  766. $margin=28.35/$this->k;
  767. $this->SetMargins($margin,$margin);
  768. //Interior cell margin (1 mm)
  769. $this->cMargin=$margin/10;
  770. //Line width (0.2 mm)
  771. $this->LineWidth=.567/$this->k;
  772. //Automatic page break
  773. $this->SetAutoPageBreak(true,2*$margin);
  774. //Full width display mode
  775. $this->SetDisplayMode('fullwidth');
  776. //Compression
  777. $this->SetCompression(true);
  778. //Set default PDF version number
  779. $this->PDFVersion = "1.3";
  780. $this->encoding = $encoding;
  781. $this->b = 0;
  782. $this->i = 0;
  783. $this->u = 0;
  784. $this->HREF = '';
  785. $this->fontlist = array("arial", "times", "courier", "helvetica", "symbol");
  786. $this->issetfont = false;
  787. $this->issetcolor = false;
  788. $this->tableborder = 0;
  789. $this->tdbegin = false;
  790. $this->tdwidth= 0;
  791. $this->tdheight = 0;
  792. $this->tdalign = "L";
  793. $this->tdbgcolor = false;
  794. $this->SetFillColor(200, 200, 200, true);
  795. $this->SetTextColor(0, 0, 0, true);
  796. }
  797. /**
  798. * Set the image scale.
  799. * @param float $scale image scale.
  800. * @author Nicola Asuni
  801. * @since 1.5.2
  802. */
  803. function setImageScale($scale) {
  804. $this->imgscale=$scale;
  805. }
  806. /**
  807. * Returns the image scale.
  808. * @return float image scale.
  809. * @author Nicola Asuni
  810. * @since 1.5.2
  811. */
  812. function getImageScale() {
  813. return $this->imgscale;
  814. }
  815. /**
  816. * Returns the page width in units.
  817. * @return int page width.
  818. * @author Nicola Asuni
  819. * @since 1.5.2
  820. */
  821. function getPageWidth() {
  822. return $this->w;
  823. }
  824. /**
  825. * Returns the page height in units.
  826. * @return int page height.
  827. * @author Nicola Asuni
  828. * @since 1.5.2
  829. */
  830. function getPageHeight() {
  831. return $this->fh;
  832. }
  833. /**
  834. * Returns the page break margin.
  835. * @return int page break margin.
  836. * @author Nicola Asuni
  837. * @since 1.5.2
  838. */
  839. function getBreakMargin() {
  840. return $this->bMargin;
  841. }
  842. /**
  843. * Returns the scale factor (number of points in user unit).
  844. * @return int scale factor.
  845. * @author Nicola Asuni
  846. * @since 1.5.2
  847. */
  848. function getScaleFactor() {
  849. return $this->k;
  850. }
  851. /**
  852. * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
  853. * @param float $left Left margin.
  854. * @param float $top Top margin.
  855. * @param float $right Right margin. Default value is the left one.
  856. * @since 1.0
  857. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  858. */
  859. function SetMargins($left, $top, $right=-1) {
  860. //Set left, top and right margins
  861. $this->lMargin=$left;
  862. $this->tMargin=$top;
  863. if($right==-1) {
  864. $right=$left;
  865. }
  866. $this->rMargin=$right;
  867. }
  868. /**
  869. * 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.
  870. * @param float $margin The margin.
  871. * @since 1.4
  872. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  873. */
  874. function SetLeftMargin($margin) {
  875. //Set left margin
  876. $this->lMargin=$margin;
  877. if(($this->page>0) and ($this->x<$margin)) {
  878. $this->x=$margin;
  879. }
  880. }
  881. /**
  882. * Defines the top margin. The method can be called before creating the first page.
  883. * @param float $margin The margin.
  884. * @since 1.5
  885. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  886. */
  887. function SetTopMargin($margin) {
  888. //Set top margin
  889. $this->tMargin=$margin;
  890. }
  891. /**
  892. * Defines the right margin. The method can be called before creating the first page.
  893. * @param float $margin The margin.
  894. * @since 1.5
  895. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  896. */
  897. function SetRightMargin($margin) {
  898. //Set right margin
  899. $this->rMargin=$margin;
  900. }
  901. /**
  902. * 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.
  903. * @param boolean $auto Boolean indicating if mode should be on or off.
  904. * @param float $margin Distance from the bottom of the page.
  905. * @since 1.0
  906. * @see Cell(), MultiCell(), AcceptPageBreak()
  907. */
  908. function SetAutoPageBreak($auto, $margin=0) {
  909. //Set auto page break mode and triggering margin
  910. $this->AutoPageBreak=$auto;
  911. $this->bMargin=$margin;
  912. $this->PageBreakTrigger=$this->h-$margin;
  913. }
  914. /**
  915. * 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.
  916. * @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>
  917. * @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>
  918. * @since 1.2
  919. */
  920. function SetDisplayMode($zoom, $layout='continuous') {
  921. //Set display mode in viewer
  922. if($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom)) {
  923. $this->ZoomMode=$zoom;
  924. }
  925. else {
  926. $this->Error('Incorrect zoom display mode: '.$zoom);
  927. }
  928. if($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default') {
  929. $this->LayoutMode=$layout;
  930. }
  931. else {
  932. $this->Error('Incorrect layout display mode: '.$layout);
  933. }
  934. }
  935. /**
  936. * 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.
  937. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  938. * @param boolean $compress Boolean indicating if compression must be enabled.
  939. * @since 1.4
  940. */
  941. function SetCompression($compress) {
  942. //Set page compression
  943. if(function_exists('gzcompress')) {
  944. $this->compress=$compress;
  945. }
  946. else {
  947. $this->compress=false;
  948. }
  949. }
  950. /**
  951. * Defines the title of the document.
  952. * @param string $title The title.
  953. * @since 1.2
  954. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  955. */
  956. function SetTitle($title) {
  957. //Title of document
  958. $this->title=$title;
  959. }
  960. /**
  961. * Defines the subject of the document.
  962. * @param string $subject The subject.
  963. * @since 1.2
  964. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  965. */
  966. function SetSubject($subject) {
  967. //Subject of document
  968. $this->subject=$subject;
  969. }
  970. /**
  971. * Defines the author of the document.
  972. * @param string $author The name of the author.
  973. * @since 1.2
  974. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  975. */
  976. function SetAuthor($author) {
  977. //Author of document
  978. $this->author=$author;
  979. }
  980. /**
  981. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  982. * @param string $keywords The list of keywords.
  983. * @since 1.2
  984. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  985. */
  986. function SetKeywords($keywords) {
  987. //Keywords of document
  988. $this->keywords=$keywords;
  989. }
  990. /**
  991. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  992. * @param string $creator The name of the creator.
  993. * @since 1.2
  994. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  995. */
  996. function SetCreator($creator) {
  997. //Creator of document
  998. $this->creator=$creator;
  999. }
  1000. /**
  1001. * Defines an alias for the total number of pages. It will be substituted as the document is closed.<br />
  1002. * <b>Example:</b><br />
  1003. * <pre>
  1004. * class PDF extends TCPDF {
  1005. * function Footer() {
  1006. * //Go to 1.5 cm from bottom
  1007. * $this->SetY(-15);
  1008. * //Select Arial italic 8
  1009. * $this->SetFont('Arial','I',8);
  1010. * //Print current and total page numbers
  1011. * $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  1012. * }
  1013. * }
  1014. * $pdf=new PDF();
  1015. * $pdf->AliasNbPages();
  1016. * </pre>
  1017. * @param string $alias The alias. Default value: {nb}.
  1018. * @since 1.4
  1019. * @see PageNo(), Footer()
  1020. */
  1021. function AliasNbPages($alias='{nb}') {
  1022. //Define an alias for total number of pages
  1023. $this->AliasNbPages = $this->_escapetext($alias);
  1024. }
  1025. /**
  1026. * 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.
  1027. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  1028. * @param string $msg The error message
  1029. * @since 1.0
  1030. */
  1031. function Error($msg) {
  1032. //Fatal error
  1033. die('<strong>TCPDF error: </strong>'.$msg);
  1034. }
  1035. /**
  1036. * This method begins the generation of the PDF document. It is not necessary to call it explicitly because AddPage() does it automatically.
  1037. * Note: no page is created by this method
  1038. * @since 1.0
  1039. * @see AddPage(), Close()
  1040. */
  1041. function Open() {
  1042. //Begin document
  1043. $this->state=1;
  1044. }
  1045. /**
  1046. * 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.
  1047. * @since 1.0
  1048. * @see Open(), Output()
  1049. */
  1050. function Close() {
  1051. //Terminate document
  1052. if($this->state==3) {
  1053. return;
  1054. }
  1055. if($this->page==0) {
  1056. $this->AddPage();
  1057. }
  1058. //Page footer
  1059. $this->InFooter=true;
  1060. $this->Footer();
  1061. $this->InFooter=false;
  1062. //Close page
  1063. $this->_endpage();
  1064. //Close document
  1065. $this->_enddoc();
  1066. }
  1067. /**
  1068. * 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.
  1069. * 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.
  1070. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  1071. * @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.
  1072. * @since 1.0
  1073. * @see TCPDF(), Header(), Footer(), SetMargins()
  1074. */
  1075. function AddPage($orientation='') {
  1076. //Start a new page
  1077. if($this->state==0) {
  1078. $this->Open();
  1079. }
  1080. $family=$this->FontFamily;
  1081. $style=$this->FontStyle.($this->underline ? 'U' : '');
  1082. $size=$this->FontSizePt;
  1083. $lw=$this->LineWidth;
  1084. $dc=$this->DrawColor;
  1085. $fc=$this->FillColor;
  1086. $tc=$this->TextColor;
  1087. $cf=$this->ColorFlag;
  1088. if($this->page>0) {
  1089. //Page footer
  1090. $this->InFooter=true;
  1091. $this->Footer();
  1092. $this->InFooter=false;
  1093. //Close page
  1094. $this->_endpage();
  1095. }
  1096. //Start new page
  1097. $this->_beginpage($orientation);
  1098. //Set line cap style to square
  1099. $this->_out('2 J');
  1100. //Set line width
  1101. $this->LineWidth=$lw;
  1102. $this->_out(sprintf('%.2f w',$lw*$this->k));
  1103. //Set font
  1104. if($family) {
  1105. $this->SetFont($family,$style,$size);
  1106. }
  1107. //Set colors
  1108. $this->DrawColor=$dc;
  1109. if($dc!='0 G') {
  1110. $this->_out($dc);
  1111. }
  1112. $this->FillColor=$fc;
  1113. if($fc!='0 g') {
  1114. $this->_out($fc);
  1115. }
  1116. $this->TextColor=$tc;
  1117. $this->ColorFlag=$cf;
  1118. //Page header
  1119. $this->Header();
  1120. //Restore line width
  1121. if($this->LineWidth!=$lw) {
  1122. $this->LineWidth=$lw;
  1123. $this->_out(sprintf('%.2f w',$lw*$this->k));
  1124. }
  1125. //Restore font
  1126. if($family) {
  1127. $this->SetFont($family,$style,$size);
  1128. }
  1129. //Restore colors
  1130. if($this->DrawColor!=$dc) {
  1131. $this->DrawColor=$dc;
  1132. $this->_out($dc);
  1133. }
  1134. if($this->FillColor!=$fc) {
  1135. $this->FillColor=$fc;
  1136. $this->_out($fc);
  1137. }
  1138. $this->TextColor=$tc;
  1139. $this->ColorFlag=$cf;
  1140. }
  1141. /**
  1142. * Set header data.
  1143. * @param string $ln header image logo
  1144. * @param string $lw header image logo width in mm
  1145. * @param string $ht string to print as title on document header
  1146. * @param string $hs string to print on document header
  1147. */
  1148. function setHeaderData($ln="", $lw=0, $ht="", $hs="") {
  1149. $this->header_logo = $ln;
  1150. $this->header_logo_width = $lw;
  1151. $this->header_title = $ht;
  1152. $this->header_string = $hs;
  1153. }
  1154. /**
  1155. * Set header margin.
  1156. * (minimum distance between header and top page margin)
  1157. * @param int $hm distance in millimeters
  1158. */
  1159. function setHeaderMargin($hm=10) {
  1160. $this->header_margin = $hm;
  1161. }
  1162. /**
  1163. * Set footer margin.
  1164. * (minimum distance between footer and bottom page margin)
  1165. * @param int $fm distance in millimeters
  1166. */
  1167. function setFooterMargin($fm=10) {
  1168. $this->footer_margin = $fm;
  1169. }
  1170. /**
  1171. * This method is used to render the page header.
  1172. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  1173. */
  1174. function Header() {
  1175. if ($this->print_header) {
  1176. if (!isset($this->original_lMargin)) {
  1177. $this->original_lMargin = $this->lMargin;
  1178. }
  1179. if (!isset($this->original_rMargin)) {
  1180. $this->original_rMargin = $this->rMargin;
  1181. }
  1182. //set current position
  1183. $this->SetXY($this->original_lMargin, $this->header_margin);
  1184. if (($this->header_logo) AND ($this->header_logo != K_BLANK_IMAGE)) {
  1185. $this->Image(K_PATH_IMAGES.$this->header_logo, $this->original_lMargin, $this->header_margin, $this->header_logo_width);
  1186. }
  1187. else {
  1188. $this->img_rb_y = $this->GetY();
  1189. }
  1190. $cell_height = round((K_CELL_HEIGHT_RATIO * $this->header_font[2]) / $this->k, 2);
  1191. $header_x = $this->original_lMargin + ($this->header_logo_width * 1.05); //set left margin for text data cell
  1192. // header title
  1193. $this->SetFont($this->header_font[0], 'B', $this->header_font[2] + 1);
  1194. $this->SetX($header_x);
  1195. $this->Cell($this->header_width, $cell_height, $this->header_title, 0, 1, 'L');
  1196. // header string
  1197. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  1198. $this->SetX($header_x);
  1199. $this->MultiCell($this->header_width, $cell_height, $this->header_string, 0, 'L', 0);
  1200. // print an ending header line
  1201. if (empty($this->header_width)) {
  1202. //set style for cell border
  1203. $this->SetLineWidth(0.3);
  1204. $this->SetDrawColor(0, 0, 0);
  1205. $this->SetY(1 + max($this->img_rb_y, $this->GetY()));
  1206. $this->SetX($this->original_lMargin);
  1207. $this->Cell(0, 0, '', 'T', 0, 'C');
  1208. }
  1209. //restore position
  1210. $this->SetXY($this->original_lMargin, $this->tMargin);
  1211. }
  1212. }
  1213. /**
  1214. * This method is used to render the page footer.
  1215. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  1216. */
  1217. function Footer() {
  1218. if ($this->print_footer) {
  1219. if (!isset($this->original_lMargin)) {
  1220. $this->original_lMargin = $this->lMargin;
  1221. }
  1222. if (!isset($this->original_rMargin)) {
  1223. $this->original_rMargin = $this->rMargin;
  1224. }
  1225. //set font
  1226. $this->SetFont($this->footer_font[0], $this->footer_font[1] , $this->footer_font[2]);
  1227. //set style for cell border
  1228. $line_width = 0.3;
  1229. $this->SetLineWidth($line_width);
  1230. $this->SetDrawColor(0, 0, 0);
  1231. $footer_height = round((K_CELL_HEIGHT_RATIO * $this->footer_font[2]) / $this->k, 2); //footer height
  1232. //get footer y position
  1233. $footer_y = $this->h - $this->footer_margin - $footer_height;
  1234. //set current position
  1235. $this->SetXY($this->original_lMargin, $footer_y);
  1236. //print document barcode
  1237. if ($this->barcode) {
  1238. $this->Ln();
  1239. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin)); //max width
  1240. $this->writeBarcode($this->original_lMargin, $footer_y + $line_width, $barcode_width, $footer_height - $line_width, "C128B", false, false, 2, $this->barcode);
  1241. }
  1242. $this->SetXY($this->original_lMargin, $footer_y);
  1243. //Print page number
  1244. $this->Cell(0, $footer_height, $this->l['w_page']." ".$this->PageNo().' / {nb}', 'T', 0, 'R');
  1245. }
  1246. }
  1247. /**
  1248. * Returns the current page number.
  1249. * @return int page number
  1250. * @since 1.0
  1251. * @see AliasNbPages()
  1252. */
  1253. function PageNo() {
  1254. //Get current page number
  1255. return $this->page;
  1256. }
  1257. /**
  1258. * 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.
  1259. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1260. * @param int $g Green component (between 0 and 255)
  1261. * @param int $b Blue component (between 0 and 255)
  1262. * @since 1.3
  1263. * @see SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  1264. */
  1265. function SetDrawColor($r, $g=-1, $b=-1) {
  1266. //Set color for all stroking operations
  1267. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1268. $this->DrawColor=sprintf('%.3f G',$r/255);
  1269. }
  1270. else {
  1271. $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
  1272. }
  1273. if($this->page>0) {
  1274. $this->_out($this->DrawColor);
  1275. }
  1276. }
  1277. /**
  1278. * 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.
  1279. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1280. * @param int $g Green component (between 0 and 255)
  1281. * @param int $b Blue component (between 0 and 255)
  1282. * @param boolean $storeprev if true stores the RGB array on $prevFillColor variable.
  1283. * @since 1.3
  1284. * @see SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  1285. */
  1286. function SetFillColor($r, $g=-1, $b=-1, $storeprev=false) {
  1287. //Set color for all filling operations
  1288. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1289. $this->FillColor=sprintf('%.3f g',$r/255);
  1290. }
  1291. else {
  1292. $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  1293. }
  1294. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  1295. if($this->page>0) {
  1296. $this->_out($this->FillColor);
  1297. }
  1298. if ($storeprev) {
  1299. // store color as previous value
  1300. $this->prevFillColor = array($r, $g, $b);
  1301. }
  1302. }
  1303. /**
  1304. * 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.
  1305. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1306. * @param int $g Green component (between 0 and 255)
  1307. * @param int $b Blue component (between 0 and 255)
  1308. * @param boolean $storeprev if true stores the RGB array on $prevTextColor variable.
  1309. * @since 1.3
  1310. * @see SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  1311. */
  1312. function SetTextColor($r, $g=-1, $b=-1, $storeprev=false) {
  1313. //Set color for text
  1314. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1315. $this->TextColor=sprintf('%.3f g',$r/255);
  1316. }
  1317. else {
  1318. $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  1319. }
  1320. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  1321. if ($storeprev) {
  1322. // store color as previous value
  1323. $this->prevTextColor = array($r, $g, $b);
  1324. }
  1325. }
  1326. /**
  1327. * Returns the length of a string in user unit. A font must be selected.<br>
  1328. * Support UTF-8 Unicode [Nicola Asuni, 2005-01-02]
  1329. * @param string $s The string whose length is to be computed
  1330. * @return int
  1331. * @since 1.2
  1332. */
  1333. function GetStringWidth($s) {
  1334. //Get width of a string in the current font
  1335. $s = (string)$s;
  1336. $cw = &$this->CurrentFont['cw'];
  1337. $w = 0;
  1338. if($this->isunicode) {
  1339. $unicode = $this->UTF8StringToArray($s);
  1340. foreach($unicode as $char) {
  1341. if (isset($cw[$char])) {
  1342. $w+=$cw[$char];
  1343. } elseif(isset($cw[ord($char)])) {
  1344. $w+=$cw[ord($char)];
  1345. } elseif(isset($cw[chr($char)])) {
  1346. $w+=$cw[chr($char)];
  1347. } elseif(isset($this->CurrentFont['desc']['MissingWidth'])) {
  1348. $w += $this->CurrentFont['desc']['MissingWidth']; // set default size
  1349. } else {
  1350. $w += 500;
  1351. }
  1352. }
  1353. } else {
  1354. $l = strlen($s);
  1355. for($i=0; $i<$l; $i++) {
  1356. if (isset($cw[$s{$i}])) {
  1357. $w += $cw[$s{$i}];
  1358. } else if (isset($cw[ord($s{$i})])) {
  1359. $w += $cw[ord($s{$i})];
  1360. }
  1361. }
  1362. }
  1363. return ($w * $this->FontSize / 1000);
  1364. }
  1365. /**
  1366. * 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.
  1367. * @param float $width The width.
  1368. * @since 1.0
  1369. * @see Line(), Rect(), Cell(), MultiCell()
  1370. */
  1371. function SetLineWidth($width) {
  1372. //Set line width
  1373. $this->LineWidth=$width;
  1374. if($this->page>0) {
  1375. $this->_out(sprintf('%.2f w',$width*$this->k));
  1376. }
  1377. }
  1378. /**
  1379. * Draws a line between two points.
  1380. * @param float $x1 Abscissa of first point
  1381. * @param float $y1 Ordinate of first point
  1382. * @param float $x2 Abscissa of second point
  1383. * @param float $y2 Ordinate of second point
  1384. * @since 1.0
  1385. * @see SetLineWidth(), SetDrawColor()
  1386. */
  1387. function Line($x1, $y1, $x2, $y2) {
  1388. //Draw a line
  1389. $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));
  1390. }
  1391. /**
  1392. * Outputs a rectangle. It can be drawn (border only), filled (with no border) or both.
  1393. * @param float $x Abscissa of upper-left corner
  1394. * @param float $y Ordinate of upper-left corner
  1395. * @param float $w Width
  1396. * @param float $h Height
  1397. * @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>
  1398. * @since 1.0
  1399. * @see SetLineWidth(), SetDrawColor(), SetFillColor()
  1400. */
  1401. function Rect($x, $y, $w, $h, $style='') {
  1402. //Draw a rectangle
  1403. if($style=='F') {
  1404. $op='f';
  1405. }
  1406. elseif($style=='FD' or $style=='DF') {
  1407. $op='B';
  1408. }
  1409. else {
  1410. $op='S';
  1411. }
  1412. $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
  1413. }
  1414. /**
  1415. * 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.
  1416. * Support UTF-8 Unicode [Nicola Asuni, 2005-01-02].
  1417. * <b>Example</b>:<br />
  1418. * <pre>
  1419. * $pdf->AddFont('Comic','I');
  1420. * // is equivalent to:
  1421. * $pdf->AddFont('Comic','I','comici.php');
  1422. * </pre>
  1423. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  1424. * @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>
  1425. * @param string $file The font definition file. By default, the name is built from the family and style, in lower case with no space.
  1426. * @since 1.5
  1427. * @see SetFont()
  1428. */
  1429. function AddFont($family, $style='', $file='') {
  1430. if(empty($family)) {
  1431. return;
  1432. }
  1433. //Add a TrueType or Type1 font
  1434. $family = strtolower($family);
  1435. if((!$this->isunicode) AND ($family == 'arial')) {
  1436. $family = 'helvetica';
  1437. }
  1438. $style=strtoupper($style);
  1439. $style=str_replace('U','',$style);
  1440. if($style == 'IB') {
  1441. $style = 'BI';
  1442. }
  1443. $fontkey = $family.$style;
  1444. // check if the font has been already added
  1445. if(isset($this->fonts[$fontkey])) {
  1446. return;
  1447. }
  1448. if($file=='') {
  1449. $file = str_replace(' ', '', $family).strtolower($style).'.php';
  1450. }
  1451. if(!file_exists($this->_getfontpath().$file)) {
  1452. // try to load the basic file without styles
  1453. $file = str_replace(' ', '', $family).'.php';
  1454. }
  1455. include($this->_getfontpath().$file);
  1456. if(!isset($name) AND !isset($fpdf_charwidths)) {
  1457. $this->Error('Could not include font definition file');
  1458. }
  1459. $i = count($this->fonts)+1;
  1460. if($this->isunicode) {
  1461. $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
  1462. $fpdf_charwidths[$fontkey] = $cw;
  1463. } else {
  1464. $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
  1465. }
  1466. if(isset($diff) AND (!empty($diff))) {
  1467. //Search existing encodings
  1468. $d=0;
  1469. $nb=count($this->diffs);
  1470. for($i=1;$i<=$nb;$i++) {
  1471. if($this->diffs[$i]==$diff) {
  1472. $d=$i;
  1473. break;
  1474. }
  1475. }
  1476. if($d==0) {
  1477. $d=$nb+1;
  1478. $this->diffs[$d]=$diff;
  1479. }
  1480. $this->fonts[$fontkey]['diff']=$d;
  1481. }
  1482. if(!empty($file)) {
  1483. if((strcasecmp($type,"TrueType") == 0) OR (strcasecmp($type,"TrueTypeUnicode") == 0)) {
  1484. $this->FontFiles[$file]=array('length1'=>$originalsize);
  1485. }
  1486. else {
  1487. $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
  1488. }
  1489. }
  1490. }
  1491. /**
  1492. * 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.
  1493. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  1494. * The method can be called before the first page is created and the font is retained from page to page.
  1495. If you just wish to change the current font size, it is simpler to call SetFontSize().
  1496. * 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 />
  1497. * Example for the last case (note the trailing slash):<br />
  1498. * <pre>
  1499. * define('FPDF_FONTPATH','/home/www/font/');
  1500. * require('tcpdf.php');
  1501. *
  1502. * //Times regular 12
  1503. * $pdf->SetFont('Times');
  1504. * //Arial bold 14
  1505. * $pdf->SetFont('Arial','B',14);
  1506. * //Removes bold
  1507. * $pdf->SetFont('');
  1508. * //Times bold, italic and underlined 14
  1509. * $pdf->SetFont('Times','BIU');
  1510. * </pre><br />
  1511. * If the file corresponding to the requested font is not found, the error "Could not include font metric file" is generated.
  1512. * @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.
  1513. * @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
  1514. * @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
  1515. * @since 1.0
  1516. * @see AddFont(), SetFontSize(), Cell(), MultiCell(), Write()
  1517. */
  1518. function SetFont($family, $style='', $size=0) {
  1519. // save previous values
  1520. $this->prevFontFamily = $this->FontFamily;
  1521. $this->prevFontStyle = $this->FontStyle;
  1522. //Select a font; size given in points
  1523. global $fpdf_charwidths;
  1524. $family=strtolower($family);
  1525. if($family=='') {
  1526. $family=$this->FontFamily;
  1527. }
  1528. if((!$this->isunicode) AND ($family == 'arial')) {
  1529. $family = 'helvetica';
  1530. }
  1531. elseif(($family=="symbol") OR ($family=="zapfdingbats")) {
  1532. $style='';
  1533. }
  1534. $style=strtoupper($style);
  1535. if(strpos($style,'U')!==false) {
  1536. $this->underline=true;
  1537. $style=str_replace('U','',$style);
  1538. }
  1539. else {
  1540. $this->underline=false;
  1541. }
  1542. if($style=='IB') {
  1543. $style='BI';
  1544. }
  1545. if($size==0) {
  1546. $size=$this->FontSizePt;
  1547. }
  1548. // try to add font (if not already added)
  1549. if($this->isunicode) {
  1550. $this->AddFont($family, $style);
  1551. }
  1552. //Test if font is already selected
  1553. if(($this->FontFamily == $family) AND ($this->FontStyle == $style) AND ($this->FontSizePt == $size)) {
  1554. return;
  1555. }
  1556. $fontkey = $family.$style;
  1557. //if(!isset($this->fonts[$fontkey]) AND isset($this->fonts[$family])) {
  1558. // $style='';
  1559. //}
  1560. //Test if used for the first time
  1561. if(!isset($this->fonts[$fontkey])) {
  1562. //Check if one of the standard fonts
  1563. if(isset($this->CoreFonts[$fontkey])) {
  1564. if(!isset($fpdf_charwidths[$fontkey])) {
  1565. //Load metric file
  1566. $file = $family;
  1567. if(($family!='symbol') AND ($family!='zapfdingbats')) {
  1568. $file .= strtolower($style);
  1569. }
  1570. if(!file_exists($this->_getfontpath().$file.'.php')) {
  1571. // try to load the basic file without styles
  1572. $file = $family;
  1573. $fontkey = $family;
  1574. }
  1575. include($this->_getfontpath().$file.'.php');
  1576. if (($this->isunicode AND !isset($ctg)) OR ((!$this->isunicode) AND (!isset($fpdf_charwidths[$fontkey]))) ) {
  1577. $this->Error("Could not include font metric file [".$fontkey."]: ".$this->_getfontpath().$file.".php");
  1578. }
  1579. }
  1580. $i = count($this->fonts) + 1;
  1581. if($this->isunicode) {
  1582. $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
  1583. $fpdf_charwidths[$fontkey] = $cw;
  1584. } else {
  1585. $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
  1586. }
  1587. }
  1588. else {
  1589. $this->Error('Undefined font: '.$family.' '.$style);
  1590. }
  1591. }
  1592. //Select it
  1593. $this->FontFamily = $family;
  1594. $this->FontStyle = $style;
  1595. $this->FontSizePt = $size;
  1596. $this->FontSize = $size / $this->k;
  1597. $this->CurrentFont = &$this->fonts[$fontkey];
  1598. if($this->page>0) {
  1599. $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  1600. }
  1601. }
  1602. /**
  1603. * Defines the size of the current font.
  1604. * @param float $size The size (in points)
  1605. * @since 1.0
  1606. * @see SetFont()
  1607. */
  1608. function SetFontSize($size) {
  1609. //Set font size in points
  1610. if($this->FontSizePt==$size) {
  1611. return;
  1612. }
  1613. $this->FontSizePt = $size;
  1614. $this->FontSize = $size / $this->k;
  1615. if($this->page > 0) {
  1616. $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  1617. }
  1618. }
  1619. /**
  1620. * 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 />
  1621. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  1622. * @since 1.5
  1623. * @see Cell(), Write(), Image(), Link(), SetLink()
  1624. */
  1625. function AddLink() {
  1626. //Create a new internal link
  1627. $n=count($this->links)+1;
  1628. $this->links[$n]=array(0,0);
  1629. return $n;
  1630. }
  1631. /**
  1632. * Defines the page and position a link points to
  1633. * @param int $link The link identifier returned by AddLink()
  1634. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  1635. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  1636. * @since 1.5
  1637. * @see AddLink()
  1638. */
  1639. function SetLink($link, $y=0, $page=-1) {
  1640. //Set destination of internal link
  1641. if($y==-1) {
  1642. $y=$this->y;
  1643. }
  1644. if($page==-1) {
  1645. $page=$this->page;
  1646. }
  1647. $this->links[$link]=array($page,$y);
  1648. }
  1649. /**
  1650. * 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.
  1651. * @param float $x Abscissa of the upper-left corner of the rectangle
  1652. * @param float $y Ordinate of the upper-left corner of the rectangle
  1653. * @param float $w Width of the rectangle
  1654. * @param float $h Height of the rectangle
  1655. * @param mixed $link URL or identifier returned by AddLink()
  1656. * @since 1.5
  1657. * @see AddLink(), Cell(), Write(), Image()
  1658. */
  1659. function Link($x, $y, $w, $h, $link) {
  1660. //Put a link on the page
  1661. $this->PageLinks[$this->page][] = array($x * $this->k, $this->hPt - $y * $this->k, $w * $this->k, $h*$this->k, $link);
  1662. }
  1663. /**
  1664. * 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.
  1665. * @param float $x Abscissa of the origin
  1666. * @param float $y Ordinate of the origin
  1667. * @param string $txt String to print
  1668. * @since 1.0
  1669. * @see SetFont(), SetTextColor(), Cell(), MultiCell(), Write()
  1670. */
  1671. function Text($x, $y, $txt) {
  1672. //Output a string
  1673. $s=sprintf('BT %.2f %.2f Td (%s) Tj ET', $x * $this->k, ($this->h-$y) * $this->k, $this->_escapetext($txt));
  1674. if($this->underline AND ($txt!='')) {
  1675. $s .= ' '.$this->_dounderline($x,$y,$txt);
  1676. }
  1677. if($this->ColorFlag) {
  1678. $s='q '.$this->TextColor.' '.$s.' Q';
  1679. }
  1680. $this->_out($s);
  1681. }
  1682. /**
  1683. * 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 />
  1684. * This method is called automatically and should not be called directly by the application.<br />
  1685. * <b>Example:</b><br />
  1686. * The method is overriden in an inherited class in order to obtain a 3 column layout:<br />
  1687. * <pre>
  1688. * class PDF extends TCPDF {
  1689. * var $col=0;
  1690. *
  1691. * function SetCol($col) {
  1692. * //Move position to a column
  1693. * $this->col=$col;
  1694. * $x=10+$col*65;
  1695. * $this->SetLeftMargin($x);
  1696. * $this->SetX($x);
  1697. * }
  1698. *
  1699. * function AcceptPageBreak() {
  1700. * if($this->col<2) {
  1701. * //Go to next column
  1702. * $this->SetCol($this->col+1);
  1703. * $this->SetY(10);
  1704. * return false;
  1705. * }
  1706. * else {
  1707. * //Go back to first column and issue page break
  1708. * $this->SetCol(0);
  1709. * return true;
  1710. * }
  1711. * }
  1712. * }
  1713. *
  1714. * $pdf=new PDF();
  1715. * $pdf->Open();
  1716. * $pdf->AddPage();
  1717. * $pdf->SetFont('Arial','',12);
  1718. * for($i=1;$i<=300;$i++) {
  1719. * $pdf->Cell(0,5,"Line $i",0,1);
  1720. * }
  1721. * $pdf->Output();
  1722. * </pre>
  1723. * @return boolean
  1724. * @since 1.4
  1725. * @see SetAutoPageBreak()
  1726. */
  1727. function AcceptPageBreak() {
  1728. //Accept automatic page break or not
  1729. return $this->AutoPageBreak;
  1730. }
  1731. /**
  1732. * 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 />
  1733. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  1734. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  1735. * @param float $h Cell height. Default value: 0.
  1736. * @param string $txt String to print. Default value: empty string.
  1737. * @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>
  1738. * @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>
  1739. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  1740. * @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>
  1741. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  1742. * @param mixed $link URL or identifier returned by AddLink().
  1743. * @since 1.0
  1744. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  1745. */
  1746. function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='') {
  1747. //Output a cell
  1748. $k=$this->k;
  1749. if(($this->y + $h) > $this->PageBreakTrigger AND empty($this->InFooter) AND $this->AcceptPageBreak()) {
  1750. //Automatic page break
  1751. $x = $this->x;
  1752. $ws = $this->ws;
  1753. if($ws > 0) {
  1754. $this->ws = 0;
  1755. $this->_out('0 Tw');
  1756. }
  1757. $this->AddPage($this->CurOrientation);
  1758. $this->x = $x;
  1759. if($ws > 0) {
  1760. $this->ws = $ws;
  1761. $this->_out(sprintf('%.3f Tw',$ws * $k));
  1762. }
  1763. }
  1764. if($w == 0) {
  1765. $w = $this->w - $this->rMargin - $this->x;
  1766. }
  1767. $s = '';
  1768. if(($fill == 1) OR ($border == 1)) {
  1769. if($fill == 1) {
  1770. $op = ($border == 1) ? 'B' : 'f';
  1771. }
  1772. else {
  1773. $op = 'S';
  1774. }
  1775. $s = sprintf('%.2f %.2f %.2f %.2f re %s ', $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
  1776. }
  1777. if(is_string($border)) {
  1778. $x=$this->x;
  1779. $y=$this->y;
  1780. if(strpos($border,'L')!==false) {
  1781. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
  1782. }
  1783. if(strpos($border,'T')!==false) {
  1784. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
  1785. }
  1786. if(strpos($border,'R')!==false) {
  1787. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  1788. }
  1789. if(strpos($border,'B')!==false) {
  1790. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  1791. }
  1792. }
  1793. if($txt != '') {
  1794. $width = $this->GetStringWidth($txt);
  1795. if($align == 'R') {
  1796. $dx = $w - $this->cMargin - $width;
  1797. }
  1798. elseif($align=='C') {
  1799. $dx = ($w - $width)/2;
  1800. }
  1801. else {
  1802. $dx = $this->cMargin;
  1803. }
  1804. if($this->ColorFlag) {
  1805. $s .= 'q '.$this->TextColor.' ';
  1806. }
  1807. $txt2 = $this->_escapetext($txt);
  1808. $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);
  1809. if($this->underline) {
  1810. $s.=' '.$this->_dounderline($this->x + $dx, $this->y + 0.5 * $h + 0.3 * $this->FontSize, $txt);
  1811. }
  1812. if($this->ColorFlag) {
  1813. $s.=' Q';
  1814. }
  1815. if($link) {
  1816. $this->Link($this->x + $dx, $this->y + 0.5 * $h - 0.5 * $this->FontSize, $width, $this->FontSize, $link);
  1817. }
  1818. }
  1819. if($s) {
  1820. $this->_out($s);
  1821. }
  1822. $this->lasth = $h;
  1823. if($ln>0) {
  1824. //Go to next line
  1825. $this->y += $h;
  1826. if($ln == 1) {
  1827. $this->x = $this->lMargin;
  1828. }
  1829. }
  1830. else {
  1831. $this->x += $w;
  1832. }
  1833. }
  1834. /**
  1835. * 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 />
  1836. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  1837. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  1838. * @param float $h Height of cells.
  1839. * @param string $txt String to print
  1840. * @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>
  1841. * @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>
  1842. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  1843. * @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>
  1844. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  1845. * @since 1.3
  1846. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  1847. */
  1848. function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0) {
  1849. //Output text with automatic or explicit line breaks
  1850. $cw = &$this->CurrentFont['cw'];
  1851. if($w == 0) {
  1852. $w = $this->w - $this->rMargin - $this->x;
  1853. }
  1854. $wmax = ($w - 2 * $this->cMargin);
  1855. $s = str_replace("\r", '', $txt); // remove carriage returns
  1856. $nb = strlen($s);
  1857. $b=0;
  1858. if($border) {
  1859. if($border==1) {
  1860. $border='LTRB';
  1861. $b='LRT';
  1862. $b2='LR';
  1863. }
  1864. else {
  1865. $b2='';
  1866. if(strpos($border,'L')!==false) {
  1867. $b2.='L';
  1868. }
  1869. if(strpos($border,'R')!==false) {
  1870. $b2.='R';
  1871. }
  1872. $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2;
  1873. }
  1874. }
  1875. $sep=-1;
  1876. $i=0;
  1877. $j=0;
  1878. $l=0;
  1879. $ns=0;
  1880. $nl=1;
  1881. while($i<$nb) {
  1882. //Get next character
  1883. $c = $s{$i};
  1884. if(preg_match("/[\n]/u", $c)) {
  1885. //Explicit line break
  1886. if($this->ws > 0) {
  1887. $this->ws = 0;
  1888. $this->_out('0 Tw');
  1889. }
  1890. $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
  1891. $i++;
  1892. $sep=-1;
  1893. $j=$i;
  1894. $l=0;
  1895. $ns=0;
  1896. $nl++;
  1897. if($border and $nl==2) {
  1898. $b = $b2;
  1899. }
  1900. continue;
  1901. }
  1902. if(preg_match("/[ ]/u", $c)) {
  1903. $sep = $i;
  1904. $ls = $l;
  1905. $ns++;
  1906. }
  1907. $l = $this->GetStringWidth(substr($s, $j, $i-$j));
  1908. if($l > $wmax) {
  1909. //Automatic line break
  1910. if($sep == -1) {
  1911. if($i == $j) {
  1912. $i++;
  1913. }
  1914. if($this->ws > 0) {
  1915. $this->ws = 0;
  1916. $this->_out('0 Tw');
  1917. }
  1918. $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
  1919. }
  1920. else {
  1921. if($align=='J') {
  1922. $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
  1923. $this->_out(sprintf('%.3f Tw', $this->ws * $this->k));
  1924. }
  1925. $this->Cell($w, $h, substr($s, $j, $sep-$j), $b, 2, $align, $fill);
  1926. $i = $sep + 1;
  1927. }
  1928. $sep=-1;
  1929. $j=$i;
  1930. $l=0;
  1931. $ns=0;
  1932. $nl++;
  1933. if($border AND ($nl==2)) {
  1934. $b=$b2;
  1935. }
  1936. }
  1937. else {
  1938. $i++;
  1939. }
  1940. }
  1941. //Last chunk
  1942. if($this->ws>0) {
  1943. $this->ws=0;
  1944. $this->_out('0 Tw');
  1945. }
  1946. if($border and is_int(strpos($border,'B'))) {
  1947. $b.='B';
  1948. }
  1949. $this->Cell($w, $h, substr($s, $j, $i-$j), $b, 2, $align, $fill);
  1950. $this->x=$this->lMargin;
  1951. }
  1952. /**
  1953. * 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 />
  1954. * <b>Example:</b><br />
  1955. * <pre>
  1956. * //Begin with regular font
  1957. * $pdf->SetFont('Arial','',14);
  1958. * $pdf->Write(5,'Visit ');
  1959. * //Then put a blue underlined link
  1960. * $pdf->SetTextColor(0,0,255);
  1961. * $pdf->SetFont('','U');
  1962. * $pdf->Write(5,'www.tecnick.com','http://www.tecnick.com');
  1963. * </pre>
  1964. * @param float $h Line height
  1965. * @param string $txt String to print
  1966. * @param mixed $link URL or identifier returned by AddLink()
  1967. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  1968. * @since 1.5
  1969. * @see SetFont(), SetTextColor(), AddLink(), MultiCell(), SetAutoPageBreak()
  1970. */
  1971. function Write($h, $txt, $link='', $fill=0) {
  1972. //Output text in flowing mode
  1973. $cw = &$this->CurrentFont['cw'];
  1974. $w = $this->w - $this->rMargin - $this->x;
  1975. $wmax = ($w - 2 * $this->cMargin);
  1976. $s = str_replace("\r", '', $txt);
  1977. $nb = strlen($s);
  1978. // handle single space character
  1979. if(($nb==1) AND preg_match("/[ ]/u", $s)) {
  1980. $this->x += $this->GetStringWidth($s);
  1981. return;
  1982. }
  1983. $sep=-1;
  1984. $i=0;
  1985. $j=0;
  1986. $l=0;
  1987. $nl=1;
  1988. while($i<$nb) {
  1989. //Get next character
  1990. $c=$s{$i};
  1991. if(preg_match("/[\n]/u", $c)) {
  1992. //Explicit line break
  1993. $this->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, '', $fill, $link);
  1994. $i++;
  1995. $sep = -1;
  1996. $j = $i;
  1997. $l = 0;
  1998. if($nl == 1) {
  1999. $this->x = $this->lMargin;
  2000. $w = $this->w - $this->rMargin - $this->x;
  2001. $wmax = ($w - 2 * $this->cMargin);
  2002. }
  2003. $nl++;
  2004. continue;
  2005. }
  2006. if(preg_match("/[ ]/u", $c)) {
  2007. $sep= $i;
  2008. }
  2009. $l = $this->GetStringWidth(substr($s, $j, $i-$j));
  2010. if($l > $wmax) {
  2011. //Automatic line break (word wrapping)
  2012. if($sep == -1) {
  2013. if($this->x > $this->lMargin) {
  2014. //Move to next line
  2015. $this->x = $this->lMargin;
  2016. $this->y += $h;
  2017. $w=$this->w - $this->rMargin - $this->x;
  2018. $wmax=($w - 2 * $this->cMargin);
  2019. $i++;
  2020. $nl++;
  2021. continue;
  2022. }
  2023. if($i==$j) {
  2024. $i++;
  2025. }
  2026. $this->Cell($w, $h, substr($s, $j, $i-$j), 0, 2, '', $fill, $link);
  2027. }
  2028. else {
  2029. $this->Cell($w, $h, substr($s, $j, $sep-$j), 0, 2, '', $fill, $link);
  2030. $i=$sep+1;
  2031. }
  2032. $sep = -1;
  2033. $j = $i;
  2034. $l = 0;
  2035. if($nl==1) {
  2036. $this->x = $this->lMargin;
  2037. $w = $this->w - $this->rMargin - $this->x;
  2038. $wmax = ($w - 2 * $this->cMargin);
  2039. }
  2040. $nl++;
  2041. }
  2042. else {
  2043. $i++;
  2044. }
  2045. }
  2046. //Last chunk
  2047. if($i!=$j) {
  2048. $this->Cell($this->GetStringWidth(substr($s, $j)." "), $h, substr($s, $j), 0, 0, '', $fill, $link);
  2049. }
  2050. }
  2051. /**
  2052. * 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>
  2053. * Supported formats are JPEG and PNG.
  2054. * For JPEG, all flavors are allowed:<ul><li>gray scales</li><li>true colors (24 bits)</li><li>CMYK (32 bits)</li></ul>
  2055. * 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>
  2056. * but are not supported:<ul><li>Interlacing</li><li>Alpha channel</li></ul>
  2057. * If a transparent color is defined, it will be taken into account (but will be only interpreted by Acrobat 4 and above).<br />
  2058. * The format can be specified explicitly or inferred from the file extension.<br />
  2059. * It is possible to put a link on the image.<br />
  2060. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  2061. * @param string $file Name of the file containing the image.
  2062. * @param float $x Abscissa of the upper-left corner.
  2063. * @param float $y Ordinate of the upper-left corner.
  2064. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  2065. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  2066. * @param string $type Image format. Possible values are (case insensitive): JPG, JPEG, PNG. If not specified, the type is inferred from the file extension.
  2067. * @param mixed $link URL or identifier returned by AddLink().
  2068. * @since 1.1
  2069. * @see AddLink()
  2070. */
  2071. function Image($file, $x, $y, $w=0, $h=0, $type='', $link='') {
  2072. //Put an image on the page
  2073. if(!isset($this->images[$file])) {
  2074. //First use of image, get info
  2075. if($type == '') {
  2076. $pos = strrpos($file,'.');
  2077. if(empty($pos)) {
  2078. $this->Error('Image file has no extension and no type was specified: '.$file);
  2079. }
  2080. $type = substr($file, $pos+1);
  2081. }
  2082. $type = strtolower($type);
  2083. $mqr = get_magic_quotes_runtime();
  2084. set_magic_quotes_runtime(0);
  2085. if($type == 'jpg' or $type == 'jpeg') {
  2086. $info=$this->_parsejpg($file);
  2087. }
  2088. elseif($type == 'png') {
  2089. $info=$this->_parsepng($file);
  2090. }
  2091. else {
  2092. //Allow for additional formats
  2093. $mtd='_parse'.$type;
  2094. if(!method_exists($this,$mtd)) {
  2095. $this->Error('Unsupported image type: '.$type);
  2096. }
  2097. $info=$this->$mtd($file);
  2098. }
  2099. set_magic_quotes_runtime($mqr);
  2100. $info['i']=count($this->images)+1;
  2101. $this->images[$file]=$info;
  2102. }
  2103. else {
  2104. $info=$this->images[$file];
  2105. }
  2106. //Automatic width and height calculation if needed
  2107. if(($w == 0) and ($h == 0)) {
  2108. //Put image at 72 dpi
  2109. // 2004-06-14 :: Nicola Asuni, scale factor where added
  2110. $w = $info['w'] / ($this->imgscale * $this->k);
  2111. $h = $info['h'] / ($this->imgscale * $this->k);
  2112. }
  2113. if($w == 0) {
  2114. $w = $h * $info['w'] / $info['h'];
  2115. }
  2116. if($h == 0) {
  2117. $h = $w * $info['h'] / $info['w'];
  2118. }
  2119. $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']));
  2120. if($link) {
  2121. $this->Link($x, $y, $w, $h, $link);
  2122. }
  2123. //2002-07-31 - Nicola Asuni
  2124. // set right-bottom corner coordinates
  2125. $this->img_rb_x = $x + $w;
  2126. $this->img_rb_y = $y + $h;
  2127. }
  2128. /**
  2129. * Performs a line break. The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  2130. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  2131. * @since 1.0
  2132. * @see Cell()
  2133. */
  2134. function Ln($h='') {
  2135. //Line feed; default value is last cell height
  2136. $this->x=$this->lMargin;
  2137. if(is_string($h)) {
  2138. $this->y+=$this->lasth;
  2139. }
  2140. else {
  2141. $this->y+=$h;
  2142. }
  2143. }
  2144. /**
  2145. * Returns the abscissa of the current position.
  2146. * @return float
  2147. * @since 1.2
  2148. * @see SetX(), GetY(), SetY()
  2149. */
  2150. function GetX() {
  2151. //Get x position
  2152. return $this->x;
  2153. }
  2154. /**
  2155. * Defines the abscissa of the current position. If the passed value is negative, it is relative to the right of the page.
  2156. * @param float $x The value of the abscissa.
  2157. * @since 1.2
  2158. * @see GetX(), GetY(), SetY(), SetXY()
  2159. */
  2160. function SetX($x) {
  2161. //Set x position
  2162. if($x>=0) {
  2163. $this->x=$x;
  2164. }
  2165. else {
  2166. $this->x=$this->w+$x;
  2167. }
  2168. }
  2169. /**
  2170. * Returns the ordinate of the current position.
  2171. * @return float
  2172. * @since 1.0
  2173. * @see SetY(), GetX(), SetX()
  2174. */
  2175. function GetY() {
  2176. //Get y position
  2177. return $this->y;
  2178. }
  2179. /**
  2180. * 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.
  2181. * @param float $y The value of the ordinate.
  2182. * @since 1.0
  2183. * @see GetX(), GetY(), SetY(), SetXY()
  2184. */
  2185. function SetY($y) {
  2186. //Set y position and reset x
  2187. $this->x=$this->lMargin;
  2188. if($y>=0) {
  2189. $this->y=$y;
  2190. }
  2191. else {
  2192. $this->y=$this->h+$y;
  2193. }
  2194. }
  2195. /**
  2196. * 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.
  2197. * @param float $x The value of the abscissa
  2198. * @param float $y The value of the ordinate
  2199. * @since 1.2
  2200. * @see SetX(), SetY()
  2201. */
  2202. function SetXY($x, $y) {
  2203. //Set x and y positions
  2204. $this->SetY($y);
  2205. $this->SetX($x);
  2206. }
  2207. /**
  2208. * 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 />
  2209. * The method first calls Close() if necessary to terminate the document.
  2210. * @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.
  2211. * @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).
  2212. * @since 1.0
  2213. * @see Close()
  2214. */
  2215. function Output($name='',$dest='') {
  2216. //Output PDF to some destination
  2217. //Finish document if necessary
  2218. if($this->state < 3) {
  2219. $this->Close();
  2220. }
  2221. //Normalize parameters
  2222. if(is_bool($dest)) {
  2223. $dest=$dest ? 'D' : 'F';
  2224. }
  2225. $dest=strtoupper($dest);
  2226. if($dest=='') {
  2227. if($name=='') {
  2228. $name='doc.pdf';
  2229. $dest='I';
  2230. } else {
  2231. $dest='F';
  2232. }
  2233. }
  2234. switch($dest) {
  2235. case 'I': {
  2236. //Send to standard output
  2237. if(ob_get_contents()) {
  2238. $this->Error('Some data has already been output, can\'t send PDF file');
  2239. }
  2240. if(php_sapi_name()!='cli') {
  2241. //We send to a browser
  2242. header('Content-Type: application/pdf');
  2243. if(headers_sent()) {
  2244. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  2245. }
  2246. header('Content-Length: '.strlen($this->buffer));
  2247. header('Content-disposition: inline; filename="'.$name.'"');
  2248. }
  2249. echo $this->buffer;
  2250. break;
  2251. }
  2252. case 'D': {
  2253. //Download file
  2254. if(ob_get_contents()) {
  2255. $this->Error('Some data has already been output, can\'t send PDF file');
  2256. }
  2257. if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) {
  2258. header('Content-Type: application/force-download');
  2259. } else {
  2260. header('Content-Type: application/octet-stream');
  2261. }
  2262. if(headers_sent()) {
  2263. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  2264. }
  2265. header('Content-Length: '.strlen($this->buffer));
  2266. header('Content-disposition: attachment; filename="'.$name.'"');
  2267. echo $this->buffer;
  2268. break;
  2269. }
  2270. case 'F': {
  2271. //Save to local file
  2272. $f=fopen($name,'wb');
  2273. if(!$f) {
  2274. $this->Error('Unable to create output file: '.$name);
  2275. }
  2276. fwrite($f,$this->buffer,strlen($this->buffer));
  2277. fclose($f);
  2278. break;
  2279. }
  2280. case 'S': {
  2281. //Return as a string
  2282. return $this->buffer;
  2283. }
  2284. default: {
  2285. $this->Error('Incorrect output destination: '.$dest);
  2286. }
  2287. }
  2288. return '';
  2289. }
  2290. // var methods
  2291. /**
  2292. * Check for locale-related bug
  2293. * @access protected
  2294. */
  2295. function _dochecks() {
  2296. //Check for locale-related bug
  2297. if(1.1==1) {
  2298. $this->Error('Don\'t alter the locale before including class file');
  2299. }
  2300. //Check for decimal separator
  2301. if(sprintf('%.1f',1.0)!='1.0') {
  2302. setlocale(LC_NUMERIC,'C');
  2303. }
  2304. }
  2305. /**
  2306. * Return fonts path
  2307. * @access protected
  2308. */
  2309. function _getfontpath() {
  2310. if(!defined('FPDF_FONTPATH') AND is_dir(dirname(__FILE__).'/font')) {
  2311. define('FPDF_FONTPATH', dirname(__FILE__).'/font/');
  2312. }
  2313. return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : '';
  2314. }
  2315. /**
  2316. * Start document
  2317. * @access protected
  2318. */
  2319. function _begindoc() {
  2320. //Start document
  2321. $this->state=1;
  2322. $this->_out('%PDF-1.3');
  2323. }
  2324. /**
  2325. * _putpages
  2326. * @access protected
  2327. */
  2328. function _putpages() {
  2329. $nb = $this->page;
  2330. if(!empty($this->AliasNbPages)) {
  2331. $nbstr = $this->UTF8ToUTF16BE($nb, false);
  2332. //Replace number of pages
  2333. for($n=1;$n<=$nb;$n++) {
  2334. $this->pages[$n]=str_replace($this->AliasNbPages, $nbstr, $this->pages[$n]);
  2335. }
  2336. }
  2337. if($this->DefOrientation=='P') {
  2338. $wPt=$this->fwPt;
  2339. $hPt=$this->fhPt;
  2340. }
  2341. else {
  2342. $wPt=$this->fhPt;
  2343. $hPt=$this->fwPt;
  2344. }
  2345. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  2346. for($n=1;$n<=$nb;$n++) {
  2347. //Page
  2348. $this->_newobj();
  2349. $this->_out('<</Type /Page');
  2350. $this->_out('/Parent 1 0 R');
  2351. if(isset($this->OrientationChanges[$n])) {
  2352. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
  2353. }
  2354. $this->_out('/Resources 2 0 R');
  2355. if(isset($this->PageLinks[$n])) {
  2356. //Links
  2357. $annots='/Annots [';
  2358. foreach($this->PageLinks[$n] as $pl) {
  2359. $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  2360. $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  2361. if(is_string($pl[4])) {
  2362. $annots.='/A <</S /URI /URI ('.$this->_escape($pl[4]).')>>>>';
  2363. }
  2364. else {
  2365. $l=$this->links[$pl[4]];
  2366. $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
  2367. $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  2368. }
  2369. }
  2370. $this->_out($annots.']');
  2371. }
  2372. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  2373. $this->_out('endobj');
  2374. //Page content
  2375. $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  2376. $this->_newobj();
  2377. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  2378. $this->_putstream($p);
  2379. $this->_out('endobj');
  2380. }
  2381. //Pages root
  2382. $this->offsets[1]=strlen($this->buffer);
  2383. $this->_out('1 0 obj');
  2384. $this->_out('<</Type /Pages');
  2385. $kids='/Kids [';
  2386. for($i=0;$i<$nb;$i++) {
  2387. $kids.=(3+2*$i).' 0 R ';
  2388. }
  2389. $this->_out($kids.']');
  2390. $this->_out('/Count '.$nb);
  2391. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
  2392. $this->_out('>>');
  2393. $this->_out('endobj');
  2394. }
  2395. /**
  2396. * Adds fonts
  2397. * _putfonts
  2398. * @access protected
  2399. */
  2400. function _putfonts() {
  2401. $nf=$this->n;
  2402. foreach($this->diffs as $diff) {
  2403. //Encodings
  2404. $this->_newobj();
  2405. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  2406. $this->_out('endobj');
  2407. }
  2408. $mqr=get_magic_quotes_runtime();
  2409. set_magic_quotes_runtime(0);
  2410. foreach($this->FontFiles as $file=>$info) {
  2411. //Font file embedding
  2412. $this->_newobj();
  2413. $this->FontFiles[$file]['n']=$this->n;
  2414. $font='';
  2415. $f = fopen($this->_getfontpath().$file,'rb',1);
  2416. if(!$f) {
  2417. $this->Error('Font file not found');
  2418. }
  2419. while(!feof($f)) {
  2420. $font .= fread($f, 8192);
  2421. }
  2422. fclose($f);
  2423. $compressed=(substr($file,-2)=='.z');
  2424. if(!$compressed && isset($info['length2'])) {
  2425. $header=(ord($font{0})==128);
  2426. if($header) {
  2427. //Strip first binary header
  2428. $font=substr($font,6);
  2429. }
  2430. if($header && ord($font{$info['length1']})==128) {
  2431. //Strip second binary header
  2432. $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
  2433. }
  2434. }
  2435. $this->_out('<</Length '.strlen($font));
  2436. if($compressed) {
  2437. $this->_out('/Filter /FlateDecode');
  2438. }
  2439. $this->_out('/Length1 '.$info['length1']);
  2440. if(isset($info['length2'])) {
  2441. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  2442. }
  2443. $this->_out('>>');
  2444. $this->_putstream($font);
  2445. $this->_out('endobj');
  2446. }
  2447. set_magic_quotes_runtime($mqr);
  2448. foreach($this->fonts as $k=>$font) {
  2449. //Font objects
  2450. $this->fonts[$k]['n']=$this->n+1;
  2451. $type=$font['type'];
  2452. $name=$font['name'];
  2453. if($type=='core') {
  2454. //Standard font
  2455. $this->_newobj();
  2456. $this->_out('<</Type /Font');
  2457. $this->_out('/BaseFont /'.$name);
  2458. $this->_out('/Subtype /Type1');
  2459. if($name!='Symbol' && $name!='ZapfDingbats') {
  2460. $this->_out('/Encoding /WinAnsiEncoding');
  2461. }
  2462. $this->_out('>>');
  2463. $this->_out('endobj');
  2464. } elseif($type=='Type1' || $type=='TrueType') {
  2465. //Additional Type1 or TrueType font
  2466. $this->_newobj();
  2467. $this->_out('<</Type /Font');
  2468. $this->_out('/BaseFont /'.$name);
  2469. $this->_out('/Subtype /'.$type);
  2470. $this->_out('/FirstChar 32 /LastChar 255');
  2471. $this->_out('/Widths '.($this->n+1).' 0 R');
  2472. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  2473. if($font['enc']) {
  2474. if(isset($font['diff'])) {
  2475. $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
  2476. } else {
  2477. $this->_out('/Encoding /WinAnsiEncoding');
  2478. }
  2479. }
  2480. $this->_out('>>');
  2481. $this->_out('endobj');
  2482. //Widths
  2483. $this->_newobj();
  2484. $cw=&$font['cw'];
  2485. $s='[';
  2486. for($i=32;$i<=255;$i++) {
  2487. $s.=$cw[chr($i)].' ';
  2488. }
  2489. $this->_out($s.']');
  2490. $this->_out('endobj');
  2491. //Descriptor
  2492. $this->_newobj();
  2493. $s='<</Type /FontDescriptor /FontName /'.$name;
  2494. foreach($font['desc'] as $k=>$v) {
  2495. $s.=' /'.$k.' '.$v;
  2496. }
  2497. $file = $font['file'];
  2498. if($file) {
  2499. $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
  2500. }
  2501. $this->_out($s.'>>');
  2502. $this->_out('endobj');
  2503. } else {
  2504. //Allow for additional types
  2505. $mtd='_put'.strtolower($type);
  2506. if(!method_exists($this, $mtd)) {
  2507. $this->Error('Unsupported font type: '.$type);
  2508. }
  2509. $this->$mtd($font);
  2510. }
  2511. }
  2512. }
  2513. /**
  2514. * _putimages
  2515. * @access protected
  2516. */
  2517. function _putimages() {
  2518. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  2519. reset($this->images);
  2520. while(list($file,$info)=each($this->images)) {
  2521. $this->_newobj();
  2522. $this->images[$file]['n']=$this->n;
  2523. $this->_out('<</Type /XObject');
  2524. $this->_out('/Subtype /Image');
  2525. $this->_out('/Width '.$info['w']);
  2526. $this->_out('/Height '.$info['h']);
  2527. if($info['cs']=='Indexed') {
  2528. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  2529. }
  2530. else {
  2531. $this->_out('/ColorSpace /'.$info['cs']);
  2532. if($info['cs']=='DeviceCMYK') {
  2533. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  2534. }
  2535. }
  2536. $this->_out('/BitsPerComponent '.$info['bpc']);
  2537. if(isset($info['f'])) {
  2538. $this->_out('/Filter /'.$info['f']);
  2539. }
  2540. if(isset($info['parms'])) {
  2541. $this->_out($info['parms']);
  2542. }
  2543. if(isset($info['trns']) and is_array($info['trns'])) {
  2544. $trns='';
  2545. for($i=0;$i<count($info['trns']);$i++) {
  2546. $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
  2547. }
  2548. $this->_out('/Mask ['.$trns.']');
  2549. }
  2550. $this->_out('/Length '.strlen($info['data']).'>>');
  2551. $this->_putstream($info['data']);
  2552. unset($this->images[$file]['data']);
  2553. $this->_out('endobj');
  2554. //Palette
  2555. if($info['cs']=='Indexed') {
  2556. $this->_newobj();
  2557. $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  2558. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  2559. $this->_putstream($pal);
  2560. $this->_out('endobj');
  2561. }
  2562. }
  2563. }
  2564. /**
  2565. * _putxobjectdict
  2566. * @access protected
  2567. */
  2568. function _putxobjectdict() {
  2569. foreach($this->images as $image) {
  2570. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  2571. }
  2572. }
  2573. /**
  2574. * _putresourcedict
  2575. * @access protected
  2576. */
  2577. function _putresourcedict(){
  2578. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  2579. $this->_out('/Font <<');
  2580. foreach($this->fonts as $font) {
  2581. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  2582. }
  2583. $this->_out('>>');
  2584. $this->_out('/XObject <<');
  2585. $this->_putxobjectdict();
  2586. $this->_out('>>');
  2587. }
  2588. /**
  2589. * _putresources
  2590. * @access protected
  2591. */
  2592. function _putresources() {
  2593. $this->_putfonts();
  2594. $this->_putimages();
  2595. //Resource dictionary
  2596. $this->offsets[2]=strlen($this->buffer);
  2597. $this->_out('2 0 obj');
  2598. $this->_out('<<');
  2599. $this->_putresourcedict();
  2600. $this->_out('>>');
  2601. $this->_out('endobj');
  2602. }
  2603. /**
  2604. * _putinfo
  2605. * @access protected
  2606. */
  2607. function _putinfo() {
  2608. $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
  2609. if(!empty($this->title)) {
  2610. $this->_out('/Title '.$this->_textstring($this->title));
  2611. }
  2612. if(!empty($this->subject)) {
  2613. $this->_out('/Subject '.$this->_textstring($this->subject));
  2614. }
  2615. if(!empty($this->author)) {
  2616. $this->_out('/Author '.$this->_textstring($this->author));
  2617. }
  2618. if(!empty($this->keywords)) {
  2619. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  2620. }
  2621. if(!empty($this->creator)) {
  2622. $this->_out('/Creator '.$this->_textstring($this->creator));
  2623. }
  2624. $this->_out('/CreationDate '.$this->_textstring('D:'.date('YmdHis')));
  2625. }
  2626. /**
  2627. * _putcatalog
  2628. * @access protected
  2629. */
  2630. function _putcatalog() {
  2631. $this->_out('/Type /Catalog');
  2632. $this->_out('/Pages 1 0 R');
  2633. if($this->ZoomMode=='fullpage') {
  2634. $this->_out('/OpenAction [3 0 R /Fit]');
  2635. }
  2636. elseif($this->ZoomMode=='fullwidth') {
  2637. $this->_out('/OpenAction [3 0 R /FitH null]');
  2638. }
  2639. elseif($this->ZoomMode=='real') {
  2640. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  2641. }
  2642. elseif(!is_string($this->ZoomMode)) {
  2643. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
  2644. }
  2645. if($this->LayoutMode=='single') {
  2646. $this->_out('/PageLayout /SinglePage');
  2647. }
  2648. elseif($this->LayoutMode=='continuous') {
  2649. $this->_out('/PageLayout /OneColumn');
  2650. }
  2651. elseif($this->LayoutMode=='two') {
  2652. $this->_out('/PageLayout /TwoColumnLeft');
  2653. }
  2654. }
  2655. /**
  2656. * _puttrailer
  2657. * @access protected
  2658. */
  2659. function _puttrailer() {
  2660. $this->_out('/Size '.($this->n+1));
  2661. $this->_out('/Root '.$this->n.' 0 R');
  2662. $this->_out('/Info '.($this->n-1).' 0 R');
  2663. }
  2664. /**
  2665. * _putheader
  2666. * @access protected
  2667. */
  2668. function _putheader() {
  2669. $this->_out('%PDF-'.$this->PDFVersion);
  2670. }
  2671. /**
  2672. * _enddoc
  2673. * @access protected
  2674. */
  2675. function _enddoc() {
  2676. $this->_putheader();
  2677. $this->_putpages();
  2678. $this->_putresources();
  2679. //Info
  2680. $this->_newobj();
  2681. $this->_out('<<');
  2682. $this->_putinfo();
  2683. $this->_out('>>');
  2684. $this->_out('endobj');
  2685. //Catalog
  2686. $this->_newobj();
  2687. $this->_out('<<');
  2688. $this->_putcatalog();
  2689. $this->_out('>>');
  2690. $this->_out('endobj');
  2691. //Cross-ref
  2692. $o=strlen($this->buffer);
  2693. $this->_out('xref');
  2694. $this->_out('0 '.($this->n+1));
  2695. $this->_out('0000000000 65535 f ');
  2696. for($i=1;$i<=$this->n;$i++) {
  2697. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  2698. }
  2699. //Trailer
  2700. $this->_out('trailer');
  2701. $this->_out('<<');
  2702. $this->_puttrailer();
  2703. $this->_out('>>');
  2704. $this->_out('startxref');
  2705. $this->_out($o);
  2706. $this->_out('%%EOF');
  2707. $this->state=3;
  2708. }
  2709. /**
  2710. * _beginpage
  2711. * @access protected
  2712. */
  2713. function _beginpage($orientation) {
  2714. $this->page++;
  2715. $this->pages[$this->page]='';
  2716. $this->state=2;
  2717. $this->x=$this->lMargin;
  2718. $this->y=$this->tMargin;
  2719. $this->FontFamily='';
  2720. //Page orientation
  2721. if(empty($orientation)) {
  2722. $orientation=$this->DefOrientation;
  2723. }
  2724. else {
  2725. $orientation=strtoupper($orientation{0});
  2726. if($orientation!=$this->DefOrientation) {
  2727. $this->OrientationChanges[$this->page]=true;
  2728. }
  2729. }
  2730. if($orientation!=$this->CurOrientation) {
  2731. //Change orientation
  2732. if($orientation=='P') {
  2733. $this->wPt=$this->fwPt;
  2734. $this->hPt=$this->fhPt;
  2735. $this->w=$this->fw;
  2736. $this->h=$this->fh;
  2737. }
  2738. else {
  2739. $this->wPt=$this->fhPt;
  2740. $this->hPt=$this->fwPt;
  2741. $this->w=$this->fh;
  2742. $this->h=$this->fw;
  2743. }
  2744. $this->PageBreakTrigger=$this->h-$this->bMargin;
  2745. $this->CurOrientation=$orientation;
  2746. }
  2747. }
  2748. /**
  2749. * End of page contents
  2750. * @access protected
  2751. */
  2752. function _endpage() {
  2753. $this->state=1;
  2754. }
  2755. /**
  2756. * Begin a new object
  2757. * @access protected
  2758. */
  2759. function _newobj() {
  2760. $this->n++;
  2761. $this->offsets[$this->n]=strlen($this->buffer);
  2762. $this->_out($this->n.' 0 obj');
  2763. }
  2764. /**
  2765. * Underline text
  2766. * @access protected
  2767. */
  2768. function _dounderline($x,$y,$txt) {
  2769. $up = $this->CurrentFont['up'];
  2770. $ut = $this->CurrentFont['ut'];
  2771. $w = $this->GetStringWidth($txt) + $this->ws * substr_count($txt,' ');
  2772. 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);
  2773. }
  2774. /**
  2775. * Extract info from a JPEG file
  2776. * @access protected
  2777. */
  2778. function _parsejpg($file) {
  2779. $a=GetImageSize($file);
  2780. if(empty($a)) {
  2781. $this->Error('Missing or incorrect image file: '.$file);
  2782. }
  2783. if($a[2]!=2) {
  2784. $this->Error('Not a JPEG file: '.$file);
  2785. }
  2786. if(!isset($a['channels']) or $a['channels']==3) {
  2787. $colspace='DeviceRGB';
  2788. }
  2789. elseif($a['channels']==4) {
  2790. $colspace='DeviceCMYK';
  2791. }
  2792. else {
  2793. $colspace='DeviceGray';
  2794. }
  2795. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  2796. //Read whole file
  2797. $f=fopen($file,'rb');
  2798. $data='';
  2799. while(!feof($f)) {
  2800. $data.=fread($f,4096);
  2801. }
  2802. fclose($f);
  2803. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  2804. }
  2805. /**
  2806. * Extract info from a PNG file
  2807. * @access protected
  2808. */
  2809. function _parsepng($file) {
  2810. $f=fopen($file,'rb');
  2811. if(empty($f)) {
  2812. $this->Error('Can\'t open image file: '.$file);
  2813. }
  2814. //Check signature
  2815. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  2816. $this->Error('Not a PNG file: '.$file);
  2817. }
  2818. //Read header chunk
  2819. fread($f,4);
  2820. if(fread($f,4)!='IHDR') {
  2821. $this->Error('Incorrect PNG file: '.$file);
  2822. }
  2823. $w=$this->_freadint($f);
  2824. $h=$this->_freadint($f);
  2825. $bpc=ord(fread($f,1));
  2826. if($bpc>8) {
  2827. $this->Error('16-bit depth not supported: '.$file);
  2828. }
  2829. $ct=ord(fread($f,1));
  2830. if($ct==0) {
  2831. $colspace='DeviceGray';
  2832. }
  2833. elseif($ct==2) {
  2834. $colspace='DeviceRGB';
  2835. }
  2836. elseif($ct==3) {
  2837. $colspace='Indexed';
  2838. }
  2839. else {
  2840. $this->Error('Alpha channel not supported: '.$file);
  2841. }
  2842. if(ord(fread($f,1))!=0) {
  2843. $this->Error('Unknown compression method: '.$file);
  2844. }
  2845. if(ord(fread($f,1))!=0) {
  2846. $this->Error('Unknown filter method: '.$file);
  2847. }
  2848. if(ord(fread($f,1))!=0) {
  2849. $this->Error('Interlacing not supported: '.$file);
  2850. }
  2851. fread($f,4);
  2852. $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  2853. //Scan chunks looking for palette, transparency and image data
  2854. $pal='';
  2855. $trns='';
  2856. $data='';
  2857. do {
  2858. $n=$this->_freadint($f);
  2859. $type=fread($f,4);
  2860. if($type=='PLTE') {
  2861. //Read palette
  2862. $pal=fread($f,$n);
  2863. fread($f,4);
  2864. }
  2865. elseif($type=='tRNS') {
  2866. //Read transparency info
  2867. $t=fread($f,$n);
  2868. if($ct==0) {
  2869. $trns=array(ord(substr($t,1,1)));
  2870. }
  2871. elseif($ct==2) {
  2872. $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  2873. }
  2874. else {
  2875. $pos=strpos($t,chr(0));
  2876. if($pos!==false) {
  2877. $trns=array($pos);
  2878. }
  2879. }
  2880. fread($f,4);
  2881. }
  2882. elseif($type=='IDAT') {
  2883. //Read image data block
  2884. $data.=fread($f,$n);
  2885. fread($f,4);
  2886. }
  2887. elseif($type=='IEND') {
  2888. break;
  2889. }
  2890. else {
  2891. fread($f,$n+4);
  2892. }
  2893. }
  2894. while($n);
  2895. if($colspace=='Indexed' and empty($pal)) {
  2896. $this->Error('Missing palette in '.$file);
  2897. }
  2898. fclose($f);
  2899. return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
  2900. }
  2901. /**
  2902. * Read a 4-byte integer from file
  2903. * @access protected
  2904. */
  2905. function _freadint($f) {
  2906. //Read a 4-byte integer from file
  2907. $a=unpack('Ni',fread($f,4));
  2908. return $a['i'];
  2909. }
  2910. /**
  2911. * Format a text string
  2912. * @access protected
  2913. */
  2914. function _textstring($s) {
  2915. if($this->isunicode) {
  2916. //Convert string to UTF-16BE
  2917. $s = $this->UTF8ToUTF16BE($s, true);
  2918. }
  2919. return '('. $this->_escape($s).')';
  2920. }
  2921. /**
  2922. * Format a text string
  2923. * @access protected
  2924. */
  2925. function _escapetext($s) {
  2926. if($this->isunicode) {
  2927. //Convert string to UTF-16BE
  2928. $s = $this->UTF8ToUTF16BE($s, false);
  2929. }
  2930. return $this->_escape($s);
  2931. }
  2932. /**
  2933. * Add \ before \, ( and )
  2934. * @access protected
  2935. */
  2936. function _escape($s) {
  2937. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\'));
  2938. }
  2939. /**
  2940. *
  2941. * @access protected
  2942. */
  2943. function _putstream($s) {
  2944. $this->_out('stream');
  2945. $this->_out($s);
  2946. $this->_out('endstream');
  2947. }
  2948. /**
  2949. * Add a line to the document
  2950. * @access protected
  2951. */
  2952. function _out($s) {
  2953. if($this->state==2) {
  2954. $this->pages[$this->page] .= $s."\n";
  2955. }
  2956. else {
  2957. $this->buffer .= $s."\n";
  2958. }
  2959. }
  2960. /**
  2961. * Adds unicode fonts.<br>
  2962. * Based on PDF Reference 1.3 (section 5)
  2963. * @access protected
  2964. * @author Nicola Asuni
  2965. * @since 1.52.0.TC005 (2005-01-05)
  2966. */
  2967. function _puttruetypeunicode($font) {
  2968. // Type0 Font
  2969. // A composite font—a font composed of other fonts, organized hierarchically
  2970. $this->_newobj();
  2971. $this->_out('<</Type /Font');
  2972. $this->_out('/Subtype /Type0');
  2973. $this->_out('/BaseFont /'.$font['name'].'');
  2974. $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.
  2975. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  2976. $this->_out('>>');
  2977. $this->_out('endobj');
  2978. // CIDFontType2
  2979. // A CIDFont whose glyph descriptions are based on TrueType font technology
  2980. $this->_newobj();
  2981. $this->_out('<</Type /Font');
  2982. $this->_out('/Subtype /CIDFontType2');
  2983. $this->_out('/BaseFont /'.$font['name'].'');
  2984. $this->_out('/CIDSystemInfo '.($this->n + 1).' 0 R');
  2985. $this->_out('/FontDescriptor '.($this->n + 2).' 0 R');
  2986. if (isset($font['desc']['MissingWidth'])){
  2987. $this->_out('/DW '.$font['desc']['MissingWidth'].''); // The default width for glyphs in the CIDFont MissingWidth
  2988. }
  2989. $w = "";
  2990. foreach ($font['cw'] as $cid => $width) {
  2991. $w .= ''.$cid.' ['.$width.'] '; // define a specific width for each individual CID
  2992. }
  2993. $this->_out('/W ['.$w.']'); // A description of the widths for the glyphs in the CIDFont
  2994. $this->_out('/CIDToGIDMap '.($this->n + 3).' 0 R');
  2995. $this->_out('>>');
  2996. $this->_out('endobj');
  2997. // CIDSystemInfo dictionary
  2998. // A dictionary containing entries that define the character collection of the CIDFont.
  2999. $this->_newobj();
  3000. $this->_out('<</Registry (Adobe)'); // A string identifying an issuer of character collections
  3001. $this->_out('/Ordering (UCS)'); // A string that uniquely names a character collection issued by a specific registry
  3002. $this->_out('/Supplement 0'); // The supplement number of the character collection.
  3003. $this->_out('>>');
  3004. $this->_out('endobj');
  3005. // Font descriptor
  3006. // A font descriptor describing the CIDFont’s default metrics other than its glyph widths
  3007. $this->_newobj();
  3008. $this->_out('<</Type /FontDescriptor');
  3009. $this->_out('/FontName /'.$font['name']);
  3010. foreach ($font['desc'] as $key => $value) {
  3011. $this->_out('/'.$key.' '.$value);
  3012. }
  3013. if ($font['file']) {
  3014. // A stream containing a TrueType font program
  3015. $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
  3016. }
  3017. $this->_out('>>');
  3018. $this->_out('endobj');
  3019. // Embed CIDToGIDMap
  3020. // A specification of the mapping from CIDs to glyph indices
  3021. $this->_newobj();
  3022. $ctgfile = $this->_getfontpath().$font['ctg'];
  3023. if(!file_exists($ctgfile)) {
  3024. $this->Error('Font file not found: '.$ctgfile);
  3025. }
  3026. $size = filesize($ctgfile);
  3027. $this->_out('<</Length '.$size.'');
  3028. if(substr($ctgfile, -2) == '.z') { // check file extension
  3029. /* Decompresses data encoded using the public-domain
  3030. zlib/deflate compression method, reproducing the
  3031. original text or binary data */
  3032. $this->_out('/Filter /FlateDecode');
  3033. }
  3034. $this->_out('>>');
  3035. $this->_putstream(file_get_contents($ctgfile));
  3036. $this->_out('endobj');
  3037. }
  3038. /**
  3039. * Converts UTF-8 strings to codepoints array.<br>
  3040. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  3041. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  3042. * <pre>
  3043. * Char. number range | UTF-8 octet sequence
  3044. * (hexadecimal) | (binary)
  3045. * --------------------+-----------------------------------------------
  3046. * 0000 0000-0000 007F | 0xxxxxxx
  3047. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  3048. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  3049. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  3050. * ---------------------------------------------------------------------
  3051. *
  3052. * ABFN notation:
  3053. * ---------------------------------------------------------------------
  3054. * UTF8-octets = *( UTF8-char )
  3055. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  3056. * UTF8-1 = %x00-7F
  3057. * UTF8-2 = %xC2-DF UTF8-tail
  3058. *
  3059. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  3060. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  3061. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  3062. * %xF4 %x80-8F 2( UTF8-tail )
  3063. * UTF8-tail = %x80-BF
  3064. * ---------------------------------------------------------------------
  3065. * </pre>
  3066. * @param string $str string to process.
  3067. * @return array containing codepoints (UTF-8 characters values)
  3068. * @access protected
  3069. * @author Nicola Asuni
  3070. * @since 1.53.0.TC005 (2005-01-05)
  3071. */
  3072. function UTF8StringToArray($str) {
  3073. if(!$this->isunicode) {
  3074. return $str; // string is not in unicode
  3075. }
  3076. $unicode = array(); // array containing unicode values
  3077. $bytes = array(); // array containing single character byte sequences
  3078. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  3079. $str .= ""; // force $str to be a string
  3080. $length = strlen($str);
  3081. for($i = 0; $i < $length; $i++) {
  3082. $char = ord($str{$i}); // get one string character at time
  3083. if(count($bytes) == 0) { // get starting octect
  3084. if ($char <= 0x7F) {
  3085. $unicode[] = $char; // use the character "as is" because is ASCII
  3086. $numbytes = 1;
  3087. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  3088. $bytes[] = ($char - 0xC0) << 0x06;
  3089. $numbytes = 2;
  3090. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  3091. $bytes[] = ($char - 0xE0) << 0x0C;
  3092. $numbytes = 3;
  3093. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  3094. $bytes[] = ($char - 0xF0) << 0x12;
  3095. $numbytes = 4;
  3096. } else {
  3097. // use replacement character for other invalid sequences
  3098. $unicode[] = 0xFFFD;
  3099. $bytes = array();
  3100. $numbytes = 1;
  3101. }
  3102. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  3103. $bytes[] = $char - 0x80;
  3104. if (count($bytes) == $numbytes) {
  3105. // compose UTF-8 bytes to a single unicode value
  3106. $char = $bytes[0];
  3107. for($j = 1; $j < $numbytes; $j++) {
  3108. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  3109. }
  3110. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  3111. /* The definition of UTF-8 prohibits encoding character numbers between
  3112. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  3113. encoding form (as surrogate pairs) and do not directly represent
  3114. characters. */
  3115. $unicode[] = 0xFFFD; // use replacement character
  3116. }
  3117. else {
  3118. $unicode[] = $char; // add char to array
  3119. }
  3120. // reset data for next char
  3121. $bytes = array();
  3122. $numbytes = 1;
  3123. }
  3124. } else {
  3125. // use replacement character for other invalid sequences
  3126. $unicode[] = 0xFFFD;
  3127. $bytes = array();
  3128. $numbytes = 1;
  3129. }
  3130. }
  3131. return $unicode;
  3132. }
  3133. /**
  3134. * Converts UTF-8 strings to UTF16-BE.<br>
  3135. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  3136. * <pre>
  3137. * Encoding UTF-16:
  3138. *
  3139. * Encoding of a single character from an ISO 10646 character value to
  3140. * UTF-16 proceeds as follows. Let U be the character number, no greater
  3141. * than 0x10FFFF.
  3142. *
  3143. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  3144. * terminate.
  3145. *
  3146. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  3147. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  3148. * represented in 20 bits.
  3149. *
  3150. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  3151. * 0xDC00, respectively. These integers each have 10 bits free to
  3152. * encode the character value, for a total of 20 bits.
  3153. *
  3154. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  3155. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  3156. * bits of W2. Terminate.
  3157. *
  3158. * Graphically, steps 2 through 4 look like:
  3159. * U' = yyyyyyyyyyxxxxxxxxxx
  3160. * W1 = 110110yyyyyyyyyy
  3161. * W2 = 110111xxxxxxxxxx
  3162. * </pre>
  3163. * @param string $str string to process.
  3164. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  3165. * @return string
  3166. * @access protected
  3167. * @author Nicola Asuni
  3168. * @since 1.53.0.TC005 (2005-01-05)
  3169. * @uses UTF8StringToArray
  3170. */
  3171. function UTF8ToUTF16BE($str, $setbom=true) {
  3172. if(!$this->isunicode) {
  3173. return $str; // string is not in unicode
  3174. }
  3175. $outstr = ""; // string to be returned
  3176. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  3177. $numitems = count($unicode);
  3178. if ($setbom) {
  3179. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  3180. }
  3181. foreach($unicode as $char) {
  3182. if($char == 0xFFFD) {
  3183. $outstr .= "\xFF\xFD"; // replacement character
  3184. } elseif ($char < 0x10000) {
  3185. $outstr .= chr($char >> 0x08);
  3186. $outstr .= chr($char & 0xFF);
  3187. } else {
  3188. $char -= 0x10000;
  3189. $w1 = 0xD800 | ($char >> 0x10);
  3190. $w2 = 0xDC00 | ($char & 0x3FF);
  3191. $outstr .= chr($w1 >> 0x08);
  3192. $outstr .= chr($w1 & 0xFF);
  3193. $outstr .= chr($w2 >> 0x08);
  3194. $outstr .= chr($w2 & 0xFF);
  3195. }
  3196. }
  3197. return $outstr;
  3198. }
  3199. // ====================================================
  3200. /**
  3201. * Set header font.
  3202. * @param array $font font
  3203. * @since 1.1
  3204. */
  3205. function setHeaderFont($font) {
  3206. $this->header_font = $font;
  3207. }
  3208. /**
  3209. * Set footer font.
  3210. * @param array $font font
  3211. * @since 1.1
  3212. */
  3213. function setFooterFont($font) {
  3214. $this->footer_font = $font;
  3215. }
  3216. /**
  3217. * Set language array.
  3218. * @param array $language
  3219. * @since 1.1
  3220. */
  3221. function setLanguageArray($language) {
  3222. $this->l = $language;
  3223. }
  3224. /**
  3225. * Set document barcode.
  3226. * @param string $bc barcode
  3227. */
  3228. function setBarcode($bc="") {
  3229. $this->barcode = $bc;
  3230. }
  3231. /**
  3232. * Print Barcode.
  3233. * @param int $x x position in user units
  3234. * @param int $y y position in user units
  3235. * @param int $w width in user units
  3236. * @param int $h height position in user units
  3237. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  3238. * @param string $style barcode style
  3239. * @param string $font font for text
  3240. * @param int $xres x resolution
  3241. * @param string $code code to print
  3242. */
  3243. function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  3244. require_once(dirname(__FILE__)."/barcode/barcode.php");
  3245. require_once(dirname(__FILE__)."/barcode/i25object.php");
  3246. require_once(dirname(__FILE__)."/barcode/c39object.php");
  3247. require_once(dirname(__FILE__)."/barcode/c128aobject.php");
  3248. require_once(dirname(__FILE__)."/barcode/c128bobject.php");
  3249. require_once(dirname(__FILE__)."/barcode/c128cobject.php");
  3250. if (empty($code)) {
  3251. return;
  3252. }
  3253. if (empty($style)) {
  3254. $style = BCS_ALIGN_LEFT;
  3255. $style |= BCS_IMAGE_PNG;
  3256. $style |= BCS_TRANSPARENT;
  3257. //$style |= BCS_BORDER;
  3258. //$style |= BCS_DRAW_TEXT;
  3259. //$style |= BCS_STRETCH_TEXT;
  3260. //$style |= BCS_REVERSE_COLOR;
  3261. }
  3262. if (empty($font)) {$font = BCD_DEFAULT_FONT;}
  3263. if (empty($xres)) {$xres = BCD_DEFAULT_XRES;}
  3264. $scale_factor = 1.5 * $xres * $this->k;
  3265. $bc_w = round($w * $scale_factor); //width in points
  3266. $bc_h = round($h * $scale_factor); //height in points
  3267. switch (strtoupper($type)) {
  3268. case "I25": {
  3269. $obj = new I25Object($bc_w, $bc_h, $style, $code);
  3270. break;
  3271. }
  3272. case "C128A": {
  3273. $obj = new C128AObject($bc_w, $bc_h, $style, $code);
  3274. break;
  3275. }
  3276. default:
  3277. case "C128B": {
  3278. $obj = new C128BObject($bc_w, $bc_h, $style, $code);
  3279. break;
  3280. }
  3281. case "C128C": {
  3282. $obj = new C128CObject($bc_w, $bc_h, $style, $code);
  3283. break;
  3284. }
  3285. case "C39": {
  3286. $obj = new C39Object($bc_w, $bc_h, $style, $code);
  3287. break;
  3288. }
  3289. }
  3290. $obj->SetFont($font);
  3291. $obj->DrawObject($xres);
  3292. //use a temporary file....
  3293. $tmpName = tempnam(K_PATH_CACHE,'img');
  3294. imagepng($obj->getImage(), $tmpName);
  3295. $this->Image($tmpName, $x, $y, $w, $h, 'png');
  3296. $obj->DestroyObject();
  3297. unset($obj);
  3298. unlink($tmpName);
  3299. }
  3300. /**
  3301. * Returns the PDF data.
  3302. */
  3303. function getPDFData() {
  3304. if($this->state < 3) {
  3305. $this->Close();
  3306. }
  3307. return $this->buffer;
  3308. }
  3309. // --- HTML PARSER FUNCTIONS ---
  3310. /**
  3311. * Allows to preserve some HTML formatting.<br />
  3312. * 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
  3313. * @param string $html text to display
  3314. * @param boolean $ln if true add a new line after text (default = true)
  3315. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  3316. */
  3317. function writeHTML($html, $ln=true, $fill=0) {
  3318. // store some variables
  3319. $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
  3320. //replace carriage returns, newlines and tabs
  3321. $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
  3322. $html = strtr($html, $repTable);
  3323. $pattern = '/(<[^>]+>)/Uu';
  3324. $a = preg_split($pattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); //explodes the string
  3325. if (empty($this->lasth)) {
  3326. //set row height
  3327. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3328. }
  3329. foreach($a as $key=>$element) {
  3330. if (!preg_match($pattern, $element)) {
  3331. //Text
  3332. if($this->HREF) {
  3333. $this->addHtmlLink($this->HREF, $element, $fill);
  3334. }
  3335. elseif($this->tdbegin) {
  3336. if((strlen(trim($element)) > 0) AND ($element != "&nbsp;")) {
  3337. $this->Cell($this->tdwidth, $this->tdheight, $this->unhtmlentities($element), $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
  3338. }
  3339. elseif($element == "&nbsp;") {
  3340. $this->Cell($this->tdwidth, $this->tdheight, '', $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
  3341. }
  3342. }
  3343. else {
  3344. $this->Write($this->lasth, stripslashes($this->unhtmlentities($element)), '', $fill);
  3345. }
  3346. }
  3347. else {
  3348. $element = substr($element, 1, -1);
  3349. //Tag
  3350. if($element{0}=='/') {
  3351. $this->closedHTMLTagHandler(strtolower(substr($element, 1)));
  3352. }
  3353. else {
  3354. //Extract attributes
  3355. // get tag name
  3356. preg_match('/([a-zA-Z0-9]*)/', $element, $tag);
  3357. $tag = strtolower($tag[0]);
  3358. // get attributes
  3359. preg_match_all('/([^=\s]*)=["\']?([^"\']*)["\']?/', $element, $attr_array, PREG_PATTERN_ORDER);
  3360. $attr = array(); // reset attribute array
  3361. while(list($id,$name)=each($attr_array[1])) {
  3362. $attr[strtolower($name)] = $attr_array[2][$id];
  3363. }
  3364. $this->openHTMLTagHandler($tag, $attr, $fill);
  3365. }
  3366. }
  3367. }
  3368. if ($ln) {
  3369. $this->Ln($this->lasth);
  3370. }
  3371. }
  3372. /**
  3373. * 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 />
  3374. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3375. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3376. * @param float $h Cell minimum height. The cell extends automatically if needed.
  3377. * @param float $x upper-left corner X coordinate
  3378. * @param float $y upper-left corner Y coordinate
  3379. * @param string $html html text to print. Default value: empty string.
  3380. * @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>
  3381. * @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>
  3382. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3383. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3384. * @see Cell()
  3385. */
  3386. function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0) {
  3387. if (empty($this->lasth)) {
  3388. //set row height
  3389. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3390. }
  3391. if (empty($x)) {
  3392. $x = $this->GetX();
  3393. }
  3394. if (empty($y)) {
  3395. $y = $this->GetY();
  3396. }
  3397. // get current page number
  3398. $pagenum = $this->page;
  3399. $this->SetX($x);
  3400. $this->SetY($y);
  3401. if(empty($w)) {
  3402. $w = $this->fw - $x - $this->rMargin;
  3403. }
  3404. // store original margin values
  3405. $lMargin = $this->lMargin;
  3406. $rMargin = $this->rMargin;
  3407. // set new margin values
  3408. $this->SetLeftMargin($x);
  3409. $this->SetRightMargin($this->fw - $x - $w);
  3410. // calculate remaining vertical space on page
  3411. $restspace = $this->getPageHeight() - $this->GetY() - $this->getBreakMargin();
  3412. $this->writeHTML($html, true, $fill); // write html text
  3413. $currentY = $this->GetY();
  3414. // check if a new page has been created
  3415. if ($this->page > $pagenum) {
  3416. // design a cell around the text on first page
  3417. $currentpage = $this->page;
  3418. $this->page = $pagenum;
  3419. $this->SetY($this->getPageHeight() - $restspace - $this->getBreakMargin());
  3420. $h = $restspace - 1;
  3421. $this->Cell($w, $h, "", $border, $ln, 'L', 0);
  3422. // design a cell around the text on last page
  3423. $this->page = $currentpage;
  3424. $h = $currentY - $this->tMargin;
  3425. $this->SetY($this->tMargin); // put cursor at the beginning of text
  3426. $this->Cell($w, $h, "", $border, $ln, 'L', 0);
  3427. } else {
  3428. $h = max($h, ($currentY - $y));
  3429. $this->SetY($y); // put cursor at the beginning of text
  3430. // design a cell around the text
  3431. $this->Cell($w, $h, "", $border, $ln, 'L', 0);
  3432. }
  3433. // restore original margin values
  3434. $this->SetLeftMargin($lMargin);
  3435. $this->SetRightMargin($rMargin);
  3436. if ($ln) {
  3437. $this->Ln(0);
  3438. }
  3439. }
  3440. /**
  3441. * Process opening tags.
  3442. * @param string $tag tag name (in uppercase)
  3443. * @param string $attr tag attribute (in uppercase)
  3444. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3445. * @access private
  3446. */
  3447. function openHTMLTagHandler($tag, $attr, $fill=0) {
  3448. //Opening tag
  3449. switch($tag) {
  3450. case 'table': {
  3451. if ((isset($attr['border'])) AND ($attr['border'] != '')) {
  3452. $this->tableborder = $attr['border'];
  3453. }
  3454. else {
  3455. $this->tableborder = 0;
  3456. }
  3457. break;
  3458. }
  3459. case 'tr': {
  3460. break;
  3461. }
  3462. case 'td':
  3463. case 'th': {
  3464. if ((isset($attr['width'])) AND ($attr['width'] != '')) {
  3465. $this->tdwidth = ($attr['width']/4);
  3466. }
  3467. else {
  3468. $this->tdwidth = (($this->w - $this->lMargin - $this->rMargin) / $this->default_table_columns);
  3469. }
  3470. if ((isset($attr['height'])) AND ($attr['height'] != '')) {
  3471. $this->tdheight=($attr['height'] / $this->k);
  3472. }
  3473. else {
  3474. $this->tdheight = $this->lasth;
  3475. }
  3476. if ((isset($attr['align'])) AND ($attr['align'] != '')) {
  3477. switch ($attr['align']) {
  3478. case 'center': {
  3479. $this->tdalign = "C";
  3480. break;
  3481. }
  3482. case 'right': {
  3483. $this->tdalign = "R";
  3484. break;
  3485. }
  3486. default:
  3487. case 'left': {
  3488. $this->tdalign = "L";
  3489. break;
  3490. }
  3491. }
  3492. }
  3493. if ((isset($attr['bgcolor'])) AND ($attr['bgcolor'] != '')) {
  3494. $coul = $this->convertColorHexToDec($attr['bgcolor']);
  3495. $this->SetFillColor($coul['R'], $coul['G'], $coul['B']);
  3496. $this->tdbgcolor=true;
  3497. }
  3498. $this->tdbegin=true;
  3499. break;
  3500. }
  3501. case 'hr': {
  3502. $this->Ln();
  3503. if ((isset($attr['width'])) AND ($attr['width'] != '')) {
  3504. $hrWidth = $attr['width'];
  3505. }
  3506. else {
  3507. $hrWidth = $this->w - $this->lMargin - $this->rMargin;
  3508. }
  3509. $x = $this->GetX();
  3510. $y = $this->GetY();
  3511. $this->SetLineWidth(0.2);
  3512. $this->Line($x, $y, $x + $hrWidth, $y);
  3513. $this->SetLineWidth(0.2);
  3514. $this->Ln();
  3515. break;
  3516. }
  3517. case 'strong': {
  3518. $this->setStyle('b', true);
  3519. break;
  3520. }
  3521. case 'em': {
  3522. $this->setStyle('i', true);
  3523. break;
  3524. }
  3525. case 'b':
  3526. case 'i':
  3527. case 'u': {
  3528. $this->setStyle($tag, true);
  3529. break;
  3530. }
  3531. case 'a': {
  3532. $this->HREF = $attr['href'];
  3533. break;
  3534. }
  3535. case 'img': {
  3536. if(isset($attr['src'])) {
  3537. // replace relative path with real server path
  3538. $attr['src'] = str_replace(K_PATH_URL_CACHE, K_PATH_CACHE, $attr['src']);
  3539. if(!isset($attr['width'])) {
  3540. $attr['width'] = 0;
  3541. }
  3542. if(!isset($attr['height'])) {
  3543. $attr['height'] = 0;
  3544. }
  3545. $this->Image($attr['src'], $this->GetX(),$this->GetY(), $this->pixelsToMillimeters($attr['width']), $this->pixelsToMillimeters($attr['height']));
  3546. //$this->SetX($this->img_rb_x);
  3547. $this->SetY($this->img_rb_y);
  3548. }
  3549. break;
  3550. }
  3551. case 'ul': {
  3552. $this->listordered = false;
  3553. $this->listcount = 0;
  3554. break;
  3555. }
  3556. case 'ol': {
  3557. $this->listordered = true;
  3558. $this->listcount = 0;
  3559. break;
  3560. }
  3561. case 'li': {
  3562. $this->Ln();
  3563. if ($this->listordered) {
  3564. $this->lispacer = " ".(++$this->listcount).". ";
  3565. }
  3566. else {
  3567. //unordered list simbol
  3568. $this->lispacer = " - ";
  3569. }
  3570. $this->Write($this->lasth, $this->lispacer, '', $fill);
  3571. break;
  3572. }
  3573. case 'tr':
  3574. case 'blockquote':
  3575. case 'br': {
  3576. $this->Ln();
  3577. if(strlen($this->lispacer) > 0) {
  3578. $this->x += $this->GetStringWidth($this->lispacer);
  3579. }
  3580. break;
  3581. }
  3582. case 'p': {
  3583. $this->Ln();
  3584. $this->Ln();
  3585. break;
  3586. }
  3587. case 'sup': {
  3588. $currentFontSize = $this->FontSize;
  3589. $this->tempfontsize = $this->FontSizePt;
  3590. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  3591. $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  3592. break;
  3593. }
  3594. case 'sub': {
  3595. $currentFontSize = $this->FontSize;
  3596. $this->tempfontsize = $this->FontSizePt;
  3597. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  3598. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  3599. break;
  3600. }
  3601. case 'small': {
  3602. $currentFontSize = $this->FontSize;
  3603. $this->tempfontsize = $this->FontSizePt;
  3604. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  3605. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)/3));
  3606. break;
  3607. }
  3608. case 'font': {
  3609. if (isset($attr['color']) AND $attr['color']!='') {
  3610. $coul = $this->convertColorHexToDec($attr['color']);
  3611. $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
  3612. $this->issetcolor=true;
  3613. }
  3614. if (isset($attr['face']) and in_array(strtolower($attr['face']), $this->fontlist)) {
  3615. $this->SetFont(strtolower($attr['FACE']));
  3616. $this->issetfont=true;
  3617. }
  3618. if (isset($attr['size'])) {
  3619. $headsize = intval($attr['size']);
  3620. } else {
  3621. $headsize = 0;
  3622. }
  3623. $currentFontSize = $this->FontSize;
  3624. $this->tempfontsize = $this->FontSizePt;
  3625. $this->SetFontSize($this->FontSizePt + $headsize);
  3626. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3627. break;
  3628. }
  3629. case 'h1':
  3630. case 'h2':
  3631. case 'h3':
  3632. case 'h4':
  3633. case 'h5':
  3634. case 'h6': {
  3635. $headsize = (4 - substr($tag, 1)) * 2;
  3636. $currentFontSize = $this->FontSize;
  3637. $this->tempfontsize = $this->FontSizePt;
  3638. $this->SetFontSize($this->FontSizePt + $headsize);
  3639. $this->setStyle('b', true);
  3640. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3641. break;
  3642. }
  3643. }
  3644. }
  3645. /**
  3646. * Process closing tags.
  3647. * @param string $tag tag name (in uppercase)
  3648. * @access private
  3649. */
  3650. function closedHTMLTagHandler($tag) {
  3651. //Closing tag
  3652. switch($tag) {
  3653. case 'td':
  3654. case 'th': {
  3655. $this->tdbegin = false;
  3656. $this->tdwidth = 0;
  3657. $this->tdheight = 0;
  3658. $this->tdalign = "L";
  3659. $this->tdbgcolor = false;
  3660. $this->SetFillColor($this->prevFillColor[0], $this->prevFillColor[1], $this->prevFillColor[2]);
  3661. break;
  3662. }
  3663. case 'tr': {
  3664. $this->Ln();
  3665. break;
  3666. }
  3667. case 'table': {
  3668. $this->tableborder=0;
  3669. break;
  3670. }
  3671. case 'strong': {
  3672. $this->setStyle('b', false);
  3673. }
  3674. case 'em': {
  3675. $this->setStyle('i', false);
  3676. }
  3677. case 'b':
  3678. case 'i':
  3679. case 'u': {
  3680. $this->setStyle($tag, false);
  3681. break;
  3682. }
  3683. case 'a': {
  3684. $this->HREF = '';
  3685. break;
  3686. }
  3687. case 'sup': {
  3688. $currentFontSize = $this->FontSize;
  3689. $this->SetFontSize($this->tempfontsize);
  3690. $this->tempfontsize = $this->FontSizePt;
  3691. $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  3692. break;
  3693. }
  3694. case 'sub': {
  3695. $currentFontSize = $this->FontSize;
  3696. $this->SetFontSize($this->tempfontsize);
  3697. $this->tempfontsize = $this->FontSizePt;
  3698. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  3699. break;
  3700. }
  3701. case 'small': {
  3702. $currentFontSize = $this->FontSize;
  3703. $this->SetFontSize($this->tempfontsize);
  3704. $this->tempfontsize = $this->FontSizePt;
  3705. $this->SetXY($this->GetX(), $this->GetY() - (($this->FontSize - $currentFontSize)/3));
  3706. break;
  3707. }
  3708. case 'font': {
  3709. if ($this->issetcolor == true) {
  3710. $this->SetTextColor($this->prevTextColor[0], $this->prevTextColor[1], $this->prevTextColor[2]);
  3711. }
  3712. if ($this->issetfont) {
  3713. $this->FontFamily = $this->prevFontFamily;
  3714. $this->FontStyle = $this->prevFontStyle;
  3715. $this->SetFont($this->FontFamily);
  3716. $this->issetfont = false;
  3717. }
  3718. $currentFontSize = $this->FontSize;
  3719. $this->SetFontSize($this->tempfontsize);
  3720. $this->tempfontsize = $this->FontSizePt;
  3721. //$this->TextColor = $this->prevTextColor;
  3722. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3723. break;
  3724. }
  3725. case 'ul': {
  3726. $this->Ln();
  3727. break;
  3728. }
  3729. case 'ol': {
  3730. $this->Ln();
  3731. break;
  3732. }
  3733. case 'li': {
  3734. $this->lispacer = "";
  3735. break;
  3736. }
  3737. case 'h1':
  3738. case 'h2':
  3739. case 'h3':
  3740. case 'h4':
  3741. case 'h5':
  3742. case 'h6': {
  3743. $currentFontSize = $this->FontSize;
  3744. $this->SetFontSize($this->tempfontsize);
  3745. $this->tempfontsize = $this->FontSizePt;
  3746. $this->setStyle('b', false);
  3747. $this->Ln();
  3748. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  3749. break;
  3750. }
  3751. default : {
  3752. break;
  3753. }
  3754. }
  3755. }
  3756. /**
  3757. * Sets font style.
  3758. * @param string $tag tag name (in lowercase)
  3759. * @param boolean $enable
  3760. * @access private
  3761. */
  3762. function setStyle($tag, $enable) {
  3763. //Modify style and select corresponding font
  3764. $this->$tag += ($enable ? 1 : -1);
  3765. $style='';
  3766. foreach(array('b', 'i', 'u') as $s) {
  3767. if($this->$s > 0) {
  3768. $style .= $s;
  3769. }
  3770. }
  3771. $this->SetFont('', $style);
  3772. }
  3773. /**
  3774. * Output anchor link.
  3775. * @param string $url link URL
  3776. * @param string $name link name
  3777. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3778. * @access public
  3779. */
  3780. function addHtmlLink($url, $name, $fill=0) {
  3781. //Put a hyperlink
  3782. $this->SetTextColor(0, 0, 255);
  3783. $this->setStyle('u', true);
  3784. $this->Write($this->lasth, $name, $url, $fill);
  3785. $this->setStyle('u', false);
  3786. $this->SetTextColor(0);
  3787. }
  3788. /**
  3789. * Returns an associative array (keys: R,G,B) from
  3790. * a hex html code (e.g. #3FE5AA).
  3791. * @param string $color hexadecimal html color [#rrggbb]
  3792. * @return array
  3793. * @access private
  3794. */
  3795. function convertColorHexToDec($color = "#000000"){
  3796. $tbl_color = array();
  3797. $tbl_color['R'] = hexdec(substr($color, 1, 2));
  3798. $tbl_color['G'] = hexdec(substr($color, 3, 2));
  3799. $tbl_color['B'] = hexdec(substr($color, 5, 2));
  3800. return $tbl_color;
  3801. }
  3802. /**
  3803. * Converts pixels to millimeters in 72 dpi.
  3804. * @param int $px pixels
  3805. * @return float millimeters
  3806. * @access private
  3807. */
  3808. function pixelsToMillimeters($px){
  3809. return $px * 25.4 / 72;
  3810. }
  3811. /**
  3812. * Reverse function for htmlentities.
  3813. * Convert entities in UTF-8.
  3814. *
  3815. * @param $text_to_convert Text to convert.
  3816. * @return string converted
  3817. */
  3818. function unhtmlentities($text_to_convert) {
  3819. require_once(dirname(__FILE__).'/html_entity_decode_php4.php');
  3820. return html_entity_decode_php4($text_to_convert);
  3821. }
  3822. } // END OF CLASS
  3823. //Handle special IE contype request
  3824. if(isset($_SERVER['HTTP_USER_AGENT']) AND ($_SERVER['HTTP_USER_AGENT']=='contype')) {
  3825. header('Content-Type: application/pdf');
  3826. exit;
  3827. }
  3828. }
  3829. //============================================================+
  3830. // END OF FILE
  3831. //============================================================+
  3832. ?>