PageRenderTime 143ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 3ms

/include/tcpdf/tcpdf.php

https://bitbucket.org/thomashii/vtigercrm-5.4-for-postgresql
PHP | 13746 lines | 8798 code | 521 blank | 4427 comment | 2027 complexity | 915a05fc764b6d60ecaaeca45bdf6cfa MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Begin : 2002-08-03
  5. // Last Update : 2009-05-23
  6. // Author : Nicola Asuni - info@tecnick.com - http://www.tcpdf.org
  7. // Version : 4.6.012
  8. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  9. // ----------------------------------------------------------------------------
  10. // Copyright (C) 2002-2009 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU Lesser General Public License as published by
  14. // the Free Software Foundation, either version 2.1 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. // See LICENSE.TXT file for more information.
  26. // ----------------------------------------------------------------------------
  27. //
  28. // Description : This is a PHP class for generating PDF documents without
  29. // requiring external extensions.
  30. //
  31. // NOTE:
  32. // This class was originally derived in 2002 from the Public
  33. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  34. // but now is almost entirely rewritten.
  35. //
  36. // Main features:
  37. // * no external libraries are required for the basic functions;
  38. // * supports all ISO page formats;
  39. // * supports custom page formats, margins and units of measure;
  40. // * supports UTF-8 Unicode and Right-To-Left languages;
  41. // * supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  42. // * supports document encryption;
  43. // * includes methods to publish some XHTML code;
  44. // * includes graphic (geometric) and transformation methods;
  45. // * includes Javascript and forms support;
  46. // * includes a method to print various barcode formats: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS;
  47. // * includes methods to set Bookmarks and print a Table of Content;
  48. // * includes methods to move and delete pages;
  49. // * includes methods for automatic page header and footer management;
  50. // * supports automatic page break;
  51. // * supports automatic page numbering and page groups;
  52. // * supports automatic line break and text justification;
  53. // * supports JPEG and PNG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)
  54. // * supports stroke and clipping mode for text;
  55. // * supports clipping masks;
  56. // * supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  57. // * supports several annotations, including links, text and file attachments;
  58. // * supports page compression (requires zlib extension);
  59. // * supports text hyphenation.
  60. // * supports transactions to UNDO commands.
  61. //
  62. // -----------------------------------------------------------
  63. // THANKS TO:
  64. //
  65. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  66. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  67. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  68. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  69. // dullus for text Justification.
  70. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  71. // Patrick Benny for text stretch suggestion on Cell().
  72. // Johannes Güntert for JavaScript support.
  73. // Denis Van Nuffelen for Dynamic Form.
  74. // Jacek Czekaj for multibyte justification
  75. // Anthony Ferrara for the reintroduction of legacy image methods.
  76. // Sourceforge user 1707880 (hucste) for line-trough mode.
  77. // Larry Stanbery for page groups.
  78. // Martin Hall-May for transparency.
  79. // Aaron C. Spike for Polycurve method.
  80. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  81. // Moritz Wagner and Andreas Wurmser for graphic functions.
  82. // Andrew Whitehead for core fonts support.
  83. // Esteban Joël Marín for OpenType font conversion.
  84. // Teus Hagen for several suggestions and fixes.
  85. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  86. // Kosmas Papachristos for some CSS improvements.
  87. // Marcel Partap for some fixes.
  88. // Won Kyu Park for several suggestions, fixes and patches.
  89. // Anyone that has reported a bug or sent a suggestion.
  90. //============================================================+
  91. /**
  92. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  93. * TCPDF project (http://www.tcpdf.org) was originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  94. * <h3>TCPDF main features are:</h3>
  95. * <ul>
  96. * <li>no external libraries are required for the basic functions;</li>
  97. * <li>supports all ISO page formats;</li>
  98. * <li>supports custom page formats, margins and units of measure;</li>
  99. * <li>supports UTF-8 Unicode and Right-To-Left languages;</li>
  100. * <li>supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;</li>
  101. * <li>supports document encryption;</li>
  102. * <li>includes methods to publish some XHTML code;</li>
  103. * <li>includes graphic (geometric) and transformation methods;</li>
  104. * <li>includes Javascript and forms support;</li>
  105. * <li>includes a method to print various barcode formats: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS;</li>
  106. * <li>includes methods to set Bookmarks and print a Table of Content;</li>
  107. * <li>includes methods to move and delete pages;</li>
  108. * <li>includes methods for automatic page header and footer management;</li>
  109. * <li>supports automatic page break;</li>
  110. * <li>supports automatic page numbering and page groups;</li>
  111. * <li>supports automatic line break and text justification;</li>
  112. * <li>supports JPEG and PNG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)</li>
  113. * <li>supports stroke and clipping mode for text;</li>
  114. * <li>supports clipping masks;</li>
  115. * <li>supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  116. * <li>supports several annotations, including links, text and file attachments;</li>
  117. * <li>supports page compression (requires zlib extension);</li>
  118. * <li>supports text hyphenation.</li>
  119. * <li>supports transactions to UNDO commands.</li>
  120. * </ul>
  121. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  122. * @package com.tecnick.tcpdf
  123. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  124. * @author Nicola Asuni
  125. * @copyright 2002-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  126. * @link http://www.tcpdf.org
  127. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  128. * @version 4.6.012
  129. */
  130. /**
  131. * main configuration file
  132. */
  133. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  134. // includes some support files
  135. /**
  136. * unicode data
  137. */
  138. require_once(dirname(__FILE__).'/unicode_data.php');
  139. /**
  140. * html colors table
  141. */
  142. require_once(dirname(__FILE__).'/htmlcolors.php');
  143. if (!class_exists('TCPDF', false)) {
  144. /**
  145. * define default PDF document producer
  146. */
  147. define('PDF_PRODUCER', 'TCPDF 4.6.012 (http://www.tcpdf.org)');
  148. /**
  149. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  150. * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  151. * @name TCPDF
  152. * @package com.tecnick.tcpdf
  153. * @version 4.6.012
  154. * @author Nicola Asuni - info@tecnick.com
  155. * @link http://www.tcpdf.org
  156. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  157. */
  158. class TCPDF {
  159. // protected or Protected properties
  160. /**
  161. * @var current page number
  162. * @access protected
  163. */
  164. protected $page;
  165. /**
  166. * @var current object number
  167. * @access protected
  168. */
  169. protected $n;
  170. /**
  171. * @var array of object offsets
  172. * @access protected
  173. */
  174. protected $offsets;
  175. /**
  176. * @var buffer holding in-memory PDF
  177. * @access protected
  178. */
  179. protected $buffer;
  180. /**
  181. * @var array containing pages
  182. * @access protected
  183. */
  184. protected $pages = array();
  185. /**
  186. * @var current document state
  187. * @access protected
  188. */
  189. protected $state;
  190. /**
  191. * @var compression flag
  192. * @access protected
  193. */
  194. protected $compress;
  195. /**
  196. * @var current page orientation (P = Portrait, L = Landscape)
  197. * @access protected
  198. */
  199. protected $CurOrientation;
  200. /**
  201. * @var array that stores page dimensions and graphic status.<ul><li>$this->pagedim[$this->page]['w'] => page_width_in_points</li><li>$this->pagedim[$this->page]['h'] => height in points</li><li>$this->pagedim[$this->page]['wk'] => page_width_in_points</li><li>$this->pagedim[$this->page]['hk'] => height</li><li>$this->pagedim[$this->page]['tm'] => top_margin</li><li>$this->pagedim[$this->page]['bm'] => bottom_margin</li><li>$this->pagedim[$this->page]['lm'] => left_margin</li><li>$this->pagedim[$this->page]['rm'] => right_margin</li><li>$this->pagedim[$this->page]['pb'] => auto_page_break</li><li>$this->pagedim[$this->page]['or'] => page_orientation</li><li>$this->pagedim[$this->page]['olm'] => original_left_margin</li><li>$this->pagedim[$this->page]['orm'] => original_right_margin</li></ul>
  202. * @access protected
  203. */
  204. protected $pagedim = array();
  205. /**
  206. * @var scale factor (number of points in user unit)
  207. * @access protected
  208. */
  209. protected $k;
  210. /**
  211. * @var width of page format in points
  212. * @access protected
  213. */
  214. protected $fwPt;
  215. /**
  216. * @var height of page format in points
  217. * @access protected
  218. */
  219. protected $fhPt;
  220. /**
  221. * @var current width of page in points
  222. * @access protected
  223. */
  224. protected $wPt;
  225. /**
  226. * @var current height of page in points
  227. * @access protected
  228. */
  229. protected $hPt;
  230. /**
  231. * @var current width of page in user unit
  232. * @access protected
  233. */
  234. protected $w;
  235. /**
  236. * @var current height of page in user unit
  237. * @access protected
  238. */
  239. protected $h;
  240. /**
  241. * @var left margin
  242. * @access protected
  243. */
  244. protected $lMargin;
  245. /**
  246. * @var top margin
  247. * @access protected
  248. */
  249. protected $tMargin;
  250. /**
  251. * @var right margin
  252. * @access protected
  253. */
  254. protected $rMargin;
  255. /**
  256. * @var page break margin
  257. * @access protected
  258. */
  259. protected $bMargin;
  260. /**
  261. * @var cell internal padding
  262. * @access protected
  263. */
  264. //protected
  265. public $cMargin;
  266. /**
  267. * @var cell internal padding (previous value)
  268. * @access protected
  269. */
  270. protected $oldcMargin;
  271. /**
  272. * @var current horizontal position in user unit for cell positioning
  273. * @access protected
  274. */
  275. protected $x;
  276. /**
  277. * @var current vertical position in user unit for cell positioning
  278. * @access protected
  279. */
  280. protected $y;
  281. /**
  282. * @var height of last cell printed
  283. * @access protected
  284. */
  285. protected $lasth;
  286. /**
  287. * @var line width in user unit
  288. * @access protected
  289. */
  290. protected $LineWidth;
  291. /**
  292. * @var array of standard font names
  293. * @access protected
  294. */
  295. protected $CoreFonts;
  296. /**
  297. * @var array of used fonts
  298. * @access protected
  299. */
  300. protected $fonts = array();
  301. /**
  302. * @var array of font files
  303. * @access protected
  304. */
  305. protected $FontFiles = array();
  306. /**
  307. * @var array of encoding differences
  308. * @access protected
  309. */
  310. protected $diffs = array();
  311. /**
  312. * @var array of used images
  313. * @access protected
  314. */
  315. protected $images = array();
  316. /**
  317. * @var array of Annotations in pages
  318. * @access protected
  319. */
  320. protected $PageAnnots = array();
  321. /**
  322. * @var array of internal links
  323. * @access protected
  324. */
  325. protected $links = array();
  326. /**
  327. * @var current font family
  328. * @access protected
  329. */
  330. protected $FontFamily;
  331. /**
  332. * @var current font style
  333. * @access protected
  334. */
  335. protected $FontStyle;
  336. /**
  337. * @var current font ascent (distance between font top and baseline)
  338. * @access protected
  339. * @since 2.8.000 (2007-03-29)
  340. */
  341. protected $FontAscent;
  342. /**
  343. * @var current font descent (distance between font bottom and baseline)
  344. * @access protected
  345. * @since 2.8.000 (2007-03-29)
  346. */
  347. protected $FontDescent;
  348. /**
  349. * @var underlining flag
  350. * @access protected
  351. */
  352. protected $underline;
  353. /**
  354. * @var current font info
  355. * @access protected
  356. */
  357. protected $CurrentFont;
  358. /**
  359. * @var current font size in points
  360. * @access protected
  361. */
  362. protected $FontSizePt;
  363. /**
  364. * @var current font size in user unit
  365. * @access protected
  366. */
  367. protected $FontSize;
  368. /**
  369. * @var commands for drawing color
  370. * @access protected
  371. */
  372. protected $DrawColor;
  373. /**
  374. * @var commands for filling color
  375. * @access protected
  376. */
  377. protected $FillColor;
  378. /**
  379. * @var commands for text color
  380. * @access protected
  381. */
  382. protected $TextColor;
  383. /**
  384. * @var indicates whether fill and text colors are different
  385. * @access protected
  386. */
  387. protected $ColorFlag;
  388. /**
  389. * @var automatic page breaking
  390. * @access protected
  391. */
  392. protected $AutoPageBreak;
  393. /**
  394. * @var threshold used to trigger page breaks
  395. * @access protected
  396. */
  397. protected $PageBreakTrigger;
  398. /**
  399. * @var flag set when processing footer
  400. * @access protected
  401. */
  402. protected $InFooter = false;
  403. /**
  404. * @var zoom display mode
  405. * @access protected
  406. */
  407. protected $ZoomMode;
  408. /**
  409. * @var layout display mode
  410. * @access protected
  411. */
  412. protected $LayoutMode;
  413. /**
  414. * @var title
  415. * @access protected
  416. */
  417. protected $title = '';
  418. /**
  419. * @var subject
  420. * @access protected
  421. */
  422. protected $subject = '';
  423. /**
  424. * @var author
  425. * @access protected
  426. */
  427. protected $author = '';
  428. /**
  429. * @var keywords
  430. * @access protected
  431. */
  432. protected $keywords = '';
  433. /**
  434. * @var creator
  435. * @access protected
  436. */
  437. protected $creator = '';
  438. /**
  439. * @var alias for total number of pages
  440. * @access protected
  441. */
  442. protected $AliasNbPages = '{nb}';
  443. /**
  444. * @var alias for page number
  445. * @access protected
  446. */
  447. protected $AliasNumPage = '{pnb}';
  448. /**
  449. * @var right-bottom corner X coordinate of inserted image
  450. * @since 2002-07-31
  451. * @author Nicola Asuni
  452. * @access protected
  453. */
  454. protected $img_rb_x;
  455. /**
  456. * @var right-bottom corner Y coordinate of inserted image
  457. * @since 2002-07-31
  458. * @author Nicola Asuni
  459. * @access protected
  460. */
  461. protected $img_rb_y;
  462. /**
  463. * @var adjusting factor to convert pixels to user units.
  464. * @since 2004-06-14
  465. * @author Nicola Asuni
  466. * @access protected
  467. */
  468. protected $imgscale = 1;
  469. /**
  470. * @var boolean set to true when the input text is unicode (require unicode fonts)
  471. * @since 2005-01-02
  472. * @author Nicola Asuni
  473. * @access protected
  474. */
  475. protected $isunicode = false;
  476. /**
  477. * @var PDF version
  478. * @since 1.5.3
  479. * @access protected
  480. */
  481. protected $PDFVersion = '1.7';
  482. // ----------------------
  483. /**
  484. * @var Minimum distance between header and top page margin.
  485. * @access protected
  486. */
  487. protected $header_margin;
  488. /**
  489. * @var Minimum distance between footer and bottom page margin.
  490. * @access protected
  491. */
  492. protected $footer_margin;
  493. /**
  494. * @var original left margin value
  495. * @access protected
  496. * @since 1.53.0.TC013
  497. */
  498. protected $original_lMargin;
  499. /**
  500. * @var original right margin value
  501. * @access protected
  502. * @since 1.53.0.TC013
  503. */
  504. protected $original_rMargin;
  505. /**
  506. * @var Header font.
  507. * @access protected
  508. */
  509. protected $header_font;
  510. /**
  511. * @var Footer font.
  512. * @access protected
  513. */
  514. protected $footer_font;
  515. /**
  516. * @var Language templates.
  517. * @access protected
  518. */
  519. protected $l;
  520. /**
  521. * @var Barcode to print on page footer (only if set).
  522. * @access protected
  523. */
  524. protected $barcode = false;
  525. /**
  526. * @var If true prints header
  527. * @access protected
  528. */
  529. protected $print_header = true;
  530. /**
  531. * @var If true prints footer.
  532. * @access protected
  533. */
  534. protected $print_footer = true;
  535. /**
  536. * @var Header image logo.
  537. * @access protected
  538. */
  539. protected $header_logo = '';
  540. /**
  541. * @var Header image logo width in mm.
  542. * @access protected
  543. */
  544. protected $header_logo_width = 30;
  545. /**
  546. * @var String to print as title on document header.
  547. * @access protected
  548. */
  549. protected $header_title = '';
  550. /**
  551. * @var String to print on document header.
  552. * @access protected
  553. */
  554. protected $header_string = '';
  555. /**
  556. * @var Default number of columns for html table.
  557. * @access protected
  558. */
  559. protected $default_table_columns = 4;
  560. // variables for html parser
  561. /**
  562. * @var HTML PARSER: array to store current link and rendering styles.
  563. * @access protected
  564. */
  565. protected $HREF = array();
  566. /**
  567. * @var store a list of available fonts on filesystem.
  568. * @access protected
  569. */
  570. protected $fontlist = array();
  571. /**
  572. * @var current foreground color
  573. * @access protected
  574. */
  575. protected $fgcolor;
  576. /**
  577. * @var HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  578. * @access protected
  579. */
  580. protected $listordered = array();
  581. /**
  582. * @var HTML PARSER: array count list items on nested lists.
  583. * @access protected
  584. */
  585. protected $listcount = array();
  586. /**
  587. * @var HTML PARSER: current list nesting level.
  588. * @access protected
  589. */
  590. protected $listnum = 0;
  591. /**
  592. * @var HTML PARSER: indent amount for lists.
  593. * @access protected
  594. */
  595. protected $listindent;
  596. /**
  597. * @var current background color
  598. * @access protected
  599. */
  600. protected $bgcolor;
  601. /**
  602. * @var Store temporary font size in points.
  603. * @access protected
  604. */
  605. protected $tempfontsize = 10;
  606. /**
  607. * @var spacer for LI tags.
  608. * @access protected
  609. */
  610. protected $lispacer = '';
  611. /**
  612. * @var default encoding
  613. * @access protected
  614. * @since 1.53.0.TC010
  615. */
  616. protected $encoding = 'UTF-8';
  617. /**
  618. * @var PHP internal encoding
  619. * @access protected
  620. * @since 1.53.0.TC016
  621. */
  622. protected $internal_encoding;
  623. /**
  624. * @var indicates if the document language is Right-To-Left
  625. * @access protected
  626. * @since 2.0.000
  627. */
  628. protected $rtl = false;
  629. /**
  630. * @var used to force RTL or LTR string inversion
  631. * @access protected
  632. * @since 2.0.000
  633. */
  634. protected $tmprtl = false;
  635. // --- Variables used for document encryption:
  636. /**
  637. * Indicates whether document is protected
  638. * @access protected
  639. * @since 2.0.000 (2008-01-02)
  640. */
  641. protected $encrypted;
  642. /**
  643. * U entry in pdf document
  644. * @access protected
  645. * @since 2.0.000 (2008-01-02)
  646. */
  647. protected $Uvalue;
  648. /**
  649. * O entry in pdf document
  650. * @access protected
  651. * @since 2.0.000 (2008-01-02)
  652. */
  653. protected $Ovalue;
  654. /**
  655. * P entry in pdf document
  656. * @access protected
  657. * @since 2.0.000 (2008-01-02)
  658. */
  659. protected $Pvalue;
  660. /**
  661. * encryption object id
  662. * @access protected
  663. * @since 2.0.000 (2008-01-02)
  664. */
  665. protected $enc_obj_id;
  666. /**
  667. * last RC4 key encrypted (cached for optimisation)
  668. * @access protected
  669. * @since 2.0.000 (2008-01-02)
  670. */
  671. protected $last_rc4_key;
  672. /**
  673. * last RC4 computed key
  674. * @access protected
  675. * @since 2.0.000 (2008-01-02)
  676. */
  677. protected $last_rc4_key_c;
  678. /**
  679. * RC4 padding
  680. * @access protected
  681. */
  682. protected $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";
  683. /**
  684. * RC4 encryption key
  685. * @access protected
  686. */
  687. protected $encryption_key;
  688. // --- bookmark ---
  689. /**
  690. * Outlines for bookmark
  691. * @access protected
  692. * @since 2.1.002 (2008-02-12)
  693. */
  694. protected $outlines = array();
  695. /**
  696. * Outline root for bookmark
  697. * @access protected
  698. * @since 2.1.002 (2008-02-12)
  699. */
  700. protected $OutlineRoot;
  701. // --- javascript and form ---
  702. /**
  703. * javascript code
  704. * @access protected
  705. * @since 2.1.002 (2008-02-12)
  706. */
  707. protected $javascript = '';
  708. /**
  709. * javascript counter
  710. * @access protected
  711. * @since 2.1.002 (2008-02-12)
  712. */
  713. protected $n_js;
  714. /**
  715. * line trough state
  716. * @access protected
  717. * @since 2.8.000 (2008-03-19)
  718. */
  719. protected $linethrough;
  720. // --- Variables used for User's Rights ---
  721. // See PDF reference chapter 8.7 Digital Signatures
  722. /**
  723. * If true enables user's rights on PDF reader
  724. * @access protected
  725. * @since 2.9.000 (2008-03-26)
  726. */
  727. protected $ur;
  728. /**
  729. * Names specifying additional document-wide usage rights for the document.
  730. * @access protected
  731. * @since 2.9.000 (2008-03-26)
  732. */
  733. protected $ur_document;
  734. /**
  735. * Names specifying additional annotation-related usage rights for the document.
  736. * @access protected
  737. * @since 2.9.000 (2008-03-26)
  738. */
  739. protected $ur_annots;
  740. /**
  741. * Names specifying additional form-field-related usage rights for the document.
  742. * @access protected
  743. * @since 2.9.000 (2008-03-26)
  744. */
  745. protected $ur_form;
  746. /**
  747. * Names specifying additional signature-related usage rights for the document.
  748. * @access protected
  749. * @since 2.9.000 (2008-03-26)
  750. */
  751. protected $ur_signature;
  752. /**
  753. * Dot Per Inch Document Resolution (do not change)
  754. * @access protected
  755. * @since 3.0.000 (2008-03-27)
  756. */
  757. protected $dpi = 72;
  758. /**
  759. * Array of page numbers were a new page group was started
  760. * @access protected
  761. * @since 3.0.000 (2008-03-27)
  762. */
  763. protected $newpagegroup = array();
  764. /**
  765. * Contains the number of pages of the groups
  766. * @access protected
  767. * @since 3.0.000 (2008-03-27)
  768. */
  769. protected $pagegroups;
  770. /**
  771. * Contains the alias of the current page group
  772. * @access protected
  773. * @since 3.0.000 (2008-03-27)
  774. */
  775. protected $currpagegroup;
  776. /**
  777. * Restrict the rendering of some elements to screen or printout.
  778. * @access protected
  779. * @since 3.0.000 (2008-03-27)
  780. */
  781. protected $visibility = 'all';
  782. /**
  783. * Print visibility.
  784. * @access protected
  785. * @since 3.0.000 (2008-03-27)
  786. */
  787. protected $n_ocg_print;
  788. /**
  789. * View visibility.
  790. * @access protected
  791. * @since 3.0.000 (2008-03-27)
  792. */
  793. protected $n_ocg_view;
  794. /**
  795. * Array of transparency objects and parameters.
  796. * @access protected
  797. * @since 3.0.000 (2008-03-27)
  798. */
  799. protected $extgstates;
  800. /**
  801. * Set the default JPEG compression quality (1-100)
  802. * @access protected
  803. * @since 3.0.000 (2008-03-27)
  804. */
  805. protected $jpeg_quality;
  806. /**
  807. * Default cell height ratio.
  808. * @access protected
  809. * @since 3.0.014 (2008-05-23)
  810. */
  811. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  812. /**
  813. * PDF viewer preferences.
  814. * @access protected
  815. * @since 3.1.000 (2008-06-09)
  816. */
  817. protected $viewer_preferences;
  818. /**
  819. * A name object specifying how the document should be displayed when opened.
  820. * @access protected
  821. * @since 3.1.000 (2008-06-09)
  822. */
  823. protected $PageMode;
  824. /**
  825. * Array for storing gradient information.
  826. * @access protected
  827. * @since 3.1.000 (2008-06-09)
  828. */
  829. protected $gradients = array();
  830. /**
  831. * Array used to store positions inside the pages buffer.
  832. * keys are the page numbers
  833. * @access protected
  834. * @since 3.2.000 (2008-06-26)
  835. */
  836. protected $intmrk = array();
  837. /**
  838. * Array used to store footer positions of each page.
  839. * @access protected
  840. * @since 3.2.000 (2008-07-01)
  841. */
  842. protected $footerpos = array();
  843. /**
  844. * Array used to store footer lenght of each page.
  845. * @access protected
  846. * @since 4.0.014 (2008-07-29)
  847. */
  848. protected $footerlen = array();
  849. /**
  850. * True if a newline is created.
  851. * @access protected
  852. * @since 3.2.000 (2008-07-01)
  853. */
  854. protected $newline = true;
  855. /**
  856. * End position of the latest inserted line
  857. * @access protected
  858. * @since 3.2.000 (2008-07-01)
  859. */
  860. protected $endlinex = 0;
  861. /**
  862. * PDF string for last line width
  863. * @access protected
  864. * @since 4.0.006 (2008-07-16)
  865. */
  866. protected $linestyleWidth = '';
  867. /**
  868. * PDF string for last line width
  869. * @access protected
  870. * @since 4.0.006 (2008-07-16)
  871. */
  872. protected $linestyleCap = '0 J';
  873. /**
  874. * PDF string for last line width
  875. * @access protected
  876. * @since 4.0.006 (2008-07-16)
  877. */
  878. protected $linestyleJoin = '0 j';
  879. /**
  880. * PDF string for last line width
  881. * @access protected
  882. * @since 4.0.006 (2008-07-16)
  883. */
  884. protected $linestyleDash = '[] 0 d';
  885. /**
  886. * True if marked-content sequence is open
  887. * @access protected
  888. * @since 4.0.013 (2008-07-28)
  889. */
  890. protected $openMarkedContent = false;
  891. /**
  892. * Count the latest inserted vertical spaces on HTML
  893. * @access protected
  894. * @since 4.0.021 (2008-08-24)
  895. */
  896. protected $htmlvspace = 0;
  897. /**
  898. * Array of Spot colors
  899. * @access protected
  900. * @since 4.0.024 (2008-09-12)
  901. */
  902. protected $spot_colors = array();
  903. /**
  904. * Symbol used for HTML unordered list items
  905. * @access protected
  906. * @since 4.0.028 (2008-09-26)
  907. */
  908. protected $lisymbol = '';
  909. /**
  910. * String used to mark the beginning and end of EPS image blocks
  911. * @access protected
  912. * @since 4.1.000 (2008-10-18)
  913. */
  914. protected $epsmarker = 'x#!#EPS#!#x';
  915. /**
  916. * Array of transformation matrix
  917. * @access protected
  918. * @since 4.2.000 (2008-10-29)
  919. */
  920. protected $transfmatrix = array();
  921. /**
  922. * Booklet mode for double-sided pages
  923. * @access protected
  924. * @since 4.2.000 (2008-10-29)
  925. */
  926. protected $booklet = false;
  927. /**
  928. * Epsilon value used for float calculations
  929. * @access protected
  930. * @since 4.2.000 (2008-10-29)
  931. */
  932. protected $feps = 0.001;
  933. /**
  934. * Array used for custom vertical spaces for HTML tags
  935. * @access protected
  936. * @since 4.2.001 (2008-10-30)
  937. */
  938. protected $tagvspaces = array();
  939. /**
  940. * @var HTML PARSER: custom indent amount for lists.
  941. * Negative value means disabled.
  942. * @access protected
  943. * @since 4.2.007 (2008-11-12)
  944. */
  945. protected $customlistindent = -1;
  946. /**
  947. * @var if true keeps the border open for the cell sides that cross the page.
  948. * @access protected
  949. * @since 4.2.010 (2008-11-14)
  950. */
  951. protected $opencell = true;
  952. /**
  953. * @var array of files to embedd
  954. * @access protected
  955. * @since 4.4.000 (2008-12-07)
  956. */
  957. protected $embeddedfiles = array();
  958. /**
  959. * @var boolean true when inside html pre tag
  960. * @access protected
  961. * @since 4.4.001 (2008-12-08)
  962. */
  963. protected $premode = false;
  964. /**
  965. * Array used to store positions of graphics transformation blocks inside the page buffer.
  966. * keys are the page numbers
  967. * @access protected
  968. * @since 4.4.002 (2008-12-09)
  969. */
  970. protected $transfmrk = array();
  971. /**
  972. * Default color for html links
  973. * @access protected
  974. * @since 4.4.003 (2008-12-09)
  975. */
  976. protected $htmlLinkColorArray = array(0, 0, 255);
  977. /**
  978. * Default font style to add to html links
  979. * @access protected
  980. * @since 4.4.003 (2008-12-09)
  981. */
  982. protected $htmlLinkFontStyle = 'U';
  983. /**
  984. * Counts the number of pages.
  985. * @access protected
  986. * @since 4.5.000 (2008-12-31)
  987. */
  988. protected $numpages = 0;
  989. /**
  990. * Array containing page lenghts in bytes.
  991. * @access protected
  992. * @since 4.5.000 (2008-12-31)
  993. */
  994. protected $pagelen = array();
  995. /**
  996. * Counts the number of pages.
  997. * @access protected
  998. * @since 4.5.000 (2008-12-31)
  999. */
  1000. protected $numimages = 0;
  1001. /**
  1002. * Store the image keys.
  1003. * @access protected
  1004. * @since 4.5.000 (2008-12-31)
  1005. */
  1006. protected $imagekeys = array();
  1007. /**
  1008. * Lenght of the buffer in bytes.
  1009. * @access protected
  1010. * @since 4.5.000 (2008-12-31)
  1011. */
  1012. protected $bufferlen = 0;
  1013. /**
  1014. * If true enables disk caching.
  1015. * @access protected
  1016. * @since 4.5.000 (2008-12-31)
  1017. */
  1018. protected $diskcache = false;
  1019. /**
  1020. * Counts the number of fonts.
  1021. * @access protected
  1022. * @since 4.5.000 (2009-01-02)
  1023. */
  1024. protected $numfonts = 0;
  1025. /**
  1026. * Store the font keys.
  1027. * @access protected
  1028. * @since 4.5.000 (2009-01-02)
  1029. */
  1030. protected $fontkeys = array();
  1031. /**
  1032. * Store the fage status (true when opened, false when closed).
  1033. * @access protected
  1034. * @since 4.5.000 (2009-01-02)
  1035. */
  1036. protected $pageopen = array();
  1037. /**
  1038. * Default monospaced font
  1039. * @access protected
  1040. * @since 4.5.025 (2009-03-10)
  1041. */
  1042. protected $default_monospaced_font = 'courier';
  1043. /**
  1044. * Used to store a cloned copy of the current class object
  1045. * @access protected
  1046. * @since 4.5.029 (2009-03-19)
  1047. */
  1048. protected $objcopy;
  1049. /**
  1050. * Array used to store the lenghts of cache files
  1051. * @access protected
  1052. * @since 4.5.029 (2009-03-19)
  1053. */
  1054. protected $cache_file_lenght = array();
  1055. /**
  1056. * Table header content to be repeated on each new page
  1057. * @access protected
  1058. * @since 4.5.030 (2009-03-20)
  1059. */
  1060. protected $thead = '';
  1061. /**
  1062. * Distance between the top of page and end of table headers on a new page.
  1063. * @access protected
  1064. * @since 4.5.030 (2009-03-20)
  1065. */
  1066. protected $theadMargin = '';
  1067. /**
  1068. * Cache array for UTF8StringToArray() method.
  1069. * @access protected
  1070. * @since 4.5.037 (2009-04-07)
  1071. */
  1072. protected $cache_UTF8StringToArray = array();
  1073. /**
  1074. * Maximum size of cache array used for UTF8StringToArray() method.
  1075. * @access protected
  1076. * @since 4.5.037 (2009-04-07)
  1077. */
  1078. protected $cache_maxsize_UTF8StringToArray = 8;
  1079. /**
  1080. * Current size of cache array used for UTF8StringToArray() method.
  1081. * @access protected
  1082. * @since 4.5.037 (2009-04-07)
  1083. */
  1084. protected $cache_size_UTF8StringToArray = 0;
  1085. /**
  1086. * If true enables document signing
  1087. * @access protected
  1088. * @since 4.6.005 (2009-04-24)
  1089. */
  1090. protected $sign = false;
  1091. /**
  1092. * Signature data
  1093. * @access protected
  1094. * @since 4.6.005 (2009-04-24)
  1095. */
  1096. protected $signature_data = array();
  1097. /**
  1098. * Signature max lenght
  1099. * @access protected
  1100. * @since 4.6.005 (2009-04-24)
  1101. */
  1102. protected $signature_max_lenght = 5120;
  1103. /**
  1104. * Regular expression used to find blank characters used for word-wrapping.
  1105. * @access protected
  1106. * @since 4.6.006 (2009-04-28)
  1107. */
  1108. protected $re_spaces = '/[\s\p{Z}\p{Lo}]/';
  1109. //------------------------------------------------------------
  1110. // METHODS
  1111. //------------------------------------------------------------
  1112. /**
  1113. * This is the class constructor.
  1114. * It allows to set up the page format, the orientation and
  1115. * the measure unit used in all the methods (except for the font sizes).
  1116. * @since 1.0
  1117. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  1118. * @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.
  1119. * @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>
  1120. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  1121. * @param boolean $diskcache if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1122. * @param String $encoding charset encoding; default is UTF-8
  1123. * @access public
  1124. */
  1125. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false) {
  1126. /* Set internal character encoding to ASCII */
  1127. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1128. $this->internal_encoding = mb_internal_encoding();
  1129. mb_internal_encoding('ASCII');
  1130. }
  1131. // set disk caching
  1132. $this->diskcache = $diskcache ? true : false;
  1133. // set language direction
  1134. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  1135. $this->tmprtl = false;
  1136. //Some checks
  1137. $this->_dochecks();
  1138. //Initialization of properties
  1139. $this->isunicode = $unicode;
  1140. $this->page = 0;
  1141. $this->transfmrk[0] = array();
  1142. $this->pagedim = array();
  1143. $this->n = 2;
  1144. $this->buffer = '';
  1145. $this->pages = array();
  1146. $this->state = 0;
  1147. $this->fonts = array();
  1148. $this->FontFiles = array();
  1149. $this->diffs = array();
  1150. $this->images = array();
  1151. $this->links = array();
  1152. $this->gradients = array();
  1153. $this->InFooter = false;
  1154. $this->lasth = 0;
  1155. $this->FontFamily = 'helvetica';
  1156. $this->FontStyle = '';
  1157. $this->FontSizePt = 12;
  1158. $this->underline = false;
  1159. $this->linethrough = false;
  1160. $this->DrawColor = '0 G';
  1161. $this->FillColor = '0 g';
  1162. $this->TextColor = '0 g';
  1163. $this->ColorFlag = false;
  1164. // encryption values
  1165. $this->encrypted = false;
  1166. $this->last_rc4_key = '';
  1167. $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";
  1168. //Standard Unicode fonts
  1169. $this->CoreFonts = array(
  1170. 'courier'=>'Courier',
  1171. 'courierB'=>'Courier-Bold',
  1172. 'courierI'=>'Courier-Oblique',
  1173. 'courierBI'=>'Courier-BoldOblique',
  1174. 'helvetica'=>'Helvetica',
  1175. 'helveticaB'=>'Helvetica-Bold',
  1176. 'helveticaI'=>'Helvetica-Oblique',
  1177. 'helveticaBI'=>'Helvetica-BoldOblique',
  1178. 'times'=>'Times-Roman',
  1179. 'timesB'=>'Times-Bold',
  1180. 'timesI'=>'Times-Italic',
  1181. 'timesBI'=>'Times-BoldItalic',
  1182. 'symbol'=>'Symbol',
  1183. 'zapfdingbats'=>'ZapfDingbats'
  1184. );
  1185. //Set scale factor
  1186. $this->setPageUnit($unit);
  1187. // set page format and orientation
  1188. $this->setPageFormat($format, $orientation);
  1189. //Page margins (1 cm)
  1190. $margin = 28.35 / $this->k;
  1191. $this->SetMargins($margin, $margin);
  1192. //Interior cell margin
  1193. $this->cMargin = $margin / 10;
  1194. //Line width (0.2 mm)
  1195. $this->LineWidth = 0.57 / $this->k;
  1196. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1197. $this->linestyleCap = '0 J';
  1198. $this->linestyleJoin = '0 j';
  1199. $this->linestyleDash = '[] 0 d';
  1200. //Automatic page break
  1201. $this->SetAutoPageBreak(true, (2 * $margin));
  1202. //Full width display mode
  1203. $this->SetDisplayMode('fullwidth');
  1204. //Compression
  1205. $this->SetCompression(true);
  1206. //Set default PDF version number
  1207. $this->PDFVersion = '1.7';
  1208. $this->encoding = $encoding;
  1209. $this->HREF = array();
  1210. $this->getFontsList();
  1211. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1212. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1213. $this->extgstates = array();
  1214. // user's rights
  1215. $this->sign = false;
  1216. $this->ur = false;
  1217. $this->ur_document = '/FullSave';
  1218. $this->ur_annots = '/Create/Delete/Modify/Copy/Import/Export';
  1219. $this->ur_form = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1220. $this->ur_signature = '/Modify';
  1221. // set default JPEG quality
  1222. $this->jpeg_quality = 75;
  1223. // initialize some settings
  1224. $this->utf8Bidi(array(''), '');
  1225. // set default font
  1226. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1227. // check if PCRE Unicode support is enabled
  1228. if (@preg_match('/\pL/u', 'a') == 1) {
  1229. // PCRE unicode support is turned ON
  1230. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1231. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1232. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1233. $this->re_spaces = '/[\s\p{Z}\p{Lo}]/';
  1234. } else {
  1235. // PCRE unicode support is turned OFF
  1236. $this->re_spaces = '/[\s]/';
  1237. }
  1238. }
  1239. /**
  1240. * Default destructor.
  1241. * @access public
  1242. * @since 1.53.0.TC016
  1243. */
  1244. public function __destruct() {
  1245. // restore internal encoding
  1246. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1247. mb_internal_encoding($this->internal_encoding);
  1248. }
  1249. // unset all class variables
  1250. $this->_destroy(true);
  1251. }
  1252. /**
  1253. * Set the units of measure for the document.
  1254. * @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.
  1255. * @access public
  1256. * @since 3.0.015 (2008-06-06)
  1257. */
  1258. public function setPageUnit($unit) {
  1259. //Set scale factor
  1260. switch (strtolower($unit)) {
  1261. // points
  1262. case 'px':
  1263. case 'pt': {
  1264. $this->k = 1;
  1265. break;
  1266. }
  1267. // millimeters
  1268. case 'mm': {
  1269. $this->k = $this->dpi / 25.4;
  1270. break;
  1271. }
  1272. // centimeters
  1273. case 'cm': {
  1274. $this->k = $this->dpi / 2.54;
  1275. break;
  1276. }
  1277. // inches
  1278. case 'in': {
  1279. $this->k = $this->dpi;
  1280. break;
  1281. }
  1282. // unsupported unit
  1283. default : {
  1284. $this->Error('Incorrect unit: '.$unit);
  1285. break;
  1286. }
  1287. }
  1288. if (isset($this->CurOrientation)) {
  1289. $this->setPageOrientation($this->CurOrientation);
  1290. }
  1291. }
  1292. /**
  1293. * Set the page format
  1294. * @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>
  1295. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1296. * @access public
  1297. * @since 3.0.015 (2008-06-06)
  1298. */
  1299. public function setPageFormat($format, $orientation='P') {
  1300. //Page format
  1301. if (is_string($format)) {
  1302. // Page formats (45 standard ISO paper formats and 4 american common formats).
  1303. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  1304. switch (strtoupper($format)) {
  1305. case '4A0': {$format = array(4767.87,6740.79); break;}
  1306. case '2A0': {$format = array(3370.39,4767.87); break;}
  1307. case 'A0': {$format = array(2383.94,3370.39); break;}
  1308. case 'A1': {$format = array(1683.78,2383.94); break;}
  1309. case 'A2': {$format = array(1190.55,1683.78); break;}
  1310. case 'A3': {$format = array(841.89,1190.55); break;}
  1311. case 'A4': default: {$format = array(595.28,841.89); break;}
  1312. case 'A5': {$format = array(419.53,595.28); break;}
  1313. case 'A6': {$format = array(297.64,419.53); break;}
  1314. case 'A7': {$format = array(209.76,297.64); break;}
  1315. case 'A8': {$format = array(147.40,209.76); break;}
  1316. case 'A9': {$format = array(104.88,147.40); break;}
  1317. case 'A10': {$format = array(73.70,104.88); break;}
  1318. case 'B0': {$format = array(2834.65,4008.19); break;}
  1319. case 'B1': {$format = array(2004.09,2834.65); break;}
  1320. case 'B2': {$format = array(1417.32,2004.09); break;}
  1321. case 'B3': {$format = array(1000.63,1417.32); break;}
  1322. case 'B4': {$format = array(708.66,1000.63); break;}
  1323. case 'B5': {$format = array(498.90,708.66); break;}
  1324. case 'B6': {$format = array(354.33,498.90); break;}
  1325. case 'B7': {$format = array(249.45,354.33); break;}
  1326. case 'B8': {$format = array(175.75,249.45); break;}
  1327. case 'B9': {$format = array(124.72,175.75); break;}
  1328. case 'B10': {$format = array(87.87,124.72); break;}
  1329. case 'C0': {$format = array(2599.37,3676.54); break;}
  1330. case 'C1': {$format = array(1836.85,2599.37); break;}
  1331. case 'C2': {$format = array(1298.27,1836.85); break;}
  1332. case 'C3': {$format = array(918.43,1298.27); break;}
  1333. case 'C4': {$format = array(649.13,918.43); break;}
  1334. case 'C5': {$format = array(459.21,649.13); break;}
  1335. case 'C6': {$format = array(323.15,459.21); break;}
  1336. case 'C7': {$format = array(229.61,323.15); break;}
  1337. case 'C8': {$format = array(161.57,229.61); break;}
  1338. case 'C9': {$format = array(113.39,161.57); break;}
  1339. case 'C10': {$format = array(79.37,113.39); break;}
  1340. case 'RA0': {$format = array(2437.80,3458.27); break;}
  1341. case 'RA1': {$format = array(1729.13,2437.80); break;}
  1342. case 'RA2': {$format = array(1218.90,1729.13); break;}
  1343. case 'RA3': {$format = array(864.57,1218.90); break;}
  1344. case 'RA4': {$format = array(609.45,864.57); break;}
  1345. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  1346. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  1347. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  1348. case 'SRA3': {$format = array(907.09,1275.59); break;}
  1349. case 'SRA4': {$format = array(637.80,907.09); break;}
  1350. case 'LETTER': {$format = array(612.00,792.00); break;}
  1351. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  1352. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  1353. case 'FOLIO': {$format = array(612.00,936.00); break;}
  1354. }
  1355. $this->fwPt = $format[0];
  1356. $this->fhPt = $format[1];
  1357. } else {
  1358. $this->fwPt = $format[0] * $this->k;
  1359. $this->fhPt = $format[1] * $this->k;
  1360. }
  1361. $this->setPageOrientation($orientation);
  1362. }
  1363. /**
  1364. * Set page orientation.
  1365. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1366. * @param boolean $autopagebreak Boolean indicating if auto-page-break mode should be on or off.
  1367. * @param float $bottommargin bottom margin of the page.
  1368. * @access public
  1369. * @since 3.0.015 (2008-06-06)
  1370. */
  1371. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1372. $orientation = strtoupper($orientation);
  1373. if (($orientation == 'P') OR ($orientation == 'PORTRAIT')) {
  1374. $this->CurOrientation = 'P';
  1375. $this->wPt = $this->fwPt;
  1376. $this->hPt = $this->fhPt;
  1377. } elseif (($orientation == 'L') OR ($orientation == 'LANDSCAPE')) {
  1378. $this->CurOrientation = 'L';
  1379. $this->wPt = $this->fhPt;
  1380. $this->hPt = $this->fwPt;
  1381. } else {
  1382. $this->Error('Incorrect orientation: '.$orientation);
  1383. }
  1384. $this->w = $this->wPt / $this->k;
  1385. $this->h = $this->hPt / $this->k;
  1386. if ($this->empty_string($autopagebreak)) {
  1387. if (isset($this->AutoPageBreak)) {
  1388. $autopagebreak = $this->AutoPageBreak;
  1389. } else {
  1390. $autopagebreak = true;
  1391. }
  1392. }
  1393. if ($this->empty_string($bottommargin)) {
  1394. if (isset($this->bMargin)) {
  1395. $bottommargin = $this->bMargin;
  1396. } else {
  1397. // default value = 2 cm
  1398. $bottommargin = 2 * 28.35 / $this->k;
  1399. }
  1400. }
  1401. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  1402. // store page dimensions
  1403. $this->pagedim[$this->page] = array('w' => $this->wPt, 'h' => $this->hPt, 'wk' => $this->w, 'hk' => $this->h, 'tm' => $this->tMargin, 'bm' => $bottommargin, 'lm' => $this->lMargin, 'rm' => $this->rMargin, 'pb' => $autopagebreak, 'or' => $this->CurOrientation, 'olm' => $this->original_lMargin, 'orm' => $this->original_rMargin);
  1404. }
  1405. /**
  1406. * Enable or disable Right-To-Left language mode
  1407. * @param Boolean $enable if true enable Right-To-Left language mode.
  1408. * @access public
  1409. * @since 2.0.000 (2008-01-03)
  1410. */
  1411. public function setRTL($enable) {
  1412. $this->rtl = $enable ? true : false;
  1413. $this->tmprtl = false;
  1414. }
  1415. /**
  1416. * Return the RTL status
  1417. * @return boolean
  1418. * @access public
  1419. * @since 4.0.012 (2008-07-24)
  1420. */
  1421. public function getRTL() {
  1422. return $this->rtl;
  1423. }
  1424. /**
  1425. * Force temporary RTL language direction
  1426. * @param mixed $mode can be false, 'L' for LTR or 'R' for RTL
  1427. * @access public
  1428. * @since 2.1.000 (2008-01-09)
  1429. */
  1430. public function setTempRTL($mode) {
  1431. switch ($mode) {
  1432. case false:
  1433. case 'L':
  1434. case 'R': {
  1435. $this->tmprtl = $mode;
  1436. }
  1437. }
  1438. }
  1439. /**
  1440. * Set the last cell height.
  1441. * @param float $h cell height.
  1442. * @author Nicola Asuni
  1443. * @access public
  1444. * @since 1.53.0.TC034
  1445. */
  1446. public function setLastH($h) {
  1447. $this->lasth = $h;
  1448. }
  1449. /**
  1450. * Get the last cell height.
  1451. * @return last cell height
  1452. * @access public
  1453. * @since 4.0.017 (2008-08-05)
  1454. */
  1455. public function getLastH() {
  1456. return $this->lasth;
  1457. }
  1458. /**
  1459. * Set the adjusting factor to convert pixels to user units.
  1460. * @param float $scale adjusting factor to convert pixels to user units.
  1461. * @author Nicola Asuni
  1462. * @access public
  1463. * @since 1.5.2
  1464. */
  1465. public function setImageScale($scale) {
  1466. $this->imgscale = $scale;
  1467. }
  1468. /**
  1469. * Returns the adjusting factor to convert pixels to user units.
  1470. * @return float adjusting factor to convert pixels to user units.
  1471. * @author Nicola Asuni
  1472. * @access public
  1473. * @since 1.5.2
  1474. */
  1475. public function getImageScale() {
  1476. return $this->imgscale;
  1477. }
  1478. /**
  1479. * Returns an array of page dimensions:
  1480. * <ul><li>$this->pagedim[$this->page]['w'] => page_width_in_points</li><li>$this->pagedim[$this->page]['h'] => height in points</li><li>$this->pagedim[$this->page]['wk'] => page_width_in_points</li><li>$this->pagedim[$this->page]['hk'] => height</li><li>$this->pagedim[$this->page]['tm'] => top_margin</li><li>$this->pagedim[$this->page]['bm'] => bottom_margin</li><li>$this->pagedim[$this->page]['lm'] => left_margin</li><li>$this->pagedim[$this->page]['rm'] => right_margin</li><li>$this->pagedim[$this->page]['pb'] => auto_page_break</li><li>$this->pagedim[$this->page]['or'] => page_orientation</li><li>$this->pagedim[$this->page]['olm'] => original_left_margin</li><li>$this->pagedim[$this->page]['orm'] => original_right_margin</li></ul>
  1481. * @param int $pagenum page number (empty = current page)
  1482. * @return array of page dimensions.
  1483. * @author Nicola Asuni
  1484. * @access public
  1485. * @since 4.5.027 (2009-03-16)
  1486. */
  1487. public function getPageDimensions($pagenum='') {
  1488. if (empty($pagenum)) {
  1489. $pagenum = $this->page;
  1490. }
  1491. return $this->pagedim[$pagenum];
  1492. }
  1493. /**
  1494. * Returns the page width in units.
  1495. * @param int $pagenum page number (empty = current page)
  1496. * @return int page width.
  1497. * @author Nicola Asuni
  1498. * @access public
  1499. * @since 1.5.2
  1500. * @see getPageDimensions()
  1501. */
  1502. public function getPageWidth($pagenum='') {
  1503. if (empty($pagenum)) {
  1504. return $this->w;
  1505. }
  1506. return $this->pagedim[$pagenum]['w'];
  1507. }
  1508. /**
  1509. * Returns the page height in units.
  1510. * @param int $pagenum page number (empty = current page)
  1511. * @return int page height.
  1512. * @author Nicola Asuni
  1513. * @access public
  1514. * @since 1.5.2
  1515. * @see getPageDimensions()
  1516. */
  1517. public function getPageHeight($pagenum='') {
  1518. if (empty($pagenum)) {
  1519. return $this->h;
  1520. }
  1521. return $this->pagedim[$pagenum]['h'];
  1522. }
  1523. /**
  1524. * Returns the page break margin.
  1525. * @param int $pagenum page number (empty = current page)
  1526. * @return int page break margin.
  1527. * @author Nicola Asuni
  1528. * @access public
  1529. * @since 1.5.2
  1530. * @see getPageDimensions()
  1531. */
  1532. public function getBreakMargin($pagenum='') {
  1533. if (empty($pagenum)) {
  1534. return $this->bMargin;
  1535. }
  1536. return $this->pagedim[$pagenum]['bm'];
  1537. }
  1538. /**
  1539. * Returns the scale factor (number of points in user unit).
  1540. * @return int scale factor.
  1541. * @author Nicola Asuni
  1542. * @access public
  1543. * @since 1.5.2
  1544. */
  1545. public function getScaleFactor() {
  1546. return $this->k;
  1547. }
  1548. /**
  1549. * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
  1550. * @param float $left Left margin.
  1551. * @param float $top Top margin.
  1552. * @param float $right Right margin. Default value is the left one.
  1553. * @access public
  1554. * @since 1.0
  1555. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  1556. */
  1557. public function SetMargins($left, $top, $right=-1) {
  1558. //Set left, top and right margins
  1559. $this->lMargin = $left;
  1560. $this->tMargin = $top;
  1561. if ($right == -1) {
  1562. $right = $left;
  1563. }
  1564. $this->rMargin = $right;
  1565. }
  1566. /**
  1567. * 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.
  1568. * @param float $margin The margin.
  1569. * @access public
  1570. * @since 1.4
  1571. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1572. */
  1573. public function SetLeftMargin($margin) {
  1574. //Set left margin
  1575. $this->lMargin=$margin;
  1576. if (($this->page > 0) AND ($this->x < $margin)) {
  1577. $this->x = $margin;
  1578. }
  1579. }
  1580. /**
  1581. * Defines the top margin. The method can be called before creating the first page.
  1582. * @param float $margin The margin.
  1583. * @access public
  1584. * @since 1.5
  1585. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1586. */
  1587. public function SetTopMargin($margin) {
  1588. //Set top margin
  1589. $this->tMargin=$margin;
  1590. if (($this->page > 0) AND ($this->y < $margin)) {
  1591. $this->y = $margin;
  1592. }
  1593. }
  1594. /**
  1595. * Defines the right margin. The method can be called before creating the first page.
  1596. * @param float $margin The margin.
  1597. * @access public
  1598. * @since 1.5
  1599. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1600. */
  1601. public function SetRightMargin($margin) {
  1602. $this->rMargin=$margin;
  1603. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  1604. $this->x = $this->w - $margin;
  1605. }
  1606. }
  1607. /**
  1608. * Set the internal Cell padding.
  1609. * @param float $pad internal padding.
  1610. * @access public
  1611. * @since 2.1.000 (2008-01-09)
  1612. * @see Cell(), SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1613. */
  1614. public function SetCellPadding($pad) {
  1615. $this->cMargin = $pad;
  1616. }
  1617. /**
  1618. * 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.
  1619. * @param boolean $auto Boolean indicating if mode should be on or off.
  1620. * @param float $margin Distance from the bottom of the page.
  1621. * @access public
  1622. * @since 1.0
  1623. * @see Cell(), MultiCell(), AcceptPageBreak()
  1624. */
  1625. public function SetAutoPageBreak($auto, $margin=0) {
  1626. //Set auto page break mode and triggering margin
  1627. $this->AutoPageBreak = $auto;
  1628. $this->bMargin = $margin;
  1629. $this->PageBreakTrigger = $this->h - $margin;
  1630. }
  1631. /**
  1632. * Defines the way the document is to be displayed by the viewer.
  1633. * @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>
  1634. * @param string $layout The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
  1635. * @param string $mode A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
  1636. * @access public
  1637. * @since 1.2
  1638. */
  1639. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  1640. //Set display mode in viewer
  1641. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  1642. $this->ZoomMode = $zoom;
  1643. } else {
  1644. $this->Error('Incorrect zoom display mode: '.$zoom);
  1645. }
  1646. switch ($layout) {
  1647. case 'default':
  1648. case 'single':
  1649. case 'SinglePage': {
  1650. $this->LayoutMode = 'SinglePage';
  1651. break;
  1652. }
  1653. case 'continuous':
  1654. case 'OneColumn': {
  1655. $this->LayoutMode = 'OneColumn';
  1656. break;
  1657. }
  1658. case 'two':
  1659. case 'TwoColumnLeft': {
  1660. $this->LayoutMode = 'TwoColumnLeft';
  1661. break;
  1662. }
  1663. case 'TwoColumnRight': {
  1664. $this->LayoutMode = 'TwoColumnRight';
  1665. break;
  1666. }
  1667. case 'TwoPageLeft': {
  1668. $this->LayoutMode = 'TwoPageLeft';
  1669. break;
  1670. }
  1671. case 'TwoPageRight': {
  1672. $this->LayoutMode = 'TwoPageRight';
  1673. break;
  1674. }
  1675. default: {
  1676. $this->LayoutMode = 'SinglePage';
  1677. }
  1678. }
  1679. switch ($mode) {
  1680. case 'UseNone': {
  1681. $this->PageMode = 'UseNone';
  1682. break;
  1683. }
  1684. case 'UseOutlines': {
  1685. $this->PageMode = 'UseOutlines';
  1686. break;
  1687. }
  1688. case 'UseThumbs': {
  1689. $this->PageMode = 'UseThumbs';
  1690. break;
  1691. }
  1692. case 'FullScreen': {
  1693. $this->PageMode = 'FullScreen';
  1694. break;
  1695. }
  1696. case 'UseOC': {
  1697. $this->PageMode = 'UseOC';
  1698. break;
  1699. }
  1700. case '': {
  1701. $this->PageMode = 'UseAttachments';
  1702. break;
  1703. }
  1704. default: {
  1705. $this->PageMode = 'UseNone';
  1706. }
  1707. }
  1708. }
  1709. /**
  1710. * 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.
  1711. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  1712. * @param boolean $compress Boolean indicating if compression must be enabled.
  1713. * @access public
  1714. * @since 1.4
  1715. */
  1716. public function SetCompression($compress) {
  1717. //Set page compression
  1718. if (function_exists('gzcompress')) {
  1719. $this->compress = $compress;
  1720. } else {
  1721. $this->compress = false;
  1722. }
  1723. }
  1724. /**
  1725. * Defines the title of the document.
  1726. * @param string $title The title.
  1727. * @access public
  1728. * @since 1.2
  1729. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  1730. */
  1731. public function SetTitle($title) {
  1732. //Title of document
  1733. $this->title = $title;
  1734. }
  1735. /**
  1736. * Defines the subject of the document.
  1737. * @param string $subject The subject.
  1738. * @access public
  1739. * @since 1.2
  1740. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  1741. */
  1742. public function SetSubject($subject) {
  1743. //Subject of document
  1744. $this->subject = $subject;
  1745. }
  1746. /**
  1747. * Defines the author of the document.
  1748. * @param string $author The name of the author.
  1749. * @access public
  1750. * @since 1.2
  1751. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  1752. */
  1753. public function SetAuthor($author) {
  1754. //Author of document
  1755. $this->author = $author;
  1756. }
  1757. /**
  1758. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  1759. * @param string $keywords The list of keywords.
  1760. * @access public
  1761. * @since 1.2
  1762. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  1763. */
  1764. public function SetKeywords($keywords) {
  1765. //Keywords of document
  1766. $this->keywords = $keywords;
  1767. }
  1768. /**
  1769. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  1770. * @param string $creator The name of the creator.
  1771. * @access public
  1772. * @since 1.2
  1773. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  1774. */
  1775. public function SetCreator($creator) {
  1776. //Creator of document
  1777. $this->creator = $creator;
  1778. }
  1779. /**
  1780. * 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.
  1781. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  1782. * @param string $msg The error message
  1783. * @access public
  1784. * @since 1.0
  1785. */
  1786. public function Error($msg) {
  1787. // unset all class variables
  1788. $this->_destroy(true);
  1789. // exit program and print error
  1790. die('<strong>TCPDF ERROR: </strong>'.$msg);
  1791. }
  1792. /**
  1793. * This method begins the generation of the PDF document.
  1794. * It is not necessary to call it explicitly because AddPage() does it automatically.
  1795. * Note: no page is created by this method
  1796. * @access public
  1797. * @since 1.0
  1798. * @see AddPage(), Close()
  1799. */
  1800. public function Open() {
  1801. //Begin document
  1802. $this->state = 1;
  1803. }
  1804. /**
  1805. * Terminates the PDF document.
  1806. * It is not necessary to call this method explicitly because Output() does it automatically.
  1807. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  1808. * @access public
  1809. * @since 1.0
  1810. * @see Open(), Output()
  1811. */
  1812. public function Close() {
  1813. if ($this->state == 3) {
  1814. return;
  1815. }
  1816. if ($this->page == 0) {
  1817. $this->AddPage();
  1818. }
  1819. // close page
  1820. $this->endPage();
  1821. // close document
  1822. $this->_enddoc();
  1823. // unset all class variables (except critical ones)
  1824. $this->_destroy(false);
  1825. }
  1826. /**
  1827. * Move pointer at the specified document page and update page dimensions.
  1828. * @param int $pnum page number
  1829. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  1830. * @access public
  1831. * @since 2.1.000 (2008-01-07)
  1832. * @see getPage(), lastpage(), getNumPages()
  1833. */
  1834. public function setPage($pnum, $resetmargins=false) {
  1835. if ($pnum == $this->page) {
  1836. return;
  1837. }
  1838. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  1839. $this->state = 2;
  1840. // save current graphic settings
  1841. //$gvars = $this->getGraphicVars();
  1842. $oldpage = $this->page;
  1843. $this->page = $pnum;
  1844. $this->wPt = $this->pagedim[$this->page]['w'];
  1845. $this->hPt = $this->pagedim[$this->page]['h'];
  1846. $this->w = $this->wPt / $this->k;
  1847. $this->h = $this->hPt / $this->k;
  1848. $this->tMargin = $this->pagedim[$this->page]['tm'];
  1849. $this->bMargin = $this->pagedim[$this->page]['bm'];
  1850. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  1851. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  1852. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  1853. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  1854. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  1855. // restore graphic settings
  1856. //$this->setGraphicVars($gvars);
  1857. if ($resetmargins) {
  1858. $this->lMargin = $this->pagedim[$this->page]['olm'];
  1859. $this->rMargin = $this->pagedim[$this->page]['orm'];
  1860. $this->SetY($this->tMargin);
  1861. } else {
  1862. // account for booklet mode
  1863. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  1864. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  1865. $this->lMargin += $deltam;
  1866. $this->rMargin -= $deltam;
  1867. }
  1868. }
  1869. } else {
  1870. $this->Error('Wrong page number on setPage() function.');
  1871. }
  1872. }
  1873. /**
  1874. * Reset pointer to the last document page.
  1875. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  1876. * @access public
  1877. * @since 2.0.000 (2008-01-04)
  1878. * @see setPage(), getPage(), getNumPages()
  1879. */
  1880. public function lastPage($resetmargins=false) {
  1881. $this->setPage($this->getNumPages(), $resetmargins);
  1882. }
  1883. /**
  1884. * Get current document page number.
  1885. * @return int page number
  1886. * @access public
  1887. * @since 2.1.000 (2008-01-07)
  1888. * @see setPage(), lastpage(), getNumPages()
  1889. */
  1890. public function getPage() {
  1891. return $this->page;
  1892. }
  1893. /**
  1894. * Get the total number of insered pages.
  1895. * @return int number of pages
  1896. * @access public
  1897. * @since 2.1.000 (2008-01-07)
  1898. * @see setPage(), getPage(), lastpage()
  1899. */
  1900. public function getNumPages() {
  1901. return $this->numpages;
  1902. }
  1903. /**
  1904. * Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
  1905. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  1906. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1907. * @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>
  1908. * @access public
  1909. * @since 1.0
  1910. * @see startPage(), endPage()
  1911. */
  1912. public function AddPage($orientation='', $format='') {
  1913. if (!isset($this->original_lMargin)) {
  1914. $this->original_lMargin = $this->lMargin;
  1915. }
  1916. if (!isset($this->original_rMargin)) {
  1917. $this->original_rMargin = $this->rMargin;
  1918. }
  1919. // terminate previous page
  1920. $this->endPage();
  1921. // start new page
  1922. $this->startPage($orientation, $format);
  1923. }
  1924. /**
  1925. * Terminate the current page
  1926. * @access protected
  1927. * @since 4.2.010 (2008-11-14)
  1928. * @see startPage(), AddPage()
  1929. */
  1930. protected function endPage() {
  1931. // check if page is already closed
  1932. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  1933. return;
  1934. }
  1935. $this->InFooter = true;
  1936. // print page footer
  1937. $this->setFooter();
  1938. // close page
  1939. $this->_endpage();
  1940. // mark page as closed
  1941. $this->pageopen[$this->page] = false;
  1942. $this->InFooter = false;
  1943. }
  1944. /**
  1945. * Starts a new page to the document. The page must be closed using the endPage() function.
  1946. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  1947. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1948. * @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>
  1949. * @access protected
  1950. * @since 4.2.010 (2008-11-14)
  1951. * @see endPage(), AddPage()
  1952. */
  1953. protected function startPage($orientation='', $format='') {
  1954. if ($this->numpages > $this->page) {
  1955. // this page has been already added
  1956. $this->setPage($this->page + 1);
  1957. $this->SetY($this->tMargin);
  1958. return;
  1959. }
  1960. // start a new page
  1961. if ($this->state == 0) {
  1962. $this->Open();
  1963. }
  1964. ++$this->numpages;
  1965. $this->swapMargins($this->booklet);
  1966. // save current graphic settings
  1967. $gvars = $this->getGraphicVars();
  1968. // start new page
  1969. $this->_beginpage($orientation, $format);
  1970. // mark page as open
  1971. $this->pageopen[$this->page] = true;
  1972. // restore graphic settings
  1973. $this->setGraphicVars($gvars);
  1974. // mark this point
  1975. $this->setPageMark();
  1976. // print page header
  1977. $this->setHeader();
  1978. // restore graphic settings
  1979. $this->setGraphicVars($gvars);
  1980. // mark this point
  1981. $this->setPageMark();
  1982. // print table header (if any)
  1983. $this->setTableHeader();
  1984. }
  1985. /**
  1986. * Set start-writing mark on current page for multicell borders and fills.
  1987. * This function must be called after calling Image() function for a background image.
  1988. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  1989. * @access public
  1990. * @since 4.0.016 (2008-07-30)
  1991. */
  1992. public function setPageMark() {
  1993. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  1994. }
  1995. /**
  1996. * Set header data.
  1997. * @param string $ln header image logo
  1998. * @param string $lw header image logo width in mm
  1999. * @param string $ht string to print as title on document header
  2000. * @param string $hs string to print on document header
  2001. * @access public
  2002. */
  2003. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  2004. $this->header_logo = $ln;
  2005. $this->header_logo_width = $lw;
  2006. $this->header_title = $ht;
  2007. $this->header_string = $hs;
  2008. }
  2009. /**
  2010. * Returns header data:
  2011. * <ul><li>$ret['logo'] = logo image</li><li>$ret['logo_width'] = width of the image logo in user units</li><li>$ret['title'] = header title</li><li>$ret['string'] = header description string</li></ul>
  2012. * @return array()
  2013. * @access public
  2014. * @since 4.0.012 (2008-07-24)
  2015. */
  2016. public function getHeaderData() {
  2017. $ret = array();
  2018. $ret['logo'] = $this->header_logo;
  2019. $ret['logo_width'] = $this->header_logo_width;
  2020. $ret['title'] = $this->header_title;
  2021. $ret['string'] = $this->header_string;
  2022. return $ret;
  2023. }
  2024. /**
  2025. * Set header margin.
  2026. * (minimum distance between header and top page margin)
  2027. * @param int $hm distance in user units
  2028. * @access public
  2029. */
  2030. public function setHeaderMargin($hm=10) {
  2031. $this->header_margin = $hm;
  2032. }
  2033. /**
  2034. * Returns header margin in user units.
  2035. * @return float
  2036. * @since 4.0.012 (2008-07-24)
  2037. * @access public
  2038. */
  2039. public function getHeaderMargin() {
  2040. return $this->header_margin;
  2041. }
  2042. /**
  2043. * Set footer margin.
  2044. * (minimum distance between footer and bottom page margin)
  2045. * @param int $fm distance in user units
  2046. * @access public
  2047. */
  2048. public function setFooterMargin($fm=10) {
  2049. $this->footer_margin = $fm;
  2050. }
  2051. /**
  2052. * Returns footer margin in user units.
  2053. * @return float
  2054. * @since 4.0.012 (2008-07-24)
  2055. * @access public
  2056. */
  2057. public function getFooterMargin() {
  2058. return $this->footer_margin;
  2059. }
  2060. /**
  2061. * Set a flag to print page header.
  2062. * @param boolean $val set to true to print the page header (default), false otherwise.
  2063. * @access public
  2064. */
  2065. public function setPrintHeader($val=true) {
  2066. $this->print_header = $val;
  2067. }
  2068. /**
  2069. * Set a flag to print page footer.
  2070. * @param boolean $value set to true to print the page footer (default), false otherwise.
  2071. * @access public
  2072. */
  2073. public function setPrintFooter($val=true) {
  2074. $this->print_footer = $val;
  2075. }
  2076. /**
  2077. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  2078. * @return float
  2079. * @access public
  2080. */
  2081. public function getImageRBX() {
  2082. return $this->img_rb_x;
  2083. }
  2084. /**
  2085. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  2086. * @return float
  2087. * @access public
  2088. */
  2089. public function getImageRBY() {
  2090. return $this->img_rb_y;
  2091. }
  2092. /**
  2093. * This method is used to render the page header.
  2094. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2095. * @access public
  2096. */
  2097. public function Header() {
  2098. $ormargins = $this->getOriginalMargins();
  2099. $headerfont = $this->getHeaderFont();
  2100. $headerdata = $this->getHeaderData();
  2101. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  2102. $this->Image(K_PATH_IMAGES.$headerdata['logo'], $this->GetX(), $this->getHeaderMargin(), $headerdata['logo_width']);
  2103. $imgy = $this->getImageRBY();
  2104. } else {
  2105. $imgy = $this->GetY();
  2106. }
  2107. $cell_height = round(($this->getCellHeightRatio() * $headerfont[2]) / $this->getScaleFactor(), 2);
  2108. // set starting margin for text data cell
  2109. if ($this->getRTL()) {
  2110. $header_x = $ormargins['right'] + ($headerdata['logo_width'] * 1.1);
  2111. } else {
  2112. $header_x = $ormargins['left'] + ($headerdata['logo_width'] * 1.1);
  2113. }
  2114. $this->SetTextColor(0, 0, 0);
  2115. // header title
  2116. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  2117. $this->SetX($header_x);
  2118. $this->Cell(0, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  2119. // header string
  2120. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  2121. $this->SetX($header_x);
  2122. $this->MultiCell(0, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false);
  2123. // print an ending header line
  2124. $this->SetLineStyle(array('width' => 0.85 / $this->getScaleFactor(), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2125. $this->SetY((2.835 / $this->getScaleFactor()) + max($imgy, $this->GetY()));
  2126. if ($this->getRTL()) {
  2127. $this->SetX($ormargins['right']);
  2128. } else {
  2129. $this->SetX($ormargins['left']);
  2130. }
  2131. $this->Cell(0, 0, '', 'T', 0, 'C');
  2132. }
  2133. /**
  2134. * This method is used to render the page footer.
  2135. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2136. * @access public
  2137. */
  2138. public function Footer() {
  2139. $cur_y = $this->GetY();
  2140. $ormargins = $this->getOriginalMargins();
  2141. $this->SetTextColor(0, 0, 0);
  2142. //set style for cell border
  2143. $line_width = 0.85 / $this->getScaleFactor();
  2144. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2145. //print document barcode
  2146. $barcode = $this->getBarcode();
  2147. if (!empty($barcode)) {
  2148. $this->Ln($line_width);
  2149. $barcode_width = round(($this->getPageWidth() - $ormargins['left'] - $ormargins['right'])/3);
  2150. $this->write1DBarcode($barcode, 'C128B', $this->GetX(), $cur_y + $line_width, $barcode_width, (($this->getFooterMargin() / 3) - $line_width), 0.3, '', '');
  2151. }
  2152. if (empty($this->pagegroups)) {
  2153. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  2154. } else {
  2155. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  2156. }
  2157. $this->SetY($cur_y);
  2158. //Print page number
  2159. if ($this->getRTL()) {
  2160. $this->SetX($ormargins['right']);
  2161. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  2162. } else {
  2163. $this->SetX($ormargins['left']);
  2164. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'R');
  2165. }
  2166. }
  2167. /**
  2168. * This method is used to render the page header.
  2169. * @access protected
  2170. * @since 4.0.012 (2008-07-24)
  2171. */
  2172. protected function setHeader() {
  2173. if ($this->print_header) {
  2174. $lasth = $this->lasth;
  2175. $this->_out('q');
  2176. $this->rMargin = $this->original_rMargin;
  2177. $this->lMargin = $this->original_lMargin;
  2178. $this->cMargin = 0;
  2179. //set current position
  2180. if ($this->rtl) {
  2181. $this->SetXY($this->original_rMargin, $this->header_margin);
  2182. } else {
  2183. $this->SetXY($this->original_lMargin, $this->header_margin);
  2184. }
  2185. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  2186. $this->Header();
  2187. //restore position
  2188. if ($this->rtl) {
  2189. $this->SetXY($this->original_rMargin, $this->tMargin);
  2190. } else {
  2191. $this->SetXY($this->original_lMargin, $this->tMargin);
  2192. }
  2193. $this->_out('Q');
  2194. $this->lasth = $lasth;
  2195. }
  2196. }
  2197. /**
  2198. * This method is used to render the page footer.
  2199. * @access protected
  2200. * @since 4.0.012 (2008-07-24)
  2201. */
  2202. protected function setFooter() {
  2203. //Page footer
  2204. // save current graphic settings
  2205. $gvars = $this->getGraphicVars();
  2206. // mark this point
  2207. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  2208. $this->_out("\n");
  2209. if ($this->print_footer) {
  2210. $lasth = $this->lasth;
  2211. $this->_out('q');
  2212. $this->rMargin = $this->original_rMargin;
  2213. $this->lMargin = $this->original_lMargin;
  2214. $this->cMargin = 0;
  2215. //set current position
  2216. $footer_y = $this->h - $this->footer_margin;
  2217. if ($this->rtl) {
  2218. $this->SetXY($this->original_rMargin, $footer_y);
  2219. } else {
  2220. $this->SetXY($this->original_lMargin, $footer_y);
  2221. }
  2222. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  2223. $this->Footer();
  2224. //restore position
  2225. if ($this->rtl) {
  2226. $this->SetXY($this->original_rMargin, $this->tMargin);
  2227. } else {
  2228. $this->SetXY($this->original_lMargin, $this->tMargin);
  2229. }
  2230. $this->_out('Q');
  2231. $this->lasth = $lasth;
  2232. }
  2233. // restore graphic settings
  2234. $this->setGraphicVars($gvars);
  2235. // calculate footer lenght
  2236. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  2237. }
  2238. /**
  2239. * This method is used to render the table header on new page (if any).
  2240. * @access protected
  2241. * @since 4.5.030 (2009-03-25)
  2242. */
  2243. protected function setTableHeader() {
  2244. if (!$this->empty_string($this->theadMargin)) {
  2245. // restore the original top-margin
  2246. $this->tMargin = $this->theadMargin;
  2247. $this->pagedim[$this->page]['tm'] = $this->theadMargin;
  2248. $this->y = $this->theadMargin;
  2249. }
  2250. if (!$this->empty_string($this->thead)) {
  2251. // print table header
  2252. $this->writeHTML($this->thead, false, false, false, false, '');
  2253. // set new top margin to skip the table headers
  2254. if (!isset($this->theadMargin) OR ($this->empty_string($this->theadMargin))) {
  2255. $this->theadMargin = $this->tMargin;
  2256. }
  2257. $this->tMargin = $this->y;
  2258. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2259. }
  2260. }
  2261. /**
  2262. * Returns the current page number.
  2263. * @return int page number
  2264. * @access public
  2265. * @since 1.0
  2266. * @see AliasNbPages(), getAliasNbPages()
  2267. */
  2268. public function PageNo() {
  2269. return $this->page;
  2270. }
  2271. /**
  2272. * Defines a new spot color.
  2273. * It can be expressed in RGB components or gray scale.
  2274. * The method can be called before the first page is created and the value is retained from page to page.
  2275. * @param int $c Cyan color for CMYK. Value between 0 and 255
  2276. * @param int $m Magenta color for CMYK. Value between 0 and 255
  2277. * @param int $y Yellow color for CMYK. Value between 0 and 255
  2278. * @param int $k Key (Black) color for CMYK. Value between 0 and 255
  2279. * @access public
  2280. * @since 4.0.024 (2008-09-12)
  2281. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2282. */
  2283. public function AddSpotColor($name, $c, $m, $y, $k) {
  2284. if (!isset($this->spot_colors[$name])) {
  2285. $i = 1 + count($this->spot_colors);
  2286. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  2287. }
  2288. }
  2289. /**
  2290. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  2291. * It can be expressed in RGB components or gray scale.
  2292. * The method can be called before the first page is created and the value is retained from page to page.
  2293. * @param array $color array of colors
  2294. * @access public
  2295. * @since 3.1.000 (2008-06-11)
  2296. * @see SetDrawColor()
  2297. */
  2298. public function SetDrawColorArray($color) {
  2299. if (isset($color)) {
  2300. $color = array_values($color);
  2301. $r = isset($color[0]) ? $color[0] : -1;
  2302. $g = isset($color[1]) ? $color[1] : -1;
  2303. $b = isset($color[2]) ? $color[2] : -1;
  2304. $k = isset($color[3]) ? $color[3] : -1;
  2305. if ($r >= 0) {
  2306. $this->SetDrawColor($r, $g, $b, $k);
  2307. }
  2308. }
  2309. }
  2310. /**
  2311. * 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.
  2312. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2313. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2314. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2315. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2316. * @access public
  2317. * @since 1.3
  2318. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  2319. */
  2320. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2321. // set default values
  2322. if (!is_numeric($col1)) {
  2323. $col1 = 0;
  2324. }
  2325. if (!is_numeric($col2)) {
  2326. $col2 = -1;
  2327. }
  2328. if (!is_numeric($col3)) {
  2329. $col3 = -1;
  2330. }
  2331. if (!is_numeric($col4)) {
  2332. $col4 = -1;
  2333. }
  2334. //Set color for all stroking operations
  2335. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2336. // Grey scale
  2337. $this->DrawColor = sprintf('%.3F G', $col1/255);
  2338. } elseif ($col4 == -1) {
  2339. // RGB
  2340. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $col1/255, $col2/255, $col3/255);
  2341. } else {
  2342. // CMYK
  2343. $this->DrawColor = sprintf('%.3F %.3F %.3F %.3F K', $col1/100, $col2/100, $col3/100, $col4/100);
  2344. }
  2345. if ($this->page > 0) {
  2346. $this->_out($this->DrawColor);
  2347. }
  2348. }
  2349. /**
  2350. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  2351. * @param string $name name of the spot color
  2352. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2353. * @access public
  2354. * @since 4.0.024 (2008-09-12)
  2355. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2356. */
  2357. public function SetDrawSpotColor($name, $tint=100) {
  2358. if (!isset($this->spot_colors[$name])) {
  2359. $this->Error('Undefined spot color: '.$name);
  2360. }
  2361. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], $tint/100);
  2362. if ($this->page > 0) {
  2363. $this->_out($this->DrawColor);
  2364. }
  2365. }
  2366. /**
  2367. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  2368. * It can be expressed in RGB components or gray scale.
  2369. * The method can be called before the first page is created and the value is retained from page to page.
  2370. * @param array $color array of colors
  2371. * @access public
  2372. * @since 3.1.000 (2008-6-11)
  2373. * @see SetFillColor()
  2374. */
  2375. public function SetFillColorArray($color) {
  2376. if (isset($color)) {
  2377. $color = array_values($color);
  2378. $r = isset($color[0]) ? $color[0] : -1;
  2379. $g = isset($color[1]) ? $color[1] : -1;
  2380. $b = isset($color[2]) ? $color[2] : -1;
  2381. $k = isset($color[3]) ? $color[3] : -1;
  2382. if ($r >= 0) {
  2383. $this->SetFillColor($r, $g, $b, $k);
  2384. }
  2385. }
  2386. }
  2387. /**
  2388. * 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.
  2389. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2390. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2391. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2392. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2393. * @access public
  2394. * @since 1.3
  2395. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  2396. */
  2397. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2398. // set default values
  2399. if (!is_numeric($col1)) {
  2400. $col1 = 0;
  2401. }
  2402. if (!is_numeric($col2)) {
  2403. $col2 = -1;
  2404. }
  2405. if (!is_numeric($col3)) {
  2406. $col3 = -1;
  2407. }
  2408. if (!is_numeric($col4)) {
  2409. $col4 = -1;
  2410. }
  2411. //Set color for all filling operations
  2412. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2413. // Grey scale
  2414. $this->FillColor = sprintf('%.3F g', $col1/255);
  2415. $this->bgcolor = array('G' => $col1);
  2416. } elseif ($col4 == -1) {
  2417. // RGB
  2418. $this->FillColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2419. $this->bgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2420. } else {
  2421. // CMYK
  2422. $this->FillColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2423. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2424. }
  2425. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2426. if ($this->page > 0) {
  2427. $this->_out($this->FillColor);
  2428. }
  2429. }
  2430. /**
  2431. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  2432. * @param string $name name of the spot color
  2433. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2434. * @access public
  2435. * @since 4.0.024 (2008-09-12)
  2436. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  2437. */
  2438. public function SetFillSpotColor($name, $tint=100) {
  2439. if (!isset($this->spot_colors[$name])) {
  2440. $this->Error('Undefined spot color: '.$name);
  2441. }
  2442. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2443. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2444. if ($this->page > 0) {
  2445. $this->_out($this->FillColor);
  2446. }
  2447. }
  2448. /**
  2449. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  2450. * The method can be called before the first page is created and the value is retained from page to page.
  2451. * @param array $color array of colors
  2452. * @access public
  2453. * @since 3.1.000 (2008-6-11)
  2454. * @see SetFillColor()
  2455. */
  2456. public function SetTextColorArray($color) {
  2457. if (isset($color)) {
  2458. $color = array_values($color);
  2459. $r = isset($color[0]) ? $color[0] : -1;
  2460. $g = isset($color[1]) ? $color[1] : -1;
  2461. $b = isset($color[2]) ? $color[2] : -1;
  2462. $k = isset($color[3]) ? $color[3] : -1;
  2463. if ($r >= 0) {
  2464. $this->SetTextColor($r, $g, $b, $k);
  2465. }
  2466. }
  2467. }
  2468. /**
  2469. * 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.
  2470. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2471. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2472. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2473. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2474. * @access public
  2475. * @since 1.3
  2476. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  2477. */
  2478. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2479. // set default values
  2480. if (!is_numeric($col1)) {
  2481. $col1 = 0;
  2482. }
  2483. if (!is_numeric($col2)) {
  2484. $col2 = -1;
  2485. }
  2486. if (!is_numeric($col3)) {
  2487. $col3 = -1;
  2488. }
  2489. if (!is_numeric($col4)) {
  2490. $col4 = -1;
  2491. }
  2492. //Set color for text
  2493. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2494. // Grey scale
  2495. $this->TextColor = sprintf('%.3F g', $col1/255);
  2496. $this->fgcolor = array('G' => $col1);
  2497. } elseif ($col4 == -1) {
  2498. // RGB
  2499. $this->TextColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2500. $this->fgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2501. } else {
  2502. // CMYK
  2503. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2504. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2505. }
  2506. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2507. }
  2508. /**
  2509. * Defines the spot color used for text.
  2510. * @param string $name name of the spot color
  2511. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2512. * @access public
  2513. * @since 4.0.024 (2008-09-12)
  2514. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  2515. */
  2516. public function SetTextSpotColor($name, $tint=100) {
  2517. if (!isset($this->spot_colors[$name])) {
  2518. $this->Error('Undefined spot color: '.$name);
  2519. }
  2520. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2521. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2522. if ($this->page > 0) {
  2523. $this->_out($this->TextColor);
  2524. }
  2525. }
  2526. /**
  2527. * Returns the length of a string in user unit. A font must be selected.<br>
  2528. * @param string $s The string whose length is to be computed
  2529. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  2530. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular.
  2531. * @param float $fontsize Font size in points. The default value is the current size.
  2532. * @return int string length
  2533. * @author Nicola Asuni
  2534. * @access public
  2535. * @since 1.2
  2536. */
  2537. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0) {
  2538. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize);
  2539. }
  2540. /**
  2541. * Returns the string length of an array of chars in user unit. A font must be selected.<br>
  2542. * @param string $arr The array of chars whose total length is to be computed
  2543. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  2544. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular.
  2545. * @param float $fontsize Font size in points. The default value is the current size.
  2546. * @return int string length
  2547. * @author Nicola Asuni
  2548. * @access public
  2549. * @since 2.4.000 (2008-03-06)
  2550. */
  2551. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0) {
  2552. // store current values
  2553. if (!$this->empty_string($fontname)) {
  2554. $prev_FontFamily = $this->FontFamily;
  2555. $prev_FontStyle = $this->FontStyle;
  2556. $prev_FontSizePt = $this->FontSizePt;
  2557. $this->SetFont($fontname, $fontstyle, $fontsize);
  2558. }
  2559. $w = 0;
  2560. foreach ($sa as $char) {
  2561. $w += $this->GetCharWidth($char);
  2562. }
  2563. // restore previous values
  2564. if (!$this->empty_string($fontname)) {
  2565. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt);
  2566. }
  2567. return $w;
  2568. }
  2569. /**
  2570. * Returns the length of the char in user unit for the current font.<br>
  2571. * @param int $char The char code whose length is to be returned
  2572. * @return int char width
  2573. * @author Nicola Asuni
  2574. * @access public
  2575. * @since 2.4.000 (2008-03-06)
  2576. */
  2577. public function GetCharWidth($char) {
  2578. if ($char == 173) {
  2579. // SHY character will not be printed
  2580. return (0);
  2581. }
  2582. $cw = &$this->CurrentFont['cw'];
  2583. if (isset($cw[$char])) {
  2584. $w = $cw[$char];
  2585. } elseif (isset($this->CurrentFont['dw'])) {
  2586. // default width
  2587. $w = $this->CurrentFont['dw'];
  2588. } elseif (isset($cw[32])) {
  2589. // default width
  2590. $dw = $cw[32];
  2591. } else {
  2592. $w = 600;
  2593. }
  2594. return ($w * $this->FontSize / 1000);
  2595. }
  2596. /**
  2597. * Returns the numbero of characters in a string.
  2598. * @param string $s The input string.
  2599. * @return int number of characters
  2600. * @access public
  2601. * @since 2.0.0001 (2008-01-07)
  2602. */
  2603. public function GetNumChars($s) {
  2604. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  2605. return count($this->UTF8StringToArray($s));
  2606. }
  2607. return strlen($s);
  2608. }
  2609. /**
  2610. * Fill the list of available fonts ($this->fontlist).
  2611. * @access protected
  2612. * @since 4.0.013 (2008-07-28)
  2613. */
  2614. protected function getFontsList() {
  2615. $fontsdir = opendir($this->_getfontpath());
  2616. while (($file = readdir($fontsdir)) !== false) {
  2617. if (substr($file, -4) == '.php') {
  2618. array_push($this->fontlist, strtolower(basename($file, '.php')));
  2619. }
  2620. }
  2621. closedir($fontsdir);
  2622. }
  2623. /**
  2624. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  2625. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  2626. * 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.
  2627. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  2628. * @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>
  2629. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  2630. * @return array containing the font data, or false in case of error.
  2631. * @access public
  2632. * @since 1.5
  2633. * @see SetFont()
  2634. */
  2635. public function AddFont($family, $style='', $fontfile='') {
  2636. if ($this->empty_string($family)) {
  2637. if (!$this->empty_string($this->FontFamily)) {
  2638. $family = $this->FontFamily;
  2639. } else {
  2640. $this->Error('Empty font family');
  2641. }
  2642. }
  2643. $family = strtolower($family);
  2644. if ((!$this->isunicode) AND ($family == 'arial')) {
  2645. $family = 'helvetica';
  2646. }
  2647. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  2648. $style = '';
  2649. }
  2650. $tempstyle = strtoupper($style);
  2651. $style = '';
  2652. // underline
  2653. if (strpos($tempstyle, 'U') !== false) {
  2654. $this->underline = true;
  2655. } else {
  2656. $this->underline = false;
  2657. }
  2658. // line through (deleted)
  2659. if (strpos($tempstyle, 'D') !== false) {
  2660. $this->linethrough = true;
  2661. } else {
  2662. $this->linethrough = false;
  2663. }
  2664. // bold
  2665. if (strpos($tempstyle, 'B') !== false) {
  2666. $style .= 'B';
  2667. }
  2668. // oblique
  2669. if (strpos($tempstyle, 'I') !== false) {
  2670. $style .= 'I';
  2671. }
  2672. $bistyle = $style;
  2673. $fontkey = $family.$style;
  2674. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '');
  2675. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  2676. // check if the font has been already added
  2677. if ($this->getFontBuffer($fontkey) !== false) {
  2678. return $fontdata;
  2679. }
  2680. if (isset($type)) {
  2681. unset($type);
  2682. }
  2683. if (isset($cw)) {
  2684. unset($cw);
  2685. }
  2686. // get specified font directory (if any)
  2687. $fontdir = '';
  2688. if (!$this->empty_string($fontfile)) {
  2689. $fontdir = dirname($fontfile);
  2690. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  2691. $fontdir = '';
  2692. } else {
  2693. $fontdir .= '/';
  2694. }
  2695. }
  2696. // search and include font file
  2697. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  2698. // build a standard filenames for specified font
  2699. $fontfile1 = str_replace(' ', '', $family).strtolower($style).'.php';
  2700. $fontfile2 = str_replace(' ', '', $family).'.php';
  2701. // search files on various directories
  2702. if (file_exists($fontdir.$fontfile1)) {
  2703. $fontfile = $fontdir.$fontfile1;
  2704. } elseif (file_exists($this->_getfontpath().$fontfile1)) {
  2705. $fontfile = $this->_getfontpath().$fontfile1;
  2706. } elseif (file_exists($fontfile1)) {
  2707. $fontfile = $fontfile1;
  2708. } elseif (file_exists($fontdir.$fontfile2)) {
  2709. $fontfile = $fontdir.$fontfile2;
  2710. } elseif (file_exists($this->_getfontpath().$fontfile2)) {
  2711. $fontfile = $this->_getfontpath().$fontfile2;
  2712. } else {
  2713. $fontfile = $fontfile2;
  2714. }
  2715. }
  2716. // include font file
  2717. if (file_exists($fontfile)) {
  2718. include($fontfile);
  2719. } else {
  2720. $this->Error('Could not include font definition file: '.$family.'');
  2721. }
  2722. // check font parameters
  2723. if ((!isset($type)) OR (!isset($cw))) {
  2724. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  2725. }
  2726. if (!isset($file)) {
  2727. $file = '';
  2728. }
  2729. if (!isset($enc)) {
  2730. $enc = '';
  2731. }
  2732. if (!isset($dw) OR $this->empty_string($dw)) {
  2733. // set default width
  2734. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  2735. $dw = $desc['MissingWidth'];
  2736. } elseif (isset($cw[32])) {
  2737. $dw = $cw[32];
  2738. } else {
  2739. $dw = 600;
  2740. }
  2741. }
  2742. ++$this->numfonts;
  2743. // register CID font (all styles at once)
  2744. if ($type == 'cidfont0') {
  2745. $file = ''; // not embedded
  2746. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  2747. $sname = $name.$styles[$bistyle];
  2748. if ((strpos($bistyle, 'B') !== false) AND (isset($desc['StemV'])) AND ($desc['StemV'] == 70)) {
  2749. $desc['StemV'] = 120;
  2750. }
  2751. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => $type, 'name' => $sname, 'desc' => $desc, 'cidinfo' => $cidinfo, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc));
  2752. } elseif ($type == 'core') {
  2753. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => 'core', 'name' => $this->CoreFonts[$fontkey], 'up' => -100, 'ut' => 50, 'cw' => $cw, 'dw' => $dw));
  2754. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  2755. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => $type, 'name' => $name, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'file' => $file, 'enc' => $enc, 'desc' => $desc));
  2756. } elseif ($type == 'TrueTypeUnicode') {
  2757. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'file' => $file, 'ctg' => $ctg));
  2758. } else {
  2759. $this->Error('Unknow font type: '.$type.'');
  2760. }
  2761. if (isset($diff) AND (!empty($diff))) {
  2762. //Search existing encodings
  2763. $d = 0;
  2764. $nb = count($this->diffs);
  2765. for ($i=1; $i <= $nb; ++$i) {
  2766. if ($this->diffs[$i] == $diff) {
  2767. $d = $i;
  2768. break;
  2769. }
  2770. }
  2771. if ($d == 0) {
  2772. $d = $nb + 1;
  2773. $this->diffs[$d] = $diff;
  2774. }
  2775. $this->setFontSubBuffer($fontkey, 'diff', $d);
  2776. }
  2777. if (!$this->empty_string($file)) {
  2778. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  2779. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir);
  2780. } elseif ($type != 'core') {
  2781. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir);
  2782. }
  2783. }
  2784. return $fontdata;
  2785. }
  2786. /**
  2787. * Sets the font used to print character strings.
  2788. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  2789. * The method can be called before the first page is created and the font is retained from page to page.
  2790. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  2791. * 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 />
  2792. * @param string $family Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):<ul><li>times (Times-Roman)</li><li>timesb (Times-Bold)</li><li>timesi (Times-Italic)</li><li>timesbi (Times-BoldItalic)</li><li>helvetica (Helvetica)</li><li>helveticab (Helvetica-Bold)</li><li>helveticai (Helvetica-Oblique)</li><li>helveticabi (Helvetica-BoldOblique)</li><li>courier (Courier)</li><li>courierb (Courier-Bold)</li><li>courieri (Courier-Oblique)</li><li>courierbi (Courier-BoldOblique)</li><li>symbol (Symbol)</li><li>zapfdingbats (ZapfDingbats)</li></ul> It is also possible to pass an empty string. In that case, the current family is retained.
  2793. * @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><li>D: line trough</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
  2794. * @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
  2795. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  2796. * @access public
  2797. * @since 1.0
  2798. * @see AddFont(), SetFontSize()
  2799. */
  2800. public function SetFont($family, $style='', $size=0, $fontfile='') {
  2801. //Select a font; size given in points
  2802. if ($size == 0) {
  2803. $size = $this->FontSizePt;
  2804. }
  2805. // try to add font (if not already added)
  2806. $fontdata = $this->AddFont($family, $style, $fontfile);
  2807. $this->FontFamily = $fontdata['family'];
  2808. $this->FontStyle = $fontdata['style'];
  2809. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  2810. $this->SetFontSize($size);
  2811. }
  2812. /**
  2813. * Defines the size of the current font.
  2814. * @param float $size The size (in points)
  2815. * @access public
  2816. * @since 1.0
  2817. * @see SetFont()
  2818. */
  2819. public function SetFontSize($size) {
  2820. //Set font size in points
  2821. $this->FontSizePt = $size;
  2822. $this->FontSize = $size / $this->k;
  2823. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  2824. $this->FontAscent = $this->CurrentFont['desc']['Ascent'] * $this->FontSize / 1000;
  2825. } else {
  2826. $this->FontAscent = 0.8 * $this->FontSize;
  2827. }
  2828. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] > 0)) {
  2829. $this->FontDescent = - $this->CurrentFont['desc']['Descent'] * $this->FontSize / 1000;
  2830. } else {
  2831. $this->FontDescent = 0.2 * $this->FontSize;
  2832. }
  2833. if (($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  2834. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  2835. }
  2836. }
  2837. /**
  2838. * Defines the default monospaced font.
  2839. * @param string $font Font name.
  2840. * @access public
  2841. * @since 4.5.025
  2842. */
  2843. public function SetDefaultMonospacedFont($font) {
  2844. $this->default_monospaced_font = $font;
  2845. }
  2846. /**
  2847. * 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 />
  2848. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  2849. * @access public
  2850. * @since 1.5
  2851. * @see Cell(), Write(), Image(), Link(), SetLink()
  2852. */
  2853. public function AddLink() {
  2854. //Create a new internal link
  2855. $n = count($this->links) + 1;
  2856. $this->links[$n] = array(0, 0);
  2857. return $n;
  2858. }
  2859. /**
  2860. * Defines the page and position a link points to.
  2861. * @param int $link The link identifier returned by AddLink()
  2862. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  2863. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  2864. * @access public
  2865. * @since 1.5
  2866. * @see AddLink()
  2867. */
  2868. public function SetLink($link, $y=0, $page=-1) {
  2869. if ($y == -1) {
  2870. $y = $this->y;
  2871. }
  2872. if ($page == -1) {
  2873. $page = $this->page;
  2874. }
  2875. $this->links[$link] = array($page, $y);
  2876. }
  2877. /**
  2878. * Puts a link on a rectangular area of the page.
  2879. * 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.
  2880. * @param float $x Abscissa of the upper-left corner of the rectangle
  2881. * @param float $y Ordinate of the upper-left corner of the rectangle
  2882. * @param float $w Width of the rectangle
  2883. * @param float $h Height of the rectangle
  2884. * @param mixed $link URL or identifier returned by AddLink()
  2885. * @param int $spaces number of spaces on the text to link
  2886. * @access public
  2887. * @since 1.5
  2888. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  2889. */
  2890. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  2891. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  2892. }
  2893. /**
  2894. * Puts a markup annotation on a rectangular area of the page.
  2895. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  2896. * @param float $x Abscissa of the upper-left corner of the rectangle
  2897. * @param float $y Ordinate of the upper-left corner of the rectangle
  2898. * @param float $w Width of the rectangle
  2899. * @param float $h Height of the rectangle
  2900. * @param string $text annotation text or alternate content
  2901. * @param array $opt array of options (see section 8.4 of PDF reference 1.7).
  2902. * @param int $spaces number of spaces on the text to link
  2903. * @access public
  2904. * @since 4.0.018 (2008-08-06)
  2905. */
  2906. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  2907. // recalculate coordinates to account for graphic transformations
  2908. if (isset($this->transfmatrix)) {
  2909. $maxid = count($this->transfmatrix) - 1;
  2910. for ($i=$maxid; $i >= 0; $i--) {
  2911. $ctm = $this->transfmatrix[$i];
  2912. if (isset($ctm['a'])) {
  2913. $x = $x * $this->k;
  2914. $y = ($this->h - $y) * $this->k;
  2915. $w = $w * $this->k;
  2916. $h = $h * $this->k;
  2917. // top left
  2918. $xt = $x;
  2919. $yt = $y;
  2920. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  2921. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  2922. // top right
  2923. $xt = $x + $w;
  2924. $yt = $y;
  2925. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  2926. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  2927. // bottom left
  2928. $xt = $x;
  2929. $yt = $y - $h;
  2930. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  2931. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  2932. // bottom right
  2933. $xt = $x + $w;
  2934. $yt = $y - $h;
  2935. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  2936. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  2937. // new coordinates (rectangle area)
  2938. $x = min($x1, $x2, $x3, $x4);
  2939. $y = max($y1, $y2, $y3, $y4);
  2940. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  2941. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  2942. $x = $x / $this->k;
  2943. $y = $this->h - ($y / $this->k);
  2944. }
  2945. }
  2946. }
  2947. $this->PageAnnots[$this->page][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  2948. if (($opt['Subtype'] == 'FileAttachment') AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  2949. $this->embeddedfiles[basename($opt['FS'])] = array('file' => $opt['FS'], 'n' => (count($this->embeddedfiles) + 100000));
  2950. }
  2951. }
  2952. /**
  2953. * Embedd the attached files.
  2954. * @since 4.4.000 (2008-12-07)
  2955. * @access protected
  2956. * @see Annotation()
  2957. */
  2958. protected function _putEmbeddedFiles() {
  2959. reset($this->embeddedfiles);
  2960. foreach ($this->embeddedfiles as $filename => $filedata) {
  2961. $data = file_get_contents($filedata['file']);
  2962. $filter = '';
  2963. if ($this->compress) {
  2964. $data = gzcompress($data);
  2965. $filter = ' /Filter /FlateDecode';
  2966. }
  2967. $this->offsets[$filedata['n']] = $this->bufferlen;
  2968. $this->_out($filedata['n'].' 0 obj');
  2969. $this->_out('<</Type /EmbeddedFile'.$filter.' /Length '.strlen($data).' >>');
  2970. $this->_putstream($data);
  2971. $this->_out('endobj');
  2972. }
  2973. }
  2974. /**
  2975. * Prints a character string.
  2976. * The origin is on the left of the first charcter, on the baseline.
  2977. * This method allows to place a string precisely on the page.
  2978. * @param float $x Abscissa of the origin
  2979. * @param float $y Ordinate of the origin
  2980. * @param string $txt String to print
  2981. * @param int $stroke outline size in points (0 = disable)
  2982. * @param boolean $clip if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  2983. * @access public
  2984. * @since 1.0
  2985. * @deprecated deprecated since version 4.3.005 (2008-11-25)
  2986. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  2987. */
  2988. public function Text($x, $y, $txt, $stroke=0, $clip=false) {
  2989. //Output a string
  2990. if ($this->rtl) {
  2991. // bidirectional algorithm (some chars may be changed affecting the line length)
  2992. $s = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  2993. $l = $this->GetArrStringWidth($s);
  2994. $xr = $this->w - $x - $this->GetArrStringWidth($s);
  2995. } else {
  2996. $xr = $x;
  2997. }
  2998. $opt = '';
  2999. if (($stroke > 0) AND (!$clip)) {
  3000. $opt .= '1 Tr '.intval($stroke).' w ';
  3001. } elseif (($stroke > 0) AND $clip) {
  3002. $opt .= '5 Tr '.intval($stroke).' w ';
  3003. } elseif ($clip) {
  3004. $opt .= '7 Tr ';
  3005. }
  3006. $s = sprintf('BT %.2F %.2F Td %s(%s) Tj ET 0 Tr', $xr * $this->k, ($this->h-$y) * $this->k, $opt, $this->_escapetext($txt));
  3007. if ($this->underline AND ($txt!='')) {
  3008. $s .= ' '.$this->_dounderline($xr, $y, $txt);
  3009. }
  3010. if ($this->linethrough AND ($txt!='')) {
  3011. $s .= ' '.$this->_dolinethrough($xr, $y, $txt);
  3012. }
  3013. if ($this->ColorFlag AND (!$clip)) {
  3014. $s='q '.$this->TextColor.' '.$s.' Q';
  3015. }
  3016. $this->_out($s);
  3017. }
  3018. /**
  3019. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  3020. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  3021. * This method is called automatically and should not be called directly by the application.
  3022. * @return boolean
  3023. * @access public
  3024. * @since 1.4
  3025. * @see SetAutoPageBreak()
  3026. */
  3027. public function AcceptPageBreak() {
  3028. return $this->AutoPageBreak;
  3029. }
  3030. /**
  3031. * Add page if needed.
  3032. * @param float $h Cell height. Default value: 0.
  3033. * @param mixed $y starting y position, leave empty for current position.
  3034. * @return boolean true in case of page break, false otherwise.
  3035. * @since 3.2.000 (2008-07-01)
  3036. * @access protected
  3037. */
  3038. protected function checkPageBreak($h=0, $y='') {
  3039. if ($this->empty_string($y)) {
  3040. $y = $this->y;
  3041. }
  3042. if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
  3043. //Automatic page break
  3044. $x = $this->x;
  3045. $this->AddPage($this->CurOrientation);
  3046. $this->y = $this->tMargin;
  3047. $oldpage = $this->page - 1;
  3048. if ($this->rtl) {
  3049. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  3050. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  3051. } else {
  3052. $this->x = $x;
  3053. }
  3054. } else {
  3055. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3056. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  3057. } else {
  3058. $this->x = $x;
  3059. }
  3060. }
  3061. return true;
  3062. }
  3063. return false;
  3064. }
  3065. /**
  3066. * 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 />
  3067. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3068. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3069. * @param float $h Cell height. Default value: 0.
  3070. * @param string $txt String to print. Default value: empty string.
  3071. * @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>
  3072. * @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>
  3073. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3074. * @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>
  3075. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3076. * @param mixed $link URL or identifier returned by AddLink().
  3077. * @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>
  3078. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3079. * @access public
  3080. * @since 1.0
  3081. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  3082. */
  3083. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3084. //$min_cell_height = $this->FontAscent + $this->FontDescent;
  3085. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3086. if ($h < $min_cell_height) {
  3087. $h = $min_cell_height;
  3088. }
  3089. $this->checkPageBreak($h);
  3090. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height));
  3091. }
  3092. /**
  3093. * Removes SHY characters from text.
  3094. * @param string $txt input string
  3095. * @return string without SHY characters.
  3096. * @access public
  3097. * @since (4.5.019) 2009-02-28
  3098. */
  3099. public function removeSHY($txt='') {
  3100. /*
  3101. * Unicode Data
  3102. * Name : SOFT HYPHEN, commonly abbreviated as SHY
  3103. * HTML Entity (decimal): &#173;
  3104. * HTML Entity (hex): &#xad;
  3105. * HTML Entity (named): &shy;
  3106. * How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]
  3107. * UTF-8 (hex): 0xC2 0xAD (c2ad)
  3108. * UTF-8 character: chr(194).chr(173)
  3109. */
  3110. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  3111. if (!$this->isunicode) {
  3112. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  3113. }
  3114. return $txt;
  3115. }
  3116. /**
  3117. * Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
  3118. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3119. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3120. * @param float $h Cell height. Default value: 0.
  3121. * @param string $txt String to print. Default value: empty string.
  3122. * @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>
  3123. * @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>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3124. * @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>
  3125. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3126. * @param mixed $link URL or identifier returned by AddLink().
  3127. * @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>
  3128. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3129. * @access protected
  3130. * @since 1.0
  3131. * @see Cell()
  3132. */
  3133. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3134. $txt = $this->removeSHY($txt);
  3135. $rs = ''; //string to be returned
  3136. if (!$ignore_min_height) {
  3137. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3138. if ($h < $min_cell_height) {
  3139. $h = $min_cell_height;
  3140. }
  3141. }
  3142. $k = $this->k;
  3143. if ($this->empty_string($w) OR ($w <= 0)) {
  3144. if ($this->rtl) {
  3145. $w = $this->x - $this->lMargin;
  3146. } else {
  3147. $w = $this->w - $this->rMargin - $this->x;
  3148. }
  3149. }
  3150. $s = '';
  3151. if (($fill == 1) OR ($border == 1)) {
  3152. if ($fill == 1) {
  3153. $op = ($border == 1) ? 'B' : 'f';
  3154. } else {
  3155. $op = 'S';
  3156. }
  3157. if ($this->rtl) {
  3158. $xk = (($this->x - $w) * $k);
  3159. } else {
  3160. $xk = ($this->x * $k);
  3161. }
  3162. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $this->y) * $k), ($w * $k), (-$h * $k), $op);
  3163. }
  3164. if (is_string($border)) {
  3165. $lm = ($this->LineWidth / 2);
  3166. $x = $this->x;
  3167. $y = $this->y;
  3168. if (strpos($border,'L') !== false) {
  3169. if ($this->rtl) {
  3170. $xk = ($x - $w) * $k;
  3171. } else {
  3172. $xk = $x * $k;
  3173. }
  3174. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm)) * $k));
  3175. }
  3176. if (strpos($border,'T') !== false) {
  3177. if ($this->rtl) {
  3178. $xk = ($x - $w + $lm) * $k;
  3179. $xwk = ($x - $lm) * $k;
  3180. } else {
  3181. $xk = ($x - $lm) * $k;
  3182. $xwk = ($x + $w + $lm) * $k;
  3183. }
  3184. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y) * $k), $xwk, (($this->h - $y) * $k));
  3185. }
  3186. if (strpos($border,'R') !== false) {
  3187. if ($this->rtl) {
  3188. $xk = $x * $k;
  3189. } else {
  3190. $xk = ($x + $w) * $k;
  3191. }
  3192. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm))* $k));
  3193. }
  3194. if (strpos($border,'B') !== false) {
  3195. if ($this->rtl) {
  3196. $xk = ($x - $w + $lm) * $k;
  3197. $xwk = ($x - $lm) * $k;
  3198. } else {
  3199. $xk = ($x - $lm) * $k;
  3200. $xwk = ($x + $w + $lm) * $k;
  3201. }
  3202. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - ($y + $h)) * $k), $xwk, (($this->h - ($y + $h)) * $k));
  3203. }
  3204. }
  3205. if ($txt != '') {
  3206. // text lenght
  3207. $width = $this->GetStringWidth($txt);
  3208. // ratio between cell lenght and text lenght
  3209. $ratio = ($w - (2 * $this->cMargin)) / $width;
  3210. // stretch text if required
  3211. if (($stretch > 0) AND (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0)))) {
  3212. if ($stretch > 2) {
  3213. // spacing
  3214. //Calculate character spacing in points
  3215. $char_space = (($w - $width - (2 * $this->cMargin)) * $this->k) / max($this->GetNumChars($txt)-1,1);
  3216. //Set character spacing
  3217. $rs .= sprintf('BT %.2F Tc ET ', $char_space);
  3218. } else {
  3219. // scaling
  3220. //Calculate horizontal scaling
  3221. $horiz_scale = $ratio * 100.0;
  3222. //Set horizontal scaling
  3223. $rs .= sprintf('BT %.2F Tz ET ', $horiz_scale);
  3224. }
  3225. $align = '';
  3226. $width = $w - (2 * $this->cMargin);
  3227. } else {
  3228. $stretch == 0;
  3229. }
  3230. if ($align == 'L') {
  3231. if ($this->rtl) {
  3232. $dx = $w - $width - $this->cMargin;
  3233. } else {
  3234. $dx = $this->cMargin;
  3235. }
  3236. } elseif ($align == 'R') {
  3237. if ($this->rtl) {
  3238. $dx = $this->cMargin;
  3239. } else {
  3240. $dx = $w - $width - $this->cMargin;
  3241. }
  3242. } elseif ($align == 'C') {
  3243. $dx = ($w - $width) / 2;
  3244. } elseif ($align == 'J') {
  3245. if ($this->rtl) {
  3246. $dx = $w - $width - $this->cMargin;
  3247. } else {
  3248. $dx = $this->cMargin;
  3249. }
  3250. } else {
  3251. $dx = $this->cMargin;
  3252. }
  3253. if ($this->ColorFlag) {
  3254. $s .= 'q '.$this->TextColor.' ';
  3255. }
  3256. $txt2 = $this->_escapetext($txt);
  3257. if ($this->rtl) {
  3258. $xdk = ($this->x - $dx - $width) * $k;
  3259. } else {
  3260. $xdk = ($this->x + $dx) * $k;
  3261. }
  3262. // Justification
  3263. if ($align == 'J') {
  3264. // count number of spaces
  3265. $ns = substr_count($txt, ' ');
  3266. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  3267. // get string width without spaces
  3268. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  3269. // calculate average space width
  3270. $spacewidth = ($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1) / $this->FontSize / $this->k;
  3271. // set word position to be used with TJ operator
  3272. $txt2 = str_replace(chr(0).' ', ') '.(-2830 * $spacewidth).' (', $txt2);
  3273. } else {
  3274. // get string width
  3275. $width = $this->GetStringWidth($txt);
  3276. $spacewidth = (($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1)) * $this->k;
  3277. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  3278. }
  3279. }
  3280. // calculate approximate position of the font base line
  3281. //$basefonty = $this->y + (($h + $this->FontAscent - $this->FontDescent)/2);
  3282. $basefonty = $this->y + ($h/2) + ($this->FontSize/3);
  3283. // print text
  3284. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  3285. if ($this->rtl) {
  3286. $xdx = $this->x - $dx - $width;
  3287. } else {
  3288. $xdx = $this->x + $dx;
  3289. }
  3290. if ($this->underline) {
  3291. $s .= ' '.$this->_dounderline($xdx, $basefonty, $txt);
  3292. }
  3293. if ($this->linethrough) {
  3294. $s .= ' '.$this->_dolinethrough($xdx, $basefonty, $txt);
  3295. }
  3296. if ($this->ColorFlag) {
  3297. $s .= ' Q';
  3298. }
  3299. if ($link) {
  3300. $this->Link($xdx, $this->y + (($h - $this->FontSize)/2), $width, $this->FontSize, $link, substr_count($txt, chr(32)));
  3301. }
  3302. }
  3303. // output cell
  3304. if ($s) {
  3305. // output cell
  3306. $rs .= $s;
  3307. // reset text stretching
  3308. if ($stretch > 2) {
  3309. //Reset character horizontal spacing
  3310. $rs .= ' BT 0 Tc ET';
  3311. } elseif ($stretch > 0) {
  3312. //Reset character horizontal scaling
  3313. $rs .= ' BT 100 Tz ET';
  3314. }
  3315. }
  3316. // reset word spacing
  3317. if (!(($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($align == 'J')) {
  3318. $rs .= ' BT 0 Tw ET';
  3319. }
  3320. $this->lasth = $h;
  3321. if ($ln > 0) {
  3322. //Go to the beginning of the next line
  3323. $this->y += $h;
  3324. if ($ln == 1) {
  3325. if ($this->rtl) {
  3326. $this->x = $this->w - $this->rMargin;
  3327. } else {
  3328. $this->x = $this->lMargin;
  3329. }
  3330. }
  3331. } else {
  3332. // go left or right by case
  3333. if ($this->rtl) {
  3334. $this->x -= $w;
  3335. } else {
  3336. $this->x += $w;
  3337. }
  3338. }
  3339. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  3340. $rs = $gstyles.$rs;
  3341. return $rs;
  3342. }
  3343. /**
  3344. * This method allows printing text with line breaks.
  3345. * 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 />
  3346. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  3347. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  3348. * @param float $h Cell minimum height. The cell extends automatically if needed.
  3349. * @param string $txt String to print
  3350. * @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>
  3351. * @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 when $ishtml=false)</li></ul>
  3352. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3353. * @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>
  3354. * @param int $x x position in user units
  3355. * @param int $y y position in user units
  3356. * @param boolean $reseth if true reset the last cell height (default true).
  3357. * @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>
  3358. * @param boolean $ishtml set to true if $txt is HTML content (default = false).
  3359. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  3360. * @param float $maxh maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
  3361. * @return int Return the number of cells or 1 for html mode.
  3362. * @access public
  3363. * @since 1.3
  3364. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  3365. */
  3366. public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0) {
  3367. if ($this->empty_string($this->lasth) OR $reseth) {
  3368. //set row height
  3369. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  3370. }
  3371. if (!$this->empty_string($y)) {
  3372. $this->SetY($y);
  3373. } else {
  3374. $y = $this->GetY();
  3375. }
  3376. // check for page break
  3377. $this->checkPageBreak($h);
  3378. $y = $this->GetY();
  3379. // get current page number
  3380. $startpage = $this->page;
  3381. if (!$this->empty_string($x)) {
  3382. $this->SetX($x);
  3383. } else {
  3384. $x = $this->GetX();
  3385. }
  3386. if ($this->empty_string($w) OR ($w <= 0)) {
  3387. if ($this->rtl) {
  3388. $w = $this->x - $this->lMargin;
  3389. } else {
  3390. $w = $this->w - $this->rMargin - $this->x;
  3391. }
  3392. }
  3393. // store original margin values
  3394. $lMargin = $this->lMargin;
  3395. $rMargin = $this->rMargin;
  3396. if ($this->rtl) {
  3397. $this->SetRightMargin($this->w - $this->x);
  3398. $this->SetLeftMargin($this->x - $w);
  3399. } else {
  3400. $this->SetLeftMargin($this->x);
  3401. $this->SetRightMargin($this->w - $this->x - $w);
  3402. }
  3403. $starty = $this->y;
  3404. if ($autopadding) {
  3405. // Adjust internal padding
  3406. if ($this->cMargin < ($this->LineWidth / 2)) {
  3407. $this->cMargin = ($this->LineWidth / 2);
  3408. }
  3409. // Add top space if needed
  3410. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3411. $this->y += $this->LineWidth / 2;
  3412. }
  3413. // add top padding
  3414. $this->y += $this->cMargin;
  3415. }
  3416. if ($ishtml) {
  3417. // ******* Write HTML text
  3418. $this->writeHTML($txt, true, 0, $reseth, true, $align);
  3419. $nl = 1;
  3420. } else {
  3421. // ******* Write text
  3422. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, false, $maxh);
  3423. }
  3424. if ($autopadding) {
  3425. // add bottom padding
  3426. $this->y += $this->cMargin;
  3427. // Add bottom space if needed
  3428. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3429. $this->y += $this->LineWidth / 2;
  3430. }
  3431. }
  3432. // Get end-of-text Y position
  3433. $currentY = $this->y;
  3434. // get latest page number
  3435. $endpage = $this->page;
  3436. // check if a new page has been created
  3437. if ($endpage > $startpage) {
  3438. // design borders around HTML cells.
  3439. for ($page=$startpage; $page <= $endpage; ++$page) {
  3440. $this->setPage($page);
  3441. if ($page == $startpage) {
  3442. $this->y = $starty; // put cursor at the beginning of cell on the first page
  3443. $h = $this->getPageHeight() - $starty - $this->getBreakMargin();
  3444. $cborder = $this->getBorderMode($border, $position='start');
  3445. } elseif ($page == $endpage) {
  3446. $this->y = $this->tMargin; // put cursor at the beginning of last page
  3447. $h = $currentY - $this->tMargin;
  3448. $cborder = $this->getBorderMode($border, $position='end');
  3449. } else {
  3450. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  3451. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  3452. $cborder = $this->getBorderMode($border, $position='middle');
  3453. }
  3454. $nx = $x;
  3455. // account for margin changes
  3456. if ($page > $startpage) {
  3457. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  3458. $nx = $x + ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  3459. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  3460. $nx = $x + ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  3461. }
  3462. }
  3463. $this->SetX($nx);
  3464. $ccode = $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, false);
  3465. if ($cborder OR $fill) {
  3466. $pagebuff = $this->getPageBuffer($this->page);
  3467. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  3468. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  3469. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3470. $this->intmrk[$this->page] += strlen($ccode."\n");
  3471. }
  3472. }
  3473. } else {
  3474. $h = max($h, ($currentY - $y));
  3475. // put cursor at the beginning of text
  3476. $this->SetY($y);
  3477. $this->SetX($x);
  3478. // design a cell around the text
  3479. $ccode = $this->getCellCode($w, $h, '', $border, 1, '', $fill, '', 0, true);
  3480. if ($border OR $fill) {
  3481. if (end($this->transfmrk[$this->page]) !== false) {
  3482. $pagemarkkey = key($this->transfmrk[$this->page]);
  3483. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  3484. } elseif ($this->InFooter) {
  3485. $pagemark = &$this->footerpos[$this->page];
  3486. } else {
  3487. $pagemark = &$this->intmrk[$this->page];
  3488. }
  3489. $pagebuff = $this->getPageBuffer($this->page);
  3490. $pstart = substr($pagebuff, 0, $pagemark);
  3491. $pend = substr($pagebuff, $pagemark);
  3492. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3493. $pagemark += strlen($ccode."\n");
  3494. }
  3495. }
  3496. // Get end-of-cell Y position
  3497. $currentY = $this->GetY();
  3498. // restore original margin values
  3499. $this->SetLeftMargin($lMargin);
  3500. $this->SetRightMargin($rMargin);
  3501. if ($ln > 0) {
  3502. //Go to the beginning of the next line
  3503. $this->SetY($currentY);
  3504. if ($ln == 2) {
  3505. $this->SetX($x + $w);
  3506. }
  3507. } else {
  3508. // go left or right by case
  3509. $this->setPage($startpage);
  3510. $this->y = $y;
  3511. $this->SetX($x + $w);
  3512. }
  3513. return $nl;
  3514. }
  3515. /**
  3516. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  3517. * @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>
  3518. * @param string multicell position: 'start', 'middle', 'end'
  3519. * @return border mode
  3520. * @access protected
  3521. * @since 4.4.002 (2008-12-09)
  3522. */
  3523. protected function getBorderMode($border, $position='start') {
  3524. if ((!$this->opencell) AND ($border == 1)) {
  3525. return 1;
  3526. }
  3527. $cborder = '';
  3528. switch ($position) {
  3529. case 'start': {
  3530. if ($border == 1) {
  3531. $cborder = 'LTR';
  3532. } else {
  3533. if (!(false === strpos($border, 'L'))) {
  3534. $cborder .= 'L';
  3535. }
  3536. if (!(false === strpos($border, 'T'))) {
  3537. $cborder .= 'T';
  3538. }
  3539. if (!(false === strpos($border, 'R'))) {
  3540. $cborder .= 'R';
  3541. }
  3542. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3543. $cborder .= 'B';
  3544. }
  3545. }
  3546. break;
  3547. }
  3548. case 'middle': {
  3549. if ($border == 1) {
  3550. $cborder = 'LR';
  3551. } else {
  3552. if (!(false === strpos($border, 'L'))) {
  3553. $cborder .= 'L';
  3554. }
  3555. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3556. $cborder .= 'T';
  3557. }
  3558. if (!(false === strpos($border, 'R'))) {
  3559. $cborder .= 'R';
  3560. }
  3561. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3562. $cborder .= 'B';
  3563. }
  3564. }
  3565. break;
  3566. }
  3567. case 'end': {
  3568. if ($border == 1) {
  3569. $cborder = 'LRB';
  3570. } else {
  3571. if (!(false === strpos($border, 'L'))) {
  3572. $cborder .= 'L';
  3573. }
  3574. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3575. $cborder .= 'T';
  3576. }
  3577. if (!(false === strpos($border, 'R'))) {
  3578. $cborder .= 'R';
  3579. }
  3580. if (!(false === strpos($border, 'B'))) {
  3581. $cborder .= 'B';
  3582. }
  3583. }
  3584. break;
  3585. }
  3586. default: {
  3587. $cborder = $border;
  3588. break;
  3589. }
  3590. }
  3591. return $cborder;
  3592. }
  3593. /**
  3594. * This method returns the estimated number of lines required to print the text.
  3595. * @param string $txt text to print
  3596. * @param float $w width of cell. If 0, they extend up to the right margin of the page.
  3597. * @return int Return the estimated number of lines.
  3598. * @access public
  3599. * @since 4.5.011
  3600. */
  3601. public function getNumLines($txt, $w=0) {
  3602. $lines = 0;
  3603. if ($this->empty_string($w) OR ($w <= 0)) {
  3604. if ($this->rtl) {
  3605. $w = $this->x - $this->lMargin;
  3606. } else {
  3607. $w = $this->w - $this->rMargin - $this->x;
  3608. }
  3609. }
  3610. // max column width
  3611. $wmax = $w - (2 * $this->cMargin);
  3612. // remove carriage returns
  3613. $txt = str_replace("\r", '', $txt);
  3614. // remove last newline (if any)
  3615. if (substr($txt,-1) == "\n") {
  3616. $txt = substr($txt, 0, -1);
  3617. }
  3618. // divide text in blocks
  3619. $txtblocks = explode("\n", $txt);
  3620. // for each block;
  3621. foreach ($txtblocks as $block) {
  3622. // estimate the number of lines
  3623. $lines += $this->empty_string($block) ? 1 : (ceil($this->GetStringWidth($block) / $wmax));
  3624. }
  3625. return $lines;
  3626. }
  3627. /**
  3628. * This method prints text from the current position.<br />
  3629. * @param float $h Line height
  3630. * @param string $txt String to print
  3631. * @param mixed $link URL or identifier returned by AddLink()
  3632. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  3633. * @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>
  3634. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  3635. * @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>
  3636. * @param boolean $firstline if true prints only the first line and return the remaining string.
  3637. * @param boolean $firstblock if true the string is the starting of a line.
  3638. * @param float $maxh maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  3639. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  3640. * @access public
  3641. * @since 1.5
  3642. */
  3643. public function Write($h, $txt, $link='', $fill=0, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0) {
  3644. if (strlen($txt) == 0) {
  3645. $txt = ' ';
  3646. }
  3647. // remove carriage returns
  3648. $s = str_replace("\r", '', $txt);
  3649. // check if string contains arabic text
  3650. if (preg_match(K_RE_PATTERN_ARABIC, $s)) {
  3651. $arabic = true;
  3652. } else {
  3653. $arabic = false;
  3654. }
  3655. // check if string contains RTL text
  3656. if ($arabic OR $this->tmprtl OR preg_match(K_RE_PATTERN_RTL, $txt)) {
  3657. $rtlmode = true;
  3658. } else {
  3659. $rtlmode = false;
  3660. }
  3661. // get a char width
  3662. $chrwidth = $this->GetCharWidth('.');
  3663. // get array of unicode values
  3664. $chars = $this->UTF8StringToArray($s);
  3665. // get array of chars
  3666. $uchars = $this->UTF8ArrayToUniArray($chars);
  3667. // get the number of characters
  3668. $nb = count($chars);
  3669. // replacement for SHY character (minus symbol)
  3670. $shy_replacement = 45;
  3671. $shy_replacement_char = $this->unichr($shy_replacement);
  3672. // widht for SHY replacement
  3673. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  3674. // store current position
  3675. $prevx = $this->x;
  3676. $prevy = $this->y;
  3677. // max Y
  3678. $maxy = $this->y + $maxh - $h - (2 * $this->cMargin);
  3679. // calculate remaining line width ($w)
  3680. if ($this->rtl) {
  3681. $w = $this->x - $this->lMargin;
  3682. } else {
  3683. $w = $this->w - $this->rMargin - $this->x;
  3684. }
  3685. // max column width
  3686. $wmax = $w - (2 * $this->cMargin);
  3687. $i = 0; // character position
  3688. $j = 0; // current starting position
  3689. $sep = -1; // position of the last blank space
  3690. $shy = false; // true if the last blank is a soft hypen (SHY)
  3691. $l = 0; // current string lenght
  3692. $nl = 0; //number of lines
  3693. $linebreak = false;
  3694. // for each character
  3695. while ($i < $nb) {
  3696. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  3697. $firstline = true;
  3698. }
  3699. //Get the current character
  3700. $c = $chars[$i];
  3701. if ($c == 10) { // 10 = "\n" = new line
  3702. //Explicit line break
  3703. if ($align == 'J') {
  3704. if ($this->rtl) {
  3705. $talign = 'R';
  3706. } else {
  3707. $talign = 'L';
  3708. }
  3709. } else {
  3710. $talign = $align;
  3711. }
  3712. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  3713. if ($firstline) {
  3714. $startx = $this->x;
  3715. $tmparr = array_slice($chars, $j, $i);
  3716. if ($rtlmode) {
  3717. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3718. }
  3719. $linew = $this->GetArrStringWidth($tmparr);
  3720. unset($tmparr);
  3721. if ($this->rtl) {
  3722. $this->endlinex = $startx - $linew;
  3723. } else {
  3724. $this->endlinex = $startx + $linew;
  3725. }
  3726. $w = $linew;
  3727. $tmpcmargin = $this->cMargin;
  3728. if ($maxh == 0) {
  3729. $this->cMargin = 0;
  3730. }
  3731. }
  3732. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  3733. unset($tmpstr);
  3734. if ($firstline) {
  3735. $this->cMargin = $tmpcmargin;
  3736. return ($this->UniArrSubString($uchars, $i));
  3737. }
  3738. ++$nl;
  3739. $j = $i + 1;
  3740. $l = 0;
  3741. $sep = -1;
  3742. $shy = false;
  3743. // account for margin changes
  3744. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  3745. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  3746. $this->AcceptPageBreak();
  3747. }
  3748. $w = $this->getRemainingWidth();
  3749. $wmax = $w - (2 * $this->cMargin);
  3750. } else {
  3751. // 160 is the non-breaking space.
  3752. // 173 is SHY (Soft Hypen).
  3753. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  3754. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  3755. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  3756. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  3757. // update last blank space position
  3758. $sep = $i;
  3759. // check if is a SHY
  3760. if ($c == 173) {
  3761. $shy = true;
  3762. } else {
  3763. $shy = false;
  3764. }
  3765. }
  3766. // update string length
  3767. if ((($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($arabic)) {
  3768. // with bidirectional algorithm some chars may be changed affecting the line length
  3769. // *** very slow ***
  3770. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, $i-$j+1), '', $this->tmprtl));
  3771. } else {
  3772. $l += $this->GetCharWidth($c);
  3773. }
  3774. if (($l > $wmax) OR ($shy AND (($l + $shy_replacement_width) > $wmax)) ) {
  3775. // we have reached the end of column
  3776. if ($sep == -1) {
  3777. // check if the line was already started
  3778. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  3779. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  3780. // print a void cell and go to next line
  3781. $this->Cell($w, $h, '', 0, 1);
  3782. $linebreak = true;
  3783. if ($firstline) {
  3784. return ($this->UniArrSubString($uchars, $j));
  3785. }
  3786. } else {
  3787. // truncate the word because do not fit on column
  3788. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  3789. if ($firstline) {
  3790. $startx = $this->x;
  3791. $tmparr = array_slice($chars, $j, $i);
  3792. if ($rtlmode) {
  3793. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3794. }
  3795. $linew = $this->GetArrStringWidth($tmparr);
  3796. unset($tmparr);
  3797. if ($this->rtl) {
  3798. $this->endlinex = $startx - $linew;
  3799. } else {
  3800. $this->endlinex = $startx + $linew;
  3801. }
  3802. $w = $linew;
  3803. $tmpcmargin = $this->cMargin;
  3804. if ($maxh == 0) {
  3805. $this->cMargin = 0;
  3806. }
  3807. }
  3808. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  3809. unset($tmpstr);
  3810. if ($firstline) {
  3811. $this->cMargin = $tmpcmargin;
  3812. return ($this->UniArrSubString($uchars, $i));
  3813. }
  3814. $j = $i;
  3815. --$i;
  3816. }
  3817. } else {
  3818. // word wrapping
  3819. if ($this->rtl AND (!$firstblock)) {
  3820. $endspace = 1;
  3821. } else {
  3822. $endspace = 0;
  3823. }
  3824. if ($shy) {
  3825. // add hypen (minus symbol) at the end of the line
  3826. $shy_width = $shy_replacement_width;
  3827. if ($this->rtl) {
  3828. $shy_char_left = $shy_replacement_char;
  3829. $shy_char_right = '';
  3830. } else {
  3831. $shy_char_left = '';
  3832. $shy_char_right = $shy_replacement_char;
  3833. }
  3834. } else {
  3835. $shy_width = 0;
  3836. $shy_char_left = '';
  3837. $shy_char_right = '';
  3838. }
  3839. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  3840. if ($firstline) {
  3841. $startx = $this->x;
  3842. $tmparr = array_slice($chars, $j, ($sep + $endspace));
  3843. if ($rtlmode) {
  3844. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3845. }
  3846. $linew = $this->GetArrStringWidth($tmparr);
  3847. unset($tmparr);
  3848. if ($this->rtl) {
  3849. $this->endlinex = $startx - $linew - $shy_width;
  3850. } else {
  3851. $this->endlinex = $startx + $linew + $shy_width;
  3852. }
  3853. $w = $linew;
  3854. $tmpcmargin = $this->cMargin;
  3855. if ($maxh == 0) {
  3856. $this->cMargin = 0;
  3857. }
  3858. }
  3859. // print the line
  3860. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  3861. unset($tmpstr);
  3862. if ($firstline) {
  3863. // return the remaining text
  3864. $this->cMargin = $tmpcmargin;
  3865. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  3866. }
  3867. $i = $sep;
  3868. $sep = -1;
  3869. $shy = false;
  3870. $j = ($i+1);
  3871. }
  3872. // account for margin changes
  3873. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  3874. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  3875. $this->AcceptPageBreak();
  3876. }
  3877. $w = $this->getRemainingWidth();
  3878. $wmax = $w - (2 * $this->cMargin);
  3879. if ($linebreak) {
  3880. $linebreak = false;
  3881. } else {
  3882. ++$nl;
  3883. $l = 0;
  3884. }
  3885. }
  3886. }
  3887. ++$i;
  3888. } // end while i < nb
  3889. // print last substring (if any)
  3890. if ($l > 0) {
  3891. switch ($align) {
  3892. case 'J':
  3893. case 'C': {
  3894. $w = $w;
  3895. break;
  3896. }
  3897. case 'L': {
  3898. if ($this->rtl) {
  3899. $w = $w;
  3900. } else {
  3901. $w = $l;
  3902. }
  3903. break;
  3904. }
  3905. case 'R': {
  3906. if ($this->rtl) {
  3907. $w = $l;
  3908. } else {
  3909. $w = $w;
  3910. }
  3911. break;
  3912. }
  3913. default: {
  3914. $w = $l;
  3915. break;
  3916. }
  3917. }
  3918. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  3919. if ($firstline) {
  3920. $startx = $this->x;
  3921. $tmparr = array_slice($chars, $j, $nb);
  3922. if ($rtlmode) {
  3923. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3924. }
  3925. $linew = $this->GetArrStringWidth($tmparr);
  3926. unset($tmparr);
  3927. if ($this->rtl) {
  3928. $this->endlinex = $startx - $linew;
  3929. } else {
  3930. $this->endlinex = $startx + $linew;
  3931. }
  3932. $w = $linew;
  3933. $tmpcmargin = $this->cMargin;
  3934. if ($maxh == 0) {
  3935. $this->cMargin = 0;
  3936. }
  3937. }
  3938. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  3939. unset($tmpstr);
  3940. if ($firstline) {
  3941. $this->cMargin = $tmpcmargin;
  3942. return ($this->UniArrSubString($uchars, $nb));
  3943. }
  3944. ++$nl;
  3945. }
  3946. if ($firstline) {
  3947. return '';
  3948. }
  3949. return $nl;
  3950. }
  3951. /**
  3952. * Returns the remaining width between the current position and margins.
  3953. * @return int Return the remaining width
  3954. * @access protected
  3955. */
  3956. protected function getRemainingWidth() {
  3957. if ($this->rtl) {
  3958. return ($this->x - $this->lMargin);
  3959. } else {
  3960. return ($this->w - $this->rMargin - $this->x);
  3961. }
  3962. }
  3963. /**
  3964. * Extract a slice of the $strarr array and return it as string.
  3965. * @param string $strarr The input array of characters.
  3966. * @param int $start the starting element of $strarr.
  3967. * @param int $end first element that will not be returned.
  3968. * @return Return part of a string
  3969. * @access public
  3970. */
  3971. public function UTF8ArrSubString($strarr, $start='', $end='') {
  3972. if (strlen($start) == 0) {
  3973. $start = 0;
  3974. }
  3975. if (strlen($end) == 0) {
  3976. $end = count($strarr);
  3977. }
  3978. $string = '';
  3979. for ($i=$start; $i < $end; ++$i) {
  3980. $string .= $this->unichr($strarr[$i]);
  3981. }
  3982. return $string;
  3983. }
  3984. /**
  3985. * Extract a slice of the $uniarr array and return it as string.
  3986. * @param string $uniarr The input array of characters.
  3987. * @param int $start the starting element of $strarr.
  3988. * @param int $end first element that will not be returned.
  3989. * @return Return part of a string
  3990. * @access public
  3991. * @since 4.5.037 (2009-04-07)
  3992. */
  3993. public function UniArrSubString($uniarr, $start='', $end='') {
  3994. if (strlen($start) == 0) {
  3995. $start = 0;
  3996. }
  3997. if (strlen($end) == 0) {
  3998. $end = count($uniarr);
  3999. }
  4000. $string = '';
  4001. for ($i=$start; $i < $end; ++$i) {
  4002. $string .= $uniarr[$i];
  4003. }
  4004. return $string;
  4005. }
  4006. /**
  4007. * Convert an array of UTF8 values to array of unicode characters
  4008. * @param string $ta The input array of UTF8 values.
  4009. * @return Return array of unicode characters
  4010. * @access public
  4011. * @since 4.5.037 (2009-04-07)
  4012. */
  4013. public function UTF8ArrayToUniArray($ta) {
  4014. return array_map(array($this, 'unichr'), $ta);
  4015. }
  4016. /**
  4017. * Returns the unicode caracter specified by UTF-8 code
  4018. * @param int $c UTF-8 code
  4019. * @return Returns the specified character.
  4020. * @author Miguel Perez, Nicola Asuni
  4021. * @access public
  4022. * @since 2.3.000 (2008-03-05)
  4023. */
  4024. public function unichr($c) {
  4025. if (!$this->isunicode) {
  4026. return chr($c);
  4027. } elseif ($c <= 0x7F) {
  4028. // one byte
  4029. return chr($c);
  4030. } elseif ($c <= 0x7FF) {
  4031. // two bytes
  4032. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  4033. } elseif ($c <= 0xFFFF) {
  4034. // three bytes
  4035. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4036. } elseif ($c <= 0x10FFFF) {
  4037. // four bytes
  4038. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4039. } else {
  4040. return '';
  4041. }
  4042. }
  4043. /**
  4044. * Puts an image in the page.
  4045. * The upper-left corner must be given.
  4046. * The dimensions can be specified in different ways:<ul>
  4047. * <li>explicit width and height (expressed in user unit)</li>
  4048. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  4049. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  4050. * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
  4051. * The format can be specified explicitly or inferred from the file extension.<br />
  4052. * It is possible to put a link on the image.<br />
  4053. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  4054. * @param string $file Name of the file containing the image.
  4055. * @param float $x Abscissa of the upper-left corner.
  4056. * @param float $y Ordinate of the upper-left corner.
  4057. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4058. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4059. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  4060. * @param mixed $link URL or identifier returned by AddLink().
  4061. * @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>
  4062. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4063. * @param int $dpi dot-per-inch resolution used on resize
  4064. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  4065. * @param boolean $ismask true if this image is a mask, false otherwise
  4066. * @param mixed $imgmask image object returned by this function or false
  4067. * @param mixed $border Indicates if borders must be drawn around the image. 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>
  4068. * @param boolean $fitbox If true scale image dimensions proportionally to fit within the ($w, $h) box.
  4069. * @return image information
  4070. * @access public
  4071. * @since 1.1
  4072. */
  4073. public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false) {
  4074. if ($x === '') {
  4075. $x = $this->x;
  4076. }
  4077. if ($y === '') {
  4078. $y = $this->y;
  4079. }
  4080. // get image dimensions
  4081. $imsize = @getimagesize($file);
  4082. if ($imsize === FALSE) {
  4083. // encode spaces on filename
  4084. $file = str_replace(' ', '%20', $file);
  4085. $imsize = @getimagesize($file);
  4086. if ($imsize === FALSE) {
  4087. $this->Error('[Image] No such file or directory in '.$file);
  4088. }
  4089. }
  4090. // get original image width and height in pixels
  4091. list($pixw, $pixh) = $imsize;
  4092. // calculate image width and height on document
  4093. if (($w <= 0) AND ($h <= 0)) {
  4094. // convert image size to document unit
  4095. $w = $this->pixelsToUnits($pixw);
  4096. $h = $this->pixelsToUnits($pixh);
  4097. } elseif ($w <= 0) {
  4098. $w = $h * $pixw / $pixh;
  4099. } elseif ($h <= 0) {
  4100. $h = $w * $pixh / $pixw;
  4101. } elseif ($fitbox AND ($w > 0) AND ($h > 0)) {
  4102. // scale image dimensions proportionally to fit within the ($w, $h) box
  4103. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  4104. $h = $w * $pixh / $pixw;
  4105. } else {
  4106. $w = $h * $pixw / $pixh;
  4107. }
  4108. }
  4109. // calculate new minimum dimensions in pixels
  4110. $neww = round($w * $this->k * $dpi / $this->dpi);
  4111. $newh = round($h * $this->k * $dpi / $this->dpi);
  4112. // check if resize is necessary (resize is used only to reduce the image)
  4113. if (($neww * $newh) >= ($pixw * $pixh)) {
  4114. $resize = false;
  4115. }
  4116. // check if image has been already added on document
  4117. if (!in_array($file, $this->imagekeys)) {
  4118. //First use of image, get info
  4119. if ($type == '') {
  4120. $fileinfo = pathinfo($file);
  4121. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  4122. $type = $fileinfo['extension'];
  4123. } else {
  4124. $this->Error('Image file has no extension and no type was specified: '.$file);
  4125. }
  4126. }
  4127. $type = strtolower($type);
  4128. if ($type == 'jpg') {
  4129. $type = 'jpeg';
  4130. }
  4131. $mqr = get_magic_quotes_runtime();
  4132. set_magic_quotes_runtime(0);
  4133. // Specific image handlers
  4134. $mtd = '_parse'.$type;
  4135. // GD image handler function
  4136. $gdfunction = 'imagecreatefrom'.$type;
  4137. $info = false;
  4138. if ((method_exists($this, $mtd)) AND (!($resize AND function_exists($gdfunction)))) {
  4139. // TCPDF image functions
  4140. $info = $this->$mtd($file);
  4141. if ($info == 'pngalpha') {
  4142. return $this->ImagePngAlpha($file, $x, $y, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign);
  4143. }
  4144. }
  4145. if (!$info) {
  4146. if (function_exists($gdfunction)) {
  4147. // GD library
  4148. $img = $gdfunction($file);
  4149. if ($resize) {
  4150. $imgr = imagecreatetruecolor($neww, $newh);
  4151. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  4152. $info = $this->_toJPEG($imgr);
  4153. } else {
  4154. $info = $this->_toJPEG($img);
  4155. }
  4156. } elseif (extension_loaded('imagick')) {
  4157. // ImageMagick library
  4158. $img = new Imagick();
  4159. $img->readImage($file);
  4160. if ($resize) {
  4161. $img->resizeImage($neww, $newh, 10, 1, false);
  4162. }
  4163. $img->setCompressionQuality($this->jpeg_quality);
  4164. $img->setImageFormat('jpeg');
  4165. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4166. $img->writeImage($tempname);
  4167. $info = $this->_parsejpeg($tempname);
  4168. unlink($tempname);
  4169. $img->destroy();
  4170. } else {
  4171. return;
  4172. }
  4173. }
  4174. if ($info === false) {
  4175. //If false, we cannot process image
  4176. return;
  4177. }
  4178. set_magic_quotes_runtime($mqr);
  4179. if ($ismask) {
  4180. // force grayscale
  4181. $info['cs'] = 'DeviceGray';
  4182. }
  4183. $info['i'] = $this->numimages + 1;
  4184. if ($imgmask !== false) {
  4185. $info['masked'] = $imgmask;
  4186. }
  4187. // add image to document
  4188. $this->setImageBuffer($file, $info);
  4189. } else {
  4190. $info = $this->getImageBuffer($file);
  4191. }
  4192. // Check whether we need a new page first as this does not fit
  4193. if ($this->checkPageBreak($h, $y)) {
  4194. $y = $this->GetY() + $this->cMargin;
  4195. }
  4196. // set bottomcoordinates
  4197. $this->img_rb_y = $y + $h;
  4198. // set alignment
  4199. if ($this->rtl) {
  4200. if ($palign == 'L') {
  4201. $ximg = $this->lMargin;
  4202. // set right side coordinate
  4203. $this->img_rb_x = $ximg + $w;
  4204. } elseif ($palign == 'C') {
  4205. $ximg = ($this->w - $x - $w) / 2;
  4206. // set right side coordinate
  4207. $this->img_rb_x = $ximg + $w;
  4208. } else {
  4209. $ximg = $this->w - $x - $w;
  4210. // set left side coordinate
  4211. $this->img_rb_x = $ximg;
  4212. }
  4213. } else {
  4214. if ($palign == 'R') {
  4215. $ximg = $this->w - $this->rMargin - $w;
  4216. // set left side coordinate
  4217. $this->img_rb_x = $ximg;
  4218. } elseif ($palign == 'C') {
  4219. $ximg = ($this->w - $x - $w) / 2;
  4220. // set right side coordinate
  4221. $this->img_rb_x = $ximg + $w;
  4222. } else {
  4223. $ximg = $x;
  4224. // set right side coordinate
  4225. $this->img_rb_x = $ximg + $w;
  4226. }
  4227. }
  4228. if ($ismask) {
  4229. // embed hidden, ouside the canvas
  4230. $xkimg = ($this->pagedim[$this->page]['w'] + 10);
  4231. } else {
  4232. $xkimg = $ximg * $this->k;
  4233. }
  4234. $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']));
  4235. if (!empty($border)) {
  4236. $bx = $x;
  4237. $by = $y;
  4238. $this->x = $ximg;
  4239. $this->y = $y;
  4240. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  4241. $this->x = $bx;
  4242. $this->y = $by;
  4243. }
  4244. if ($link) {
  4245. $this->Link($ximg, $y, $w, $h, $link, 0);
  4246. }
  4247. // set pointer to align the successive text/objects
  4248. switch($align) {
  4249. case 'T': {
  4250. $this->y = $y;
  4251. $this->x = $this->img_rb_x;
  4252. break;
  4253. }
  4254. case 'M': {
  4255. $this->y = $y + round($h/2);
  4256. $this->x = $this->img_rb_x;
  4257. break;
  4258. }
  4259. case 'B': {
  4260. $this->y = $this->img_rb_y;
  4261. $this->x = $this->img_rb_x;
  4262. break;
  4263. }
  4264. case 'N': {
  4265. $this->SetY($this->img_rb_y);
  4266. break;
  4267. }
  4268. default:{
  4269. break;
  4270. }
  4271. }
  4272. $this->endlinex = $this->img_rb_x;
  4273. return $info['i'];
  4274. }
  4275. /**
  4276. * Convert the loaded php image to a JPEG and then return a structure for the PDF creator.
  4277. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  4278. * @param string $file Image file name.
  4279. * @param image $image Image object.
  4280. * return image JPEG image object.
  4281. * @access protected
  4282. */
  4283. protected function _toJPEG($image) {
  4284. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4285. imagejpeg($image, $tempname, $this->jpeg_quality);
  4286. imagedestroy($image);
  4287. $retvars = $this->_parsejpeg($tempname);
  4288. // tidy up by removing temporary image
  4289. unlink($tempname);
  4290. return $retvars;
  4291. }
  4292. /**
  4293. * Extract info from a JPEG file without using the GD library.
  4294. * @param string $file image file to parse
  4295. * @return array structure containing the image data
  4296. * @access protected
  4297. */
  4298. protected function _parsejpeg($file) {
  4299. $a = getimagesize($file);
  4300. if (empty($a)) {
  4301. $this->Error('Missing or incorrect image file: '.$file);
  4302. }
  4303. if ($a[2] != 2) {
  4304. $this->Error('Not a JPEG file: '.$file);
  4305. }
  4306. if ((!isset($a['channels'])) OR ($a['channels'] == 3)) {
  4307. $colspace = 'DeviceRGB';
  4308. } elseif ($a['channels'] == 4) {
  4309. $colspace = 'DeviceCMYK';
  4310. } else {
  4311. $colspace = 'DeviceGray';
  4312. }
  4313. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  4314. $data = file_get_contents($file);
  4315. return array('w' => $a[0], 'h' => $a[1], 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  4316. }
  4317. /**
  4318. * Extract info from a PNG file without using the GD library.
  4319. * @param string $file image file to parse
  4320. * @return array structure containing the image data
  4321. * @access protected
  4322. */
  4323. protected function _parsepng($file) {
  4324. $f = fopen($file, 'rb');
  4325. if ($f === false) {
  4326. $this->Error('Can\'t open image file: '.$file);
  4327. }
  4328. //Check signature
  4329. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  4330. $this->Error('Not a PNG file: '.$file);
  4331. }
  4332. //Read header chunk
  4333. fread($f, 4);
  4334. if (fread($f, 4) != 'IHDR') {
  4335. $this->Error('Incorrect PNG file: '.$file);
  4336. }
  4337. $w = $this->_freadint($f);
  4338. $h = $this->_freadint($f);
  4339. $bpc = ord(fread($f, 1));
  4340. if ($bpc > 8) {
  4341. //$this->Error('16-bit depth not supported: '.$file);
  4342. fclose($f);
  4343. return false;
  4344. }
  4345. $ct = ord(fread($f, 1));
  4346. if ($ct == 0) {
  4347. $colspace = 'DeviceGray';
  4348. } elseif ($ct == 2) {
  4349. $colspace = 'DeviceRGB';
  4350. } elseif ($ct == 3) {
  4351. $colspace = 'Indexed';
  4352. } else {
  4353. // alpha channel
  4354. fclose($f);
  4355. return 'pngalpha';
  4356. }
  4357. if (ord(fread($f, 1)) != 0) {
  4358. //$this->Error('Unknown compression method: '.$file);
  4359. fclose($f);
  4360. return false;
  4361. }
  4362. if (ord(fread($f, 1)) != 0) {
  4363. //$this->Error('Unknown filter method: '.$file);
  4364. fclose($f);
  4365. return false;
  4366. }
  4367. if (ord(fread($f, 1)) != 0) {
  4368. //$this->Error('Interlacing not supported: '.$file);
  4369. fclose($f);
  4370. return false;
  4371. }
  4372. fread($f, 4);
  4373. $parms = '/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  4374. //Scan chunks looking for palette, transparency and image data
  4375. $pal = '';
  4376. $trns = '';
  4377. $data = '';
  4378. do {
  4379. $n = $this->_freadint($f);
  4380. $type = fread($f, 4);
  4381. if ($type == 'PLTE') {
  4382. //Read palette
  4383. $pal = $this->rfread($f, $n);
  4384. fread($f, 4);
  4385. } elseif ($type == 'tRNS') {
  4386. //Read transparency info
  4387. $t = $this->rfread($f, $n);
  4388. if ($ct == 0) {
  4389. $trns = array(ord(substr($t, 1, 1)));
  4390. } elseif ($ct == 2) {
  4391. $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  4392. } else {
  4393. $pos = strpos($t, chr(0));
  4394. if ($pos !== false) {
  4395. $trns = array($pos);
  4396. }
  4397. }
  4398. fread($f, 4);
  4399. } elseif ($type == 'IDAT') {
  4400. //Read image data block
  4401. $data .= $this->rfread($f, $n);
  4402. fread($f, 4);
  4403. } elseif ($type == 'IEND') {
  4404. break;
  4405. } else {
  4406. $this->rfread($f, $n + 4);
  4407. }
  4408. } while ($n);
  4409. if (($colspace == 'Indexed') AND (empty($pal))) {
  4410. //$this->Error('Missing palette in '.$file);
  4411. fclose($f);
  4412. return false;
  4413. }
  4414. fclose($f);
  4415. return array('w' => $w, 'h' => $h, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  4416. }
  4417. /**
  4418. * Binary-safe and URL-safe file read.
  4419. * Reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met: length bytes have been read; EOF (end of file) is reached.
  4420. * @param resource $handle
  4421. * @param int $length
  4422. * @return Returns the read string or FALSE in case of error.
  4423. * @author Nicola Asuni
  4424. * @access protected
  4425. * @since 4.5.027 (2009-03-16)
  4426. */
  4427. protected function rfread($handle, $length) {
  4428. $data = fread($handle, $length);
  4429. if ($data === false) {
  4430. return false;
  4431. }
  4432. $rest = $length - strlen($data);
  4433. if ($rest > 0) {
  4434. $data .= $this->rfread($handle, $rest);
  4435. }
  4436. return $data;
  4437. }
  4438. /**
  4439. * Extract info from a PNG image with alpha channel using the GD library.
  4440. * @param string $file Name of the file containing the image.
  4441. * @param float $x Abscissa of the upper-left corner.
  4442. * @param float $y Ordinate of the upper-left corner.
  4443. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4444. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4445. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  4446. * @param mixed $link URL or identifier returned by AddLink().
  4447. * @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>
  4448. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4449. * @param int $dpi dot-per-inch resolution used on resize
  4450. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  4451. * @author Valentin Schmidt, Nicola Asuni
  4452. * @access protected
  4453. * @since 4.3.007 (2008-12-04)
  4454. * @see Image()
  4455. */
  4456. protected function ImagePngAlpha($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='') {
  4457. // get image size
  4458. list($wpx, $hpx) = getimagesize($file);
  4459. // generate images
  4460. $img = imagecreatefrompng($file);
  4461. $imgalpha = imagecreate($wpx, $hpx);
  4462. // generate gray scale pallete
  4463. for ($c = 0; $c < 256; ++$c) {
  4464. ImageColorAllocate($imgalpha, $c, $c, $c);
  4465. }
  4466. // extract alpha channel
  4467. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  4468. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  4469. $colorindex = imagecolorat($img, $xpx, $ypx);
  4470. $col = imagecolorsforindex($img, $colorindex);
  4471. imagesetpixel($imgalpha, $xpx, $ypx, $this->getGDgamma((127 - $col['alpha']) * 255 / 127));
  4472. }
  4473. }
  4474. // create temp alpha file
  4475. $tempfile_alpha = tempnam(K_PATH_CACHE, 'mska_');
  4476. imagepng($imgalpha, $tempfile_alpha);
  4477. imagedestroy($imgalpha);
  4478. // extract image without alpha channel
  4479. $imgplain = imagecreatetruecolor($wpx, $hpx);
  4480. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  4481. // create temp image file
  4482. $tempfile_plain = tempnam(K_PATH_CACHE, 'mskp_');
  4483. imagepng($imgplain, $tempfile_plain);
  4484. imagedestroy($imgplain);
  4485. // embed mask image
  4486. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  4487. // embed image, masked with previously embedded mask
  4488. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  4489. // remove temp files
  4490. unlink($tempfile_alpha);
  4491. unlink($tempfile_plain);
  4492. }
  4493. /**
  4494. * Correct the gamma value to be used with GD library
  4495. * @param float $v the gamma value to be corrected
  4496. * @access protected
  4497. * @since 4.3.007 (2008-12-04)
  4498. */
  4499. protected function getGDgamma($v) {
  4500. return (pow(($v / 255), 2.2) * 255);
  4501. }
  4502. /**
  4503. * Performs a line break.
  4504. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  4505. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  4506. * @param boolean $cell if true add a cMargin to the x coordinate
  4507. * @access public
  4508. * @since 1.0
  4509. * @see Cell()
  4510. */
  4511. public function Ln($h='', $cell=false) {
  4512. //Line feed; default value is last cell height
  4513. if ($cell) {
  4514. $cellmargin = $this->cMargin;
  4515. } else {
  4516. $cellmargin = 0;
  4517. }
  4518. if ($this->rtl) {
  4519. $this->x = $this->w - $this->rMargin - $cellmargin;
  4520. } else {
  4521. $this->x = $this->lMargin + $cellmargin;
  4522. }
  4523. if (is_string($h)) {
  4524. $this->y += $this->lasth;
  4525. } else {
  4526. $this->y += $h;
  4527. }
  4528. $this->newline = true;
  4529. }
  4530. /**
  4531. * Returns the relative X value of current position.
  4532. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  4533. * @return float
  4534. * @access public
  4535. * @since 1.2
  4536. * @see SetX(), GetY(), SetY()
  4537. */
  4538. public function GetX() {
  4539. //Get x position
  4540. if ($this->rtl) {
  4541. return ($this->w - $this->x);
  4542. } else {
  4543. return $this->x;
  4544. }
  4545. }
  4546. /**
  4547. * Returns the absolute X value of current position.
  4548. * @return float
  4549. * @access public
  4550. * @since 1.2
  4551. * @see SetX(), GetY(), SetY()
  4552. */
  4553. public function GetAbsX() {
  4554. return $this->x;
  4555. }
  4556. /**
  4557. * Returns the ordinate of the current position.
  4558. * @return float
  4559. * @access public
  4560. * @since 1.0
  4561. * @see SetY(), GetX(), SetX()
  4562. */
  4563. public function GetY() {
  4564. //Get y position
  4565. return $this->y;
  4566. }
  4567. /**
  4568. * Defines the abscissa of the current position.
  4569. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  4570. * @param float $x The value of the abscissa.
  4571. * @access public
  4572. * @since 1.2
  4573. * @see GetX(), GetY(), SetY(), SetXY()
  4574. */
  4575. public function SetX($x) {
  4576. //Set x position
  4577. if ($this->rtl) {
  4578. if ($x >= 0) {
  4579. $this->x = $this->w - $x;
  4580. } else {
  4581. $this->x = abs($x);
  4582. }
  4583. } else {
  4584. if ($x >= 0) {
  4585. $this->x = $x;
  4586. } else {
  4587. $this->x = $this->w + $x;
  4588. }
  4589. }
  4590. if ($this->x < 0) {
  4591. $this->x = 0;
  4592. }
  4593. if ($this->x > $this->w) {
  4594. $this->x = $this->w;
  4595. }
  4596. }
  4597. /**
  4598. * Moves the current abscissa back to the left margin and sets the ordinate.
  4599. * If the passed value is negative, it is relative to the bottom of the page.
  4600. * @param float $y The value of the ordinate.
  4601. * @param bool $resetx if true (default) reset the X position.
  4602. * @access public
  4603. * @since 1.0
  4604. * @see GetX(), GetY(), SetY(), SetXY()
  4605. */
  4606. public function SetY($y, $resetx=true) {
  4607. if ($resetx) {
  4608. //reset x
  4609. if ($this->rtl) {
  4610. $this->x = $this->w - $this->rMargin;
  4611. } else {
  4612. $this->x = $this->lMargin;
  4613. }
  4614. }
  4615. if ($y >= 0) {
  4616. $this->y = $y;
  4617. } else {
  4618. $this->y = $this->h + $y;
  4619. }
  4620. if ($this->y < 0) {
  4621. $this->y = 0;
  4622. }
  4623. if ($this->y > $this->h) {
  4624. $this->y = $this->h;
  4625. }
  4626. }
  4627. /**
  4628. * Defines the abscissa and ordinate of the current position.
  4629. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  4630. * @param float $x The value of the abscissa
  4631. * @param float $y The value of the ordinate
  4632. * @access public
  4633. * @since 1.2
  4634. * @see SetX(), SetY()
  4635. */
  4636. public function SetXY($x, $y) {
  4637. //Set x and y positions
  4638. $this->SetY($y);
  4639. $this->SetX($x);
  4640. }
  4641. /**
  4642. * Send the document to a given destination: string, local file or browser.
  4643. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  4644. * The method first calls Close() if necessary to terminate the document.
  4645. * @param string $name The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
  4646. * @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 (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local file with the name given by name.</li><li>S: return the document as a string. name is ignored.</li></ul>
  4647. * @access public
  4648. * @since 1.0
  4649. * @see Close()
  4650. */
  4651. public function Output($name='doc.pdf', $dest='I') {
  4652. //Output PDF to some destination
  4653. //Finish document if necessary
  4654. if ($this->state < 3) {
  4655. $this->Close();
  4656. }
  4657. //Normalize parameters
  4658. if (is_bool($dest)) {
  4659. $dest = $dest ? 'D' : 'F';
  4660. }
  4661. $dest = strtoupper($dest);
  4662. if ($dest != 'F') {
  4663. $name = preg_replace('/[\s]+/', '_', $name);
  4664. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  4665. }
  4666. if ($this->sign) {
  4667. // *** apply digital signature to the document ***
  4668. // get the document content
  4669. $pdfdoc = $this->getBuffer();
  4670. // remove last newline
  4671. $pdfdoc = substr($pdfdoc, 0, -1);
  4672. // Remove the original buffer
  4673. if (isset($this->diskcache) AND $this->diskcache) {
  4674. // remove buffer file from cache
  4675. unlink($this->buffer);
  4676. }
  4677. unset($this->buffer);
  4678. // remove filler space
  4679. $tmppos = strpos($pdfdoc, '/ByteRange[0 ********** ********** **********]') + 58;
  4680. $pdfdoc = substr($pdfdoc, 0, $tmppos).substr($pdfdoc, $tmppos + $this->signature_max_lenght);
  4681. // define the ByteRange
  4682. $byte_range = array();
  4683. $byte_range[0] = 0;
  4684. $byte_range[1] = $tmppos - 1;
  4685. $byte_range[2] = $byte_range[1] + $this->signature_max_lenght;
  4686. $byte_range[3] = strlen($pdfdoc) - $byte_range[1];
  4687. // replace the ByteRange
  4688. $byterange = sprintf('/ByteRange[0 %010u %010u %010u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  4689. $pdfdoc = str_replace('/ByteRange[0 ********** ********** **********]', $byterange, $pdfdoc);
  4690. // write the document to a temporary folder
  4691. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  4692. $f = fopen($tempdoc, 'wb');
  4693. if (!$f) {
  4694. $this->Error('Unable to create temporary file: '.$tempdoc);
  4695. }
  4696. $pdfdoc_lenght = strlen($pdfdoc);
  4697. fwrite($f, $pdfdoc, $pdfdoc_lenght);
  4698. fclose($f);
  4699. // get digital signature.
  4700. // IS THE FOLLOWING PROCEDURE CORRECT? THE SIGNED DOCUMENTS ARE NOT VALID!
  4701. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  4702. if (empty($this->signature_data['extracerts'])) {
  4703. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  4704. } else {
  4705. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED, $this->signature_data['extracerts']);
  4706. }
  4707. unlink($tempdoc);
  4708. // read signature
  4709. $signature = file_get_contents($tempsign, false, null, $pdfdoc_lenght);
  4710. unlink($tempsign);
  4711. // extract signature
  4712. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  4713. $tmparr = explode("\n\n", $signature);
  4714. $signature = $tmparr[1];
  4715. unset($tmparr);
  4716. // decode signature
  4717. $signature = base64_decode(trim($signature));
  4718. // convert signature to hex
  4719. $signature = current(unpack('H*', $signature));
  4720. $signature = str_pad($signature, $this->signature_max_lenght, '0');
  4721. // Add signature to the document
  4722. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).$signature.substr($pdfdoc, (0 - $byte_range[3]));
  4723. $this->diskcache = false;
  4724. $this->buffer = &$pdfdoc;
  4725. $this->bufferlen = strlen($pdfdoc);
  4726. }
  4727. switch($dest) {
  4728. case 'I': {
  4729. // Send PDF to the standard output
  4730. if (ob_get_contents()) {
  4731. $this->Error('Some data has already been output, can\'t send PDF file');
  4732. }
  4733. if (php_sapi_name() != 'cli') {
  4734. //We send to a browser
  4735. header('Content-Type: application/pdf');
  4736. if (headers_sent()) {
  4737. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  4738. }
  4739. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  4740. header('Pragma: public');
  4741. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  4742. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  4743. header('Content-Length: '.$this->bufferlen);
  4744. header('Content-Disposition: inline; filename="'.basename($name).'";');
  4745. }
  4746. echo $this->getBuffer();
  4747. break;
  4748. }
  4749. case 'D': {
  4750. // Download PDF as file
  4751. if (ob_get_contents()) {
  4752. $this->Error('Some data has already been output, can\'t send PDF file');
  4753. }
  4754. header('Content-Description: File Transfer');
  4755. if (headers_sent()) {
  4756. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  4757. }
  4758. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  4759. header('Pragma: public');
  4760. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  4761. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  4762. // force download dialog
  4763. header('Content-Type: application/force-download');
  4764. //crm-now: changed to avoid error 500 with fastcgi
  4765. //header('Content-Type: application/octet-stream', false);
  4766. //header('Content-Type: application/download', false);
  4767. //header('Content-Type: application/pdf', false);
  4768. // use the Content-Disposition header to supply a recommended filename
  4769. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  4770. header('Content-Transfer-Encoding: binary');
  4771. header('Content-Length: '.$this->bufferlen);
  4772. echo $this->getBuffer();
  4773. break;
  4774. }
  4775. case 'F': {
  4776. // Save PDF to a local file
  4777. if ($this->diskcache) {
  4778. copy($this->buffer, $name);
  4779. } else {
  4780. $f = fopen($name, 'wb');
  4781. if (!$f) {
  4782. $this->Error('Unable to create output file: '.$name);
  4783. }
  4784. fwrite($f, $this->getBuffer(), $this->bufferlen);
  4785. fclose($f);
  4786. }
  4787. break;
  4788. }
  4789. case 'S': {
  4790. // Returns PDF as a string
  4791. return $this->getBuffer();
  4792. }
  4793. default: {
  4794. $this->Error('Incorrect output destination: '.$dest);
  4795. }
  4796. }
  4797. return '';
  4798. }
  4799. /**
  4800. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  4801. * @param boolean $destroyall if true destroys all class variables, otherwise preserves critical variables.
  4802. * @param boolean $preserve_objcopy if true preserves the objcopy variable
  4803. * @access public
  4804. * @since 4.5.016 (2009-02-24)
  4805. */
  4806. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  4807. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  4808. // remove buffer file from cache
  4809. unlink($this->buffer);
  4810. }
  4811. foreach (array_keys(get_object_vars($this)) as $val) {
  4812. if ($destroyall OR (
  4813. ($val != 'internal_encoding')
  4814. AND ($val != 'state')
  4815. AND ($val != 'bufferlen')
  4816. AND ($val != 'buffer')
  4817. AND ($val != 'diskcache')
  4818. AND ($val != 'sign')
  4819. AND ($val != 'signature_data')
  4820. AND ($val != 'signature_max_lenght')
  4821. )) {
  4822. if (!$preserve_objcopy OR ($val != 'objcopy')) {
  4823. unset($this->$val);
  4824. }
  4825. }
  4826. }
  4827. }
  4828. /**
  4829. * Check for locale-related bug
  4830. * @access protected
  4831. */
  4832. protected function _dochecks() {
  4833. //Check for locale-related bug
  4834. if (1.1 == 1) {
  4835. $this->Error('Don\'t alter the locale before including class file');
  4836. }
  4837. //Check for decimal separator
  4838. if (sprintf('%.1F', 1.0) != '1.0') {
  4839. setlocale(LC_NUMERIC, 'C');
  4840. }
  4841. }
  4842. /**
  4843. * Return fonts path
  4844. * @return string
  4845. * @access protected
  4846. */
  4847. protected function _getfontpath() {
  4848. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  4849. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  4850. }
  4851. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  4852. }
  4853. /**
  4854. * Output pages.
  4855. * @access protected
  4856. */
  4857. protected function _putpages() {
  4858. $nb = $this->numpages;
  4859. if (!empty($this->AliasNbPages)) {
  4860. $nbs = $this->formatPageNumber($nb);
  4861. $nbu = $this->UTF8ToUTF16BE($nbs, false); // replacement for unicode font
  4862. $alias_a = $this->_escape($this->AliasNbPages);
  4863. $alias_au = $this->_escape('{'.$this->AliasNbPages.'}');
  4864. if ($this->isunicode) {
  4865. $alias_b = $this->_escape($this->UTF8ToLatin1($this->AliasNbPages));
  4866. $alias_bu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNbPages.'}'));
  4867. $alias_c = $this->_escape($this->utf8StrRev($this->AliasNbPages, false, $this->tmprtl));
  4868. $alias_cu = $this->_escape($this->utf8StrRev('{'.$this->AliasNbPages.'}', false, $this->tmprtl));
  4869. }
  4870. }
  4871. if (!empty($this->AliasNumPage)) {
  4872. $alias_pa = $this->_escape($this->AliasNumPage);
  4873. $alias_pau = $this->_escape('{'.$this->AliasNumPage.'}');
  4874. if ($this->isunicode) {
  4875. $alias_pb = $this->_escape($this->UTF8ToLatin1($this->AliasNumPage));
  4876. $alias_pbu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNumPage.'}'));
  4877. $alias_pc = $this->_escape($this->utf8StrRev($this->AliasNumPage, false, $this->tmprtl));
  4878. $alias_pcu = $this->_escape($this->utf8StrRev('{'.$this->AliasNumPage.'}', false, $this->tmprtl));
  4879. }
  4880. }
  4881. $pagegroupnum = 0;
  4882. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  4883. for ($n=1; $n <= $nb; ++$n) {
  4884. $temppage = $this->getPageBuffer($n);
  4885. if (!empty($this->pagegroups)) {
  4886. if(isset($this->newpagegroup[$n])) {
  4887. $pagegroupnum = 0;
  4888. }
  4889. ++$pagegroupnum;
  4890. foreach ($this->pagegroups as $k => $v) {
  4891. // replace total pages group numbers
  4892. $vs = $this->formatPageNumber($v);
  4893. $vu = $this->UTF8ToUTF16BE($vs, false);
  4894. $alias_ga = $this->_escape($k);
  4895. $alias_gau = $this->_escape('{'.$k.'}');
  4896. if ($this->isunicode) {
  4897. $alias_gb = $this->_escape($this->UTF8ToLatin1($k));
  4898. $alias_gbu = $this->_escape($this->UTF8ToLatin1('{'.$k.'}'));
  4899. $alias_gc = $this->_escape($this->utf8StrRev($k, false, $this->tmprtl));
  4900. $alias_gcu = $this->_escape($this->utf8StrRev('{'.$k.'}', false, $this->tmprtl));
  4901. }
  4902. $temppage = str_replace($alias_gau, $vu, $temppage);
  4903. if ($this->isunicode) {
  4904. $temppage = str_replace($alias_gbu, $vu, $temppage);
  4905. $temppage = str_replace($alias_gcu, $vu, $temppage);
  4906. $temppage = str_replace($alias_gb, $vs, $temppage);
  4907. $temppage = str_replace($alias_gc, $vs, $temppage);
  4908. }
  4909. $temppage = str_replace($alias_ga, $vs, $temppage);
  4910. // replace page group numbers
  4911. $pvs = $this->formatPageNumber($pagegroupnum);
  4912. $pvu = $this->UTF8ToUTF16BE($pvs, false);
  4913. $pk = str_replace('{nb', '{pnb', $k);
  4914. $alias_pga = $this->_escape($pk);
  4915. $alias_pgau = $this->_escape('{'.$pk.'}');
  4916. if ($this->isunicode) {
  4917. $alias_pgb = $this->_escape($this->UTF8ToLatin1($pk));
  4918. $alias_pgbu = $this->_escape($this->UTF8ToLatin1('{'.$pk.'}'));
  4919. $alias_pgc = $this->_escape($this->utf8StrRev($pk, false, $this->tmprtl));
  4920. $alias_pgcu = $this->_escape($this->utf8StrRev('{'.$pk.'}', false, $this->tmprtl));
  4921. }
  4922. $temppage = str_replace($alias_pgau, $pvu, $temppage);
  4923. if ($this->isunicode) {
  4924. $temppage = str_replace($alias_pgbu, $pvu, $temppage);
  4925. $temppage = str_replace($alias_pgcu, $pvu, $temppage);
  4926. $temppage = str_replace($alias_pgb, $pvs, $temppage);
  4927. $temppage = str_replace($alias_pgc, $pvs, $temppage);
  4928. }
  4929. $temppage = str_replace($alias_pga, $pvs, $temppage);
  4930. }
  4931. }
  4932. if (!empty($this->AliasNbPages)) {
  4933. // replace total pages number
  4934. $temppage = str_replace($alias_au, $nbu, $temppage);
  4935. if ($this->isunicode) {
  4936. $temppage = str_replace($alias_bu, $nbu, $temppage);
  4937. $temppage = str_replace($alias_cu, $nbu, $temppage);
  4938. $temppage = str_replace($alias_b, $nbs, $temppage);
  4939. $temppage = str_replace($alias_c, $nbs, $temppage);
  4940. }
  4941. $temppage = str_replace($alias_a, $nbs, $temppage);
  4942. }
  4943. if (!empty($this->AliasNumPage)) {
  4944. // replace page number
  4945. $pnbs = $this->formatPageNumber($n);
  4946. $pnbu = $this->UTF8ToUTF16BE($pnbs, false); // replacement for unicode font
  4947. $temppage = str_replace($alias_pau, $pnbu, $temppage);
  4948. if ($this->isunicode) {
  4949. $temppage = str_replace($alias_pbu, $pnbu, $temppage);
  4950. $temppage = str_replace($alias_pcu, $pnbu, $temppage);
  4951. $temppage = str_replace($alias_pb, $pnbs, $temppage);
  4952. $temppage = str_replace($alias_pc, $pnbs, $temppage);
  4953. }
  4954. $temppage = str_replace($alias_pa, $pnbs, $temppage);
  4955. }
  4956. $temppage = str_replace($this->epsmarker, '', $temppage);
  4957. //$this->setPageBuffer($n, $temppage);
  4958. //Page
  4959. $this->_newobj();
  4960. $this->_out('<</Type /Page');
  4961. $this->_out('/Parent 1 0 R');
  4962. $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->pagedim[$n]['w'], $this->pagedim[$n]['h']));
  4963. $this->_out('/Resources 2 0 R');
  4964. $this->_putannots($n);
  4965. $this->_out('/Contents '.($this->n + 1).' 0 R>>');
  4966. $this->_out('endobj');
  4967. //Page content
  4968. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  4969. $this->_newobj();
  4970. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  4971. $this->_putstream($p);
  4972. $this->_out('endobj');
  4973. if ($this->diskcache) {
  4974. // remove temporary files
  4975. unlink($this->pages[$n]);
  4976. }
  4977. }
  4978. //Pages root
  4979. $this->offsets[1] = $this->bufferlen;
  4980. $this->_out('1 0 obj');
  4981. $this->_out('<</Type /Pages');
  4982. $kids='/Kids [';
  4983. for ($i=0; $i < $nb; ++$i) {
  4984. $kids .= (3 + (2 * $i)).' 0 R ';
  4985. }
  4986. $this->_out($kids.']');
  4987. $this->_out('/Count '.$nb);
  4988. //$this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->pagedim[0]['w'],$this->pagedim[0]['h']));
  4989. $this->_out('>>');
  4990. $this->_out('endobj');
  4991. }
  4992. /**
  4993. * Output Page Annotations.
  4994. * !!! THIS FUNCTION IS NOT YET COMPLETED !!!
  4995. * See section 8.4 of PDF reference.
  4996. * @param int $n page number
  4997. * @access protected
  4998. * @author Nicola Asuni
  4999. * @since 4.0.018 (2008-08-06)
  5000. */
  5001. protected function _putannots($n) {
  5002. if (isset($this->PageAnnots[$n])) {
  5003. $annots = '/Annots [';
  5004. foreach ($this->PageAnnots[$n] as $key => $pl) {
  5005. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  5006. $a = $pl['x'] * $this->k;
  5007. $b = $this->pagedim[$n]['h'] - ($pl['y'] * $this->k);
  5008. $c = $pl['w'] * $this->k;
  5009. $d = $pl['h'] * $this->k;
  5010. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b-$d);
  5011. $annots .= "\n";
  5012. $annots .= '<</Type /Annot';
  5013. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  5014. $annots .= ' /Rect ['.$rect.']';
  5015. $annots .= ' /Contents '.$this->_textstring($pl['txt']);
  5016. //$annots .= ' /P ';
  5017. $annots .= ' /NM '.$this->_textstring(sprintf('%04u-%04u', $n, $key));
  5018. $annots .= ' /M '.$this->_datastring('D:'.date('YmdHis'));
  5019. if (isset($pl['opt']['f'])) {
  5020. $val = 0;
  5021. if (is_array($pl['opt']['f'])) {
  5022. foreach ($pl['opt']['f'] as $f) {
  5023. switch (strtolower($f)) {
  5024. case 'invisible': {
  5025. $val += 1 << 0;
  5026. break;
  5027. }
  5028. case 'hidden': {
  5029. $val += 1 << 1;
  5030. break;
  5031. }
  5032. case 'print': {
  5033. $val += 1 << 2;
  5034. break;
  5035. }
  5036. case 'nozoom': {
  5037. $val += 1 << 3;
  5038. break;
  5039. }
  5040. case 'norotate': {
  5041. $val += 1 << 4;
  5042. break;
  5043. }
  5044. case 'noview': {
  5045. $val += 1 << 5;
  5046. break;
  5047. }
  5048. case 'readonly': {
  5049. $val += 1 << 6;
  5050. break;
  5051. }
  5052. case 'locked': {
  5053. $val += 1 << 8;
  5054. break;
  5055. }
  5056. case 'togglenoview': {
  5057. $val += 1 << 9;
  5058. break;
  5059. }
  5060. case 'lockedcontents': {
  5061. $val += 1 << 10;
  5062. break;
  5063. }
  5064. default: {
  5065. break;
  5066. }
  5067. }
  5068. }
  5069. }
  5070. $annots .= ' /F '.intval($val);
  5071. }
  5072. //$annots .= ' /AP ';
  5073. //$annots .= ' /AS ';
  5074. $annots .= ' /Border [';
  5075. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  5076. $annots .= intval($pl['opt']['border'][0]).' ';
  5077. $annots .= intval($pl['opt']['border'][1]).' ';
  5078. $annots .= intval($pl['opt']['border'][2]);
  5079. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  5080. $annots .= ' [';
  5081. foreach ($pl['opt']['border'][3] as $dash) {
  5082. $annots .= intval($dash).' ';
  5083. }
  5084. $annots .= ']';
  5085. }
  5086. } else {
  5087. $annots .= '0 0 0';
  5088. }
  5089. $annots .= ']';
  5090. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  5091. $annots .= ' /BS <<Type /Border';
  5092. if (isset($pl['opt']['bs']['w'])) {
  5093. $annots .= ' /W '.sprintf("%.4F", floatval($pl['opt']['bs']['w']));
  5094. }
  5095. $bstyles = array('S', 'D', 'B', 'I', 'U');
  5096. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  5097. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5098. }
  5099. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  5100. $annots .= ' /D [';
  5101. foreach ($pl['opt']['bs']['d'] as $cord) {
  5102. $cord = floatval($cord);
  5103. $annots .= sprintf(" %.4F", $cord);
  5104. }
  5105. $annots .= ']';
  5106. }
  5107. $annots .= '>> ';
  5108. }
  5109. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  5110. $annots .= ' /BE <<';
  5111. $bstyles = array('S', 'C');
  5112. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  5113. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5114. } else {
  5115. $annots .= ' /S /S';
  5116. }
  5117. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  5118. $annots .= ' /I '.sprintf(" %.4F", $pl['opt']['be']['i']);
  5119. }
  5120. $annots .= '>>';
  5121. }
  5122. $annots .= ' /C [';
  5123. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c']))) {
  5124. foreach ($pl['opt']['c'] as $col) {
  5125. $col = intval($col);
  5126. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  5127. $annots .= sprintf(" %.4F", $color);
  5128. }
  5129. }
  5130. $annots .= ']';
  5131. //$annots .= ' /StructParent ';
  5132. //$annots .= ' /OC ';
  5133. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  5134. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  5135. // this is a markup type
  5136. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  5137. $annots .= ' /T '.$this->_textstring($pl['opt']['t']);
  5138. }
  5139. //$annots .= ' /Popup ';
  5140. if (isset($pl['opt']['ca'])) {
  5141. $annots .= ' /CA '.sprintf("%.4F", floatval($pl['opt']['ca']));
  5142. }
  5143. if (isset($pl['opt']['rc'])) {
  5144. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5145. }
  5146. $annots .= ' /CreationDate '.$this->_datastring('D:'.date('YmdHis'));
  5147. //$annots .= ' /IRT ';
  5148. if (isset($pl['opt']['subj'])) {
  5149. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj']);
  5150. }
  5151. //$annots .= ' /RT ';
  5152. //$annots .= ' /IT ';
  5153. //$annots .= ' /ExData ';
  5154. }
  5155. switch (strtolower($pl['opt']['subtype'])) {
  5156. case 'text': {
  5157. if (isset($pl['opt']['open'])) {
  5158. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  5159. }
  5160. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  5161. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5162. $annots .= ' /Name /'.$pl['opt']['name'];
  5163. } else {
  5164. $annots .= ' /Name /Note';
  5165. }
  5166. $statemodels = array('Marked', 'Review');
  5167. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  5168. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5169. } else {
  5170. $pl['opt']['statemodel'] = 'Marked';
  5171. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5172. }
  5173. if ($pl['opt']['statemodel'] == 'Marked') {
  5174. $states = array('Accepted', 'Unmarked');
  5175. } else {
  5176. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  5177. }
  5178. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  5179. $annots .= ' /State /'.$pl['opt']['state'];
  5180. } else {
  5181. if ($pl['opt']['statemodel'] == 'Marked') {
  5182. $annots .= ' /State /Unmarked';
  5183. } else {
  5184. $annots .= ' /State /None';
  5185. }
  5186. }
  5187. break;
  5188. }
  5189. case 'link': {
  5190. if(is_string($pl['txt'])) {
  5191. // external URI link
  5192. $annots .= ' /A <</S /URI /URI '.$this->_datastring($pl['txt']).'>>';
  5193. } else {
  5194. // internal link
  5195. $l = $this->links[$pl['txt']];
  5196. $annots .= sprintf(' /Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $l[0])), ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  5197. }
  5198. $hmodes = array('N', 'I', 'O', 'P');
  5199. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  5200. $annots .= ' /H /'.$pl['opt']['h'];
  5201. } else {
  5202. $annots .= ' /H /I';
  5203. }
  5204. //$annots .= ' /PA ';
  5205. //$annots .= ' /Quadpoints ';
  5206. break;
  5207. }
  5208. case 'freetext': {
  5209. $annots .= ' /DA '.$this->_textstring($pl['txt']);
  5210. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  5211. $annots .= ' /Q '.intval($pl['opt']['q']);
  5212. }
  5213. if (isset($pl['opt']['rc'])) {
  5214. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5215. }
  5216. if (isset($pl['opt']['ds'])) {
  5217. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds']);
  5218. }
  5219. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  5220. $annots .= ' /CL [';
  5221. foreach ($pl['opt']['cl'] as $cl) {
  5222. $annots .= sprintf("%.4F ", $cl * $this->k);
  5223. }
  5224. $annots .= ']';
  5225. }
  5226. $tfit = array('FreeTextCallout', 'FreeTextTypeWriter');
  5227. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  5228. $annots .= ' /IT '.$pl['opt']['it'];
  5229. }
  5230. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  5231. $l = $pl['opt']['rd'][0] * $this->k;
  5232. $r = $pl['opt']['rd'][1] * $this->k;
  5233. $t = $pl['opt']['rd'][2] * $this->k;
  5234. $b = $pl['opt']['rd'][3] * $this->k;
  5235. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  5236. }
  5237. //$annots .= ' /LE ';
  5238. break;
  5239. }
  5240. // ... to be completed ...
  5241. case 'line': {
  5242. break;
  5243. }
  5244. case 'square': {
  5245. break;
  5246. }
  5247. case 'circle': {
  5248. break;
  5249. }
  5250. case 'polygon': {
  5251. break;
  5252. }
  5253. case 'polyline': {
  5254. break;
  5255. }
  5256. case 'highlight': {
  5257. break;
  5258. }
  5259. case 'underline': {
  5260. break;
  5261. }
  5262. case 'squiggly': {
  5263. break;
  5264. }
  5265. case 'strikeout': {
  5266. break;
  5267. }
  5268. case 'stamp': {
  5269. break;
  5270. }
  5271. case 'caret': {
  5272. break;
  5273. }
  5274. case 'ink': {
  5275. break;
  5276. }
  5277. case 'popup': {
  5278. break;
  5279. }
  5280. case 'fileattachment': {
  5281. if (!isset($pl['opt']['fs'])) {
  5282. break;
  5283. }
  5284. $filename = basename($pl['opt']['fs']);
  5285. if (isset($this->embeddedfiles[$filename]['n'])) {
  5286. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  5287. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  5288. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5289. $annots .= ' /Name /'.$pl['opt']['name'];
  5290. } else {
  5291. $annots .= ' /Name /PushPin';
  5292. }
  5293. }
  5294. break;
  5295. }
  5296. case 'sound': {
  5297. if (!isset($pl['opt']['sound'])) {
  5298. break;
  5299. }
  5300. $filename = basename($pl['opt']['sound']);
  5301. if (isset($this->embeddedfiles[$filename]['n'])) {
  5302. // ... TO BE COMPLETED ...
  5303. $iconsapp = array('Speaker', 'Mic');
  5304. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5305. $annots .= ' /Name /'.$pl['opt']['name'];
  5306. } else {
  5307. $annots .= ' /Name /Speaker';
  5308. }
  5309. }
  5310. break;
  5311. }
  5312. case 'movie': {
  5313. break;
  5314. }
  5315. case 'widget': {
  5316. if (isset($pl['opt']['h'])) {
  5317. $annots .= ' /H '.intval($pl['opt']['h']);
  5318. }
  5319. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk']))) {
  5320. $annots .= ' /MK <<';
  5321. // ... TO BE COMPLETED ...
  5322. $annots .= '>>';
  5323. }
  5324. break;
  5325. }
  5326. case 'screen': {
  5327. break;
  5328. }
  5329. case 'printermark': {
  5330. break;
  5331. }
  5332. case 'trapnet': {
  5333. break;
  5334. }
  5335. case 'watermark': {
  5336. break;
  5337. }
  5338. case '3d': {
  5339. break;
  5340. }
  5341. default: {
  5342. break;
  5343. }
  5344. }
  5345. $annots .= '>>';
  5346. }
  5347. $annots .= "\n]";
  5348. $this->_out($annots);
  5349. }
  5350. }
  5351. /**
  5352. * Output fonts.
  5353. * @access protected
  5354. */
  5355. protected function _putfonts() {
  5356. $nf = $this->n;
  5357. foreach ($this->diffs as $diff) {
  5358. //Encodings
  5359. $this->_newobj();
  5360. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  5361. $this->_out('endobj');
  5362. }
  5363. $mqr = get_magic_quotes_runtime();
  5364. set_magic_quotes_runtime(0);
  5365. foreach ($this->FontFiles as $file => $info) {
  5366. // search and get font file to embedd
  5367. $fontdir = $info['fontdir'];
  5368. $file = strtolower($file);
  5369. $fontfile = '';
  5370. // search files on various directories
  5371. if (file_exists($fontdir.$file)) {
  5372. $fontfile = $fontdir.$file;
  5373. } elseif (file_exists($this->_getfontpath().$file)) {
  5374. $fontfile = $this->_getfontpath().$file;
  5375. } elseif (file_exists($file)) {
  5376. $fontfile = $file;
  5377. }
  5378. if (!$this->empty_string($fontfile)) {
  5379. $font = file_get_contents($fontfile);
  5380. $compressed = (substr($file, -2) == '.z');
  5381. if ((!$compressed) AND (isset($info['length2']))) {
  5382. $header = (ord($font{0}) == 128);
  5383. if ($header) {
  5384. //Strip first binary header
  5385. $font = substr($font, 6);
  5386. }
  5387. if ($header AND (ord($font{$info['length1']}) == 128)) {
  5388. //Strip second binary header
  5389. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  5390. }
  5391. }
  5392. $this->_newobj();
  5393. $this->FontFiles[$file]['n'] = $this->n;
  5394. $this->_out('<</Length '.strlen($font));
  5395. if ($compressed) {
  5396. $this->_out('/Filter /FlateDecode');
  5397. }
  5398. $this->_out('/Length1 '.$info['length1']);
  5399. if (isset($info['length2'])) {
  5400. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  5401. }
  5402. $this->_out('>>');
  5403. $this->_putstream($font);
  5404. $this->_out('endobj');
  5405. }
  5406. }
  5407. set_magic_quotes_runtime($mqr);
  5408. foreach ($this->fontkeys as $k) {
  5409. //Font objects
  5410. $this->setFontSubBuffer($k, 'n', $this->n + 1);
  5411. $font = $this->getFontBuffer($k);
  5412. $type = $font['type'];
  5413. $name = $font['name'];
  5414. if ($type == 'core') {
  5415. //Standard font
  5416. $this->_newobj();
  5417. $this->_out('<</Type /Font');
  5418. $this->_out('/BaseFont /'.$name);
  5419. $this->_out('/Subtype /Type1');
  5420. if (($name != 'symbol') AND ($name != 'zapfdingbats')) {
  5421. $this->_out('/Encoding /WinAnsiEncoding');
  5422. }
  5423. $this->_out('>>');
  5424. $this->_out('endobj');
  5425. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  5426. //Additional Type1 or TrueType font
  5427. $this->_newobj();
  5428. $this->_out('<</Type /Font');
  5429. $this->_out('/BaseFont /'.$name);
  5430. $this->_out('/Subtype /'.$type);
  5431. $this->_out('/FirstChar 32 /LastChar 255');
  5432. $this->_out('/Widths '.($this->n + 1).' 0 R');
  5433. $this->_out('/FontDescriptor '.($this->n + 2).' 0 R');
  5434. if ($font['enc']) {
  5435. if (isset($font['diff'])) {
  5436. $this->_out('/Encoding '.($nf + $font['diff']).' 0 R');
  5437. } else {
  5438. $this->_out('/Encoding /WinAnsiEncoding');
  5439. }
  5440. }
  5441. $this->_out('>>');
  5442. $this->_out('endobj');
  5443. // Widths
  5444. $this->_newobj();
  5445. $cw = &$font['cw'];
  5446. $s = '[';
  5447. for ($i = 32; $i < 256; ++$i) {
  5448. $s .= $cw[$i].' ';
  5449. }
  5450. $this->_out($s.']');
  5451. $this->_out('endobj');
  5452. //Descriptor
  5453. $this->_newobj();
  5454. $s = '<</Type /FontDescriptor /FontName /'.$name;
  5455. foreach ($font['desc'] as $k => $v) {
  5456. $s .= ' /'.$k.' '.$v.'';
  5457. }
  5458. if (!$this->empty_string($font['file'])) {
  5459. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  5460. }
  5461. $this->_out($s.'>>');
  5462. $this->_out('endobj');
  5463. } else {
  5464. //Allow for additional types
  5465. $mtd = '_put'.strtolower($type);
  5466. if (!method_exists($this, $mtd)) {
  5467. $this->Error('Unsupported font type: '.$type);
  5468. }
  5469. $this->$mtd($font);
  5470. }
  5471. }
  5472. }
  5473. /**
  5474. * Outputs font widths
  5475. * @parameter array $font font data
  5476. * @parameter int $cidoffset offset for CID values
  5477. * @author Nicola Asuni
  5478. * @access protected
  5479. * @since 4.4.000 (2008-12-07)
  5480. */
  5481. protected function _putfontwidths($font, $cidoffset=0) {
  5482. ksort($font['cw']);
  5483. $rangeid = 0;
  5484. $range = array();
  5485. $prevcid = -2;
  5486. $prevwidth = -1;
  5487. $interval = false;
  5488. // for each character
  5489. foreach ($font['cw'] as $cid => $width) {
  5490. $cid -= $cidoffset;
  5491. if ($width != $font['dw']) {
  5492. if ($cid == ($prevcid + 1)) {
  5493. // consecutive CID
  5494. if ($width == $prevwidth) {
  5495. if ($width == $range[$rangeid][0]) {
  5496. $range[$rangeid][] = $width;
  5497. } else {
  5498. array_pop($range[$rangeid]);
  5499. // new range
  5500. $rangeid = $prevcid;
  5501. $range[$rangeid] = array();
  5502. $range[$rangeid][] = $prevwidth;
  5503. $range[$rangeid][] = $width;
  5504. }
  5505. $interval = true;
  5506. $range[$rangeid]['interval'] = true;
  5507. } else {
  5508. if ($interval) {
  5509. // new range
  5510. $rangeid = $cid;
  5511. $range[$rangeid] = array();
  5512. $range[$rangeid][] = $width;
  5513. } else {
  5514. $range[$rangeid][] = $width;
  5515. }
  5516. $interval = false;
  5517. }
  5518. } else {
  5519. // new range
  5520. $rangeid = $cid;
  5521. $range[$rangeid] = array();
  5522. $range[$rangeid][] = $width;
  5523. $interval = false;
  5524. }
  5525. $prevcid = $cid;
  5526. $prevwidth = $width;
  5527. }
  5528. }
  5529. // optimize ranges
  5530. $prevk = -1;
  5531. $nextk = -1;
  5532. $prevint = false;
  5533. foreach ($range as $k => $ws) {
  5534. $cws = count($ws);
  5535. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  5536. if (isset($range[$k]['interval'])) {
  5537. unset($range[$k]['interval']);
  5538. }
  5539. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  5540. unset($range[$k]);
  5541. } else {
  5542. $prevk = $k;
  5543. }
  5544. $nextk = $k + $cws;
  5545. if (isset($ws['interval'])) {
  5546. if ($cws > 3) {
  5547. $prevint = true;
  5548. } else {
  5549. $prevint = false;
  5550. }
  5551. unset($range[$k]['interval']);
  5552. --$nextk;
  5553. } else {
  5554. $prevint = false;
  5555. }
  5556. }
  5557. // output data
  5558. $w = '';
  5559. foreach ($range as $k => $ws) {
  5560. if (count(array_count_values($ws)) == 1) {
  5561. // interval mode is more compact
  5562. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  5563. } else {
  5564. // range mode
  5565. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  5566. }
  5567. }
  5568. $this->_out('/W ['.$w.' ]');
  5569. }
  5570. /**
  5571. * Adds unicode fonts.<br>
  5572. * Based on PDF Reference 1.3 (section 5)
  5573. * @parameter array $font font data
  5574. * @access protected
  5575. * @author Nicola Asuni
  5576. * @since 1.52.0.TC005 (2005-01-05)
  5577. */
  5578. protected function _puttruetypeunicode($font) {
  5579. // Type0 Font
  5580. // A composite font composed of other fonts, organized hierarchically
  5581. $this->_newobj();
  5582. $this->_out('<</Type /Font');
  5583. $this->_out('/Subtype /Type0');
  5584. $this->_out('/BaseFont /'.$font['name'].'');
  5585. $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.
  5586. $this->_out('/ToUnicode /Identity-H');
  5587. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  5588. $this->_out('>>');
  5589. $this->_out('endobj');
  5590. // CIDFontType2
  5591. // A CIDFont whose glyph descriptions are based on TrueType font technology
  5592. $this->_newobj();
  5593. $this->_out('<</Type /Font');
  5594. $this->_out('/Subtype /CIDFontType2');
  5595. $this->_out('/BaseFont /'.$font['name'].'');
  5596. // A dictionary containing entries that define the character collection of the CIDFont.
  5597. $cidinfo = '/Registry '.$this->_datastring('Adobe');
  5598. $cidinfo .= ' /Ordering '.$this->_datastring('Identity');
  5599. $cidinfo .= ' /Supplement 0';
  5600. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  5601. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  5602. $this->_out('/DW '.$font['dw'].''); // default width
  5603. $this->_putfontwidths($font, 0);
  5604. $this->_out('/CIDToGIDMap '.($this->n + 2).' 0 R');
  5605. $this->_out('>>');
  5606. $this->_out('endobj');
  5607. // Font descriptor
  5608. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  5609. $this->_newobj();
  5610. $this->_out('<</Type /FontDescriptor');
  5611. $this->_out('/FontName /'.$font['name']);
  5612. foreach ($font['desc'] as $key => $value) {
  5613. $this->_out('/'.$key.' '.$value);
  5614. }
  5615. $fontdir = '';
  5616. if (!$this->empty_string($font['file'])) {
  5617. // A stream containing a TrueType font
  5618. $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
  5619. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  5620. }
  5621. $this->_out('>>');
  5622. $this->_out('endobj');
  5623. $this->_newobj();
  5624. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  5625. // Embed CIDToGIDMap
  5626. // A specification of the mapping from CIDs to glyph indices
  5627. // search and get CTG font file to embedd
  5628. $ctgfile = strtolower($font['ctg']);
  5629. // search and get ctg font file to embedd
  5630. $fontfile = '';
  5631. // search files on various directories
  5632. if (file_exists($fontdir.$ctgfile)) {
  5633. $fontfile = $fontdir.$ctgfile;
  5634. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  5635. $fontfile = $this->_getfontpath().$ctgfile;
  5636. } elseif (file_exists($ctgfile)) {
  5637. $fontfile = $ctgfile;
  5638. }
  5639. if ($this->empty_string($fontfile)) {
  5640. $this->Error('Font file not found: '.$ctgfile);
  5641. }
  5642. $size = filesize($fontfile);
  5643. $this->_out('<</Length '.$size.'');
  5644. if (substr($fontfile, -2) == '.z') { // check file extension
  5645. // Decompresses data encoded using the public-domain
  5646. // zlib/deflate compression method, reproducing the
  5647. // original text or binary data
  5648. $this->_out('/Filter /FlateDecode');
  5649. }
  5650. $this->_out('>>');
  5651. $this->_putstream(file_get_contents($fontfile));
  5652. }
  5653. $this->_out('endobj');
  5654. }
  5655. /**
  5656. * Output CID-0 fonts.
  5657. * @param array $font font data
  5658. * @access protected
  5659. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  5660. * @since 3.2.000 (2008-06-23)
  5661. */
  5662. protected function _putcidfont0($font) {
  5663. $cidoffset = 31;
  5664. if (isset($font['cidinfo']['uni2cid'])) {
  5665. // convert unicode to cid.
  5666. $uni2cid = $font['cidinfo']['uni2cid'];
  5667. $cw = array();
  5668. foreach ($font['cw'] as $uni => $width) {
  5669. if (isset($uni2cid[$uni])) {
  5670. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  5671. } elseif ($uni < 256) {
  5672. $cw[$uni] = $width;
  5673. } // else unknown character
  5674. }
  5675. $font = array_merge($font, array('cw' => $cw));
  5676. }
  5677. $name = $font['name'];
  5678. $enc = $font['enc'];
  5679. if ($enc) {
  5680. $longname = $name.'-'.$enc;
  5681. } else {
  5682. $longname = $name;
  5683. }
  5684. $this->_newobj();
  5685. $this->_out('<</Type /Font');
  5686. $this->_out('/BaseFont /'.$longname);
  5687. $this->_out('/Subtype /Type0');
  5688. if ($enc) {
  5689. $this->_out('/Encoding /'.$enc);
  5690. }
  5691. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  5692. $this->_out('>>');
  5693. $this->_out('endobj');
  5694. $this->_newobj();
  5695. $this->_out('<</Type /Font');
  5696. $this->_out('/BaseFont /'.$name);
  5697. $this->_out('/Subtype /CIDFontType0');
  5698. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  5699. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  5700. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  5701. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  5702. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  5703. $this->_out('/DW '.$font['dw']);
  5704. $this->_putfontwidths($font, $cidoffset);
  5705. $this->_out('>>');
  5706. $this->_out('endobj');
  5707. $this->_newobj();
  5708. $s = '<</Type /FontDescriptor /FontName /'.$name;
  5709. foreach ($font['desc'] as $k => $v) {
  5710. if ($k != 'Style') {
  5711. $s .= ' /'.$k.' '.$v.'';
  5712. }
  5713. }
  5714. $this->_out($s.'>>');
  5715. $this->_out('endobj');
  5716. }
  5717. /**
  5718. * Output images.
  5719. * @access protected
  5720. */
  5721. protected function _putimages() {
  5722. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  5723. foreach ($this->imagekeys as $file) {
  5724. $info = $this->getImageBuffer($file);
  5725. $this->_newobj();
  5726. $this->setImageSubBuffer($file, 'n', $this->n);
  5727. $this->_out('<</Type /XObject');
  5728. $this->_out('/Subtype /Image');
  5729. $this->_out('/Width '.$info['w']);
  5730. $this->_out('/Height '.$info['h']);
  5731. if (isset($info['masked'])) {
  5732. $this->_out('/SMask '.($this->n - 1).' 0 R');
  5733. }
  5734. if ($info['cs'] == 'Indexed') {
  5735. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]');
  5736. } else {
  5737. $this->_out('/ColorSpace /'.$info['cs']);
  5738. if ($info['cs'] == 'DeviceCMYK') {
  5739. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  5740. }
  5741. }
  5742. $this->_out('/BitsPerComponent '.$info['bpc']);
  5743. if (isset($info['f'])) {
  5744. $this->_out('/Filter /'.$info['f']);
  5745. }
  5746. if (isset($info['parms'])) {
  5747. $this->_out($info['parms']);
  5748. }
  5749. if (isset($info['trns']) AND is_array($info['trns'])) {
  5750. $trns='';
  5751. $count_info = count($info['trns']);
  5752. for ($i=0; $i < $count_info; ++$i) {
  5753. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  5754. }
  5755. $this->_out('/Mask ['.$trns.']');
  5756. }
  5757. $this->_out('/Length '.strlen($info['data']).'>>');
  5758. $this->_putstream($info['data']);
  5759. $this->_out('endobj');
  5760. //Palette
  5761. if ($info['cs'] == 'Indexed') {
  5762. $this->_newobj();
  5763. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  5764. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  5765. $this->_putstream($pal);
  5766. $this->_out('endobj');
  5767. }
  5768. }
  5769. }
  5770. /**
  5771. * Output Spot Colors Resources.
  5772. * @access protected
  5773. * @since 4.0.024 (2008-09-12)
  5774. */
  5775. protected function _putspotcolors() {
  5776. foreach ($this->spot_colors as $name => $color) {
  5777. $this->_newobj();
  5778. $this->spot_colors[$name]['n'] = $this->n;
  5779. $this->_out('[/Separation /'.str_replace(' ', '#20', $name));
  5780. $this->_out('/DeviceCMYK <<');
  5781. $this->_out('/Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] ');
  5782. $this->_out(sprintf('/C1 [%.4F %.4F %.4F %.4F] ', $color['c']/100, $color['m']/100, $color['y']/100, $color['k']/100));
  5783. $this->_out('/FunctionType 2 /Domain [0 1] /N 1>>]');
  5784. $this->_out('endobj');
  5785. }
  5786. }
  5787. /**
  5788. * Output object dictionary for images.
  5789. * @access protected
  5790. */
  5791. protected function _putxobjectdict() {
  5792. foreach ($this->imagekeys as $file) {
  5793. $info = $this->getImageBuffer($file);
  5794. $this->_out('/I'.$info['i'].' '.$info['n'].' 0 R');
  5795. }
  5796. }
  5797. /**
  5798. * Output Resources Dictionary.
  5799. * @access protected
  5800. */
  5801. protected function _putresourcedict() {
  5802. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  5803. $this->_out('/Font <<');
  5804. foreach ($this->fontkeys as $fontkey) {
  5805. $font = $this->getFontBuffer($fontkey);
  5806. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  5807. }
  5808. $this->_out('>>');
  5809. $this->_out('/XObject <<');
  5810. $this->_putxobjectdict();
  5811. $this->_out('>>');
  5812. // visibility
  5813. $this->_out('/Properties <</OC1 '.$this->n_ocg_print.' 0 R /OC2 '.$this->n_ocg_view.' 0 R>>');
  5814. // transparency
  5815. $this->_out('/ExtGState <<');
  5816. foreach ($this->extgstates as $k => $extgstate) {
  5817. $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R');
  5818. }
  5819. $this->_out('>>');
  5820. // gradients
  5821. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  5822. $this->_out('/Shading <<');
  5823. foreach ($this->gradients as $id => $grad) {
  5824. $this->_out('/Sh'.$id.' '.$grad['id'].' 0 R');
  5825. }
  5826. $this->_out('>>');
  5827. }
  5828. // spot colors
  5829. if (isset($this->spot_colors) AND (count($this->spot_colors) > 0)) {
  5830. $this->_out('/ColorSpace <<');
  5831. foreach ($this->spot_colors as $color) {
  5832. $this->_out('/CS'.$color['i'].' '.$color['n'].' 0 R');
  5833. }
  5834. $this->_out('>>');
  5835. }
  5836. }
  5837. /**
  5838. * Output Resources.
  5839. * @access protected
  5840. */
  5841. protected function _putresources() {
  5842. $this->_putextgstates();
  5843. $this->_putocg();
  5844. $this->_putfonts();
  5845. $this->_putimages();
  5846. $this->_putspotcolors();
  5847. $this->_putshaders();
  5848. //Resource dictionary
  5849. $this->offsets[2] = $this->bufferlen;
  5850. $this->_out('2 0 obj');
  5851. $this->_out('<<');
  5852. $this->_putresourcedict();
  5853. $this->_out('>>');
  5854. $this->_out('endobj');
  5855. $this->_putjavascript();
  5856. $this->_putbookmarks();
  5857. $this->_putEmbeddedFiles();
  5858. // encryption
  5859. if ($this->encrypted) {
  5860. $this->_newobj();
  5861. $this->enc_obj_id = $this->n;
  5862. $this->_out('<<');
  5863. $this->_putencryption();
  5864. $this->_out('>>');
  5865. $this->_out('endobj');
  5866. }
  5867. }
  5868. /**
  5869. * Adds some Metadata information
  5870. * (see Chapter 10.2 of PDF Reference)
  5871. * @access protected
  5872. */
  5873. protected function _putinfo() {
  5874. if (!$this->empty_string($this->title)) {
  5875. $this->_out('/Title '.$this->_textstring($this->title));
  5876. }
  5877. if (!$this->empty_string($this->author)) {
  5878. $this->_out('/Author '.$this->_textstring($this->author));
  5879. }
  5880. if (!$this->empty_string($this->subject)) {
  5881. $this->_out('/Subject '.$this->_textstring($this->subject));
  5882. }
  5883. if (!$this->empty_string($this->keywords)) {
  5884. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  5885. }
  5886. if (!$this->empty_string($this->creator)) {
  5887. $this->_out('/Creator '.$this->_textstring($this->creator));
  5888. }
  5889. if (defined('PDF_PRODUCER')) {
  5890. $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
  5891. }
  5892. $this->_out('/CreationDate '.$this->_datastring('D:'.date('YmdHis')));
  5893. $this->_out('/ModDate '.$this->_datastring('D:'.date('YmdHis')));
  5894. }
  5895. /**
  5896. * Output Catalog.
  5897. * @access protected
  5898. */
  5899. protected function _putcatalog() {
  5900. $this->_out('/Type /Catalog');
  5901. $this->_out('/Pages 1 0 R');
  5902. if ($this->ZoomMode == 'fullpage') {
  5903. $this->_out('/OpenAction [3 0 R /Fit]');
  5904. } elseif ($this->ZoomMode == 'fullwidth') {
  5905. $this->_out('/OpenAction [3 0 R /FitH null]');
  5906. } elseif ($this->ZoomMode == 'real') {
  5907. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  5908. } elseif (!is_string($this->ZoomMode)) {
  5909. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode / 100).']');
  5910. }
  5911. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  5912. $this->_out('/PageLayout /'.$this->LayoutMode.'');
  5913. }
  5914. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  5915. $this->_out('/PageMode /'.$this->PageMode);
  5916. }
  5917. if (isset($this->l['a_meta_language'])) {
  5918. $this->_out('/Lang /'.$this->l['a_meta_language']);
  5919. }
  5920. $this->_out('/Names <<');
  5921. if (!$this->empty_string($this->javascript)) {
  5922. $this->_out('/JavaScript '.($this->n_js).' 0 R');
  5923. }
  5924. $this->_out('>>');
  5925. if (count($this->outlines) > 0) {
  5926. $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
  5927. $this->_out('/PageMode /UseOutlines');
  5928. }
  5929. $this->_putviewerpreferences();
  5930. $p = $this->n_ocg_print.' 0 R';
  5931. $v = $this->n_ocg_view.' 0 R';
  5932. $as = '<</Event /Print /OCGs ['.$p.' '.$v.'] /Category [/Print]>> <</Event /View /OCGs ['.$p.' '.$v.'] /Category [/View]>>';
  5933. $this->_out('/OCProperties <</OCGs ['.$p.' '.$v.'] /D <</ON ['.$p.'] /OFF ['.$v.'] /AS ['.$as.']>>>>');
  5934. }
  5935. /**
  5936. * Output viewer preferences.
  5937. * @author Nicola asuni
  5938. * @since 3.1.000 (2008-06-09)
  5939. * @access protected
  5940. */
  5941. protected function _putviewerpreferences() {
  5942. $this->_out('/ViewerPreferences<<');
  5943. if ($this->rtl) {
  5944. $this->_out('/Direction /R2L');
  5945. } else {
  5946. $this->_out('/Direction /L2R');
  5947. }
  5948. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  5949. $this->_out('/HideToolbar true');
  5950. }
  5951. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  5952. $this->_out('/HideMenubar true');
  5953. }
  5954. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  5955. $this->_out('/HideWindowUI true');
  5956. }
  5957. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  5958. $this->_out('/FitWindow true');
  5959. }
  5960. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  5961. $this->_out('/CenterWindow true');
  5962. }
  5963. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  5964. $this->_out('/DisplayDocTitle true');
  5965. }
  5966. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  5967. $this->_out('/NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'].'');
  5968. }
  5969. if (isset($this->viewer_preferences['ViewArea'])) {
  5970. $this->_out('/ViewArea /'.$this->viewer_preferences['ViewArea']);
  5971. }
  5972. if (isset($this->viewer_preferences['ViewClip'])) {
  5973. $this->_out('/ViewClip /'.$this->viewer_preferences['ViewClip']);
  5974. }
  5975. if (isset($this->viewer_preferences['PrintArea'])) {
  5976. $this->_out('/PrintArea /'.$this->viewer_preferences['PrintArea']);
  5977. }
  5978. if (isset($this->viewer_preferences['PrintClip'])) {
  5979. $this->_out('/PrintClip /'.$this->viewer_preferences['PrintClip']);
  5980. }
  5981. if (isset($this->viewer_preferences['PrintScaling'])) {
  5982. $this->_out('/PrintScaling /'.$this->viewer_preferences['PrintScaling']);
  5983. }
  5984. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  5985. $this->_out('/Duplex /'.$this->viewer_preferences['Duplex']);
  5986. }
  5987. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  5988. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  5989. $this->_out('/PickTrayByPDFSize true');
  5990. } else {
  5991. $this->_out('/PickTrayByPDFSize false');
  5992. }
  5993. }
  5994. if (isset($this->viewer_preferences['PrintPageRange'])) {
  5995. $PrintPageRangeNum = '';
  5996. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  5997. $PrintPageRangeNum .= ' '.($v - 1).'';
  5998. }
  5999. $this->_out('/PrintPageRange ['.substr($PrintPageRangeNum,1).']');
  6000. }
  6001. if (isset($this->viewer_preferences['NumCopies'])) {
  6002. $this->_out('/NumCopies '.intval($this->viewer_preferences['NumCopies']));
  6003. }
  6004. $this->_out('>>');
  6005. }
  6006. /**
  6007. * Output trailer.
  6008. * @access protected
  6009. */
  6010. protected function _puttrailer() {
  6011. $this->_out('/Size '.($this->n + 1));
  6012. $this->_out('/Root '.$this->n.' 0 R');
  6013. $this->_out('/Info '.($this->n - 1).' 0 R');
  6014. if ($this->encrypted) {
  6015. $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
  6016. $this->_out('/ID [()()]');
  6017. }
  6018. }
  6019. /**
  6020. * Output PDF header.
  6021. * @access protected
  6022. */
  6023. protected function _putheader() {
  6024. $this->_out('%PDF-'.$this->PDFVersion);
  6025. }
  6026. /**
  6027. * Output end of document (EOF).
  6028. * @access protected
  6029. */
  6030. protected function _enddoc() {
  6031. $this->state = 1;
  6032. $this->_putheader();
  6033. $this->_putpages();
  6034. $this->_putresources();
  6035. //Info
  6036. $this->_newobj();
  6037. $this->_out('<<');
  6038. $this->_putinfo();
  6039. $this->_out('>>');
  6040. $this->_out('endobj');
  6041. //Catalog
  6042. $this->_newobj();
  6043. $this->_out('<<');
  6044. $this->_putcatalog();
  6045. $this->_putcertification();
  6046. $this->_putuserrights();
  6047. $this->_out('>>');
  6048. $this->_out('endobj');
  6049. //Cross-ref
  6050. $o = $this->bufferlen;
  6051. $this->_out('xref');
  6052. $this->_out('0 '.($this->n + 1));
  6053. $this->_out('0000000000 65535 f ');
  6054. for ($i=1; $i <= $this->n; ++$i) {
  6055. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6056. }
  6057. if (isset($this->embeddedfiles) AND count($this->embeddedfiles) > 0) {
  6058. $this->_out('100000 '.count($this->embeddedfiles));
  6059. foreach ($this->embeddedfiles as $filename => $filedata) {
  6060. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$filedata['n']]));
  6061. }
  6062. }
  6063. //Trailer
  6064. $this->_out('trailer');
  6065. $this->_out('<<');
  6066. $this->_puttrailer();
  6067. $this->_out('>>');
  6068. $this->_out('startxref');
  6069. $this->_out($o);
  6070. $this->_out('%%EOF');
  6071. $this->state = 3; // end-of-doc
  6072. if ($this->diskcache) {
  6073. // remove temporary files used for images
  6074. foreach ($this->imagekeys as $key) {
  6075. // remove temporary files
  6076. unlink($this->images[$key]);
  6077. }
  6078. foreach ($this->fontkeys as $key) {
  6079. // remove temporary files
  6080. unlink($this->fonts[$key]);
  6081. }
  6082. }
  6083. }
  6084. /**
  6085. * Initialize a new page.
  6086. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  6087. * @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>
  6088. * @access protected
  6089. */
  6090. protected function _beginpage($orientation='', $format='') {
  6091. ++$this->page;
  6092. $this->setPageBuffer($this->page, '');
  6093. // initialize array for graphics tranformation positions inside a page buffer
  6094. $this->transfmrk[$this->page] = array();
  6095. $this->state = 2;
  6096. if ($this->empty_string($orientation)) {
  6097. if (isset($this->CurOrientation)) {
  6098. $orientation = $this->CurOrientation;
  6099. } else {
  6100. $orientation = 'P';
  6101. }
  6102. }
  6103. if ($this->empty_string($format)) {
  6104. $this->setPageOrientation($orientation);
  6105. } else {
  6106. $this->setPageFormat($format, $orientation);
  6107. }
  6108. if ($this->rtl) {
  6109. $this->x = $this->w - $this->rMargin;
  6110. } else {
  6111. $this->x = $this->lMargin;
  6112. }
  6113. $this->y = $this->tMargin;
  6114. if (isset($this->newpagegroup[$this->page])) {
  6115. // start a new group
  6116. $n = sizeof($this->pagegroups) + 1;
  6117. $alias = '{nb'.$n.'}';
  6118. $this->pagegroups[$alias] = 1;
  6119. $this->currpagegroup = $alias;
  6120. } elseif ($this->currpagegroup) {
  6121. ++$this->pagegroups[$this->currpagegroup];
  6122. }
  6123. }
  6124. /**
  6125. * Mark end of page.
  6126. * @access protected
  6127. */
  6128. protected function _endpage() {
  6129. $this->setVisibility('all');
  6130. $this->state = 1;
  6131. }
  6132. /**
  6133. * Begin a new object.
  6134. * @access protected
  6135. */
  6136. protected function _newobj() {
  6137. ++$this->n;
  6138. $this->offsets[$this->n] = $this->bufferlen;
  6139. $this->_out($this->n.' 0 obj');
  6140. }
  6141. /**
  6142. * Underline text.
  6143. * @param int $x X coordinate
  6144. * @param int $y Y coordinate
  6145. * @param string $txt text to underline
  6146. * @access protected
  6147. */
  6148. protected function _dounderline($x, $y, $txt) {
  6149. $up = $this->CurrentFont['up'];
  6150. $ut = $this->CurrentFont['ut'];
  6151. $w = $this->GetStringWidth($txt);
  6152. 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);
  6153. }
  6154. /**
  6155. * Line through text.
  6156. * @param int $x X coordinate
  6157. * @param int $y Y coordinate
  6158. * @param string $txt text to linethrough
  6159. * @access protected
  6160. */
  6161. protected function _dolinethrough($x, $y, $txt) {
  6162. $up = $this->CurrentFont['up'];
  6163. $ut = $this->CurrentFont['ut'];
  6164. $w = $this->GetStringWidth($txt);
  6165. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - ($y - ($this->FontSize/2) - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt);
  6166. }
  6167. /**
  6168. * Read a 4-byte integer from file.
  6169. * @param string $f file name.
  6170. * @return 4-byte integer
  6171. * @access protected
  6172. */
  6173. protected function _freadint($f) {
  6174. $a = unpack('Ni', fread($f, 4));
  6175. return $a['i'];
  6176. }
  6177. /**
  6178. * Add "\" before "\", "(" and ")"
  6179. * @param string $s string to escape.
  6180. * @return string escaped string.
  6181. * @access protected
  6182. */
  6183. protected function _escape($s) {
  6184. // the chr(13) substitution fixes the Bugs item #1421290.
  6185. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  6186. }
  6187. /**
  6188. * Format a date string for meta information
  6189. * @param string $s date string to escape.
  6190. * @return string escaped string.
  6191. * @access protected
  6192. */
  6193. protected function _datastring($s) {
  6194. if ($this->encrypted) {
  6195. $s = $this->_RC4($this->_objectkey($this->n), $s);
  6196. }
  6197. return '('. $this->_escape($s).')';
  6198. }
  6199. /**
  6200. * Format a text string for meta information
  6201. * @param string $s string to escape.
  6202. * @return string escaped string.
  6203. * @access protected
  6204. */
  6205. protected function _textstring($s) {
  6206. if ($this->isunicode) {
  6207. //Convert string to UTF-16BE
  6208. $s = $this->UTF8ToUTF16BE($s, true);
  6209. }
  6210. return $this->_datastring($s);
  6211. }
  6212. /**
  6213. * Format a text string
  6214. * @param string $s string to escape.
  6215. * @return string escaped string.
  6216. * @access protected
  6217. */
  6218. protected function _escapetext($s) {
  6219. if ($this->isunicode) {
  6220. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  6221. $s = $this->UTF8ToLatin1($s);
  6222. } else {
  6223. //Convert string to UTF-16BE and reverse RTL language
  6224. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  6225. }
  6226. }
  6227. return $this->_escape($s);
  6228. }
  6229. /**
  6230. * Output a stream.
  6231. * @param string $s string to output.
  6232. * @access protected
  6233. */
  6234. protected function _putstream($s) {
  6235. if ($this->encrypted) {
  6236. $s = $this->_RC4($this->_objectkey($this->n), $s);
  6237. }
  6238. $this->_out('stream');
  6239. $this->_out($s);
  6240. $this->_out('endstream');
  6241. }
  6242. /**
  6243. * Output a string to the document.
  6244. * @param string $s string to output.
  6245. * @access protected
  6246. */
  6247. protected function _out($s) {
  6248. if ($this->state == 2) {
  6249. if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  6250. // puts data before page footer
  6251. $pagebuff = $this->getPageBuffer($this->page);
  6252. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  6253. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  6254. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  6255. // update footer position
  6256. $this->footerpos[$this->page] += strlen($s."\n");
  6257. } else {
  6258. $this->setPageBuffer($this->page, $s."\n", true);
  6259. }
  6260. } else {
  6261. $this->setBuffer($s."\n");
  6262. }
  6263. }
  6264. /**
  6265. * Converts UTF-8 strings to codepoints array.<br>
  6266. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  6267. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  6268. * <pre>
  6269. * Char. number range | UTF-8 octet sequence
  6270. * (hexadecimal) | (binary)
  6271. * --------------------+-----------------------------------------------
  6272. * 0000 0000-0000 007F | 0xxxxxxx
  6273. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  6274. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  6275. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6276. * ---------------------------------------------------------------------
  6277. *
  6278. * ABFN notation:
  6279. * ---------------------------------------------------------------------
  6280. * UTF8-octets = *( UTF8-char )
  6281. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  6282. * UTF8-1 = %x00-7F
  6283. * UTF8-2 = %xC2-DF UTF8-tail
  6284. *
  6285. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  6286. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  6287. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  6288. * %xF4 %x80-8F 2( UTF8-tail )
  6289. * UTF8-tail = %x80-BF
  6290. * ---------------------------------------------------------------------
  6291. * </pre>
  6292. * @param string $str string to process.
  6293. * @return array containing codepoints (UTF-8 characters values)
  6294. * @access protected
  6295. * @author Nicola Asuni
  6296. * @since 1.53.0.TC005 (2005-01-05)
  6297. */
  6298. protected function UTF8StringToArray($str) {
  6299. if (isset($this->cache_UTF8StringToArray['_'.$str])) {
  6300. // return cached value
  6301. return($this->cache_UTF8StringToArray['_'.$str]);
  6302. }
  6303. // check cache size
  6304. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  6305. // remove first element
  6306. array_shift($this->cache_UTF8StringToArray);
  6307. }
  6308. ++$this->cache_size_UTF8StringToArray;
  6309. if (!$this->isunicode) {
  6310. // split string into array of equivalent codes
  6311. $strarr = array();
  6312. $strlen = strlen($str);
  6313. for ($i=0; $i < $strlen; ++$i) {
  6314. $strarr[] = ord($str{$i});
  6315. }
  6316. // insert new value on cache
  6317. $this->cache_UTF8StringToArray['_'.$str] = $strarr;
  6318. return $strarr;
  6319. }
  6320. $unicode = array(); // array containing unicode values
  6321. $bytes = array(); // array containing single character byte sequences
  6322. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  6323. $str .= ''; // force $str to be a string
  6324. $length = strlen($str);
  6325. for ($i = 0; $i < $length; ++$i) {
  6326. $char = ord($str{$i}); // get one string character at time
  6327. if (count($bytes) == 0) { // get starting octect
  6328. if ($char <= 0x7F) {
  6329. $unicode[] = $char; // use the character "as is" because is ASCII
  6330. $numbytes = 1;
  6331. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  6332. $bytes[] = ($char - 0xC0) << 0x06;
  6333. $numbytes = 2;
  6334. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  6335. $bytes[] = ($char - 0xE0) << 0x0C;
  6336. $numbytes = 3;
  6337. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  6338. $bytes[] = ($char - 0xF0) << 0x12;
  6339. $numbytes = 4;
  6340. } else {
  6341. // use replacement character for other invalid sequences
  6342. $unicode[] = 0xFFFD;
  6343. $bytes = array();
  6344. $numbytes = 1;
  6345. }
  6346. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  6347. $bytes[] = $char - 0x80;
  6348. if (count($bytes) == $numbytes) {
  6349. // compose UTF-8 bytes to a single unicode value
  6350. $char = $bytes[0];
  6351. for ($j = 1; $j < $numbytes; ++$j) {
  6352. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  6353. }
  6354. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  6355. /* The definition of UTF-8 prohibits encoding character numbers between
  6356. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  6357. encoding form (as surrogate pairs) and do not directly represent
  6358. characters. */
  6359. $unicode[] = 0xFFFD; // use replacement character
  6360. } else {
  6361. $unicode[] = $char; // add char to array
  6362. }
  6363. // reset data for next char
  6364. $bytes = array();
  6365. $numbytes = 1;
  6366. }
  6367. } else {
  6368. // use replacement character for other invalid sequences
  6369. $unicode[] = 0xFFFD;
  6370. $bytes = array();
  6371. $numbytes = 1;
  6372. }
  6373. }
  6374. // insert new value on cache
  6375. $this->cache_UTF8StringToArray['_'.$str] = $unicode;
  6376. return $unicode;
  6377. }
  6378. /**
  6379. * Converts UTF-8 strings to UTF16-BE.<br>
  6380. * @param string $str string to process.
  6381. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  6382. * @return string
  6383. * @access protected
  6384. * @author Nicola Asuni
  6385. * @since 1.53.0.TC005 (2005-01-05)
  6386. * @uses UTF8StringToArray(), arrUTF8ToUTF16BE()
  6387. */
  6388. protected function UTF8ToUTF16BE($str, $setbom=true) {
  6389. if (!$this->isunicode) {
  6390. return $str; // string is not in unicode
  6391. }
  6392. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  6393. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  6394. }
  6395. /**
  6396. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  6397. * @param string $str string to process.
  6398. * @return string
  6399. * @author Andrew Whitehead, Nicola Asuni
  6400. * @access protected
  6401. * @since 3.2.000 (2008-06-23)
  6402. */
  6403. protected function UTF8ToLatin1($str) {
  6404. global $utf8tolatin;
  6405. if (!$this->isunicode) {
  6406. return $str; // string is not in unicode
  6407. }
  6408. $outstr = ''; // string to be returned
  6409. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  6410. foreach ($unicode as $char) {
  6411. if ($char < 256) {
  6412. $outstr .= chr($char);
  6413. } elseif (array_key_exists($char, $utf8tolatin)) {
  6414. // map from UTF-8
  6415. $outstr .= chr($utf8tolatin[$char]);
  6416. } elseif ($char == 0xFFFD) {
  6417. // skip
  6418. } else {
  6419. $outstr .= '?';
  6420. }
  6421. }
  6422. return $outstr;
  6423. }
  6424. /**
  6425. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  6426. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  6427. * <pre>
  6428. * Encoding UTF-16:
  6429. *
  6430. * Encoding of a single character from an ISO 10646 character value to
  6431. * UTF-16 proceeds as follows. Let U be the character number, no greater
  6432. * than 0x10FFFF.
  6433. *
  6434. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  6435. * terminate.
  6436. *
  6437. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  6438. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  6439. * represented in 20 bits.
  6440. *
  6441. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  6442. * 0xDC00, respectively. These integers each have 10 bits free to
  6443. * encode the character value, for a total of 20 bits.
  6444. *
  6445. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  6446. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  6447. * bits of W2. Terminate.
  6448. *
  6449. * Graphically, steps 2 through 4 look like:
  6450. * U' = yyyyyyyyyyxxxxxxxxxx
  6451. * W1 = 110110yyyyyyyyyy
  6452. * W2 = 110111xxxxxxxxxx
  6453. * </pre>
  6454. * @param array $unicode array containing UTF-8 unicode values
  6455. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  6456. * @return string
  6457. * @access protected
  6458. * @author Nicola Asuni
  6459. * @since 2.1.000 (2008-01-08)
  6460. * @see UTF8ToUTF16BE()
  6461. */
  6462. protected function arrUTF8ToUTF16BE($unicode, $setbom=true) {
  6463. $outstr = ''; // string to be returned
  6464. if ($setbom) {
  6465. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  6466. }
  6467. foreach ($unicode as $char) {
  6468. if ($char == 0xFFFD) {
  6469. $outstr .= "\xFF\xFD"; // replacement character
  6470. } elseif ($char < 0x10000) {
  6471. $outstr .= chr($char >> 0x08);
  6472. $outstr .= chr($char & 0xFF);
  6473. } else {
  6474. $char -= 0x10000;
  6475. $w1 = 0xD800 | ($char >> 0x10);
  6476. $w2 = 0xDC00 | ($char & 0x3FF);
  6477. $outstr .= chr($w1 >> 0x08);
  6478. $outstr .= chr($w1 & 0xFF);
  6479. $outstr .= chr($w2 >> 0x08);
  6480. $outstr .= chr($w2 & 0xFF);
  6481. }
  6482. }
  6483. return $outstr;
  6484. }
  6485. // ====================================================
  6486. /**
  6487. * Set header font.
  6488. * @param array $font font
  6489. * @access public
  6490. * @since 1.1
  6491. */
  6492. public function setHeaderFont($font) {
  6493. $this->header_font = $font;
  6494. }
  6495. /**
  6496. * Get header font.
  6497. * @return array()
  6498. * @access public
  6499. * @since 4.0.012 (2008-07-24)
  6500. */
  6501. public function getHeaderFont() {
  6502. return $this->header_font;
  6503. }
  6504. /**
  6505. * Set footer font.
  6506. * @param array $font font
  6507. * @access public
  6508. * @since 1.1
  6509. */
  6510. public function setFooterFont($font) {
  6511. $this->footer_font = $font;
  6512. }
  6513. /**
  6514. * Get Footer font.
  6515. * @return array()
  6516. * @access public
  6517. * @since 4.0.012 (2008-07-24)
  6518. */
  6519. public function getFooterFont() {
  6520. return $this->footer_font;
  6521. }
  6522. /**
  6523. * Set language array.
  6524. * @param array $language
  6525. * @access public
  6526. * @since 1.1
  6527. */
  6528. public function setLanguageArray($language) {
  6529. $this->l = $language;
  6530. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  6531. }
  6532. /**
  6533. * Returns the PDF data.
  6534. * @access public
  6535. */
  6536. public function getPDFData() {
  6537. if ($this->state < 3) {
  6538. $this->Close();
  6539. }
  6540. return $this->buffer;
  6541. }
  6542. /**
  6543. * Output anchor link.
  6544. * @param string $url link URL or internal link (i.e.: &lt;a href="#23"&gt;link to page 23&lt;/a&gt;)
  6545. * @param string $name link name
  6546. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  6547. * @param boolean $firstline if true prints only the first line and return the remaining string.
  6548. * @param array $color array of RGB text color
  6549. * @param string $style font style (U, D, B, I)
  6550. * @return the number of cells used or the remaining text if $firstline = true;
  6551. * @access public
  6552. */
  6553. public function addHtmlLink($url, $name, $fill=0, $firstline=false, $color='', $style=-1) {
  6554. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  6555. // convert url to internal link
  6556. $page = intval(substr($url, 1));
  6557. $url = $this->AddLink();
  6558. $this->SetLink($url, 0, $page);
  6559. }
  6560. // store current settings
  6561. $prevcolor = $this->fgcolor;
  6562. $prevstyle = $this->FontStyle;
  6563. if (empty($color)) {
  6564. $this->SetTextColorArray($this->htmlLinkColorArray);
  6565. } else {
  6566. $this->SetTextColorArray($color);
  6567. }
  6568. if ($style == -1) {
  6569. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  6570. } else {
  6571. $this->SetFont('', $this->FontStyle.$style);
  6572. }
  6573. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline);
  6574. // restore settings
  6575. $this->SetFont('', $prevstyle);
  6576. $this->SetTextColorArray($prevcolor);
  6577. return $ret;
  6578. }
  6579. /**
  6580. * Returns an associative array (keys: R,G,B) from an html color name or a six-digit or three-digit hexadecimal color representation (i.e. #3FE5AA or #7FF).
  6581. * @param string $color html color
  6582. * @return array RGB color or false in case of error.
  6583. * @access public
  6584. */
  6585. public function convertHTMLColorToDec($color='#FFFFFF') {
  6586. global $webcolor;
  6587. $returncolor = false;
  6588. $color = preg_replace('/[\s]*/', '', $color); // remove extra spaces
  6589. $color = strtolower($color);
  6590. if (strlen($color) == 0) {
  6591. return false;
  6592. }
  6593. if (substr($color, 0, 3) == 'rgb') {
  6594. $codes = substr($color, 4);
  6595. $codes = str_replace(')', '', $codes);
  6596. $returncolor = explode(',', $codes, 3);
  6597. return $returncolor;
  6598. }
  6599. if (substr($color, 0, 1) != '#') {
  6600. // decode color name
  6601. if (isset($webcolor[$color])) {
  6602. $color_code = $webcolor[$color];
  6603. } else {
  6604. return false;
  6605. }
  6606. } else {
  6607. $color_code = substr($color, 1);
  6608. }
  6609. switch (strlen($color_code)) {
  6610. case 3: {
  6611. // three-digit hexadecimal representation
  6612. $r = substr($color_code, 0, 1);
  6613. $g = substr($color_code, 1, 1);
  6614. $b = substr($color_code, 2, 1);
  6615. $returncolor['R'] = hexdec($r.$r);
  6616. $returncolor['G'] = hexdec($g.$g);
  6617. $returncolor['B'] = hexdec($b.$b);
  6618. break;
  6619. }
  6620. case 6: {
  6621. // six-digit hexadecimal representation
  6622. $returncolor['R'] = hexdec(substr($color_code, 0, 2));
  6623. $returncolor['G'] = hexdec(substr($color_code, 2, 2));
  6624. $returncolor['B'] = hexdec(substr($color_code, 4, 2));
  6625. break;
  6626. }
  6627. }
  6628. return $returncolor;
  6629. }
  6630. /**
  6631. * Converts pixels to User's Units.
  6632. * @param int $px pixels
  6633. * @return float value in user's unit
  6634. * @access public
  6635. * @see setImageScale(), getImageScale()
  6636. */
  6637. public function pixelsToUnits($px) {
  6638. return ($px / ($this->imgscale * $this->k));
  6639. }
  6640. /**
  6641. * Reverse function for htmlentities.
  6642. * Convert entities in UTF-8.
  6643. * @param $text_to_convert Text to convert.
  6644. * @return string converted
  6645. * @access public
  6646. */
  6647. public function unhtmlentities($text_to_convert) {
  6648. return html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  6649. }
  6650. // ENCRYPTION METHODS ----------------------------------
  6651. // SINCE 2.0.000 (2008-01-02)
  6652. /**
  6653. * Compute encryption key depending on object number where the encrypted data is stored
  6654. * @param int $n object number
  6655. * @access protected
  6656. * @since 2.0.000 (2008-01-02)
  6657. */
  6658. protected function _objectkey($n) {
  6659. return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10);
  6660. }
  6661. /**
  6662. * Put encryption on PDF document.
  6663. * @access protected
  6664. * @since 2.0.000 (2008-01-02)
  6665. */
  6666. protected function _putencryption() {
  6667. $this->_out('/Filter /Standard');
  6668. $this->_out('/V 1');
  6669. $this->_out('/R 2');
  6670. $this->_out('/O ('.$this->_escape($this->Ovalue).')');
  6671. $this->_out('/U ('.$this->_escape($this->Uvalue).')');
  6672. $this->_out('/P '.$this->Pvalue);
  6673. }
  6674. /**
  6675. * Returns the input text exrypted using RC4 algorithm and the specified key.
  6676. * RC4 is the standard encryption algorithm used in PDF format
  6677. * @param string $key encryption key
  6678. * @param String $text input text to be encrypted
  6679. * @return String encrypted text
  6680. * @access protected
  6681. * @since 2.0.000 (2008-01-02)
  6682. * @author Klemen Vodopivec
  6683. */
  6684. protected function _RC4($key, $text) {
  6685. if ($this->last_rc4_key != $key) {
  6686. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  6687. $rc4 = range(0, 255);
  6688. $j = 0;
  6689. for ($i = 0; $i < 256; ++$i) {
  6690. $t = $rc4[$i];
  6691. $j = ($j + $t + ord($k{$i})) % 256;
  6692. $rc4[$i] = $rc4[$j];
  6693. $rc4[$j] = $t;
  6694. }
  6695. $this->last_rc4_key = $key;
  6696. $this->last_rc4_key_c = $rc4;
  6697. } else {
  6698. $rc4 = $this->last_rc4_key_c;
  6699. }
  6700. $len = strlen($text);
  6701. $a = 0;
  6702. $b = 0;
  6703. $out = '';
  6704. for ($i = 0; $i < $len; ++$i) {
  6705. $a = ($a + 1) % 256;
  6706. $t = $rc4[$a];
  6707. $b = ($b + $t) % 256;
  6708. $rc4[$a] = $rc4[$b];
  6709. $rc4[$b] = $t;
  6710. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  6711. $out .= chr(ord($text{$i}) ^ $k);
  6712. }
  6713. return $out;
  6714. }
  6715. /**
  6716. * Encrypts a string using MD5 and returns it's value as a binary string.
  6717. * @param string $str input string
  6718. * @return String MD5 encrypted binary string
  6719. * @access protected
  6720. * @since 2.0.000 (2008-01-02)
  6721. * @author Klemen Vodopivec
  6722. */
  6723. protected function _md5_16($str) {
  6724. return pack('H*', md5($str));
  6725. }
  6726. /**
  6727. * Compute O value (used for RC4 encryption)
  6728. * @param String $user_pass user password
  6729. * @param String $owner_pass user password
  6730. * @return String O value
  6731. * @access protected
  6732. * @since 2.0.000 (2008-01-02)
  6733. * @author Klemen Vodopivec
  6734. */
  6735. protected function _Ovalue($user_pass, $owner_pass) {
  6736. $tmp = $this->_md5_16($owner_pass);
  6737. $owner_RC4_key = substr($tmp, 0, 5);
  6738. return $this->_RC4($owner_RC4_key, $user_pass);
  6739. }
  6740. /**
  6741. * Compute U value (used for RC4 encryption)
  6742. * @return String U value
  6743. * @access protected
  6744. * @since 2.0.000 (2008-01-02)
  6745. * @author Klemen Vodopivec
  6746. */
  6747. protected function _Uvalue() {
  6748. return $this->_RC4($this->encryption_key, $this->padding);
  6749. }
  6750. /**
  6751. * Compute encryption key
  6752. * @param String $user_pass user password
  6753. * @param String $owner_pass user password
  6754. * @param String $protection protection type
  6755. * @access protected
  6756. * @since 2.0.000 (2008-01-02)
  6757. * @author Klemen Vodopivec
  6758. */
  6759. protected function _generateencryptionkey($user_pass, $owner_pass, $protection) {
  6760. // Pad passwords
  6761. $user_pass = substr($user_pass.$this->padding, 0, 32);
  6762. $owner_pass = substr($owner_pass.$this->padding, 0, 32);
  6763. // Compute O value
  6764. $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass);
  6765. // Compute encyption key
  6766. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  6767. $this->encryption_key = substr($tmp, 0, 5);
  6768. // Compute U value
  6769. $this->Uvalue = $this->_Uvalue();
  6770. // Compute P value
  6771. $this->Pvalue = -(($protection^255) + 1);
  6772. }
  6773. /**
  6774. * Set document protection
  6775. * The permission array is composed of values taken from the following ones:
  6776. * - copy: copy text and images to the clipboard
  6777. * - print: print the document
  6778. * - modify: modify it (except for annotations and forms)
  6779. * - annot-forms: add annotations and forms
  6780. * Remark: the protection against modification is for people who have the full Acrobat product.
  6781. * 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.
  6782. * 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.
  6783. * @param Array $permissions the set of permissions. Empty by default (only viewing is allowed). (print, modify, copy, annot-forms)
  6784. * @param String $user_pass user password. Empty by default.
  6785. * @param String $owner_pass owner password. If not specified, a random value is used.
  6786. * @access public
  6787. * @since 2.0.000 (2008-01-02)
  6788. * @author Klemen Vodopivec
  6789. */
  6790. public function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) {
  6791. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
  6792. $protection = 192;
  6793. foreach ($permissions as $permission) {
  6794. if (!isset($options[$permission])) {
  6795. $this->Error('Incorrect permission: '.$permission);
  6796. }
  6797. $protection += $options[$permission];
  6798. }
  6799. if ($owner_pass === null) {
  6800. $owner_pass = uniqid(rand());
  6801. }
  6802. $this->encrypted = true;
  6803. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  6804. }
  6805. // END OF ENCRYPTION FUNCTIONS -------------------------
  6806. // START TRANSFORMATIONS SECTION -----------------------
  6807. /**
  6808. * Starts a 2D tranformation saving current graphic state.
  6809. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  6810. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  6811. * @access public
  6812. * @since 2.1.000 (2008-01-07)
  6813. * @see StartTransform(), StopTransform()
  6814. */
  6815. public function StartTransform() {
  6816. $this->_out('q');
  6817. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  6818. }
  6819. /**
  6820. * Stops a 2D tranformation restoring previous graphic state.
  6821. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  6822. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  6823. * @access public
  6824. * @since 2.1.000 (2008-01-07)
  6825. * @see StartTransform(), StopTransform()
  6826. */
  6827. public function StopTransform() {
  6828. $this->_out('Q');
  6829. if (isset($this->transfmatrix)) {
  6830. array_pop($this->transfmatrix);
  6831. }
  6832. array_pop($this->transfmrk[$this->page]);
  6833. }
  6834. /**
  6835. * Horizontal Scaling.
  6836. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  6837. * @param int $x abscissa of the scaling center. Default is current x position
  6838. * @param int $y ordinate of the scaling center. Default is current y position
  6839. * @access public
  6840. * @since 2.1.000 (2008-01-07)
  6841. * @see StartTransform(), StopTransform()
  6842. */
  6843. public function ScaleX($s_x, $x='', $y='') {
  6844. $this->Scale($s_x, 100, $x, $y);
  6845. }
  6846. /**
  6847. * Vertical Scaling.
  6848. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  6849. * @param int $x abscissa of the scaling center. Default is current x position
  6850. * @param int $y ordinate of the scaling center. Default is current y position
  6851. * @access public
  6852. * @since 2.1.000 (2008-01-07)
  6853. * @see StartTransform(), StopTransform()
  6854. */
  6855. public function ScaleY($s_y, $x='', $y='') {
  6856. $this->Scale(100, $s_y, $x, $y);
  6857. }
  6858. /**
  6859. * Vertical and horizontal proportional Scaling.
  6860. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  6861. * @param int $x abscissa of the scaling center. Default is current x position
  6862. * @param int $y ordinate of the scaling center. Default is current y position
  6863. * @access public
  6864. * @since 2.1.000 (2008-01-07)
  6865. * @see StartTransform(), StopTransform()
  6866. */
  6867. public function ScaleXY($s, $x='', $y='') {
  6868. $this->Scale($s, $s, $x, $y);
  6869. }
  6870. /**
  6871. * Vertical and horizontal non-proportional Scaling.
  6872. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  6873. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  6874. * @param int $x abscissa of the scaling center. Default is current x position
  6875. * @param int $y ordinate of the scaling center. Default is current y position
  6876. * @access public
  6877. * @since 2.1.000 (2008-01-07)
  6878. * @see StartTransform(), StopTransform()
  6879. */
  6880. public function Scale($s_x, $s_y, $x='', $y='') {
  6881. if ($x === '') {
  6882. $x = $this->x;
  6883. }
  6884. if ($y === '') {
  6885. $y = $this->y;
  6886. }
  6887. if ($this->rtl) {
  6888. $x = $this->w - $x;
  6889. }
  6890. if (($s_x == 0) OR ($s_y == 0)) {
  6891. $this->Error('Please do not use values equal to zero for scaling');
  6892. }
  6893. $y = ($this->h - $y) * $this->k;
  6894. $x *= $this->k;
  6895. //calculate elements of transformation matrix
  6896. $s_x /= 100;
  6897. $s_y /= 100;
  6898. $tm[0] = $s_x;
  6899. $tm[1] = 0;
  6900. $tm[2] = 0;
  6901. $tm[3] = $s_y;
  6902. $tm[4] = $x * (1 - $s_x);
  6903. $tm[5] = $y * (1 - $s_y);
  6904. //scale the coordinate system
  6905. $this->Transform($tm);
  6906. }
  6907. /**
  6908. * Horizontal Mirroring.
  6909. * @param int $x abscissa of the point. Default is current x position
  6910. * @access public
  6911. * @since 2.1.000 (2008-01-07)
  6912. * @see StartTransform(), StopTransform()
  6913. */
  6914. public function MirrorH($x='') {
  6915. $this->Scale(-100, 100, $x);
  6916. }
  6917. /**
  6918. * Verical Mirroring.
  6919. * @param int $y ordinate of the point. Default is current y position
  6920. * @access public
  6921. * @since 2.1.000 (2008-01-07)
  6922. * @see StartTransform(), StopTransform()
  6923. */
  6924. public function MirrorV($y='') {
  6925. $this->Scale(100, -100, '', $y);
  6926. }
  6927. /**
  6928. * Point reflection mirroring.
  6929. * @param int $x abscissa of the point. Default is current x position
  6930. * @param int $y ordinate of the point. Default is current y position
  6931. * @access public
  6932. * @since 2.1.000 (2008-01-07)
  6933. * @see StartTransform(), StopTransform()
  6934. */
  6935. public function MirrorP($x='',$y='') {
  6936. $this->Scale(-100, -100, $x, $y);
  6937. }
  6938. /**
  6939. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  6940. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  6941. * @param int $x abscissa of the point. Default is current x position
  6942. * @param int $y ordinate of the point. Default is current y position
  6943. * @access public
  6944. * @since 2.1.000 (2008-01-07)
  6945. * @see StartTransform(), StopTransform()
  6946. */
  6947. public function MirrorL($angle=0, $x='',$y='') {
  6948. $this->Scale(-100, 100, $x, $y);
  6949. $this->Rotate(-2*($angle-90), $x, $y);
  6950. }
  6951. /**
  6952. * Translate graphic object horizontally.
  6953. * @param int $t_x movement to the right (or left for RTL)
  6954. * @access public
  6955. * @since 2.1.000 (2008-01-07)
  6956. * @see StartTransform(), StopTransform()
  6957. */
  6958. public function TranslateX($t_x) {
  6959. $this->Translate($t_x, 0);
  6960. }
  6961. /**
  6962. * Translate graphic object vertically.
  6963. * @param int $t_y movement to the bottom
  6964. * @access public
  6965. * @since 2.1.000 (2008-01-07)
  6966. * @see StartTransform(), StopTransform()
  6967. */
  6968. public function TranslateY($t_y) {
  6969. $this->Translate(0, $t_y);
  6970. }
  6971. /**
  6972. * Translate graphic object horizontally and vertically.
  6973. * @param int $t_x movement to the right
  6974. * @param int $t_y movement to the bottom
  6975. * @access public
  6976. * @since 2.1.000 (2008-01-07)
  6977. * @see StartTransform(), StopTransform()
  6978. */
  6979. public function Translate($t_x, $t_y) {
  6980. if ($this->rtl) {
  6981. $t_x = -$t_x;
  6982. }
  6983. //calculate elements of transformation matrix
  6984. $tm[0] = 1;
  6985. $tm[1] = 0;
  6986. $tm[2] = 0;
  6987. $tm[3] = 1;
  6988. $tm[4] = $t_x * $this->k;
  6989. $tm[5] = -$t_y * $this->k;
  6990. //translate the coordinate system
  6991. $this->Transform($tm);
  6992. }
  6993. /**
  6994. * Rotate object.
  6995. * @param float $angle angle in degrees for counter-clockwise rotation
  6996. * @param int $x abscissa of the rotation center. Default is current x position
  6997. * @param int $y ordinate of the rotation center. Default is current y position
  6998. * @access public
  6999. * @since 2.1.000 (2008-01-07)
  7000. * @see StartTransform(), StopTransform()
  7001. */
  7002. public function Rotate($angle, $x='', $y='') {
  7003. if ($x === '') {
  7004. $x = $this->x;
  7005. }
  7006. if ($y === '') {
  7007. $y = $this->y;
  7008. }
  7009. if ($this->rtl) {
  7010. $x = $this->w - $x;
  7011. $angle = -$angle;
  7012. }
  7013. $y = ($this->h - $y) * $this->k;
  7014. $x *= $this->k;
  7015. //calculate elements of transformation matrix
  7016. $tm[0] = cos(deg2rad($angle));
  7017. $tm[1] = sin(deg2rad($angle));
  7018. $tm[2] = -$tm[1];
  7019. $tm[3] = $tm[0];
  7020. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  7021. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  7022. //rotate the coordinate system around ($x,$y)
  7023. $this->Transform($tm);
  7024. }
  7025. /**
  7026. * Skew horizontally.
  7027. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7028. * @param int $x abscissa of the skewing center. default is current x position
  7029. * @param int $y ordinate of the skewing center. default is current y position
  7030. * @access public
  7031. * @since 2.1.000 (2008-01-07)
  7032. * @see StartTransform(), StopTransform()
  7033. */
  7034. public function SkewX($angle_x, $x='', $y='') {
  7035. $this->Skew($angle_x, 0, $x, $y);
  7036. }
  7037. /**
  7038. * Skew vertically.
  7039. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7040. * @param int $x abscissa of the skewing center. default is current x position
  7041. * @param int $y ordinate of the skewing center. default is current y position
  7042. * @access public
  7043. * @since 2.1.000 (2008-01-07)
  7044. * @see StartTransform(), StopTransform()
  7045. */
  7046. public function SkewY($angle_y, $x='', $y='') {
  7047. $this->Skew(0, $angle_y, $x, $y);
  7048. }
  7049. /**
  7050. * Skew.
  7051. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7052. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7053. * @param int $x abscissa of the skewing center. default is current x position
  7054. * @param int $y ordinate of the skewing center. default is current y position
  7055. * @access public
  7056. * @since 2.1.000 (2008-01-07)
  7057. * @see StartTransform(), StopTransform()
  7058. */
  7059. public function Skew($angle_x, $angle_y, $x='', $y='') {
  7060. if ($x === '') {
  7061. $x = $this->x;
  7062. }
  7063. if ($y === '') {
  7064. $y = $this->y;
  7065. }
  7066. if ($this->rtl) {
  7067. $x = $this->w - $x;
  7068. $angle_x = -$angle_x;
  7069. }
  7070. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  7071. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  7072. }
  7073. $x *= $this->k;
  7074. $y = ($this->h - $y) * $this->k;
  7075. //calculate elements of transformation matrix
  7076. $tm[0] = 1;
  7077. $tm[1] = tan(deg2rad($angle_y));
  7078. $tm[2] = tan(deg2rad($angle_x));
  7079. $tm[3] = 1;
  7080. $tm[4] = -$tm[2] * $y;
  7081. $tm[5] = -$tm[1] * $x;
  7082. //skew the coordinate system
  7083. $this->Transform($tm);
  7084. }
  7085. /**
  7086. * Apply graphic transformations.
  7087. * @access protected
  7088. * @since 2.1.000 (2008-01-07)
  7089. * @see StartTransform(), StopTransform()
  7090. */
  7091. protected function Transform($tm) {
  7092. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  7093. // store transformation matrix
  7094. $this->transfmatrix[] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  7095. // update tranformation mark
  7096. if (end($this->transfmrk[$this->page]) !== false) {
  7097. $key = key($this->transfmrk[$this->page]);
  7098. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  7099. }
  7100. }
  7101. // END TRANSFORMATIONS SECTION -------------------------
  7102. // START GRAPHIC FUNCTIONS SECTION ---------------------
  7103. // The following section is based on the code provided by David Hernandez Sanz
  7104. /**
  7105. * 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.
  7106. * @param float $width The width.
  7107. * @access public
  7108. * @since 1.0
  7109. * @see Line(), Rect(), Cell(), MultiCell()
  7110. */
  7111. public function SetLineWidth($width) {
  7112. //Set line width
  7113. $this->LineWidth = $width;
  7114. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  7115. $this->_out($this->linestyleWidth);
  7116. }
  7117. /**
  7118. * Returns the current the line width.
  7119. * @return int Line width
  7120. * @access public
  7121. * @since 2.1.000 (2008-01-07)
  7122. * @see Line(), SetLineWidth()
  7123. */
  7124. public function GetLineWidth() {
  7125. return $this->LineWidth;
  7126. }
  7127. /**
  7128. * Set line style.
  7129. * @param array $style Line style. Array with keys among the following:
  7130. * <ul>
  7131. * <li>width (float): Width of the line in user units.</li>
  7132. * <li>cap (string): Type of cap to put on the line. Possible values are:
  7133. * butt, round, square. The difference between "square" and "butt" is that
  7134. * "square" projects a flat end past the end of the line.</li>
  7135. * <li>join (string): Type of join. Possible values are: miter, round,
  7136. * bevel.</li>
  7137. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  7138. * series of length values, which are the lengths of the on and off dashes.
  7139. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  7140. * 1 off, 2 on, 1 off, ...</li>
  7141. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  7142. * the point at which the pattern starts.</li>
  7143. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  7144. * </ul>
  7145. * @access public
  7146. * @since 2.1.000 (2008-01-08)
  7147. */
  7148. public function SetLineStyle($style) {
  7149. extract($style);
  7150. if (isset($width)) {
  7151. $width_prev = $this->LineWidth;
  7152. $this->SetLineWidth($width);
  7153. $this->LineWidth = $width_prev;
  7154. }
  7155. if (isset($cap)) {
  7156. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  7157. if (isset($ca[$cap])) {
  7158. $this->linestyleCap = $ca[$cap].' J';
  7159. $this->_out($this->linestyleCap);
  7160. }
  7161. }
  7162. if (isset($join)) {
  7163. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  7164. if (isset($ja[$join])) {
  7165. $this->linestyleJoin = $ja[$join].' j';
  7166. $this->_out($this->linestyleJoin);
  7167. }
  7168. }
  7169. if (isset($dash)) {
  7170. $dash_string = '';
  7171. if ($dash) {
  7172. if (ereg('^.+,', $dash)) {
  7173. $tab = explode(',', $dash);
  7174. } else {
  7175. $tab = array($dash);
  7176. }
  7177. $dash_string = '';
  7178. foreach ($tab as $i => $v) {
  7179. if ($i) {
  7180. $dash_string .= ' ';
  7181. }
  7182. $dash_string .= sprintf("%.2F", $v);
  7183. }
  7184. }
  7185. if (!isset($phase) OR !$dash) {
  7186. $phase = 0;
  7187. }
  7188. $this->linestyleDash = sprintf("[%s] %.2F d", $dash_string, $phase);
  7189. $this->_out($this->linestyleDash);
  7190. }
  7191. if (isset($color)) {
  7192. $this->SetDrawColorArray($color);
  7193. }
  7194. }
  7195. /*
  7196. * Set a draw point.
  7197. * @param float $x Abscissa of point.
  7198. * @param float $y Ordinate of point.
  7199. * @access protected
  7200. * @since 2.1.000 (2008-01-08)
  7201. */
  7202. protected function _outPoint($x, $y) {
  7203. if ($this->rtl) {
  7204. $x = $this->w - $x;
  7205. }
  7206. $this->_out(sprintf("%.2F %.2F m", $x * $this->k, ($this->h - $y) * $this->k));
  7207. }
  7208. /*
  7209. * Draws a line from last draw point.
  7210. * @param float $x Abscissa of end point.
  7211. * @param float $y Ordinate of end point.
  7212. * @access protected
  7213. * @since 2.1.000 (2008-01-08)
  7214. */
  7215. protected function _outLine($x, $y) {
  7216. if ($this->rtl) {
  7217. $x = $this->w - $x;
  7218. }
  7219. $this->_out(sprintf("%.2F %.2F l", $x * $this->k, ($this->h - $y) * $this->k));
  7220. }
  7221. /**
  7222. * Draws a rectangle.
  7223. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  7224. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  7225. * @param float $w Width.
  7226. * @param float $h Height.
  7227. * @param string $op options
  7228. * @access protected
  7229. * @since 2.1.000 (2008-01-08)
  7230. */
  7231. protected function _outRect($x, $y, $w, $h, $op) {
  7232. if ($this->rtl) {
  7233. $x = $this->w - $x - $w;
  7234. }
  7235. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k, $op));
  7236. }
  7237. /*
  7238. * Draws a Bezier curve from last draw point.
  7239. * The Bezier curve is a tangent to the line between the control points at either end of the curve.
  7240. * @param float $x1 Abscissa of control point 1.
  7241. * @param float $y1 Ordinate of control point 1.
  7242. * @param float $x2 Abscissa of control point 2.
  7243. * @param float $y2 Ordinate of control point 2.
  7244. * @param float $x3 Abscissa of end point.
  7245. * @param float $y3 Ordinate of end point.
  7246. * @access protected
  7247. * @since 2.1.000 (2008-01-08)
  7248. */
  7249. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  7250. if ($this->rtl) {
  7251. $x1 = $this->w - $x1;
  7252. $x2 = $this->w - $x2;
  7253. $x3 = $this->w - $x3;
  7254. }
  7255. $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));
  7256. }
  7257. /**
  7258. * Draws a line between two points.
  7259. * @param float $x1 Abscissa of first point.
  7260. * @param float $y1 Ordinate of first point.
  7261. * @param float $x2 Abscissa of second point.
  7262. * @param float $y2 Ordinate of second point.
  7263. * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7264. * @access public
  7265. * @since 1.0
  7266. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  7267. */
  7268. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  7269. if ($style) {
  7270. $this->SetLineStyle($style);
  7271. }
  7272. $this->_outPoint($x1, $y1);
  7273. $this->_outLine($x2, $y2);
  7274. $this->_out(' S');
  7275. }
  7276. /**
  7277. * Draws a rectangle.
  7278. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  7279. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  7280. * @param float $w Width.
  7281. * @param float $h Height.
  7282. * @param string $style Style of rendering. Possible values are:
  7283. * <ul>
  7284. * <li>D or empty string: Draw (default).</li>
  7285. * <li>F: Fill.</li>
  7286. * <li>DF or FD: Draw and fill.</li>
  7287. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7288. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7289. * </ul>
  7290. * @param array $border_style Border style of rectangle. Array with keys among the following:
  7291. * <ul>
  7292. * <li>all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7293. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7294. * </ul>
  7295. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  7296. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7297. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7298. * @access public
  7299. * @since 1.0
  7300. * @see SetLineStyle()
  7301. */
  7302. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  7303. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7304. $this->SetFillColorArray($fill_color);
  7305. }
  7306. switch ($style) {
  7307. case 'F': {
  7308. $op = 'f';
  7309. $border_style = array();
  7310. $this->_outRect($x, $y, $w, $h, $op);
  7311. break;
  7312. }
  7313. case 'DF':
  7314. case 'FD': {
  7315. if ((!$border_style) OR (isset($border_style['all']))) {
  7316. $op = 'B';
  7317. if (isset($border_style['all'])) {
  7318. $this->SetLineStyle($border_style['all']);
  7319. $border_style = array();
  7320. }
  7321. } else {
  7322. $op = 'f';
  7323. }
  7324. $this->_outRect($x, $y, $w, $h, $op);
  7325. break;
  7326. }
  7327. case 'CNZ': {
  7328. $op = 'W n';
  7329. $this->_outRect($x, $y, $w, $h, $op);
  7330. break;
  7331. }
  7332. case 'CEO': {
  7333. $op = 'W* n';
  7334. $this->_outRect($x, $y, $w, $h, $op);
  7335. break;
  7336. }
  7337. default: {
  7338. $op = 'S';
  7339. if ((!$border_style) OR (isset($border_style['all']))) {
  7340. if (isset($border_style['all']) AND $border_style['all']) {
  7341. $this->SetLineStyle($border_style['all']);
  7342. $border_style = array();
  7343. }
  7344. $this->_outRect($x, $y, $w, $h, $op);
  7345. }
  7346. break;
  7347. }
  7348. }
  7349. if ($border_style) {
  7350. $border_style2 = array();
  7351. foreach ($border_style as $line => $value) {
  7352. $lenght = strlen($line);
  7353. for ($i = 0; $i < $lenght; ++$i) {
  7354. $border_style2[$line[$i]] = $value;
  7355. }
  7356. }
  7357. $border_style = $border_style2;
  7358. if (isset($border_style['L']) AND $border_style['L']) {
  7359. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  7360. }
  7361. if (isset($border_style['T']) AND $border_style['T']) {
  7362. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  7363. }
  7364. if (isset($border_style['R']) AND $border_style['R']) {
  7365. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  7366. }
  7367. if (isset($border_style['B']) AND $border_style['B']) {
  7368. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  7369. }
  7370. }
  7371. }
  7372. /**
  7373. * Draws a Bezier curve.
  7374. * The Bezier curve is a tangent to the line between the control points at
  7375. * either end of the curve.
  7376. * @param float $x0 Abscissa of start point.
  7377. * @param float $y0 Ordinate of start point.
  7378. * @param float $x1 Abscissa of control point 1.
  7379. * @param float $y1 Ordinate of control point 1.
  7380. * @param float $x2 Abscissa of control point 2.
  7381. * @param float $y2 Ordinate of control point 2.
  7382. * @param float $x3 Abscissa of end point.
  7383. * @param float $y3 Ordinate of end point.
  7384. * @param string $style Style of rendering. Possible values are:
  7385. * <ul>
  7386. * <li>D or empty string: Draw (default).</li>
  7387. * <li>F: Fill.</li>
  7388. * <li>DF or FD: Draw and fill.</li>
  7389. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7390. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7391. * </ul>
  7392. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7393. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7394. * @access public
  7395. * @see SetLineStyle()
  7396. * @since 2.1.000 (2008-01-08)
  7397. */
  7398. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  7399. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7400. $this->SetFillColorArray($fill_color);
  7401. }
  7402. switch ($style) {
  7403. case 'F': {
  7404. $op = 'f';
  7405. $line_style = array();
  7406. break;
  7407. }
  7408. case 'FD':
  7409. case 'DF': {
  7410. $op = 'B';
  7411. break;
  7412. }
  7413. case 'CNZ': {
  7414. $op = 'W n';
  7415. break;
  7416. }
  7417. case 'CEO': {
  7418. $op = 'W* n';
  7419. break;
  7420. }
  7421. default: {
  7422. $op = 'S';
  7423. break;
  7424. }
  7425. }
  7426. if ($line_style) {
  7427. $this->SetLineStyle($line_style);
  7428. }
  7429. $this->_outPoint($x0, $y0);
  7430. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  7431. $this->_out($op);
  7432. }
  7433. /**
  7434. * Draws a poly-Bezier curve.
  7435. * Each Bezier curve segment is a tangent to the line between the control points at
  7436. * either end of the curve.
  7437. * @param float $x0 Abscissa of start point.
  7438. * @param float $y0 Ordinate of start point.
  7439. * @param float $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  7440. * @param string $style Style of rendering. Possible values are:
  7441. * <ul>
  7442. * <li>D or empty string: Draw (default).</li>
  7443. * <li>F: Fill.</li>
  7444. * <li>DF or FD: Draw and fill.</li>
  7445. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7446. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7447. * </ul>
  7448. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7449. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7450. * @access public
  7451. * @see SetLineStyle()
  7452. * @since 3.0008 (2008-05-12)
  7453. */
  7454. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  7455. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7456. $this->SetFillColorArray($fill_color);
  7457. }
  7458. switch ($style) {
  7459. case 'F': {
  7460. $op = 'f';
  7461. $line_style = array();
  7462. break;
  7463. }
  7464. case 'FD':
  7465. case 'DF': {
  7466. $op = 'B';
  7467. break;
  7468. }
  7469. case 'CNZ': {
  7470. $op = 'W n';
  7471. break;
  7472. }
  7473. case 'CEO': {
  7474. $op = 'W* n';
  7475. break;
  7476. }
  7477. default: {
  7478. $op = 'S';
  7479. break;
  7480. }
  7481. }
  7482. if ($line_style) {
  7483. $this->SetLineStyle($line_style);
  7484. }
  7485. $this->_outPoint($x0, $y0);
  7486. foreach ($segments as $segment) {
  7487. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  7488. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  7489. }
  7490. $this->_out($op);
  7491. }
  7492. /**
  7493. * Draws an ellipse.
  7494. * An ellipse is formed from n Bezier curves.
  7495. * @param float $x0 Abscissa of center point.
  7496. * @param float $y0 Ordinate of center point.
  7497. * @param float $rx Horizontal radius.
  7498. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  7499. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  7500. * @param float $astart: Angle start of draw line. Default value: 0.
  7501. * @param float $afinish: Angle finish of draw line. Default value: 360.
  7502. * @param string $style Style of rendering. Possible values are:
  7503. * <ul>
  7504. * <li>D or empty string: Draw (default).</li>
  7505. * <li>F: Fill.</li>
  7506. * <li>DF or FD: Draw and fill.</li>
  7507. * <li>C: Draw close.</li>
  7508. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7509. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7510. * </ul>
  7511. * @param array $line_style Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7512. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7513. * @param integer $nc Number of curves used in ellipse. Default value: 8.
  7514. * @access public
  7515. * @since 2.1.000 (2008-01-08)
  7516. */
  7517. public function Ellipse($x0, $y0, $rx, $ry=0, $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  7518. if ($angle) {
  7519. $this->StartTransform();
  7520. $this->Rotate($angle, $x0, $y0);
  7521. $this->Ellipse($x0, $y0, $rx, $ry, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  7522. $this->StopTransform();
  7523. return;
  7524. }
  7525. if ($rx) {
  7526. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7527. $this->SetFillColorArray($fill_color);
  7528. }
  7529. switch ($style) {
  7530. case 'F': {
  7531. $op = 'f';
  7532. $line_style = array();
  7533. break;
  7534. }
  7535. case 'FD':
  7536. case 'DF': {
  7537. $op = 'B';
  7538. break;
  7539. }
  7540. case 'C': {
  7541. $op = 's'; // Small 's' signifies closing the path as well
  7542. break;
  7543. }
  7544. case 'CNZ': {
  7545. $op = 'W n';
  7546. break;
  7547. }
  7548. case 'CEO': {
  7549. $op = 'W* n';
  7550. break;
  7551. }
  7552. default: {
  7553. $op = 'S';
  7554. break;
  7555. }
  7556. }
  7557. if ($line_style) {
  7558. $this->SetLineStyle($line_style);
  7559. }
  7560. if (!$ry) {
  7561. $ry = $rx;
  7562. }
  7563. $rx *= $this->k;
  7564. $ry *= $this->k;
  7565. if ($nc < 2) {
  7566. $nc = 2;
  7567. }
  7568. $astart = deg2rad((float) $astart);
  7569. $afinish = deg2rad((float) $afinish);
  7570. $total_angle = $afinish - $astart;
  7571. $dt = $total_angle / $nc;
  7572. $dtm = $dt / 3;
  7573. $x0 *= $this->k;
  7574. $y0 = ($this->h - $y0) * $this->k;
  7575. $t1 = $astart;
  7576. $a0 = $x0 + ($rx * cos($t1));
  7577. $b0 = $y0 + ($ry * sin($t1));
  7578. $c0 = -$rx * sin($t1);
  7579. $d0 = $ry * cos($t1);
  7580. $this->_outPoint($a0 / $this->k, $this->h - ($b0 / $this->k));
  7581. for ($i = 1; $i <= $nc; ++$i) {
  7582. // Draw this bit of the total curve
  7583. $t1 = ($i * $dt) + $astart;
  7584. $a1 = $x0 + ($rx * cos($t1));
  7585. $b1 = $y0 + ($ry * sin($t1));
  7586. $c1 = -$rx * sin($t1);
  7587. $d1 = $ry * cos($t1);
  7588. $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));
  7589. $a0 = $a1;
  7590. $b0 = $b1;
  7591. $c0 = $c1;
  7592. $d0 = $d1;
  7593. }
  7594. $this->_out($op);
  7595. }
  7596. }
  7597. /**
  7598. * Draws a circle.
  7599. * A circle is formed from n Bezier curves.
  7600. * @param float $x0 Abscissa of center point.
  7601. * @param float $y0 Ordinate of center point.
  7602. * @param float $r Radius.
  7603. * @param float $astart: Angle start of draw line. Default value: 0.
  7604. * @param float $afinish: Angle finish of draw line. Default value: 360.
  7605. * @param string $style Style of rendering. Possible values are:
  7606. * <ul>
  7607. * <li>D or empty string: Draw (default).</li>
  7608. * <li>F: Fill.</li>
  7609. * <li>DF or FD: Draw and fill.</li>
  7610. * <li>C: Draw close.</li>
  7611. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7612. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7613. * </ul>
  7614. * @param array $line_style Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7615. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  7616. * @param integer $nc Number of curves used in circle. Default value: 8.
  7617. * @access public
  7618. * @since 2.1.000 (2008-01-08)
  7619. */
  7620. public function Circle($x0, $y0, $r, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  7621. $this->Ellipse($x0, $y0, $r, 0, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  7622. }
  7623. /**
  7624. * Draws a polygon.
  7625. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  7626. * @param string $style Style of rendering. Possible values are:
  7627. * <ul>
  7628. * <li>D or empty string: Draw (default).</li>
  7629. * <li>F: Fill.</li>
  7630. * <li>DF or FD: Draw and fill.</li>
  7631. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7632. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7633. * </ul>
  7634. * @param array $line_style Line style of polygon. Array with keys among the following:
  7635. * <ul>
  7636. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7637. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7638. * </ul>
  7639. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  7640. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7641. * @access public
  7642. * @since 2.1.000 (2008-01-08)
  7643. */
  7644. public function Polygon($p, $style='', $line_style=array(), $fill_color=array()) {
  7645. $np = count($p) / 2;
  7646. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7647. $this->SetFillColorArray($fill_color);
  7648. }
  7649. switch ($style) {
  7650. case 'F': {
  7651. $line_style = array();
  7652. $op = 'f';
  7653. break;
  7654. }
  7655. case 'FD':
  7656. case 'DF': {
  7657. $op = 'B';
  7658. break;
  7659. }
  7660. case 'CNZ': {
  7661. $op = 'W n';
  7662. break;
  7663. }
  7664. case 'CEO': {
  7665. $op = 'W* n';
  7666. break;
  7667. }
  7668. default: {
  7669. $op = 'S';
  7670. break;
  7671. }
  7672. }
  7673. $draw = true;
  7674. if ($line_style) {
  7675. if (isset($line_style['all'])) {
  7676. $this->SetLineStyle($line_style['all']);
  7677. } else { // 0 .. (np - 1), op = {B, S}
  7678. $draw = false;
  7679. if ('B' == $op) {
  7680. $op = 'f';
  7681. $this->_outPoint($p[0], $p[1]);
  7682. for ($i = 2; $i < ($np * 2); $i = $i + 2) {
  7683. $this->_outLine($p[$i], $p[$i + 1]);
  7684. }
  7685. $this->_outLine($p[0], $p[1]);
  7686. $this->_out($op);
  7687. }
  7688. $p[($np * 2)] = $p[0];
  7689. $p[(($np * 2) + 1)] = $p[1];
  7690. for ($i = 0; $i < $np; ++$i) {
  7691. if (isset($line_style[$i]) AND ($line_style[$i] != 0)) {
  7692. $this->Line($p[($i * 2)], $p[(($i * 2) + 1)], $p[(($i * 2) + 2)], $p[(($i * 2) + 3)], $line_style[$i]);
  7693. }
  7694. }
  7695. }
  7696. }
  7697. if ($draw) {
  7698. $this->_outPoint($p[0], $p[1]);
  7699. for ($i = 2; $i < ($np * 2); $i = $i + 2) {
  7700. $this->_outLine($p[$i], $p[$i + 1]);
  7701. }
  7702. $this->_outLine($p[0], $p[1]);
  7703. $this->_out($op);
  7704. }
  7705. }
  7706. /**
  7707. * Draws a regular polygon.
  7708. * @param float $x0 Abscissa of center point.
  7709. * @param float $y0 Ordinate of center point.
  7710. * @param float $r: Radius of inscribed circle.
  7711. * @param integer $ns Number of sides.
  7712. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  7713. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  7714. * @param string $style Style of rendering. Possible values are:
  7715. * <ul>
  7716. * <li>D or empty string: Draw (default).</li>
  7717. * <li>F: Fill.</li>
  7718. * <li>DF or FD: Draw and fill.</li>
  7719. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7720. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7721. * </ul>
  7722. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  7723. * <ul>
  7724. * <li>all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7725. * <li>0 to ($ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7726. * </ul>
  7727. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  7728. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  7729. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  7730. * <ul>
  7731. * <li>D or empty string: Draw (default).</li>
  7732. * <li>F: Fill.</li>
  7733. * <li>DF or FD: Draw and fill.</li>
  7734. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7735. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7736. * </ul>
  7737. * @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).
  7738. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  7739. * @access public
  7740. * @since 2.1.000 (2008-01-08)
  7741. */
  7742. public function RegularPolygon($x0, $y0, $r, $ns, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  7743. if (3 > $ns) {
  7744. $ns = 3;
  7745. }
  7746. if ($draw_circle) {
  7747. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  7748. }
  7749. $p = array();
  7750. for ($i = 0; $i < $ns; ++$i) {
  7751. $a = $angle + ($i * 360 / $ns);
  7752. $a_rad = deg2rad((float) $a);
  7753. $p[] = $x0 + ($r * sin($a_rad));
  7754. $p[] = $y0 + ($r * cos($a_rad));
  7755. }
  7756. $this->Polygon($p, $style, $line_style, $fill_color);
  7757. }
  7758. /**
  7759. * Draws a star polygon
  7760. * @param float $x0 Abscissa of center point.
  7761. * @param float $y0 Ordinate of center point.
  7762. * @param float $r Radius of inscribed circle.
  7763. * @param integer $nv Number of vertices.
  7764. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  7765. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  7766. * @param boolean $draw_circle: Draw inscribed circle or not. Default value is false.
  7767. * @param string $style Style of rendering. Possible values are:
  7768. * <ul>
  7769. * <li>D or empty string: Draw (default).</li>
  7770. * <li>F: Fill.</li>
  7771. * <li>DF or FD: Draw and fill.</li>
  7772. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7773. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7774. * </ul>
  7775. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  7776. * <ul>
  7777. * <li>all: Line style of all sides. Array like for
  7778. * {@link SetLineStyle SetLineStyle}.</li>
  7779. * <li>0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7780. * </ul>
  7781. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  7782. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  7783. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  7784. * <ul>
  7785. * <li>D or empty string: Draw (default).</li>
  7786. * <li>F: Fill.</li>
  7787. * <li>DF or FD: Draw and fill.</li>
  7788. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7789. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7790. * </ul>
  7791. * @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).
  7792. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  7793. * @access public
  7794. * @since 2.1.000 (2008-01-08)
  7795. */
  7796. public function StarPolygon($x0, $y0, $r, $nv, $ng, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  7797. if (2 > $nv) {
  7798. $nv = 2;
  7799. }
  7800. if ($draw_circle) {
  7801. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  7802. }
  7803. $p2 = array();
  7804. $visited = array();
  7805. for ($i = 0; $i < $nv; ++$i) {
  7806. $a = $angle + ($i * 360 / $nv);
  7807. $a_rad = deg2rad((float) $a);
  7808. $p2[] = $x0 + ($r * sin($a_rad));
  7809. $p2[] = $y0 + ($r * cos($a_rad));
  7810. $visited[] = false;
  7811. }
  7812. $p = array();
  7813. $i = 0;
  7814. do {
  7815. $p[] = $p2[$i * 2];
  7816. $p[] = $p2[($i * 2) + 1];
  7817. $visited[$i] = true;
  7818. $i += $ng;
  7819. $i %= $nv;
  7820. } while (!$visited[$i]);
  7821. $this->Polygon($p, $style, $line_style, $fill_color);
  7822. }
  7823. /**
  7824. * Draws a rounded rectangle.
  7825. * @param float $x Abscissa of upper-left corner.
  7826. * @param float $y Ordinate of upper-left corner.
  7827. * @param float $w Width.
  7828. * @param float $h Height.
  7829. * @param float $r Radius of the rounded corners.
  7830. * @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").
  7831. * @param string $style Style of rendering. Possible values are:
  7832. * <ul>
  7833. * <li>D or empty string: Draw (default).</li>
  7834. * <li>F: Fill.</li>
  7835. * <li>DF or FD: Draw and fill.</li>
  7836. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7837. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7838. * </ul>
  7839. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7840. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7841. * @access public
  7842. * @since 2.1.000 (2008-01-08)
  7843. */
  7844. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  7845. if ('0000' == $round_corner) { // Not rounded
  7846. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  7847. } else { // Rounded
  7848. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7849. $this->SetFillColorArray($fill_color);
  7850. }
  7851. switch ($style) {
  7852. case 'F': {
  7853. $border_style = array();
  7854. $op = 'f';
  7855. break;
  7856. }
  7857. case 'FD':
  7858. case 'DF': {
  7859. $op = 'B';
  7860. break;
  7861. }
  7862. case 'CNZ': {
  7863. $op = 'W n';
  7864. break;
  7865. }
  7866. case 'CEO': {
  7867. $op = 'W* n';
  7868. break;
  7869. }
  7870. default: {
  7871. $op = 'S';
  7872. break;
  7873. }
  7874. }
  7875. if ($border_style) {
  7876. $this->SetLineStyle($border_style);
  7877. }
  7878. $MyArc = 4 / 3 * (sqrt(2) - 1);
  7879. $this->_outPoint($x + $r, $y);
  7880. $xc = $x + $w - $r;
  7881. $yc = $y + $r;
  7882. $this->_outLine($xc, $y);
  7883. if ($round_corner[0]) {
  7884. $this->_outCurve($xc + ($r * $MyArc), $yc - $r, $xc + $r, $yc - ($r * $MyArc), $xc + $r, $yc);
  7885. } else {
  7886. $this->_outLine($x + $w, $y);
  7887. }
  7888. $xc = $x + $w - $r;
  7889. $yc = $y + $h - $r;
  7890. $this->_outLine($x + $w, $yc);
  7891. if ($round_corner[1]) {
  7892. $this->_outCurve($xc + $r, $yc + ($r * $MyArc), $xc + ($r * $MyArc), $yc + $r, $xc, $yc + $r);
  7893. } else {
  7894. $this->_outLine($x + $w, $y + $h);
  7895. }
  7896. $xc = $x + $r;
  7897. $yc = $y + $h - $r;
  7898. $this->_outLine($xc, $y + $h);
  7899. if ($round_corner[2]) {
  7900. $this->_outCurve($xc - ($r * $MyArc), $yc + $r, $xc - $r, $yc + ($r * $MyArc), $xc - $r, $yc);
  7901. } else {
  7902. $this->_outLine($x, $y + $h);
  7903. }
  7904. $xc = $x + $r;
  7905. $yc = $y + $r;
  7906. $this->_outLine($x, $yc);
  7907. if ($round_corner[3]) {
  7908. $this->_outCurve($xc - $r, $yc - ($r * $MyArc), $xc - ($r * $MyArc), $yc - $r, $xc, $yc - $r);
  7909. } else {
  7910. $this->_outLine($x, $y);
  7911. $this->_outLine($x + $r, $y);
  7912. }
  7913. $this->_out($op);
  7914. }
  7915. }
  7916. // END GRAPHIC FUNCTIONS SECTION -----------------------
  7917. // BIDIRECTIONAL TEXT SECTION --------------------------
  7918. /**
  7919. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  7920. * @param string $str string to manipulate.
  7921. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  7922. * @return string
  7923. * @access protected
  7924. * @author Nicola Asuni
  7925. * @since 2.1.000 (2008-01-08)
  7926. */
  7927. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  7928. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($this->UTF8StringToArray($str), $str, $forcertl), $setbom);
  7929. }
  7930. /**
  7931. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  7932. * @param array $ta array of characters composing the string.
  7933. * @param string $str string to process
  7934. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  7935. * @return string
  7936. * @author Nicola Asuni
  7937. * @access protected
  7938. * @since 2.4.000 (2008-03-06)
  7939. */
  7940. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  7941. global $unicode, $unicode_mirror, $unicode_arlet, $laa_array, $diacritics;
  7942. // paragraph embedding level
  7943. $pel = 0;
  7944. // max level
  7945. $maxlevel = 0;
  7946. if ($this->empty_string($str)) {
  7947. // create string from array
  7948. $str = $this->UTF8ArrSubString($ta);
  7949. }
  7950. // check if string contains arabic text
  7951. if (preg_match(K_RE_PATTERN_ARABIC, $str)) {
  7952. $arabic = true;
  7953. } else {
  7954. $arabic = false;
  7955. }
  7956. // check if string contains RTL text
  7957. if (!($forcertl OR $arabic OR preg_match(K_RE_PATTERN_RTL, $str))) {
  7958. return $ta;
  7959. }
  7960. // get number of chars
  7961. $numchars = count($ta);
  7962. if ($forcertl == 'R') {
  7963. $pel = 1;
  7964. } elseif ($forcertl == 'L') {
  7965. $pel = 0;
  7966. } else {
  7967. // P2. In each paragraph, find the first character of type L, AL, or R.
  7968. // 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.
  7969. for ($i=0; $i < $numchars; ++$i) {
  7970. $type = $unicode[$ta[$i]];
  7971. if ($type == 'L') {
  7972. $pel = 0;
  7973. break;
  7974. } elseif (($type == 'AL') OR ($type == 'R')) {
  7975. $pel = 1;
  7976. break;
  7977. }
  7978. }
  7979. }
  7980. // Current Embedding Level
  7981. $cel = $pel;
  7982. // directional override status
  7983. $dos = 'N';
  7984. $remember = array();
  7985. // start-of-level-run
  7986. $sor = $pel % 2 ? 'R' : 'L';
  7987. $eor = $sor;
  7988. // Array of characters data
  7989. $chardata = Array();
  7990. // 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.
  7991. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  7992. for ($i=0; $i < $numchars; ++$i) {
  7993. if ($ta[$i] == K_RLE) {
  7994. // X2. With each RLE, compute the least greater odd embedding level.
  7995. // 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.
  7996. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  7997. $next_level = $cel + ($cel % 2) + 1;
  7998. if ($next_level < 62) {
  7999. $remember[] = array('num' => K_RLE, 'cel' => $cel, 'dos' => $dos);
  8000. $cel = $next_level;
  8001. $dos = 'N';
  8002. $sor = $eor;
  8003. $eor = $cel % 2 ? 'R' : 'L';
  8004. }
  8005. } elseif ($ta[$i] == K_LRE) {
  8006. // X3. With each LRE, compute the least greater even embedding level.
  8007. // 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.
  8008. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8009. $next_level = $cel + 2 - ($cel % 2);
  8010. if ( $next_level < 62 ) {
  8011. $remember[] = array('num' => K_LRE, 'cel' => $cel, 'dos' => $dos);
  8012. $cel = $next_level;
  8013. $dos = 'N';
  8014. $sor = $eor;
  8015. $eor = $cel % 2 ? 'R' : 'L';
  8016. }
  8017. } elseif ($ta[$i] == K_RLO) {
  8018. // X4. With each RLO, compute the least greater odd embedding level.
  8019. // 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.
  8020. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8021. $next_level = $cel + ($cel % 2) + 1;
  8022. if ($next_level < 62) {
  8023. $remember[] = array('num' => K_RLO, 'cel' => $cel, 'dos' => $dos);
  8024. $cel = $next_level;
  8025. $dos = 'R';
  8026. $sor = $eor;
  8027. $eor = $cel % 2 ? 'R' : 'L';
  8028. }
  8029. } elseif ($ta[$i] == K_LRO) {
  8030. // X5. With each LRO, compute the least greater even embedding level.
  8031. // 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.
  8032. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8033. $next_level = $cel + 2 - ($cel % 2);
  8034. if ( $next_level < 62 ) {
  8035. $remember[] = array('num' => K_LRO, 'cel' => $cel, 'dos' => $dos);
  8036. $cel = $next_level;
  8037. $dos = 'L';
  8038. $sor = $eor;
  8039. $eor = $cel % 2 ? 'R' : 'L';
  8040. }
  8041. } elseif ($ta[$i] == K_PDF) {
  8042. // 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.
  8043. if (count($remember)) {
  8044. $last = count($remember ) - 1;
  8045. if (($remember[$last]['num'] == K_RLE) OR
  8046. ($remember[$last]['num'] == K_LRE) OR
  8047. ($remember[$last]['num'] == K_RLO) OR
  8048. ($remember[$last]['num'] == K_LRO)) {
  8049. $match = array_pop($remember);
  8050. $cel = $match['cel'];
  8051. $dos = $match['dos'];
  8052. $sor = $eor;
  8053. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  8054. }
  8055. }
  8056. } elseif (($ta[$i] != K_RLE) AND
  8057. ($ta[$i] != K_LRE) AND
  8058. ($ta[$i] != K_RLO) AND
  8059. ($ta[$i] != K_LRO) AND
  8060. ($ta[$i] != K_PDF)) {
  8061. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  8062. // a. Set the level of the current character to the current embedding level.
  8063. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  8064. if ($dos != 'N') {
  8065. $chardir = $dos;
  8066. } else {
  8067. $chardir = $unicode[$ta[$i]];
  8068. }
  8069. // stores string characters and other information
  8070. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  8071. }
  8072. } // end for each char
  8073. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  8074. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  8075. // 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.
  8076. // 3.3.3 Resolving Weak Types
  8077. // 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.
  8078. // Nonspacing marks are now resolved based on the previous characters.
  8079. $numchars = count($chardata);
  8080. // 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.
  8081. $prevlevel = -1; // track level changes
  8082. $levcount = 0; // counts consecutive chars at the same level
  8083. for ($i=0; $i < $numchars; ++$i) {
  8084. if ($chardata[$i]['type'] == 'NSM') {
  8085. if ($levcount) {
  8086. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8087. } elseif ($i > 0) {
  8088. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  8089. }
  8090. }
  8091. if ($chardata[$i]['level'] != $prevlevel) {
  8092. $levcount = 0;
  8093. } else {
  8094. ++$levcount;
  8095. }
  8096. $prevlevel = $chardata[$i]['level'];
  8097. }
  8098. // 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.
  8099. $prevlevel = -1;
  8100. $levcount = 0;
  8101. for ($i=0; $i < $numchars; ++$i) {
  8102. if ($chardata[$i]['char'] == 'EN') {
  8103. for ($j=$levcount; $j >= 0; $j--) {
  8104. if ($chardata[$j]['type'] == 'AL') {
  8105. $chardata[$i]['type'] = 'AN';
  8106. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  8107. break;
  8108. }
  8109. }
  8110. }
  8111. if ($chardata[$i]['level'] != $prevlevel) {
  8112. $levcount = 0;
  8113. } else {
  8114. ++$levcount;
  8115. }
  8116. $prevlevel = $chardata[$i]['level'];
  8117. }
  8118. // W3. Change all ALs to R.
  8119. for ($i=0; $i < $numchars; ++$i) {
  8120. if ($chardata[$i]['type'] == 'AL') {
  8121. $chardata[$i]['type'] = 'R';
  8122. }
  8123. }
  8124. // 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.
  8125. $prevlevel = -1;
  8126. $levcount = 0;
  8127. for ($i=0; $i < $numchars; ++$i) {
  8128. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8129. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  8130. $chardata[$i]['type'] = 'EN';
  8131. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  8132. $chardata[$i]['type'] = 'EN';
  8133. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  8134. $chardata[$i]['type'] = 'AN';
  8135. }
  8136. }
  8137. if ($chardata[$i]['level'] != $prevlevel) {
  8138. $levcount = 0;
  8139. } else {
  8140. ++$levcount;
  8141. }
  8142. $prevlevel = $chardata[$i]['level'];
  8143. }
  8144. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  8145. $prevlevel = -1;
  8146. $levcount = 0;
  8147. for ($i=0; $i < $numchars; ++$i) {
  8148. if ($chardata[$i]['type'] == 'ET') {
  8149. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  8150. $chardata[$i]['type'] = 'EN';
  8151. } else {
  8152. $j = $i+1;
  8153. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  8154. if ($chardata[$j]['type'] == 'EN') {
  8155. $chardata[$i]['type'] = 'EN';
  8156. break;
  8157. } elseif ($chardata[$j]['type'] != 'ET') {
  8158. break;
  8159. }
  8160. ++$j;
  8161. }
  8162. }
  8163. }
  8164. if ($chardata[$i]['level'] != $prevlevel) {
  8165. $levcount = 0;
  8166. } else {
  8167. ++$levcount;
  8168. }
  8169. $prevlevel = $chardata[$i]['level'];
  8170. }
  8171. // W6. Otherwise, separators and terminators change to Other Neutral.
  8172. $prevlevel = -1;
  8173. $levcount = 0;
  8174. for ($i=0; $i < $numchars; ++$i) {
  8175. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  8176. $chardata[$i]['type'] = 'ON';
  8177. }
  8178. if ($chardata[$i]['level'] != $prevlevel) {
  8179. $levcount = 0;
  8180. } else {
  8181. ++$levcount;
  8182. }
  8183. $prevlevel = $chardata[$i]['level'];
  8184. }
  8185. //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.
  8186. $prevlevel = -1;
  8187. $levcount = 0;
  8188. for ($i=0; $i < $numchars; ++$i) {
  8189. if ($chardata[$i]['char'] == 'EN') {
  8190. for ($j=$levcount; $j >= 0; $j--) {
  8191. if ($chardata[$j]['type'] == 'L') {
  8192. $chardata[$i]['type'] = 'L';
  8193. } elseif ($chardata[$j]['type'] == 'R') {
  8194. break;
  8195. }
  8196. }
  8197. }
  8198. if ($chardata[$i]['level'] != $prevlevel) {
  8199. $levcount = 0;
  8200. } else {
  8201. ++$levcount;
  8202. }
  8203. $prevlevel = $chardata[$i]['level'];
  8204. }
  8205. // 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.
  8206. $prevlevel = -1;
  8207. $levcount = 0;
  8208. for ($i=0; $i < $numchars; ++$i) {
  8209. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8210. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  8211. $chardata[$i]['type'] = 'L';
  8212. } elseif (($chardata[$i]['type'] == 'N') AND
  8213. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  8214. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  8215. $chardata[$i]['type'] = 'R';
  8216. } elseif ($chardata[$i]['type'] == 'N') {
  8217. // N2. Any remaining neutrals take the embedding direction
  8218. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8219. }
  8220. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8221. // first char
  8222. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  8223. $chardata[$i]['type'] = 'L';
  8224. } elseif (($chardata[$i]['type'] == 'N') AND
  8225. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  8226. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  8227. $chardata[$i]['type'] = 'R';
  8228. } elseif ($chardata[$i]['type'] == 'N') {
  8229. // N2. Any remaining neutrals take the embedding direction
  8230. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8231. }
  8232. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  8233. //last char
  8234. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  8235. $chardata[$i]['type'] = 'L';
  8236. } elseif (($chardata[$i]['type'] == 'N') AND
  8237. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  8238. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  8239. $chardata[$i]['type'] = 'R';
  8240. } elseif ($chardata[$i]['type'] == 'N') {
  8241. // N2. Any remaining neutrals take the embedding direction
  8242. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8243. }
  8244. } elseif ($chardata[$i]['type'] == 'N') {
  8245. // N2. Any remaining neutrals take the embedding direction
  8246. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8247. }
  8248. if ($chardata[$i]['level'] != $prevlevel) {
  8249. $levcount = 0;
  8250. } else {
  8251. ++$levcount;
  8252. }
  8253. $prevlevel = $chardata[$i]['level'];
  8254. }
  8255. // 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.
  8256. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  8257. for ($i=0; $i < $numchars; ++$i) {
  8258. $odd = $chardata[$i]['level'] % 2;
  8259. if ($odd) {
  8260. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  8261. $chardata[$i]['level'] += 1;
  8262. }
  8263. } else {
  8264. if ($chardata[$i]['type'] == 'R') {
  8265. $chardata[$i]['level'] += 1;
  8266. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  8267. $chardata[$i]['level'] += 2;
  8268. }
  8269. }
  8270. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  8271. }
  8272. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  8273. // 1. Segment separators,
  8274. // 2. Paragraph separators,
  8275. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  8276. // 4. Any sequence of white space characters at the end of the line.
  8277. for ($i=0; $i < $numchars; ++$i) {
  8278. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  8279. $chardata[$i]['level'] = $pel;
  8280. } elseif ($chardata[$i]['type'] == 'WS') {
  8281. $j = $i+1;
  8282. while ($j < $numchars) {
  8283. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  8284. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  8285. $chardata[$i]['level'] = $pel;
  8286. break;
  8287. } elseif ($chardata[$j]['type'] != 'WS') {
  8288. break;
  8289. }
  8290. ++$j;
  8291. }
  8292. }
  8293. }
  8294. // Arabic Shaping
  8295. // 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.
  8296. if ($arabic) {
  8297. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  8298. $alfletter = array(1570,1571,1573,1575);
  8299. $chardata2 = $chardata;
  8300. $laaletter = false;
  8301. $charAL = array();
  8302. $x = 0;
  8303. for ($i=0; $i < $numchars; ++$i) {
  8304. if (($unicode[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  8305. $charAL[$x] = $chardata[$i];
  8306. $charAL[$x]['i'] = $i;
  8307. $chardata[$i]['x'] = $x;
  8308. ++$x;
  8309. }
  8310. }
  8311. $numAL = $x;
  8312. for ($i=0; $i < $numchars; ++$i) {
  8313. $thischar = $chardata[$i];
  8314. if ($i > 0) {
  8315. $prevchar = $chardata[($i-1)];
  8316. } else {
  8317. $prevchar = false;
  8318. }
  8319. if (($i+1) < $numchars) {
  8320. $nextchar = $chardata[($i+1)];
  8321. } else {
  8322. $nextchar = false;
  8323. }
  8324. if ($unicode[$thischar['char']] == 'AL') {
  8325. $x = $thischar['x'];
  8326. if ($x > 0) {
  8327. $prevchar = $charAL[($x-1)];
  8328. } else {
  8329. $prevchar = false;
  8330. }
  8331. if (($x+1) < $numAL) {
  8332. $nextchar = $charAL[($x+1)];
  8333. } else {
  8334. $nextchar = false;
  8335. }
  8336. // if laa letter
  8337. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  8338. $arabicarr = $laa_array;
  8339. $laaletter = true;
  8340. if ($x > 1) {
  8341. $prevchar = $charAL[($x-2)];
  8342. } else {
  8343. $prevchar = false;
  8344. }
  8345. } else {
  8346. $arabicarr = $unicode_arlet;
  8347. $laaletter = false;
  8348. }
  8349. if (($prevchar !== false) AND ($nextchar !== false) AND
  8350. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  8351. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  8352. ($prevchar['type'] == $thischar['type']) AND
  8353. ($nextchar['type'] == $thischar['type']) AND
  8354. ($nextchar['char'] != 1567)) {
  8355. if (in_array($prevchar['char'], $endedletter)) {
  8356. if (isset($arabicarr[$thischar['char']][2])) {
  8357. // initial
  8358. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  8359. }
  8360. } else {
  8361. if (isset($arabicarr[$thischar['char']][3])) {
  8362. // medial
  8363. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  8364. }
  8365. }
  8366. } elseif (($nextchar !== false) AND
  8367. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  8368. ($nextchar['type'] == $thischar['type']) AND
  8369. ($nextchar['char'] != 1567)) {
  8370. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  8371. // initial
  8372. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  8373. }
  8374. } elseif ((($prevchar !== false) AND
  8375. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  8376. ($prevchar['type'] == $thischar['type'])) OR
  8377. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  8378. // final
  8379. if (($i > 1) AND ($thischar['char'] == 1607) AND
  8380. ($chardata[$i-1]['char'] == 1604) AND
  8381. ($chardata[$i-2]['char'] == 1604)) {
  8382. //Allah Word
  8383. // mark characters to delete with false
  8384. $chardata2[$i-2]['char'] = false;
  8385. $chardata2[$i-1]['char'] = false;
  8386. $chardata2[$i]['char'] = 65010;
  8387. } else {
  8388. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  8389. if (isset($arabicarr[$thischar['char']][0])) {
  8390. // isolated
  8391. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  8392. }
  8393. } else {
  8394. if (isset($arabicarr[$thischar['char']][1])) {
  8395. // final
  8396. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  8397. }
  8398. }
  8399. }
  8400. } elseif (isset($arabicarr[$thischar['char']][0])) {
  8401. // isolated
  8402. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  8403. }
  8404. // if laa letter
  8405. if ($laaletter) {
  8406. // mark characters to delete with false
  8407. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  8408. }
  8409. } // end if AL (Arabic Letter)
  8410. } // end for each char
  8411. /*
  8412. * Combining characters that can occur with Shadda (0651 HEX, 1617 DEC) are placed in UE586-UE594.
  8413. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  8414. */
  8415. $cw = &$this->CurrentFont['cw'];
  8416. for ($i = 0; $i < ($numchars-1); ++$i) {
  8417. if (($chardata2[$i]['char'] == 1617) AND (isset($diacritics[($chardata2[$i+1]['char'])]))) {
  8418. // check if the subtitution font is defined on current font
  8419. if (isset($cw[($diacritics[($chardata2[$i+1]['char'])])])) {
  8420. $chardata2[$i]['char'] = false;
  8421. $chardata2[$i+1]['char'] = $diacritics[($chardata2[$i+1]['char'])];
  8422. }
  8423. }
  8424. }
  8425. // remove marked characters
  8426. foreach ($chardata2 as $key => $value) {
  8427. if ($value['char'] === false) {
  8428. unset($chardata2[$key]);
  8429. }
  8430. }
  8431. $chardata = array_values($chardata2);
  8432. $numchars = count($chardata);
  8433. unset($chardata2);
  8434. unset($arabicarr);
  8435. unset($laaletter);
  8436. unset($charAL);
  8437. }
  8438. // 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.
  8439. for ($j=$maxlevel; $j > 0; $j--) {
  8440. $ordarray = Array();
  8441. $revarr = Array();
  8442. $onlevel = false;
  8443. for ($i=0; $i < $numchars; ++$i) {
  8444. if ($chardata[$i]['level'] >= $j) {
  8445. $onlevel = true;
  8446. if (isset($unicode_mirror[$chardata[$i]['char']])) {
  8447. // 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.
  8448. $chardata[$i]['char'] = $unicode_mirror[$chardata[$i]['char']];
  8449. }
  8450. $revarr[] = $chardata[$i];
  8451. } else {
  8452. if ($onlevel) {
  8453. $revarr = array_reverse($revarr);
  8454. $ordarray = array_merge($ordarray, $revarr);
  8455. $revarr = Array();
  8456. $onlevel = false;
  8457. }
  8458. $ordarray[] = $chardata[$i];
  8459. }
  8460. }
  8461. if ($onlevel) {
  8462. $revarr = array_reverse($revarr);
  8463. $ordarray = array_merge($ordarray, $revarr);
  8464. }
  8465. $chardata = $ordarray;
  8466. }
  8467. $ordarray = array();
  8468. for ($i=0; $i < $numchars; ++$i) {
  8469. $ordarray[] = $chardata[$i]['char'];
  8470. }
  8471. return $ordarray;
  8472. }
  8473. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  8474. /*
  8475. * Adds a bookmark.
  8476. * @param string $txt bookmark description.
  8477. * @param int $level bookmark level (minimum value is 0).
  8478. * @param float $y Ordinate of the boorkmark position (default = -1 = current position).
  8479. * @param int $page target page number (leave empty for current page).
  8480. * @access public
  8481. * @author Olivier Plathey, Nicola Asuni
  8482. * @since 2.1.002 (2008-02-12)
  8483. */
  8484. public function Bookmark($txt, $level=0, $y=-1, $page='') {
  8485. if ($level < 0) {
  8486. $level = 0;
  8487. }
  8488. if (isset($this->outlines[0])) {
  8489. $lastoutline = end($this->outlines);
  8490. $maxlevel = $lastoutline['l'] + 1;
  8491. } else {
  8492. $maxlevel = 0;
  8493. }
  8494. if ($level > $maxlevel) {
  8495. $level = $maxlevel;
  8496. }
  8497. if ($y == -1) {
  8498. $y = $this->GetY();
  8499. }
  8500. if (empty($page)) {
  8501. $page = $this->PageNo();
  8502. }
  8503. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page);
  8504. }
  8505. /*
  8506. * Create a bookmark PDF string.
  8507. * @access protected
  8508. * @author Olivier Plathey, Nicola Asuni
  8509. * @since 2.1.002 (2008-02-12)
  8510. */
  8511. protected function _putbookmarks() {
  8512. $nb = count($this->outlines);
  8513. if ($nb == 0) {
  8514. return;
  8515. }
  8516. $lru = array();
  8517. $level = 0;
  8518. foreach ($this->outlines as $i => $o) {
  8519. if ($o['l'] > 0) {
  8520. $parent = $lru[($o['l'] - 1)];
  8521. //Set parent and last pointers
  8522. $this->outlines[$i]['parent'] = $parent;
  8523. $this->outlines[$parent]['last'] = $i;
  8524. if ($o['l'] > $level) {
  8525. //Level increasing: set first pointer
  8526. $this->outlines[$parent]['first'] = $i;
  8527. }
  8528. } else {
  8529. $this->outlines[$i]['parent'] = $nb;
  8530. }
  8531. if (($o['l'] <= $level) AND ($i > 0)) {
  8532. //Set prev and next pointers
  8533. $prev = $lru[$o['l']];
  8534. $this->outlines[$prev]['next'] = $i;
  8535. $this->outlines[$i]['prev'] = $prev;
  8536. }
  8537. $lru[$o['l']] = $i;
  8538. $level = $o['l'];
  8539. }
  8540. //Outline items
  8541. $n = $this->n + 1;
  8542. foreach ($this->outlines as $i => $o) {
  8543. $this->_newobj();
  8544. $this->_out('<</Title '.$this->_textstring($o['t']));
  8545. $this->_out('/Parent '.($n + $o['parent']).' 0 R');
  8546. if (isset($o['prev']))
  8547. $this->_out('/Prev '.($n + $o['prev']).' 0 R');
  8548. if (isset($o['next']))
  8549. $this->_out('/Next '.($n + $o['next']).' 0 R');
  8550. if (isset($o['first']))
  8551. $this->_out('/First '.($n + $o['first']).' 0 R');
  8552. if (isset($o['last']))
  8553. $this->_out('/Last '.($n + $o['last']).' 0 R');
  8554. $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $o['p'])), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k))));
  8555. $this->_out('/Count 0>>');
  8556. $this->_out('endobj');
  8557. }
  8558. //Outline root
  8559. $this->_newobj();
  8560. $this->OutlineRoot = $this->n;
  8561. $this->_out('<</Type /Outlines /First '.$n.' 0 R');
  8562. $this->_out('/Last '.($n + $lru[0]).' 0 R>>');
  8563. $this->_out('endobj');
  8564. }
  8565. // --- JAVASCRIPT - FORMS ------------------------------
  8566. /*
  8567. * Adds a javascript
  8568. * @access public
  8569. * @author Johannes Güntert, Nicola Asuni
  8570. * @since 2.1.002 (2008-02-12)
  8571. */
  8572. public function IncludeJS($script) {
  8573. $this->javascript .= $script;
  8574. }
  8575. /*
  8576. * Create a javascript PDF string.
  8577. * @access protected
  8578. * @author Johannes Güntert, Nicola Asuni
  8579. * @since 2.1.002 (2008-02-12)
  8580. */
  8581. protected function _putjavascript() {
  8582. if (empty($this->javascript)) {
  8583. return;
  8584. }
  8585. // the following two lines are uded to avoid form fields duplication after saving
  8586. $js1 = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  8587. $js2 = "getField('tcpdfdocsaved').value = 'saved';";
  8588. $this->_newobj();
  8589. $this->n_js = $this->n;
  8590. $this->_out('<<');
  8591. $this->_out('/Names [(EmbeddedJS) '.($this->n + 1).' 0 R ]');
  8592. $this->_out('>>');
  8593. $this->_out('endobj');
  8594. $this->_newobj();
  8595. $this->_out('<<');
  8596. $this->_out('/S /JavaScript');
  8597. $this->_out('/JS '.$this->_textstring($js1."\n".$this->javascript."\n".$js2));
  8598. $this->_out('>>');
  8599. $this->_out('endobj');
  8600. }
  8601. /*
  8602. * Convert color to javascript color.
  8603. * @param string $color color name or #RRGGBB
  8604. * @access protected
  8605. * @author Denis Van Nuffelen, Nicola Asuni
  8606. * @since 2.1.002 (2008-02-12)
  8607. */
  8608. protected function _JScolor($color) {
  8609. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  8610. if (substr($color,0,1) == '#') {
  8611. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  8612. }
  8613. if (!in_array($color,$aColors)) {
  8614. $this->Error('Invalid color: '.$color);
  8615. }
  8616. return 'color.'.$color;
  8617. }
  8618. /*
  8619. * Adds a javascript form field.
  8620. * @param string $type field type
  8621. * @param string $name field name
  8622. * @param int $x horizontal position
  8623. * @param int $y vertical position
  8624. * @param int $w width
  8625. * @param int $h height
  8626. * @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>
  8627. * @access protected
  8628. * @author Denis Van Nuffelen, Nicola Asuni
  8629. * @since 2.1.002 (2008-02-12)
  8630. */
  8631. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  8632. if ($this->rtl) {
  8633. $x = $x - $w;
  8634. }
  8635. // the followind avoid fields duplication after saving the document
  8636. $this->javascript .= "if(getField('tcpdfdocsaved').value != 'saved') {";
  8637. $k = $this->k;
  8638. $this->javascript .= sprintf("f".$name."=this.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)."\n";
  8639. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  8640. while (list($key, $val) = each($prop)) {
  8641. if (strcmp(substr($key, -5), 'Color') == 0) {
  8642. $val = $this->_JScolor($val);
  8643. } else {
  8644. $val = "'".$val."'";
  8645. }
  8646. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  8647. }
  8648. if ($this->rtl) {
  8649. $this->x -= $w;
  8650. } else {
  8651. $this->x += $w;
  8652. }
  8653. $this->javascript .= '}';
  8654. }
  8655. /*
  8656. * Creates a text field
  8657. * @param string $name field name
  8658. * @param int $w width
  8659. * @param int $h height
  8660. * @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>
  8661. * @access public
  8662. * @author Denis Van Nuffelen, Nicola Asuni
  8663. * @since 2.1.002 (2008-02-12)
  8664. */
  8665. public function TextField($name, $w, $h, $prop=array()) {
  8666. $this->_addfield('text', $name, $this->x, $this->y, $w, $h, $prop);
  8667. }
  8668. /*
  8669. * Creates a RadioButton field
  8670. * @param string $name field name
  8671. * @param int $w width
  8672. * @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>
  8673. * @access public
  8674. * @author Nicola Asuni
  8675. * @since 2.2.003 (2008-03-03)
  8676. */
  8677. public function RadioButton($name, $w, $prop=array()) {
  8678. if (!isset($prop['strokeColor'])) {
  8679. $prop['strokeColor']='black';
  8680. }
  8681. $this->_addfield('radiobutton', $name, $this->x, $this->y, $w, $w, $prop);
  8682. }
  8683. /*
  8684. * Creates a List-box field
  8685. * @param string $name field name
  8686. * @param int $w width
  8687. * @param int $h height
  8688. * @param array $values array containing the list of values.
  8689. * @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>
  8690. * @access public
  8691. * @author Nicola Asuni
  8692. * @since 2.2.003 (2008-03-03)
  8693. */
  8694. public function ListBox($name, $w, $h, $values, $prop=array()) {
  8695. if (!isset($prop['strokeColor'])) {
  8696. $prop['strokeColor'] = 'ltGray';
  8697. }
  8698. $this->_addfield('listbox', $name, $this->x, $this->y, $w, $h, $prop);
  8699. $s = '';
  8700. foreach ($values as $value) {
  8701. $s .= "'".addslashes($value)."',";
  8702. }
  8703. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  8704. }
  8705. /*
  8706. * Creates a Combo-box field
  8707. * @param string $name field name
  8708. * @param int $w width
  8709. * @param int $h height
  8710. * @param array $values array containing the list of values.
  8711. * @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>
  8712. * @access public
  8713. * @author Denis Van Nuffelen, Nicola Asuni
  8714. * @since 2.1.002 (2008-02-12)
  8715. */
  8716. public function ComboBox($name, $w, $h, $values, $prop=array()) {
  8717. $this->_addfield('combobox', $name, $this->x, $this->y, $w, $h, $prop);
  8718. $s = '';
  8719. foreach ($values as $value) {
  8720. $s .= "'".addslashes($value)."',";
  8721. }
  8722. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  8723. }
  8724. /*
  8725. * Creates a CheckBox field
  8726. * @param string $name field name
  8727. * @param int $w width
  8728. * @param boolean $checked define the initial state (default = false).
  8729. * @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>
  8730. * @access public
  8731. * @author Denis Van Nuffelen, Nicola Asuni
  8732. * @since 2.1.002 (2008-02-12)
  8733. */
  8734. public function CheckBox($name, $w, $checked=false, $prop=array()) {
  8735. $prop['value'] = ($checked ? 'Yes' : 'Off');
  8736. if (!isset($prop['strokeColor'])) {
  8737. $prop['strokeColor'] = 'black';
  8738. }
  8739. $this->_addfield('checkbox', $name, $this->x, $this->y, $w, $w, $prop);
  8740. }
  8741. /*
  8742. * Creates a button field
  8743. * @param string $name field name
  8744. * @param int $w width
  8745. * @param int $h height
  8746. * @param string $caption caption.
  8747. * @param string $action action triggered by the button (JavaScript code).
  8748. * @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>
  8749. * @access public
  8750. * @author Denis Van Nuffelen, Nicola Asuni
  8751. * @since 2.1.002 (2008-02-12)
  8752. */
  8753. public function Button($name, $w, $h, $caption, $action, $prop=array()) {
  8754. if (!isset($prop['strokeColor'])) {
  8755. $prop['strokeColor'] = 'black';
  8756. }
  8757. if (!isset($prop['borderStyle'])) {
  8758. $prop['borderStyle'] = 'beveled';
  8759. }
  8760. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  8761. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  8762. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  8763. $this->javascript .= 'f'.$name.".highlight='push';\n";
  8764. $this->javascript .= 'f'.$name.".print=false;\n";
  8765. }
  8766. // END JAVASCRIPT - FORMS ------------------------------
  8767. /*
  8768. * Enable Write permissions for PDF Reader.
  8769. * WARNING: This works only using the Adobe private key with the setSignature() method.
  8770. * @access protected
  8771. * @author Nicola Asuni
  8772. * @since 2.9.000 (2008-03-26)
  8773. */
  8774. protected function _putuserrights() {
  8775. if ((!$this->sign) OR (isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] > 0))) {
  8776. return;
  8777. }
  8778. $this->_out('/Perms');
  8779. $this->_out('<<');
  8780. $this->_out('/UR3');
  8781. $this->_out('<<');
  8782. $this->_out('/Type/Sig');
  8783. $this->_out('/Filter/Adobe.PPKLite');
  8784. $this->_out('/SubFilter/adbe.pkcs7.detached');
  8785. $this->_out('/ByteRange[0 ********** ********** **********]');
  8786. $this->_out('/Contents<>'.str_repeat(' ', $this->signature_max_lenght));
  8787. if ($this->ur) {
  8788. $this->_out('/Reference');
  8789. $this->_out('[');
  8790. $this->_out('<<');
  8791. $this->_out('/Type/SigRef');
  8792. $this->_out('/TransformMethod/UR3');
  8793. $this->_out('/TransformParams');
  8794. $this->_out('<<');
  8795. $this->_out('/Type/TransformParams');
  8796. $this->_out('/V/2.2');
  8797. if (!$this->empty_string($this->ur_document)) {
  8798. $this->_out('/Document['.$this->ur_document.']');
  8799. }
  8800. if (!$this->empty_string($this->ur_annots)) {
  8801. $this->_out('/Annots['.$this->ur_annots.']');
  8802. }
  8803. if (!$this->empty_string($this->ur_form)) {
  8804. $this->_out('/Form['.$this->ur_form.']');
  8805. }
  8806. if (!$this->empty_string($this->ur_signature)) {
  8807. $this->_out('/Signature['.$this->ur_signature.']');
  8808. }
  8809. $this->_out('>>');
  8810. $this->_out('>>');
  8811. $this->_out(']');
  8812. }
  8813. $this->_out('/M '.$this->_datastring('D:'.date('YmdHisO')));
  8814. $this->_out('>>');
  8815. $this->_out('>>');
  8816. }
  8817. /*
  8818. * Add certification signature (DocMDP)
  8819. * @access protected
  8820. * @author Nicola Asuni
  8821. * @since 4.6.008 (2009-05-07)
  8822. */
  8823. protected function _putcertification() {
  8824. if ((!$this->sign) OR (isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] <= 0))) {
  8825. return;
  8826. }
  8827. $this->_out('/Perms');
  8828. $this->_out('<<');
  8829. $this->_out('/DocMDP');
  8830. $this->_out('<<');
  8831. $this->_out('/Type/Sig');
  8832. $this->_out('/Filter/Adobe.PPKLite');
  8833. $this->_out('/SubFilter/adbe.pkcs7.detached');
  8834. $this->_out('/ByteRange[0 ********** ********** **********]');
  8835. $this->_out('/Contents<>'.str_repeat(' ', $this->signature_max_lenght));
  8836. $this->_out('/Reference');
  8837. $this->_out('[');
  8838. $this->_out('<<');
  8839. $this->_out('/Type/SigRef');
  8840. $this->_out('/TransformMethod/DocMDP');
  8841. $this->_out('/TransformParams');
  8842. $this->_out('<<');
  8843. $this->_out('/Type/TransformParams');
  8844. $this->_out('/V/1.2');
  8845. $this->_out('/P '.$this->signature_data['cert_type'].'');
  8846. $this->_out('>>');
  8847. $this->_out('>>');
  8848. $this->_out(']');
  8849. $this->_out('/M '.$this->_datastring('D:'.date('YmdHisO')));
  8850. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  8851. $this->_out('/Name '.$this->_textstring($this->signature_data['info']['Name']).'');
  8852. }
  8853. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  8854. $this->_out('/Location '.$this->_textstring($this->signature_data['info']['Location']).'');
  8855. }
  8856. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  8857. $this->_out('/Reason '.$this->_textstring($this->signature_data['info']['Reason']).'');
  8858. }
  8859. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  8860. $this->_out('/ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo']).'');
  8861. }
  8862. $this->_out('>>');
  8863. $this->_out('>>');
  8864. }
  8865. /*
  8866. * Set User's Rights for PDF Reader
  8867. * WARNING: This works only using the Adobe private key with the setSignature() method!.
  8868. * Check the PDF Reference 8.7.1 Transform Methods,
  8869. * Table 8.105 Entries in the UR transform parameters dictionary
  8870. * @param boolean $enable if true enable user's rights on PDF reader
  8871. * @param string $document Names specifying additional document-wide usage rights for the document. The only defined value is "/FullSave", which permits a user to save the document along with modified form and/or annotation data.
  8872. * @param string $annots Names specifying additional annotation-related usage rights for the document. Valid names in PDF 1.5 and later are /Create/Delete/Modify/Copy/Import/Export, which permit the user to perform the named operation on annotations.
  8873. * @param string $form Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  8874. * @param string $signature Names specifying additional signature-related usage rights for the document. The only defined value is /Modify, which permits a user to apply a digital signature to an existing signature form field or clear a signed signature form field.
  8875. * @access public
  8876. * @author Nicola Asuni
  8877. * @since 2.9.000 (2008-03-26)
  8878. */
  8879. public function setUserRights(
  8880. $enable=true,
  8881. $document='/FullSave',
  8882. $annots='/Create/Delete/Modify/Copy/Import/Export',
  8883. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  8884. $signature='/Modify') {
  8885. $this->ur = $enable;
  8886. $this->ur_document = $document;
  8887. $this->ur_annots = $annots;
  8888. $this->ur_form = $form;
  8889. $this->ur_signature = $signature;
  8890. }
  8891. /*
  8892. * Enable document signature (requires the OpenSSL Library).
  8893. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  8894. * @param mixed $signing_cert signing certificate (string or filename prefixed with 'file://')
  8895. * @param mixed $private_key private key (string or filename prefixed with 'file://')
  8896. * @param string $private_key_password password
  8897. * @param string $extracerts specifies the name of a file containing a bunch of extra certificates to include in the signature which can for example be used to help the recipient to verify the certificate that you used.
  8898. * @param int $cert_type The access permissions granted for this document. Valid values shall be: 1 = No changes to the document shall be permitted; any change to the document shall invalidate the signature; 2 = Permitted changes shall be filling in forms, instantiating page templates, and signing; other changes shall invalidate the signature; 3 = Permitted changes shall be the same as for 2, as well as annotation creation, deletion, and modification; other changes shall invalidate the signature.
  8899. * @parm array $info array of option information: Name, Location, Reason, ContactInfo.
  8900. * @access public
  8901. * @author Nicola Asuni
  8902. * @since 4.6.005 (2009-04-24)
  8903. */
  8904. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  8905. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.pem -out tcpdf.pem
  8906. $this->sign = true;
  8907. $this->signature_data = array();
  8908. if (strlen($signing_cert) == 0) {
  8909. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.pem';
  8910. }
  8911. if (strlen($private_key) == 0) {
  8912. $private_key = $signing_cert;
  8913. }
  8914. $this->signature_data['signcert'] = $signing_cert;
  8915. $this->signature_data['privkey'] = $private_key;
  8916. $this->signature_data['password'] = $private_key_password;
  8917. $this->signature_data['extracerts'] = $extracerts;
  8918. $this->signature_data['cert_type'] = $cert_type;
  8919. $this->signature_data['info'] = array();
  8920. }
  8921. /*
  8922. * Create a new page group.
  8923. * NOTE: call this function before calling AddPage()
  8924. * @param int $page starting group page (leave empty for next page).
  8925. * @access public
  8926. * @since 3.0.000 (2008-03-27)
  8927. */
  8928. public function startPageGroup($page='') {
  8929. if (empty($page)) {
  8930. $page = $this->page + 1;
  8931. }
  8932. $this->newpagegroup[$page] = true;
  8933. }
  8934. /**
  8935. * Defines an alias for the total number of pages.
  8936. * It will be substituted as the document is closed.
  8937. * @param string $alias The alias.
  8938. * @access public
  8939. * @since 1.4
  8940. * @see getAliasNbPages(), PageNo(), Footer()
  8941. */
  8942. public function AliasNbPages($alias='{nb}') {
  8943. $this->AliasNbPages = $alias;
  8944. }
  8945. /**
  8946. * Returns the string alias used for the total number of pages.
  8947. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  8948. * @return string
  8949. * @access public
  8950. * @since 4.0.018 (2008-08-08)
  8951. * @see AliasNbPages(), PageNo(), Footer()
  8952. */
  8953. public function getAliasNbPages() {
  8954. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  8955. return '{'.$this->AliasNbPages.'}';
  8956. }
  8957. return $this->AliasNbPages;
  8958. }
  8959. /**
  8960. * Defines an alias for the page number.
  8961. * It will be substituted as the document is closed.
  8962. * @param string $alias The alias.
  8963. * @access public
  8964. * @since 4.5.000 (2009-01-02)
  8965. * @see getAliasNbPages(), PageNo(), Footer()
  8966. */
  8967. public function AliasNumPage($alias='{pnb}') {
  8968. //Define an alias for total number of pages
  8969. $this->AliasNumPage = $alias;
  8970. }
  8971. /**
  8972. * Returns the string alias used for the page number.
  8973. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  8974. * @return string
  8975. * @access public
  8976. * @since 4.5.000 (2009-01-02)
  8977. * @see AliasNbPages(), PageNo(), Footer()
  8978. */
  8979. public function getAliasNumPage() {
  8980. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  8981. return '{'.$this->AliasNumPage.'}';
  8982. }
  8983. return $this->AliasNumPage;
  8984. }
  8985. /*
  8986. * Return the current page in the group.
  8987. * @return current page in the group
  8988. * @access public
  8989. * @since 3.0.000 (2008-03-27)
  8990. */
  8991. public function getGroupPageNo() {
  8992. return $this->pagegroups[$this->currpagegroup];
  8993. }
  8994. /**
  8995. * Returns the current group page number formatted as a string.
  8996. * @access public
  8997. * @since 4.3.003 (2008-11-18)
  8998. * @see PaneNo(), formatPageNumber()
  8999. */
  9000. public function getGroupPageNoFormatted() {
  9001. return $this->formatPageNumber($this->getGroupPageNo());
  9002. }
  9003. /*
  9004. * Return the alias of the current page group
  9005. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9006. * (will be replaced by the total number of pages in this group).
  9007. * @return alias of the current page group
  9008. * @access public
  9009. * @since 3.0.000 (2008-03-27)
  9010. */
  9011. public function getPageGroupAlias() {
  9012. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9013. return '{'.$this->currpagegroup.'}';
  9014. }
  9015. return $this->currpagegroup;
  9016. }
  9017. /*
  9018. * Return the alias for the page number on the current page group
  9019. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9020. * (will be replaced by the total number of pages in this group).
  9021. * @return alias of the current page group
  9022. * @access public
  9023. * @since 4.5.000 (2009-01-02)
  9024. */
  9025. public function getPageNumGroupAlias() {
  9026. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9027. return '{'.str_replace('{nb', '{pnb', $this->currpagegroup).'}';
  9028. }
  9029. return str_replace('{nb', '{pnb', $this->currpagegroup);
  9030. }
  9031. /**
  9032. * Format the page numbers.
  9033. * This method can be overriden for custom formats.
  9034. * @param int $num page number
  9035. * @access protected
  9036. * @since 4.2.005 (2008-11-06)
  9037. */
  9038. protected function formatPageNumber($num) {
  9039. return number_format((float)$num, 0, '', '.');
  9040. }
  9041. /**
  9042. * Format the page numbers on the Table Of Content.
  9043. * This method can be overriden for custom formats.
  9044. * @param int $num page number
  9045. * @access protected
  9046. * @since 4.5.001 (2009-01-04)
  9047. * @see addTOC()
  9048. */
  9049. protected function formatTOCPageNumber($num) {
  9050. return number_format((float)$num, 0, '', '.');
  9051. }
  9052. /**
  9053. * Returns the current page number formatted as a string.
  9054. * @access public
  9055. * @since 4.2.005 (2008-11-06)
  9056. * @see PaneNo(), formatPageNumber()
  9057. */
  9058. public function PageNoFormatted() {
  9059. return $this->formatPageNumber($this->PageNo());
  9060. }
  9061. /*
  9062. * Put visibility settings.
  9063. * @access protected
  9064. * @since 3.0.000 (2008-03-27)
  9065. */
  9066. protected function _putocg() {
  9067. $this->_newobj();
  9068. $this->n_ocg_print = $this->n;
  9069. $this->_out('<</Type /OCG /Name '.$this->_textstring('print'));
  9070. $this->_out('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>>');
  9071. $this->_out('endobj');
  9072. $this->_newobj();
  9073. $this->n_ocg_view=$this->n;
  9074. $this->_out('<</Type /OCG /Name '.$this->_textstring('view'));
  9075. $this->_out('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>>');
  9076. $this->_out('endobj');
  9077. }
  9078. /*
  9079. * Set the visibility of the successive elements.
  9080. * This can be useful, for instance, to put a background
  9081. * image or color that will show on screen but won't print.
  9082. * @param string $v visibility mode. Legal values are: all, print, screen.
  9083. * @access public
  9084. * @since 3.0.000 (2008-03-27)
  9085. */
  9086. public function setVisibility($v) {
  9087. if ($this->openMarkedContent) {
  9088. // close existing open marked-content
  9089. $this->_out('EMC');
  9090. $this->openMarkedContent = false;
  9091. }
  9092. switch($v) {
  9093. case 'print': {
  9094. $this->_out('/OC /OC1 BDC');
  9095. $this->openMarkedContent = true;
  9096. break;
  9097. }
  9098. case 'screen': {
  9099. $this->_out('/OC /OC2 BDC');
  9100. $this->openMarkedContent = true;
  9101. break;
  9102. }
  9103. case 'all': {
  9104. $this->_out('');
  9105. break;
  9106. }
  9107. default: {
  9108. $this->Error('Incorrect visibility: '.$v);
  9109. break;
  9110. }
  9111. }
  9112. $this->visibility = $v;
  9113. }
  9114. /*
  9115. * Add transparency parameters to the current extgstate
  9116. * @param array $params parameters
  9117. * @return the number of extgstates
  9118. * @access protected
  9119. * @since 3.0.000 (2008-03-27)
  9120. */
  9121. protected function addExtGState($parms) {
  9122. $n = count($this->extgstates) + 1;
  9123. $this->extgstates[$n]['parms'] = $parms;
  9124. return $n;
  9125. }
  9126. /*
  9127. * Add an extgstate
  9128. * @param array $gs extgstate
  9129. * @access protected
  9130. * @since 3.0.000 (2008-03-27)
  9131. */
  9132. protected function setExtGState($gs) {
  9133. $this->_out(sprintf('/GS%d gs', $gs));
  9134. }
  9135. /*
  9136. * Put extgstates for object transparency
  9137. * @param array $gs extgstate
  9138. * @access protected
  9139. * @since 3.0.000 (2008-03-27)
  9140. */
  9141. protected function _putextgstates() {
  9142. $ne = count($this->extgstates);
  9143. for ($i = 1; $i <= $ne; ++$i) {
  9144. $this->_newobj();
  9145. $this->extgstates[$i]['n'] = $this->n;
  9146. $this->_out('<</Type /ExtGState');
  9147. foreach ($this->extgstates[$i]['parms'] as $k => $v) {
  9148. $this->_out('/'.$k.' '.$v);
  9149. }
  9150. $this->_out('>>');
  9151. $this->_out('endobj');
  9152. }
  9153. }
  9154. /*
  9155. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  9156. * @param float $alpha real value from 0 (transparent) to 1 (opaque)
  9157. * @param string $bm blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  9158. * @access public
  9159. * @since 3.0.000 (2008-03-27)
  9160. */
  9161. public function setAlpha($alpha, $bm='Normal') {
  9162. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm));
  9163. $this->setExtGState($gs);
  9164. }
  9165. /*
  9166. * Set the default JPEG compression quality (1-100)
  9167. * @param int $quality JPEG quality, integer between 1 and 100
  9168. * @access public
  9169. * @since 3.0.000 (2008-03-27)
  9170. */
  9171. public function setJPEGQuality($quality) {
  9172. if (($quality < 1) OR ($quality > 100)) {
  9173. $quality = 75;
  9174. }
  9175. $this->jpeg_quality = intval($quality);
  9176. }
  9177. /*
  9178. * Set the default number of columns in a row for HTML tables.
  9179. * @param int $cols number of columns
  9180. * @access public
  9181. * @since 3.0.014 (2008-06-04)
  9182. */
  9183. public function setDefaultTableColumns($cols=4) {
  9184. $this->default_table_columns = intval($cols);
  9185. }
  9186. /*
  9187. * Set the height of cell repect font height.
  9188. * @param int $h cell proportion respect font height (typical value = 1.25).
  9189. * @access public
  9190. * @since 3.0.014 (2008-06-04)
  9191. */
  9192. public function setCellHeightRatio($h) {
  9193. $this->cell_height_ratio = $h;
  9194. }
  9195. /*
  9196. * return the height of cell repect font height.
  9197. * @access public
  9198. * @since 4.0.012 (2008-07-24)
  9199. */
  9200. public function getCellHeightRatio() {
  9201. return $this->cell_height_ratio;
  9202. }
  9203. /*
  9204. * Set the PDF version (check PDF reference for valid values).
  9205. * Default value is 1.t
  9206. * @access public
  9207. * @since 3.1.000 (2008-06-09)
  9208. */
  9209. public function setPDFVersion($version='1.7') {
  9210. $this->PDFVersion = $version;
  9211. }
  9212. /*
  9213. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  9214. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  9215. * <ul>
  9216. * <li>HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application's tool bars when the document is active. Default value: false.</li>
  9217. * <li>HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application's menu bar when the document is active. Default value: false.</li>
  9218. * <li>HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default value: false.</li>
  9219. * <li>FitWindow boolean (Optional) A flag specifying whether to resize the document's window to fit the size of the first displayed page. Default value: false.</li>
  9220. * <li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li>
  9221. * <li>DisplayDocTitle boolean (Optional; PDF 1.4) A flag specifying whether the window's title bar should display the document title taken from the Title entry of the document information dictionary (see Section 10.2.1, "Document Information Dictionary"). If false, the title bar should instead display the name of the PDF file containing the document. Default value: false.</li>
  9222. * <li>NonFullScreenPageMode name (Optional) The document's page mode, specifying how to display the document on exiting full-screen mode:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>UseOC Optional content group panel visible</li><ul>This entry is meaningful only if the value of the PageMode entry in the catalog dictionary (see Section 3.6.1, "Document Catalog") is FullScreen; it is ignored otherwise. Default value: UseNone.</li>
  9223. * <li>ViewArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9224. * <li>ViewClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9225. * <li>PrintArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be rendered when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9226. * <li>PrintClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9227. * <li>PrintScaling name (Optional; PDF 1.6) The page scaling option to be selected when a print dialog is displayed for this document. Valid values are: <ul><li>None, which indicates that the print dialog should reflect no page scaling</li><li>AppDefault (default), which indicates that applications should use the current print scaling</li><ul></li>
  9228. * <li>Duplex name (Optional; PDF 1.7) The paper handling option to use when printing the file from the print dialog. The following values are valid:<ul><li>Simplex - Print single-sided</li><li>DuplexFlipShortEdge - Duplex and flip on the short edge of the sheet</li><li>DuplexFlipLongEdge - Duplex and flip on the long edge of the sheet</li></ul>Default value: none</li>
  9229. * <li>PickTrayByPDFSize boolean (Optional; PDF 1.7) A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked. Note: This setting has no effect on Mac OS systems, which do not provide the ability to pick the input tray by size.</li>
  9230. * <li>PrintPageRange array (Optional; PDF 1.7) The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. An odd number of integers causes this entry to be ignored. Negative numbers cause the entire array to be ignored. Default value: as defined by PDF viewer application</li>
  9231. * <li>NumCopies integer (Optional; PDF 1.7) The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are ignored. Default value: as defined by PDF viewer application, but typically 1</li>
  9232. * </ul>
  9233. * @param array $preferences array of options.
  9234. * @author Nicola Asuni
  9235. * @access public
  9236. * @since 3.1.000 (2008-06-09)
  9237. */
  9238. public function setViewerPreferences($preferences) {
  9239. $this->viewer_preferences = $preferences;
  9240. }
  9241. /**
  9242. * Paints a linear colour gradient.
  9243. * @param float $x abscissa of the top left corner of the rectangle.
  9244. * @param float $y ordinate of the top left corner of the rectangle.
  9245. * @param float $w width of the rectangle.
  9246. * @param float $h height of the rectangle.
  9247. * @param array $col1 first color (RGB components).
  9248. * @param array $col2 second color (RGB components).
  9249. * @param array $coords array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg). The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
  9250. * @author Andreas Würmser, Nicola Asuni
  9251. * @since 3.1.000 (2008-06-09)
  9252. * @access public
  9253. */
  9254. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  9255. $this->Clip($x, $y, $w, $h);
  9256. $this->Gradient(2, $col1, $col2, $coords);
  9257. }
  9258. /**
  9259. * Paints a radial colour gradient.
  9260. * @param float $x abscissa of the top left corner of the rectangle.
  9261. * @param float $y ordinate of the top left corner of the rectangle.
  9262. * @param float $w width of the rectangle.
  9263. * @param float $h height of the rectangle.
  9264. * @param array $col1 first color (RGB components).
  9265. * @param array $col2 second color (RGB components).
  9266. * @param array $coords array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1, (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg). (fx, fy) should be inside the circle, otherwise some areas will not be defined.
  9267. * @author Andreas Würmser, Nicola Asuni
  9268. * @since 3.1.000 (2008-06-09)
  9269. * @access public
  9270. */
  9271. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  9272. $this->Clip($x, $y, $w, $h);
  9273. $this->Gradient(3, $col1, $col2, $coords);
  9274. }
  9275. /**
  9276. * Paints a coons patch mesh.
  9277. * @param float $x abscissa of the top left corner of the rectangle.
  9278. * @param float $y ordinate of the top left corner of the rectangle.
  9279. * @param float $w width of the rectangle.
  9280. * @param float $h height of the rectangle.
  9281. * @param array $col1 first color (lower left corner) (RGB components).
  9282. * @param array $col2 second color (lower right corner) (RGB components).
  9283. * @param array $col3 third color (upper right corner) (RGB components).
  9284. * @param array $col4 fourth color (upper left corner) (RGB components).
  9285. * @param array $coords <ul><li>for one patch mesh: array(float x1, float y1, .... float x12, float y12): 12 pairs of coordinates (normally from 0 to 1) which specify the Bezier control points that define the patch. First pair is the lower left edge point, next is its right control point (control point 2). Then the other points are defined in the order: control point 1, edge point, control point 2 going counter-clockwise around the patch. Last (x12, y12) is the first edge point's left control point (control point 1).</li><li>for two or more patch meshes: array[number of patches]: arrays with the following keys for each patch: f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left of precedent patch - I didn't figure this out completely - just try and error ;-) points: 12 pairs of coordinates of the Bezier control points as above for the first patch, 8 pairs of coordinates for the following patches, ignoring the coordinates already defined by the precedent patch (I also didn't figure out the order of these - also: try and see what's happening) colors: must be 4 colors for the first patch, 2 colors for the following patches</li></ul>
  9286. * @param array $coords_min minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
  9287. * @param array $coords_max maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
  9288. * @author Andreas Würmser, Nicola Asuni
  9289. * @since 3.1.000 (2008-06-09)
  9290. * @access public
  9291. */
  9292. public function CoonsPatchMesh($x, $y, $w, $h, $col1=array(), $col2=array(), $col3=array(), $col4=array(), $coords=array(0.00,0.0,0.33,0.00,0.67,0.00,1.00,0.00,1.00,0.33,1.00,0.67,1.00,1.00,0.67,1.00,0.33,1.00,0.00,1.00,0.00,0.67,0.00,0.33), $coords_min=0, $coords_max=1) {
  9293. $this->Clip($x, $y, $w, $h);
  9294. $n = count($this->gradients) + 1;
  9295. $this->gradients[$n]['type'] = 6; //coons patch mesh
  9296. //check the coords array if it is the simple array or the multi patch array
  9297. if (!isset($coords[0]['f'])) {
  9298. //simple array -> convert to multi patch array
  9299. if (!isset($col1[1])) {
  9300. $col1[1] = $col1[2] = $col1[0];
  9301. }
  9302. if (!isset($col2[1])) {
  9303. $col2[1] = $col2[2] = $col2[0];
  9304. }
  9305. if (!isset($col3[1])) {
  9306. $col3[1] = $col3[2] = $col3[0];
  9307. }
  9308. if (!isset($col4[1])) {
  9309. $col4[1] = $col4[2] = $col4[0];
  9310. }
  9311. $patch_array[0]['f'] = 0;
  9312. $patch_array[0]['points'] = $coords;
  9313. $patch_array[0]['colors'][0]['r'] = $col1[0];
  9314. $patch_array[0]['colors'][0]['g'] = $col1[1];
  9315. $patch_array[0]['colors'][0]['b'] = $col1[2];
  9316. $patch_array[0]['colors'][1]['r'] = $col2[0];
  9317. $patch_array[0]['colors'][1]['g'] = $col2[1];
  9318. $patch_array[0]['colors'][1]['b'] = $col2[2];
  9319. $patch_array[0]['colors'][2]['r'] = $col3[0];
  9320. $patch_array[0]['colors'][2]['g'] = $col3[1];
  9321. $patch_array[0]['colors'][2]['b'] = $col3[2];
  9322. $patch_array[0]['colors'][3]['r'] = $col4[0];
  9323. $patch_array[0]['colors'][3]['g'] = $col4[1];
  9324. $patch_array[0]['colors'][3]['b'] = $col4[2];
  9325. } else {
  9326. //multi patch array
  9327. $patch_array = $coords;
  9328. }
  9329. $bpcd = 65535; //16 BitsPerCoordinate
  9330. //build the data stream
  9331. $this->gradients[$n]['stream'] = '';
  9332. $count_patch = count($patch_array);
  9333. for ($i=0; $i < $count_patch; ++$i) {
  9334. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  9335. $count_points = count($patch_array[$i]['points']);
  9336. for ($j=0; $j < $count_points; ++$j) {
  9337. //each point as 16 bit
  9338. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  9339. if ($patch_array[$i]['points'][$j] < 0) {
  9340. $patch_array[$i]['points'][$j] = 0;
  9341. }
  9342. if ($patch_array[$i]['points'][$j] > $bpcd) {
  9343. $patch_array[$i]['points'][$j] = $bpcd;
  9344. }
  9345. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  9346. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  9347. }
  9348. $count_cols = count($patch_array[$i]['colors']);
  9349. for ($j=0; $j < $count_cols; ++$j) {
  9350. //each color component as 8 bit
  9351. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  9352. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  9353. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  9354. }
  9355. }
  9356. //paint the gradient
  9357. $this->_out('/Sh'.$n.' sh');
  9358. //restore previous Graphic State
  9359. $this->_out('Q');
  9360. }
  9361. /**
  9362. * Set a rectangular clipping area.
  9363. * @param float $x abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  9364. * @param float $y ordinate of the top left corner of the rectangle.
  9365. * @param float $w width of the rectangle.
  9366. * @param float $h height of the rectangle.
  9367. * @author Andreas Würmser, Nicola Asuni
  9368. * @since 3.1.000 (2008-06-09)
  9369. * @access protected
  9370. */
  9371. protected function Clip($x, $y, $w, $h) {
  9372. if ($this->rtl) {
  9373. $x = $this->w - $x - $w;
  9374. }
  9375. //save current Graphic State
  9376. $s = 'q';
  9377. //set clipping area
  9378. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  9379. //set up transformation matrix for gradient
  9380. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  9381. $this->_out($s);
  9382. }
  9383. /**
  9384. * Output gradient.
  9385. * @param int $type type of gradient.
  9386. * @param array $col1 first color (RGB components).
  9387. * @param array $col2 second color (RGB components).
  9388. * @param array $coords array of coordinates.
  9389. * @author Andreas Würmser, Nicola Asuni
  9390. * @since 3.1.000 (2008-06-09)
  9391. * @access protected
  9392. */
  9393. protected function Gradient($type, $col1, $col2, $coords) {
  9394. $n = count($this->gradients) + 1;
  9395. $this->gradients[$n]['type'] = $type;
  9396. if (!isset($col1[1])) {
  9397. $col1[1]=$col1[2]=$col1[0];
  9398. }
  9399. $this->gradients[$n]['col1'] = sprintf('%.3F %.3F %.3F', ($col1[0]/255), ($col1[1]/255), ($col1[2]/255));
  9400. if (!isset($col2[1])) {
  9401. $col2[1] = $col2[2] = $col2[0];
  9402. }
  9403. $this->gradients[$n]['col2'] = sprintf('%.3F %.3F %.3F', ($col2[0]/255), ($col2[1]/255), ($col2[2]/255));
  9404. $this->gradients[$n]['coords'] = $coords;
  9405. //paint the gradient
  9406. $this->_out('/Sh'.$n.' sh');
  9407. //restore previous Graphic State
  9408. $this->_out('Q');
  9409. }
  9410. /**
  9411. * Output shaders.
  9412. * @author Andreas Würmser, Nicola Asuni
  9413. * @since 3.1.000 (2008-06-09)
  9414. * @access protected
  9415. */
  9416. function _putshaders() {
  9417. foreach ($this->gradients as $id => $grad) {
  9418. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  9419. $this->_newobj();
  9420. $this->_out('<<');
  9421. $this->_out('/FunctionType 2');
  9422. $this->_out('/Domain [0.0 1.0]');
  9423. $this->_out('/C0 ['.$grad['col1'].']');
  9424. $this->_out('/C1 ['.$grad['col2'].']');
  9425. $this->_out('/N 1');
  9426. $this->_out('>>');
  9427. $this->_out('endobj');
  9428. $f1 = $this->n;
  9429. }
  9430. $this->_newobj();
  9431. $this->_out('<<');
  9432. $this->_out('/ShadingType '.$grad['type']);
  9433. $this->_out('/ColorSpace /DeviceRGB');
  9434. if ($grad['type'] == 2) {
  9435. $this->_out(sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]));
  9436. $this->_out('/Function '.$f1.' 0 R');
  9437. $this->_out('/Extend [true true] ');
  9438. $this->_out('>>');
  9439. } elseif ($grad['type'] == 3) {
  9440. //x0, y0, r0, x1, y1, r1
  9441. //at this this time radius of inner circle is 0
  9442. $this->_out(sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]));
  9443. $this->_out('/Function '.$f1.' 0 R');
  9444. $this->_out('/Extend [true true] ');
  9445. $this->_out('>>');
  9446. } elseif ($grad['type'] == 6) {
  9447. $this->_out('/BitsPerCoordinate 16');
  9448. $this->_out('/BitsPerComponent 8');
  9449. $this->_out('/Decode[0 1 0 1 0 1 0 1 0 1]');
  9450. $this->_out('/BitsPerFlag 8');
  9451. $this->_out('/Length '.strlen($grad['stream']));
  9452. $this->_out('>>');
  9453. $this->_putstream($grad['stream']);
  9454. }
  9455. $this->_out('endobj');
  9456. $this->gradients[$id]['id'] = $this->n;
  9457. }
  9458. }
  9459. /**
  9460. * Output an arc
  9461. * @author Maxime Delorme, Nicola Asuni
  9462. * @since 3.1.000 (2008-06-09)
  9463. * @access protected
  9464. */
  9465. protected function _outarc($x1, $y1, $x2, $y2, $x3, $y3 ) {
  9466. $h = $this->h;
  9467. $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c', $x1*$this->k, ($h-$y1)*$this->k, $x2*$this->k, ($h-$y2)*$this->k, $x3*$this->k, ($h-$y3)*$this->k));
  9468. }
  9469. /**
  9470. * Draw the sector of a circle.
  9471. * It can be used for instance to render pie charts.
  9472. * @param float $xc abscissa of the center.
  9473. * @param float $yc ordinate of the center.
  9474. * @param float $r radius.
  9475. * @param float $a start angle (in degrees).
  9476. * @param float $b end angle (in degrees).
  9477. * @param string $style: D, F, FD or DF (draw, fill, fill and draw). Default: FD.
  9478. * @param float $cw: indicates whether to go clockwise (default: true).
  9479. * @param float $o: origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
  9480. * @author Maxime Delorme, Nicola Asuni
  9481. * @since 3.1.000 (2008-06-09)
  9482. * @access public
  9483. */
  9484. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  9485. if ($this->rtl) {
  9486. $xc = $this->w - $xc;
  9487. }
  9488. if ($cw) {
  9489. $d = $b;
  9490. $b = $o - $a;
  9491. $a = $o - $d;
  9492. } else {
  9493. $b += $o;
  9494. $a += $o;
  9495. }
  9496. $a = ($a % 360) + 360;
  9497. $b = ($b % 360) + 360;
  9498. if ($a > $b) {
  9499. $b +=360;
  9500. }
  9501. $b = $b / 360 * 2 * M_PI;
  9502. $a = $a / 360 * 2 * M_PI;
  9503. $d = $b - $a;
  9504. if ($d == 0 ) {
  9505. $d = 2 * M_PI;
  9506. }
  9507. $k = $this->k;
  9508. $hp = $this->h;
  9509. if ($style=='F') {
  9510. $op = 'f';
  9511. } elseif ($style=='FD' or $style=='DF') {
  9512. $op = 'b';
  9513. } else {
  9514. $op = 's';
  9515. }
  9516. if (sin($d/2)) {
  9517. $MyArc = 4/3 * (1 - cos($d/2)) / sin($d/2) * $r;
  9518. }
  9519. //first put the center
  9520. $this->_out(sprintf('%.2F %.2F m', ($xc)*$k, ($hp-$yc)*$k));
  9521. //put the first point
  9522. $this->_out(sprintf('%.2F %.2F l', ($xc+$r*cos($a))*$k, (($hp-($yc-$r*sin($a)))*$k)));
  9523. //draw the arc
  9524. if ($d < (M_PI/2)) {
  9525. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9526. } else {
  9527. $b = $a + $d/4;
  9528. $MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
  9529. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9530. $a = $b;
  9531. $b = $a + $d/4;
  9532. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9533. $a = $b;
  9534. $b = $a + $d/4;
  9535. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b) );
  9536. $a = $b;
  9537. $b = $a + $d/4;
  9538. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9539. }
  9540. //terminate drawing
  9541. $this->_out($op);
  9542. }
  9543. /**
  9544. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  9545. * Only vector drawing is supported, not text or bitmap.
  9546. * Although the script was successfully tested with various AI format versions, best results are probably achieved with files that were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
  9547. * @param string $file Name of the file containing the image.
  9548. * @param float $x Abscissa of the upper-left corner.
  9549. * @param float $y Ordinate of the upper-left corner.
  9550. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9551. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9552. * @param mixed $link URL or identifier returned by AddLink().
  9553. * @param boolean useBoundingBox specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  9554. * @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>
  9555. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  9556. * @param mixed $border Indicates if borders must be drawn around the image. 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>
  9557. * @author Valentin Schmidt, Nicola Asuni
  9558. * @since 3.1.000 (2008-06-09)
  9559. * @access public
  9560. */
  9561. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0) {
  9562. if ($x === '') {
  9563. $x = $this->x;
  9564. }
  9565. if ($y === '') {
  9566. $y = $this->y;
  9567. }
  9568. $k = $this->k;
  9569. $data = file_get_contents($file);
  9570. if ($data === false) {
  9571. $this->Error('EPS file not found: '.$file);
  9572. }
  9573. $regs = array();
  9574. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  9575. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  9576. if (count($regs) > 1) {
  9577. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  9578. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  9579. $versexp = explode(' ', $version_str);
  9580. $version = (float)array_pop($versexp);
  9581. if ($version >= 9) {
  9582. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  9583. }
  9584. }
  9585. }
  9586. // strip binary bytes in front of PS-header
  9587. $start = strpos($data, '%!PS-Adobe');
  9588. if ($start > 0) {
  9589. $data = substr($data, $start);
  9590. }
  9591. // find BoundingBox params
  9592. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  9593. if (count($regs) > 1) {
  9594. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  9595. } else {
  9596. $this->Error('No BoundingBox found in EPS file: '.$file);
  9597. }
  9598. $start = strpos($data, '%%EndSetup');
  9599. if ($start === false) {
  9600. $start = strpos($data, '%%EndProlog');
  9601. }
  9602. if ($start === false) {
  9603. $start = strpos($data, '%%BoundingBox');
  9604. }
  9605. $data = substr($data, $start);
  9606. $end = strpos($data, '%%PageTrailer');
  9607. if ($end===false) {
  9608. $end = strpos($data, 'showpage');
  9609. }
  9610. if ($end) {
  9611. $data = substr($data, 0, $end);
  9612. }
  9613. if ($w > 0) {
  9614. $scale_x = $w / (($x2 - $x1) / $k);
  9615. if ($h > 0) {
  9616. $scale_y = $h / (($y2 - $y1) / $k);
  9617. } else {
  9618. $scale_y = $scale_x;
  9619. $h = ($y2 - $y1) / $k * $scale_y;
  9620. }
  9621. } else {
  9622. if ($h > 0) {
  9623. $scale_y = $h / (($y2 - $y1) / $k);
  9624. $scale_x = $scale_y;
  9625. $w = ($x2-$x1) / $k * $scale_x;
  9626. } else {
  9627. $w = ($x2 - $x1) / $k;
  9628. $h = ($y2 - $y1) / $k;
  9629. }
  9630. }
  9631. // Check whether we need a new page first as this does not fit
  9632. if ($this->checkPageBreak($h, $y)) {
  9633. $y = $this->GetY() + $this->cMargin;
  9634. }
  9635. // set bottomcoordinates
  9636. $this->img_rb_y = $y + $h;
  9637. // set alignment
  9638. if ($this->rtl) {
  9639. if ($palign == 'L') {
  9640. $ximg = $this->lMargin;
  9641. // set right side coordinate
  9642. $this->img_rb_x = $ximg + $w;
  9643. } elseif ($palign == 'C') {
  9644. $ximg = ($this->w - $x - $w) / 2;
  9645. // set right side coordinate
  9646. $this->img_rb_x = $ximg + $w;
  9647. } else {
  9648. $ximg = $this->w - $x - $w;
  9649. // set left side coordinate
  9650. $this->img_rb_x = $ximg;
  9651. }
  9652. } else {
  9653. if ($palign == 'R') {
  9654. $ximg = $this->w - $this->rMargin - $w;
  9655. // set left side coordinate
  9656. $this->img_rb_x = $ximg;
  9657. } elseif ($palign == 'C') {
  9658. $ximg = ($this->w - $x - $w) / 2;
  9659. // set right side coordinate
  9660. $this->img_rb_x = $ximg + $w;
  9661. } else {
  9662. $ximg = $x;
  9663. // set right side coordinate
  9664. $this->img_rb_x = $ximg + $w;
  9665. }
  9666. }
  9667. if ($useBoundingBox) {
  9668. $dx = $ximg * $k - $x1;
  9669. $dy = $y * $k - $y1;
  9670. } else {
  9671. $dx = $ximg * $k;
  9672. $dy = $y * $k;
  9673. }
  9674. // save the current graphic state
  9675. $this->_out('q'.$this->epsmarker);
  9676. // translate
  9677. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  9678. // scale
  9679. if (isset($scale_x)) {
  9680. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  9681. }
  9682. // handle pc/unix/mac line endings
  9683. preg_match('/[\r\n]+/s', $data, $regs);
  9684. $lines = explode($regs[0], $data);
  9685. $u=0;
  9686. $cnt = count($lines);
  9687. for ($i=0; $i < $cnt; ++$i) {
  9688. $line = $lines[$i];
  9689. if (($line == '') OR ($line{0} == '%')) {
  9690. continue;
  9691. }
  9692. $len = strlen($line);
  9693. $chunks = explode(' ', $line);
  9694. $cmd = array_pop($chunks);
  9695. // RGB
  9696. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  9697. $b = array_pop($chunks);
  9698. $g = array_pop($chunks);
  9699. $r = array_pop($chunks);
  9700. $this->_out(''.$r.' '.$g.' '.$b.' '.($cmd=='Xa'?'rg':'RG')); //substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
  9701. continue;
  9702. }
  9703. switch ($cmd) {
  9704. case 'm':
  9705. case 'l':
  9706. case 'v':
  9707. case 'y':
  9708. case 'c':
  9709. case 'k':
  9710. case 'K':
  9711. case 'g':
  9712. case 'G':
  9713. case 's':
  9714. case 'S':
  9715. case 'J':
  9716. case 'j':
  9717. case 'w':
  9718. case 'M':
  9719. case 'd':
  9720. case 'n':
  9721. case 'v': {
  9722. $this->_out($line);
  9723. break;
  9724. }
  9725. case 'x': {// custom fill color
  9726. list($c,$m,$y,$k) = $chunks;
  9727. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' k');
  9728. break;
  9729. }
  9730. case 'X': { // custom stroke color
  9731. list($c,$m,$y,$k) = $chunks;
  9732. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' K');
  9733. break;
  9734. }
  9735. case 'Y':
  9736. case 'N':
  9737. case 'V':
  9738. case 'L':
  9739. case 'C': {
  9740. $line{$len-1} = strtolower($cmd);
  9741. $this->_out($line);
  9742. break;
  9743. }
  9744. case 'b':
  9745. case 'B': {
  9746. $this->_out($cmd . '*');
  9747. break;
  9748. }
  9749. case 'f':
  9750. case 'F': {
  9751. if ($u > 0) {
  9752. $isU = false;
  9753. $max = min($i+5, $cnt);
  9754. for ($j=$i+1; $j < $max; ++$j)
  9755. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  9756. if ($isU) {
  9757. $this->_out('f*');
  9758. }
  9759. } else {
  9760. $this->_out('f*');
  9761. }
  9762. break;
  9763. }
  9764. case '*u': {
  9765. ++$u;
  9766. break;
  9767. }
  9768. case '*U': {
  9769. --$u;
  9770. break;
  9771. }
  9772. }
  9773. }
  9774. // restore previous graphic state
  9775. $this->_out($this->epsmarker.'Q');
  9776. if (!empty($border)) {
  9777. $bx = $x;
  9778. $by = $y;
  9779. $this->x = $x;
  9780. $this->y = $y;
  9781. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  9782. $this->x = $bx;
  9783. $this->y = $by;
  9784. }
  9785. if ($link) {
  9786. $this->Link($ximg, $y, $w, $h, $link, 0);
  9787. }
  9788. // set pointer to align the successive text/objects
  9789. switch($align) {
  9790. case 'T':{
  9791. $this->y = $y;
  9792. $this->x = $this->img_rb_x;
  9793. break;
  9794. }
  9795. case 'M':{
  9796. $this->y = $y + round($h/2);
  9797. $this->x = $this->img_rb_x;
  9798. break;
  9799. }
  9800. case 'B':{
  9801. $this->y = $this->img_rb_y;
  9802. $this->x = $this->img_rb_x;
  9803. break;
  9804. }
  9805. case 'N':{
  9806. $this->SetY($this->img_rb_y);
  9807. break;
  9808. }
  9809. default:{
  9810. break;
  9811. }
  9812. }
  9813. $this->endlinex = $this->img_rb_x;
  9814. }
  9815. /**
  9816. * Set document barcode.
  9817. * @param string $bc barcode
  9818. * @access public
  9819. */
  9820. public function setBarcode($bc='') {
  9821. $this->barcode = $bc;
  9822. }
  9823. /**
  9824. * Get current barcode.
  9825. * @return string
  9826. * @access public
  9827. * @since 4.0.012 (2008-07-24)
  9828. */
  9829. public function getBarcode() {
  9830. return $this->barcode;
  9831. }
  9832. /**
  9833. * Print a Linear Barcode.
  9834. * @param string $code code to print
  9835. * @param string $type type of barcode.
  9836. * @param int $x x position in user units
  9837. * @param int $y y position in user units
  9838. * @param int $w width in user units
  9839. * @param int $h height in user units
  9840. * @param float $xres width of the smallest bar in user units
  9841. * @param array $style array of options:<ul><li>string $style['position'] barcode position inside the specified width: L = left (default for LTR); C = center; R = right (default for RTL); S = stretch</li><li>boolean $style['border'] if true prints a border around the barcode</li><li>int $style['padding'] padding to leave around the barcode in user units</li><li>array $style['fgcolor'] color array for bars and text</li><li>mixed $style['bgcolor'] color array for background or false for transparent</li><li>boolean $style["text"] boolean if true prints text below the barcode</li><li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li><li>int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing</li></ul>
  9842. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  9843. * @author Nicola Asuni
  9844. * @since 3.1.000 (2008-06-09)
  9845. * @access public
  9846. */
  9847. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='') {
  9848. if ($this->empty_string($code)) {
  9849. return;
  9850. }
  9851. require_once(dirname(__FILE__).'/barcodes.php');
  9852. // save current graphic settings
  9853. $gvars = $this->getGraphicVars();
  9854. // create new barcode object
  9855. $barcodeobj = new TCPDFBarcode($code, $type);
  9856. $arrcode = $barcodeobj->getBarcodeArray();
  9857. if ($arrcode === false) {
  9858. $this->Error('Error in 1D barcode string');
  9859. }
  9860. // set default values
  9861. if (!isset($style['position'])) {
  9862. if ($this->rtl) {
  9863. $style['position'] = 'R';
  9864. } else {
  9865. $style['position'] = 'L';
  9866. }
  9867. }
  9868. if (!isset($style['padding'])) {
  9869. $style['padding'] = 0;
  9870. }
  9871. if (!isset($style['fgcolor'])) {
  9872. $style['fgcolor'] = array(0,0,0); // default black
  9873. }
  9874. if (!isset($style['bgcolor'])) {
  9875. $style['bgcolor'] = false; // default transparent
  9876. }
  9877. if (!isset($style['border'])) {
  9878. $style['border'] = false;
  9879. }
  9880. if (!isset($style['text'])) {
  9881. $style['text'] = false;
  9882. $fontsize = 0;
  9883. }
  9884. if ($style['text'] AND isset($style['font'])) {
  9885. if (isset($style['fontsize'])) {
  9886. $fontsize = $style['fontsize'];
  9887. } else {
  9888. $fontsize = 0;
  9889. }
  9890. $this->SetFont($style['font'], '', $fontsize);
  9891. }
  9892. if (!isset($style['stretchtext'])) {
  9893. $style['stretchtext'] = 4;
  9894. }
  9895. // set foreground color
  9896. $this->SetDrawColorArray($style['fgcolor']);
  9897. $this->SetTextColorArray($style['fgcolor']);
  9898. if ($this->empty_string($w) OR ($w <= 0)) {
  9899. if ($this->rtl) {
  9900. $w = $this->x - $this->lMargin;
  9901. } else {
  9902. $w = $this->w - $this->rMargin - $this->x;
  9903. }
  9904. }
  9905. if ($this->empty_string($x)) {
  9906. $x = $this->GetX();
  9907. }
  9908. if ($this->rtl) {
  9909. $x = $this->w - $x;
  9910. }
  9911. if ($this->empty_string($y)) {
  9912. $y = $this->GetY();
  9913. }
  9914. if ($this->empty_string($xres)) {
  9915. $xres = 0.4;
  9916. }
  9917. $fbw = ($arrcode['maxw'] * $xres) + (2 * $style['padding']);
  9918. $extraspace = ($this->cell_height_ratio * $fontsize / $this->k) + (2 * $style['padding']);
  9919. if ($this->empty_string($h) OR ($h <= 0)) {
  9920. $h = 10 + $extraspace;
  9921. }
  9922. if ($this->checkPageBreak($h)) {
  9923. $y = $this->y;
  9924. }
  9925. // maximum bar heigth
  9926. $barh = $h - $extraspace;
  9927. switch ($style['position']) {
  9928. case 'L': { // left
  9929. if ($this->rtl) {
  9930. $xpos = $x - $w;
  9931. } else {
  9932. $xpos = $x;
  9933. }
  9934. break;
  9935. }
  9936. case 'C': { // center
  9937. $xdiff = (($w - $fbw) / 2);
  9938. if ($this->rtl) {
  9939. $xpos = $x - $w + $xdiff;
  9940. } else {
  9941. $xpos = $x + $xdiff;
  9942. }
  9943. break;
  9944. }
  9945. case 'R': { // right
  9946. if ($this->rtl) {
  9947. $xpos = $x - $fbw;
  9948. } else {
  9949. $xpos = $x + $w - $fbw;
  9950. }
  9951. break;
  9952. }
  9953. case 'S': { // stretch
  9954. $fbw = $w;
  9955. $xres = ($w - (2 * $style['padding'])) / $arrcode['maxw'];
  9956. if ($this->rtl) {
  9957. $xpos = $x - $w;
  9958. } else {
  9959. $xpos = $x;
  9960. }
  9961. break;
  9962. }
  9963. }
  9964. $xpos_rect = $xpos;
  9965. $xpos = $xpos_rect + $style['padding'];
  9966. $xpos_text = $xpos;
  9967. // barcode is always printed in LTR direction
  9968. $tempRTL = $this->rtl;
  9969. $this->rtl = false;
  9970. // print background color
  9971. if ($style['bgcolor']) {
  9972. $this->Rect($xpos_rect, $y, $fbw, $h, 'DF', '', $style['bgcolor']);
  9973. } elseif ($style['border']) {
  9974. $this->Rect($xpos_rect, $y, $fbw, $h, 'D');
  9975. }
  9976. // print bars
  9977. if ($arrcode !== false) {
  9978. foreach ($arrcode['bcode'] as $k => $v) {
  9979. $bw = ($v['w'] * $xres);
  9980. if ($v['t']) {
  9981. // draw a vertical bar
  9982. $ypos = $y + $style['padding'] + ($v['p'] * $barh / $arrcode['maxh']);
  9983. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  9984. }
  9985. $xpos += $bw;
  9986. }
  9987. }
  9988. // print text
  9989. if ($style['text']) {
  9990. // print text
  9991. $this->x = $xpos_text;
  9992. $this->y = $y + $style['padding'] + $barh;
  9993. $this->Cell(($arrcode['maxw'] * $xres), ($this->cell_height_ratio * $fontsize / $this->k), $code, 0, 0, 'C', 0, '', $style['stretchtext']);
  9994. }
  9995. // restore original direction
  9996. $this->rtl = $tempRTL;
  9997. // restore previous settings
  9998. $this->setGraphicVars($gvars);
  9999. // set bottomcoordinates
  10000. $this->img_rb_y = $y + $h;
  10001. if ($this->rtl) {
  10002. // set left side coordinate
  10003. $this->img_rb_x = ($this->w - $x - $w);
  10004. } else {
  10005. // set right side coordinate
  10006. $this->img_rb_x = $x + $w;
  10007. }
  10008. // set pointer to align the successive text/objects
  10009. switch($align) {
  10010. case 'T':{
  10011. $this->y = $y;
  10012. $this->x = $this->img_rb_x;
  10013. break;
  10014. }
  10015. case 'M':{
  10016. $this->y = $y + round($h/2);
  10017. $this->x = $this->img_rb_x;
  10018. break;
  10019. }
  10020. case 'B':{
  10021. $this->y = $this->img_rb_y;
  10022. $this->x = $this->img_rb_x;
  10023. break;
  10024. }
  10025. case 'N':{
  10026. $this->SetY($this->img_rb_y);
  10027. break;
  10028. }
  10029. default:{
  10030. break;
  10031. }
  10032. }
  10033. }
  10034. /**
  10035. * This function is DEPRECATED, please use the new write1DBarcode() function.
  10036. * @param int $x x position in user units
  10037. * @param int $y y position in user units
  10038. * @param int $w width in user units
  10039. * @param int $h height position in user units
  10040. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  10041. * @param string $style barcode style
  10042. * @param string $font font for text
  10043. * @param int $xres x resolution
  10044. * @param string $code code to print
  10045. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  10046. * @access public
  10047. * @see write1DBarcode()
  10048. */
  10049. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  10050. // convert old settings for the new write1DBarcode() function.
  10051. $xres = 1 / $xres;
  10052. $newstyle = array(
  10053. 'position' => 'L',
  10054. 'border' => false,
  10055. 'padding' => 0,
  10056. 'fgcolor' => array(0,0,0),
  10057. 'bgcolor' => false,
  10058. 'text' => true,
  10059. 'font' => $font,
  10060. 'fontsize' => 8,
  10061. 'stretchtext' => 4
  10062. );
  10063. if ($style & 1) {
  10064. $newstyle['border'] = true;
  10065. }
  10066. if ($style & 2) {
  10067. $newstyle['bgcolor'] = false;
  10068. }
  10069. if ($style & 4) {
  10070. $newstyle['position'] = 'C';
  10071. } elseif ($style & 8) {
  10072. $newstyle['position'] = 'L';
  10073. } elseif ($style & 16) {
  10074. $newstyle['position'] = 'R';
  10075. }
  10076. if ($style & 128) {
  10077. $newstyle['text'] = true;
  10078. }
  10079. if ($style & 256) {
  10080. $newstyle['stretchtext'] = 4;
  10081. }
  10082. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  10083. }
  10084. /**
  10085. * Print 2D Barcode.
  10086. * @param string $code code to print
  10087. * @param string $type type of barcode.
  10088. * @param int $x x position in user units
  10089. * @param int $y y position in user units
  10090. * @param int $w width in user units
  10091. * @param int $h height in user units
  10092. * @param array $style array of options:<ul><li>boolean $style['border'] if true prints a border around the barcode</li><li>int $style['padding'] padding to leave around the barcode in user units</li><li>array $style['fgcolor'] color array for bars and text</li><li>mixed $style['bgcolor'] color array for background or false for transparent</li></ul>
  10093. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  10094. * @author Nicola Asuni
  10095. * @since 4.5.037 (2009-04-07)
  10096. * @access public
  10097. */
  10098. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='') {
  10099. if ($this->empty_string($code)) {
  10100. return;
  10101. }
  10102. require_once(dirname(__FILE__).'/2dbarcodes.php');
  10103. // save current graphic settings
  10104. $gvars = $this->getGraphicVars();
  10105. // create new barcode object
  10106. $barcodeobj = new TCPDF2DBarcode($code, $type);
  10107. $arrcode = $barcodeobj->getBarcodeArray();
  10108. if ($arrcode === false) {
  10109. $this->Error('Error in 2D barcode string');
  10110. }
  10111. // set default values
  10112. if (!isset($style['padding'])) {
  10113. $style['padding'] = 0;
  10114. }
  10115. if (!isset($style['fgcolor'])) {
  10116. $style['fgcolor'] = array(0,0,0); // default black
  10117. }
  10118. if (!isset($style['bgcolor'])) {
  10119. $style['bgcolor'] = false; // default transparent
  10120. }
  10121. if (!isset($style['border'])) {
  10122. $style['border'] = false;
  10123. }
  10124. // set foreground color
  10125. $this->SetDrawColorArray($style['fgcolor']);
  10126. if ($this->empty_string($x)) {
  10127. $x = $this->GetX();
  10128. }
  10129. if ($this->rtl) {
  10130. $x = $this->w - $x;
  10131. }
  10132. if ($this->empty_string($y)) {
  10133. $y = $this->GetY();
  10134. }
  10135. if ($this->empty_string($w) OR ($w <= 0)) {
  10136. if ($this->rtl) {
  10137. $w = $x - $this->lMargin;
  10138. } else {
  10139. $w = $this->w - $this->rMargin - $x;
  10140. }
  10141. }
  10142. if ($this->empty_string($h) OR ($h <= 0)) {
  10143. // 2d barcodes are square by default
  10144. $h = $w;
  10145. }
  10146. if ($this->checkPageBreak($h)) {
  10147. $y = $this->y;
  10148. }
  10149. // calculate barcode size (excluding padding)
  10150. $bw = $w - (2 * $style['padding']);
  10151. $bh = $h - (2 * $style['padding']);
  10152. // calculate starting coordinates
  10153. if ($this->rtl) {
  10154. $xpos = $x - $w;
  10155. } else {
  10156. $xpos = $x;
  10157. }
  10158. $xpos += $style['padding'];
  10159. $ypos = $y + $style['padding'];
  10160. // barcode is always printed in LTR direction
  10161. $tempRTL = $this->rtl;
  10162. $this->rtl = false;
  10163. // print background color
  10164. if ($style['bgcolor']) {
  10165. $this->Rect($x, $y, $w, $h, 'DF', '', $style['bgcolor']);
  10166. } elseif ($style['border']) {
  10167. $this->Rect($x, $y, $w, $h, 'D');
  10168. }
  10169. // print barcode cells
  10170. if ($arrcode !== false) {
  10171. $rows = $arrcode['num_rows'];
  10172. $cols = $arrcode['num_cols'];
  10173. // calculate dimension of single barcode cell
  10174. $cw = $bw / $cols;
  10175. $ch = $bh / $rows;
  10176. // for each row
  10177. for ($r = 0; $r < $rows; ++$r) {
  10178. $xr = $xpos;
  10179. // for each column
  10180. for ($c = 0; $c < $cols; ++$c) {
  10181. if ($arrcode['bcode'][$r][$c] == 1) {
  10182. // draw a single barcode cell
  10183. $this->Rect($xr, $ypos, $cw, $ch, 'F', array(), $style['fgcolor']);
  10184. }
  10185. $xr += $cw;
  10186. }
  10187. $ypos += $ch;
  10188. }
  10189. }
  10190. // restore original direction
  10191. $this->rtl = $tempRTL;
  10192. // restore previous settings
  10193. $this->setGraphicVars($gvars);
  10194. // set bottomcoordinates
  10195. $this->img_rb_y = $y + $h;
  10196. if ($this->rtl) {
  10197. // set left side coordinate
  10198. $this->img_rb_x = ($this->w - $x - $w);
  10199. } else {
  10200. // set right side coordinate
  10201. $this->img_rb_x = $x + $w;
  10202. }
  10203. // set pointer to align the successive text/objects
  10204. switch($align) {
  10205. case 'T':{
  10206. $this->y = $y;
  10207. $this->x = $this->img_rb_x;
  10208. break;
  10209. }
  10210. case 'M':{
  10211. $this->y = $y + round($h/2);
  10212. $this->x = $this->img_rb_x;
  10213. break;
  10214. }
  10215. case 'B':{
  10216. $this->y = $this->img_rb_y;
  10217. $this->x = $this->img_rb_x;
  10218. break;
  10219. }
  10220. case 'N':{
  10221. $this->SetY($this->img_rb_y);
  10222. break;
  10223. }
  10224. default:{
  10225. break;
  10226. }
  10227. }
  10228. }
  10229. /**
  10230. * Returns an array containing current margins:
  10231. * <ul>
  10232. <li>$ret['left'] = left margin</li>
  10233. <li>$ret['right'] = right margin</li>
  10234. <li>$ret['top'] = top margin</li>
  10235. <li>$ret['bottom'] = bottom margin</li>
  10236. <li>$ret['header'] = header margin</li>
  10237. <li>$ret['footer'] = footer margin</li>
  10238. <li>$ret['cell'] = cell margin</li>
  10239. * </ul>
  10240. * @return array containing all margins measures
  10241. * @access public
  10242. * @since 3.2.000 (2008-06-23)
  10243. */
  10244. public function getMargins() {
  10245. $ret = array(
  10246. 'left' => $this->lMargin,
  10247. 'right' => $this->rMargin,
  10248. 'top' => $this->tMargin,
  10249. 'bottom' => $this->bMargin,
  10250. 'header' => $this->header_margin,
  10251. 'footer' => $this->footer_margin,
  10252. 'cell' => $this->cMargin,
  10253. );
  10254. return $ret;
  10255. }
  10256. /**
  10257. * Returns an array containing original margins:
  10258. * <ul>
  10259. <li>$ret['left'] = left margin</li>
  10260. <li>$ret['right'] = right margin</li>
  10261. * </ul>
  10262. * @return array containing all margins measures
  10263. * @access public
  10264. * @since 4.0.012 (2008-07-24)
  10265. */
  10266. public function getOriginalMargins() {
  10267. $ret = array(
  10268. 'left' => $this->original_lMargin,
  10269. 'right' => $this->original_rMargin
  10270. );
  10271. return $ret;
  10272. }
  10273. /**
  10274. * Returns the current font size.
  10275. * @return current font size
  10276. * @access public
  10277. * @since 3.2.000 (2008-06-23)
  10278. */
  10279. public function getFontSize() {
  10280. return $this->FontSize;
  10281. }
  10282. /**
  10283. * Returns the current font size in points unit.
  10284. * @return current font size in points unit
  10285. * @access public
  10286. * @since 3.2.000 (2008-06-23)
  10287. */
  10288. public function getFontSizePt() {
  10289. return $this->FontSizePt;
  10290. }
  10291. /**
  10292. * Returns the current font family name.
  10293. * @return string current font family name
  10294. * @access public
  10295. * @since 4.3.008 (2008-12-05)
  10296. */
  10297. public function getFontFamily() {
  10298. return $this->FontFamily;
  10299. }
  10300. /**
  10301. * Returns the current font style.
  10302. * @return string current font style
  10303. * @access public
  10304. * @since 4.3.008 (2008-12-05)
  10305. */
  10306. public function getFontStyle() {
  10307. return $this->FontStyle;
  10308. }
  10309. /**
  10310. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  10311. * 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 />
  10312. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  10313. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  10314. * @param float $h Cell minimum height. The cell extends automatically if needed.
  10315. * @param float $x upper-left corner X coordinate
  10316. * @param float $y upper-left corner Y coordinate
  10317. * @param string $html html text to print. Default value: empty string.
  10318. * @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>
  10319. * @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>
  10320. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  10321. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  10322. * @param boolean $reseth if true reset the last cell height (default true).
  10323. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  10324. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  10325. * @access public
  10326. * @uses MultiCell()
  10327. * @see Multicell(), writeHTML()
  10328. */
  10329. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true) {
  10330. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0);
  10331. }
  10332. /**
  10333. * Returns the HTML DOM array.
  10334. * <ul><li>$dom[$key]['tag'] = true if tag, false otherwise;</li><li>$dom[$key]['value'] = tag name or text;</li><li>$dom[$key]['opening'] = true if opening tag, false otherwise;</li><li>$dom[$key]['attribute'] = array of attributes (attribute name is the key);</li><li>$dom[$key]['style'] = array of style attributes (attribute name is the key);</li><li>$dom[$key]['parent'] = id of parent element;</li><li>$dom[$key]['fontname'] = font family name;</li><li>$dom[$key]['fontstyle'] = font style;</li><li>$dom[$key]['fontsize'] = font size in points;</li><li>$dom[$key]['bgcolor'] = RGB array of background color;</li><li>$dom[$key]['fgcolor'] = RGB array of foreground color;</li><li>$dom[$key]['width'] = width in pixels;</li><li>$dom[$key]['height'] = height in pixels;</li><li>$dom[$key]['align'] = text alignment;</li><li>$dom[$key]['cols'] = number of colums in table;</li><li>$dom[$key]['rows'] = number of rows in table;</li></ul>
  10335. * @param string $html html code
  10336. * @return array
  10337. * @access protected
  10338. * @since 3.2.000 (2008-06-20)
  10339. */
  10340. protected function getHtmlDomArray($html) {
  10341. // remove all unsupported tags (the line below lists all supported tags)
  10342. $html = strip_tags($html, '<marker/><a><b><blockquote><br><br/><dd><del><div><dl><dt><em><font><h1><h2><h3><h4><h5><h6><hr><i><img><li><ol><p><pre><small><span><strong><sub><sup><table><tcpdf><td><th><thead><tr><tt><u><ul>');
  10343. //replace some blank characters
  10344. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  10345. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dt|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  10346. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  10347. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  10348. $html = strtr($html, $repTable);
  10349. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html)) {
  10350. // preserve newlines on <pre> tag
  10351. $html = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html);
  10352. }
  10353. $html = str_replace("\n", ' ', $html);
  10354. // remove extra spaces from code
  10355. $html = preg_replace('/[\s]+<\/(table|tr|td|th|ul|ol|li)>/', '</\\1>', $html);
  10356. $html = preg_replace('/[\s]+<(tr|td|th|ul|ol|li|br)/', '<\\1', $html);
  10357. $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
  10358. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  10359. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  10360. $html = preg_replace('/<img/', ' <img', $html);
  10361. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span></span>', $html);
  10362. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  10363. // trim string
  10364. $html = preg_replace('/^[\s]+/', '', $html);
  10365. $html = preg_replace('/[\s]+$/', '', $html);
  10366. // pattern for generic tag
  10367. $tagpattern = '/(<[^>]+>)/';
  10368. // explodes the string
  10369. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  10370. // count elements
  10371. $maxel = count($a);
  10372. $elkey = 0;
  10373. $key = 0;
  10374. // create an array of elements
  10375. $dom = array();
  10376. $dom[$key] = array();
  10377. // set first void element
  10378. $dom[$key]['tag'] = false;
  10379. $dom[$key]['value'] = '';
  10380. $dom[$key]['parent'] = 0;
  10381. $dom[$key]['fontname'] = $this->FontFamily;
  10382. $dom[$key]['fontstyle'] = $this->FontStyle;
  10383. $dom[$key]['fontsize'] = $this->FontSizePt;
  10384. $dom[$key]['bgcolor'] = false;
  10385. $dom[$key]['fgcolor'] = $this->fgcolor;
  10386. $dom[$key]['align'] = '';
  10387. $dom[$key]['listtype'] = '';
  10388. $thead = false; // true when we are inside the THEAD tag
  10389. ++$key;
  10390. $level = array();
  10391. array_push($level, 0); // root
  10392. while ($elkey < $maxel) {
  10393. $dom[$key] = array();
  10394. $element = $a[$elkey];
  10395. $dom[$key]['elkey'] = $elkey;
  10396. if (preg_match($tagpattern, $element)) {
  10397. // html tag
  10398. $element = substr($element, 1, -1);
  10399. // get tag name
  10400. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  10401. $tagname = strtolower($tag[1]);
  10402. // check if we are inside a table header
  10403. if ($tagname == 'thead') {
  10404. if ($element{0} == '/') {
  10405. $thead = false;
  10406. } else {
  10407. $thead = true;
  10408. }
  10409. ++$elkey;
  10410. continue;
  10411. }
  10412. $dom[$key]['tag'] = true;
  10413. $dom[$key]['value'] = $tagname;
  10414. if ($element{0} == '/') {
  10415. // closing html tag
  10416. $dom[$key]['opening'] = false;
  10417. $dom[$key]['parent'] = end($level);
  10418. array_pop($level);
  10419. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  10420. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  10421. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  10422. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  10423. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  10424. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  10425. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  10426. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  10427. }
  10428. // set the number of columns in table tag
  10429. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  10430. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  10431. }
  10432. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  10433. $dom[($dom[$key]['parent'])]['content'] = '';
  10434. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  10435. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  10436. }
  10437. $key = $i;
  10438. }
  10439. // store header rows on a new table
  10440. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] == true)) {
  10441. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  10442. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  10443. }
  10444. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  10445. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  10446. }
  10447. }
  10448. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  10449. $dom[($dom[$key]['parent'])]['thead'] .= '</table>';
  10450. }
  10451. } else {
  10452. // opening html tag
  10453. $dom[$key]['opening'] = true;
  10454. $dom[$key]['parent'] = end($level);
  10455. if (substr($element, -1, 1) != '/') {
  10456. // not self-closing tag
  10457. array_push($level, $key);
  10458. $dom[$key]['self'] = false;
  10459. } else {
  10460. $dom[$key]['self'] = true;
  10461. }
  10462. // copy some values from parent
  10463. $parentkey = 0;
  10464. if ($key > 0) {
  10465. $parentkey = $dom[$key]['parent'];
  10466. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  10467. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  10468. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  10469. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  10470. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  10471. $dom[$key]['align'] = $dom[$parentkey]['align'];
  10472. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  10473. }
  10474. // get attributes
  10475. preg_match_all('/([^=\s]*)=["]?([^"]*)["]?/', $element, $attr_array, PREG_PATTERN_ORDER);
  10476. $dom[$key]['attribute'] = array(); // reset attribute array
  10477. while (list($id, $name) = each($attr_array[1])) {
  10478. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  10479. }
  10480. // split style attributes
  10481. if (isset($dom[$key]['attribute']['style'])) {
  10482. // get style attributes
  10483. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  10484. $dom[$key]['style'] = array(); // reset style attribute array
  10485. while (list($id, $name) = each($style_array[1])) {
  10486. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  10487. }
  10488. // --- get some style attributes ---
  10489. if (isset($dom[$key]['style']['font-family'])) {
  10490. // font family
  10491. if (isset($dom[$key]['style']['font-family'])) {
  10492. $fontslist = split(',', strtolower($dom[$key]['style']['font-family']));
  10493. foreach ($fontslist as $font) {
  10494. $font = trim(strtolower($font));
  10495. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  10496. $dom[$key]['fontname'] = $font;
  10497. break;
  10498. }
  10499. }
  10500. }
  10501. }
  10502. // list-style-type
  10503. if (isset($dom[$key]['style']['list-style-type'])) {
  10504. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  10505. if ($dom[$key]['listtype'] == 'inherit') {
  10506. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  10507. }
  10508. }
  10509. // font size
  10510. if (isset($dom[$key]['style']['font-size'])) {
  10511. $fsize = trim($dom[$key]['style']['font-size']);
  10512. switch ($fsize) {
  10513. // absolute-size
  10514. case 'xx-small': {
  10515. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  10516. break;
  10517. }
  10518. case 'x-small': {
  10519. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  10520. break;
  10521. }
  10522. case 'small': {
  10523. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  10524. break;
  10525. }
  10526. case 'medium': {
  10527. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  10528. break;
  10529. }
  10530. case 'large': {
  10531. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  10532. break;
  10533. }
  10534. case 'x-large': {
  10535. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  10536. break;
  10537. }
  10538. case 'xx-large': {
  10539. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  10540. break;
  10541. }
  10542. // relative-size
  10543. case 'smaller': {
  10544. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  10545. break;
  10546. }
  10547. case 'larger': {
  10548. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  10549. break;
  10550. }
  10551. default: {
  10552. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  10553. }
  10554. }
  10555. }
  10556. // font style
  10557. if (isset($dom[$key]['style']['font-weight']) AND (strtolower($dom[$key]['style']['font-weight']{0}) == 'b')) {
  10558. $dom[$key]['fontstyle'] .= 'B';
  10559. }
  10560. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
  10561. $dom[$key]['fontstyle'] .= '"I';
  10562. }
  10563. // font color
  10564. if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
  10565. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
  10566. }
  10567. // background color
  10568. if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
  10569. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
  10570. }
  10571. // text-decoration
  10572. if (isset($dom[$key]['style']['text-decoration'])) {
  10573. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  10574. foreach ($decors as $dec) {
  10575. $dec = trim($dec);
  10576. if (!$this->empty_string($dec)) {
  10577. if ($dec{0} == 'u') {
  10578. $dom[$key]['fontstyle'] .= 'U';
  10579. } elseif ($dec{0} == 'l') {
  10580. $dom[$key]['fontstyle'] .= 'D';
  10581. }
  10582. }
  10583. }
  10584. }
  10585. // check for width attribute
  10586. if (isset($dom[$key]['style']['width'])) {
  10587. $dom[$key]['width'] = $dom[$key]['style']['width'];
  10588. }
  10589. // check for height attribute
  10590. if (isset($dom[$key]['style']['height'])) {
  10591. $dom[$key]['height'] = $dom[$key]['style']['height'];
  10592. }
  10593. // check for text alignment
  10594. if (isset($dom[$key]['style']['text-align'])) {
  10595. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
  10596. }
  10597. // check for border attribute
  10598. if (isset($dom[$key]['style']['border'])) {
  10599. $dom[$key]['attribute']['border'] = $dom[$key]['style']['border'];
  10600. }
  10601. }
  10602. // check for font tag
  10603. if ($dom[$key]['value'] == 'font') {
  10604. // font family
  10605. if (isset($dom[$key]['attribute']['face'])) {
  10606. $fontslist = split(',', strtolower($dom[$key]['attribute']['face']));
  10607. foreach ($fontslist as $font) {
  10608. $font = trim(strtolower($font));
  10609. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  10610. $dom[$key]['fontname'] = $font;
  10611. break;
  10612. }
  10613. }
  10614. }
  10615. // font size
  10616. if (isset($dom[$key]['attribute']['size'])) {
  10617. if ($key > 0) {
  10618. if ($dom[$key]['attribute']['size']{0} == '+') {
  10619. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  10620. } elseif ($dom[$key]['attribute']['size']{0} == '-') {
  10621. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  10622. } else {
  10623. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  10624. }
  10625. } else {
  10626. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  10627. }
  10628. }
  10629. }
  10630. // force natural alignment for lists
  10631. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  10632. AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  10633. if ($this->rtl) {
  10634. $dom[$key]['align'] = 'R';
  10635. } else {
  10636. $dom[$key]['align'] = 'L';
  10637. }
  10638. }
  10639. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  10640. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  10641. }
  10642. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  10643. $dom[$key]['fontstyle'] .= 'B';
  10644. }
  10645. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  10646. $dom[$key]['fontstyle'] .= 'I';
  10647. }
  10648. if ($dom[$key]['value'] == 'u') {
  10649. $dom[$key]['fontstyle'] .= 'U';
  10650. }
  10651. if ($dom[$key]['value'] == 'del') {
  10652. $dom[$key]['fontstyle'] .= 'D';
  10653. }
  10654. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  10655. $dom[$key]['fontname'] = $this->default_monospaced_font;
  10656. }
  10657. if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
  10658. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  10659. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  10660. $dom[$key]['fontstyle'] .= 'B';
  10661. }
  10662. if (($dom[$key]['value'] == 'table')) {
  10663. $dom[$key]['rows'] = 0; // number of rows
  10664. $dom[$key]['trids'] = array(); // IDs of TR elements
  10665. $dom[$key]['thead'] = ''; // table header rows
  10666. }
  10667. if (($dom[$key]['value'] == 'tr')) {
  10668. $dom[$key]['cols'] = 0;
  10669. // store the number of rows on table element
  10670. ++$dom[($dom[$key]['parent'])]['rows'];
  10671. // store the TR elements IDs on table element
  10672. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  10673. if ($thead) {
  10674. $dom[$key]['thead'] = true;
  10675. } else {
  10676. $dom[$key]['thead'] = false;
  10677. }
  10678. }
  10679. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  10680. if (isset($dom[$key]['attribute']['colspan'])) {
  10681. $colspan = intval($dom[$key]['attribute']['colspan']);
  10682. } else {
  10683. $colspan = 1;
  10684. }
  10685. $dom[$key]['attribute']['colspan'] = $colspan;
  10686. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  10687. }
  10688. // set foreground color attribute
  10689. if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
  10690. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
  10691. }
  10692. // set background color attribute
  10693. if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
  10694. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
  10695. }
  10696. // check for width attribute
  10697. if (isset($dom[$key]['attribute']['width'])) {
  10698. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  10699. }
  10700. // check for height attribute
  10701. if (isset($dom[$key]['attribute']['height'])) {
  10702. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  10703. }
  10704. // check for text alignment
  10705. if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  10706. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
  10707. }
  10708. } // end opening tag
  10709. } else {
  10710. // text
  10711. $dom[$key]['tag'] = false;
  10712. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  10713. $dom[$key]['parent'] = end($level);
  10714. }
  10715. ++$elkey;
  10716. ++$key;
  10717. }
  10718. return $dom;
  10719. }
  10720. /**
  10721. * Allows to preserve some HTML formatting (limited support).<br />
  10722. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  10723. * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, small, span, strong, sub, sup, table, td, th, tr, u, ul,
  10724. * @param string $html text to display
  10725. * @param boolean $ln if true add a new line after text (default = true)
  10726. * @param int $fill Indicates if the background must be painted (true) or transparent (false).
  10727. * @param boolean $reseth if true reset the last cell height (default false).
  10728. * @param boolean $cell if true add the default cMargin space to each Write (default false).
  10729. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  10730. * @access public
  10731. */
  10732. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  10733. $gvars = $this->getGraphicVars();
  10734. // store current values
  10735. $prevPage = $this->page;
  10736. $prevlMargin = $this->lMargin;
  10737. $prevrMargin = $this->rMargin;
  10738. $curfontname = $this->FontFamily;
  10739. $curfontstyle = $this->FontStyle;
  10740. $curfontsize = $this->FontSizePt;
  10741. $this->newline = true;
  10742. $minstartliney = $this->y;
  10743. $yshift = 0;
  10744. $startlinepage = $this->page;
  10745. $newline = true;
  10746. $loop = 0;
  10747. $curpos = 0;
  10748. $blocktags = array('blockquote','br','dd','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','ul','tcpdf');
  10749. $this->premode = false;
  10750. if (isset($this->PageAnnots[$this->page])) {
  10751. $pask = count($this->PageAnnots[$this->page]);
  10752. } else {
  10753. $pask = 0;
  10754. }
  10755. if (isset($this->footerlen[$this->page])) {
  10756. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  10757. } else {
  10758. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  10759. }
  10760. $startlinepos = $this->footerpos[$this->page];
  10761. $lalign = $align;
  10762. $plalign = $align;
  10763. if ($this->rtl) {
  10764. $w = $this->x - $this->lMargin;
  10765. } else {
  10766. $w = $this->w - $this->rMargin - $this->x;
  10767. }
  10768. $w -= (2 * $this->cMargin);
  10769. if ($cell) {
  10770. if ($this->rtl) {
  10771. $this->x -= $this->cMargin;
  10772. } else {
  10773. $this->x += $this->cMargin;
  10774. }
  10775. }
  10776. if ($this->customlistindent >= 0) {
  10777. $this->listindent = $this->customlistindent;
  10778. } else {
  10779. $this->listindent = $this->GetStringWidth('0000');
  10780. }
  10781. $this->listnum = 0;
  10782. if (($this->empty_string($this->lasth)) OR ($reseth)) {
  10783. //set row height
  10784. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  10785. }
  10786. $dom = $this->getHtmlDomArray($html);
  10787. $maxel = count($dom);
  10788. $key = 0;
  10789. while ($key < $maxel) {
  10790. if ($dom[$key]['tag'] OR ($key == 0)) {
  10791. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  10792. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  10793. }
  10794. // vertically align image in line
  10795. if ((!$this->newline)
  10796. AND ($dom[$key]['value'] == 'img')
  10797. AND (isset($dom[$key]['attribute']['height']))
  10798. AND ($dom[$key]['attribute']['height'] > 0)) {
  10799. // get image height
  10800. $imgh = $this->getHTMLUnitToUnits($dom[$key]['attribute']['height'], $this->lasth, 'px');
  10801. if (!$this->InFooter) {
  10802. // check for page break
  10803. $this->checkPageBreak($imgh);
  10804. }
  10805. if ($this->page > $startlinepage) {
  10806. // fix lines splitted over two pages
  10807. if (isset($this->footerlen[$startlinepage])) {
  10808. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  10809. }
  10810. // line to be moved one page forward
  10811. $pagebuff = $this->getPageBuffer($startlinepage);
  10812. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  10813. $tstart = substr($pagebuff, 0, $startlinepos);
  10814. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  10815. // remove line start from previous page
  10816. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  10817. $pagebuff = $this->getPageBuffer($this->page);
  10818. $tstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  10819. $tend = substr($pagebuff, $this->intmrk[$this->page]);
  10820. // add line start to current page
  10821. $yshift = $minstartliney - $this->y;
  10822. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  10823. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  10824. // shift the annotations and links
  10825. if (isset($this->PageAnnots[$startlinepage])) {
  10826. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  10827. if ($pak >= $pask) {
  10828. $this->PageAnnots[$this->page][] = $pac;
  10829. unset($this->PageAnnots[$startlinepage][$pak]);
  10830. $npak = count($this->PageAnnots[$this->page]) - 1;
  10831. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  10832. }
  10833. }
  10834. }
  10835. $startlinepos = $this->intmrk[$this->page];
  10836. $startlinepage = $this->page;
  10837. $startliney = $this->y;
  10838. }
  10839. $this->y += (($curfontsize / $this->k) - $imgh);
  10840. $minstartliney = min($this->y, $minstartliney);
  10841. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize'])) {
  10842. // account for different font size
  10843. $pfontname = $curfontname;
  10844. $pfontstyle = $curfontstyle;
  10845. $pfontsize = $curfontsize;
  10846. $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
  10847. $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
  10848. $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
  10849. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)) {
  10850. $this->SetFont($fontname, $fontstyle, $fontsize);
  10851. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  10852. if (is_numeric($fontsize) AND ($fontsize > 0)
  10853. AND is_numeric($curfontsize) AND ($curfontsize > 0)
  10854. AND ($fontsize != $curfontsize) AND (!$this->newline)
  10855. AND ($key < ($maxel - 1))
  10856. ) {
  10857. if ((!$this->newline) AND ($this->page > $startlinepage)) {
  10858. // fix lines splitted over two pages
  10859. if (isset($this->footerlen[$startlinepage])) {
  10860. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  10861. }
  10862. // line to be moved one page forward
  10863. $pagebuff = $this->getPageBuffer($startlinepage);
  10864. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  10865. $tstart = substr($pagebuff, 0, $startlinepos);
  10866. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  10867. // remove line start from previous page
  10868. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  10869. $pagebuff = $this->getPageBuffer($this->page);
  10870. $tstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  10871. $tend = substr($pagebuff, $this->intmrk[$this->page]);
  10872. // add line start to current page
  10873. $yshift = $minstartliney - $this->y;
  10874. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  10875. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  10876. // shift the annotations and links
  10877. if (isset($this->PageAnnots[$startlinepage])) {
  10878. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  10879. if ($pak >= $pask) {
  10880. $this->PageAnnots[$this->page][] = $pac;
  10881. unset($this->PageAnnots[$startlinepage][$pak]);
  10882. $npak = count($this->PageAnnots[$this->page]) - 1;
  10883. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  10884. }
  10885. }
  10886. }
  10887. }
  10888. $this->y += (($curfontsize - $fontsize) / $this->k);
  10889. $minstartliney = min($this->y, $minstartliney);
  10890. }
  10891. $curfontname = $fontname;
  10892. $curfontstyle = $fontstyle;
  10893. $curfontsize = $fontsize;
  10894. }
  10895. }
  10896. if (($plalign == 'J') AND (in_array($dom[$key]['value'], $blocktags))) {
  10897. $plalign = '';
  10898. }
  10899. // get current position on page buffer
  10900. $curpos = $this->pagelen[$startlinepage];
  10901. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  10902. $this->SetFillColorArray($dom[$key]['bgcolor']);
  10903. $wfill = true;
  10904. } else {
  10905. $wfill = $fill | false;
  10906. }
  10907. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  10908. $this->SetTextColorArray($dom[$key]['fgcolor']);
  10909. }
  10910. if (isset($dom[$key]['align'])) {
  10911. $lalign = $dom[$key]['align'];
  10912. }
  10913. if ($this->empty_string($lalign)) {
  10914. $lalign = $align;
  10915. }
  10916. }
  10917. // align lines
  10918. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  10919. $newline = true;
  10920. // we are at the beginning of a new line
  10921. if (isset($startlinex)) {
  10922. $yshift = $minstartliney - $startliney;
  10923. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  10924. $yshift = 0;
  10925. }
  10926. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl))))) OR ($yshift < 0)) {
  10927. // the last line must be shifted to be aligned as requested
  10928. $linew = abs($this->endlinex - $startlinex);
  10929. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  10930. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  10931. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  10932. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  10933. } elseif (isset($opentagpos)) {
  10934. $midpos = $opentagpos;
  10935. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  10936. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  10937. $midpos = $this->footerpos[$startlinepage];
  10938. } else {
  10939. $midpos = 0;
  10940. }
  10941. if ($midpos > 0) {
  10942. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  10943. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  10944. } else {
  10945. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  10946. $pend = '';
  10947. }
  10948. // calculate shifting amount
  10949. $tw = $w;
  10950. if ($this->lMargin != $prevlMargin) {
  10951. $tw += ($prevlMargin - $this->lMargin);
  10952. }
  10953. if ($this->rMargin != $prevrMargin) {
  10954. $tw += ($prevrMargin - $this->rMargin);
  10955. }
  10956. $mdiff = abs($tw - $linew);
  10957. $t_x = 0;
  10958. if ($plalign == 'C') {
  10959. if ($this->rtl) {
  10960. $t_x = -($mdiff / 2);
  10961. } else {
  10962. $t_x = ($mdiff / 2);
  10963. }
  10964. } elseif (($plalign == 'R') AND (!$this->rtl)) {
  10965. // right alignment on LTR document
  10966. $t_x = $mdiff;
  10967. } elseif (($plalign == 'L') AND ($this->rtl)) {
  10968. // left alignment on RTL document
  10969. $t_x = -$mdiff;
  10970. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  10971. // Justification
  10972. if ($this->rtl OR $this->tmprtl) {
  10973. $t_x = $this->lMargin - $this->endlinex;
  10974. }
  10975. $no = 0;
  10976. $ns = 0;
  10977. $pmidtemp = $pmid;
  10978. // escape special characters
  10979. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  10980. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  10981. // search spaces
  10982. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  10983. $maxkk = count($lnstring[1]) - 1;
  10984. for ($kk=0; $kk <= $maxkk; ++$kk) {
  10985. // restore special characters
  10986. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  10987. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  10988. if ($kk == $maxkk) {
  10989. if ($this->rtl OR $this->tmprtl) {
  10990. $tvalue = ltrim($lnstring[1][$kk]);
  10991. } else {
  10992. $tvalue = rtrim($lnstring[1][$kk]);
  10993. }
  10994. } else {
  10995. $tvalue = $lnstring[1][$kk];
  10996. }
  10997. // count spaces on line
  10998. $no += substr_count($lnstring[1][$kk], chr(32));
  10999. $ns += substr_count($tvalue, chr(32));
  11000. }
  11001. if ($this->rtl OR $this->tmprtl) {
  11002. $t_x = $this->lMargin - $this->endlinex - (($no - $ns - 1) * $this->GetStringWidth(chr(32)));
  11003. }
  11004. // calculate additional space to add to each space
  11005. $spacewidth = (($tw - $linew + (($no - $ns) * $this->GetStringWidth(chr(32)))) / ($ns?$ns:1)) * $this->k;
  11006. $spacewidthu = ($tw - $linew + ($no * $this->GetStringWidth(chr(32)))) / ($ns?$ns:1) / $this->FontSize / $this->k;
  11007. $nsmax = $ns;
  11008. $ns = 0;
  11009. reset($lnstring);
  11010. $offset = 0;
  11011. $strcount = 0;
  11012. $prev_epsposbeg = 0;
  11013. global $spacew;
  11014. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  11015. if ($this->rtl OR $this->tmprtl) {
  11016. $spacew = ($spacewidth * ($nsmax - $ns));
  11017. } else {
  11018. $spacew = ($spacewidth * $ns);
  11019. }
  11020. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  11021. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  11022. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
  11023. if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
  11024. OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
  11025. // shift EPS images
  11026. $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
  11027. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  11028. $pmid_b = substr($pmid, 0, $epsposbeg);
  11029. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  11030. $pmid_e = substr($pmid, $epsposend);
  11031. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  11032. $offset = $epsposend;
  11033. continue;
  11034. }
  11035. $prev_epsposbeg = $epsposbeg;
  11036. $currentxpos = 0;
  11037. // shift blocks of code
  11038. switch ($strpiece[2][0]) {
  11039. case 'Td':
  11040. case 'cm':
  11041. case 'm':
  11042. case 'l': {
  11043. // get current X position
  11044. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11045. $currentxpos = $xmatches[1];
  11046. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  11047. if ($strcount == $maxkk) {
  11048. if ($this->rtl OR $this->tmprtl) {
  11049. $tvalue = $lnstring[1][$strcount];
  11050. } else {
  11051. $tvalue = rtrim($lnstring[1][$strcount]);
  11052. }
  11053. } else {
  11054. $tvalue = $lnstring[1][$strcount];
  11055. }
  11056. $ns += substr_count($tvalue, chr(32));
  11057. ++$strcount;
  11058. }
  11059. if ($this->rtl OR $this->tmprtl) {
  11060. $spacew = ($spacewidth * ($nsmax - $ns));
  11061. }
  11062. // justify block
  11063. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11064. create_function('$matches', 'global $spacew;
  11065. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  11066. return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
  11067. break;
  11068. }
  11069. case 're': {
  11070. // get current X position
  11071. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11072. $currentxpos = $xmatches[1];
  11073. // justify block
  11074. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11075. create_function('$matches', 'global $spacew;
  11076. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  11077. return "".$newx." ".$matches[2]." ".$matches[3]." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
  11078. break;
  11079. }
  11080. case 'c': {
  11081. // get current X position
  11082. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11083. $currentxpos = $xmatches[1];
  11084. // justify block
  11085. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11086. create_function('$matches', 'global $spacew;
  11087. $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
  11088. $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
  11089. $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
  11090. return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
  11091. break;
  11092. }
  11093. }
  11094. // shift the annotations and links
  11095. if (isset($this->PageAnnots[$this->page])) {
  11096. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11097. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  11098. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  11099. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  11100. break;
  11101. }
  11102. }
  11103. }
  11104. } // end of while
  11105. // remove markers
  11106. $pmid = str_replace('x*#!#*x', '', $pmid);
  11107. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11108. // multibyte characters
  11109. $spacew = $spacewidthu;
  11110. $pmidtemp = $pmid;
  11111. // escape special characters
  11112. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  11113. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  11114. $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
  11115. create_function('$matches', 'global $spacew;
  11116. $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
  11117. $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
  11118. return "[(".str_replace(chr(0).chr(32), ") ".(-2830 * $spacew)." (", $matches[1]).")]";'), $pmidtemp);
  11119. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  11120. $endlinepos = strlen($pstart."\n".$pmid."\n");
  11121. } else {
  11122. // non-unicode (single-byte characters)
  11123. $rs = sprintf("%.3F Tw", $spacewidth);
  11124. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  11125. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  11126. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  11127. }
  11128. }
  11129. } // end of J
  11130. if (($t_x != 0) OR ($yshift < 0)) {
  11131. // shift the line
  11132. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  11133. $this->setPageBuffer($startlinepage, $pstart."\nq\n".$trx."\n".$pmid."\nQ\n".$pend);
  11134. $endlinepos = strlen($pstart."\nq\n".$trx."\n".$pmid."\nQ\n");
  11135. // shift the annotations and links
  11136. if (isset($this->PageAnnots[$this->page])) {
  11137. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11138. if ($pak >= $pask) {
  11139. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  11140. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  11141. }
  11142. }
  11143. }
  11144. $this->y -= $yshift;
  11145. }
  11146. }
  11147. }
  11148. $this->newline = false;
  11149. $pbrk = $this->checkPageBreak($this->lasth);
  11150. $this->SetFont($fontname, $fontstyle, $fontsize);
  11151. if ($wfill) {
  11152. $this->SetFillColorArray($this->bgcolor);
  11153. }
  11154. $startlinex = $this->x;
  11155. $startliney = $this->y;
  11156. $minstartliney = $this->y;
  11157. $startlinepage = $this->page;
  11158. if (isset($endlinepos) AND (!$pbrk)) {
  11159. $startlinepos = $endlinepos;
  11160. unset($endlinepos);
  11161. } else {
  11162. if (isset($this->footerlen[$this->page])) {
  11163. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11164. } else {
  11165. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11166. }
  11167. $startlinepos = $this->footerpos[$this->page];
  11168. }
  11169. $plalign = $lalign;
  11170. if (isset($this->PageAnnots[$this->page])) {
  11171. $pask = count($this->PageAnnots[$this->page]);
  11172. } else {
  11173. $pask = 0;
  11174. }
  11175. }
  11176. if (isset($opentagpos)) {
  11177. unset($opentagpos);
  11178. }
  11179. if ($dom[$key]['tag']) {
  11180. if ($dom[$key]['opening']) {
  11181. if ($dom[$key]['value'] == 'table') {
  11182. if ($this->rtl) {
  11183. $wtmp = $this->x - $this->lMargin;
  11184. } else {
  11185. $wtmp = $this->w - $this->rMargin - $this->x;
  11186. }
  11187. $wtmp -= (2 * $this->cMargin);
  11188. // calculate cell width
  11189. if (isset($dom[$key]['width'])) {
  11190. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  11191. } else {
  11192. $table_width = $wtmp;
  11193. }
  11194. }
  11195. // table content is handled in a special way
  11196. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  11197. $trid = $dom[$key]['parent'];
  11198. $table_el = $dom[$trid]['parent'];
  11199. if (!isset($dom[$table_el]['cols'])) {
  11200. $dom[$table_el]['cols'] = $trid['cols'];
  11201. }
  11202. $oldmargin = $this->cMargin;
  11203. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  11204. $currentcmargin = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  11205. } else {
  11206. $currentcmargin = 0;
  11207. }
  11208. $this->cMargin = $currentcmargin;
  11209. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'])) {
  11210. $cellspacing = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'], 1, 'px');
  11211. } else {
  11212. $cellspacing = 0;
  11213. }
  11214. if ($this->rtl) {
  11215. $cellspacingx = -$cellspacing;
  11216. } else {
  11217. $cellspacingx = $cellspacing;
  11218. }
  11219. $colspan = $dom[$key]['attribute']['colspan'];
  11220. $wtmp = ($colspan * ($table_width / $dom[$table_el]['cols']));
  11221. if (isset($dom[$key]['width'])) {
  11222. $cellw = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  11223. } else {
  11224. $cellw = $wtmp;
  11225. }
  11226. if (isset($dom[$key]['height'])) {
  11227. // minimum cell height
  11228. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  11229. } else {
  11230. $cellh = 0;
  11231. }
  11232. $cellw -= $cellspacing;
  11233. if (isset($dom[$key]['content'])) {
  11234. $cell_content = $dom[$key]['content'];
  11235. } else {
  11236. $cell_content = '&nbsp;';
  11237. }
  11238. $tagtype = $dom[$key]['value'];
  11239. $parentid = $key;
  11240. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  11241. // move $key index forward
  11242. ++$key;
  11243. }
  11244. if (!isset($dom[$trid]['startpage'])) {
  11245. $dom[$trid]['startpage'] = $this->page;
  11246. } else {
  11247. $this->setPage($dom[$trid]['startpage']);
  11248. }
  11249. if (!isset($dom[$trid]['starty'])) {
  11250. $dom[$trid]['starty'] = $this->y;
  11251. } else {
  11252. $this->y = $dom[$trid]['starty'];
  11253. }
  11254. if (!isset($dom[$trid]['startx'])) {
  11255. $dom[$trid]['startx'] = $this->x;
  11256. }
  11257. $this->x += ($cellspacingx / 2);
  11258. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  11259. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  11260. } else {
  11261. $rowspan = 1;
  11262. }
  11263. // skip row-spanned cells started on the previous rows
  11264. if (isset($dom[$table_el]['rowspans'])) {
  11265. $rsk = 0;
  11266. $rskmax = count($dom[$table_el]['rowspans']);
  11267. while ($rsk < $rskmax) {
  11268. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  11269. $rsstartx = $trwsp['startx'];
  11270. $rsendx = $trwsp['endx'];
  11271. // account for margin changes
  11272. if ($trwsp['startpage'] < $this->page) {
  11273. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  11274. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  11275. $rsstartx -= $dl;
  11276. $rsendx -= $dl;
  11277. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  11278. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  11279. $rsstartx += $dl;
  11280. $rsendx += $dl;
  11281. }
  11282. }
  11283. if (($trwsp['rowspan'] > 0)
  11284. AND ($rsstartx > ($this->x - $cellspacing - $currentcmargin - $this->feps))
  11285. AND ($rsstartx < ($this->x + $cellspacing + $currentcmargin + $this->feps))
  11286. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page))) {
  11287. // set the starting X position of the current cell
  11288. $this->x = $rsendx + $cellspacingx;
  11289. if (($trwsp['rowspan'] == 1)
  11290. AND (isset($dom[$trid]['endy']))
  11291. AND (isset($dom[$trid]['endpage']))
  11292. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])) {
  11293. // set ending Y position for row
  11294. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  11295. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  11296. }
  11297. $rsk = 0;
  11298. } else {
  11299. ++$rsk;
  11300. }
  11301. }
  11302. }
  11303. // add rowspan information to table element
  11304. if ($rowspan > 1) {
  11305. if (isset($this->footerlen[$this->page])) {
  11306. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11307. } else {
  11308. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11309. }
  11310. $trintmrkpos = $this->footerpos[$this->page];
  11311. $trsid = array_push($dom[$table_el]['rowspans'], array('trid' => $trid, 'rowspan' => $rowspan, 'mrowspan' => $rowspan, 'colspan' => $colspan, 'startpage' => $this->page, 'startx' => $this->x, 'starty' => $this->y, 'intmrkpos' => $trintmrkpos));
  11312. }
  11313. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  11314. if ($rowspan > 1) {
  11315. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  11316. }
  11317. // push background colors
  11318. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  11319. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  11320. }
  11321. $prevLastH = $this->lasth;
  11322. // ****** write the cell content ******
  11323. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true);
  11324. $this->lasth = $prevLastH;
  11325. $this->cMargin = $oldmargin;
  11326. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  11327. // update the end of row position
  11328. if ($rowspan <= 1) {
  11329. if (isset($dom[$trid]['endy'])) {
  11330. if ($this->page == $dom[$trid]['endpage']) {
  11331. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  11332. } elseif ($this->page > $dom[$trid]['endpage']) {
  11333. $dom[$trid]['endy'] = $this->y;
  11334. }
  11335. } else {
  11336. $dom[$trid]['endy'] = $this->y;
  11337. }
  11338. if (isset($dom[$trid]['endpage'])) {
  11339. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  11340. } else {
  11341. $dom[$trid]['endpage'] = $this->page;
  11342. }
  11343. } else {
  11344. // account for row-spanned cells
  11345. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  11346. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  11347. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  11348. }
  11349. if (isset($dom[$table_el]['rowspans'])) {
  11350. // update endy and endpage on rowspanned cells
  11351. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  11352. if ($trwsp['rowspan'] > 0) {
  11353. if (isset($dom[$trid]['endpage'])) {
  11354. if ($trwsp['endpage'] == $dom[$trid]['endpage']) {
  11355. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  11356. } elseif ($trwsp['endpage'] < $dom[$trid]['endpage']) {
  11357. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  11358. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  11359. } else {
  11360. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  11361. }
  11362. }
  11363. }
  11364. }
  11365. }
  11366. $this->x += ($cellspacingx / 2);
  11367. } else {
  11368. // opening tag (or self-closing tag)
  11369. if (!isset($opentagpos)) {
  11370. if (!$this->InFooter) {
  11371. if (isset($this->footerlen[$this->page])) {
  11372. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11373. } else {
  11374. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11375. }
  11376. $opentagpos = $this->footerpos[$this->page];
  11377. }
  11378. }
  11379. $this->openHTMLTagHandler($dom, $key, $cell);
  11380. }
  11381. } else {
  11382. // closing tag
  11383. $this->closeHTMLTagHandler($dom, $key, $cell);
  11384. }
  11385. } elseif (strlen($dom[$key]['value']) > 0) {
  11386. // print list-item
  11387. if (!$this->empty_string($this->lispacer)) {
  11388. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  11389. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11390. $minstartliney = $this->y;
  11391. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  11392. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  11393. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11394. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  11395. $this->y += (($pfontsize - $curfontsize) / $this->k);
  11396. $minstartliney = min($this->y, $minstartliney);
  11397. }
  11398. }
  11399. // text
  11400. $this->htmlvspace = 0;
  11401. if ((!$this->premode) AND ($this->rtl OR $this->tmprtl)) {
  11402. // reverse spaces order
  11403. $len1 = strlen($dom[$key]['value']);
  11404. $lsp = $len1 - strlen(ltrim($dom[$key]['value']));
  11405. $rsp = $len1 - strlen(rtrim($dom[$key]['value']));
  11406. $tmpstr = '';
  11407. if ($rsp > 0) {
  11408. $tmpstr .= substr($dom[$key]['value'], -$rsp);
  11409. }
  11410. $tmpstr .= trim($dom[$key]['value']);
  11411. if ($lsp > 0) {
  11412. $tmpstr .= substr($dom[$key]['value'], 0, $lsp);
  11413. }
  11414. $dom[$key]['value'] = $tmpstr;
  11415. }
  11416. if ($newline) {
  11417. if (!$this->premode) {
  11418. if (($this->rtl OR $this->tmprtl)) {
  11419. $dom[$key]['value'] = rtrim($dom[$key]['value']);
  11420. } else {
  11421. $dom[$key]['value'] = ltrim($dom[$key]['value']);
  11422. }
  11423. }
  11424. $newline = false;
  11425. $firstblock = true;
  11426. } else {
  11427. $firstblock = false;
  11428. }
  11429. $strrest = '';
  11430. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  11431. // HTML <a> Link
  11432. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $this->HREF['color'], $this->HREF['style']);
  11433. } else {
  11434. $ctmpmargin = $this->cMargin;
  11435. $this->cMargin = 0;
  11436. // ****** write only until the end of the line and get the rest ******
  11437. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock);
  11438. $this->cMargin = $ctmpmargin;
  11439. }
  11440. if (strlen($strrest) > 0) {
  11441. // store the remaining string on the previous $key position
  11442. $this->newline = true;
  11443. if ($cell) {
  11444. if ($this->rtl) {
  11445. $this->x -= $this->cMargin;
  11446. } else {
  11447. $this->x += $this->cMargin;
  11448. }
  11449. }
  11450. if ($strrest == $dom[$key]['value']) {
  11451. // used to avoid infinite loop
  11452. ++$loop;
  11453. } else {
  11454. $loop = 0;
  11455. }
  11456. $dom[$key]['value'] = ltrim($strrest);
  11457. if ($loop < 3) {
  11458. --$key;
  11459. }
  11460. } else {
  11461. $loop = 0;
  11462. }
  11463. }
  11464. ++$key;
  11465. } // end for each $key
  11466. // align the last line
  11467. if (isset($startlinex)) {
  11468. $yshift = $minstartliney - $startliney;
  11469. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  11470. $yshift = 0;
  11471. }
  11472. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl))))) OR ($yshift < 0)) {
  11473. // the last line must be shifted to be aligned as requested
  11474. $linew = abs($this->endlinex - $startlinex);
  11475. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  11476. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11477. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11478. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  11479. } elseif (isset($opentagpos)) {
  11480. $midpos = $opentagpos;
  11481. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11482. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11483. $midpos = $this->footerpos[$startlinepage];
  11484. } else {
  11485. $midpos = 0;
  11486. }
  11487. if ($midpos > 0) {
  11488. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  11489. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  11490. } else {
  11491. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  11492. $pend = '';
  11493. }
  11494. // calculate shifting amount
  11495. $tw = $w;
  11496. if ($this->lMargin != $prevlMargin) {
  11497. $tw += ($prevlMargin - $this->lMargin);
  11498. }
  11499. if ($this->rMargin != $prevrMargin) {
  11500. $tw += ($prevrMargin - $this->rMargin);
  11501. }
  11502. $mdiff = abs($tw - $linew);
  11503. if ($plalign == 'C') {
  11504. if ($this->rtl) {
  11505. $t_x = -($mdiff / 2);
  11506. } else {
  11507. $t_x = ($mdiff / 2);
  11508. }
  11509. } elseif (($plalign == 'R') AND (!$this->rtl)) {
  11510. // right alignment on LTR document
  11511. $t_x = $mdiff;
  11512. } elseif (($plalign == 'L') AND ($this->rtl)) {
  11513. // left alignment on RTL document
  11514. $t_x = -$mdiff;
  11515. } else {
  11516. $t_x = 0;
  11517. }
  11518. if (($t_x != 0) OR ($yshift < 0)) {
  11519. // shift the line
  11520. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  11521. $this->setPageBuffer($startlinepage, $pstart."\nq\n".$trx."\n".$pmid."\nQ\n".$pend);
  11522. $endlinepos = strlen($pstart."\nq\n".$trx."\n".$pmid."\nQ\n");
  11523. // shift the annotations and links
  11524. if (isset($this->PageAnnots[$this->page])) {
  11525. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11526. if ($pak >= $pask) {
  11527. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  11528. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  11529. }
  11530. }
  11531. }
  11532. $this->y -= $yshift;
  11533. }
  11534. }
  11535. }
  11536. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  11537. $this->Ln($this->lasth);
  11538. }
  11539. // restore previous values
  11540. $this->setGraphicVars($gvars);
  11541. if ($this->page > $prevPage) {
  11542. $this->lMargin = $this->pagedim[$this->page]['olm'];
  11543. $this->rMargin = $this->pagedim[$this->page]['orm'];
  11544. }
  11545. unset($dom);
  11546. }
  11547. /**
  11548. * Process opening tags.
  11549. * @param array $dom html dom array
  11550. * @param int $key current element id
  11551. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  11552. * @access protected
  11553. */
  11554. protected function openHTMLTagHandler(&$dom, $key, $cell=false) {
  11555. $tag = $dom[$key];
  11556. $parent = $dom[($dom[$key]['parent'])];
  11557. $firstorlast = ($key == 1);
  11558. // check for text direction attribute
  11559. if (isset($tag['attribute']['dir'])) {
  11560. $this->tmprtl = $tag['attribute']['dir'] == 'rtl' ? 'R' : 'L';
  11561. } else {
  11562. $this->tmprtl = false;
  11563. }
  11564. //Opening tag
  11565. switch($tag['value']) {
  11566. case 'table': {
  11567. $cp = 0;
  11568. $cs = 0;
  11569. $dom[$key]['rowspans'] = array();
  11570. if (!$this->empty_string($dom[$key]['thead'])) {
  11571. // set table header
  11572. $this->thead = $dom[$key]['thead'];
  11573. }
  11574. if (isset($tag['attribute']['cellpadding'])) {
  11575. $cp = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  11576. $this->oldcMargin = $this->cMargin;
  11577. $this->cMargin = $cp;
  11578. }
  11579. if (isset($tag['attribute']['cellspacing'])) {
  11580. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  11581. }
  11582. $this->checkPageBreak((2 * $cp) + (2 * $cs) + $this->lasth);
  11583. break;
  11584. }
  11585. case 'tr': {
  11586. // array of columns positions
  11587. $dom[$key]['cellpos'] = array();
  11588. break;
  11589. }
  11590. case 'hr': {
  11591. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11592. $this->htmlvspace = 0;
  11593. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  11594. if ((isset($tag['attribute']['width'])) AND ($tag['attribute']['width'] != '')) {
  11595. $hrWidth = $this->getHTMLUnitToUnits($tag['attribute']['width'], $wtmp, 'px');
  11596. } else {
  11597. $hrWidth = $wtmp;
  11598. }
  11599. $x = $this->GetX();
  11600. $y = $this->GetY();
  11601. $prevlinewidth = $this->GetLineWidth();
  11602. $this->Line($x, $y, $x + $hrWidth, $y);
  11603. $this->SetLineWidth($prevlinewidth);
  11604. $this->addHTMLVertSpace(1, $cell, '', !isset($dom[($key + 1)]), $tag['value'], false);
  11605. break;
  11606. }
  11607. case 'a': {
  11608. if (array_key_exists('href', $tag['attribute'])) {
  11609. $this->HREF['url'] = $tag['attribute']['href'];
  11610. }
  11611. $this->HREF['color'] = $this->htmlLinkColorArray;
  11612. $this->HREF['style'] = $this->htmlLinkFontStyle;
  11613. if (array_key_exists('style', $tag['attribute'])) {
  11614. // get style attributes
  11615. preg_match_all('/([^;:\s]*):([^;]*)/', $tag['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  11616. $astyle = array();
  11617. while (list($id, $name) = each($style_array[1])) {
  11618. $name = strtolower($name);
  11619. $astyle[$name] = trim($style_array[2][$id]);
  11620. }
  11621. if (isset($astyle['color'])) {
  11622. $this->HREF['color'] = $this->convertHTMLColorToDec($astyle['color']);
  11623. }
  11624. if (isset($astyle['text-decoration'])) {
  11625. $this->HREF['style'] = '';
  11626. $decors = explode(' ', strtolower($astyle['text-decoration']));
  11627. foreach ($decors as $dec) {
  11628. $dec = trim($dec);
  11629. if (!$this->empty_string($dec)) {
  11630. if ($dec{0} == 'u') {
  11631. $this->HREF['style'] .= 'U';
  11632. } elseif ($dec{0} == 'l') {
  11633. $this->HREF['style'] .= 'D';
  11634. }
  11635. }
  11636. }
  11637. }
  11638. }
  11639. break;
  11640. }
  11641. case 'img': {
  11642. if (isset($tag['attribute']['src'])) {
  11643. // replace relative path with real server path
  11644. if ($tag['attribute']['src'][0] == '/') {
  11645. $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
  11646. }
  11647. $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
  11648. $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
  11649. if (!isset($tag['attribute']['width'])) {
  11650. $tag['attribute']['width'] = 0;
  11651. }
  11652. if (!isset($tag['attribute']['height'])) {
  11653. $tag['attribute']['height'] = 0;
  11654. }
  11655. //if (!isset($tag['attribute']['align'])) {
  11656. // the only alignment supported is "bottom"
  11657. // further development is required for other modes.
  11658. $tag['attribute']['align'] = 'bottom';
  11659. //}
  11660. switch($tag['attribute']['align']) {
  11661. case 'top': {
  11662. $align = 'T';
  11663. break;
  11664. }
  11665. case 'middle': {
  11666. $align = 'M';
  11667. break;
  11668. }
  11669. case 'bottom': {
  11670. $align = 'B';
  11671. break;
  11672. }
  11673. default: {
  11674. $align = 'B';
  11675. break;
  11676. }
  11677. }
  11678. $fileinfo = pathinfo($tag['attribute']['src']);
  11679. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  11680. $type = strtolower($fileinfo['extension']);
  11681. }
  11682. $prevy = $this->y;
  11683. $xpos = $this->GetX();
  11684. if (isset($dom[($key - 1)]) AND ($dom[($key - 1)]['value'] == ' ')) {
  11685. if ($this->rtl) {
  11686. $xpos += $this->GetStringWidth(' ');
  11687. } else {
  11688. $xpos -= $this->GetStringWidth(' ');
  11689. }
  11690. }
  11691. $imglink = '';
  11692. if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
  11693. $imglink = $this->HREF['url'];
  11694. if ($imglink{0} == '#') {
  11695. // convert url to internal link
  11696. $page = intval(substr($imglink, 1));
  11697. $imglink = $this->AddLink();
  11698. $this->SetLink($imglink, 0, $page);
  11699. }
  11700. }
  11701. $border = 0;
  11702. if (isset($tag['attribute']['border']) AND !empty($tag['attribute']['border'])) {
  11703. // currently only support 1 (frame) or a combination of 'LTRB'
  11704. $border = $tag['attribute']['border'];
  11705. }
  11706. $iw = '';
  11707. if (isset($tag['attribute']['width'])) {
  11708. $iw = $this->getHTMLUnitToUnits($tag['attribute']['width'], 1, 'px', false);
  11709. }
  11710. $ih = '';
  11711. if (isset($tag['attribute']['height'])) {
  11712. $ih = $this->getHTMLUnitToUnits($tag['attribute']['height'], 1, 'px', false);
  11713. }
  11714. if (($type == 'eps') OR ($type == 'ai')) {
  11715. $this->ImageEps($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, $imglink, true, $align, '', $border);
  11716. } else {
  11717. $this->Image($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border);
  11718. }
  11719. switch($align) {
  11720. case 'T': {
  11721. $this->y = $prevy;
  11722. break;
  11723. }
  11724. case 'M': {
  11725. $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
  11726. break;
  11727. }
  11728. case 'B': {
  11729. $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
  11730. break;
  11731. }
  11732. }
  11733. }
  11734. break;
  11735. }
  11736. case 'dl': {
  11737. ++$this->listnum;
  11738. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  11739. break;
  11740. }
  11741. case 'dt': {
  11742. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11743. break;
  11744. }
  11745. case 'dd': {
  11746. if ($this->rtl) {
  11747. $this->rMargin += $this->listindent;
  11748. } else {
  11749. $this->lMargin += $this->listindent;
  11750. }
  11751. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11752. break;
  11753. }
  11754. case 'ul':
  11755. case 'ol': {
  11756. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  11757. $this->htmlvspace = 0;
  11758. ++$this->listnum;
  11759. if ($tag['value'] == 'ol') {
  11760. $this->listordered[$this->listnum] = true;
  11761. } else {
  11762. $this->listordered[$this->listnum] = false;
  11763. }
  11764. if (isset($tag['attribute']['start'])) {
  11765. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  11766. } else {
  11767. $this->listcount[$this->listnum] = 0;
  11768. }
  11769. if ($this->rtl) {
  11770. $this->rMargin += $this->listindent;
  11771. } else {
  11772. $this->lMargin += $this->listindent;
  11773. }
  11774. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  11775. $this->htmlvspace = 0;
  11776. break;
  11777. }
  11778. case 'li': {
  11779. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11780. if ($this->listordered[$this->listnum]) {
  11781. // ordered item
  11782. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  11783. $this->lispacer = $parent['attribute']['type'];
  11784. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  11785. $this->lispacer = $parent['listtype'];
  11786. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  11787. $this->lispacer = $this->lisymbol;
  11788. } else {
  11789. $this->lispacer = '#';
  11790. }
  11791. ++$this->listcount[$this->listnum];
  11792. if (isset($tag['attribute']['value'])) {
  11793. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  11794. }
  11795. } else {
  11796. // unordered item
  11797. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  11798. $this->lispacer = $parent['attribute']['type'];
  11799. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  11800. $this->lispacer = $parent['listtype'];
  11801. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  11802. $this->lispacer = $this->lisymbol;
  11803. } else {
  11804. $this->lispacer = '!';
  11805. }
  11806. }
  11807. break;
  11808. }
  11809. case 'blockquote': {
  11810. if ($this->rtl) {
  11811. $this->rMargin += $this->listindent;
  11812. } else {
  11813. $this->lMargin += $this->listindent;
  11814. }
  11815. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
  11816. break;
  11817. }
  11818. case 'br': {
  11819. $this->Ln('', $cell);
  11820. break;
  11821. }
  11822. case 'div': {
  11823. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11824. break;
  11825. }
  11826. case 'p': {
  11827. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
  11828. break;
  11829. }
  11830. case 'pre': {
  11831. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11832. $this->premode = true;
  11833. break;
  11834. }
  11835. case 'sup': {
  11836. $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  11837. break;
  11838. }
  11839. case 'sub': {
  11840. $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  11841. break;
  11842. }
  11843. case 'h1':
  11844. case 'h2':
  11845. case 'h3':
  11846. case 'h4':
  11847. case 'h5':
  11848. case 'h6': {
  11849. $this->addHTMLVertSpace(1, $cell, ($tag['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], false);
  11850. break;
  11851. }
  11852. case 'tcpdf': {
  11853. // NOT HTML: used to call TCPDF methods
  11854. if (isset($tag['attribute']['method'])) {
  11855. $tcpdf_method = $tag['attribute']['method'];
  11856. if (method_exists($this, $tcpdf_method)) {
  11857. if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
  11858. eval('$params = array('.$tag['attribute']['params'].');');
  11859. call_user_func_array(array($this, $tcpdf_method), $params);
  11860. } else {
  11861. $this->$tcpdf_method();
  11862. }
  11863. $this->newline = true;
  11864. }
  11865. }
  11866. }
  11867. default: {
  11868. break;
  11869. }
  11870. }
  11871. }
  11872. /**
  11873. * Process closing tags.
  11874. * @param array $dom html dom array
  11875. * @param int $key current element id
  11876. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  11877. * @access protected
  11878. */
  11879. protected function closeHTMLTagHandler(&$dom, $key, $cell=false) {
  11880. $tag = $dom[$key];
  11881. $parent = $dom[($dom[$key]['parent'])];
  11882. $firstorlast = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  11883. //Closing tag
  11884. switch($tag['value']) {
  11885. case 'tr': {
  11886. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  11887. if(!isset($parent['endy'])) {
  11888. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  11889. $parent['endy'] = $this->y;
  11890. }
  11891. if(!isset($parent['endpage'])) {
  11892. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  11893. $parent['endpage'] = $this->page;
  11894. }
  11895. // update row-spanned cells
  11896. if (isset($dom[$table_el]['rowspans'])) {
  11897. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  11898. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  11899. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  11900. if ($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) {
  11901. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  11902. } elseif ($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) {
  11903. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  11904. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  11905. }
  11906. }
  11907. }
  11908. // report new endy and endpage to the rowspanned cells
  11909. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  11910. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  11911. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  11912. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  11913. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  11914. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  11915. }
  11916. }
  11917. // update remaining rowspanned cells
  11918. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  11919. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  11920. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  11921. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  11922. }
  11923. }
  11924. }
  11925. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  11926. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  11927. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  11928. $cellspacing = $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  11929. $this->y += $cellspacing;
  11930. }
  11931. $this->Ln(0, $cell);
  11932. $this->x = $parent['startx'];
  11933. // account for booklet mode
  11934. if ($this->page > $parent['startpage']) {
  11935. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  11936. $this->x += ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  11937. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  11938. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  11939. }
  11940. }
  11941. break;
  11942. }
  11943. case 'table': {
  11944. // draw borders
  11945. $table_el = $parent;
  11946. if ((isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0))
  11947. OR (isset($table_el['style']['border']) AND ($table_el['style']['border'] > 0))) {
  11948. $border = 1;
  11949. } else {
  11950. $border = 0;
  11951. }
  11952. // fix bottom line alignment of last line before page break
  11953. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  11954. // update row-spanned cells
  11955. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  11956. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  11957. if ($trwsp['trid'] == $trkey) {
  11958. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  11959. }
  11960. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0)) {
  11961. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  11962. }
  11963. }
  11964. }
  11965. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  11966. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  11967. $dom[$prevtrkey]['endy'] = $pgendy;
  11968. // update row-spanned cells
  11969. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  11970. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  11971. if (($trwsp['trid'] == $trkey) AND ($trwsp['mrowspan'] == 1) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  11972. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  11973. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  11974. }
  11975. }
  11976. }
  11977. }
  11978. $prevtrkey = $trkey;
  11979. $table_el = $dom[($dom[$key]['parent'])];
  11980. }
  11981. // for each row
  11982. foreach ($table_el['trids'] as $j => $trkey) {
  11983. $parent = $dom[$trkey];
  11984. // for each cell on the row
  11985. foreach ($parent['cellpos'] as $k => $cellpos) {
  11986. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  11987. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  11988. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  11989. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  11990. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  11991. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  11992. } else {
  11993. $endy = $parent['endy'];
  11994. $startpage = $parent['startpage'];
  11995. $endpage = $parent['endpage'];
  11996. }
  11997. if ($endpage > $startpage) {
  11998. // design borders around HTML cells.
  11999. for ($page=$startpage; $page <= $endpage; ++$page) {
  12000. $this->setPage($page);
  12001. if ($page == $startpage) {
  12002. $this->y = $parent['starty']; // put cursor at the beginning of row on the first page
  12003. $ch = $this->getPageHeight() - $parent['starty'] - $this->getBreakMargin();
  12004. $cborder = $this->getBorderMode($border, $position='start');
  12005. } elseif ($page == $endpage) {
  12006. $this->y = $this->tMargin; // put cursor at the beginning of last page
  12007. $ch = $endy - $this->tMargin;
  12008. $cborder = $this->getBorderMode($border, $position='end');
  12009. } else {
  12010. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  12011. $ch = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  12012. $cborder = $this->getBorderMode($border, $position='middle');
  12013. }
  12014. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  12015. $this->SetFillColorArray($cellpos['bgcolor']);
  12016. $fill = true;
  12017. } else {
  12018. $fill = false;
  12019. }
  12020. $cw = abs($cellpos['endx'] - $cellpos['startx']);
  12021. $this->x = $cellpos['startx'];
  12022. // account for margin changes
  12023. if ($page > $startpage) {
  12024. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  12025. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  12026. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['lm'] != $this->pagedim[$startpage]['olm'])) {
  12027. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  12028. }
  12029. }
  12030. // design a cell around the text
  12031. $ccode = $this->FillColor."\n".$this->getCellCode($cw, $ch, '', $cborder, 1, '', $fill, '', 0, true);
  12032. if ($cborder OR $fill) {
  12033. $pagebuff = $this->getPageBuffer($this->page);
  12034. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  12035. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  12036. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  12037. $this->intmrk[$this->page] += strlen($ccode."\n");
  12038. }
  12039. }
  12040. } else {
  12041. $this->setPage($startpage);
  12042. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  12043. $this->SetFillColorArray($cellpos['bgcolor']);
  12044. $fill = true;
  12045. } else {
  12046. $fill = false;
  12047. }
  12048. $this->x = $cellpos['startx'];
  12049. $this->y = $parent['starty'];
  12050. $cw = abs($cellpos['endx'] - $cellpos['startx']);
  12051. $ch = $endy - $parent['starty'];
  12052. // design a cell around the text
  12053. $ccode = $this->FillColor."\n".$this->getCellCode($cw, $ch, '', $border, 1, '', $fill, '', 0, true);
  12054. if ($border OR $fill) {
  12055. if (end($this->transfmrk[$this->page]) !== false) {
  12056. $pagemarkkey = key($this->transfmrk[$this->page]);
  12057. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  12058. } elseif ($this->InFooter) {
  12059. $pagemark = &$this->footerpos[$this->page];
  12060. } else {
  12061. $pagemark = &$this->intmrk[$this->page];
  12062. }
  12063. $pagebuff = $this->getPageBuffer($this->page);
  12064. $pstart = substr($pagebuff, 0, $pagemark);
  12065. $pend = substr($pagebuff, $pagemark);
  12066. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  12067. $pagemark += strlen($ccode."\n");
  12068. }
  12069. }
  12070. }
  12071. if (isset($table_el['attribute']['cellspacing'])) {
  12072. $cellspacing = $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  12073. $this->y += $cellspacing;
  12074. }
  12075. $this->Ln(0, $cell);
  12076. $this->x = $parent['startx'];
  12077. if ($endpage > $startpage) {
  12078. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  12079. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  12080. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  12081. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  12082. }
  12083. }
  12084. }
  12085. if (isset($parent['cellpadding'])) {
  12086. $this->cMargin = $this->oldcMargin;
  12087. }
  12088. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12089. if (!$this->empty_string($this->theadMargin)) {
  12090. // restore top margin
  12091. $this->tMargin = $this->theadMargin;
  12092. $this->pagedim[$this->page]['tm'] = $this->theadMargin;
  12093. }
  12094. // reset table header
  12095. $this->thead = '';
  12096. $this->theadMargin = '';
  12097. break;
  12098. }
  12099. case 'a': {
  12100. $this->HREF = '';
  12101. break;
  12102. }
  12103. case 'sup': {
  12104. $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  12105. break;
  12106. }
  12107. case 'sub': {
  12108. $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize'])/$this->k));
  12109. break;
  12110. }
  12111. case 'div': {
  12112. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], true);
  12113. break;
  12114. }
  12115. case 'blockquote': {
  12116. if ($this->rtl) {
  12117. $this->rMargin -= $this->listindent;
  12118. } else {
  12119. $this->lMargin -= $this->listindent;
  12120. }
  12121. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12122. break;
  12123. }
  12124. case 'p': {
  12125. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12126. break;
  12127. }
  12128. case 'pre': {
  12129. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], true);
  12130. $this->premode = false;
  12131. break;
  12132. }
  12133. case 'dl': {
  12134. --$this->listnum;
  12135. if ($this->listnum <= 0) {
  12136. $this->listnum = 0;
  12137. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12138. }
  12139. break;
  12140. }
  12141. case 'dt': {
  12142. $this->lispacer = '';
  12143. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12144. break;
  12145. }
  12146. case 'dd': {
  12147. $this->lispacer = '';
  12148. if ($this->rtl) {
  12149. $this->rMargin -= $this->listindent;
  12150. } else {
  12151. $this->lMargin -= $this->listindent;
  12152. }
  12153. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12154. break;
  12155. }
  12156. case 'ul':
  12157. case 'ol': {
  12158. --$this->listnum;
  12159. $this->lispacer = '';
  12160. if ($this->rtl) {
  12161. $this->rMargin -= $this->listindent;
  12162. } else {
  12163. $this->lMargin -= $this->listindent;
  12164. }
  12165. if ($this->listnum <= 0) {
  12166. $this->listnum = 0;
  12167. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12168. }
  12169. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12170. break;
  12171. }
  12172. case 'li': {
  12173. $this->lispacer = '';
  12174. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12175. break;
  12176. }
  12177. case 'h1':
  12178. case 'h2':
  12179. case 'h3':
  12180. case 'h4':
  12181. case 'h5':
  12182. case 'h6': {
  12183. $this->addHTMLVertSpace(1, $cell, ($parent['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], true);
  12184. break;
  12185. }
  12186. default : {
  12187. break;
  12188. }
  12189. }
  12190. $this->tmprtl = false;
  12191. }
  12192. /**
  12193. * Add vertical spaces if needed.
  12194. * @param int $n number of spaces to add
  12195. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  12196. * @param string $h The height of the break. By default, the value equals the height of the last printed cell.
  12197. * @param boolean $firstorlast if true do not print additional empty lines.
  12198. * @param string $tag HTML tag to which this space will be applied
  12199. * @param boolean $closing true if this space will be applied to a closing tag, false otherwise
  12200. * @access protected
  12201. */
  12202. protected function addHTMLVertSpace($n, $cell=false, $h='', $firstorlast=false, $tag='', $closing=false) {
  12203. if ($firstorlast) {
  12204. $this->Ln(0, $cell);
  12205. $this->htmlvspace = 0;
  12206. return;
  12207. }
  12208. if (isset($this->tagvspaces[$tag][intval($closing)]['n'])) {
  12209. $n = $this->tagvspaces[$tag][intval($closing)]['n'];
  12210. }
  12211. if (isset($this->tagvspaces[$tag][intval($closing)]['h'])) {
  12212. $h = $this->tagvspaces[$tag][intval($closing)]['h'];
  12213. }
  12214. if (is_string($h)) {
  12215. $vsize = $n * $this->lasth;
  12216. } else {
  12217. $vsize = $n * $h;
  12218. }
  12219. if ($vsize > $this->htmlvspace) {
  12220. $this->Ln(($vsize - $this->htmlvspace), $cell);
  12221. $this->htmlvspace = $vsize;
  12222. }
  12223. }
  12224. /**
  12225. * Set the default bullet to be used as LI bullet symbol
  12226. * @param string $symbol character or string to be used (legal values are: '' = automatic, '!' = auto bullet, '#' = auto numbering, 'disc', 'disc', 'circle', 'square', '1', 'decimal', 'decimal-leading-zero', 'i', 'lower-roman', 'I', 'upper-roman', 'a', 'lower-alpha', 'lower-latin', 'A', 'upper-alpha', 'upper-latin', 'lower-greek')
  12227. * @access public
  12228. * @since 4.0.028 (2008-09-26)
  12229. */
  12230. public function setLIsymbol($symbol='!') {
  12231. $symbol = strtolower($symbol);
  12232. switch ($symbol) {
  12233. case '!' :
  12234. case '#' :
  12235. case 'disc' :
  12236. case 'disc' :
  12237. case 'circle' :
  12238. case 'square' :
  12239. case '1':
  12240. case 'decimal':
  12241. case 'decimal-leading-zero':
  12242. case 'i':
  12243. case 'lower-roman':
  12244. case 'I':
  12245. case 'upper-roman':
  12246. case 'a':
  12247. case 'lower-alpha':
  12248. case 'lower-latin':
  12249. case 'A':
  12250. case 'upper-alpha':
  12251. case 'upper-latin':
  12252. case 'lower-greek': {
  12253. $this->lisymbol = $symbol;
  12254. break;
  12255. }
  12256. default : {
  12257. $this->lisymbol = '';
  12258. }
  12259. }
  12260. }
  12261. /**
  12262. * Set the booklet mode for double-sided pages.
  12263. * @param boolean $booklet true set the booklet mode on, fals eotherwise.
  12264. * @param float $inner Inner page margin.
  12265. * @param float $outer Outer page margin.
  12266. * @access public
  12267. * @since 4.2.000 (2008-10-29)
  12268. */
  12269. public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
  12270. $this->booklet = $booklet;
  12271. if ($inner >= 0) {
  12272. $this->lMargin = $inner;
  12273. }
  12274. if ($outer >= 0) {
  12275. $this->rMargin = $outer;
  12276. }
  12277. }
  12278. /**
  12279. * Swap the left and right margins.
  12280. * @param boolean $reverse if true swap left and right margins.
  12281. * @access protected
  12282. * @since 4.2.000 (2008-10-29)
  12283. */
  12284. protected function swapMargins($reverse=true) {
  12285. if ($reverse) {
  12286. // swap left and right margins
  12287. $mtemp = $this->original_lMargin;
  12288. $this->original_lMargin = $this->original_rMargin;
  12289. $this->original_rMargin = $mtemp;
  12290. $deltam = $this->original_lMargin - $this->original_rMargin;
  12291. $this->lMargin += $deltam;
  12292. $this->rMargin -= $deltam;
  12293. }
  12294. }
  12295. /**
  12296. * Set the vertical spaces for HTML tags.
  12297. * The array must have the following structure (example):
  12298. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  12299. * The first array level contains the tag names,
  12300. * the second level contains 0 for opening tags or 1 for closing tags,
  12301. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  12302. * If the h parameter is not specified, default values are used.
  12303. * @param array $tagvs array of tags and relative vertical spaces.
  12304. * @access public
  12305. * @since 4.2.001 (2008-10-30)
  12306. */
  12307. public function setHtmlVSpace($tagvs) {
  12308. $this->tagvspaces = $tagvs;
  12309. }
  12310. /**
  12311. * Set custom width for list indentation.
  12312. * @param float $width width of the indentation. Use negative value to disable it.
  12313. * @access public
  12314. * @since 4.2.007 (2008-11-12)
  12315. */
  12316. public function setListIndentWidth($width) {
  12317. return $this->customlistindent = floatval($width);
  12318. }
  12319. /**
  12320. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  12321. * @param boolean $isopen if true keeps the top/bottom border open for the cell sides that cross the page.
  12322. * @access public
  12323. * @since 4.2.010 (2008-11-14)
  12324. */
  12325. public function setOpenCell($isopen) {
  12326. $this->opencell = $isopen;
  12327. }
  12328. /**
  12329. * Set the color and font style for HTML links.
  12330. * @param array $color RGB array of colors
  12331. * @param string $fontstyle additional font styles to add
  12332. * @access public
  12333. * @since 4.4.003 (2008-12-09)
  12334. */
  12335. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  12336. $this->htmlLinkColorArray = $color;
  12337. $this->htmlLinkFontStyle = $fontstyle;
  12338. }
  12339. /**
  12340. * convert html string containing value and unit of measure to user's units or points.
  12341. * @param string $htmlval string containing values and unit
  12342. * @param string $refsize reference value in points
  12343. * @param string $defaultunit default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  12344. * @param boolean $point if true returns points, otherwise returns value in user's units
  12345. * @return float value in user's unit or point if $points=true
  12346. * @access public
  12347. * @since 4.4.004 (2008-12-10)
  12348. */
  12349. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  12350. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  12351. $retval = 0;
  12352. $value = 0;
  12353. $unit = 'px';
  12354. $k = $this->k;
  12355. if ($points) {
  12356. $k = 1;
  12357. }
  12358. if (in_array($defaultunit, $supportedunits)) {
  12359. $unit = $defaultunit;
  12360. }
  12361. if (is_numeric($htmlval)) {
  12362. $value = floatval($htmlval);
  12363. } elseif (preg_match('/([0-9\.]+)/', $htmlval, $mnum)) {
  12364. $value = floatval($mnum[1]);
  12365. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  12366. if (in_array($munit[1], $supportedunits)) {
  12367. $unit = $munit[1];
  12368. }
  12369. }
  12370. }
  12371. switch ($unit) {
  12372. // percentage
  12373. case '%': {
  12374. $retval = (($value * $refsize) / 100);
  12375. break;
  12376. }
  12377. // relative-size
  12378. case 'em': {
  12379. $retval = ($value * $refsize);
  12380. break;
  12381. }
  12382. case 'ex': {
  12383. $retval = $value * ($refsize / 2);
  12384. break;
  12385. }
  12386. // absolute-size
  12387. case 'in': {
  12388. $retval = ($value * $this->dpi) / $k;
  12389. break;
  12390. }
  12391. case 'cm': {
  12392. $retval = ($value / 2.54 * $this->dpi) / $k;
  12393. break;
  12394. }
  12395. case 'mm': {
  12396. $retval = ($value / 25.4 * $this->dpi) / $k;
  12397. break;
  12398. }
  12399. case 'pc': {
  12400. // one pica is 12 points
  12401. $retval = ($value * 12) / $k;
  12402. break;
  12403. }
  12404. case 'pt': {
  12405. $retval = $value / $k;
  12406. break;
  12407. }
  12408. case 'px': {
  12409. $retval = $this->pixelsToUnits($value);
  12410. break;
  12411. }
  12412. }
  12413. return $retval;
  12414. }
  12415. /**
  12416. * Returns the Roman representation of an integer number
  12417. * @param int number to convert
  12418. * @return string roman representation of the specified number
  12419. * @access public
  12420. * @since 4.4.004 (2008-12-10)
  12421. */
  12422. public function intToRoman($number) {
  12423. $roman = '';
  12424. while ($number >= 1000) {
  12425. $roman .= 'M';
  12426. $number -= 1000;
  12427. }
  12428. while ($number >= 900) {
  12429. $roman .= 'CM';
  12430. $number -= 900;
  12431. }
  12432. while ($number >= 500) {
  12433. $roman .= 'D';
  12434. $number -= 500;
  12435. }
  12436. while ($number >= 400) {
  12437. $roman .= 'CD';
  12438. $number -= 400;
  12439. }
  12440. while ($number >= 100) {
  12441. $roman .= 'C';
  12442. $number -= 100;
  12443. }
  12444. while ($number >= 90) {
  12445. $roman .= 'XC';
  12446. $number -= 90;
  12447. }
  12448. while ($number >= 50) {
  12449. $roman .= 'L';
  12450. $number -= 50;
  12451. }
  12452. while ($number >= 40) {
  12453. $roman .= 'XL';
  12454. $number -= 40;
  12455. }
  12456. while ($number >= 10) {
  12457. $roman .= 'X';
  12458. $number -= 10;
  12459. }
  12460. while ($number >= 9) {
  12461. $roman .= 'IX';
  12462. $number -= 9;
  12463. }
  12464. while ($number >= 5) {
  12465. $roman .= 'V';
  12466. $number -= 5;
  12467. }
  12468. while ($number >= 4) {
  12469. $roman .= 'IV';
  12470. $number -= 4;
  12471. }
  12472. while ($number >= 1) {
  12473. $roman .= 'I';
  12474. --$number;
  12475. }
  12476. return $roman;
  12477. }
  12478. /**
  12479. * Output an HTML list bullet or ordered item symbol
  12480. * @param int $listdepth list nesting level
  12481. * @param string $listtype type of list
  12482. * @param float $size current font size
  12483. * @access protected
  12484. * @since 4.4.004 (2008-12-10)
  12485. */
  12486. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  12487. $size /= $this->k;
  12488. $fill = '';
  12489. $color = $this->fgcolor;
  12490. $width = 0;
  12491. $textitem = '';
  12492. $tmpx = $this->x;
  12493. $lspace = $this->GetStringWidth(' ');
  12494. if ($listtype == '!') {
  12495. // set default list type for unordered list
  12496. $deftypes = array('disc', 'circle', 'square');
  12497. $listtype = $deftypes[($listdepth - 1) % 3];
  12498. } elseif ($listtype == '#') {
  12499. // set default list type for ordered list
  12500. $listtype = 'decimal';
  12501. }
  12502. switch ($listtype) {
  12503. // unordered types
  12504. case 'none': {
  12505. break;
  12506. }
  12507. case 'disc': {
  12508. $fill = 'F';
  12509. }
  12510. case 'circle': {
  12511. $fill .= 'D';
  12512. $r = $size / 6;
  12513. $lspace += (2 * $r);
  12514. if ($this->rtl) {
  12515. $this->x += $lspace;
  12516. } else {
  12517. $this->x -= $lspace;
  12518. }
  12519. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, $fill, array('color'=>$color), $color, 8);
  12520. break;
  12521. }
  12522. case 'square': {
  12523. $l = $size / 3;
  12524. $lspace += $l;
  12525. if ($this->rtl) {
  12526. $this->x += $lspace;
  12527. } else {
  12528. $this->x -= $lspace;
  12529. }
  12530. $this->Rect($this->x, ($this->y + (($this->lasth - $l)/ 2)), $l, $l, 'F', array(), $color);
  12531. break;
  12532. }
  12533. // ordered types
  12534. // $this->listcount[$this->listnum];
  12535. // $textitem
  12536. case '1':
  12537. case 'decimal': {
  12538. $textitem = $this->listcount[$this->listnum];
  12539. break;
  12540. }
  12541. case 'decimal-leading-zero': {
  12542. $textitem = sprintf("%02d", $this->listcount[$this->listnum]);
  12543. break;
  12544. }
  12545. case 'i':
  12546. case 'lower-roman': {
  12547. $textitem = strtolower($this->intToRoman($this->listcount[$this->listnum]));
  12548. break;
  12549. }
  12550. case 'I':
  12551. case 'upper-roman': {
  12552. $textitem = $this->intToRoman($this->listcount[$this->listnum]);
  12553. break;
  12554. }
  12555. case 'a':
  12556. case 'lower-alpha':
  12557. case 'lower-latin': {
  12558. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  12559. break;
  12560. }
  12561. case 'A':
  12562. case 'upper-alpha':
  12563. case 'upper-latin': {
  12564. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  12565. break;
  12566. }
  12567. case 'lower-greek': {
  12568. $textitem = $this->unichr(945 + $this->listcount[$this->listnum] - 1);
  12569. break;
  12570. }
  12571. /*
  12572. // Types to be implemented (special handling)
  12573. case 'hebrew': {
  12574. break;
  12575. }
  12576. case 'armenian': {
  12577. break;
  12578. }
  12579. case 'georgian': {
  12580. break;
  12581. }
  12582. case 'cjk-ideographic': {
  12583. break;
  12584. }
  12585. case 'hiragana': {
  12586. break;
  12587. }
  12588. case 'katakana': {
  12589. break;
  12590. }
  12591. case 'hiragana-iroha': {
  12592. break;
  12593. }
  12594. case 'katakana-iroha': {
  12595. break;
  12596. }
  12597. */
  12598. default: {
  12599. $textitem = $this->listcount[$this->listnum];
  12600. }
  12601. }
  12602. if (!$this->empty_string($textitem)) {
  12603. // print ordered item
  12604. if ($this->rtl) {
  12605. $textitem = '.'.$textitem;
  12606. } else {
  12607. $textitem = $textitem.'.';
  12608. }
  12609. $lspace += $this->GetStringWidth($textitem);
  12610. if ($this->rtl) {
  12611. $this->x += $lspace;
  12612. } else {
  12613. $this->x -= $lspace;
  12614. }
  12615. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  12616. }
  12617. $this->x = $tmpx;
  12618. $this->lispacer = '';
  12619. }
  12620. /**
  12621. * Returns current graphic variables as array.
  12622. * @return array graphic variables
  12623. * @access protected
  12624. * @since 4.2.010 (2008-11-14)
  12625. */
  12626. protected function getGraphicVars() {
  12627. $grapvars = array(
  12628. 'FontFamily' => $this->FontFamily,
  12629. 'FontStyle' => $this->FontStyle,
  12630. 'FontSizePt' => $this->FontSizePt,
  12631. 'rMargin' => $this->rMargin,
  12632. 'lMargin' => $this->lMargin,
  12633. 'cMargin' => $this->cMargin,
  12634. 'LineWidth' => $this->LineWidth,
  12635. 'linestyleWidth' => $this->linestyleWidth,
  12636. 'linestyleCap' => $this->linestyleCap,
  12637. 'linestyleJoin' => $this->linestyleJoin,
  12638. 'linestyleDash' => $this->linestyleDash,
  12639. 'DrawColor' => $this->DrawColor,
  12640. 'FillColor' => $this->FillColor,
  12641. 'TextColor' => $this->TextColor,
  12642. 'ColorFlag' => $this->ColorFlag,
  12643. 'bgcolor' => $this->bgcolor,
  12644. 'fgcolor' => $this->fgcolor,
  12645. 'htmlvspace' => $this->htmlvspace,
  12646. 'lasth' => $this->lasth
  12647. );
  12648. return $grapvars;
  12649. }
  12650. /**
  12651. * Set graphic variables.
  12652. * @param $gvars array graphic variables
  12653. * @access protected
  12654. * @since 4.2.010 (2008-11-14)
  12655. */
  12656. protected function setGraphicVars($gvars) {
  12657. $this->FontFamily = $gvars['FontFamily'];
  12658. $this->FontStyle = $gvars['FontStyle'];
  12659. $this->FontSizePt = $gvars['FontSizePt'];
  12660. $this->rMargin = $gvars['rMargin'];
  12661. $this->lMargin = $gvars['lMargin'];
  12662. $this->cMargin = $gvars['cMargin'];
  12663. $this->LineWidth = $gvars['LineWidth'];
  12664. $this->linestyleWidth = $gvars['linestyleWidth'];
  12665. $this->linestyleCap = $gvars['linestyleCap'];
  12666. $this->linestyleJoin = $gvars['linestyleJoin'];
  12667. $this->linestyleDash = $gvars['linestyleDash'];
  12668. $this->DrawColor = $gvars['DrawColor'];
  12669. $this->FillColor = $gvars['FillColor'];
  12670. $this->TextColor = $gvars['TextColor'];
  12671. $this->ColorFlag = $gvars['ColorFlag'];
  12672. $this->bgcolor = $gvars['bgcolor'];
  12673. $this->fgcolor = $gvars['fgcolor'];
  12674. $this->htmlvspace = $gvars['htmlvspace'];
  12675. //$this->lasth = $gvars['lasth'];
  12676. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  12677. if (!$this->empty_string($this->FontFamily)) {
  12678. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  12679. }
  12680. }
  12681. /**
  12682. * Returns a temporary filename for caching object on filesystem.
  12683. * @param string $prefix prefix to add to filename
  12684. * return string filename.
  12685. * @access protected
  12686. * @since 4.5.000 (2008-12-31)
  12687. */
  12688. protected function getObjFilename($name) {
  12689. return tempnam(K_PATH_CACHE, $name.'_');
  12690. }
  12691. /**
  12692. * Writes data to a temporary file on filesystem.
  12693. * @param string $file file name
  12694. * @param mixed $data data to write on file
  12695. * @param boolean $append if true append data, false replace.
  12696. * @access protected
  12697. * @since 4.5.000 (2008-12-31)
  12698. */
  12699. protected function writeDiskCache($filename, $data, $append=false) {
  12700. if ($append) {
  12701. $fmode = 'ab+';
  12702. } else {
  12703. $fmode = 'wb+';
  12704. }
  12705. $f = @fopen($filename, $fmode);
  12706. if (!$f) {
  12707. $this->Error('Unable to write cache file: '.$filename);
  12708. } else {
  12709. fwrite($f, $data);
  12710. fclose($f);
  12711. }
  12712. // update file lenght (needed for transactions)
  12713. if (!isset($this->cache_file_lenght['_'.$filename])) {
  12714. $this->cache_file_lenght['_'.$filename] = strlen($data);
  12715. } else {
  12716. $this->cache_file_lenght['_'.$filename] += strlen($data);
  12717. }
  12718. }
  12719. /**
  12720. * Read data from a temporary file on filesystem.
  12721. * @param string $file file name
  12722. * @return mixed retrieved data
  12723. * @access protected
  12724. * @since 4.5.000 (2008-12-31)
  12725. */
  12726. protected function readDiskCache($filename) {
  12727. return file_get_contents($filename);
  12728. }
  12729. /**
  12730. * Set buffer content (always append data).
  12731. * @param string $data data
  12732. * @access protected
  12733. * @since 4.5.000 (2009-01-02)
  12734. */
  12735. protected function setBuffer($data) {
  12736. $this->bufferlen += strlen($data);
  12737. if ($this->diskcache) {
  12738. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  12739. $this->buffer = $this->getObjFilename('buffer');
  12740. }
  12741. $this->writeDiskCache($this->buffer, $data, true);
  12742. } else {
  12743. $this->buffer .= $data;
  12744. }
  12745. }
  12746. /**
  12747. * Get buffer content.
  12748. * @return string buffer content
  12749. * @access protected
  12750. * @since 4.5.000 (2009-01-02)
  12751. */
  12752. protected function getBuffer() {
  12753. if ($this->diskcache) {
  12754. return $this->readDiskCache($this->buffer);
  12755. } else {
  12756. return $this->buffer;
  12757. }
  12758. }
  12759. /**
  12760. * Set page buffer content.
  12761. * @param int $page page number
  12762. * @param string $data page data
  12763. * @param boolean $append if true append data, false replace.
  12764. * @access protected
  12765. * @since 4.5.000 (2008-12-31)
  12766. */
  12767. protected function setPageBuffer($page, $data, $append=false) {
  12768. if ($this->diskcache) {
  12769. if (!isset($this->pages[$page])) {
  12770. $this->pages[$page] = $this->getObjFilename('page'.$page);
  12771. }
  12772. $this->writeDiskCache($this->pages[$page], $data, $append);
  12773. } else {
  12774. if ($append) {
  12775. $this->pages[$page] .= $data;
  12776. } else {
  12777. $this->pages[$page] = $data;
  12778. }
  12779. }
  12780. if ($append AND isset($this->pagelen[$page])) {
  12781. $this->pagelen[$page] += strlen($data);
  12782. } else {
  12783. $this->pagelen[$page] = strlen($data);
  12784. }
  12785. }
  12786. /**
  12787. * Get page buffer content.
  12788. * @param int $page page number
  12789. * @return string page buffer content or false in case of error
  12790. * @access protected
  12791. * @since 4.5.000 (2008-12-31)
  12792. */
  12793. protected function getPageBuffer($page) {
  12794. if ($this->diskcache) {
  12795. return $this->readDiskCache($this->pages[$page]);
  12796. } elseif (isset($this->pages[$page])) {
  12797. return $this->pages[$page];
  12798. }
  12799. return false;
  12800. }
  12801. /**
  12802. * Set image buffer content.
  12803. * @param string $image image key
  12804. * @param array $data image data
  12805. * @access protected
  12806. * @since 4.5.000 (2008-12-31)
  12807. */
  12808. protected function setImageBuffer($image, $data) {
  12809. if ($this->diskcache) {
  12810. if (!isset($this->images[$image])) {
  12811. $this->images[$image] = $this->getObjFilename('image'.$image);
  12812. }
  12813. $this->writeDiskCache($this->images[$image], serialize($data));
  12814. } else {
  12815. $this->images[$image] = $data;
  12816. }
  12817. if (!in_array($image, $this->imagekeys)) {
  12818. $this->imagekeys[] = $image;
  12819. }
  12820. ++$this->numimages;
  12821. }
  12822. /**
  12823. * Set image buffer content.
  12824. * @param string $image image key
  12825. * @param string $key image sub-key
  12826. * @param array $data image data
  12827. * @access protected
  12828. * @since 4.5.000 (2008-12-31)
  12829. */
  12830. protected function setImageSubBuffer($image, $key, $data) {
  12831. if (!isset($this->images[$image])) {
  12832. $this->setImageBuffer($image, array());
  12833. }
  12834. if ($this->diskcache) {
  12835. $tmpimg = $this->getImageBuffer($image);
  12836. $tmpimg[$key] = $data;
  12837. $this->writeDiskCache($this->images[$image], serialize($tmpimg));
  12838. } else {
  12839. $this->images[$image][$key] = $data;
  12840. }
  12841. }
  12842. /**
  12843. * Get page buffer content.
  12844. * @param string $image image key
  12845. * @return string image buffer content or false in case of error
  12846. * @access protected
  12847. * @since 4.5.000 (2008-12-31)
  12848. */
  12849. protected function getImageBuffer($image) {
  12850. if ($this->diskcache AND isset($this->images[$image])) {
  12851. return unserialize($this->readDiskCache($this->images[$image]));
  12852. } elseif (isset($this->images[$image])) {
  12853. return $this->images[$image];
  12854. }
  12855. return false;
  12856. }
  12857. /**
  12858. * Set font buffer content.
  12859. * @param string $font font key
  12860. * @param array $data font data
  12861. * @access protected
  12862. * @since 4.5.000 (2009-01-02)
  12863. */
  12864. protected function setFontBuffer($font, $data) {
  12865. if ($this->diskcache) {
  12866. if (!isset($this->fonts[$font])) {
  12867. $this->fonts[$font] = $this->getObjFilename('font');
  12868. }
  12869. $this->writeDiskCache($this->fonts[$font], serialize($data));
  12870. } else {
  12871. $this->fonts[$font] = $data;
  12872. }
  12873. if (!in_array($font, $this->fontkeys)) {
  12874. $this->fontkeys[] = $font;
  12875. }
  12876. }
  12877. /**
  12878. * Set font buffer content.
  12879. * @param string $font font key
  12880. * @param string $key font sub-key
  12881. * @param array $data font data
  12882. * @access protected
  12883. * @since 4.5.000 (2009-01-02)
  12884. */
  12885. protected function setFontSubBuffer($font, $key, $data) {
  12886. if (!isset($this->fonts[$font])) {
  12887. $this->setFontBuffer($font, array());
  12888. }
  12889. if ($this->diskcache) {
  12890. $tmpfont = $this->getFontBuffer($font);
  12891. $tmpfont[$key] = $data;
  12892. $this->writeDiskCache($this->fonts[$font], serialize($tmpfont));
  12893. } else {
  12894. $this->fonts[$font][$key] = $data;
  12895. }
  12896. }
  12897. /**
  12898. * Get font buffer content.
  12899. * @param string $font font key
  12900. * @return string font buffer content or false in case of error
  12901. * @access protected
  12902. * @since 4.5.000 (2009-01-02)
  12903. */
  12904. protected function getFontBuffer($font) {
  12905. if ($this->diskcache AND isset($this->fonts[$font])) {
  12906. return unserialize($this->readDiskCache($this->fonts[$font]));
  12907. } elseif (isset($this->fonts[$font])) {
  12908. return $this->fonts[$font];
  12909. }
  12910. return false;
  12911. }
  12912. /**
  12913. * Move a page to a previous position.
  12914. * @param int $frompage number of the source page
  12915. * @param int $topage number of the destination page (must be less than $frompage)
  12916. * @return true in case of success, false in case of error.
  12917. * @access public
  12918. * @since 4.5.000 (2009-01-02)
  12919. */
  12920. public function movePage($frompage, $topage) {
  12921. if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
  12922. return false;
  12923. }
  12924. if ($frompage == $this->page) {
  12925. // close the page before moving it
  12926. $this->endPage();
  12927. }
  12928. // move all page-related states
  12929. $tmppage = $this->pages[$frompage];
  12930. $tmppagedim = $this->pagedim[$frompage];
  12931. $tmppagelen = $this->pagelen[$frompage];
  12932. $tmpintmrk = $this->intmrk[$frompage];
  12933. if (isset($this->footerpos[$frompage])) {
  12934. $tmpfooterpos = $this->footerpos[$frompage];
  12935. }
  12936. if (isset($this->footerlen[$frompage])) {
  12937. $tmpfooterlen = $this->footerlen[$frompage];
  12938. }
  12939. if (isset($this->transfmrk[$frompage])) {
  12940. $tmptransfmrk = $this->transfmrk[$frompage];
  12941. }
  12942. if (isset($this->PageAnnots[$frompage])) {
  12943. $tmpannots = $this->PageAnnots[$frompage];
  12944. }
  12945. if (isset($this->newpagegroup[$frompage])) {
  12946. $tmpnewpagegroup = $this->newpagegroup[$frompage];
  12947. }
  12948. for ($i = $frompage; $i > $topage; --$i) {
  12949. $j = $i - 1;
  12950. // shift pages down
  12951. $this->pages[$i] = $this->pages[$j];
  12952. $this->pagedim[$i] = $this->pagedim[$j];
  12953. $this->pagelen[$i] = $this->pagelen[$j];
  12954. $this->intmrk[$i] = $this->intmrk[$j];
  12955. if (isset($this->footerpos[$j])) {
  12956. $this->footerpos[$i] = $this->footerpos[$j];
  12957. } elseif (isset($this->footerpos[$i])) {
  12958. unset($this->footerpos[$i]);
  12959. }
  12960. if (isset($this->footerlen[$j])) {
  12961. $this->footerlen[$i] = $this->footerlen[$j];
  12962. } elseif (isset($this->footerlen[$i])) {
  12963. unset($this->footerlen[$i]);
  12964. }
  12965. if (isset($this->transfmrk[$j])) {
  12966. $this->transfmrk[$i] = $this->transfmrk[$j];
  12967. } elseif (isset($this->transfmrk[$i])) {
  12968. unset($this->transfmrk[$i]);
  12969. }
  12970. if (isset($this->PageAnnots[$j])) {
  12971. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  12972. } elseif (isset($this->PageAnnots[$i])) {
  12973. unset($this->PageAnnots[$i]);
  12974. }
  12975. if (isset($this->newpagegroup[$j])) {
  12976. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  12977. } elseif (isset($this->newpagegroup[$i])) {
  12978. unset($this->newpagegroup[$i]);
  12979. }
  12980. }
  12981. $this->pages[$topage] = $tmppage;
  12982. $this->pagedim[$topage] = $tmppagedim;
  12983. $this->pagelen[$topage] = $tmppagelen;
  12984. $this->intmrk[$topage] = $tmpintmrk;
  12985. if (isset($tmpfooterpos)) {
  12986. $this->footerpos[$topage] = $tmpfooterpos;
  12987. } elseif (isset($this->footerpos[$topage])) {
  12988. unset($this->footerpos[$topage]);
  12989. }
  12990. if (isset($tmpfooterlen)) {
  12991. $this->footerlen[$topage] = $tmpfooterlen;
  12992. } elseif (isset($this->footerlen[$topage])) {
  12993. unset($this->footerlen[$topage]);
  12994. }
  12995. if (isset($tmptransfmrk)) {
  12996. $this->transfmrk[$topage] = $tmptransfmrk;
  12997. } elseif (isset($this->transfmrk[$topage])) {
  12998. unset($this->transfmrk[$topage]);
  12999. }
  13000. if (isset($tmpannots)) {
  13001. $this->PageAnnots[$topage] = $tmpannots;
  13002. } elseif (isset($this->PageAnnots[$topage])) {
  13003. unset($this->PageAnnots[$topage]);
  13004. }
  13005. if (isset($tmpnewpagegroup)) {
  13006. $this->newpagegroup[$topage] = $tmpnewpagegroup;
  13007. } elseif (isset($this->newpagegroup[$topage])) {
  13008. unset($this->newpagegroup[$topage]);
  13009. }
  13010. // adjust outlines
  13011. $tmpoutlines = $this->outlines;
  13012. foreach ($tmpoutlines as $key => $outline) {
  13013. if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
  13014. $this->outlines[$key]['p'] = $outline['p'] + 1;
  13015. } elseif ($outline['p'] == $frompage) {
  13016. $this->outlines[$key]['p'] = $topage;
  13017. }
  13018. }
  13019. // adjust links
  13020. $tmplinks = $this->links;
  13021. foreach ($tmplinks as $key => $link) {
  13022. if (($link[0] >= $topage) AND ($link[0] < $frompage)) {
  13023. $this->links[$key][0] = $link[0] + 1;
  13024. } elseif ($link[0] == $frompage) {
  13025. $this->links[$key][0] = $topage;
  13026. }
  13027. }
  13028. // adjust javascript
  13029. $tmpjavascript = $this->javascript;
  13030. global $jfrompage, $jtopage;
  13031. $jfrompage = $frompage;
  13032. $jtopage = $topage;
  13033. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  13034. create_function('$matches', 'global $jfrompage, $jtopage;
  13035. $pagenum = intval($matches[3]) + 1;
  13036. if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
  13037. $newpage = ($pagenum + 1);
  13038. } elseif ($pagenum == $jfrompage) {
  13039. $newpage = $jtopage;
  13040. } else {
  13041. $newpage = $pagenum;
  13042. }
  13043. --$newpage;
  13044. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  13045. // return to last page
  13046. $this->lastPage(true);
  13047. return true;
  13048. }
  13049. /**
  13050. * Remove the specified page.
  13051. * @param int $page page to remove
  13052. * @return true in case of success, false in case of error.
  13053. * @access public
  13054. * @since 4.6.004 (2009-04-23)
  13055. */
  13056. public function deletePage($page) {
  13057. if ($page > $this->numpages) {
  13058. return false;
  13059. }
  13060. // delete current page
  13061. unset($this->pages[$page]);
  13062. unset($this->pagedim[$page]);
  13063. unset($this->pagelen[$page]);
  13064. unset($this->intmrk[$page]);
  13065. if (isset($this->footerpos[$page])) {
  13066. unset($this->footerpos[$page]);
  13067. }
  13068. if (isset($this->footerlen[$page])) {
  13069. unset($this->footerlen[$page]);
  13070. }
  13071. if (isset($this->transfmrk[$page])) {
  13072. unset($this->transfmrk[$page]);
  13073. }
  13074. if (isset($this->PageAnnots[$page])) {
  13075. unset($this->PageAnnots[$page]);
  13076. }
  13077. if (isset($this->newpagegroup[$page])) {
  13078. unset($this->newpagegroup[$page]);
  13079. }
  13080. if (isset($this->pageopen[$page])) {
  13081. unset($this->pageopen[$page]);
  13082. }
  13083. // update remaining pages
  13084. for ($i = $page; $i < $this->numpages; ++$i) {
  13085. $j = $i + 1;
  13086. // shift pages
  13087. $this->pages[$i] = $this->pages[$j];
  13088. $this->pagedim[$i] = $this->pagedim[$j];
  13089. $this->pagelen[$i] = $this->pagelen[$j];
  13090. $this->intmrk[$i] = $this->intmrk[$j];
  13091. if (isset($this->footerpos[$j])) {
  13092. $this->footerpos[$i] = $this->footerpos[$j];
  13093. } elseif (isset($this->footerpos[$i])) {
  13094. unset($this->footerpos[$i]);
  13095. }
  13096. if (isset($this->footerlen[$j])) {
  13097. $this->footerlen[$i] = $this->footerlen[$j];
  13098. } elseif (isset($this->footerlen[$i])) {
  13099. unset($this->footerlen[$i]);
  13100. }
  13101. if (isset($this->transfmrk[$j])) {
  13102. $this->transfmrk[$i] = $this->transfmrk[$j];
  13103. } elseif (isset($this->transfmrk[$i])) {
  13104. unset($this->transfmrk[$i]);
  13105. }
  13106. if (isset($this->PageAnnots[$j])) {
  13107. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  13108. } elseif (isset($this->PageAnnots[$i])) {
  13109. unset($this->PageAnnots[$i]);
  13110. }
  13111. if (isset($this->newpagegroup[$j])) {
  13112. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  13113. } elseif (isset($this->newpagegroup[$i])) {
  13114. unset($this->newpagegroup[$i]);
  13115. }
  13116. if (isset($this->pageopen[$j])) {
  13117. $this->pageopen[$i] = $this->pageopen[$j];
  13118. } elseif (isset($this->pageopen[$i])) {
  13119. unset($this->pageopen[$i]);
  13120. }
  13121. }
  13122. // remove last page
  13123. unset($this->pages[$this->numpages]);
  13124. unset($this->pagedim[$this->numpages]);
  13125. unset($this->pagelen[$this->numpages]);
  13126. unset($this->intmrk[$this->numpages]);
  13127. if (isset($this->footerpos[$this->numpages])) {
  13128. unset($this->footerpos[$this->numpages]);
  13129. }
  13130. if (isset($this->footerlen[$this->numpages])) {
  13131. unset($this->footerlen[$this->numpages]);
  13132. }
  13133. if (isset($this->transfmrk[$this->numpages])) {
  13134. unset($this->transfmrk[$this->numpages]);
  13135. }
  13136. if (isset($this->PageAnnots[$this->numpages])) {
  13137. unset($this->PageAnnots[$this->numpages]);
  13138. }
  13139. if (isset($this->newpagegroup[$this->numpages])) {
  13140. unset($this->newpagegroup[$this->numpages]);
  13141. }
  13142. if (isset($this->pageopen[$this->numpages])) {
  13143. unset($this->pageopen[$this->numpages]);
  13144. }
  13145. --$this->numpages;
  13146. $this->page = $this->numpages;
  13147. // adjust outlines
  13148. $tmpoutlines = $this->outlines;
  13149. foreach ($tmpoutlines as $key => $outline) {
  13150. if ($outline['p'] > $page) {
  13151. $this->outlines[$key]['p'] = $outline['p'] - 1;
  13152. } elseif ($outline['p'] == $page) {
  13153. unset($this->outlines[$key]);
  13154. }
  13155. }
  13156. // adjust links
  13157. $tmplinks = $this->links;
  13158. foreach ($tmplinks as $key => $link) {
  13159. if ($link[0] > $page) {
  13160. $this->links[$key][0] = $link[0] - 1;
  13161. } elseif ($link[0] == $page) {
  13162. unset($this->links[$key]);
  13163. }
  13164. }
  13165. // adjust javascript
  13166. $tmpjavascript = $this->javascript;
  13167. global $jpage;
  13168. $jpage = $page;
  13169. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  13170. create_function('$matches', 'global $jpage;
  13171. $pagenum = intval($matches[3]) + 1;
  13172. if ($pagenum >= $jpage) {
  13173. $newpage = ($pagenum - 1);
  13174. } elseif ($pagenum == $jpage) {
  13175. $newpage = 1;
  13176. } else {
  13177. $newpage = $pagenum;
  13178. }
  13179. --$newpage;
  13180. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  13181. // return to last page
  13182. $this->lastPage(true);
  13183. return true;
  13184. }
  13185. /**
  13186. * Output a Table of Content Index (TOC).
  13187. * You can override this method to achieve different styles.
  13188. * @param int $page page number where this TOC should be inserted (leave empty for current page).
  13189. * @param string $numbersfont set the font for page numbers (please use monospaced font for better alignment).
  13190. * @param string $filler string used to fill the space between text and page number.
  13191. * @access public
  13192. * @author Nicola Asuni
  13193. * @since 4.5.000 (2009-01-02)
  13194. */
  13195. public function addTOC($page='', $numbersfont='', $filler='.') {
  13196. $fontsize = $this->FontSizePt;
  13197. $fontfamily = $this->FontFamily;
  13198. $fontstyle = $this->FontStyle;
  13199. $w = $this->w - $this->lMargin - $this->rMargin;
  13200. $spacer = $this->GetStringWidth(' ') * 4;
  13201. $page_first = $this->getPage();
  13202. $lmargin = $this->lMargin;
  13203. $rmargin = $this->rMargin;
  13204. $x_start = $this->GetX();
  13205. if ($this->empty_string($numbersfont)) {
  13206. $numbersfont = $this->default_monospaced_font;
  13207. }
  13208. if ($this->empty_string($filler)) {
  13209. $filler = ' ';
  13210. }
  13211. if ($this->empty_string($page)) {
  13212. $gap = ' ';
  13213. } else {
  13214. $gap = '';
  13215. }
  13216. foreach ($this->outlines as $key => $outline) {
  13217. if ($this->rtl) {
  13218. $aligntext = 'R';
  13219. $alignnum = 'L';
  13220. } else {
  13221. $aligntext = 'L';
  13222. $alignnum = 'R';
  13223. }
  13224. if ($outline['l'] == 0) {
  13225. $this->