PageRenderTime 48ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/tcpdf/tcpdf.php

https://bitbucket.org/biojazzard/joomla-eboracast
PHP | 6997 lines | 3987 code | 482 blank | 2528 comment | 850 complexity | 8a8e84bc2bc3cf75eae666b1e0097305 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Begin : 2002-08-03
  5. // Last Update : 2008-03-07
  6. // Author : Nicola Asuni
  7. // Version : 2.5.000_PHP4
  8. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  9. //
  10. // Description : This is a PHP5 class for generating PDF files
  11. // on-the-fly without requiring external
  12. // extensions.
  13. //
  14. // NOTE:
  15. // This class was originally derived in 2002 from the Public
  16. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org).
  17. //
  18. // Main features:
  19. // - supports all ISO page formats;
  20. // - supports UTF-8 Unicode and Right-To-Left languages;
  21. // - supports document encryption;
  22. // - includes methods to publish some xhtml code;
  23. // - includes graphic and transformation methods;
  24. // - includes bookmarks;
  25. // - includes Javascript and forms support;
  26. // - 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/)
  27. // - supports TrueTypeUnicode, TrueType, Type1 and encoding;
  28. // - supports custom page formats, margins and units of measure;
  29. // - includes methods for page header and footer management;
  30. // - supports automatic page break;
  31. // - supports automatic page numbering;
  32. // - supports automatic line break and text justification;
  33. // - supports JPEG, PNG anf GIF images;
  34. // - supports colors;
  35. // - supports links;
  36. // - support page compression (require zlib extension: http://www.gzip.org/zlib/);
  37. // - the source code is full documented in PhpDocumentor Style (http://www.phpdoc.org).
  38. //
  39. // -----------------------------------------------------------
  40. // THANKS TO:
  41. //
  42. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  43. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  44. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  45. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  46. // dullus for text Justification.
  47. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  48. // Patrick Benny for text stretch suggestion on Cell().
  49. // Johannes G�ntert for JavaScript support.
  50. // Denis Van Nuffelen for Dynamic Form.
  51. // Jacek Czekaj for multibyte justification
  52. // Anthony Ferrara for the reintroduction of legacy image methods.
  53. // Anyone that has reported a bug or sent a suggestion.
  54. //============================================================+
  55. /**
  56. * This is a PHP5 class for generating PDF files on-the-fly without requiring external extensions.<br>
  57. * TCPDF project (http://tcpdf.sourceforge.net) has been originally derived from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
  58. * <h3>TCPDF main features are:</h3>
  59. * <ul>
  60. * <li>supports all ISO page formats;</li>
  61. * <li>supports UTF-8 Unicode and Right-To-Left languages;</li>
  62. * <li>supports document encryption;</li>
  63. * <li>includes methods to publish some 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>
  64. * <li>includes a method to print various barcode formats using an improved version of "Generic Barcode Render Class" by Karim Mribti (<a href="http://www.mribti.com/barcode/" target="_blank" title="Generic Barcode Render Class by Karim Mribti">http://www.mribti.com/barcode/</a>) (require GD library: <a href="http://www.boutell.com/gd/" target="_blank" title="GD library">http://www.boutell.com/gd/</a>)</li>
  65. * <li>supports TrueTypeUnicode, TrueType, Type1 and encoding; </li>
  66. * <li>supports custom page formats, margins and units of measure;</li>
  67. * <li>includes methods for page header and footer management;</li>
  68. * <li>supports automatic page break;</li>
  69. * <li>supports automatic page numbering;</li>
  70. * <li>supports automatic line break and text justification;</li>
  71. * <li>supports JPEG, PNG anf GIF images;</li>
  72. * <li>supports colors;</li>
  73. * <li>supports links;</li>
  74. * <li>support page compression (require zlib extension: <a href="http://www.gzip.org/zlib/" target="_blank" title="zlib">http://www.gzip.org/zlib/</a>);</li>
  75. * <li>the source code is full documented in PhpDocumentor Style (<a href="http://www.phpdoc.org" target="_blank" title="phpDocumentor">http://www.phpdoc.org</a>).</li>
  76. * </ul>
  77. * Tools to encode your unicode fonts are on fonts/ttf2ufm directory.</p>
  78. * @name TCPDF
  79. * @package com.tecnick.tcpdf
  80. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  81. * @author Nicola Asuni
  82. * @copyright 2004-2008 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  83. * @link http://www.tcpdf.org
  84. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  85. * @version 2.5.000_PHP4
  86. */
  87. /**
  88. * include configuration file
  89. */
  90. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  91. if(!class_exists('TCPDF')) {
  92. /**
  93. * define default PDF document producer
  94. */
  95. define('PDF_PRODUCER','TCPDF 2.5.000_PHP4 (http://www.tcpdf.org)');
  96. /**
  97. * This is a PHP5 class for generating PDF files on-the-fly without requiring external extensions.<br>
  98. * TCPDF project (http://tcpdf.sourceforge.net) has been originally derived from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org).<br>
  99. * To add your own TTF fonts please read /fonts/README.TXT
  100. * @name TCPDF
  101. * @package com.tecnick.tcpdf
  102. * @version 2.5.000_PHP4
  103. * @author Nicola Asuni
  104. * @link http://www.tcpdf.org
  105. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  106. */
  107. class TCPDF {
  108. // Private or Protected properties
  109. /**
  110. * @var current page number
  111. * @access protected
  112. */
  113. var $page;
  114. /**
  115. * @var current object number
  116. * @access protected
  117. */
  118. var $n;
  119. /**
  120. * @var array of object offsets
  121. * @access protected
  122. */
  123. var $offsets;
  124. /**
  125. * @var buffer holding in-memory PDF
  126. * @access protected
  127. */
  128. var $buffer;
  129. /**
  130. * @var array containing pages
  131. * @access protected
  132. */
  133. var $pages;
  134. /**
  135. * @var current document state
  136. * @access protected
  137. */
  138. var $state;
  139. /**
  140. * @var compression flag
  141. * @access protected
  142. */
  143. var $compress;
  144. /**
  145. * @var default page orientation (P = Portrait, L = Landscape)
  146. * @access protected
  147. */
  148. var $DefOrientation;
  149. /**
  150. * @var current page orientation (P = Portrait, L = Landscape)
  151. * @access protected
  152. */
  153. var $CurOrientation;
  154. /**
  155. * @var array indicating page orientation changes
  156. * @access protected
  157. */
  158. var $OrientationChanges;
  159. /**
  160. * @var scale factor (number of points in user unit)
  161. * @access protected
  162. */
  163. var $k;
  164. /**
  165. * @var width of page format in points
  166. * @access protected
  167. */
  168. var $fwPt;
  169. /**
  170. * @var height of page format in points
  171. * @access protected
  172. */
  173. var $fhPt;
  174. /**
  175. * @var width of page format in user unit
  176. * @access protected
  177. */
  178. var $fw;
  179. /**
  180. * @var height of page format in user unit
  181. * @access protected
  182. */
  183. var $fh;
  184. /**
  185. * @var current width of page in points
  186. * @access protected
  187. */
  188. var $wPt;
  189. /**
  190. * @var current height of page in points
  191. * @access protected
  192. */
  193. var $hPt;
  194. /**
  195. * @var current width of page in user unit
  196. * @access protected
  197. */
  198. var $w;
  199. /**
  200. * @var current height of page in user unit
  201. * @access protected
  202. */
  203. var $h;
  204. /**
  205. * @var left margin
  206. * @access protected
  207. */
  208. var $lMargin;
  209. /**
  210. * @var top margin
  211. * @access protected
  212. */
  213. var $tMargin;
  214. /**
  215. * @var right margin
  216. * @access protected
  217. */
  218. var $rMargin;
  219. /**
  220. * @var page break margin
  221. * @access protected
  222. */
  223. var $bMargin;
  224. /**
  225. * @var cell internal padding
  226. * @access protected
  227. */
  228. var $cMargin;
  229. /**
  230. * @var current horizontal position in user unit for cell positioning
  231. * @access protected
  232. */
  233. var $x;
  234. /**
  235. * @var current vertical position in user unit for cell positioning
  236. * @access protected
  237. */
  238. var $y;
  239. /**
  240. * @var height of last cell printed
  241. * @access protected
  242. */
  243. var $lasth;
  244. /**
  245. * @var line width in user unit
  246. * @access protected
  247. */
  248. var $LineWidth;
  249. /**
  250. * @var array of standard font names
  251. * @access protected
  252. */
  253. var $CoreFonts;
  254. /**
  255. * @var array of used fonts
  256. * @access protected
  257. */
  258. var $fonts;
  259. /**
  260. * @var array of font files
  261. * @access protected
  262. */
  263. var $FontFiles;
  264. /**
  265. * @var array of encoding differences
  266. * @access protected
  267. */
  268. var $diffs;
  269. /**
  270. * @var array of used images
  271. * @access protected
  272. */
  273. var $images;
  274. /**
  275. * @var array of links in pages
  276. * @access protected
  277. */
  278. var $PageLinks;
  279. /**
  280. * @var array of internal links
  281. * @access protected
  282. */
  283. var $links;
  284. /**
  285. * @var current font family
  286. * @access protected
  287. */
  288. var $FontFamily;
  289. /**
  290. * @var current font style
  291. * @access protected
  292. */
  293. var $FontStyle;
  294. /**
  295. * @var underlining flag
  296. * @access protected
  297. */
  298. var $underline;
  299. /**
  300. * @var current font info
  301. * @access protected
  302. */
  303. var $CurrentFont;
  304. /**
  305. * @var current font size in points
  306. * @access protected
  307. */
  308. var $FontSizePt;
  309. /**
  310. * @var current font size in user unit
  311. * @access protected
  312. */
  313. var $FontSize;
  314. /**
  315. * @var commands for drawing color
  316. * @access protected
  317. */
  318. var $DrawColor;
  319. /**
  320. * @var commands for filling color
  321. * @access protected
  322. */
  323. var $FillColor;
  324. /**
  325. * @var commands for text color
  326. * @access protected
  327. */
  328. var $TextColor;
  329. /**
  330. * @var indicates whether fill and text colors are different
  331. * @access protected
  332. */
  333. var $ColorFlag;
  334. /**
  335. * @var word spacing
  336. * @access protected
  337. */
  338. var $ws;
  339. /**
  340. * @var automatic page breaking
  341. * @access protected
  342. */
  343. var $AutoPageBreak;
  344. /**
  345. * @var threshold used to trigger page breaks
  346. * @access protected
  347. */
  348. var $PageBreakTrigger;
  349. /**
  350. * @var flag set when processing footer
  351. * @access protected
  352. */
  353. var $InFooter;
  354. /**
  355. * @var zoom display mode
  356. * @access protected
  357. */
  358. var $ZoomMode;
  359. /**
  360. * @var layout display mode
  361. * @access protected
  362. */
  363. var $LayoutMode;
  364. /**
  365. * @var title
  366. * @access protected
  367. */
  368. var $title;
  369. /**
  370. * @var subject
  371. * @access protected
  372. */
  373. var $subject;
  374. /**
  375. * @var author
  376. * @access protected
  377. */
  378. var $author;
  379. /**
  380. * @var keywords
  381. * @access protected
  382. */
  383. var $keywords;
  384. /**
  385. * @var creator
  386. * @access protected
  387. */
  388. var $creator;
  389. /**
  390. * @var alias for total number of pages
  391. * @access protected
  392. */
  393. var $AliasNbPages;
  394. /**
  395. * @var right-bottom corner X coordinate of inserted image
  396. * @since 2002-07-31
  397. * @author Nicola Asuni
  398. * @access protected
  399. */
  400. var $img_rb_x;
  401. /**
  402. * @var right-bottom corner Y coordinate of inserted image
  403. * @since 2002-07-31
  404. * @author Nicola Asuni
  405. * @access protected
  406. */
  407. var $img_rb_y;
  408. /**
  409. * @var image scale factor
  410. * @since 2004-06-14
  411. * @author Nicola Asuni
  412. * @access protected
  413. */
  414. var $imgscale = 1;
  415. /**
  416. * @var boolean set to true when the input text is unicode (require unicode fonts)
  417. * @since 2005-01-02
  418. * @author Nicola Asuni
  419. * @access protected
  420. */
  421. var $isunicode = false;
  422. /**
  423. * @var PDF version
  424. * @since 1.5.3
  425. * @access protected
  426. */
  427. var $PDFVersion = "1.5";
  428. // ----------------------
  429. /**
  430. * @var Minimum distance between header and top page margin.
  431. * @access private
  432. */
  433. var $header_margin;
  434. /**
  435. * @var Minimum distance between footer and bottom page margin.
  436. * @access private
  437. */
  438. var $footer_margin;
  439. /**
  440. * @var original left margin value
  441. * @access private
  442. * @since 1.53.0.TC013
  443. */
  444. var $original_lMargin;
  445. /**
  446. * @var original right margin value
  447. * @access private
  448. * @since 1.53.0.TC013
  449. */
  450. var $original_rMargin;
  451. /**
  452. * @var Header font.
  453. * @access private
  454. */
  455. var $header_font;
  456. /**
  457. * @var Footer font.
  458. * @access private
  459. */
  460. var $footer_font;
  461. /**
  462. * @var Language templates.
  463. * @access private
  464. */
  465. var $l;
  466. /**
  467. * @var Barcode to print on page footer (only if set).
  468. * @access private
  469. */
  470. var $barcode = false;
  471. /**
  472. * @var If true prints header
  473. * @access private
  474. */
  475. var $print_header = true;
  476. /**
  477. * @var If true prints footer.
  478. * @access private
  479. */
  480. var $print_footer = true;
  481. /**
  482. * @var Header width (0 = full page width).
  483. * @access private
  484. */
  485. var $header_width = 0;
  486. /**
  487. * @var Header image logo.
  488. * @access private
  489. */
  490. var $header_logo = "";
  491. /**
  492. * @var Header image logo width in mm.
  493. * @access private
  494. */
  495. var $header_logo_width = 30;
  496. /**
  497. * @var String to print as title on document header.
  498. * @access private
  499. */
  500. var $header_title = "";
  501. /**
  502. * @var String to print on document header.
  503. * @access private
  504. */
  505. var $header_string = "";
  506. /**
  507. * @var Default number of columns for html table.
  508. * @access private
  509. */
  510. var $default_table_columns = 4;
  511. // variables for html parser
  512. /**
  513. * @var HTML PARSER: store current link.
  514. * @access private
  515. */
  516. var $HREF;
  517. /**
  518. * @var HTML PARSER: store font list.
  519. * @access private
  520. */
  521. var $fontList;
  522. /**
  523. * @var HTML PARSER: true when font attribute is set.
  524. * @access private
  525. */
  526. var $issetfont;
  527. /**
  528. * @var HTML PARSER: true when color attribute is set.
  529. * @access private
  530. */
  531. var $issetcolor;
  532. /**
  533. * @var HTML PARSER: true in case of ordered list (OL), false otherwise.
  534. * @access private
  535. */
  536. var $listordered = false;
  537. /**
  538. * @var HTML PARSER: count list items.
  539. * @access private
  540. */
  541. var $listcount = 0;
  542. /**
  543. * @var HTML PARSER: size of table border.
  544. * @access private
  545. */
  546. var $tableborder = 0;
  547. /**
  548. * @var HTML PARSER: true at the beginning of table.
  549. * @access private
  550. */
  551. var $tdbegin = false;
  552. /**
  553. * @var HTML PARSER: table width.
  554. * @access private
  555. */
  556. var $tdwidth = 0;
  557. /**
  558. * @var HTML PARSER: table height.
  559. * @access private
  560. */
  561. var $tdheight = 0;
  562. /**
  563. * @var HTML PARSER: table align.
  564. * @access private
  565. */
  566. var $tdalign = "L";
  567. /**
  568. * @var HTML PARSER: table background color.
  569. * @access private
  570. */
  571. var $tdbgcolor = false;
  572. /**
  573. * @var Store temporary font size in points.
  574. * @access private
  575. */
  576. var $tempfontsize = 10;
  577. /**
  578. * @var Bold font style status.
  579. * @access private
  580. */
  581. var $b;
  582. /**
  583. * @var Underlined font style status.
  584. * @access private
  585. */
  586. var $u;
  587. /**
  588. * @var Italic font style status.
  589. * @access private
  590. */
  591. var $i;
  592. /**
  593. * @var spacer for LI tags.
  594. * @access private
  595. */
  596. var $lispacer = "";
  597. /**
  598. * @var default encoding
  599. * @access private
  600. * @since 1.53.0.TC010
  601. */
  602. var $encoding = "UTF-8";
  603. /**
  604. * @var PHP internal encoding
  605. * @access private
  606. * @since 1.53.0.TC016
  607. */
  608. var $internal_encoding;
  609. /**
  610. * @var store previous fill color as RGB array
  611. * @access private
  612. * @since 1.53.0.TC017
  613. */
  614. var $prevFillColor = array(255,255,255);
  615. /**
  616. * @var store previous text color as RGB array
  617. * @access private
  618. * @since 1.53.0.TC017
  619. */
  620. var $prevTextColor = array(0,0,0);
  621. /**
  622. * @var store previous font family
  623. * @access private
  624. * @since 1.53.0.TC017
  625. */
  626. var $prevFontFamily;
  627. /**
  628. * @var store previous font style
  629. * @access private
  630. * @since 1.53.0.TC017
  631. */
  632. var $prevFontStyle;
  633. /**
  634. * @var indicates if the document language is Right-To-Left
  635. * @access private
  636. * @since 2.0.000
  637. */
  638. var $rtl = false;
  639. /**
  640. * @var used to force RTL or LTR string inversion
  641. * @access private
  642. * @since 2.0.000
  643. */
  644. var $tmprtl = false;
  645. // --- Variables used for document encryption:
  646. /**
  647. * Indicates whether document is protected
  648. * @access private
  649. * @since 2.0.000 (2008-01-02)
  650. */
  651. var $encrypted;
  652. /**
  653. * U entry in pdf document
  654. * @access private
  655. * @since 2.0.000 (2008-01-02)
  656. */
  657. var $Uvalue;
  658. /**
  659. * O entry in pdf document
  660. * @access private
  661. * @since 2.0.000 (2008-01-02)
  662. */
  663. var $Ovalue;
  664. /**
  665. * P entry in pdf document
  666. * @access private
  667. * @since 2.0.000 (2008-01-02)
  668. */
  669. var $Pvalue;
  670. /**
  671. * encryption object id
  672. * @access private
  673. * @since 2.0.000 (2008-01-02)
  674. */
  675. var $enc_obj_id;
  676. /**
  677. * last RC4 key encrypted (cached for optimisation)
  678. * @access private
  679. * @since 2.0.000 (2008-01-02)
  680. */
  681. var $last_rc4_key;
  682. /**
  683. * last RC4 computed key
  684. * @access private
  685. * @since 2.0.000 (2008-01-02)
  686. */
  687. var $last_rc4_key_c;
  688. // --- bookmark ---
  689. /**
  690. * Outlines for bookmark
  691. * @access private
  692. * @since 2.1.002 (2008-02-12)
  693. */
  694. var $outlines = array();
  695. /**
  696. * Outline root for bookmark
  697. * @access private
  698. * @since 2.1.002 (2008-02-12)
  699. */
  700. var $OutlineRoot;
  701. // --- javascript and form ---
  702. /**
  703. * javascript code
  704. * @access private
  705. * @since 2.1.002 (2008-02-12)
  706. */
  707. var $javascript = "";
  708. /**
  709. * javascript counter
  710. * @access private
  711. * @since 2.1.002 (2008-02-12)
  712. */
  713. var $n_js;
  714. //------------------------------------------------------------
  715. // Public methods
  716. //------------------------------------------------------------
  717. /**
  718. * This is the class constructor.
  719. * It allows to set up the page format, the orientation and
  720. * the measure unit used in all the methods (except for the font sizes).
  721. * @since 1.0
  722. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  723. * @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.
  724. * @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>
  725. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  726. * @param String $encoding charset encoding; default is UTF-8
  727. */
  728. function TCPDF($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding="UTF-8") {
  729. /* Set internal character encoding to ASCII */
  730. if (function_exists("mb_internal_encoding") AND mb_internal_encoding()) {
  731. $this->internal_encoding = mb_internal_encoding();
  732. mb_internal_encoding("ASCII");
  733. }
  734. // set language direction
  735. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  736. $this->tmprtl = false;
  737. //Some checks
  738. $this->_dochecks();
  739. //Initialization of properties
  740. $this->isunicode=$unicode;
  741. $this->page=0;
  742. $this->n=2;
  743. $this->buffer='';
  744. $this->pages=array();
  745. $this->OrientationChanges=array();
  746. $this->state=0;
  747. $this->fonts=array();
  748. $this->FontFiles=array();
  749. $this->diffs=array();
  750. $this->images=array();
  751. $this->links=array();
  752. $this->InFooter=false;
  753. $this->lasth=0;
  754. $this->FontFamily='';
  755. $this->FontStyle='';
  756. $this->FontSizePt=12;
  757. $this->underline=false;
  758. $this->DrawColor='0 G';
  759. $this->FillColor='0 g';
  760. $this->TextColor='0 g';
  761. $this->ColorFlag=false;
  762. $this->ws=0;
  763. // encryption values
  764. $this->encrypted=false;
  765. $this->last_rc4_key='';
  766. $this->padding="\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  767. //Standard Unicode fonts
  768. $this->CoreFonts=array(
  769. 'courier'=>'Courier',
  770. 'courierB'=>'Courier-Bold',
  771. 'courierI'=>'Courier-Oblique',
  772. 'courierBI'=>'Courier-BoldOblique',
  773. 'helvetica'=>'Helvetica',
  774. 'helveticaB'=>'Helvetica-Bold',
  775. 'helveticaI'=>'Helvetica-Oblique',
  776. 'helveticaBI'=>'Helvetica-BoldOblique',
  777. 'times'=>'Times-Roman',
  778. 'timesB'=>'Times-Bold',
  779. 'timesI'=>'Times-Italic',
  780. 'timesBI'=>'Times-BoldItalic',
  781. 'symbol'=>'Symbol',
  782. 'zapfdingbats'=>'ZapfDingbats'
  783. );
  784. //Scale factor
  785. switch (strtolower($unit)){
  786. case 'pt': {$this->k=1; break;}
  787. case 'mm': {$this->k=72/25.4; break;}
  788. case 'cm': {$this->k=72/2.54; break;}
  789. case 'in': {$this->k=72; break;}
  790. default : {$this->Error('Incorrect unit: '.$unit); break;}
  791. }
  792. //Page format
  793. if(is_string($format)) {
  794. // Page formats (45 standard ISO paper formats and 4 american common formats).
  795. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  796. switch (strtoupper($format)){
  797. case '4A0': {$format = array(4767.87,6740.79); break;}
  798. case '2A0': {$format = array(3370.39,4767.87); break;}
  799. case 'A0': {$format = array(2383.94,3370.39); break;}
  800. case 'A1': {$format = array(1683.78,2383.94); break;}
  801. case 'A2': {$format = array(1190.55,1683.78); break;}
  802. case 'A3': {$format = array(841.89,1190.55); break;}
  803. case 'A4': default: {$format = array(595.28,841.89); break;}
  804. case 'A5': {$format = array(419.53,595.28); break;}
  805. case 'A6': {$format = array(297.64,419.53); break;}
  806. case 'A7': {$format = array(209.76,297.64); break;}
  807. case 'A8': {$format = array(147.40,209.76); break;}
  808. case 'A9': {$format = array(104.88,147.40); break;}
  809. case 'A10': {$format = array(73.70,104.88); break;}
  810. case 'B0': {$format = array(2834.65,4008.19); break;}
  811. case 'B1': {$format = array(2004.09,2834.65); break;}
  812. case 'B2': {$format = array(1417.32,2004.09); break;}
  813. case 'B3': {$format = array(1000.63,1417.32); break;}
  814. case 'B4': {$format = array(708.66,1000.63); break;}
  815. case 'B5': {$format = array(498.90,708.66); break;}
  816. case 'B6': {$format = array(354.33,498.90); break;}
  817. case 'B7': {$format = array(249.45,354.33); break;}
  818. case 'B8': {$format = array(175.75,249.45); break;}
  819. case 'B9': {$format = array(124.72,175.75); break;}
  820. case 'B10': {$format = array(87.87,124.72); break;}
  821. case 'C0': {$format = array(2599.37,3676.54); break;}
  822. case 'C1': {$format = array(1836.85,2599.37); break;}
  823. case 'C2': {$format = array(1298.27,1836.85); break;}
  824. case 'C3': {$format = array(918.43,1298.27); break;}
  825. case 'C4': {$format = array(649.13,918.43); break;}
  826. case 'C5': {$format = array(459.21,649.13); break;}
  827. case 'C6': {$format = array(323.15,459.21); break;}
  828. case 'C7': {$format = array(229.61,323.15); break;}
  829. case 'C8': {$format = array(161.57,229.61); break;}
  830. case 'C9': {$format = array(113.39,161.57); break;}
  831. case 'C10': {$format = array(79.37,113.39); break;}
  832. case 'RA0': {$format = array(2437.80,3458.27); break;}
  833. case 'RA1': {$format = array(1729.13,2437.80); break;}
  834. case 'RA2': {$format = array(1218.90,1729.13); break;}
  835. case 'RA3': {$format = array(864.57,1218.90); break;}
  836. case 'RA4': {$format = array(609.45,864.57); break;}
  837. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  838. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  839. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  840. case 'SRA3': {$format = array(907.09,1275.59); break;}
  841. case 'SRA4': {$format = array(637.80,907.09); break;}
  842. case 'LETTER': {$format = array(612.00,792.00); break;}
  843. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  844. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  845. case 'FOLIO': {$format = array(612.00,936.00); break;}
  846. // default: {$this->Error('Unknown page format: '.$format); break;}
  847. }
  848. $this->fwPt=$format[0];
  849. $this->fhPt=$format[1];
  850. }
  851. else {
  852. $this->fwPt=$format[0]*$this->k;
  853. $this->fhPt=$format[1]*$this->k;
  854. }
  855. $this->fw=$this->fwPt/$this->k;
  856. $this->fh=$this->fhPt/$this->k;
  857. //Page orientation
  858. $orientation=strtolower($orientation);
  859. if($orientation=='p' or $orientation=='portrait') {
  860. $this->DefOrientation='P';
  861. $this->wPt=$this->fwPt;
  862. $this->hPt=$this->fhPt;
  863. }
  864. elseif($orientation=='l' or $orientation=='landscape') {
  865. $this->DefOrientation='L';
  866. $this->wPt=$this->fhPt;
  867. $this->hPt=$this->fwPt;
  868. }
  869. else {
  870. $this->Error('Incorrect orientation: '.$orientation);
  871. }
  872. $this->CurOrientation=$this->DefOrientation;
  873. $this->w=$this->wPt/$this->k;
  874. $this->h=$this->hPt/$this->k;
  875. //Page margins (1 cm)
  876. $margin=28.35/$this->k;
  877. $this->SetMargins($margin,$margin);
  878. //Interior cell margin (1 mm)
  879. $this->cMargin=$margin/10;
  880. //Line width (0.2 mm)
  881. $this->LineWidth=.567/$this->k;
  882. //Automatic page break
  883. $this->SetAutoPageBreak(true,2*$margin);
  884. //Full width display mode
  885. $this->SetDisplayMode('fullwidth');
  886. //Compression
  887. $this->SetCompression(true);
  888. //Set default PDF version number
  889. $this->PDFVersion = "1.5";
  890. $this->encoding = $encoding;
  891. $this->b = 0;
  892. $this->i = 0;
  893. $this->u = 0;
  894. $this->HREF = '';
  895. $this->fontlist = array("arial", "times", "courier", "helvetica", "symbol");
  896. $this->issetfont = false;
  897. $this->issetcolor = false;
  898. $this->tableborder = 0;
  899. $this->tdbegin = false;
  900. $this->tdwidth= 0;
  901. $this->tdheight = 0;
  902. if($this->rtl) {
  903. $this->tdalign = "R";
  904. } else {
  905. $this->tdalign = "L";
  906. }
  907. $this->tdbgcolor = false;
  908. $this->SetFillColor(200, 200, 200, true);
  909. $this->SetTextColor(0, 0, 0, true);
  910. }
  911. /**
  912. * Enable or disable Right-To-Left language mode
  913. * @param Boolean $enable if true enable Right-To-Left language mode.
  914. * @since 2.0.000 (2008-01-03)
  915. */
  916. function setRTL($enable) {
  917. $this->rtl = $enable ? true : false;
  918. $this->tmprtl = false;
  919. }
  920. /**
  921. * Force temporary RTL language direction
  922. * @param mixed $mode can be false, 'L' for LTR or 'R' for RTL
  923. * @since 2.1.000 (2008-01-09)
  924. */
  925. function setTempRTL($mode) {
  926. switch ($mode) {
  927. case false:
  928. case 'L':
  929. case 'R': {
  930. $this->tmprtl = $mode;
  931. }
  932. }
  933. }
  934. /**
  935. * Set the last cell height.
  936. * @param float $h cell height.
  937. * @author Nicola Asuni
  938. * @since 1.53.0.TC034
  939. */
  940. function setLastH($h) {
  941. $this->lasth=$h;
  942. }
  943. /**
  944. * Set the image scale.
  945. * @param float $scale image scale.
  946. * @author Nicola Asuni
  947. * @since 1.5.2
  948. */
  949. function setImageScale($scale) {
  950. $this->imgscale=$scale;
  951. }
  952. /**
  953. * Returns the image scale.
  954. * @return float image scale.
  955. * @author Nicola Asuni
  956. * @since 1.5.2
  957. */
  958. function getImageScale() {
  959. return $this->imgscale;
  960. }
  961. /**
  962. * Returns the page width in units.
  963. * @return int page width.
  964. * @author Nicola Asuni
  965. * @since 1.5.2
  966. */
  967. function getPageWidth() {
  968. return $this->w;
  969. }
  970. /**
  971. * Returns the page height in units.
  972. * @return int page height.
  973. * @author Nicola Asuni
  974. * @since 1.5.2
  975. */
  976. function getPageHeight() {
  977. return $this->h;
  978. }
  979. /**
  980. * Returns the page break margin.
  981. * @return int page break margin.
  982. * @author Nicola Asuni
  983. * @since 1.5.2
  984. */
  985. function getBreakMargin() {
  986. return $this->bMargin;
  987. }
  988. /**
  989. * Returns the scale factor (number of points in user unit).
  990. * @return int scale factor.
  991. * @author Nicola Asuni
  992. * @since 1.5.2
  993. */
  994. function getScaleFactor() {
  995. return $this->k;
  996. }
  997. /**
  998. * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
  999. * @param float $left Left margin.
  1000. * @param float $top Top margin.
  1001. * @param float $right Right margin. Default value is the left one.
  1002. * @since 1.0
  1003. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  1004. */
  1005. function SetMargins($left, $top, $right=-1) {
  1006. //Set left, top and right margins
  1007. $this->lMargin=$left;
  1008. $this->tMargin=$top;
  1009. if($right==-1) {
  1010. $right=$left;
  1011. }
  1012. $this->rMargin=$right;
  1013. }
  1014. /**
  1015. * 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.
  1016. * @param float $margin The margin.
  1017. * @since 1.4
  1018. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1019. */
  1020. function SetLeftMargin($margin) {
  1021. //Set left margin
  1022. $this->lMargin=$margin;
  1023. if(($this->page > 0) AND ($this->x < $margin)) {
  1024. $this->x = $margin;
  1025. }
  1026. }
  1027. /**
  1028. * Defines the top margin. The method can be called before creating the first page.
  1029. * @param float $margin The margin.
  1030. * @since 1.5
  1031. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1032. */
  1033. function SetTopMargin($margin) {
  1034. //Set top margin
  1035. $this->tMargin=$margin;
  1036. if(($this->page > 0) AND ($this->y < $margin)) {
  1037. $this->y = $margin;
  1038. }
  1039. }
  1040. /**
  1041. * Defines the right margin. The method can be called before creating the first page.
  1042. * @param float $margin The margin.
  1043. * @since 1.5
  1044. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1045. */
  1046. function SetRightMargin($margin) {
  1047. $this->rMargin=$margin;
  1048. if(($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  1049. $this->x = $this->w - $margin;
  1050. }
  1051. }
  1052. /**
  1053. * Set the internal Cell padding.
  1054. * @param float $pad internal padding.
  1055. * @since 2.1.000 (2008-01-09)
  1056. * @see Cell(), SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1057. */
  1058. function SetCellPadding($pad) {
  1059. $this->cMargin=$pad;
  1060. }
  1061. /**
  1062. * 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.
  1063. * @param boolean $auto Boolean indicating if mode should be on or off.
  1064. * @param float $margin Distance from the bottom of the page.
  1065. * @since 1.0
  1066. * @see Cell(), MultiCell(), AcceptPageBreak()
  1067. */
  1068. function SetAutoPageBreak($auto, $margin=0) {
  1069. //Set auto page break mode and triggering margin
  1070. $this->AutoPageBreak = $auto;
  1071. $this->bMargin = $margin;
  1072. $this->PageBreakTrigger = $this->h - $margin;
  1073. }
  1074. /**
  1075. * 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.
  1076. * @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>
  1077. * @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>
  1078. * @since 1.2
  1079. */
  1080. function SetDisplayMode($zoom, $layout='continuous') {
  1081. //Set display mode in viewer
  1082. if($zoom=='fullpage' or $zoom=='fullwidth' or $zoom=='real' or $zoom=='default' or !is_string($zoom)) {
  1083. $this->ZoomMode=$zoom;
  1084. }
  1085. else {
  1086. $this->Error('Incorrect zoom display mode: '.$zoom);
  1087. }
  1088. if($layout=='single' or $layout=='continuous' or $layout=='two' or $layout=='default') {
  1089. $this->LayoutMode=$layout;
  1090. }
  1091. else {
  1092. $this->Error('Incorrect layout display mode: '.$layout);
  1093. }
  1094. }
  1095. /**
  1096. * 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.
  1097. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  1098. * @param boolean $compress Boolean indicating if compression must be enabled.
  1099. * @since 1.4
  1100. */
  1101. function SetCompression($compress) {
  1102. //Set page compression
  1103. if(function_exists('gzcompress')) {
  1104. $this->compress=$compress;
  1105. }
  1106. else {
  1107. $this->compress=false;
  1108. }
  1109. }
  1110. /**
  1111. * Defines the title of the document.
  1112. * @param string $title The title.
  1113. * @since 1.2
  1114. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  1115. */
  1116. function SetTitle($title) {
  1117. //Title of document
  1118. $this->title=$title;
  1119. }
  1120. /**
  1121. * Defines the subject of the document.
  1122. * @param string $subject The subject.
  1123. * @since 1.2
  1124. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  1125. */
  1126. function SetSubject($subject) {
  1127. //Subject of document
  1128. $this->subject=$subject;
  1129. }
  1130. /**
  1131. * Defines the author of the document.
  1132. * @param string $author The name of the author.
  1133. * @since 1.2
  1134. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  1135. */
  1136. function SetAuthor($author) {
  1137. //Author of document
  1138. $this->author=$author;
  1139. }
  1140. /**
  1141. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  1142. * @param string $keywords The list of keywords.
  1143. * @since 1.2
  1144. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  1145. */
  1146. function SetKeywords($keywords) {
  1147. //Keywords of document
  1148. $this->keywords=$keywords;
  1149. }
  1150. /**
  1151. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  1152. * @param string $creator The name of the creator.
  1153. * @since 1.2
  1154. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  1155. */
  1156. function SetCreator($creator) {
  1157. //Creator of document
  1158. $this->creator=$creator;
  1159. }
  1160. /**
  1161. * Defines an alias for the total number of pages. It will be substituted as the document is closed.<br />
  1162. * <b>Example:</b><br />
  1163. * <pre>
  1164. * class PDF extends TCPDF {
  1165. * function Footer() {
  1166. * //Go to 1.5 cm from bottom
  1167. * $this->SetY(-15);
  1168. * //Select Arial italic 8
  1169. * $this->SetFont('vera','I',8);
  1170. * //Print current and total page numbers
  1171. * $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  1172. * }
  1173. * }
  1174. * $pdf=new PDF();
  1175. * $pdf->AliasNbPages();
  1176. * </pre>
  1177. * @param string $alias The alias. Default value: {nb}.
  1178. * @since 1.4
  1179. * @see PageNo(), Footer()
  1180. */
  1181. function AliasNbPages($alias='{nb}') {
  1182. //Define an alias for total number of pages
  1183. $this->AliasNbPages = $this->_escapetext($alias);
  1184. }
  1185. /**
  1186. * 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.
  1187. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  1188. * @param string $msg The error message
  1189. * @since 1.0
  1190. */
  1191. function Error($msg) {
  1192. //Fatal error
  1193. die('<strong>TCPDF error: </strong>'.$msg);
  1194. }
  1195. /**
  1196. * This method begins the generation of the PDF document. It is not necessary to call it explicitly because AddPage() does it automatically.
  1197. * Note: no page is created by this method
  1198. * @since 1.0
  1199. * @see AddPage(), Close()
  1200. */
  1201. function Open() {
  1202. //Begin document
  1203. $this->state=1;
  1204. }
  1205. /**
  1206. * 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.
  1207. * @since 1.0
  1208. * @see Open(), Output()
  1209. */
  1210. function Close() {
  1211. //Terminate document
  1212. if($this->state==3) {
  1213. return;
  1214. }
  1215. if($this->page==0) {
  1216. $this->AddPage();
  1217. }
  1218. //Page footer
  1219. $this->InFooter=true;
  1220. $this->Footer();
  1221. $this->InFooter=false;
  1222. //Close page
  1223. $this->_endpage();
  1224. //Close document
  1225. $this->_enddoc();
  1226. }
  1227. /**
  1228. * Reset pointer to the last document page.
  1229. * @since 2.0.000 (2008-01-04)
  1230. * @see setPage(), getPage(), getNumPages()
  1231. */
  1232. function lastPage() {
  1233. $this->page = count($this->pages);
  1234. }
  1235. /**
  1236. * Move pointer to the apecified document page.
  1237. * @param int $pnum page number
  1238. * @since 2.1.000 (2008-01-07)
  1239. * @see getPage(), lastpage(), getNumPages()
  1240. */
  1241. function setPage($pnum) {
  1242. if(($pnum > 0) AND ($pnum <= count($this->pages))) {
  1243. $this->page = $pnum;
  1244. }
  1245. }
  1246. /**
  1247. * Get current document page number.
  1248. * @return int page number
  1249. * @since 2.1.000 (2008-01-07)
  1250. * @see setPage(), lastpage(), getNumPages()
  1251. */
  1252. function getPage() {
  1253. return $this->page;
  1254. }
  1255. /**
  1256. * Get the total number of insered pages.
  1257. * @return int number of pages
  1258. * @since 2.1.000 (2008-01-07)
  1259. * @see setPage(), getPage(), lastpage()
  1260. */
  1261. function getNumPages() {
  1262. return count($this->pages);
  1263. }
  1264. /**
  1265. * 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.
  1266. * 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.
  1267. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  1268. * @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.
  1269. * @since 1.0
  1270. * @see TCPDF(), Header(), Footer(), SetMargins()
  1271. */
  1272. function AddPage($orientation='') {
  1273. if (count($this->pages) > $this->page) {
  1274. // this page has been already added
  1275. $this->page++;
  1276. $this->y = $this->tMargin;
  1277. return;
  1278. }
  1279. //Start a new page
  1280. if($this->state==0) {
  1281. $this->Open();
  1282. }
  1283. $family=$this->FontFamily;
  1284. $style=$this->FontStyle.($this->underline ? 'U' : '');
  1285. $size=$this->FontSizePt;
  1286. $lw=$this->LineWidth;
  1287. $dc=$this->DrawColor;
  1288. $fc=$this->FillColor;
  1289. $tc=$this->TextColor;
  1290. $cf=$this->ColorFlag;
  1291. if($this->page>0) {
  1292. //Page footer
  1293. $this->InFooter=true;
  1294. $this->Footer();
  1295. $this->InFooter=false;
  1296. //Close page
  1297. $this->_endpage();
  1298. }
  1299. //Start new page
  1300. $this->_beginpage($orientation);
  1301. //Set line cap style to square
  1302. $this->_out('2 J');
  1303. //Set line width
  1304. $this->LineWidth=$lw;
  1305. $this->_out(sprintf('%.2f w',$lw*$this->k));
  1306. //Set font
  1307. if($family) {
  1308. $this->SetFont($family,$style,$size);
  1309. }
  1310. //Set colors
  1311. $this->DrawColor=$dc;
  1312. if($dc!='0 G') {
  1313. $this->_out($dc);
  1314. }
  1315. $this->FillColor=$fc;
  1316. if($fc!='0 g') {
  1317. $this->_out($fc);
  1318. }
  1319. $this->TextColor=$tc;
  1320. $this->ColorFlag=$cf;
  1321. //Page header
  1322. $this->Header();
  1323. //Restore line width
  1324. if($this->LineWidth!=$lw) {
  1325. $this->LineWidth=$lw;
  1326. $this->_out(sprintf('%.2f w',$lw*$this->k));
  1327. }
  1328. //Restore font
  1329. if($family) {
  1330. $this->SetFont($family,$style,$size);
  1331. }
  1332. //Restore colors
  1333. if($this->DrawColor!=$dc) {
  1334. $this->DrawColor=$dc;
  1335. $this->_out($dc);
  1336. }
  1337. if($this->FillColor!=$fc) {
  1338. $this->FillColor=$fc;
  1339. $this->_out($fc);
  1340. }
  1341. $this->TextColor=$tc;
  1342. $this->ColorFlag=$cf;
  1343. }
  1344. /**
  1345. * Set header data.
  1346. * @param string $ln header image logo
  1347. * @param string $lw header image logo width in mm
  1348. * @param string $ht string to print as title on document header
  1349. * @param string $hs string to print on document header
  1350. */
  1351. function setHeaderData($ln="", $lw=0, $ht="", $hs="") {
  1352. $this->header_logo = $ln;
  1353. $this->header_logo_width = $lw;
  1354. $this->header_title = $ht;
  1355. $this->header_string = $hs;
  1356. }
  1357. /**
  1358. * Set header margin.
  1359. * (minimum distance between header and top page margin)
  1360. * @param int $hm distance in millimeters
  1361. */
  1362. function setHeaderMargin($hm=10) {
  1363. $this->header_margin = $hm;
  1364. }
  1365. /**
  1366. * Set footer margin.
  1367. * (minimum distance between footer and bottom page margin)
  1368. * @param int $fm distance in millimeters
  1369. */
  1370. function setFooterMargin($fm=10) {
  1371. $this->footer_margin = $fm;
  1372. }
  1373. /**
  1374. * Set a flag to print page header.
  1375. * @param boolean $val set to true to print the page header (default), false otherwise.
  1376. */
  1377. function setPrintHeader($val=true) {
  1378. $this->print_header = $val;
  1379. }
  1380. /**
  1381. * Set a flag to print page footer.
  1382. * @param boolean $value set to true to print the page footer (default), false otherwise.
  1383. */
  1384. function setPrintFooter($val=true) {
  1385. $this->print_footer = $val;
  1386. }
  1387. /**
  1388. * This method is used to render the page header.
  1389. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  1390. */
  1391. function Header() {
  1392. if ($this->print_header) {
  1393. if (!isset($this->original_lMargin)) {
  1394. $this->original_lMargin = $this->lMargin;
  1395. }
  1396. if (!isset($this->original_rMargin)) {
  1397. $this->original_rMargin = $this->rMargin;
  1398. }
  1399. // reset original header margins
  1400. $this->rMargin = $this->original_rMargin;
  1401. $this->lMargin = $this->original_lMargin;
  1402. // save current font values
  1403. $font_family = $this->FontFamily;
  1404. $font_style = $this->FontStyle;
  1405. $font_size = $this->FontSizePt;
  1406. //set current position
  1407. if ($this->rtl) {
  1408. $this->SetXY($this->original_rMargin, $this->header_margin);
  1409. } else {
  1410. $this->SetXY($this->original_lMargin, $this->header_margin);
  1411. }
  1412. if (($this->header_logo) AND ($this->header_logo != K_BLANK_IMAGE)) {
  1413. $this->Image(K_PATH_IMAGES.$this->header_logo, $this->GetX(), $this->header_margin, $this->header_logo_width);
  1414. } else {
  1415. $this->img_rb_x = $this->GetX();
  1416. $this->img_rb_y = $this->GetY();
  1417. }
  1418. $cell_height = round((K_CELL_HEIGHT_RATIO * $this->header_font[2]) / $this->k, 2);
  1419. // set starting margin for text data cell
  1420. if ($this->rtl) {
  1421. $header_x = $this->original_rMargin + ($this->header_logo_width * 1.1);
  1422. } else {
  1423. $header_x = $this->original_lMargin + ($this->header_logo_width * 1.1);
  1424. }
  1425. // header title
  1426. $this->SetFont($this->header_font[0], 'B', $this->header_font[2] + 1);
  1427. $this->SetX($header_x);
  1428. $this->Cell($this->header_width, $cell_height, $this->header_title, 0, 1, '');
  1429. // header string
  1430. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  1431. $this->SetX($header_x);
  1432. $this->MultiCell($this->header_width, $cell_height, $this->header_string, 0, '', 0, 1, 0, 0, true, 0);
  1433. // print an ending header line
  1434. //set style for cell border
  1435. $prevlinewidth = $this->GetLineWidth();
  1436. $line_width = 0.3;
  1437. $this->SetLineWidth($line_width);
  1438. $this->SetDrawColor(0, 0, 0);
  1439. $this->SetY(1 + max($this->img_rb_y, $this->GetY()));
  1440. if ($this->rtl) {
  1441. $this->SetX($this->original_rMargin);
  1442. } else {
  1443. $this->SetX($this->original_lMargin);
  1444. }
  1445. $this->Cell(0, 0, '', 'T', 0, 'C');
  1446. $this->SetLineWidth($prevlinewidth);
  1447. //restore position
  1448. if ($this->rtl) {
  1449. $this->SetXY($this->original_rMargin, $this->tMargin);
  1450. } else {
  1451. $this->SetXY($this->original_lMargin, $this->tMargin);
  1452. }
  1453. // restore font values
  1454. $this->SetFont($font_family, $font_style, $font_size);
  1455. }
  1456. }
  1457. /**
  1458. * This method is used to render the page footer.
  1459. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  1460. */
  1461. function Footer() {
  1462. if ($this->print_footer) {
  1463. if (!isset($this->original_lMargin)) {
  1464. $this->original_lMargin = $this->lMargin;
  1465. }
  1466. if (!isset($this->original_rMargin)) {
  1467. $this->original_rMargin = $this->rMargin;
  1468. }
  1469. // reset original header margins
  1470. $this->rMargin = $this->original_rMargin;
  1471. $this->lMargin = $this->original_lMargin;
  1472. // save current font values
  1473. $font_family = $this->FontFamily;
  1474. $font_style = $this->FontStyle;
  1475. $font_size = $this->FontSizePt;
  1476. //set font
  1477. $this->SetFont($this->footer_font[0], $this->footer_font[1] , $this->footer_font[2]);
  1478. //set style for cell border
  1479. $prevlinewidth = $this->GetLineWidth();
  1480. $line_width = 0.3;
  1481. $this->SetLineWidth($line_width);
  1482. $this->SetDrawColor(0, 0, 0);
  1483. $footer_height = round((K_CELL_HEIGHT_RATIO * $this->footer_font[2]) / $this->k, 2); //footer height
  1484. //get footer y position
  1485. $footer_y = $this->h - $this->footer_margin - $footer_height;
  1486. //set current position
  1487. if ($this->rtl) {
  1488. $this->SetXY($this->original_rMargin, $footer_y);
  1489. } else {
  1490. $this->SetXY($this->original_lMargin, $footer_y);
  1491. }
  1492. //print document barcode
  1493. if ($this->barcode) {
  1494. $this->Ln();
  1495. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin)/3); //max width
  1496. $this->writeBarcode($this->GetX(), $footer_y + $line_width, $barcode_width, $footer_height - $line_width, "C128B", false, false, 2, $this->barcode);
  1497. }
  1498. $pagenumtxt = $this->l['w_page']." ".$this->PageNo().' / {nb}';
  1499. $this->SetY($footer_y);
  1500. //Print page number
  1501. if ($this->rtl) {
  1502. $this->SetX($this->original_rMargin);
  1503. $this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'L');
  1504. } else {
  1505. $this->SetX($this->original_lMargin);
  1506. $this->Cell(0, $footer_height, $pagenumtxt, 'T', 0, 'R');
  1507. }
  1508. // restore line width
  1509. $this->SetLineWidth($prevlinewidth);
  1510. // restore font values
  1511. $this->SetFont($font_family, $font_style, $font_size);
  1512. }
  1513. }
  1514. /**
  1515. * Returns the current page number.
  1516. * @return int page number
  1517. * @since 1.0
  1518. * @see AliasNbPages()
  1519. */
  1520. function PageNo() {
  1521. //Get current page number
  1522. return $this->page;
  1523. }
  1524. /**
  1525. * 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.
  1526. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1527. * @param int $g Green component (between 0 and 255)
  1528. * @param int $b Blue component (between 0 and 255)
  1529. * @since 1.3
  1530. * @see SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  1531. */
  1532. function SetDrawColor($r, $g=-1, $b=-1) {
  1533. //Set color for all stroking operations
  1534. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1535. $this->DrawColor=sprintf('%.3f G',$r/255);
  1536. }
  1537. else {
  1538. $this->DrawColor=sprintf('%.3f %.3f %.3f RG',$r/255,$g/255,$b/255);
  1539. }
  1540. if($this->page>0) {
  1541. $this->_out($this->DrawColor);
  1542. }
  1543. }
  1544. /**
  1545. * 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.
  1546. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1547. * @param int $g Green component (between 0 and 255)
  1548. * @param int $b Blue component (between 0 and 255)
  1549. * @param boolean $storeprev if true stores the RGB array on $prevFillColor variable.
  1550. * @since 1.3
  1551. * @see SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  1552. */
  1553. function SetFillColor($r, $g=-1, $b=-1, $storeprev=false) {
  1554. //Set color for all filling operations
  1555. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1556. $this->FillColor=sprintf('%.3f g',$r/255);
  1557. }
  1558. else {
  1559. $this->FillColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  1560. }
  1561. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  1562. if($this->page>0) {
  1563. $this->_out($this->FillColor);
  1564. }
  1565. if ($storeprev) {
  1566. // store color as previous value
  1567. $this->prevFillColor = array($r, $g, $b);
  1568. }
  1569. }
  1570. /**
  1571. * 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.
  1572. * @param int $r If g et b are given, red component; if not, indicates the gray level. Value between 0 and 255
  1573. * @param int $g Green component (between 0 and 255)
  1574. * @param int $b Blue component (between 0 and 255)
  1575. * @param boolean $storeprev if true stores the RGB array on $prevTextColor variable.
  1576. * @since 1.3
  1577. * @see SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  1578. */
  1579. function SetTextColor($r, $g=-1, $b=-1, $storeprev=false) {
  1580. //Set color for text
  1581. if(($r==0 and $g==0 and $b==0) or $g==-1) {
  1582. $this->TextColor=sprintf('%.3f g',$r/255);
  1583. }
  1584. else {
  1585. $this->TextColor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
  1586. }
  1587. $this->ColorFlag=($this->FillColor!=$this->TextColor);
  1588. if ($storeprev) {
  1589. // store color as previous value
  1590. $this->prevTextColor = array($r, $g, $b);
  1591. }
  1592. }
  1593. /**
  1594. * Returns the length of a string in user unit. A font must be selected.<br>
  1595. * @param string $s The string whose length is to be computed
  1596. * @return int string length
  1597. * @author Nicola Asuni
  1598. * @since 1.2
  1599. */
  1600. function GetStringWidth($s) {
  1601. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $this->tmprtl));
  1602. }
  1603. /**
  1604. * Returns the string length of an array of chars in user unit. A font must be selected.<br>
  1605. * @param string $arr The array of chars whose total length is to be computed
  1606. * @return int string length
  1607. * @author Nicola Asuni
  1608. * @since 2.4.000 (2008-03-06)
  1609. */
  1610. function GetArrStringWidth($sa) {
  1611. $w = 0;
  1612. foreach($sa as $char) {
  1613. $w += $this->GetCharWidth($char);
  1614. }
  1615. return $w;
  1616. }
  1617. /**
  1618. * Returns the length of the char in user unit. A font must be selected.<br>
  1619. * @param string $char The char whose length is to be returned
  1620. * @return int char width
  1621. * @author Nicola Asuni
  1622. * @since 2.4.000 (2008-03-06)
  1623. */
  1624. function GetCharWidth($char) {
  1625. $cw = &$this->CurrentFont['cw'];
  1626. if (isset($cw[$char])) {
  1627. $w = $cw[$char];
  1628. } elseif(isset($cw[ord($char)])) {
  1629. $w = $cw[ord($char)];
  1630. } elseif(isset($cw[chr($char)])) {
  1631. $w = $cw[chr($char)];
  1632. } elseif(isset($this->CurrentFont['desc']['MissingWidth'])) {
  1633. $w = $this->CurrentFont['desc']['MissingWidth']; // set default size
  1634. } else {
  1635. $w = 500;
  1636. }
  1637. return ($w * $this->FontSize / 1000);
  1638. }
  1639. /**
  1640. * Returns the numbero of characters in a string.
  1641. * @param string $s The input string.
  1642. * @return int number of characters
  1643. * @since 2.0.0001 (2008-01-07)
  1644. */
  1645. function GetNumChars($s) {
  1646. if($this->isunicode) {
  1647. return count($this->UTF8StringToArray($s));
  1648. }
  1649. return strlen($s);
  1650. }
  1651. /**
  1652. * 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 K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
  1653. * Support UTF-8 Unicode [Nicola Asuni, 2005-01-02].
  1654. * <b>Example</b>:<br />
  1655. * <pre>
  1656. * $pdf->AddFont('Comic','I');
  1657. * // is equivalent to:
  1658. * $pdf->AddFont('Comic','I','comici.php');
  1659. * </pre>
  1660. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  1661. * @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>
  1662. * @param string $file The font definition file. By default, the name is built from the family and style, in lower case with no space.
  1663. * @since 1.5
  1664. * @see SetFont()
  1665. */
  1666. function AddFont($family, $style='', $file='') {
  1667. if(empty($family)) {
  1668. return;
  1669. }
  1670. //Add a TrueType or Type1 font
  1671. $family = strtolower($family);
  1672. if((!$this->isunicode) AND ($family == 'arial')) {
  1673. $family = 'helvetica';
  1674. }
  1675. $style=strtoupper($style);
  1676. $style=str_replace('U','',$style);
  1677. if($style == 'IB') {
  1678. $style = 'BI';
  1679. }
  1680. $fontkey = $family.$style;
  1681. // check if the font has been already added
  1682. if(isset($this->fonts[$fontkey])) {
  1683. return;
  1684. }
  1685. if($file=='') {
  1686. $file = str_replace(' ', '', $family).strtolower($style).'.php';
  1687. }
  1688. if(!file_exists($this->_getfontpath().$file)) {
  1689. // try to load the basic file without styles
  1690. $file = str_replace(' ', '', $family).'.php';
  1691. }
  1692. include($this->_getfontpath().$file);
  1693. if(!isset($name) AND !isset($fpdf_charwidths)) {
  1694. $this->Error('Could not include font definition file');
  1695. }
  1696. $i = count($this->fonts)+1;
  1697. if($this->isunicode) {
  1698. $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
  1699. $fpdf_charwidths[$fontkey] = $cw;
  1700. } else {
  1701. $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
  1702. }
  1703. if(isset($diff) AND (!empty($diff))) {
  1704. //Search existing encodings
  1705. $d=0;
  1706. $nb=count($this->diffs);
  1707. for($i=1;$i<=$nb;$i++) {
  1708. if($this->diffs[$i]==$diff) {
  1709. $d=$i;
  1710. break;
  1711. }
  1712. }
  1713. if($d==0) {
  1714. $d=$nb+1;
  1715. $this->diffs[$d]=$diff;
  1716. }
  1717. $this->fonts[$fontkey]['diff']=$d;
  1718. }
  1719. if(!empty($file)) {
  1720. if((strcasecmp($type,"TrueType") == 0) OR (strcasecmp($type,"TrueTypeUnicode") == 0)) {
  1721. $this->FontFiles[$file]=array('length1'=>$originalsize);
  1722. }
  1723. else {
  1724. $this->FontFiles[$file]=array('length1'=>$size1,'length2'=>$size2);
  1725. }
  1726. }
  1727. }
  1728. /**
  1729. * 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.
  1730. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  1731. * The method can be called before the first page is created and the font is retained from page to page.
  1732. If you just wish to change the current font size, it is simpler to call SetFontSize().
  1733. * Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:<ul><li>They are in the current directory (the one where the running script lies)</li><li>They are in one of the directories defined by the include_path parameter</li><li>They are in the directory defined by the K_PATH_FONTS constant</li></ul><br />
  1734. * Example for the last case (note the trailing slash):<br />
  1735. * <pre>
  1736. * define('K_PATH_FONTS','/home/www/font/');
  1737. * require('tcpdf.php');
  1738. *
  1739. * //Times regular 12
  1740. * $pdf->SetFont('Times');
  1741. * //Arial bold 14
  1742. * $pdf->SetFont('vera','B',14);
  1743. * //Removes bold
  1744. * $pdf->SetFont('');
  1745. * //Times bold, italic and underlined 14
  1746. * $pdf->SetFont('Times','BIU');
  1747. * </pre><br />
  1748. * If the file corresponding to the requested font is not found, the error "Could not include font metric file" is generated.
  1749. * @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.
  1750. * @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
  1751. * @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
  1752. * @since 1.0
  1753. * @see AddFont(), SetFontSize()
  1754. */
  1755. function SetFont($family, $style='', $size=0) {
  1756. // save previous values
  1757. $this->prevFontFamily = $this->FontFamily;
  1758. $this->prevFontStyle = $this->FontStyle;
  1759. //Select a font; size given in points
  1760. global $fpdf_charwidths;
  1761. $family=strtolower($family);
  1762. if($family=='') {
  1763. $family=$this->FontFamily;
  1764. }
  1765. if((!$this->isunicode) AND ($family == 'arial')) {
  1766. $family = 'helvetica';
  1767. }
  1768. elseif(($family=="symbol") OR ($family=="zapfdingbats")) {
  1769. $style='';
  1770. }
  1771. $style=strtoupper($style);
  1772. if(strpos($style,'U')!==false) {
  1773. $this->underline=true;
  1774. $style=str_replace('U','',$style);
  1775. }
  1776. else {
  1777. $this->underline=false;
  1778. }
  1779. if($style=='IB') {
  1780. $style='BI';
  1781. }
  1782. if($size==0) {
  1783. $size=$this->FontSizePt;
  1784. }
  1785. // try to add font (if not already added)
  1786. if($this->isunicode) {
  1787. $this->AddFont($family, $style);
  1788. }
  1789. //Test if font is already selected
  1790. if(($this->FontFamily == $family) AND ($this->FontStyle == $style) AND ($this->FontSizePt == $size)) {
  1791. return;
  1792. }
  1793. $fontkey = $family.$style;
  1794. //if(!isset($this->fonts[$fontkey]) AND isset($this->fonts[$family])) {
  1795. // $style='';
  1796. //}
  1797. //Test if used for the first time
  1798. if(!isset($this->fonts[$fontkey])) {
  1799. //Check if one of the standard fonts
  1800. if(isset($this->CoreFonts[$fontkey])) {
  1801. if(!isset($fpdf_charwidths[$fontkey])) {
  1802. //Load metric file
  1803. $file = $family;
  1804. if(($family!='symbol') AND ($family!='zapfdingbats')) {
  1805. $file .= strtolower($style);
  1806. }
  1807. if(!file_exists($this->_getfontpath().$file.'.php')) {
  1808. // try to load the basic file without styles
  1809. $file = $family;
  1810. $fontkey = $family;
  1811. }
  1812. include($this->_getfontpath().$file.'.php');
  1813. if (($this->isunicode AND !isset($ctg)) OR ((!$this->isunicode) AND (!isset($fpdf_charwidths[$fontkey]))) ) {
  1814. $this->Error("Could not include font metric file [".$fontkey."]: ".$this->_getfontpath().$file.".php");
  1815. }
  1816. }
  1817. $i = count($this->fonts) + 1;
  1818. if($this->isunicode) {
  1819. $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file, 'ctg'=>$ctg);
  1820. $fpdf_charwidths[$fontkey] = $cw;
  1821. } else {
  1822. $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$this->CoreFonts[$fontkey], 'up'=>-100, 'ut'=>50, 'cw'=>$fpdf_charwidths[$fontkey]);
  1823. }
  1824. }
  1825. else {
  1826. $this->Error('Undefined font: '.$family.' '.$style);
  1827. }
  1828. }
  1829. //Select it
  1830. $this->FontFamily = $family;
  1831. $this->FontStyle = $style;
  1832. $this->FontSizePt = $size;
  1833. $this->FontSize = $size / $this->k;
  1834. $this->CurrentFont = &$this->fonts[$fontkey];
  1835. if($this->page>0) {
  1836. $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  1837. }
  1838. }
  1839. /**
  1840. * Defines the size of the current font.
  1841. * @param float $size The size (in points)
  1842. * @since 1.0
  1843. * @see SetFont()
  1844. */
  1845. function SetFontSize($size) {
  1846. //Set font size in points
  1847. if($this->FontSizePt==$size) {
  1848. return;
  1849. }
  1850. $this->FontSizePt = $size;
  1851. $this->FontSize = $size / $this->k;
  1852. if($this->page > 0) {
  1853. $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  1854. }
  1855. }
  1856. /**
  1857. * 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 />
  1858. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  1859. * @since 1.5
  1860. * @see Cell(), Write(), Image(), Link(), SetLink()
  1861. */
  1862. function AddLink() {
  1863. //Create a new internal link
  1864. $n=count($this->links)+1;
  1865. $this->links[$n]=array(0,0);
  1866. return $n;
  1867. }
  1868. /**
  1869. * Defines the page and position a link points to
  1870. * @param int $link The link identifier returned by AddLink()
  1871. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  1872. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  1873. * @since 1.5
  1874. * @see AddLink()
  1875. */
  1876. function SetLink($link, $y=0, $page=-1) {
  1877. //Set destination of internal link
  1878. if($y==-1) {
  1879. $y=$this->y;
  1880. }
  1881. if($page==-1) {
  1882. $page=$this->page;
  1883. }
  1884. $this->links[$link]=array($page,$y);
  1885. }
  1886. /**
  1887. * 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.
  1888. * @param float $x Abscissa of the upper-left corner of the rectangle (or upper-right for RTL languages)
  1889. * @param float $y Ordinate of the upper-left corner of the rectangle (or upper-right for RTL languages)
  1890. * @param float $w Width of the rectangle
  1891. * @param float $h Height of the rectangle
  1892. * @param mixed $link URL or identifier returned by AddLink()
  1893. * @since 1.5
  1894. * @see AddLink(), Cell(), Write(), Image()
  1895. */
  1896. function Link($x, $y, $w, $h, $link) {
  1897. $this->PageLinks[$this->page][] = array($x * $this->k, $this->hPt - $y * $this->k, $w * $this->k, $h*$this->k, $link);
  1898. }
  1899. /**
  1900. * 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.
  1901. * @param float $x Abscissa of the origin
  1902. * @param float $y Ordinate of the origin
  1903. * @param string $txt String to print
  1904. * @since 1.0
  1905. * @see SetFont(), SetTextColor(), Cell(), MultiCell(), Write()
  1906. */
  1907. function Text($x, $y, $txt) {
  1908. //Output a string
  1909. if($this->rtl) {
  1910. // bidirectional algorithm (some chars may be changed affecting the line length)
  1911. $s = $this->utf8Bidi($this->UTF8StringToArray($txt), $this->tmprtl);
  1912. $l = $this->GetArrStringWidth($s);
  1913. $xr = $this->w - $x - $this->GetArrStringWidth($s);
  1914. } else {
  1915. $xr = $x;
  1916. }
  1917. $s = sprintf('BT %.2f %.2f Td (%s) Tj ET', $xr * $this->k, ($this->h-$y) * $this->k, $this->_escapetext($txt));
  1918. if($this->underline AND ($txt!='')) {
  1919. $s .= ' '.$this->_dounderline($xr, $y, $txt);
  1920. }
  1921. if($this->ColorFlag) {
  1922. $s='q '.$this->TextColor.' '.$s.' Q';
  1923. }
  1924. $this->_out($s);
  1925. }
  1926. /**
  1927. * 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 />
  1928. * This method is called automatically and should not be called directly by the application.<br />
  1929. * <b>Example:</b><br />
  1930. * The method is overriden in an inherited class in order to obtain a 3 column layout:<br />
  1931. * <pre>
  1932. * class PDF extends TCPDF {
  1933. * var $col=0;
  1934. *
  1935. * function SetCol($col) {
  1936. * //Move position to a column
  1937. * $this->col=$col;
  1938. * $x=10+$col*65;
  1939. * $this->SetLeftMargin($x);
  1940. * $this->SetX($x);
  1941. * }
  1942. *
  1943. * function AcceptPageBreak() {
  1944. * if($this->col<2) {
  1945. * //Go to next column
  1946. * $this->SetCol($this->col+1);
  1947. * $this->SetY(10);
  1948. * return false;
  1949. * }
  1950. * else {
  1951. * //Go back to first column and issue page break
  1952. * $this->SetCol(0);
  1953. * return true;
  1954. * }
  1955. * }
  1956. * }
  1957. *
  1958. * $pdf=new PDF();
  1959. * $pdf->Open();
  1960. * $pdf->AddPage();
  1961. * $pdf->SetFont('vera','',12);
  1962. * for($i=1;$i<=300;$i++) {
  1963. * $pdf->Cell(0,5,"Line $i",0,1);
  1964. * }
  1965. * $pdf->Output();
  1966. * </pre>
  1967. * @return boolean
  1968. * @since 1.4
  1969. * @see SetAutoPageBreak()
  1970. */
  1971. function AcceptPageBreak() {
  1972. //Accept automatic page break or not
  1973. return $this->AutoPageBreak;
  1974. }
  1975. /**
  1976. * 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 />
  1977. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  1978. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  1979. * @param float $h Cell height. Default value: 0.
  1980. * @param string $txt String to print. Default value: empty string.
  1981. * @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>
  1982. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>
  1983. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  1984. * @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><li>J: justify</li></ul>
  1985. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  1986. * @param mixed $link URL or identifier returned by AddLink().
  1987. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  1988. * @since 1.0
  1989. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  1990. */
  1991. function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0) {
  1992. $k = $this->k;
  1993. if((($this->y + $h) > $this->PageBreakTrigger) AND empty($this->InFooter) AND $this->AcceptPageBreak()) {
  1994. //Automatic page break
  1995. $x = $this->x;
  1996. $ws = $this->ws;
  1997. if($ws > 0) {
  1998. $this->ws = 0;
  1999. $this->_out('0 Tw');
  2000. }
  2001. $this->AddPage($this->CurOrientation);
  2002. if($ws > 0) {
  2003. $this->ws = $ws;
  2004. $this->_out(sprintf('%.3f Tw',$ws * $k));
  2005. }
  2006. $this->x = $x;
  2007. }
  2008. if($w == 0) {
  2009. if ($this->rtl) {
  2010. $w = $this->x - $this->lMargin;
  2011. } else {
  2012. $w = $this->w - $this->rMargin - $this->x;
  2013. }
  2014. }
  2015. $s = '';
  2016. if(($fill == 1) OR ($border == 1)) {
  2017. if($fill == 1) {
  2018. $op = ($border == 1) ? 'B' : 'f';
  2019. } else {
  2020. $op = 'S';
  2021. }
  2022. if ($this->rtl) {
  2023. $xk = ($this->x - $w) * $k;
  2024. } else {
  2025. $xk = $this->x * $k;
  2026. }
  2027. $s .= sprintf('%.2f %.2f %.2f %.2f re %s ', $xk, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op);
  2028. }
  2029. if(is_string($border)) {
  2030. $x=$this->x;
  2031. $y=$this->y;
  2032. if(strpos($border,'L')!==false) {
  2033. if ($this->rtl) {
  2034. $xk = ($x - $w) * $k;
  2035. } else {
  2036. $xk = $x * $k;
  2037. }
  2038. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$xk,($this->h-$y)*$k,$xk,($this->h-($y+$h))*$k);
  2039. }
  2040. if(strpos($border,'T')!==false) {
  2041. if ($this->rtl) {
  2042. $xk = ($x - $w) * $k;
  2043. $xwk = $x * $k;
  2044. } else {
  2045. $xk = $x * $k;
  2046. $xwk = ($x + $w) * $k;
  2047. }
  2048. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$xk,($this->h-$y)*$k,$xwk,($this->h-$y)*$k);
  2049. }
  2050. if(strpos($border,'R')!==false) {
  2051. if ($this->rtl) {
  2052. $xk = $x * $k;
  2053. } else {
  2054. $xk = ($x + $w) * $k;
  2055. }
  2056. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$xk,($this->h-$y)*$k,$xk,($this->h-($y+$h))*$k);
  2057. }
  2058. if(strpos($border,'B')!==false) {
  2059. if ($this->rtl) {
  2060. $xk = ($x - $w) * $k;
  2061. $xwk = $x * $k;
  2062. } else {
  2063. $xk = $x * $k;
  2064. $xwk = ($x + $w) * $k;
  2065. }
  2066. $s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$xk,($this->h-($y+$h))*$k,$xwk,($this->h-($y+$h))*$k);
  2067. }
  2068. }
  2069. if($txt != '') {
  2070. // text lenght
  2071. $width = $this->GetStringWidth($txt);
  2072. // ratio between cell lenght and text lenght
  2073. $ratio = ($w - (2 * $this->cMargin)) / $width;
  2074. // stretch text if required
  2075. if (($stretch > 0) AND (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0)))) {
  2076. if ($stretch > 2) {
  2077. // spacing
  2078. //Calculate character spacing in points
  2079. $char_space = ($w - $width - (2 * $this->cMargin)) / max($this->GetNumChars($s)-1,1) * $this->k;
  2080. //Set character spacing
  2081. $this->_out(sprintf('BT %.2f Tc ET', $char_space));
  2082. } else {
  2083. // scaling
  2084. //Calculate horizontal scaling
  2085. $horiz_scale = $ratio*100.0;
  2086. //Set horizontal scaling
  2087. $this->_out(sprintf('BT %.2f Tz ET', $horiz_scale));
  2088. }
  2089. $align = '';
  2090. $width = $w - (2 * $this->cMargin);
  2091. } else {
  2092. $stretch == 0;
  2093. }
  2094. if($align == 'L') {
  2095. if ($this->rtl) {
  2096. $dx = $w - $width - $this->cMargin;
  2097. } else {
  2098. $dx = $this->cMargin;
  2099. }
  2100. } elseif($align == 'R') {
  2101. if ($this->rtl) {
  2102. $dx = $this->cMargin;
  2103. } else {
  2104. $dx = $w - $width - $this->cMargin;
  2105. }
  2106. } elseif($align=='C') {
  2107. $dx = ($w - $width)/2;
  2108. } elseif($align=='J') {
  2109. if ($this->rtl) {
  2110. $dx = $w - $width - $this->cMargin;
  2111. } else {
  2112. $dx = $this->cMargin;
  2113. }
  2114. } else {
  2115. $dx = $this->cMargin;
  2116. }
  2117. if($this->ColorFlag) {
  2118. $s .= 'q '.$this->TextColor.' ';
  2119. }
  2120. $txt2 = $this->_escapetext($txt);
  2121. if ($this->rtl) {
  2122. $xdk = ($this->x - $dx - $width) * $k;
  2123. } else {
  2124. $xdk = ($this->x + $dx) * $k;
  2125. }
  2126. // 2008-02-16 Jacek Czekaj - multibyte justification
  2127. if ($align == 'J') {
  2128. // count number of spaces
  2129. $ns = substr_count($txt, ' ');
  2130. // get string width without spaces
  2131. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  2132. // set word position to be used with TJ operator
  2133. $txt2 = str_replace(chr(0).' ', ') '. -2830*($w-$width-(2*$this->cMargin))/($ns?$ns:1)/$this->FontSize/$this->k . ' (', $txt2);
  2134. }
  2135. $s.=sprintf('BT %.2f %.2f Td [(%s)] TJ ET', $xdk, ($this->h - ($this->y + 0.5 * $h + 0.3 * $this->FontSize)) * $k, $txt2);
  2136. if($this->underline) {
  2137. if ($this->rtl) {
  2138. $xdx = $this->x - $dx - $width;
  2139. } else {
  2140. $xdx = $this->x + $dx;
  2141. }
  2142. $s.=' '.$this->_dounderline($xdx, $this->y + 0.5 * $h + 0.3 * $this->FontSize, $txt);
  2143. }
  2144. if($this->ColorFlag) {
  2145. $s.=' Q';
  2146. }
  2147. if($link) {
  2148. if ($this->rtl) {
  2149. $xdx = $this->x - $dx - $width;
  2150. } else {
  2151. $xdx = $this->x + $dx;
  2152. }
  2153. $this->Link($xdx, $this->y + 0.5 * $h - 0.5 * $this->FontSize, $width, $this->FontSize, $link);
  2154. }
  2155. }
  2156. // output cell
  2157. if($s) {
  2158. // output cell
  2159. $this->_out($s);
  2160. // reset text stretching
  2161. if($stretch > 2) {
  2162. //Reset character horizontal spacing
  2163. $this->_out('BT 0 Tc ET');
  2164. } elseif($stretch > 0) {
  2165. //Reset character horizontal scaling
  2166. $this->_out('BT 100 Tz ET');
  2167. }
  2168. }
  2169. $this->lasth = $h;
  2170. if($ln>0) {
  2171. //Go to the beginning of the next line
  2172. $this->y += $h;
  2173. if($ln == 1) {
  2174. if ($this->rtl) {
  2175. $this->x = $this->w - $this->rMargin;
  2176. } else {
  2177. $this->x = $this->lMargin;
  2178. }
  2179. }
  2180. } else {
  2181. // go left or right by case
  2182. if ($this->rtl) {
  2183. $this->x -= $w;
  2184. } else {
  2185. $this->x += $w;
  2186. }
  2187. }
  2188. }
  2189. /**
  2190. * 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 />
  2191. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  2192. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  2193. * @param float $h Cell minimum height. The cell extends automatically if needed.
  2194. * @param string $txt String to print
  2195. * @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>
  2196. * @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>
  2197. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  2198. * @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 [DEFAULT]</li><li>2: below</li></ul>
  2199. * @param int $x x position in user units
  2200. * @param int $y y position in user units
  2201. * @param boolean $reseth if true reset the last cell height (default true).
  2202. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  2203. * @return int Rerurn the number of lines.
  2204. * @since 1.3
  2205. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  2206. */
  2207. function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0) {
  2208. if ((empty($this->lasth))OR ($reseth)) {
  2209. //set row height
  2210. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  2211. }
  2212. // get current page number
  2213. $startpage = $this->page;
  2214. if (!empty($y)) {
  2215. $this->SetY($y);
  2216. } else {
  2217. $y = $this->GetY();
  2218. }
  2219. if (!empty($x)) {
  2220. $this->SetX($x);
  2221. } else {
  2222. $x = $this->GetX();
  2223. }
  2224. if(empty($w)) {
  2225. if ($this->rtl) {
  2226. $w = $this->x - $this->lMargin;
  2227. } else {
  2228. $w = $this->w - $this->rMargin - $this->x;
  2229. }
  2230. }
  2231. // store original margin values
  2232. $lMargin = $this->lMargin;
  2233. $rMargin = $this->rMargin;
  2234. // set new margin values
  2235. if ($this->rtl) {
  2236. $this->SetLeftMargin($this->x - $w);
  2237. $this->SetRightMargin($this->w - $this->x);
  2238. } else {
  2239. $this->SetLeftMargin($this->x);
  2240. $this->SetRightMargin($this->w - $this->x - $w);
  2241. }
  2242. // calculate remaining vertical space on first page ($startpage)
  2243. $restspace = $this->getPageHeight() - $this->GetY() - $this->getBreakMargin();
  2244. // Write text
  2245. $nl = $this->Write($this->lasth, $txt, '', $fill, $align, true, $stretch);
  2246. // Get end-of-text Y position
  2247. $currentY = $this->GetY();
  2248. // get latest page number
  2249. $endpage = $this->page;
  2250. if (!empty($border)) {
  2251. // check if a new page has been created
  2252. if ($endpage > $startpage) {
  2253. // design borders around HTML cells.
  2254. for ($page=$startpage; $page<=$endpage; $page++) {
  2255. $this->page = $page;
  2256. if ($page==$startpage) {
  2257. $this->SetY($this->getPageHeight() - $restspace - $this->getBreakMargin());
  2258. $h = $restspace - 1;
  2259. } elseif ($page==$endpage) {
  2260. $this->SetY($this->tMargin); // put cursor at the beginning of text
  2261. $h = $currentY - $this->tMargin;
  2262. } else {
  2263. $this->SetY($this->tMargin); // put cursor at the beginning of text
  2264. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  2265. }
  2266. $this->SetX($x);
  2267. $this->Cell($w, $h, "", $border, 1, '', 0);
  2268. }
  2269. } else {
  2270. $h = max($h, ($currentY - $y));
  2271. $this->SetY($y); // put cursor at the beginning of text
  2272. $this->SetX($x);
  2273. // design a cell around the text
  2274. $this->Cell($w, $h, "", $border, 1, '', 0);
  2275. }
  2276. }
  2277. // restore original margin values
  2278. $this->SetLeftMargin($lMargin);
  2279. $this->SetRightMargin($rMargin);
  2280. if($ln>0) {
  2281. //Go to the beginning of the next line
  2282. $this->SetY($currentY);
  2283. if($ln == 2) {
  2284. $this->SetX($x + $w);
  2285. }
  2286. } else {
  2287. // go left or right by case
  2288. $this->page = $startpage;
  2289. $this->y = $y;
  2290. $this->SetX($x + $w);
  2291. }
  2292. return $nl;
  2293. }
  2294. /**
  2295. * This method prints text from the current position.<br />
  2296. * @param float $h Line height
  2297. * @param string $txt String to print
  2298. * @param mixed $link URL or identifier returned by AddLink()
  2299. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  2300. * @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><li>J: justify</li></ul>
  2301. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  2302. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  2303. * @return int Rerurn the number of lines.
  2304. * @since 1.5
  2305. */
  2306. function Write($h, $txt, $link='', $fill=0, $align='', $ln=false, $stretch=0) {
  2307. // store current position
  2308. $prevx = $this->x;
  2309. $prevy = $this->y;
  2310. // Adjust internal padding
  2311. if ($this->cMargin < ($this->LineWidth/2)) {
  2312. $this->cMargin = ($this->LineWidth/2);
  2313. }
  2314. // Add top space if needed
  2315. if (($h - $this->FontSize) < $this->LineWidth) {
  2316. $this->y += $this->LineWidth/2;
  2317. }
  2318. //if ($h < ($this->LineWidth)) {
  2319. // $h = ($this->LineWidth);
  2320. //}
  2321. // calculating remaining line width ($w)
  2322. if ($this->rtl) {
  2323. $w = $this->x - $this->lMargin;
  2324. } else {
  2325. $w = $this->w - $this->rMargin - $this->x;
  2326. }
  2327. // remove carriage returns
  2328. $s = str_replace("\r", '', $txt);
  2329. // get array of chars
  2330. $chars = $this->UTF8StringToArray($s);
  2331. // get the number of characters
  2332. $nb = count($chars);
  2333. // handle single space character
  2334. if(($nb==1) AND preg_match("/[\s]/u", $s)) {
  2335. if ($this->rtl) {
  2336. $this->x -= $this->GetStringWidth($s);
  2337. } else {
  2338. $this->x += $this->GetStringWidth($s);
  2339. }
  2340. return;
  2341. }
  2342. // max column width
  2343. $wmax = $w - (2 * $this->cMargin);
  2344. $i = 0; // character position
  2345. $j = 0; // current srting starting position
  2346. $sep = -1; // position of the last blank space
  2347. $l = 0; // current string lenght
  2348. $nl = 0; //number of lines
  2349. // for each character
  2350. while($i < $nb) {
  2351. //Get the current character
  2352. $c = $chars[$i];
  2353. if ($c == 10) {
  2354. // 10 = "\n" = new line
  2355. //Explicit line break
  2356. if ($align == "J") {
  2357. if ($this->rtl) {
  2358. $talign = "R";
  2359. } else {
  2360. $talign = "L";
  2361. }
  2362. } else {
  2363. $talign = $align;
  2364. }
  2365. $this->Cell($w, $h, $this->UTF8ArrSubString($chars, $j, $i), 0, 2, $talign, $fill, $link, $stretch);
  2366. $nl++;
  2367. $j = $i + 1;
  2368. $l = 0;
  2369. $sep = -1;
  2370. if($nl == 1) {
  2371. // set the next line width and position
  2372. if ($this->rtl) {
  2373. $this->x = $this->w - $this->rMargin;
  2374. $w = $this->x - $this->lMargin;
  2375. }
  2376. else {
  2377. $this->x = $this->lMargin;
  2378. $w = $this->w - $this->rMargin - $this->x;
  2379. }
  2380. $wmax = $w - (2 * $this->cMargin);
  2381. }
  2382. } else {
  2383. if(preg_match("/[\s]/u", $this->unichr($c))) {
  2384. // update last blank space position
  2385. $sep = $i;
  2386. }
  2387. // update string length
  2388. if($this->isunicode) {
  2389. // with bidirectional algorithm some chars may be changed affecting the line length
  2390. // *** very slow
  2391. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, $i-$j+1), $this->tmprtl));
  2392. } else {
  2393. $l += $this->GetCharWidth($c);
  2394. }
  2395. if($l > $wmax) {
  2396. // we have reached the end of column
  2397. if($sep == -1) {
  2398. // truncate the word because do not fit on column
  2399. $this->Cell($w, $h, $this->UTF8ArrSubString($chars, $j, $i), 0, 2, $align, $fill, $link, $stretch);
  2400. $nl++;
  2401. if($nl == 1) {
  2402. // set the next line width and position
  2403. if ($this->rtl) {
  2404. $this->x = $this->w - $this->rMargin;
  2405. $w = $this->x - $this->lMargin;
  2406. }
  2407. else {
  2408. $this->x = $this->lMargin;
  2409. $w = $this->w - $this->rMargin - $this->x;
  2410. }
  2411. $wmax = $w - (2 * $this->cMargin);
  2412. }
  2413. } else {
  2414. // word wrapping
  2415. $this->Cell($w, $h, $this->UTF8ArrSubString($chars, $j, $sep), 0, 2, $align, $fill, $link, $stretch);
  2416. $nl++;
  2417. $i = $sep + 1;
  2418. if($nl == 1) {
  2419. // set the next line width and position
  2420. if ($this->rtl) {
  2421. $this->x = $this->w - $this->rMargin;
  2422. $w = $this->x - $this->lMargin;
  2423. }
  2424. else {
  2425. $this->x = $this->lMargin;
  2426. $w = $this->w - $this->rMargin - $this->x;
  2427. }
  2428. $wmax = $w - (2 * $this->cMargin);
  2429. }
  2430. }
  2431. $sep = -1;
  2432. $j = $i;
  2433. $l = 0;
  2434. }
  2435. }
  2436. $i++;
  2437. } // end while i < nb
  2438. // print last row
  2439. if($i != $j) {
  2440. $this->Cell($w, $h, $this->UTF8ArrSubString($chars, $j, $nb), 0, $ln, $align, $fill, $link, $stretch);
  2441. $nl++;
  2442. }
  2443. $w = $this->GetStringWidth($this->UTF8ArrSubString($chars, $j, $nb)) + (2 * $this->cMargin);
  2444. if ($this->rtl) {
  2445. $this->x = $prevx - $w;
  2446. } else {
  2447. $this->x = $prevx + $w;
  2448. }
  2449. // Add bottom space if needed
  2450. if (($ln > 0) AND (($h - $this->FontSize) < $this->LineWidth)) {
  2451. $this->y += $this->LineWidth/2;
  2452. }
  2453. return $nl;
  2454. }
  2455. /**
  2456. * Extract a slice of the $strarr array and return it as string.
  2457. * @param string $strarr The input array of characters.
  2458. * @param int $start the starting element of $strarr.
  2459. * @param int $end first element that will not be returned.
  2460. * @return Return part of a string
  2461. */
  2462. function UTF8ArrSubString($strarr, $start='', $end='') {
  2463. if (strlen($start) == 0) {
  2464. $start = 0;
  2465. }
  2466. if (strlen($end) == 0) {
  2467. $end = count($strarr);
  2468. }
  2469. $string = "";
  2470. for ($i=$start; $i < $end; $i++) {
  2471. $string .= $this->unichr($strarr[$i]);
  2472. }
  2473. return $string;
  2474. }
  2475. /**
  2476. * Returns the unicode caracter specified by UTF-8 code
  2477. * @param int $c UTF-8 code
  2478. * @return Returns the specified character.
  2479. * @author Miguel Perez, Nicola Asuni
  2480. * @since 2.3.000 (2008-03-05)
  2481. */
  2482. function unichr($c) {
  2483. if (!$this->isunicode) {
  2484. return chr($c);
  2485. } elseif ($c <= 0x7F) {
  2486. // one byte
  2487. return chr($c);
  2488. } else if ($c <= 0x7FF) {
  2489. // two bytes
  2490. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  2491. } else if ($c <= 0xFFFF) {
  2492. // three bytes
  2493. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  2494. } else if ($c <= 0x10FFFF) {
  2495. // four bytes
  2496. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  2497. } else {
  2498. return "";
  2499. }
  2500. }
  2501. /**
  2502. * 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>
  2503. * Supported formats are JPEG and PNG.
  2504. * For JPEG, all flavors are allowed:<ul><li>gray scales</li><li>true colors (24 bits)</li><li>CMYK (32 bits)</li></ul>
  2505. * 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>
  2506. * If a transparent color is defined, it will be taken into account (but will be only interpreted by Acrobat 4 and above).<br />
  2507. * The format can be specified explicitly or inferred from the file extension.<br />
  2508. * It is possible to put a link on the image.<br />
  2509. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  2510. * @param string $file Name of the file containing the image.
  2511. * @param float $x Abscissa of the upper-left corner.
  2512. * @param float $y Ordinate of the upper-left corner.
  2513. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  2514. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  2515. * @param string $type Image format. Possible values are (case insensitive): JPG, JPEG, PNG. If not specified, the type is inferred from the file extension.
  2516. * @param mixed $link URL or identifier returned by AddLink().
  2517. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  2518. * @since 1.1
  2519. * @see AddLink()
  2520. */
  2521. function Image($file, $x, $y, $w=0, $h=0, $type='', $link='', $align='') {
  2522. //Put an image on the page
  2523. if(!isset($this->images[$file])) {
  2524. //First use of image, get info
  2525. if($type == '') {
  2526. $pos = strrpos($file,'.');
  2527. if(empty($pos)) {
  2528. $this->Error('Image file has no extension and no type was specified: '.$file);
  2529. }
  2530. $type = substr($file, $pos+1);
  2531. }
  2532. $type = strtolower($type);
  2533. $mqr = get_magic_quotes_runtime();
  2534. set_magic_quotes_runtime(0);
  2535. if($type == 'jpg' or $type == 'jpeg') {
  2536. $info=$this->_parsejpg($file);
  2537. } elseif($type == 'gif') {
  2538. $info=$this->_parsegif($file);
  2539. } elseif($type == 'png') {
  2540. $info=$this->_parsepng($file);
  2541. }else {
  2542. //Allow for additional formats
  2543. $mtd='_parse'.$type;
  2544. if(!method_exists($this,$mtd)) {
  2545. $this->Error('Unsupported image type: '.$type);
  2546. }
  2547. $info=$this->$mtd($file);
  2548. }
  2549. if($info === false) {
  2550. //If false, we cannot process image
  2551. return;
  2552. }
  2553. set_magic_quotes_runtime($mqr);
  2554. $info['i']=count($this->images)+1;
  2555. $this->images[$file]=$info;
  2556. }
  2557. else {
  2558. $info=$this->images[$file];
  2559. }
  2560. //Automatic width and height calculation if needed
  2561. if(($w == 0) and ($h == 0)) {
  2562. //Put image at 72 dpi
  2563. // 2004-06-14 :: Nicola Asuni, scale factor where added
  2564. $w = $info['w'] / ($this->imgscale * $this->k);
  2565. $h = $info['h'] / ($this->imgscale * $this->k);
  2566. }
  2567. if($w == 0) {
  2568. $w = $h * $info['w'] / $info['h'];
  2569. }
  2570. if($h == 0) {
  2571. $h = $w * $info['h'] / $info['w'];
  2572. }
  2573. // 2007-10-19 Warren Sherliker
  2574. // Check whether we need a new page first as this does not fit
  2575. // Copied from Cell()
  2576. if((($this->y + $h) > $this->PageBreakTrigger) AND empty($this->InFooter) AND $this->AcceptPageBreak()) {
  2577. // Automatic page break
  2578. $this->AddPage($this->CurOrientation);
  2579. // Reset coordinates to top fo next page
  2580. $x = $this->GetX();
  2581. $y = $this->GetY();
  2582. }
  2583. // 2007-10-19 Warren Sherliker: End Edit
  2584. // set bottomcoordinates
  2585. $this->img_rb_y = $y + $h;
  2586. if ($this->rtl) {
  2587. $ximg = ($this->w - $x -$w);
  2588. // set left side coordinate
  2589. $this->img_rb_x = $ximg;
  2590. } else {
  2591. $ximg = $x;
  2592. // set right side coordinate
  2593. $this->img_rb_x = $ximg + $w;
  2594. }
  2595. $xkimg = $ximg * $this->k;
  2596. $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', $w*$this->k, $h*$this->k, $xkimg, ($this->h-($y+$h))*$this->k, $info['i']));
  2597. if($link) {
  2598. $this->Link($ximg, $y, $w, $h, $link);
  2599. }
  2600. // set pointer to align the successive text/objects
  2601. switch($align) {
  2602. case 'T':{
  2603. $this->y = $y;
  2604. $this->x = $this->img_rb_x;
  2605. break;
  2606. }
  2607. case 'M':{
  2608. $this->y = $y + round($h/2);
  2609. $this->x = $this->img_rb_x;
  2610. break;
  2611. }
  2612. case 'B':{
  2613. $this->y = $this->img_rb_y;
  2614. $this->x = $this->img_rb_x;
  2615. break;
  2616. }
  2617. case 'N':{
  2618. $this->SetY($this->img_rb_y);
  2619. break;
  2620. }
  2621. default:{
  2622. break;
  2623. }
  2624. }
  2625. }
  2626. /**
  2627. * Performs a line break. The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  2628. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  2629. * @since 1.0
  2630. * @see Cell()
  2631. */
  2632. function Ln($h='') {
  2633. //Line feed; default value is last cell height
  2634. if ($this->rtl) {
  2635. $this->x = $this->w - $this->rMargin;
  2636. } else {
  2637. $this->x = $this->lMargin;
  2638. }
  2639. if(is_string($h)) {
  2640. $this->y += $this->lasth;
  2641. } else {
  2642. $this->y += $h;
  2643. }
  2644. }
  2645. /**
  2646. * Returns the relative X value of current position.
  2647. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  2648. * @return float
  2649. * @since 1.2
  2650. * @see SetX(), GetY(), SetY()
  2651. */
  2652. function GetX() {
  2653. //Get x position
  2654. if ($this->rtl) {
  2655. return ($this->w - $this->x);
  2656. } else {
  2657. return $this->x;
  2658. }
  2659. }
  2660. /**
  2661. * Returns the absolute X value of current position.
  2662. * @return float
  2663. * @since 1.2
  2664. * @see SetX(), GetY(), SetY()
  2665. */
  2666. function GetAbsX() {
  2667. return $this->x;
  2668. }
  2669. /**
  2670. * Returns the ordinate of the current position.
  2671. * @return float
  2672. * @since 1.0
  2673. * @see SetY(), GetX(), SetX()
  2674. */
  2675. function GetY() {
  2676. //Get y position
  2677. return $this->y;
  2678. }
  2679. /**
  2680. * Defines the abscissa of the current position.
  2681. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  2682. * @param float $x The value of the abscissa.
  2683. * @since 1.2
  2684. * @see GetX(), GetY(), SetY(), SetXY()
  2685. */
  2686. function SetX($x) {
  2687. //Set x position
  2688. if ($this->rtl) {
  2689. if($x >= 0) {
  2690. $this->x = $this->w - $x;
  2691. } else {
  2692. $this->x = abs($x);
  2693. }
  2694. } else {
  2695. if($x >= 0) {
  2696. $this->x = $x;
  2697. } else {
  2698. $this->x = $this->w + $x;
  2699. }
  2700. }
  2701. }
  2702. /**
  2703. * Moves the current abscissa back to the left margin and sets the ordinate.
  2704. * If the passed value is negative, it is relative to the bottom of the page.
  2705. * @param float $y The value of the ordinate.
  2706. * @since 1.0
  2707. * @see GetX(), GetY(), SetY(), SetXY()
  2708. */
  2709. function SetY($y) {
  2710. //Set y position and reset x
  2711. if ($this->rtl) {
  2712. $this->x = $this->w - $this->rMargin;
  2713. } else {
  2714. $this->x = $this->lMargin;
  2715. }
  2716. if($y >= 0) {
  2717. $this->y = $y;
  2718. } else {
  2719. $this->y = $this->h + $y;
  2720. }
  2721. }
  2722. /**
  2723. * 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.
  2724. * @param float $x The value of the abscissa
  2725. * @param float $y The value of the ordinate
  2726. * @since 1.2
  2727. * @see SetX(), SetY()
  2728. */
  2729. function SetXY($x, $y) {
  2730. //Set x and y positions
  2731. $this->SetY($y);
  2732. $this->SetX($x);
  2733. }
  2734. /**
  2735. * 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 />
  2736. * The method first calls Close() if necessary to terminate the document.
  2737. * @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.
  2738. * @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).
  2739. * @since 1.0
  2740. * @see Close()
  2741. */
  2742. function Output($name='',$dest='') {
  2743. //Output PDF to some destination
  2744. //Finish document if necessary
  2745. if($this->state < 3) {
  2746. $this->Close();
  2747. }
  2748. //Normalize parameters
  2749. if(is_bool($dest)) {
  2750. $dest=$dest ? 'D' : 'F';
  2751. }
  2752. $dest=strtoupper($dest);
  2753. if($dest=='') {
  2754. if($name=='') {
  2755. $name='doc.pdf';
  2756. $dest='I';
  2757. } else {
  2758. $dest='F';
  2759. }
  2760. }
  2761. switch($dest) {
  2762. case 'I': {
  2763. //Send to standard output
  2764. if(ob_get_contents()) {
  2765. $this->Error('Some data has already been output, can\'t send PDF file');
  2766. }
  2767. if(php_sapi_name()!='cli') {
  2768. //We send to a browser
  2769. header('Content-Type: application/pdf');
  2770. if(headers_sent()) {
  2771. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  2772. }
  2773. header('Content-Length: '.strlen($this->buffer));
  2774. header('Content-disposition: inline; filename="'.$name.'"');
  2775. }
  2776. echo $this->buffer;
  2777. break;
  2778. }
  2779. case 'D': {
  2780. //Download file
  2781. if(ob_get_contents()) {
  2782. $this->Error('Some data has already been output, can\'t send PDF file');
  2783. }
  2784. if(isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) {
  2785. header('Content-Type: application/force-download');
  2786. } else {
  2787. header('Content-Type: application/octet-stream');
  2788. }
  2789. if(headers_sent()) {
  2790. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  2791. }
  2792. header('Content-Length: '.strlen($this->buffer));
  2793. header('Content-disposition: attachment; filename="'.$name.'"');
  2794. echo $this->buffer;
  2795. break;
  2796. }
  2797. case 'F': {
  2798. //Save to local file
  2799. $f=fopen($name,'wb');
  2800. if(!$f) {
  2801. $this->Error('Unable to create output file: '.$name);
  2802. }
  2803. fwrite($f,$this->buffer,strlen($this->buffer));
  2804. fclose($f);
  2805. break;
  2806. }
  2807. case 'S': {
  2808. //Return as a string
  2809. return $this->buffer;
  2810. }
  2811. default: {
  2812. $this->Error('Incorrect output destination: '.$dest);
  2813. }
  2814. }
  2815. return '';
  2816. }
  2817. // Protected methods
  2818. /**
  2819. * Check for locale-related bug
  2820. * @access protected
  2821. */
  2822. function _dochecks() {
  2823. //Check for locale-related bug
  2824. if(1.1==1) {
  2825. $this->Error('Don\'t alter the locale before including class file');
  2826. }
  2827. //Check for decimal separator
  2828. if(sprintf('%.1f',1.0)!='1.0') {
  2829. setlocale(LC_NUMERIC,'C');
  2830. }
  2831. }
  2832. /**
  2833. * Return fonts path
  2834. * @access protected
  2835. */
  2836. function _getfontpath() {
  2837. if(!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/font')) {
  2838. define('K_PATH_FONTS', dirname(__FILE__).'/font/');
  2839. }
  2840. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  2841. }
  2842. /**
  2843. * Start document
  2844. * @access protected
  2845. */
  2846. function _begindoc() {
  2847. //Start document
  2848. $this->state=1;
  2849. $this->_out('%PDF-'.$this->PDFVersion);
  2850. }
  2851. /**
  2852. * _putpages
  2853. * @access protected
  2854. */
  2855. function _putpages() {
  2856. $nb = $this->page;
  2857. if(!empty($this->AliasNbPages)) {
  2858. $nbstr = $this->UTF8ToUTF16BE($nb, false);
  2859. //Replace number of pages
  2860. for($n=1;$n<=$nb;$n++) {
  2861. $this->pages[$n]=str_replace($this->AliasNbPages, $nbstr, $this->pages[$n]);
  2862. }
  2863. }
  2864. if($this->DefOrientation=='P') {
  2865. $wPt=$this->fwPt;
  2866. $hPt=$this->fhPt;
  2867. }
  2868. else {
  2869. $wPt=$this->fhPt;
  2870. $hPt=$this->fwPt;
  2871. }
  2872. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  2873. for($n=1;$n<=$nb;$n++) {
  2874. //Page
  2875. $this->_newobj();
  2876. $this->_out('<</Type /Page');
  2877. $this->_out('/Parent 1 0 R');
  2878. if(isset($this->OrientationChanges[$n])) {
  2879. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$hPt,$wPt));
  2880. }
  2881. $this->_out('/Resources 2 0 R');
  2882. if(isset($this->PageLinks[$n])) {
  2883. //Links
  2884. $annots='/Annots [';
  2885. foreach($this->PageLinks[$n] as $pl) {
  2886. $rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
  2887. $annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
  2888. if(is_string($pl[4])) {
  2889. $annots.='/A <</S /URI /URI '.$this->_uristring($pl[4]).'>>>>';
  2890. }
  2891. else {
  2892. $l=$this->links[$pl[4]];
  2893. $h=isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;
  2894. $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
  2895. }
  2896. }
  2897. $this->_out($annots.']');
  2898. }
  2899. $this->_out('/Contents '.($this->n+1).' 0 R>>');
  2900. $this->_out('endobj');
  2901. //Page content
  2902. $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
  2903. $this->_newobj();
  2904. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  2905. $this->_putstream($p);
  2906. $this->_out('endobj');
  2907. }
  2908. //Pages root
  2909. $this->offsets[1]=strlen($this->buffer);
  2910. $this->_out('1 0 obj');
  2911. $this->_out('<</Type /Pages');
  2912. $kids='/Kids [';
  2913. for($i=0;$i<$nb;$i++) {
  2914. $kids.=(3+2*$i).' 0 R ';
  2915. }
  2916. $this->_out($kids.']');
  2917. $this->_out('/Count '.$nb);
  2918. $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]',$wPt,$hPt));
  2919. $this->_out('>>');
  2920. $this->_out('endobj');
  2921. }
  2922. /**
  2923. * Adds fonts
  2924. * _putfonts
  2925. * @access protected
  2926. */
  2927. function _putfonts() {
  2928. $nf=$this->n;
  2929. foreach($this->diffs as $diff) {
  2930. //Encodings
  2931. $this->_newobj();
  2932. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  2933. $this->_out('endobj');
  2934. }
  2935. $mqr=get_magic_quotes_runtime();
  2936. set_magic_quotes_runtime(0);
  2937. foreach($this->FontFiles as $file=>$info) {
  2938. //Font file embedding
  2939. $this->_newobj();
  2940. $this->FontFiles[$file]['n']=$this->n;
  2941. $font='';
  2942. $f=fopen($this->_getfontpath().strtolower($file),'rb',1);
  2943. if(!$f) {
  2944. $this->Error('Font file not found: '.$file);
  2945. }
  2946. while(!feof($f)) {
  2947. $font .= fread($f, 8192);
  2948. }
  2949. fclose($f);
  2950. $compressed=(substr($file,-2)=='.z');
  2951. if(!$compressed && isset($info['length2'])) {
  2952. $header=(ord($font{0})==128);
  2953. if($header) {
  2954. //Strip first binary header
  2955. $font=substr($font,6);
  2956. }
  2957. if($header && ord($font{$info['length1']})==128) {
  2958. //Strip second binary header
  2959. $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
  2960. }
  2961. }
  2962. $this->_out('<</Length '.strlen($font));
  2963. if($compressed) {
  2964. $this->_out('/Filter /FlateDecode');
  2965. }
  2966. $this->_out('/Length1 '.$info['length1']);
  2967. if(isset($info['length2'])) {
  2968. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  2969. }
  2970. $this->_out('>>');
  2971. $this->_putstream($font);
  2972. $this->_out('endobj');
  2973. }
  2974. set_magic_quotes_runtime($mqr);
  2975. foreach($this->fonts as $k=>$font) {
  2976. //Font objects
  2977. $this->fonts[$k]['n']=$this->n+1;
  2978. $type=$font['type'];
  2979. $name=$font['name'];
  2980. if($type=='core') {
  2981. //Standard font
  2982. $this->_newobj();
  2983. $this->_out('<</Type /Font');
  2984. $this->_out('/BaseFont /'.$name);
  2985. $this->_out('/Subtype /Type1');
  2986. if($name!='Symbol' && $name!='ZapfDingbats') {
  2987. $this->_out('/Encoding /WinAnsiEncoding');
  2988. }
  2989. $this->_out('>>');
  2990. $this->_out('endobj');
  2991. } elseif($type=='Type1' OR $type=='TrueType') {
  2992. //Additional Type1 or TrueType font
  2993. $this->_newobj();
  2994. $this->_out('<</Type /Font');
  2995. $this->_out('/BaseFont /'.$name);
  2996. $this->_out('/Subtype /'.$type);
  2997. $this->_out('/FirstChar 32 /LastChar 255');
  2998. $this->_out('/Widths '.($this->n+1).' 0 R');
  2999. $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
  3000. if($font['enc']) {
  3001. if(isset($font['diff'])) {
  3002. $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
  3003. } else {
  3004. $this->_out('/Encoding /WinAnsiEncoding');
  3005. }
  3006. }
  3007. $this->_out('>>');
  3008. $this->_out('endobj');
  3009. //Widths
  3010. $this->_newobj();
  3011. $cw=&$font['cw'];
  3012. $s='[';
  3013. for($i=32;$i<=255;$i++) {
  3014. $s.=$cw[chr($i)].' ';
  3015. }
  3016. $this->_out($s.']');
  3017. $this->_out('endobj');
  3018. //Descriptor
  3019. $this->_newobj();
  3020. $s='<</Type /FontDescriptor /FontName /'.$name;
  3021. foreach($font['desc'] as $k=>$v) {
  3022. $s.=' /'.$k.' '.$v;
  3023. }
  3024. $file = $font['file'];
  3025. if($file) {
  3026. $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
  3027. }
  3028. $this->_out($s.'>>');
  3029. $this->_out('endobj');
  3030. } else {
  3031. //Allow for additional types
  3032. $mtd='_put'.strtolower($type);
  3033. if(!method_exists($this, $mtd)) {
  3034. $this->Error('Unsupported font type: '.$type);
  3035. }
  3036. $this->$mtd($font);
  3037. }
  3038. }
  3039. }
  3040. /**
  3041. * _putimages
  3042. * @access protected
  3043. */
  3044. function _putimages() {
  3045. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  3046. reset($this->images);
  3047. while(list($file,$info)=each($this->images)) {
  3048. $this->_newobj();
  3049. $this->images[$file]['n']=$this->n;
  3050. $this->_out('<</Type /XObject');
  3051. $this->_out('/Subtype /Image');
  3052. $this->_out('/Width '.$info['w']);
  3053. $this->_out('/Height '.$info['h']);
  3054. if (isset($info["masked"])) {
  3055. $this->_out('/SMask '.($this->n-1).' 0 R');
  3056. }
  3057. if($info['cs']=='Indexed') {
  3058. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
  3059. }
  3060. else {
  3061. $this->_out('/ColorSpace /'.$info['cs']);
  3062. if($info['cs']=='DeviceCMYK') {
  3063. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  3064. }
  3065. }
  3066. $this->_out('/BitsPerComponent '.$info['bpc']);
  3067. if(isset($info['f'])) {
  3068. $this->_out('/Filter /'.$info['f']);
  3069. }
  3070. if(isset($info['parms'])) {
  3071. $this->_out($info['parms']);
  3072. }
  3073. if(isset($info['trns']) and is_array($info['trns'])) {
  3074. $trns='';
  3075. for($i=0;$i<count($info['trns']);$i++) {
  3076. $trns.=$info['trns'][$i].' '.$info['trns'][$i].' ';
  3077. }
  3078. $this->_out('/Mask ['.$trns.']');
  3079. }
  3080. $this->_out('/Length '.strlen($info['data']).'>>');
  3081. $this->_putstream($info['data']);
  3082. unset($this->images[$file]['data']);
  3083. $this->_out('endobj');
  3084. //Palette
  3085. if($info['cs']=='Indexed') {
  3086. $this->_newobj();
  3087. $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  3088. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  3089. $this->_putstream($pal);
  3090. $this->_out('endobj');
  3091. }
  3092. }
  3093. }
  3094. /**
  3095. * _putxobjectdict
  3096. * @access protected
  3097. */
  3098. function _putxobjectdict() {
  3099. foreach($this->images as $image) {
  3100. $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
  3101. }
  3102. }
  3103. /**
  3104. * _putresourcedict
  3105. * @access protected
  3106. */
  3107. function _putresourcedict(){
  3108. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  3109. $this->_out('/Font <<');
  3110. foreach($this->fonts as $font) {
  3111. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  3112. }
  3113. $this->_out('>>');
  3114. $this->_out('/XObject <<');
  3115. $this->_putxobjectdict();
  3116. $this->_out('>>');
  3117. }
  3118. /**
  3119. * _putresources
  3120. * @access protected
  3121. */
  3122. function _putresources() {
  3123. $this->_putfonts();
  3124. $this->_putimages();
  3125. //Resource dictionary
  3126. $this->offsets[2]=strlen($this->buffer);
  3127. $this->_out('2 0 obj');
  3128. $this->_out('<<');
  3129. $this->_putresourcedict();
  3130. $this->_out('>>');
  3131. $this->_out('endobj');
  3132. $this->_putjavascript();
  3133. $this->_putbookmarks();
  3134. // encryption
  3135. if ($this->encrypted) {
  3136. $this->_newobj();
  3137. $this->enc_obj_id = $this->n;
  3138. $this->_out('<<');
  3139. $this->_putencryption();
  3140. $this->_out('>>');
  3141. $this->_out('endobj');
  3142. }
  3143. }
  3144. /**
  3145. * _putinfo
  3146. * Adds some meta information
  3147. * @access protected
  3148. */
  3149. function _putinfo() {
  3150. $this->_out('/CreationDate ('.$this->_escape('D:'.date('YmdHis')).')');
  3151. $this->_out('/ModDate ('.$this->_escape('D:'.date('YmdHis')).')');
  3152. $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
  3153. if(!empty($this->title)) {
  3154. $this->_out('/Title '.$this->_textstring($this->title));
  3155. }
  3156. if(!empty($this->subject)) {
  3157. $this->_out('/Subject '.$this->_textstring($this->subject));
  3158. }
  3159. if(!empty($this->author)) {
  3160. $this->_out('/Author '.$this->_textstring($this->author));
  3161. }
  3162. if(!empty($this->keywords)) {
  3163. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  3164. }
  3165. if(!empty($this->creator)) {
  3166. $this->_out('/Creator '.$this->_textstring($this->creator));
  3167. }
  3168. }
  3169. /**
  3170. * _putcatalog
  3171. * @access protected
  3172. */
  3173. function _putcatalog() {
  3174. $this->_out('/Type /Catalog');
  3175. $this->_out('/Pages 1 0 R');
  3176. if($this->ZoomMode=='fullpage') {
  3177. $this->_out('/OpenAction [3 0 R /Fit]');
  3178. }
  3179. elseif($this->ZoomMode=='fullwidth') {
  3180. $this->_out('/OpenAction [3 0 R /FitH null]');
  3181. }
  3182. elseif($this->ZoomMode=='real') {
  3183. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  3184. }
  3185. elseif(!is_string($this->ZoomMode)) {
  3186. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']');
  3187. }
  3188. if($this->LayoutMode=='single') {
  3189. $this->_out('/PageLayout /SinglePage');
  3190. }
  3191. elseif($this->LayoutMode=='continuous') {
  3192. $this->_out('/PageLayout /OneColumn');
  3193. }
  3194. elseif($this->LayoutMode=='two') {
  3195. $this->_out('/PageLayout /TwoColumnLeft');
  3196. }
  3197. if (!empty($this->javascript)) {
  3198. $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
  3199. }
  3200. if(count($this->outlines)>0) {
  3201. $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
  3202. $this->_out('/PageMode /UseOutlines');
  3203. }
  3204. if($this->rtl) {
  3205. $this->_out('/ViewerPreferences << /Direction /R2L >>');
  3206. }
  3207. }
  3208. /**
  3209. * _puttrailer
  3210. * @access protected
  3211. */
  3212. function _puttrailer() {
  3213. $this->_out('/Size '.($this->n+1));
  3214. $this->_out('/Root '.$this->n.' 0 R');
  3215. $this->_out('/Info '.($this->n-1).' 0 R');
  3216. if ($this->encrypted) {
  3217. $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
  3218. $this->_out('/ID [()()]');
  3219. }
  3220. }
  3221. /**
  3222. * _putheader
  3223. * @access protected
  3224. */
  3225. function _putheader() {
  3226. $this->_out('%PDF-'.$this->PDFVersion);
  3227. }
  3228. /**
  3229. * _enddoc
  3230. * @access protected
  3231. */
  3232. function _enddoc() {
  3233. $this->_putheader();
  3234. $this->_putpages();
  3235. $this->_putresources();
  3236. //Info
  3237. $this->_newobj();
  3238. $this->_out('<<');
  3239. $this->_putinfo();
  3240. $this->_out('>>');
  3241. $this->_out('endobj');
  3242. //Catalog
  3243. $this->_newobj();
  3244. $this->_out('<<');
  3245. $this->_putcatalog();
  3246. $this->_out('>>');
  3247. $this->_out('endobj');
  3248. //Cross-ref
  3249. $o=strlen($this->buffer);
  3250. $this->_out('xref');
  3251. $this->_out('0 '.($this->n+1));
  3252. $this->_out('0000000000 65535 f ');
  3253. for($i=1;$i<=$this->n;$i++) {
  3254. $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
  3255. }
  3256. //Trailer
  3257. $this->_out('trailer');
  3258. $this->_out('<<');
  3259. $this->_puttrailer();
  3260. $this->_out('>>');
  3261. $this->_out('startxref');
  3262. $this->_out($o);
  3263. $this->_out('%%EOF');
  3264. $this->state=3;
  3265. }
  3266. /**
  3267. * _beginpage
  3268. * @access protected
  3269. */
  3270. function _beginpage($orientation) {
  3271. $this->page++;
  3272. $this->pages[$this->page]='';
  3273. $this->state=2;
  3274. if ($this->rtl) {
  3275. $this->x = $this->w - $this->rMargin;
  3276. } else {
  3277. $this->x = $this->lMargin;
  3278. }
  3279. $this->y = $this->tMargin;
  3280. $this->FontFamily='';
  3281. //Page orientation
  3282. if(empty($orientation)) {
  3283. $orientation=$this->DefOrientation;
  3284. }
  3285. else {
  3286. $orientation=strtoupper($orientation{0});
  3287. if($orientation!=$this->DefOrientation) {
  3288. $this->OrientationChanges[$this->page]=true;
  3289. }
  3290. }
  3291. if($orientation!=$this->CurOrientation) {
  3292. //Change orientation
  3293. if($orientation=='P') {
  3294. $this->wPt=$this->fwPt;
  3295. $this->hPt=$this->fhPt;
  3296. $this->w=$this->fw;
  3297. $this->h=$this->fh;
  3298. }
  3299. else {
  3300. $this->wPt=$this->fhPt;
  3301. $this->hPt=$this->fwPt;
  3302. $this->w=$this->fh;
  3303. $this->h=$this->fw;
  3304. }
  3305. $this->PageBreakTrigger=$this->h-$this->bMargin;
  3306. $this->CurOrientation=$orientation;
  3307. }
  3308. }
  3309. /**
  3310. * End of page contents
  3311. * @access protected
  3312. */
  3313. function _endpage() {
  3314. $this->state=1;
  3315. }
  3316. /**
  3317. * Begin a new object
  3318. * @access protected
  3319. */
  3320. function _newobj() {
  3321. $this->n++;
  3322. $this->offsets[$this->n]=strlen($this->buffer);
  3323. $this->_out($this->n.' 0 obj');
  3324. }
  3325. /**
  3326. * Underline text
  3327. * @param int $x X coordinate
  3328. * @param int $y Y coordinate
  3329. * @param string $txt text to underline
  3330. * @access protected
  3331. */
  3332. function _dounderline($x, $y, $txt) {
  3333. $up = $this->CurrentFont['up'];
  3334. $ut = $this->CurrentFont['ut'];
  3335. $w = $this->GetStringWidth($txt) + $this->ws * substr_count($txt,' ');
  3336. 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);
  3337. }
  3338. // REWRITTEN by Warren Sherliker wsherliker@gmail.com
  3339. // altered to allow compatibility with all sorts of image formats including gif.
  3340. // Can easily extend to work with others
  3341. // such as gd xbm etc. which are all supported by php 5+
  3342. // (Requires GD library)
  3343. /**
  3344. * Extract info from a JPEG file
  3345. * @param string $file image file to parse
  3346. * @return string
  3347. * @access protected
  3348. */
  3349. function _parsejpg($file) {
  3350. if(!function_exists('imagecreatefromjpeg')) {
  3351. // GD is not installed, try legacy method
  3352. return $this->_legacyparsejpg($file);
  3353. }
  3354. $a=getimagesize($file);
  3355. if(empty($a)) {
  3356. $this->Error('Missing or incorrect image file: '.$file);
  3357. }
  3358. if($a[2]!=2) {
  3359. $this->Error('Not a JPEG file: '.$file);
  3360. }
  3361. $jpeg = imagecreatefromjpeg($file);
  3362. return $this->outputjpg($file, $jpeg);
  3363. }
  3364. /**
  3365. * Extract info from a GIF file
  3366. * @param string $file image file to parse
  3367. * @return string
  3368. * @access protected
  3369. */
  3370. function _parsegif($file) {
  3371. if(!function_exists('imagecreatefromgif')) {
  3372. // PDF doesn't support native GIF and GD is not installed
  3373. return false;
  3374. }
  3375. $a=getimagesize($file);
  3376. if(empty($a)) {
  3377. $this->Error('Missing or incorrect image file: '.$file);
  3378. }
  3379. if($a[2]!=1) {
  3380. $this->Error('Not a GIF file: '.$file);
  3381. }
  3382. // Temporary convert file to jpg and then delete this temp data file
  3383. $gif = imagecreatefromgif($file);
  3384. return $this->toJPEG($file, $gif);
  3385. }
  3386. /**
  3387. * Extract info from a PNG file
  3388. * @param string $file image file to parse
  3389. * @return string
  3390. * @access protected
  3391. */
  3392. function _parsepng($file) {
  3393. if(!function_exists('imagecreatefrompng')) {
  3394. // GD is not installed, try legacy method
  3395. return $this->_legacyparsepng($file);
  3396. }
  3397. $f=fopen($file,'rb');
  3398. if(empty($f)) {
  3399. $this->Error('Can\'t open image file: '.$file);
  3400. }
  3401. //Check signature
  3402. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  3403. $this->Error('Not a PNG file: '.$file);
  3404. }
  3405. //Read header chunk
  3406. fread($f,4);
  3407. if(fread($f,4)!='IHDR') {
  3408. $this->Error('Incorrect PNG file: '.$file);
  3409. }
  3410. // Temporary convert file to jpg and then delete this temp data file
  3411. $a=getimagesize($file);
  3412. $png = imagecreatefrompng($file);
  3413. return $this->toJPEG($file, $png);
  3414. }
  3415. /**
  3416. * Extract info from a JPEG file without using GD
  3417. * @param string $file image file to parse
  3418. * @return string
  3419. * @access protected
  3420. */
  3421. function _legacyparsejpg($file) {
  3422. $a=GetImageSize($file);
  3423. if(empty($a)) {
  3424. $this->Error('Missing or incorrect image file: '.$file);
  3425. }
  3426. if($a[2]!=2) {
  3427. $this->Error('Not a JPEG file: '.$file);
  3428. }
  3429. if(!isset($a['channels']) or $a['channels']==3) {
  3430. $colspace='DeviceRGB';
  3431. }
  3432. elseif($a['channels']==4) {
  3433. $colspace='DeviceCMYK';
  3434. }
  3435. else {
  3436. $colspace='DeviceGray';
  3437. }
  3438. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  3439. //Read whole file
  3440. $f=fopen($file,'rb');
  3441. $data='';
  3442. while(!feof($f)) {
  3443. $data.=fread($f,4096);
  3444. }
  3445. fclose($f);
  3446. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  3447. }
  3448. /**
  3449. * Extract info from a PNG file without using GD
  3450. * @param string $file image file to parse
  3451. * @return string
  3452. * @access protected
  3453. */
  3454. function _legacyparsepng($file) {
  3455. $f=fopen($file,'rb');
  3456. if(empty($f)) {
  3457. $this->Error('Can\'t open image file: '.$file);
  3458. }
  3459. //Check signature
  3460. if(fread($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  3461. $this->Error('Not a PNG file: '.$file);
  3462. }
  3463. //Read header chunk
  3464. fread($f,4);
  3465. if(fread($f,4)!='IHDR') {
  3466. $this->Error('Incorrect PNG file: '.$file);
  3467. }
  3468. $w=$this->_freadint($f);
  3469. $h=$this->_freadint($f);
  3470. $bpc=ord(fread($f,1));
  3471. if($bpc>8) {
  3472. $this->Error('16-bit depth not supported: '.$file);
  3473. }
  3474. $ct=ord(fread($f,1));
  3475. if($ct==0) {
  3476. $colspace='DeviceGray';
  3477. }
  3478. elseif($ct==2) {
  3479. $colspace='DeviceRGB';
  3480. }
  3481. elseif($ct==3) {
  3482. $colspace='Indexed';
  3483. }
  3484. else {
  3485. $this->Error('Alpha channel not supported: '.$file);
  3486. }
  3487. if(ord(fread($f,1))!=0) {
  3488. $this->Error('Unknown compression method: '.$file);
  3489. }
  3490. if(ord(fread($f,1))!=0) {
  3491. $this->Error('Unknown filter method: '.$file);
  3492. }
  3493. if(ord(fread($f,1))!=0) {
  3494. $this->Error('Interlacing not supported: '.$file);
  3495. }
  3496. fread($f,4);
  3497. $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  3498. //Scan chunks looking for palette, transparency and image data
  3499. $pal='';
  3500. $trns='';
  3501. $data='';
  3502. do {
  3503. $n=$this->_freadint($f);
  3504. $type=fread($f,4);
  3505. if($type=='PLTE') {
  3506. //Read palette
  3507. $pal=fread($f,$n);
  3508. fread($f,4);
  3509. }
  3510. elseif($type=='tRNS') {
  3511. //Read transparency info
  3512. $t=fread($f,$n);
  3513. if($ct==0) {
  3514. $trns=array(ord(substr($t,1,1)));
  3515. }
  3516. elseif($ct==2) {
  3517. $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1)));
  3518. }
  3519. else {
  3520. $pos=strpos($t,chr(0));
  3521. if($pos!==false) {
  3522. $trns=array($pos);
  3523. }
  3524. }
  3525. fread($f,4);
  3526. }
  3527. elseif($type=='IDAT') {
  3528. //Read image data block
  3529. $data.=fread($f,$n);
  3530. fread($f,4);
  3531. }
  3532. elseif($type=='IEND') {
  3533. break;
  3534. }
  3535. else {
  3536. fread($f,$n+4);
  3537. }
  3538. }
  3539. while($n);
  3540. if($colspace=='Indexed' and empty($pal)) {
  3541. $this->Error('Missing palette in '.$file);
  3542. }
  3543. fclose($f);
  3544. return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
  3545. }
  3546. /**
  3547. * Convert the loaded php image to a JPEG and then return a structure for the PDF creator.
  3548. * @param string $file Image file name.
  3549. * @param image $image Image object.
  3550. * return image JPEG image object.
  3551. * @access protected
  3552. */
  3553. function toJPEG($file, $image) {
  3554. if ($image) {
  3555. // output
  3556. $tempname = tempnam(K_PATH_CACHE,'jpg');
  3557. imagejpeg($image, $tempname, 100);
  3558. imagedestroy($image);
  3559. $retvars = $this->outputjpg($tempname);
  3560. // tidy up by removing temporary image
  3561. unlink($tempname);
  3562. return $retvars;
  3563. } else {
  3564. $this->Error('Can\'t open image file: '.$file);
  3565. }
  3566. }
  3567. /**
  3568. * Get a JPEG filename and return a structure for the PDF creator.
  3569. * @param string $filename JPEG file name.
  3570. * @return array structure containing the image data
  3571. * @access protected
  3572. */
  3573. function outputjpg($filename) {
  3574. $a=getimagesize($filename);
  3575. if(!isset($a['channels']) or $a['channels']==3) {
  3576. $colspace='DeviceRGB';
  3577. }
  3578. elseif($a['channels']==4) {
  3579. $colspace='DeviceCMYK';
  3580. }
  3581. else {
  3582. $colspace='DeviceGray';
  3583. }
  3584. $bpc=isset($a['bits']) ? $a['bits'] : 8;
  3585. //Read whole file
  3586. $f=fopen($filename,'rb');
  3587. $data='';
  3588. while(!feof($f)) {
  3589. $data.=fread($f,4096);
  3590. }
  3591. fclose($f);
  3592. return array('w'=>$a[0],'h'=>$a[1],'cs'=>$colspace,'bpc'=>$bpc,'f'=>'DCTDecode','data'=>$data);
  3593. }
  3594. /// END OF REWRITE BY Warren Sherliker wsherliker@gmail.com
  3595. /**
  3596. * Read a 4-byte integer from file
  3597. * @param string $f file name.
  3598. * @return 4-byte integer
  3599. * @access protected
  3600. */
  3601. function _freadint($f) {
  3602. $a=unpack('Ni',fread($f,4));
  3603. return $a['i'];
  3604. }
  3605. /**
  3606. * Format a text string for meta information
  3607. * @param string $s string to escape.
  3608. * @return string escaped string.
  3609. * @access protected
  3610. */
  3611. function _textstring($s) {
  3612. if($this->isunicode) {
  3613. //Convert string to UTF-16BE
  3614. $s = $this->UTF8ToUTF16BE($s, true);
  3615. }
  3616. if ($this->encrypted) {
  3617. $s = $this->_RC4($this->_objectkey($this->n), $s);
  3618. }
  3619. return '('. $this->_escape($s).')';
  3620. }
  3621. /**
  3622. * Format an URI string
  3623. * @param string $s string to escape.
  3624. * @return string escaped string.
  3625. * @access protected
  3626. */
  3627. function _uristring($s) {
  3628. if ($this->encrypted) {
  3629. $s = $this->_RC4($this->_objectkey($this->n), $s);
  3630. }
  3631. return '('.$this->_escape($s).')';
  3632. }
  3633. /**
  3634. * Format a text string
  3635. * @param string $s string to escape.
  3636. * @return string escaped string.
  3637. * @access protected
  3638. */
  3639. function _escapetext($s) {
  3640. if($this->isunicode) {
  3641. //Convert string to UTF-16BE and reverse RTL language
  3642. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  3643. }
  3644. return $this->_escape($s);
  3645. }
  3646. /**
  3647. * Add \ before \, ( and )
  3648. * @param string $s string to escape.
  3649. * @return string escaped string.
  3650. * @access protected
  3651. */
  3652. function _escape($s) {
  3653. // the chr(13) substitution fixes the Bugs item #1421290.
  3654. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  3655. }
  3656. /**
  3657. * Output a stream.
  3658. * @param string $s string to output.
  3659. * @access protected
  3660. */
  3661. function _putstream($s) {
  3662. if ($this->encrypted) {
  3663. $s = $this->_RC4($this->_objectkey($this->n), $s);
  3664. }
  3665. $this->_out('stream');
  3666. $this->_out($s);
  3667. $this->_out('endstream');
  3668. }
  3669. /**
  3670. * Output a string to the document.
  3671. * @param string $s string to output.
  3672. * @access protected
  3673. */
  3674. function _out($s) {
  3675. if($this->state==2) {
  3676. $this->pages[$this->page] .= $s."\n";
  3677. }
  3678. else {
  3679. $this->buffer .= $s."\n";
  3680. }
  3681. }
  3682. /**
  3683. * Adds unicode fonts.<br>
  3684. * Based on PDF Reference 1.3 (section 5)
  3685. * @access protected
  3686. * @author Nicola Asuni
  3687. * @since 1.52.0.TC005 (2005-01-05)
  3688. */
  3689. function _puttruetypeunicode($font) {
  3690. // Type0 Font
  3691. // A composite font composed of other fonts, organized hierarchically
  3692. $this->_newobj();
  3693. $this->_out('<</Type /Font');
  3694. $this->_out('/Subtype /Type0');
  3695. $this->_out('/BaseFont /'.$font['name'].'');
  3696. $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.
  3697. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  3698. $this->_out('/ToUnicode '.($this->n + 2).' 0 R');
  3699. $this->_out('>>');
  3700. $this->_out('endobj');
  3701. // CIDFontType2
  3702. // A CIDFont whose glyph descriptions are based on TrueType font technology
  3703. $this->_newobj();
  3704. $this->_out('<</Type /Font');
  3705. $this->_out('/Subtype /CIDFontType2');
  3706. $this->_out('/BaseFont /'.$font['name'].'');
  3707. $this->_out('/CIDSystemInfo '.($this->n + 2).' 0 R');
  3708. $this->_out('/FontDescriptor '.($this->n + 3).' 0 R');
  3709. if (isset($font['desc']['MissingWidth'])){
  3710. $this->_out('/DW '.$font['desc']['MissingWidth'].''); // The default width for glyphs in the CIDFont MissingWidth
  3711. }
  3712. $w = "";
  3713. foreach ($font['cw'] as $cid => $width) {
  3714. $w .= ''.$cid.' ['.$width.'] '; // define a specific width for each individual CID
  3715. }
  3716. $this->_out('/W ['.$w.']'); // A description of the widths for the glyphs in the CIDFont
  3717. $this->_out('/CIDToGIDMap '.($this->n + 4).' 0 R');
  3718. $this->_out('>>');
  3719. $this->_out('endobj');
  3720. // ToUnicode
  3721. // is a stream object that contains the definition of the CMap
  3722. // (PDF Reference 1.3 chap. 5.9)
  3723. $this->_newobj();
  3724. $this->_out('<</Length 345>>');
  3725. $this->_out('stream');
  3726. $this->_out('/CIDInit /ProcSet findresource begin');
  3727. $this->_out('12 dict begin');
  3728. $this->_out('begincmap');
  3729. $this->_out('/CIDSystemInfo');
  3730. $this->_out('<</Registry (Adobe)');
  3731. $this->_out('/Ordering (UCS)');
  3732. $this->_out('/Supplement 0');
  3733. $this->_out('>> def');
  3734. $this->_out('/CMapName /Adobe-Identity-UCS def');
  3735. $this->_out('/CMapType 2 def');
  3736. $this->_out('1 begincodespacerange');
  3737. $this->_out('<0000> <FFFF>');
  3738. $this->_out('endcodespacerange');
  3739. $this->_out('1 beginbfrange');
  3740. $this->_out('<0000> <FFFF> <0000>');
  3741. $this->_out('endbfrange');
  3742. $this->_out('endcmap');
  3743. $this->_out('CMapName currentdict /CMap defineresource pop');
  3744. $this->_out('end');
  3745. $this->_out('end');
  3746. $this->_out('endstream');
  3747. $this->_out('endobj');
  3748. // CIDSystemInfo dictionary
  3749. // A dictionary containing entries that define the character collection of the CIDFont.
  3750. $this->_newobj();
  3751. $this->_out('<</Registry (Adobe)'); // A string identifying an issuer of character collections
  3752. $this->_out('/Ordering (UCS)'); // A string that uniquely names a character collection issued by a specific registry
  3753. $this->_out('/Supplement 0'); // The supplement number of the character collection.
  3754. $this->_out('>>');
  3755. $this->_out('endobj');
  3756. // Font descriptor
  3757. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  3758. $this->_newobj();
  3759. $this->_out('<</Type /FontDescriptor');
  3760. $this->_out('/FontName /'.$font['name']);
  3761. foreach ($font['desc'] as $key => $value) {
  3762. $this->_out('/'.$key.' '.$value);
  3763. }
  3764. if ($font['file']) {
  3765. // A stream containing a TrueType font program
  3766. $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
  3767. }
  3768. $this->_out('>>');
  3769. $this->_out('endobj');
  3770. // Embed CIDToGIDMap
  3771. // A specification of the mapping from CIDs to glyph indices
  3772. $this->_newobj();
  3773. $ctgfile = $this->_getfontpath().strtolower($font['ctg']);
  3774. if(!file_exists($ctgfile)) {
  3775. $this->Error('Font file not found: '.$ctgfile);
  3776. }
  3777. $size = filesize($ctgfile);
  3778. $this->_out('<</Length '.$size.'');
  3779. if(substr($ctgfile, -2) == '.z') { // check file extension
  3780. /* Decompresses data encoded using the public-domain
  3781. zlib/deflate compression method, reproducing the
  3782. original text or binary data */
  3783. $this->_out('/Filter /FlateDecode');
  3784. }
  3785. $this->_out('>>');
  3786. $this->_putstream(file_get_contents($ctgfile));
  3787. $this->_out('endobj');
  3788. }
  3789. /**
  3790. * Converts UTF-8 strings to codepoints array.<br>
  3791. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  3792. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  3793. * <pre>
  3794. * Char. number range | UTF-8 octet sequence
  3795. * (hexadecimal) | (binary)
  3796. * --------------------+-----------------------------------------------
  3797. * 0000 0000-0000 007F | 0xxxxxxx
  3798. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  3799. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  3800. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  3801. * ---------------------------------------------------------------------
  3802. *
  3803. * ABFN notation:
  3804. * ---------------------------------------------------------------------
  3805. * UTF8-octets = *( UTF8-char )
  3806. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  3807. * UTF8-1 = %x00-7F
  3808. * UTF8-2 = %xC2-DF UTF8-tail
  3809. *
  3810. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  3811. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  3812. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  3813. * %xF4 %x80-8F 2( UTF8-tail )
  3814. * UTF8-tail = %x80-BF
  3815. * ---------------------------------------------------------------------
  3816. * </pre>
  3817. * @param string $str string to process.
  3818. * @return array containing codepoints (UTF-8 characters values)
  3819. * @access protected
  3820. * @author Nicola Asuni
  3821. * @since 1.53.0.TC005 (2005-01-05)
  3822. */
  3823. function UTF8StringToArray($str) {
  3824. if(!$this->isunicode) {
  3825. // split string into array of chars
  3826. $strarr = str_split($str);
  3827. // convert chars to equivalent code
  3828. while(list($pos,$char)=each($strarr)) {
  3829. $strarr[$pos] = ord($char);
  3830. }
  3831. return $strarr;
  3832. }
  3833. $unicode = array(); // array containing unicode values
  3834. $bytes = array(); // array containing single character byte sequences
  3835. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  3836. $str .= ""; // force $str to be a string
  3837. $length = strlen($str);
  3838. for($i = 0; $i < $length; $i++) {
  3839. $char = ord($str{$i}); // get one string character at time
  3840. if(count($bytes) == 0) { // get starting octect
  3841. if ($char <= 0x7F) {
  3842. $unicode[] = $char; // use the character "as is" because is ASCII
  3843. $numbytes = 1;
  3844. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  3845. $bytes[] = ($char - 0xC0) << 0x06;
  3846. $numbytes = 2;
  3847. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  3848. $bytes[] = ($char - 0xE0) << 0x0C;
  3849. $numbytes = 3;
  3850. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  3851. $bytes[] = ($char - 0xF0) << 0x12;
  3852. $numbytes = 4;
  3853. } else {
  3854. // use replacement character for other invalid sequences
  3855. $unicode[] = 0xFFFD;
  3856. $bytes = array();
  3857. $numbytes = 1;
  3858. }
  3859. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  3860. $bytes[] = $char - 0x80;
  3861. if (count($bytes) == $numbytes) {
  3862. // compose UTF-8 bytes to a single unicode value
  3863. $char = $bytes[0];
  3864. for($j = 1; $j < $numbytes; $j++) {
  3865. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  3866. }
  3867. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  3868. /* The definition of UTF-8 prohibits encoding character numbers between
  3869. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  3870. encoding form (as surrogate pairs) and do not directly represent
  3871. characters. */
  3872. $unicode[] = 0xFFFD; // use replacement character
  3873. }
  3874. else {
  3875. $unicode[] = $char; // add char to array
  3876. }
  3877. // reset data for next char
  3878. $bytes = array();
  3879. $numbytes = 1;
  3880. }
  3881. } else {
  3882. // use replacement character for other invalid sequences
  3883. $unicode[] = 0xFFFD;
  3884. $bytes = array();
  3885. $numbytes = 1;
  3886. }
  3887. }
  3888. return $unicode;
  3889. }
  3890. /**
  3891. * Converts UTF-8 strings to UTF16-BE.<br>
  3892. * @param string $str string to process.
  3893. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  3894. * @return string
  3895. * @access protected
  3896. * @author Nicola Asuni
  3897. * @since 1.53.0.TC005 (2005-01-05)
  3898. * @uses UTF8StringToArray(), arrUTF8ToUTF16BE()
  3899. */
  3900. function UTF8ToUTF16BE($str, $setbom=true) {
  3901. if(!$this->isunicode) {
  3902. return $str; // string is not in unicode
  3903. }
  3904. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  3905. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  3906. }
  3907. /**
  3908. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  3909. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  3910. * <pre>
  3911. * Encoding UTF-16:
  3912. *
  3913. * Encoding of a single character from an ISO 10646 character value to
  3914. * UTF-16 proceeds as follows. Let U be the character number, no greater
  3915. * than 0x10FFFF.
  3916. *
  3917. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  3918. * terminate.
  3919. *
  3920. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  3921. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  3922. * represented in 20 bits.
  3923. *
  3924. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  3925. * 0xDC00, respectively. These integers each have 10 bits free to
  3926. * encode the character value, for a total of 20 bits.
  3927. *
  3928. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  3929. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  3930. * bits of W2. Terminate.
  3931. *
  3932. * Graphically, steps 2 through 4 look like:
  3933. * U' = yyyyyyyyyyxxxxxxxxxx
  3934. * W1 = 110110yyyyyyyyyy
  3935. * W2 = 110111xxxxxxxxxx
  3936. * </pre>
  3937. * @param array $unicode array containing UTF-8 unicode values
  3938. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  3939. * @return string
  3940. * @access protected
  3941. * @author Nicola Asuni
  3942. * @since 2.1.000 (2008-01-08)
  3943. * @see UTF8ToUTF16BE()
  3944. */
  3945. function arrUTF8ToUTF16BE($unicode, $setbom=true) {
  3946. $outstr = ""; // string to be returned
  3947. if ($setbom) {
  3948. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  3949. }
  3950. foreach($unicode as $char) {
  3951. if($char == 0xFFFD) {
  3952. $outstr .= "\xFF\xFD"; // replacement character
  3953. } elseif ($char < 0x10000) {
  3954. $outstr .= chr($char >> 0x08);
  3955. $outstr .= chr($char & 0xFF);
  3956. } else {
  3957. $char -= 0x10000;
  3958. $w1 = 0xD800 | ($char >> 0x10);
  3959. $w2 = 0xDC00 | ($char & 0x3FF);
  3960. $outstr .= chr($w1 >> 0x08);
  3961. $outstr .= chr($w1 & 0xFF);
  3962. $outstr .= chr($w2 >> 0x08);
  3963. $outstr .= chr($w2 & 0xFF);
  3964. }
  3965. }
  3966. return $outstr;
  3967. }
  3968. // ====================================================
  3969. /**
  3970. * Set header font.
  3971. * @param array $font font
  3972. * @since 1.1
  3973. */
  3974. function setHeaderFont($font) {
  3975. $this->header_font = $font;
  3976. }
  3977. /**
  3978. * Set footer font.
  3979. * @param array $font font
  3980. * @since 1.1
  3981. */
  3982. function setFooterFont($font) {
  3983. $this->footer_font = $font;
  3984. }
  3985. /**
  3986. * Set language array.
  3987. * @param array $language
  3988. * @since 1.1
  3989. */
  3990. function setLanguageArray($language) {
  3991. $this->l = $language;
  3992. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  3993. }
  3994. /**
  3995. * Set document barcode.
  3996. * @param string $bc barcode
  3997. */
  3998. function setBarcode($bc="") {
  3999. $this->barcode = $bc;
  4000. }
  4001. /**
  4002. * Print Barcode.
  4003. * @param int $x x position in user units
  4004. * @param int $y y position in user units
  4005. * @param int $w width in user units
  4006. * @param int $h height position in user units
  4007. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  4008. * @param string $style barcode style
  4009. * @param string $font font for text
  4010. * @param int $xres x resolution
  4011. * @param string $code code to print
  4012. */
  4013. function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  4014. require_once(dirname(__FILE__)."/barcode/barcode.php");
  4015. require_once(dirname(__FILE__)."/barcode/i25object.php");
  4016. require_once(dirname(__FILE__)."/barcode/c39object.php");
  4017. require_once(dirname(__FILE__)."/barcode/c128aobject.php");
  4018. require_once(dirname(__FILE__)."/barcode/c128bobject.php");
  4019. require_once(dirname(__FILE__)."/barcode/c128cobject.php");
  4020. if (empty($code)) {
  4021. return;
  4022. }
  4023. if (empty($style)) {
  4024. $style = BCS_ALIGN_LEFT;
  4025. $style |= BCS_IMAGE_PNG;
  4026. $style |= BCS_TRANSPARENT;
  4027. //$style |= BCS_BORDER;
  4028. //$style |= BCS_DRAW_TEXT;
  4029. //$style |= BCS_STRETCH_TEXT;
  4030. //$style |= BCS_REVERSE_COLOR;
  4031. }
  4032. if (empty($font)) {$font = BCD_DEFAULT_FONT;}
  4033. if (empty($xres)) {$xres = BCD_DEFAULT_XRES;}
  4034. $scale_factor = 1.5 * $xres * $this->k;
  4035. $bc_w = round($w * $scale_factor); //width in points
  4036. $bc_h = round($h * $scale_factor); //height in points
  4037. switch (strtoupper($type)) {
  4038. case "I25": {
  4039. $obj = new I25Object($bc_w, $bc_h, $style, $code);
  4040. break;
  4041. }
  4042. case "C128A": {
  4043. $obj = new C128AObject($bc_w, $bc_h, $style, $code);
  4044. break;
  4045. }
  4046. default:
  4047. case "C128B": {
  4048. $obj = new C128BObject($bc_w, $bc_h, $style, $code);
  4049. break;
  4050. }
  4051. case "C128C": {
  4052. $obj = new C128CObject($bc_w, $bc_h, $style, $code);
  4053. break;
  4054. }
  4055. case "C39": {
  4056. $obj = new C39Object($bc_w, $bc_h, $style, $code);
  4057. break;
  4058. }
  4059. }
  4060. $obj->SetFont($font);
  4061. $obj->DrawObject($xres);
  4062. //use a temporary file....
  4063. $tmpName = tempnam(K_PATH_CACHE,'img');
  4064. imagepng($obj->getImage(), $tmpName);
  4065. $this->Image($tmpName, $x, $y, $w, $h, 'png');
  4066. $obj->DestroyObject();
  4067. unset($obj);
  4068. unlink($tmpName);
  4069. }
  4070. /**
  4071. * Returns the PDF data.
  4072. */
  4073. function getPDFData() {
  4074. if($this->state < 3) {
  4075. $this->Close();
  4076. }
  4077. return $this->buffer;
  4078. }
  4079. // --- HTML PARSER FUNCTIONS ---
  4080. /**
  4081. * Allows to preserve some HTML formatting.<br />
  4082. * 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
  4083. * @param string $html text to display
  4084. * @param boolean $ln if true add a new line after text (default = true)
  4085. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  4086. * @param boolean $reseth if true reset the last cell height (default false).
  4087. * @param boolean $cell if true add the default cMargin space to each Write (default false).
  4088. */
  4089. function writeHTML($html, $ln=true, $fill=0, $reseth=false, $cell=false) {
  4090. // store some variables
  4091. $html=strip_tags($html,"<h1><h2><h3><h4><h5><h6><b><u><i><a><img><p><br><br/><strong><em><font><blockquote><li><ul><ol><hr><td><th><tr><table><sup><sub><small><span><div>"); //remove all unsupported tags
  4092. //replace carriage returns, newlines and tabs
  4093. $repTable = array("\t" => " ", "\n" => " ", "\r" => " ", "\0" => " ", "\x0B" => " ");
  4094. $html = strtr($html, $repTable);
  4095. $pattern = '/(<[^>]+>)/Uu';
  4096. $a = preg_split($pattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); //explodes the string
  4097. if ((empty($this->lasth))OR ($reseth)) {
  4098. //set row height
  4099. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4100. }
  4101. foreach($a as $key=>$element) {
  4102. if (!preg_match($pattern, $element)) {
  4103. //Text
  4104. if($this->HREF) {
  4105. $this->addHtmlLink($this->HREF, $element, $fill);
  4106. } elseif($this->tdbegin) {
  4107. if((strlen(trim($element)) > 0) AND ($element != "&nbsp;")) {
  4108. $this->Cell($this->tdwidth, $this->tdheight, $this->unhtmlentities($element), $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
  4109. } elseif($element == "&nbsp;") {
  4110. $this->Cell($this->tdwidth, $this->tdheight, '', $this->tableborder, '', $this->tdalign, $this->tdbgcolor);
  4111. }
  4112. } else {
  4113. $ctmpmargin = $this->cMargin;
  4114. if(!$cell) {
  4115. $this->cMargin = 0;
  4116. }
  4117. $this->Write($this->lasth, stripslashes($this->unhtmlentities($element)), '', $fill, '', false, 0);
  4118. $this->cMargin = $ctmpmargin;
  4119. }
  4120. } else {
  4121. $element = substr($element, 1, -1);
  4122. //Tag
  4123. if($element{0}=='/') {
  4124. $this->closedHTMLTagHandler(strtolower(substr($element, 1)));
  4125. }
  4126. else {
  4127. //Extract attributes
  4128. // get tag name
  4129. preg_match('/([a-zA-Z0-9]*)/', $element, $tag);
  4130. $tag = strtolower($tag[0]);
  4131. // get attributes
  4132. preg_match_all('/([^=\s]*)=["\']?([^"\']*)["\']?/', $element, $attr_array, PREG_PATTERN_ORDER);
  4133. $attr = array(); // reset attribute array
  4134. while(list($id,$name)=each($attr_array[1])) {
  4135. $attr[strtolower($name)] = $attr_array[2][$id];
  4136. }
  4137. $this->openHTMLTagHandler($tag, $attr, $fill);
  4138. }
  4139. }
  4140. }
  4141. if ($ln) {
  4142. $this->Ln($this->lasth);
  4143. }
  4144. }
  4145. /**
  4146. * 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 />
  4147. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4148. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  4149. * @param float $h Cell minimum height. The cell extends automatically if needed.
  4150. * @param float $x upper-left corner X coordinate
  4151. * @param float $y upper-left corner Y coordinate
  4152. * @param string $html html text to print. Default value: empty string.
  4153. * @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>
  4154. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL language)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>
  4155. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  4156. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  4157. * @param boolean $reseth if true reset the last cell height (default true).
  4158. * @see Cell()
  4159. */
  4160. function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true) {
  4161. if ((empty($this->lasth))OR ($reseth)) {
  4162. //set row height
  4163. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4164. }
  4165. // get current page number
  4166. $startpage = $this->page;
  4167. if (!empty($y)) {
  4168. $this->SetY($y);
  4169. } else {
  4170. $y = $this->GetY();
  4171. }
  4172. if (!empty($x)) {
  4173. $this->SetX($x);
  4174. } else {
  4175. $x = $this->GetX();
  4176. }
  4177. if(empty($w)) {
  4178. if ($this->rtl) {
  4179. $w = $this->x - $this->lMargin;
  4180. } else {
  4181. $w = $this->w - $this->rMargin - $this->x;
  4182. }
  4183. }
  4184. // store original margin values
  4185. $lMargin = $this->lMargin;
  4186. $rMargin = $this->rMargin;
  4187. // set new margin values
  4188. if ($this->rtl) {
  4189. $this->SetLeftMargin($this->x - $w);
  4190. $this->SetRightMargin($this->w - $this->x);
  4191. } else {
  4192. $this->SetLeftMargin($this->x);
  4193. $this->SetRightMargin($this->w - $this->x - $w);
  4194. }
  4195. // calculate remaining vertical space on first page ($startpage)
  4196. $restspace = $this->getPageHeight() - $this->GetY() - $this->getBreakMargin();
  4197. // Write HTML text
  4198. $this->writeHTML($html, true, $fill, $reseth, true);
  4199. // Get end-of-text Y position
  4200. $currentY = $this->GetY();
  4201. // get latest page number
  4202. $endpage = $this->page;
  4203. if (!empty($border)) {
  4204. // check if a new page has been created
  4205. if ($endpage > $startpage) {
  4206. // design borders around HTML cells.
  4207. for ($page=$startpage; $page<=$endpage; $page++) {
  4208. $this->page = $page;
  4209. if ($page==$startpage) {
  4210. $this->SetY($this->getPageHeight() - $restspace - $this->getBreakMargin());
  4211. $h = $restspace - 1;
  4212. } elseif ($page==$endpage) {
  4213. $this->SetY($this->tMargin); // put cursor at the beginning of text
  4214. $h = $currentY - $this->tMargin;
  4215. } else {
  4216. $this->SetY($this->tMargin); // put cursor at the beginning of text
  4217. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  4218. }
  4219. $this->SetX($x);
  4220. $this->Cell($w, $h, "", $border, 1, '', 0);
  4221. }
  4222. } else {
  4223. $h = max($h, ($currentY - $y));
  4224. $this->SetY($y); // put cursor at the beginning of text
  4225. $this->SetX($x);
  4226. // design a cell around the text
  4227. $this->Cell($w, $h, "", $border, 1, '', 0);
  4228. }
  4229. }
  4230. // restore original margin values
  4231. $this->SetLeftMargin($lMargin);
  4232. $this->SetRightMargin($rMargin);
  4233. if($ln>0) {
  4234. //Go to the beginning of the next line
  4235. $this->SetY($currentY);
  4236. if($ln == 2) {
  4237. $this->SetX($x + $w);
  4238. }
  4239. } else {
  4240. // go left or right by case
  4241. $this->page = $startpage;
  4242. $this->y = $y;
  4243. $this->SetX($x + $w);
  4244. }
  4245. }
  4246. /**
  4247. * Process opening tags.
  4248. * @param string $tag tag name (in uppercase)
  4249. * @param string $attr tag attribute (in uppercase)
  4250. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  4251. * @access private
  4252. */
  4253. function openHTMLTagHandler($tag, $attr, $fill=0) {
  4254. // check for text direction attribute
  4255. if (isset($attr['dir'])) {
  4256. $this->tmprtl = $attr['dir']=='rtl' ? 'R' : 'L';
  4257. } else {
  4258. $this->tmprtl = false;
  4259. }
  4260. //Opening tag
  4261. switch($tag) {
  4262. case 'table': {
  4263. if ((isset($attr['border'])) AND ($attr['border'] != '')) {
  4264. $this->tableborder = $attr['border'];
  4265. }
  4266. else {
  4267. $this->tableborder = 0;
  4268. }
  4269. break;
  4270. }
  4271. case 'tr': {
  4272. break;
  4273. }
  4274. case 'td':
  4275. case 'th': {
  4276. if ((isset($attr['width'])) AND ($attr['width'] != '')) {
  4277. $this->tdwidth = ($attr['width']/4);
  4278. }
  4279. else {
  4280. $this->tdwidth = (($this->w - $this->lMargin - $this->rMargin) / $this->default_table_columns);
  4281. }
  4282. if ((isset($attr['height'])) AND ($attr['height'] != '')) {
  4283. $this->tdheight=($attr['height'] / $this->k);
  4284. }
  4285. else {
  4286. $this->tdheight = $this->lasth;
  4287. }
  4288. if ((isset($attr['align'])) AND ($attr['align'] != '')) {
  4289. switch ($attr['align']) {
  4290. case 'center': {
  4291. $this->tdalign = "C";
  4292. break;
  4293. }
  4294. case 'right': {
  4295. $this->tdalign = "R";
  4296. break;
  4297. }
  4298. default:
  4299. case 'left': {
  4300. $this->tdalign = "L";
  4301. break;
  4302. }
  4303. }
  4304. } else {
  4305. if($this->rtl) {
  4306. $this->tdalign = "R";
  4307. } else {
  4308. $this->tdalign = "L";
  4309. }
  4310. }
  4311. if ((isset($attr['bgcolor'])) AND ($attr['bgcolor'] != '')) {
  4312. $coul = $this->convertColorHexToDec($attr['bgcolor']);
  4313. $this->SetFillColor($coul['R'], $coul['G'], $coul['B']);
  4314. $this->tdbgcolor=true;
  4315. }
  4316. $this->tdbegin=true;
  4317. break;
  4318. }
  4319. case 'hr': {
  4320. $this->Ln();
  4321. if ((isset($attr['width'])) AND ($attr['width'] != '')) {
  4322. $hrWidth = $attr['width'];
  4323. }
  4324. else {
  4325. $hrWidth = $this->w - $this->lMargin - $this->rMargin;
  4326. }
  4327. $x = $this->GetX();
  4328. $y = $this->GetY();
  4329. $this->GetLineWidth();
  4330. $prevlinewidth = $this->SetLineWidth(0.2);
  4331. $this->Line($x, $y, $x + $hrWidth, $y);
  4332. $this->SetLineWidth($prevlinewidth);
  4333. $this->Ln();
  4334. break;
  4335. }
  4336. case 'strong': {
  4337. $this->setStyle('b', true);
  4338. break;
  4339. }
  4340. case 'em': {
  4341. $this->setStyle('i', true);
  4342. break;
  4343. }
  4344. case 'b':
  4345. case 'i':
  4346. case 'u': {
  4347. $this->setStyle($tag, true);
  4348. break;
  4349. }
  4350. case 'a': {
  4351. $this->HREF = $attr['href'];
  4352. break;
  4353. }
  4354. case 'img': {
  4355. if(isset($attr['src'])) {
  4356. // replace relative path with real server path
  4357. if ($attr['src'][0] == '/') {
  4358. $attr['src'] = $_SERVER['DOCUMENT_ROOT'].$attr['src'];
  4359. }
  4360. $attr['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $attr['src']);
  4361. if(!isset($attr['width'])) {
  4362. $attr['width'] = 0;
  4363. }
  4364. if(!isset($attr['height'])) {
  4365. $attr['height'] = 0;
  4366. }
  4367. if(!isset($attr['align'])) {
  4368. $align = 'N';
  4369. } else {
  4370. switch($attr['align']) {
  4371. case 'top':{
  4372. $align = 'T';
  4373. break;
  4374. }
  4375. case 'middle':{
  4376. $align = 'M';
  4377. break;
  4378. }
  4379. case 'bottom':{
  4380. $align = 'B';
  4381. break;
  4382. }
  4383. default:{
  4384. $align = 'N';
  4385. break;
  4386. }
  4387. }
  4388. }
  4389. $this->Image($attr['src'], $this->GetX(),$this->GetY(), $this->pixelsToMillimeters($attr['width']), $this->pixelsToMillimeters($attr['height']), '', '', $align);
  4390. }
  4391. break;
  4392. }
  4393. case 'ul': {
  4394. $this->listordered = false;
  4395. $this->listcount = 0;
  4396. break;
  4397. }
  4398. case 'ol': {
  4399. $this->listordered = true;
  4400. $this->listcount = 0;
  4401. break;
  4402. }
  4403. case 'li': {
  4404. $this->Ln();
  4405. if ($this->listordered) {
  4406. if (isset($attr['value'])) {
  4407. $this->listcount = intval($attr['value']);
  4408. }
  4409. $this->lispacer = " ".(++$this->listcount).". ";
  4410. } else {
  4411. //unordered list simbol
  4412. $this->lispacer = " - ";
  4413. }
  4414. $rtldir = $this->tmprtl;
  4415. $this->tmprtl = false;
  4416. $this->Write($this->lasth, $this->lispacer, '', $fill, '', false, 0);
  4417. $this->tmprtl = $rtldir;
  4418. break;
  4419. }
  4420. case 'blockquote':
  4421. case 'br': {
  4422. $this->Ln();
  4423. if(strlen($this->lispacer) > 0) {
  4424. if ($this->rtl) {
  4425. $this->x -= $this->GetStringWidth($this->lispacer);
  4426. } else {
  4427. $this->x += $this->GetStringWidth($this->lispacer);
  4428. }
  4429. }
  4430. break;
  4431. }
  4432. case 'p': {
  4433. $this->Ln();
  4434. $this->Ln();
  4435. break;
  4436. }
  4437. case 'sup': {
  4438. $currentFontSize = $this->FontSize;
  4439. $this->tempfontsize = $this->FontSizePt;
  4440. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  4441. $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  4442. break;
  4443. }
  4444. case 'sub': {
  4445. $currentFontSize = $this->FontSize;
  4446. $this->tempfontsize = $this->FontSizePt;
  4447. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  4448. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  4449. break;
  4450. }
  4451. case 'small': {
  4452. $currentFontSize = $this->FontSize;
  4453. $this->tempfontsize = $this->FontSizePt;
  4454. $this->SetFontSize($this->FontSizePt * K_SMALL_RATIO);
  4455. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)/3));
  4456. break;
  4457. }
  4458. case 'font': {
  4459. if (isset($attr['color']) AND $attr['color']!='') {
  4460. $coul = $this->convertColorHexToDec($attr['color']);
  4461. $this->SetTextColor($coul['R'],$coul['G'],$coul['B']);
  4462. $this->issetcolor=true;
  4463. }
  4464. if (isset($attr['face']) and in_array(strtolower($attr['face']), $this->fontlist)) {
  4465. $this->SetFont(strtolower($attr['face']));
  4466. $this->issetfont=true;
  4467. }
  4468. if (isset($attr['size'])) {
  4469. $headsize = intval($attr['size']);
  4470. } else {
  4471. $headsize = 0;
  4472. }
  4473. $currentFontSize = $this->FontSize;
  4474. $this->tempfontsize = $this->FontSizePt;
  4475. $this->SetFontSize($this->FontSizePt + $headsize);
  4476. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4477. break;
  4478. }
  4479. case 'h1':
  4480. case 'h2':
  4481. case 'h3':
  4482. case 'h4':
  4483. case 'h5':
  4484. case 'h6': {
  4485. $headsize = (4 - substr($tag, 1)) * 2;
  4486. $currentFontSize = $this->FontSize;
  4487. $this->tempfontsize = $this->FontSizePt;
  4488. $this->SetFontSize($this->FontSizePt + $headsize);
  4489. $this->setStyle('b', true);
  4490. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4491. break;
  4492. }
  4493. }
  4494. }
  4495. /**
  4496. * Process closing tags.
  4497. * @param string $tag tag name (in uppercase)
  4498. * @access private
  4499. */
  4500. function closedHTMLTagHandler($tag) {
  4501. //Closing tag
  4502. switch($tag) {
  4503. case 'td':
  4504. case 'th': {
  4505. $this->tdbegin = false;
  4506. $this->tdwidth = 0;
  4507. $this->tdheight = 0;
  4508. if($this->rtl) {
  4509. $this->tdalign = "R";
  4510. } else {
  4511. $this->tdalign = "L";
  4512. }
  4513. $this->tdbgcolor = false;
  4514. $this->SetFillColor($this->prevFillColor[0], $this->prevFillColor[1], $this->prevFillColor[2]);
  4515. break;
  4516. }
  4517. case 'tr': {
  4518. $this->Ln();
  4519. break;
  4520. }
  4521. case 'table': {
  4522. $this->tableborder=0;
  4523. break;
  4524. }
  4525. case 'strong': {
  4526. $this->setStyle('b', false);
  4527. break;
  4528. }
  4529. case 'em': {
  4530. $this->setStyle('i', false);
  4531. break;
  4532. }
  4533. case 'b':
  4534. case 'i':
  4535. case 'u': {
  4536. $this->setStyle($tag, false);
  4537. break;
  4538. }
  4539. case 'a': {
  4540. $this->HREF = '';
  4541. break;
  4542. }
  4543. case 'sup': {
  4544. $currentFontSize = $this->FontSize;
  4545. $this->SetFontSize($this->tempfontsize);
  4546. $this->tempfontsize = $this->FontSizePt;
  4547. $this->SetXY($this->GetX(), $this->GetY() - (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  4548. break;
  4549. }
  4550. case 'sub': {
  4551. $currentFontSize = $this->FontSize;
  4552. $this->SetFontSize($this->tempfontsize);
  4553. $this->tempfontsize = $this->FontSizePt;
  4554. $this->SetXY($this->GetX(), $this->GetY() + (($currentFontSize - $this->FontSize)*(K_SMALL_RATIO)));
  4555. break;
  4556. }
  4557. case 'small': {
  4558. $currentFontSize = $this->FontSize;
  4559. $this->SetFontSize($this->tempfontsize);
  4560. $this->tempfontsize = $this->FontSizePt;
  4561. $this->SetXY($this->GetX(), $this->GetY() - (($this->FontSize - $currentFontSize)/3));
  4562. break;
  4563. }
  4564. case 'font': {
  4565. if ($this->issetcolor == true) {
  4566. $this->SetTextColor($this->prevTextColor[0], $this->prevTextColor[1], $this->prevTextColor[2]);
  4567. }
  4568. if ($this->issetfont) {
  4569. $this->FontFamily = $this->prevFontFamily;
  4570. $this->FontStyle = $this->prevFontStyle;
  4571. $this->SetFont($this->FontFamily);
  4572. $this->issetfont = false;
  4573. }
  4574. $currentFontSize = $this->FontSize;
  4575. $this->SetFontSize($this->tempfontsize);
  4576. $this->tempfontsize = $this->FontSizePt;
  4577. //$this->TextColor = $this->prevTextColor;
  4578. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4579. break;
  4580. }
  4581. case 'p': {
  4582. $this->Ln();
  4583. $this->Ln();
  4584. break;
  4585. }
  4586. case 'ul':
  4587. case 'ol': {
  4588. $this->Ln();
  4589. $this->Ln();
  4590. break;
  4591. }
  4592. case 'li': {
  4593. $this->lispacer = "";
  4594. break;
  4595. }
  4596. case 'h1':
  4597. case 'h2':
  4598. case 'h3':
  4599. case 'h4':
  4600. case 'h5':
  4601. case 'h6': {
  4602. $currentFontSize = $this->FontSize;
  4603. $this->SetFontSize($this->tempfontsize);
  4604. $this->tempfontsize = $this->FontSizePt;
  4605. $this->setStyle('b', false);
  4606. $this->Ln();
  4607. $this->lasth = $this->FontSize * K_CELL_HEIGHT_RATIO;
  4608. break;
  4609. }
  4610. default : {
  4611. break;
  4612. }
  4613. }
  4614. $this->tmprtl = false;
  4615. }
  4616. /**
  4617. * Sets font style.
  4618. * @param string $tag tag name (in lowercase)
  4619. * @param boolean $enable
  4620. * @access private
  4621. */
  4622. function setStyle($tag, $enable) {
  4623. //Modify style and select corresponding font
  4624. $this->$tag += ($enable ? 1 : -1);
  4625. $style='';
  4626. foreach(array('b', 'i', 'u') as $s) {
  4627. if($this->$s > 0) {
  4628. $style .= $s;
  4629. }
  4630. }
  4631. $this->SetFont('', $style);
  4632. }
  4633. /**
  4634. * Output anchor link.
  4635. * @param string $url link URL
  4636. * @param string $name link name
  4637. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  4638. * @access public
  4639. */
  4640. function addHtmlLink($url, $name, $fill=0) {
  4641. //Put a hyperlink
  4642. $this->SetTextColor(0, 0, 255);
  4643. $this->setStyle('u', true);
  4644. $this->Write($this->lasth, $name, $url, $fill, '', false, 0);
  4645. $this->setStyle('u', false);
  4646. $this->SetTextColor(0);
  4647. }
  4648. /**
  4649. * Returns an associative array (keys: R,G,B) from
  4650. * a hex html code (e.g. #3FE5AA).
  4651. * @param string $color hexadecimal html color [#rrggbb]
  4652. * @return array
  4653. * @access private
  4654. */
  4655. function convertColorHexToDec($color = "#000000"){
  4656. $tbl_color = array();
  4657. $tbl_color['R'] = hexdec(substr($color, 1, 2));
  4658. $tbl_color['G'] = hexdec(substr($color, 3, 2));
  4659. $tbl_color['B'] = hexdec(substr($color, 5, 2));
  4660. return $tbl_color;
  4661. }
  4662. /**
  4663. * Converts pixels to millimeters in 72 dpi.
  4664. * @param int $px pixels
  4665. * @return float millimeters
  4666. * @access private
  4667. */
  4668. function pixelsToMillimeters($px){
  4669. return $px * 25.4 / 72;
  4670. }
  4671. /**
  4672. * Reverse function for htmlentities.
  4673. * Convert entities in UTF-8.
  4674. *
  4675. * @param $text_to_convert Text to convert.
  4676. * @return string converted
  4677. */
  4678. function unhtmlentities($text_to_convert) {
  4679. if (!$this->isunicode) {
  4680. return html_entity_decode($text_to_convert);
  4681. }
  4682. require_once(dirname(__FILE__).'/html_entity_decode_php4.php');
  4683. return html_entity_decode_php4($text_to_convert);
  4684. }
  4685. // ENCRYPTION METHODS ----------------------------------
  4686. // SINCE 2.0.000 (2008-01-02)
  4687. /**
  4688. * Compute encryption key depending on object number where the encrypted data is stored
  4689. * @param int $n object number
  4690. * @since 2.0.000 (2008-01-02)
  4691. */
  4692. function _objectkey($n) {
  4693. return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10);
  4694. }
  4695. /**
  4696. * Put encryption on PDF document
  4697. * @since 2.0.000 (2008-01-02)
  4698. */
  4699. function _putencryption() {
  4700. $this->_out('/Filter /Standard');
  4701. $this->_out('/V 1');
  4702. $this->_out('/R 2');
  4703. $this->_out('/O ('.$this->_escape($this->Ovalue).')');
  4704. $this->_out('/U ('.$this->_escape($this->Uvalue).')');
  4705. $this->_out('/P '.$this->Pvalue);
  4706. }
  4707. /**
  4708. * Returns the input text exrypted using RC4 algorithm and the specified key.
  4709. * RC4 is the standard encryption algorithm used in PDF format
  4710. * @param string $key encryption key
  4711. * @param String $text input text to be encrypted
  4712. * @return String encrypted text
  4713. * @since 2.0.000 (2008-01-02)
  4714. * @author Klemen Vodopivec
  4715. */
  4716. function _RC4($key, $text) {
  4717. if ($this->last_rc4_key != $key) {
  4718. $k = str_repeat($key, 256/strlen($key)+1);
  4719. $rc4 = range(0,255);
  4720. $j = 0;
  4721. for ($i=0; $i<256; $i++) {
  4722. $t = $rc4[$i];
  4723. $j = ($j + $t + ord($k{$i})) % 256;
  4724. $rc4[$i] = $rc4[$j];
  4725. $rc4[$j] = $t;
  4726. }
  4727. $this->last_rc4_key = $key;
  4728. $this->last_rc4_key_c = $rc4;
  4729. } else {
  4730. $rc4 = $this->last_rc4_key_c;
  4731. }
  4732. $len = strlen($text);
  4733. $a = 0;
  4734. $b = 0;
  4735. $out = '';
  4736. for ($i=0; $i<$len; $i++) {
  4737. $a = ($a+1)%256;
  4738. $t= $rc4[$a];
  4739. $b = ($b+$t)%256;
  4740. $rc4[$a] = $rc4[$b];
  4741. $rc4[$b] = $t;
  4742. $k = $rc4[($rc4[$a]+$rc4[$b])%256];
  4743. $out.=chr(ord($text{$i}) ^ $k);
  4744. }
  4745. return $out;
  4746. }
  4747. /**
  4748. * Encrypts a string using MD5 and returns it's value as a binary string.
  4749. * @param string $str input string
  4750. * @return String MD5 encrypted binary string
  4751. * @since 2.0.000 (2008-01-02)
  4752. * @author Klemen Vodopivec
  4753. */
  4754. function _md5_16($str) {
  4755. return pack('H*',md5($str));
  4756. }
  4757. /**
  4758. * Compute O value (used for RC4 encryption)
  4759. * @param String $user_pass user password
  4760. * @param String $owner_pass user password
  4761. * @return String O value
  4762. * @since 2.0.000 (2008-01-02)
  4763. * @author Klemen Vodopivec
  4764. */
  4765. function _Ovalue($user_pass, $owner_pass) {
  4766. $tmp = $this->_md5_16($owner_pass);
  4767. $owner_RC4_key = substr($tmp,0,5);
  4768. return $this->_RC4($owner_RC4_key, $user_pass);
  4769. }
  4770. /**
  4771. * Compute U value (used for RC4 encryption)
  4772. * @return String U value
  4773. * @since 2.0.000 (2008-01-02)
  4774. * @author Klemen Vodopivec
  4775. */
  4776. function _Uvalue() {
  4777. return $this->_RC4($this->encryption_key, $this->padding);
  4778. }
  4779. /**
  4780. * Compute encryption key
  4781. * @param String $user_pass user password
  4782. * @param String $owner_pass user password
  4783. * @param String $protection protection type
  4784. * @since 2.0.000 (2008-01-02)
  4785. * @author Klemen Vodopivec
  4786. */
  4787. function _generateencryptionkey($user_pass, $owner_pass, $protection) {
  4788. // Pad passwords
  4789. $user_pass = substr($user_pass.$this->padding,0,32);
  4790. $owner_pass = substr($owner_pass.$this->padding,0,32);
  4791. // Compute O value
  4792. $this->Ovalue = $this->_Ovalue($user_pass,$owner_pass);
  4793. // Compute encyption key
  4794. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  4795. $this->encryption_key = substr($tmp,0,5);
  4796. // Compute U value
  4797. $this->Uvalue = $this->_Uvalue();
  4798. // Compute P value
  4799. $this->Pvalue = -(($protection^255)+1);
  4800. }
  4801. /**
  4802. * Set document protection
  4803. * The permission array is composed of values taken from the following ones:
  4804. * - copy: copy text and images to the clipboard
  4805. * - print: print the document
  4806. * - modify: modify it (except for annotations and forms)
  4807. * - annot-forms: add annotations and forms
  4808. * Remark: the protection against modification is for people who have the full Acrobat product.
  4809. * If you don't set any password, the document will open as usual. If you set a user password, the PDF viewer will ask for it before displaying the document. The master password, if different from the user one, can be used to get full access.
  4810. * Note: protecting a document requires to encrypt it, which increases the processing time a lot. This can cause a PHP time-out in some cases, especially if the document contains images or fonts.
  4811. * @param Array $permissions the set of permissions. Empty by default (only viewing is allowed). (print, modify, copy, annot-forms)
  4812. * @param String $user_pass user password. Empty by default.
  4813. * @param String $owner_pass owner password. If not specified, a random value is used.
  4814. * @since 2.0.000 (2008-01-02)
  4815. * @author Klemen Vodopivec
  4816. */
  4817. function SetProtection($permissions=array(),$user_pass='',$owner_pass=null) {
  4818. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
  4819. $protection = 192;
  4820. foreach($permissions as $permission) {
  4821. if (!isset($options[$permission])) {
  4822. $this->Error('Incorrect permission: '.$permission);
  4823. }
  4824. $protection += $options[$permission];
  4825. }
  4826. if ($owner_pass === null) {
  4827. $owner_pass = uniqid(rand());
  4828. }
  4829. $this->encrypted = true;
  4830. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  4831. }
  4832. // END OF ENCRYPTION FUNCTIONS -------------------------
  4833. // START TRANSFORMATIONS SECTION -----------------------
  4834. // authors: Moritz Wagner, Andreas Wurmser, Nicola Asuni
  4835. /**
  4836. * Starts a 2D tranformation saving current graphic state.
  4837. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  4838. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  4839. * @since 2.1.000 (2008-01-07)
  4840. * @see StartTransform(), StopTransform()
  4841. */
  4842. function StartTransform() {
  4843. $this->_out('q');
  4844. }
  4845. /**
  4846. * Stops a 2D tranformation restoring previous graphic state.
  4847. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  4848. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  4849. * @since 2.1.000 (2008-01-07)
  4850. * @see StartTransform(), StopTransform()
  4851. */
  4852. function StopTransform() {
  4853. $this->_out('Q');
  4854. }
  4855. /**
  4856. * Horizontal Scaling.
  4857. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  4858. * @param int $x abscissa of the scaling center. Default is current x position
  4859. * @param int $y ordinate of the scaling center. Default is current y position
  4860. * @since 2.1.000 (2008-01-07)
  4861. * @see StartTransform(), StopTransform()
  4862. */
  4863. function ScaleX($s_x, $x='', $y=''){
  4864. $this->Scale($s_x, 100, $x, $y);
  4865. }
  4866. /**
  4867. * Vertical Scaling.
  4868. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  4869. * @param int $x abscissa of the scaling center. Default is current x position
  4870. * @param int $y ordinate of the scaling center. Default is current y position
  4871. * @since 2.1.000 (2008-01-07)
  4872. * @see StartTransform(), StopTransform()
  4873. */
  4874. function ScaleY($s_y, $x='', $y=''){
  4875. $this->Scale(100, $s_y, $x, $y);
  4876. }
  4877. /**
  4878. * Vertical and horizontal proportional Scaling.
  4879. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  4880. * @param int $x abscissa of the scaling center. Default is current x position
  4881. * @param int $y ordinate of the scaling center. Default is current y position
  4882. * @since 2.1.000 (2008-01-07)
  4883. * @see StartTransform(), StopTransform()
  4884. */
  4885. function ScaleXY($s, $x='', $y=''){
  4886. $this->Scale($s, $s, $x, $y);
  4887. }
  4888. /**
  4889. * Vertical and horizontal non-proportional Scaling.
  4890. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  4891. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  4892. * @param int $x abscissa of the scaling center. Default is current x position
  4893. * @param int $y ordinate of the scaling center. Default is current y position
  4894. * @since 2.1.000 (2008-01-07)
  4895. * @see StartTransform(), StopTransform()
  4896. */
  4897. function Scale($s_x, $s_y, $x='', $y=''){
  4898. if($x === '') {
  4899. $x=$this->x;
  4900. }
  4901. if($y === '') {
  4902. $y=$this->y;
  4903. }
  4904. if($this->rtl) {
  4905. $x = $this->w - $x;
  4906. }
  4907. if($s_x == 0 OR $s_y == 0)
  4908. $this->Error('Please use values unequal to zero for Scaling');
  4909. $y=($this->h-$y)*$this->k;
  4910. $x*=$this->k;
  4911. //calculate elements of transformation matrix
  4912. $s_x/=100;
  4913. $s_y/=100;
  4914. $tm[0]=$s_x;
  4915. $tm[1]=0;
  4916. $tm[2]=0;
  4917. $tm[3]=$s_y;
  4918. $tm[4]=$x*(1-$s_x);
  4919. $tm[5]=$y*(1-$s_y);
  4920. //scale the coordinate system
  4921. $this->Transform($tm);
  4922. }
  4923. /**
  4924. * Horizontal Mirroring.
  4925. * @param int $x abscissa of the point. Default is current x position
  4926. * @since 2.1.000 (2008-01-07)
  4927. * @see StartTransform(), StopTransform()
  4928. */
  4929. function MirrorH($x=''){
  4930. $this->Scale(-100, 100, $x);
  4931. }
  4932. /**
  4933. * Verical Mirroring.
  4934. * @param int $y ordinate of the point. Default is current y position
  4935. * @since 2.1.000 (2008-01-07)
  4936. * @see StartTransform(), StopTransform()
  4937. */
  4938. function MirrorV($y=''){
  4939. $this->Scale(100, -100, '', $y);
  4940. }
  4941. /**
  4942. * Point reflection mirroring.
  4943. * @param int $x abscissa of the point. Default is current x position
  4944. * @param int $y ordinate of the point. Default is current y position
  4945. * @since 2.1.000 (2008-01-07)
  4946. * @see StartTransform(), StopTransform()
  4947. */
  4948. function MirrorP($x='',$y=''){
  4949. $this->Scale(-100, -100, $x, $y);
  4950. }
  4951. /**
  4952. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  4953. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  4954. * @param int $x abscissa of the point. Default is current x position
  4955. * @param int $y ordinate of the point. Default is current y position
  4956. * @since 2.1.000 (2008-01-07)
  4957. * @see StartTransform(), StopTransform()
  4958. */
  4959. function MirrorL($angle=0, $x='',$y=''){
  4960. $this->Scale(-100, 100, $x, $y);
  4961. $this->Rotate(-2*($angle-90),$x,$y);
  4962. }
  4963. /**
  4964. * Translate graphic object horizontally.
  4965. * @param int $t_x movement to the right
  4966. * @since 2.1.000 (2008-01-07)
  4967. * @see StartTransform(), StopTransform()
  4968. */
  4969. function TranslateX($t_x){
  4970. $this->Translate($t_x, 0);
  4971. }
  4972. /**
  4973. * Translate graphic object vertically.
  4974. * @param int $t_y movement to the bottom
  4975. * @since 2.1.000 (2008-01-07)
  4976. * @see StartTransform(), StopTransform()
  4977. */
  4978. function TranslateY($t_y){
  4979. $this->Translate(0, $t_y, $x, $y);
  4980. }
  4981. /**
  4982. * Translate graphic object horizontally and vertically.
  4983. * @param int $t_x movement to the right
  4984. * @param int $t_y movement to the bottom
  4985. * @since 2.1.000 (2008-01-07)
  4986. * @see StartTransform(), StopTransform()
  4987. */
  4988. function Translate($t_x, $t_y){
  4989. if($this->rtl) {
  4990. $t_x = -$t_x;
  4991. }
  4992. //calculate elements of transformation matrix
  4993. $tm[0]=1;
  4994. $tm[1]=0;
  4995. $tm[2]=0;
  4996. $tm[3]=1;
  4997. $tm[4]=$t_x*$this->k;
  4998. $tm[5]=-$t_y*$this->k;
  4999. //translate the coordinate system
  5000. $this->Transform($tm);
  5001. }
  5002. /**
  5003. * Rotate object.
  5004. * @param float $angle angle in degrees for counter-clockwise rotation
  5005. * @param int $x abscissa of the rotation center. Default is current x position
  5006. * @param int $y ordinate of the rotation center. Default is current y position
  5007. * @since 2.1.000 (2008-01-07)
  5008. * @see StartTransform(), StopTransform()
  5009. */
  5010. function Rotate($angle, $x='', $y=''){
  5011. if($x === '') {
  5012. $x=$this->x;
  5013. }
  5014. if($y === '') {
  5015. $y=$this->y;
  5016. }
  5017. if($this->rtl) {
  5018. $x = $this->w - $x;
  5019. $angle = -$angle;
  5020. }
  5021. $y=($this->h-$y)*$this->k;
  5022. $x*=$this->k;
  5023. //calculate elements of transformation matrix
  5024. $tm[0]=cos(deg2rad($angle));
  5025. $tm[1]=sin(deg2rad($angle));
  5026. $tm[2]=-$tm[1];
  5027. $tm[3]=$tm[0];
  5028. $tm[4]=$x+$tm[1]*$y-$tm[0]*$x;
  5029. $tm[5]=$y-$tm[0]*$y-$tm[1]*$x;
  5030. //rotate the coordinate system around ($x,$y)
  5031. $this->Transform($tm);
  5032. }
  5033. /**
  5034. * Skew horizontally.
  5035. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  5036. * @param int $x abscissa of the skewing center. default is current x position
  5037. * @param int $y ordinate of the skewing center. default is current y position
  5038. * @since 2.1.000 (2008-01-07)
  5039. * @see StartTransform(), StopTransform()
  5040. */
  5041. function SkewX($angle_x, $x='', $y=''){
  5042. $this->Skew($angle_x, 0, $x, $y);
  5043. }
  5044. /**
  5045. * Skew vertically.
  5046. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  5047. * @param int $x abscissa of the skewing center. default is current x position
  5048. * @param int $y ordinate of the skewing center. default is current y position
  5049. * @since 2.1.000 (2008-01-07)
  5050. * @see StartTransform(), StopTransform()
  5051. */
  5052. function SkewY($angle_y, $x='', $y=''){
  5053. $this->Skew(0, $angle_y, $x, $y);
  5054. }
  5055. /**
  5056. * Skew.
  5057. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  5058. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  5059. * @param int $x abscissa of the skewing center. default is current x position
  5060. * @param int $y ordinate of the skewing center. default is current y position
  5061. * @since 2.1.000 (2008-01-07)
  5062. * @see StartTransform(), StopTransform()
  5063. */
  5064. function Skew($angle_x, $angle_y, $x='', $y=''){
  5065. if($x === '') {
  5066. $x=$this->x;
  5067. }
  5068. if($y === '') {
  5069. $y=$this->y;
  5070. }
  5071. if($this->rtl) {
  5072. $x = $this->w - $x;
  5073. $angle_x = -$angle_x;
  5074. }
  5075. if($angle_x <= -90 OR $angle_x >= 90 OR $angle_y <= -90 OR $angle_y >= 90)
  5076. $this->Error('Please use values between -90� and 90� for skewing');
  5077. $x*=$this->k;
  5078. $y=($this->h-$y)*$this->k;
  5079. //calculate elements of transformation matrix
  5080. $tm[0]=1;
  5081. $tm[1]=tan(deg2rad($angle_y));
  5082. $tm[2]=tan(deg2rad($angle_x));
  5083. $tm[3]=1;
  5084. $tm[4]=-$tm[2]*$y;
  5085. $tm[5]=-$tm[1]*$x;
  5086. //skew the coordinate system
  5087. $this->Transform($tm);
  5088. }
  5089. /**
  5090. * Apply graphic transformations.
  5091. * @since 2.1.000 (2008-01-07)
  5092. * @see StartTransform(), StopTransform()
  5093. */
  5094. function Transform($tm){
  5095. $this->_out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', $tm[0],$tm[1],$tm[2],$tm[3],$tm[4],$tm[5]));
  5096. }
  5097. // END TRANSFORMATIONS SECTION -------------------------
  5098. // START GRAPHIC FUNCTIONS SECTION ---------------------
  5099. // The following section is based on the code provided by David Hernandez Sanz
  5100. /**
  5101. * 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.
  5102. * @param float $width The width.
  5103. * @since 1.0
  5104. * @see Line(), Rect(), Cell(), MultiCell()
  5105. */
  5106. function SetLineWidth($width) {
  5107. //Set line width
  5108. $this->LineWidth=$width;
  5109. if($this->page>0) {
  5110. $this->_out(sprintf('%.2f w',$width*$this->k));
  5111. }
  5112. }
  5113. /**
  5114. * Returns the current the line width.
  5115. * @return int Line width
  5116. * @since 2.1.000 (2008-01-07)
  5117. * @see Line(), SetLineWidth()
  5118. */
  5119. function GetLineWidth() {
  5120. return $this->LineWidth;
  5121. }
  5122. /**
  5123. * Set line style.
  5124. *
  5125. * @param array $style Line style. Array with keys among the following:
  5126. * <ul>
  5127. * <li>width (float): Width of the line in user units.</li>
  5128. * <li>cap (string): Type of cap to put on the line. Possible values are:
  5129. * butt, round, square. The difference between "square" and "butt" is that
  5130. * "square" projects a flat end past the end of the line.</li>
  5131. * <li>join (string): Type of join. Possible values are: miter, round,
  5132. * bevel.</li>
  5133. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  5134. * series of length values, which are the lengths of the on and off dashes.
  5135. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  5136. * 1 off, 2 on, 1 off, ...</li>
  5137. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  5138. * the point at which the pattern starts.</li>
  5139. * <li>color (array): Draw color. Format: array(red, green, blue).</li>
  5140. * </ul>
  5141. * @access public
  5142. * @since 2.1.000 (2008-01-08)
  5143. */
  5144. function SetLineStyle($style) {
  5145. extract($style);
  5146. if (isset($width)) {
  5147. $width_prev = $this->LineWidth;
  5148. $this->SetLineWidth($width);
  5149. $this->LineWidth = $width_prev;
  5150. }
  5151. if (isset($cap)) {
  5152. $ca = array("butt" => 0, "round"=> 1, "square" => 2);
  5153. if (isset($ca[$cap])) {
  5154. $this->_out($ca[$cap] . " J");
  5155. }
  5156. }
  5157. if (isset($join)) {
  5158. $ja = array("miter" => 0, "round" => 1, "bevel" => 2);
  5159. if (isset($ja[$join])) {
  5160. $this->_out($ja[$join] . " j");
  5161. }
  5162. }
  5163. if (isset($dash)) {
  5164. $dash_string = "";
  5165. if ($dash) {
  5166. if (ereg("^.+,", $dash)) {
  5167. $tab = explode(",", $dash);
  5168. } else {
  5169. $tab = array($dash);
  5170. }
  5171. $dash_string = "";
  5172. foreach ($tab as $i => $v) {
  5173. if ($i) {
  5174. $dash_string .= " ";
  5175. }
  5176. $dash_string .= sprintf("%.2f", $v);
  5177. }
  5178. }
  5179. if (!isset($phase) OR !$dash) {
  5180. $phase = 0;
  5181. }
  5182. $this->_out(sprintf("[%s] %.2f d", $dash_string, $phase));
  5183. }
  5184. if (isset($color)) {
  5185. list($r, $g, $b) = $color;
  5186. $this->SetDrawColor($r, $g, $b);
  5187. }
  5188. }
  5189. /*
  5190. * Set a draw point.
  5191. * @param float $x Abscissa of point.
  5192. * @param float $y Ordinate of point.
  5193. * @access private
  5194. * @since 2.1.000 (2008-01-08)
  5195. */
  5196. function _outPoint($x, $y) {
  5197. if($this->rtl) {
  5198. $x = $this->w - $x;
  5199. }
  5200. $this->_out(sprintf("%.2f %.2f m", $x * $this->k, ($this->h - $y) * $this->k));
  5201. }
  5202. /*
  5203. * Draws a line from last draw point.
  5204. * @param float $x Abscissa of end point.
  5205. * @param float $y Ordinate of end point.
  5206. * @access private
  5207. * @since 2.1.000 (2008-01-08)
  5208. */
  5209. function _outLine($x, $y) {
  5210. if($this->rtl) {
  5211. $x = $this->w - $x;
  5212. }
  5213. $this->_out(sprintf("%.2f %.2f l", $x * $this->k, ($this->h - $y) * $this->k));
  5214. }
  5215. /**
  5216. * Draws a rectangle.
  5217. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  5218. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  5219. * @param float $w Width.
  5220. * @param float $h Height.
  5221. * @param string $op options
  5222. * @access protected
  5223. * @since 2.1.000 (2008-01-08)
  5224. */
  5225. function _outRect($x, $y, $w, $h, $op) {
  5226. if($this->rtl) {
  5227. $x = $this->w - $x - $w;
  5228. }
  5229. $this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
  5230. }
  5231. /*
  5232. * Draws a Bezier curve from last draw point.
  5233. * The Bezier curve is a tangent to the line between the control points at either end of the curve.
  5234. * @param float $x1 Abscissa of control point 1.
  5235. * @param float $y1 Ordinate of control point 1.
  5236. * @param float $x2 Abscissa of control point 2.
  5237. * @param float $y2 Ordinate of control point 2.
  5238. * @param float $x3 Abscissa of end point.
  5239. * @param float $y3 Ordinate of end point.
  5240. * @access private
  5241. * @since 2.1.000 (2008-01-08)
  5242. */
  5243. function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  5244. if($this->rtl) {
  5245. $x1 = $this->w - $x1;
  5246. $x2 = $this->w - $x2;
  5247. $x3 = $this->w - $x3;
  5248. }
  5249. $this->_out(sprintf("%.2f %.2f %.2f %.2f %.2f %.2f c", $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  5250. }
  5251. /**
  5252. * Draws a line between two points.
  5253. * @param float $x1 Abscissa of first point.
  5254. * @param float $y1 Ordinate of first point.
  5255. * @param float $x2 Abscissa of second point.
  5256. * @param float $y2 Ordinate of second point.
  5257. * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5258. * @access public
  5259. * @since 1.0
  5260. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  5261. */
  5262. function Line($x1, $y1, $x2, $y2, $style = array()) {
  5263. if ($style) {
  5264. $this->SetLineStyle($style);
  5265. }
  5266. $this->_outPoint($x1, $y1);
  5267. $this->_outLine($x2, $y2);
  5268. $this->_out(" S");
  5269. }
  5270. /**
  5271. * Draws a rectangle.
  5272. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  5273. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  5274. * @param float $w Width.
  5275. * @param float $h Height.
  5276. * @param string $style Style of rendering. Possible values are:
  5277. * <ul>
  5278. * <li>D or empty string: Draw (default).</li>
  5279. * <li>F: Fill.</li>
  5280. * <li>DF or FD: Draw and fill.</li>
  5281. * </ul>
  5282. * @param array $border_style Border style of rectangle. Array with keys among the following:
  5283. * <ul>
  5284. * <li>all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5285. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5286. * </ul>
  5287. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  5288. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5289. * @access public
  5290. * @since 1.0
  5291. * @see SetLineStyle()
  5292. */
  5293. function Rect($x, $y, $w, $h, $style='', $border_style = array(), $fill_color = array()) {
  5294. if (!(false === strpos($style, "F")) AND $fill_color) {
  5295. list($r, $g, $b) = $fill_color;
  5296. $this->SetFillColor($r, $g, $b);
  5297. }
  5298. switch ($style) {
  5299. case "F": {
  5300. $op='f';
  5301. $border_style = array();
  5302. $this->_outRect($x, $y, $w, $h, $op);
  5303. break;
  5304. }
  5305. case "DF":
  5306. case "FD": {
  5307. if (!$border_style OR isset($border_style["all"])) {
  5308. $op='B';
  5309. if (isset($border_style["all"])) {
  5310. $this->SetLineStyle($border_style["all"]);
  5311. $border_style = array();
  5312. }
  5313. } else {
  5314. $op='f';
  5315. }
  5316. $this->_outRect($x, $y, $w, $h, $op);
  5317. break;
  5318. }
  5319. default: {
  5320. $op='S';
  5321. if (!$border_style OR isset($border_style["all"])) {
  5322. if (isset($border_style["all"]) && $border_style["all"]) {
  5323. $this->SetLineStyle($border_style["all"]);
  5324. $border_style = array();
  5325. }
  5326. $this->_outRect($x, $y, $w, $h, $op);
  5327. }
  5328. break;
  5329. }
  5330. }
  5331. if ($border_style) {
  5332. $border_style2 = array();
  5333. foreach ($border_style as $line => $value) {
  5334. $lenght = strlen($line);
  5335. for ($i = 0; $i < $lenght; $i++) {
  5336. $border_style2[$line[$i]] = $value;
  5337. }
  5338. }
  5339. $border_style = $border_style2;
  5340. if (isset($border_style["L"]) && $border_style["L"]) {
  5341. $this->Line($x, $y, $x, $y + $h, $border_style["L"]);
  5342. }
  5343. if (isset($border_style["T"]) && $border_style["T"]) {
  5344. $this->Line($x, $y, $x + $w, $y, $border_style["T"]);
  5345. }
  5346. if (isset($border_style["R"]) && $border_style["R"]) {
  5347. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style["R"]);
  5348. }
  5349. if (isset($border_style["B"]) && $border_style["B"]) {
  5350. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style["B"]);
  5351. }
  5352. }
  5353. }
  5354. /**
  5355. * Draws a Bezier curve.
  5356. * The Bezier curve is a tangent to the line between the control points at
  5357. * either end of the curve.
  5358. * @param float $x0 Abscissa of start point.
  5359. * @param float $y0 Ordinate of start point.
  5360. * @param float $x1 Abscissa of control point 1.
  5361. * @param float $y1 Ordinate of control point 1.
  5362. * @param float $x2 Abscissa of control point 2.
  5363. * @param float $y2 Ordinate of control point 2.
  5364. * @param float $x3 Abscissa of end point.
  5365. * @param float $y3 Ordinate of end point.
  5366. * @param string $style Style of rendering. Possible values are:
  5367. * <ul>
  5368. * <li>D or empty string: Draw (default).</li>
  5369. * <li>F: Fill.</li>
  5370. * <li>DF or FD: Draw and fill.</li>
  5371. * </ul>
  5372. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5373. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5374. * @access public
  5375. * @see SetLineStyle()
  5376. * @since 2.1.000 (2008-01-08)
  5377. */
  5378. function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style = "", $line_style = array(), $fill_color = array()) {
  5379. if (!(false === strpos($style, "F")) AND $fill_color) {
  5380. list($r, $g, $b) = $fill_color;
  5381. $this->SetFillColor($r, $g, $b);
  5382. }
  5383. switch ($style) {
  5384. case "F": {
  5385. $op = "f";
  5386. $line_style = array();
  5387. break;
  5388. }
  5389. case "FD":
  5390. case "DF": {
  5391. $op = "B";
  5392. break;
  5393. }
  5394. default: {
  5395. $op = "S";
  5396. break;
  5397. }
  5398. }
  5399. if ($line_style) {
  5400. $this->SetLineStyle($line_style);
  5401. }
  5402. $this->_outPoint($x0, $y0);
  5403. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  5404. $this->_out($op);
  5405. }
  5406. /**
  5407. * Draws an ellipse.
  5408. * An ellipse is formed from n Bezier curves.
  5409. * @param float $x0 Abscissa of center point.
  5410. * @param float $y0 Ordinate of center point.
  5411. * @param float $rx Horizontal radius.
  5412. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  5413. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  5414. * @param float $astart: Angle start of draw line. Default value: 0.
  5415. * @param float $afinish: Angle finish of draw line. Default value: 360.
  5416. * @param string $style Style of rendering. Possible values are:
  5417. * <ul>
  5418. * <li>D or empty string: Draw (default).</li>
  5419. * <li>F: Fill.</li>
  5420. * <li>DF or FD: Draw and fill.</li>
  5421. * <li>C: Draw close.</li>
  5422. * </ul>
  5423. * @param array $line_style Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5424. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5425. * @param integer $nc Number of curves used in ellipse. Default value: 8.
  5426. * @access public
  5427. * @since 2.1.000 (2008-01-08)
  5428. */
  5429. function Ellipse($x0, $y0, $rx, $ry = 0, $angle = 0, $astart = 0, $afinish = 360, $style = "", $line_style = array(), $fill_color = array(), $nc = 8) {
  5430. if ($angle) {
  5431. $this->StartTransform();
  5432. $this->Rotate($angle, $x0, $y0);
  5433. $this->Ellipse($x0, $y0, $rx, $ry, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  5434. $this->StopTransform();
  5435. return;
  5436. }
  5437. if ($rx) {
  5438. if (!(false === strpos($style, "F")) AND $fill_color) {
  5439. list($r, $g, $b) = $fill_color;
  5440. $this->SetFillColor($r, $g, $b);
  5441. }
  5442. switch ($style) {
  5443. case "F": {
  5444. $op = "f";
  5445. $line_style = array();
  5446. break;
  5447. }
  5448. case "FD":
  5449. case "DF": {
  5450. $op = "B";
  5451. break;
  5452. }
  5453. case "C": {
  5454. $op = "s"; // Small "s" signifies closing the path as well
  5455. break;
  5456. }
  5457. default: {
  5458. $op = "S";
  5459. break;
  5460. }
  5461. }
  5462. if ($line_style) {
  5463. $this->SetLineStyle($line_style);
  5464. }
  5465. if (!$ry) {
  5466. $ry = $rx;
  5467. }
  5468. $rx *= $this->k;
  5469. $ry *= $this->k;
  5470. if ($nc < 2){
  5471. $nc = 2;
  5472. }
  5473. $astart = deg2rad((float) $astart);
  5474. $afinish = deg2rad((float) $afinish);
  5475. $total_angle = $afinish - $astart;
  5476. $dt = $total_angle / $nc;
  5477. $dtm = $dt/3;
  5478. $x0 *= $this->k;
  5479. $y0 = ($this->h - $y0) * $this->k;
  5480. $t1 = $astart;
  5481. $a0 = $x0 + ($rx * cos($t1));
  5482. $b0 = $y0 + ($ry * sin($t1));
  5483. $c0 = -$rx * sin($t1);
  5484. $d0 = $ry * cos($t1);
  5485. $this->_outPoint($a0 / $this->k, $this->h - ($b0 / $this->k));
  5486. for ($i = 1; $i <= $nc; $i++) {
  5487. // Draw this bit of the total curve
  5488. $t1 = ($i * $dt) + $astart;
  5489. $a1 = $x0 + ($rx * cos($t1));
  5490. $b1 = $y0 + ($ry * sin($t1));
  5491. $c1 = -$rx * sin($t1);
  5492. $d1 = $ry * cos($t1);
  5493. $this->_outCurve(($a0 + ($c0 * $dtm)) / $this->k, $this->h - (($b0 + ($d0 * $dtm)) / $this->k), ($a1 - ($c1 * $dtm)) / $this->k, $this->h - (($b1 - ($d1 * $dtm)) / $this->k), $a1 / $this->k, $this->h - ($b1 / $this->k));
  5494. $a0 = $a1;
  5495. $b0 = $b1;
  5496. $c0 = $c1;
  5497. $d0 = $d1;
  5498. }
  5499. $this->_out($op);
  5500. }
  5501. }
  5502. /**
  5503. * Draws a circle.
  5504. * A circle is formed from n Bezier curves.
  5505. * @param float $x0 Abscissa of center point.
  5506. * @param float $y0 Ordinate of center point.
  5507. * @param float $r Radius.
  5508. * @param float $astart: Angle start of draw line. Default value: 0.
  5509. * @param float $afinish: Angle finish of draw line. Default value: 360.
  5510. * @param string $style Style of rendering. Possible values are:
  5511. * <ul>
  5512. * <li>D or empty string: Draw (default).</li>
  5513. * <li>F: Fill.</li>
  5514. * <li>DF or FD: Draw and fill.</li>
  5515. * <li>C: Draw close.</li>
  5516. * </ul>
  5517. * @param array $line_style Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5518. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5519. * @param integer $nc Number of curves used in circle. Default value: 8.
  5520. * @access public
  5521. * @since 2.1.000 (2008-01-08)
  5522. */
  5523. function Circle($x0, $y0, $r, $astart = 0, $afinish = 360, $style = "", $line_style = array(), $fill_color = array(), $nc = 8) {
  5524. $this->Ellipse($x0, $y0, $r, 0, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  5525. }
  5526. /**
  5527. * Draws a polygon.
  5528. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  5529. * @param string $style Style of rendering. Possible values are:
  5530. * <ul>
  5531. * <li>D or empty string: Draw (default).</li>
  5532. * <li>F: Fill.</li>
  5533. * <li>DF or FD: Draw and fill.</li>
  5534. * </ul>
  5535. * @param array $line_style Line style of polygon. Array with keys among the following:
  5536. * <ul>
  5537. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5538. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5539. * </ul>
  5540. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  5541. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5542. * @access public
  5543. * @since 2.1.000 (2008-01-08)
  5544. */
  5545. function Polygon($p, $style = "", $line_style = array(), $fill_color = array()) {
  5546. $np = count($p) / 2;
  5547. if (!(false === strpos($style, "F")) AND $fill_color) {
  5548. list($r, $g, $b) = $fill_color;
  5549. $this->SetFillColor($r, $g, $b);
  5550. }
  5551. switch ($style) {
  5552. case "F": {
  5553. $line_style = array();
  5554. $op = "f";
  5555. break;
  5556. }
  5557. case "FD":
  5558. case "DF": {
  5559. $op = "B";
  5560. break;
  5561. }
  5562. default: {
  5563. $op = "S";
  5564. break;
  5565. }
  5566. }
  5567. $draw = true;
  5568. if ($line_style) {
  5569. if (isset($line_style["all"])) {
  5570. $this->SetLineStyle($line_style["all"]);
  5571. }
  5572. else { // 0 .. (np - 1), op = {B, S}
  5573. $draw = false;
  5574. if ("B" == $op) {
  5575. $op = "f";
  5576. $this->_outPoint($p[0], $p[1]);
  5577. for ($i = 2; $i < ($np * 2); $i = $i + 2) {
  5578. $this->_outLine($p[$i], $p[$i + 1]);
  5579. }
  5580. $this->_outLine($p[0], $p[1]);
  5581. $this->_out($op);
  5582. }
  5583. $p[$np * 2] = $p[0];
  5584. $p[($np * 2) + 1] = $p[1];
  5585. for ($i = 0; $i < $np; $i++) {
  5586. if (isset($line_style[$i])) {
  5587. $this->Line($p[$i * 2], $p[($i * 2) + 1], $p[($i * 2) + 2], $p[($i * 2) + 3], $line_style[$i]);
  5588. }
  5589. }
  5590. }
  5591. }
  5592. if ($draw) {
  5593. $this->_outPoint($p[0], $p[1]);
  5594. for ($i = 2; $i < ($np * 2); $i = $i + 2) {
  5595. $this->_outLine($p[$i], $p[$i + 1]);
  5596. }
  5597. $this->_outLine($p[0], $p[1]);
  5598. $this->_out($op);
  5599. }
  5600. }
  5601. /**
  5602. * Draws a regular polygon.
  5603. * @param float $x0 Abscissa of center point.
  5604. * @param float $y0 Ordinate of center point.
  5605. * @param float $r: Radius of inscribed circle.
  5606. * @param integer $ns Number of sides.
  5607. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  5608. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  5609. * @param string $style Style of rendering. Possible values are:
  5610. * <ul>
  5611. * <li>D or empty string: Draw (default).</li>
  5612. * <li>F: Fill.</li>
  5613. * <li>DF or FD: Draw and fill.</li>
  5614. * </ul>
  5615. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  5616. * <ul>
  5617. * <li>all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5618. * <li>0 to ($ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5619. * </ul>
  5620. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  5621. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5622. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  5623. * <ul>
  5624. * <li>D or empty string: Draw (default).</li>
  5625. * <li>F: Fill.</li>
  5626. * <li>DF or FD: Draw and fill.</li>
  5627. * </ul>
  5628. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5629. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  5630. * @access public
  5631. * @since 2.1.000 (2008-01-08)
  5632. */
  5633. function RegularPolygon($x0, $y0, $r, $ns, $angle = 0, $draw_circle = false, $style = "", $line_style = array(), $fill_color = array(), $circle_style = "", $circle_outLine_style = array(), $circle_fill_color = array()) {
  5634. if (3 > $ns) {
  5635. $ns = 3;
  5636. }
  5637. if ($draw_circle) {
  5638. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  5639. }
  5640. $p = array();
  5641. for ($i = 0; $i < $ns; $i++) {
  5642. $a = $angle + ($i * 360 / $ns);
  5643. $a_rad = deg2rad((float) $a);
  5644. $p[] = $x0 + ($r * sin($a_rad));
  5645. $p[] = $y0 + ($r * cos($a_rad));
  5646. }
  5647. $this->Polygon($p, $style, $line_style, $fill_color);
  5648. }
  5649. /**
  5650. * Draws a star polygon
  5651. * @param float $x0 Abscissa of center point.
  5652. * @param float $y0 Ordinate of center point.
  5653. * @param float $r Radius of inscribed circle.
  5654. * @param integer $nv Number of vertices.
  5655. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  5656. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  5657. * @param boolean $draw_circle: Draw inscribed circle or not. Default value is false.
  5658. * @param string $style Style of rendering. Possible values are:
  5659. * <ul>
  5660. * <li>D or empty string: Draw (default).</li>
  5661. * <li>F: Fill.</li>
  5662. * <li>DF or FD: Draw and fill.</li>
  5663. * </ul>
  5664. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  5665. * <ul>
  5666. * <li>all: Line style of all sides. Array like for
  5667. * {@link SetLineStyle SetLineStyle}.</li>
  5668. * <li>0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  5669. * </ul>
  5670. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  5671. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5672. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  5673. * <ul>
  5674. * <li>D or empty string: Draw (default).</li>
  5675. * <li>F: Fill.</li>
  5676. * <li>DF or FD: Draw and fill.</li>
  5677. * </ul>
  5678. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5679. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  5680. * @access public
  5681. * @since 2.1.000 (2008-01-08)
  5682. */
  5683. function StarPolygon($x0, $y0, $r, $nv, $ng, $angle = 0, $draw_circle = false, $style = "", $line_style = array(), $fill_color = array(), $circle_style = "", $circle_outLine_style = array(), $circle_fill_color = array()) {
  5684. if (2 > $nv) {
  5685. $nv = 2;
  5686. }
  5687. if ($draw_circle) {
  5688. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  5689. }
  5690. $p2 = array();
  5691. $visited = array();
  5692. for ($i = 0; $i < $nv; $i++) {
  5693. $a = $angle + ($i * 360 / $nv);
  5694. $a_rad = deg2rad((float) $a);
  5695. $p2[] = $x0 + ($r * sin($a_rad));
  5696. $p2[] = $y0 + ($r * cos($a_rad));
  5697. $visited[] = false;
  5698. }
  5699. $p = array();
  5700. $i = 0;
  5701. do {
  5702. $p[] = $p2[$i * 2];
  5703. $p[] = $p2[($i * 2) + 1];
  5704. $visited[$i] = true;
  5705. $i += $ng;
  5706. $i %= $nv;
  5707. } while (!$visited[$i]);
  5708. $this->Polygon($p, $style, $line_style, $fill_color);
  5709. }
  5710. /**
  5711. * Draws a rounded rectangle.
  5712. * @param float $x Abscissa of upper-left corner.
  5713. * @param float $y Ordinate of upper-left corner.
  5714. * @param float $w Width.
  5715. * @param float $h Height.
  5716. * @param float $r Radius of the rounded corners.
  5717. * @param string $round_corner Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
  5718. * @param string $style Style of rendering. Possible values are:
  5719. * <ul>
  5720. * <li>D or empty string: Draw (default).</li>
  5721. * <li>F: Fill.</li>
  5722. * <li>DF or FD: Draw and fill.</li>
  5723. * </ul>
  5724. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  5725. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  5726. * @access public
  5727. * @since 2.1.000 (2008-01-08)
  5728. */
  5729. function RoundedRect($x, $y, $w, $h, $r, $round_corner = "1111", $style = "", $border_style = array(), $fill_color = array()) {
  5730. if ("0000" == $round_corner) { // Not rounded
  5731. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  5732. } else { // Rounded
  5733. if (!(false === strpos($style, "F")) AND $fill_color) {
  5734. list($red, $g, $b) = $fill_color;
  5735. $this->SetFillColor($red, $g, $b);
  5736. }
  5737. switch ($style) {
  5738. case "F": {
  5739. $border_style = array();
  5740. $op = "f";
  5741. break;
  5742. }
  5743. case "FD":
  5744. case "DF": {
  5745. $op = "B";
  5746. break;
  5747. }
  5748. default: {
  5749. $op = "S";
  5750. break;
  5751. }
  5752. }
  5753. if ($border_style) {
  5754. $this->SetLineStyle($border_style);
  5755. }
  5756. $MyArc = 4 / 3 * (sqrt(2) - 1);
  5757. $this->_outPoint($x + $r, $y);
  5758. $xc = $x + $w - $r;
  5759. $yc = $y + $r;
  5760. $this->_outLine($xc, $y);
  5761. if ($round_corner[0]) {
  5762. $this->_outCurve($xc + ($r * $MyArc), $yc - $r, $xc + $r, $yc - ($r * $MyArc), $xc + $r, $yc);
  5763. } else {
  5764. $this->_outLine($x + $w, $y);
  5765. }
  5766. $xc = $x + $w - $r;
  5767. $yc = $y + $h - $r;
  5768. $this->_outLine($x + $w, $yc);
  5769. if ($round_corner[1]) {
  5770. $this->_outCurve($xc + $r, $yc + ($r * $MyArc), $xc + ($r * $MyArc), $yc + $r, $xc, $yc + $r);
  5771. } else {
  5772. $this->_outLine($x + $w, $y + $h);
  5773. }
  5774. $xc = $x + $r;
  5775. $yc = $y + $h - $r;
  5776. $this->_outLine($xc, $y + $h);
  5777. if ($round_corner[2]) {
  5778. $this->_outCurve($xc - ($r * $MyArc), $yc + $r, $xc - $r, $yc + ($r * $MyArc), $xc - $r, $yc);
  5779. } else {
  5780. $this->_outLine($x, $y + $h);
  5781. }
  5782. $xc = $x + $r;
  5783. $yc = $y + $r;
  5784. $this->_outLine($x, $yc);
  5785. if ($round_corner[3]) {
  5786. $this->_outCurve($xc - $r, $yc - ($r * $MyArc), $xc - ($r * $MyArc), $yc - $r, $xc, $yc - $r);
  5787. } else {
  5788. $this->_outLine($x, $y);
  5789. $this->_outLine($x + $r, $y);
  5790. }
  5791. $this->_out($op);
  5792. }
  5793. }
  5794. // END GRAPHIC FUNCTIONS SECTION -----------------------
  5795. // BIDIRECTIONAL TEXT SECTION --------------------------
  5796. /**
  5797. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  5798. * @param string $str string to manipulate.
  5799. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  5800. * @return string
  5801. * @author Nicola Asuni
  5802. * @since 2.1.000 (2008-01-08)
  5803. */
  5804. function utf8StrRev($str, $setbom=false, $forcertl=false) {
  5805. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($this->UTF8StringToArray($str), $forcertl=false), $setbom);
  5806. }
  5807. /**
  5808. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  5809. * @param array $ta array of characters composing the string.
  5810. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  5811. * @return string
  5812. * @author Nicola Asuni
  5813. * @since 2.4.000 (2008-03-06)
  5814. */
  5815. function utf8Bidi($ta, $forcertl=false) {
  5816. global $unicode,$unicode_mirror, $unicode_arlet;
  5817. require_once(dirname(__FILE__).'/unicode_data.php');
  5818. // paragraph embedding level
  5819. $pel = 0;
  5820. // max level
  5821. $maxlevel = 0;
  5822. // create string from array
  5823. $str = $this->UTF8ArrSubString($ta);
  5824. // check if string contains arabic text
  5825. if (preg_match(K_RE_PATTERN_ARABIC, $str)) {
  5826. $arabic = true;
  5827. } else {
  5828. $arabic = false;
  5829. }
  5830. // check if string contains RTL text
  5831. if (!($forcertl OR $arabic OR preg_match(K_RE_PATTERN_RTL, $str))) {
  5832. return $ta;
  5833. }
  5834. // get number of chars
  5835. $numchars = count($ta);
  5836. if ($forcertl == 'R') {
  5837. $pel = 1;
  5838. } elseif ($forcertl == 'L') {
  5839. $pel = 0;
  5840. } else {
  5841. // P2. In each paragraph, find the first character of type L, AL, or R.
  5842. // P3. If a character is found in P2 and it is of type AL or R, then set the paragraph embedding level to one; otherwise, set it to zero.
  5843. for ($i=0; $i < $numchars; $i++) {
  5844. $type = $unicode[$ta[$i]];
  5845. if ($type == 'L') {
  5846. $pel = 0;
  5847. break;
  5848. } elseif (($type == 'AL') OR ($type == 'R')) {
  5849. $pel = 1;
  5850. break;
  5851. }
  5852. }
  5853. }
  5854. // Current Embedding Level
  5855. $cel = $pel;
  5856. // directional override status
  5857. $dos = 'N';
  5858. $remember = array();
  5859. // start-of-level-run
  5860. $sor = $pel % 2 ? 'R' : 'L';
  5861. $eor = $sor;
  5862. //$levels = array(array('level' => $cel, 'sor' => $sor, 'eor' => '', 'chars' => array()));
  5863. //$current_level = &$levels[count( $levels )-1];
  5864. // Array of characters data
  5865. $chardata = Array();
  5866. // X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
  5867. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  5868. for ($i=0; $i < $numchars; $i++) {
  5869. if ($ta[$i] == K_RLE) {
  5870. // X2. With each RLE, compute the least greater odd embedding level.
  5871. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  5872. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  5873. $next_level = $cel + ($cel % 2) + 1;
  5874. if ($next_level < 62) {
  5875. $remember[] = array('num' => K_RLE, 'cel' => $cel, 'dos' => $dos);
  5876. $cel = $next_level;
  5877. $dos = 'N';
  5878. $sor = $eor;
  5879. $eor = $cel % 2 ? 'R' : 'L';
  5880. }
  5881. } elseif ($ta[$i] == K_LRE) {
  5882. // X3. With each LRE, compute the least greater even embedding level.
  5883. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  5884. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  5885. $next_level = $cel + 2 - ($cel % 2);
  5886. if ( $next_level < 62 ) {
  5887. $remember[] = array('num' => K_LRE, 'cel' => $cel, 'dos' => $dos);
  5888. $cel = $next_level;
  5889. $dos = 'N';
  5890. $sor = $eor;
  5891. $eor = $cel % 2 ? 'R' : 'L';
  5892. }
  5893. } elseif ($ta[$i] == K_RLO) {
  5894. // X4. With each RLO, compute the least greater odd embedding level.
  5895. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
  5896. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  5897. $next_level = $cel + ($cel % 2) + 1;
  5898. if ($next_level < 62) {
  5899. $remember[] = array('num' => K_RLO, 'cel' => $cel, 'dos' => $dos);
  5900. $cel = $next_level;
  5901. $dos = 'R';
  5902. $sor = $eor;
  5903. $eor = $cel % 2 ? 'R' : 'L';
  5904. }
  5905. } elseif ($ta[$i] == K_LRO) {
  5906. // X5. With each LRO, compute the least greater even embedding level.
  5907. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
  5908. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  5909. $next_level = $cel + 2 - ($cel % 2);
  5910. if ( $next_level < 62 ) {
  5911. $remember[] = array('num' => K_LRO, 'cel' => $cel, 'dos' => $dos);
  5912. $cel = $next_level;
  5913. $dos = 'L';
  5914. $sor = $eor;
  5915. $eor = $cel % 2 ? 'R' : 'L';
  5916. }
  5917. } elseif ($ta[$i] == K_PDF) {
  5918. // X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
  5919. if (count($remember)) {
  5920. $last = count($remember ) - 1;
  5921. if (($remember[$last]['num'] == K_RLE) OR
  5922. ($remember[$last]['num'] == K_LRE) OR
  5923. ($remember[$last]['num'] == K_RLO) OR
  5924. ($remember[$last]['num'] == K_LRO)) {
  5925. $match = array_pop($remember);
  5926. $cel = $match['cel'];
  5927. $dos = $match['dos'];
  5928. $sor = $eor;
  5929. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  5930. }
  5931. }
  5932. } elseif (($ta[$i] != K_RLE) AND
  5933. ($ta[$i] != K_LRE) AND
  5934. ($ta[$i] != K_RLO) AND
  5935. ($ta[$i] != K_LRO) AND
  5936. ($ta[$i] != K_PDF)) {
  5937. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  5938. // a. Set the level of the current character to the current embedding level.
  5939. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  5940. if ($dos != 'N') {
  5941. $chardir = $dos;
  5942. } else {
  5943. $chardir = $unicode[$ta[$i]];
  5944. }
  5945. // stores string characters and other information
  5946. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  5947. }
  5948. } // end for each char
  5949. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  5950. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  5951. // X10. The remaining rules are applied to each run of characters at the same level. For each run, determine the start-of-level-run (sor) and end-of-level-run (eor) type, either L or R. This depends on the higher of the two levels on either side of the boundary (at the start or end of the paragraph, the level of the �other� run is the base embedding level). If the higher level is odd, the type is R; otherwise, it is L.
  5952. // 3.3.3 Resolving Weak Types
  5953. // Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
  5954. // Nonspacing marks are now resolved based on the previous characters.
  5955. $numchars = count($chardata);
  5956. // W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
  5957. $prevlevel = -1; // track level changes
  5958. $levcount = 0; // counts consecutive chars at the same level
  5959. for ($i=0; $i < $numchars; $i++) {
  5960. if ($chardata[$i]['type'] == 'NSM') {
  5961. if ($levcount) {
  5962. $chardata[$i]['type'] = $chardata[$i]['sor'];
  5963. } elseif ($i > 0) {
  5964. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  5965. }
  5966. }
  5967. if ($chardata[$i]['level'] != $prevlevel) {
  5968. $levcount = 0;
  5969. } else {
  5970. $levcount++;
  5971. }
  5972. $prevlevel = $chardata[$i]['level'];
  5973. }
  5974. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
  5975. $prevlevel = -1;
  5976. $levcount = 0;
  5977. for ($i=0; $i < $numchars; $i++) {
  5978. if ($chardata[$i]['char'] == 'EN') {
  5979. for ($j=$levcount; $j >= 0; $j--) {
  5980. if ($chardata[$j]['type'] == 'AL') {
  5981. $chardata[$i]['type'] = 'AN';
  5982. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  5983. break;
  5984. }
  5985. }
  5986. }
  5987. if ($chardata[$i]['level'] != $prevlevel) {
  5988. $levcount = 0;
  5989. } else {
  5990. $levcount++;
  5991. }
  5992. $prevlevel = $chardata[$i]['level'];
  5993. }
  5994. // W3. Change all ALs to R.
  5995. for ($i=0; $i < $numchars; $i++) {
  5996. if ($chardata[$i]['type'] == 'AL') {
  5997. $chardata[$i]['type'] = 'R';
  5998. }
  5999. }
  6000. // W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
  6001. $prevlevel = -1;
  6002. $levcount = 0;
  6003. for ($i=0; $i < $numchars; $i++) {
  6004. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  6005. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  6006. $chardata[$i]['type'] = 'EN';
  6007. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  6008. $chardata[$i]['type'] = 'EN';
  6009. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  6010. $chardata[$i]['type'] = 'AN';
  6011. }
  6012. }
  6013. if ($chardata[$i]['level'] != $prevlevel) {
  6014. $levcount = 0;
  6015. } else {
  6016. $levcount++;
  6017. }
  6018. $prevlevel = $chardata[$i]['level'];
  6019. }
  6020. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  6021. $prevlevel = -1;
  6022. $levcount = 0;
  6023. for ($i=0; $i < $numchars; $i++) {
  6024. if($chardata[$i]['type'] == 'ET') {
  6025. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  6026. $chardata[$i]['type'] = 'EN';
  6027. } else {
  6028. $j = $i+1;
  6029. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  6030. if ($chardata[$j]['type'] == 'EN') {
  6031. $chardata[$i]['type'] = 'EN';
  6032. break;
  6033. } elseif ($chardata[$j]['type'] != 'ET') {
  6034. break;
  6035. }
  6036. $j++;
  6037. }
  6038. }
  6039. }
  6040. if ($chardata[$i]['level'] != $prevlevel) {
  6041. $levcount = 0;
  6042. } else {
  6043. $levcount++;
  6044. }
  6045. $prevlevel = $chardata[$i]['level'];
  6046. }
  6047. // W6. Otherwise, separators and terminators change to Other Neutral.
  6048. $prevlevel = -1;
  6049. $levcount = 0;
  6050. for ($i=0; $i < $numchars; $i++) {
  6051. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  6052. $chardata[$i]['type'] = 'ON';
  6053. }
  6054. if ($chardata[$i]['level'] != $prevlevel) {
  6055. $levcount = 0;
  6056. } else {
  6057. $levcount++;
  6058. }
  6059. $prevlevel = $chardata[$i]['level'];
  6060. }
  6061. //W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
  6062. $prevlevel = -1;
  6063. $levcount = 0;
  6064. for ($i=0; $i < $numchars; $i++) {
  6065. if ($chardata[$i]['char'] == 'EN') {
  6066. for ($j=$levcount; $j >= 0; $j--) {
  6067. if ($chardata[$j]['type'] == 'L') {
  6068. $chardata[$i]['type'] = 'L';
  6069. } elseif ($chardata[$j]['type'] == 'R') {
  6070. break;
  6071. }
  6072. }
  6073. }
  6074. if ($chardata[$i]['level'] != $prevlevel) {
  6075. $levcount = 0;
  6076. } else {
  6077. $levcount++;
  6078. }
  6079. $prevlevel = $chardata[$i]['level'];
  6080. }
  6081. // N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
  6082. $prevlevel = -1;
  6083. $levcount = 0;
  6084. for ($i=0; $i < $numchars; $i++) {
  6085. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  6086. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  6087. $chardata[$i]['type'] = 'L';
  6088. } elseif (($chardata[$i]['type'] == 'N') AND
  6089. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  6090. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  6091. $chardata[$i]['type'] = 'R';
  6092. } elseif ($chardata[$i]['type'] == 'N') {
  6093. // N2. Any remaining neutrals take the embedding direction
  6094. $chardata[$i]['type'] = $chardata[$i]['sor'];
  6095. }
  6096. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  6097. // first char
  6098. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  6099. $chardata[$i]['type'] = 'L';
  6100. } elseif (($chardata[$i]['type'] == 'N') AND
  6101. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  6102. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  6103. $chardata[$i]['type'] = 'R';
  6104. } elseif ($chardata[$i]['type'] == 'N') {
  6105. // N2. Any remaining neutrals take the embedding direction
  6106. $chardata[$i]['type'] = $chardata[$i]['sor'];
  6107. }
  6108. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  6109. //last char
  6110. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  6111. $chardata[$i]['type'] = 'L';
  6112. } elseif (($chardata[$i]['type'] == 'N') AND
  6113. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  6114. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  6115. $chardata[$i]['type'] = 'R';
  6116. } elseif ($chardata[$i]['type'] == 'N') {
  6117. // N2. Any remaining neutrals take the embedding direction
  6118. $chardata[$i]['type'] = $chardata[$i]['sor'];
  6119. }
  6120. } elseif ($chardata[$i]['type'] == 'N') {
  6121. // N2. Any remaining neutrals take the embedding direction
  6122. $chardata[$i]['type'] = $chardata[$i]['sor'];
  6123. }
  6124. if ($chardata[$i]['level'] != $prevlevel) {
  6125. $levcount = 0;
  6126. } else {
  6127. $levcount++;
  6128. }
  6129. $prevlevel = $chardata[$i]['level'];
  6130. }
  6131. // I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
  6132. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  6133. for ($i=0; $i < $numchars; $i++) {
  6134. $odd = $chardata[$i]['level'] % 2;
  6135. if ($odd) {
  6136. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')){
  6137. $chardata[$i]['level'] += 1;
  6138. }
  6139. } else {
  6140. if ($chardata[$i]['type'] == 'R') {
  6141. $chardata[$i]['level'] += 1;
  6142. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')){
  6143. $chardata[$i]['level'] += 2;
  6144. }
  6145. }
  6146. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  6147. }
  6148. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  6149. // 1. Segment separators,
  6150. // 2. Paragraph separators,
  6151. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  6152. // 4. Any sequence of white space characters at the end of the line.
  6153. for ($i=0; $i < $numchars; $i++) {
  6154. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  6155. $chardata[$i]['level'] = $pel;
  6156. } elseif ($chardata[$i]['type'] == 'WS') {
  6157. $j = $i+1;
  6158. while ($j < $numchars) {
  6159. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  6160. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  6161. $chardata[$i]['level'] = $pel;;
  6162. break;
  6163. } elseif ($chardata[$j]['type'] != 'WS') {
  6164. break;
  6165. }
  6166. $j++;
  6167. }
  6168. }
  6169. }
  6170. // Arabic Shaping
  6171. // Cursively connected scripts, such as Arabic or Syriac, require the selection of positional character shapes that depend on adjacent characters. Shaping is logically applied after the Bidirectional Algorithm is used and is limited to characters within the same directional run.
  6172. if ($arabic) {
  6173. for ($i=0; $i < $numchars; $i++) {
  6174. if ($unicode[$chardata[$i]['char']] == 'AL') {
  6175. if (($i > 0) AND (($i+1) < $numchars) AND
  6176. ($unicode[$chardata[($i-1)]['char']] == 'AL') AND
  6177. ($unicode[$chardata[($i+1)]['char']] == 'AL') AND
  6178. ($chardata[($i-1)]['type'] == $chardata[$i]['type']) AND
  6179. ($chardata[($i+1)]['type'] == $chardata[$i]['type'])) {
  6180. // medial
  6181. if (isset($unicode_arlet[$chardata[$i]['char']][3])) {
  6182. $chardata[$i]['char'] = $unicode_arlet[$chardata[$i]['char']][3];
  6183. }
  6184. } elseif ((($i+1) < $numchars) AND
  6185. ($unicode[$chardata[($i+1)]['char']] == 'AL') AND
  6186. ($chardata[($i+1)]['type'] == $chardata[$i]['type'])) {
  6187. // initial
  6188. if (isset($unicode_arlet[$chardata[$i]['char']][2])) {
  6189. $chardata[$i]['char'] = $unicode_arlet[$chardata[$i]['char']][2];
  6190. }
  6191. } elseif (($i > 0) AND
  6192. ($unicode[$chardata[($i-1)]['char']] == 'AL') AND
  6193. ($chardata[($i-1)]['type'] == $chardata[$i]['type'])) {
  6194. // final
  6195. if (isset($unicode_arlet[$chardata[$i]['char']][1])) {
  6196. $chardata[$i]['char'] = $unicode_arlet[$chardata[$i]['char']][1];
  6197. }
  6198. } elseif (isset($unicode_arlet[$chardata[$i]['char']][0])) {
  6199. // isolated
  6200. $chardata[$i]['char'] = $unicode_arlet[$chardata[$i]['char']][0];
  6201. }
  6202. }
  6203. }
  6204. }
  6205. // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  6206. for ($j=$maxlevel; $j > 0; $j--) {
  6207. $ordarray = Array();
  6208. $revarr = Array();
  6209. $onlevel = false;
  6210. for ($i=0; $i < $numchars; $i++) {
  6211. if ($chardata[$i]['level'] >= $j) {
  6212. $onlevel = true;
  6213. if (isset($unicode_mirror[$chardata[$i]['char']])) {
  6214. // L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true.
  6215. $chardata[$i]['char'] = $unicode_mirror[$chardata[$i]['char']];
  6216. }
  6217. $revarr[] = $chardata[$i];
  6218. } else {
  6219. if($onlevel) {
  6220. $revarr = array_reverse($revarr);
  6221. $ordarray = array_merge($ordarray, $revarr);
  6222. $revarr = Array();
  6223. $onlevel = false;
  6224. }
  6225. $ordarray[] = $chardata[$i];
  6226. }
  6227. }
  6228. if($onlevel) {
  6229. $revarr = array_reverse($revarr);
  6230. $ordarray = array_merge($ordarray, $revarr);
  6231. }
  6232. $chardata = $ordarray;
  6233. }
  6234. $ordarray = array();
  6235. for ($i=0; $i < $numchars; $i++) {
  6236. $ordarray[] = $chardata[$i]['char'];
  6237. }
  6238. return $ordarray;
  6239. }
  6240. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  6241. /*
  6242. * Adds a bookmark.
  6243. * @param string $txt bookmark description.
  6244. * @param int $level bookmark level.
  6245. * @param float $y Ordinate of the boorkmark position (default = -1 = current position).
  6246. * @access public
  6247. * @author Olivier Plathey, Nicola Asuni
  6248. * @since 2.1.002 (2008-02-12)
  6249. */
  6250. function Bookmark($txt, $level=0, $y=-1) {
  6251. if($y == -1) {
  6252. $y = $this->GetY();
  6253. }
  6254. $this->outlines[]=array('t'=>$txt,'l'=>$level,'y'=>$y,'p'=>$this->PageNo());
  6255. }
  6256. /*
  6257. * Create a bookmark PDF string.
  6258. * @access private
  6259. * @author Olivier Plathey, Nicola Asuni
  6260. * @since 2.1.002 (2008-02-12)
  6261. */
  6262. function _putbookmarks() {
  6263. $nb = count($this->outlines);
  6264. if($nb == 0) {
  6265. return;
  6266. }
  6267. $lru = array();
  6268. $level = 0;
  6269. foreach($this->outlines as $i=>$o) {
  6270. if($o['l'] > 0) {
  6271. $parent = $lru[$o['l'] - 1];
  6272. //Set parent and last pointers
  6273. $this->outlines[$i]['parent'] = $parent;
  6274. $this->outlines[$parent]['last'] = $i;
  6275. if($o['l'] > $level) {
  6276. //Level increasing: set first pointer
  6277. $this->outlines[$parent]['first'] = $i;
  6278. }
  6279. } else {
  6280. $this->outlines[$i]['parent']=$nb;
  6281. }
  6282. if($o['l']<=$level and $i>0) {
  6283. //Set prev and next pointers
  6284. $prev = $lru[$o['l']];
  6285. $this->outlines[$prev]['next'] = $i;
  6286. $this->outlines[$i]['prev'] = $prev;
  6287. }
  6288. $lru[$o['l']] = $i;
  6289. $level = $o['l'];
  6290. }
  6291. //Outline items
  6292. $n = $this->n+1;
  6293. foreach($this->outlines as $i=>$o) {
  6294. $this->_newobj();
  6295. $this->_out('<</Title '.$this->_textstring($o['t']));
  6296. $this->_out('/Parent '.($n+$o['parent']).' 0 R');
  6297. if(isset($o['prev']))
  6298. $this->_out('/Prev '.($n+$o['prev']).' 0 R');
  6299. if(isset($o['next']))
  6300. $this->_out('/Next '.($n+$o['next']).' 0 R');
  6301. if(isset($o['first']))
  6302. $this->_out('/First '.($n+$o['first']).' 0 R');
  6303. if(isset($o['last']))
  6304. $this->_out('/Last '.($n+$o['last']).' 0 R');
  6305. $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]',1+2*$o['p'],($this->h-$o['y'])*$this->k));
  6306. $this->_out('/Count 0>>');
  6307. $this->_out('endobj');
  6308. }
  6309. //Outline root
  6310. $this->_newobj();
  6311. $this->OutlineRoot=$this->n;
  6312. $this->_out('<</Type /Outlines /First '.$n.' 0 R');
  6313. $this->_out('/Last '.($n+$lru[0]).' 0 R>>');
  6314. $this->_out('endobj');
  6315. }
  6316. // --- JAVASCRIPT - FORMS ------------------------------
  6317. /*
  6318. * Adds a javascript
  6319. * @access public
  6320. * @author Johannes G�ntert, Nicola Asuni
  6321. * @since 2.1.002 (2008-02-12)
  6322. */
  6323. function IncludeJS($script) {
  6324. $this->javascript .= $script;
  6325. }
  6326. /*
  6327. * Create a javascript PDF string.
  6328. * @access private
  6329. * @author Johannes G�ntert, Nicola Asuni
  6330. * @since 2.1.002 (2008-02-12)
  6331. */
  6332. function _putjavascript() {
  6333. if (empty($this->javascript)) {
  6334. return;
  6335. }
  6336. $this->_newobj();
  6337. $this->n_js = $this->n;
  6338. $this->_out('<<');
  6339. $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R ]');
  6340. $this->_out('>>');
  6341. $this->_out('endobj');
  6342. $this->_newobj();
  6343. $this->_out('<<');
  6344. $this->_out('/S /JavaScript');
  6345. $this->_out('/JS '.$this->_textstring($this->javascript));
  6346. $this->_out('>>');
  6347. $this->_out('endobj');
  6348. }
  6349. /*
  6350. * Convert color to javascript color.
  6351. * @param string $color color name or #RRGGBB
  6352. * @access private
  6353. * @author Denis Van Nuffelen, Nicola Asuni
  6354. * @since 2.1.002 (2008-02-12)
  6355. */
  6356. function _JScolor($color) {
  6357. static $aColors = array('transparent','black','white','red','green','blue','cyan','magenta','yellow','dkGray','gray','ltGray');
  6358. if(substr($color,0,1) == '#') {
  6359. return sprintf("['RGB',%.3f,%.3f,%.3f]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  6360. }
  6361. if(!in_array($color,$aColors)) {
  6362. $this->Error('Invalid color: '.$color);
  6363. }
  6364. return 'color.'.$color;
  6365. }
  6366. /*
  6367. * Adds a javascript form field.
  6368. * @param string $type field type
  6369. * @param string $name field name
  6370. * @param int $x horizontal position
  6371. * @param int $y vertical position
  6372. * @param int $w width
  6373. * @param int $h height
  6374. * @param array $prop array of properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6375. * @access private
  6376. * @author Denis Van Nuffelen, Nicola Asuni
  6377. * @since 2.1.002 (2008-02-12)
  6378. */
  6379. function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  6380. $k = $this->k;
  6381. $this->javascript .= sprintf("f=addField('%s','%s',%d,[%.2f,%.2f,%.2f,%.2f]);",$name,$type,$this->PageNo()-1,$x*$k,($this->h-$y)*$k+1,($x+$w)*$k,($this->h-$y-$h)*$k+1);
  6382. $this->javascript .= 'f.textSize='.$this->FontSizePt.';';
  6383. while(list($key, $val) = each($prop)) {
  6384. if (strcmp(substr($key,-5),"Color") == 0) {
  6385. $val = $this->_JScolor($val);
  6386. } else {
  6387. $val = "'".$val."'";
  6388. }
  6389. $this->javascript .= "f.".$key."=".$val.";";
  6390. }
  6391. $this->x+=$w;
  6392. }
  6393. /*
  6394. * Creates a text field
  6395. * @param string $name field name
  6396. * @param int $w width
  6397. * @param int $h height
  6398. * @param string $prop properties. The value property allows to set the initial value. The multiline property allows to define the field as multiline. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6399. * @access public
  6400. * @author Denis Van Nuffelen, Nicola Asuni
  6401. * @since 2.1.002 (2008-02-12)
  6402. */
  6403. function TextField($name, $w, $h, $prop=array()) {
  6404. $this->_addfield('text',$name,$this->x,$this->y,$w,$h,$prop);
  6405. }
  6406. /*
  6407. * Creates a RadioButton field
  6408. * @param string $name field name
  6409. * @param int $w width
  6410. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6411. * @access public
  6412. * @author Nicola Asuni
  6413. * @since 2.2.003 (2008-03-03)
  6414. */
  6415. function RadioButton($name, $w, $prop=array()) {
  6416. if(!isset($prop['strokeColor'])) {
  6417. $prop['strokeColor']='black';
  6418. }
  6419. $this->_addfield('radiobutton',$name,$this->x,$this->y,$w,$w,$prop);
  6420. }
  6421. /*
  6422. * Creates a List-box field
  6423. * @param string $name field name
  6424. * @param int $w width
  6425. * @param int $h height
  6426. * @param array $values array containing the list of values.
  6427. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6428. * @access public
  6429. * @author Nicola Asuni
  6430. * @since 2.2.003 (2008-03-03)
  6431. */
  6432. function ListBox($name, $w, $h, $values, $prop=array()) {
  6433. if(!isset($prop['strokeColor'])) {
  6434. $prop['strokeColor']='ltGray';
  6435. }
  6436. $this->_addfield('listbox',$name,$this->x,$this->y,$w,$h,$prop);
  6437. $s = '';
  6438. foreach($values as $value) {
  6439. $s .= "'".addslashes($value)."',";
  6440. }
  6441. $this->javascript .= 'f.setItems(['.substr($s,0,-1).']);';
  6442. }
  6443. /*
  6444. * Creates a Combo-box field
  6445. * @param string $name field name
  6446. * @param int $w width
  6447. * @param int $h height
  6448. * @param array $values array containing the list of values.
  6449. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6450. * @access public
  6451. * @author Denis Van Nuffelen, Nicola Asuni
  6452. * @since 2.1.002 (2008-02-12)
  6453. */
  6454. function ComboBox($name, $w, $h, $values, $prop=array()) {
  6455. $this->_addfield('combobox',$name,$this->x,$this->y,$w,$h,$prop);
  6456. $s = '';
  6457. foreach($values as $value) {
  6458. $s .= "'".addslashes($value)."',";
  6459. }
  6460. $this->javascript .= 'f.setItems(['.substr($s,0,-1).']);';
  6461. }
  6462. /*
  6463. * Creates a CheckBox field
  6464. * @param string $name field name
  6465. * @param int $w width
  6466. * @param boolean $checked define the initial state (default = false).
  6467. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6468. * @access public
  6469. * @author Denis Van Nuffelen, Nicola Asuni
  6470. * @since 2.1.002 (2008-02-12)
  6471. */
  6472. function CheckBox($name, $w, $checked=false, $prop=array()) {
  6473. $prop['value'] = ($checked ? 'Yes' : 'Off');
  6474. if(!isset($prop['strokeColor'])) {
  6475. $prop['strokeColor']='black';
  6476. }
  6477. $this->_addfield('checkbox',$name,$this->x,$this->y,$w,$w,$prop);
  6478. }
  6479. /*
  6480. * Creates a button field
  6481. * @param string $name field name
  6482. * @param int $w width
  6483. * @param int $h height
  6484. * @param string $caption caption.
  6485. * @param string $action action triggered by the button (JavaScript code).
  6486. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  6487. * @access public
  6488. * @author Denis Van Nuffelen, Nicola Asuni
  6489. * @since 2.1.002 (2008-02-12)
  6490. */
  6491. function Button($name, $w, $h, $caption, $action, $prop=array()) {
  6492. if(!isset($prop['strokeColor'])) {
  6493. $prop['strokeColor']='black';
  6494. }
  6495. if(!isset($prop['borderStyle'])) {
  6496. $prop['borderStyle']='beveled';
  6497. }
  6498. $this->_addfield('button',$name,$this->x,$this->y,$w,$h,$prop);
  6499. $this->javascript .= "f.buttonSetCaption('".addslashes($caption)."');";
  6500. $this->javascript .= "f.setAction('MouseUp','".addslashes($action)."');";
  6501. $this->javascript .= "f.highlight='push';";
  6502. $this->javascript .= 'f.print=false;';
  6503. }
  6504. // END JAVASCRIPT - FORMS ------------------------------
  6505. } // END OF TCPDF CLASS
  6506. //Handle special IE contype request
  6507. if(isset($_SERVER['HTTP_USER_AGENT']) AND ($_SERVER['HTTP_USER_AGENT']=='contype')) {
  6508. header('Content-Type: application/pdf');
  6509. exit;
  6510. }
  6511. }
  6512. //============================================================+
  6513. // END OF FILE
  6514. //============================================================+
  6515. ?>