PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 2ms

/administrator/components/com_breezingforms/libraries/tcpdf/tcpdf.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 13920 lines | 8797 code | 530 blank | 4593 comment | 2046 complexity | 5b3ba900168d878e0a3b27106a20002e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. defined('_JEXEC') or die('Direct Access to this location is not allowed.');
  3. //============================================================+
  4. // File name : tcpdf.php
  5. // Begin : 2002-08-03
  6. // Last Update : 2009-11-27
  7. // Author : Nicola Asuni - info@tecnick.com - http://www.tcpdf.org
  8. // Version : 4.8.017
  9. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  10. // ----------------------------------------------------------------------------
  11. // Copyright (C) 2002-2009 Nicola Asuni - Tecnick.com S.r.l.
  12. //
  13. // This program is free software: you can redistribute it and/or modify
  14. // it under the terms of the GNU Lesser General Public License as published by
  15. // the Free Software Foundation, either version 2.1 of the License, or
  16. // (at your option) any later version.
  17. //
  18. // This program is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. // GNU Lesser General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Lesser General Public License
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. //
  26. // See LICENSE.TXT file for more information.
  27. // ----------------------------------------------------------------------------
  28. //
  29. // Description : This is a PHP class for generating PDF documents without
  30. // requiring external extensions.
  31. //
  32. // NOTE:
  33. // This class was originally derived in 2002 from the Public
  34. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  35. // but now is almost entirely rewritten.
  36. //
  37. // Main features:
  38. // * no external libraries are required for the basic functions;
  39. // * supports all ISO page formats;
  40. // * supports custom page formats, margins and units of measure;
  41. // * supports UTF-8 Unicode and Right-To-Left languages;
  42. // * supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  43. // * supports document encryption;
  44. // * includes methods to publish some XHTML code, including forms;
  45. // * includes graphic (geometric) and transformation methods;
  46. // * includes Javascript and Forms support;
  47. // * 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;
  48. // * includes methods to set Bookmarks and print a Table of Content;
  49. // * includes methods to move and delete pages;
  50. // * includes methods for automatic page header and footer management;
  51. // * supports automatic page break;
  52. // * supports automatic page numbering and page groups;
  53. // * supports automatic line break and text justification;
  54. // * 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)
  55. // * supports stroke and clipping mode for text;
  56. // * supports clipping masks;
  57. // * supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  58. // * supports several annotations, including links, text and file attachments;
  59. // * supports page compression (requires zlib extension);
  60. // * supports text hyphenation.
  61. // * supports transactions to UNDO commands.
  62. // * supports signature certifications.
  63. //
  64. // -----------------------------------------------------------
  65. // THANKS TO:
  66. //
  67. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  68. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  69. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  70. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  71. // dullus for text Justification.
  72. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  73. // Patrick Benny for text stretch suggestion on Cell().
  74. // Johannes G�ntert for JavaScript support.
  75. // Denis Van Nuffelen for Dynamic Form.
  76. // Jacek Czekaj for multibyte justification
  77. // Anthony Ferrara for the reintroduction of legacy image methods.
  78. // Sourceforge user 1707880 (hucste) for line-trough mode.
  79. // Larry Stanbery for page groups.
  80. // Martin Hall-May for transparency.
  81. // Aaron C. Spike for Polycurve method.
  82. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  83. // Moritz Wagner and Andreas Wurmser for graphic functions.
  84. // Andrew Whitehead for core fonts support.
  85. // Esteban Jo�l Mar�n for OpenType font conversion.
  86. // Teus Hagen for several suggestions and fixes.
  87. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  88. // Kosmas Papachristos for some CSS improvements.
  89. // Marcel Partap for some fixes.
  90. // Won Kyu Park for several suggestions, fixes and patches.
  91. // Anyone that has reported a bug or sent a suggestion.
  92. //============================================================+
  93. /**
  94. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  95. * 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>
  96. * <h3>TCPDF main features are:</h3>
  97. * <ul>
  98. * <li>no external libraries are required for the basic functions;</li>
  99. * <li>supports all ISO page formats;</li>
  100. * <li>supports custom page formats, margins and units of measure;</li>
  101. * <li>supports UTF-8 Unicode and Right-To-Left languages;</li>
  102. * <li>supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;</li>
  103. * <li>supports document encryption;</li>
  104. * <li>includes methods to publish some XHTML code, including forms;</li>
  105. * <li>includes graphic (geometric) and transformation methods;</li>
  106. * <li>includes Javascript and Forms support;</li>
  107. * <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>
  108. * <li>includes methods to set Bookmarks and print a Table of Content;</li>
  109. * <li>includes methods to move and delete pages;</li>
  110. * <li>includes methods for automatic page header and footer management;</li>
  111. * <li>supports automatic page break;</li>
  112. * <li>supports automatic page numbering and page groups;</li>
  113. * <li>supports automatic line break and text justification;</li>
  114. * <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>
  115. * <li>supports stroke and clipping mode for text;</li>
  116. * <li>supports clipping masks;</li>
  117. * <li>supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  118. * <li>supports several annotations, including links, text and file attachments;</li>
  119. * <li>supports page compression (requires zlib extension);</li>
  120. * <li>supports text hyphenation.</li>
  121. * <li>supports transactions to UNDO commands.</li>
  122. * <li>supports signature certifications.</li>
  123. * </ul>
  124. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  125. * @package com.tecnick.tcpdf
  126. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  127. * @author Nicola Asuni
  128. * @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
  129. * @link http://www.tcpdf.org
  130. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  131. * @version 4.8.017
  132. */
  133. /**
  134. * main configuration file
  135. */
  136. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  137. // includes some support files
  138. /**
  139. * unicode data
  140. */
  141. require_once(dirname(__FILE__).'/unicode_data.php');
  142. /**
  143. * html colors table
  144. */
  145. require_once(dirname(__FILE__).'/htmlcolors.php');
  146. if (!class_exists('TCPDF', false)) {
  147. /**
  148. * define default PDF document producer
  149. */
  150. define('PDF_PRODUCER', 'TCPDF 4.8.017 (http://www.tcpdf.org)');
  151. /**
  152. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  153. * 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>
  154. * @name TCPDF
  155. * @package com.tecnick.tcpdf
  156. * @version 4.8.017
  157. * @author Nicola Asuni - info@tecnick.com
  158. * @link http://www.tcpdf.org
  159. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  160. */
  161. class TCPDF {
  162. // protected or Protected properties
  163. /**
  164. * @var current page number
  165. * @access protected
  166. */
  167. protected $page;
  168. /**
  169. * @var current object number
  170. * @access protected
  171. */
  172. protected $n;
  173. /**
  174. * @var array of object offsets
  175. * @access protected
  176. */
  177. protected $offsets;
  178. /**
  179. * @var buffer holding in-memory PDF
  180. * @access protected
  181. */
  182. protected $buffer;
  183. /**
  184. * @var array containing pages
  185. * @access protected
  186. */
  187. protected $pages = array();
  188. /**
  189. * @var current document state
  190. * @access protected
  191. */
  192. protected $state;
  193. /**
  194. * @var compression flag
  195. * @access protected
  196. */
  197. protected $compress;
  198. /**
  199. * @var current page orientation (P = Portrait, L = Landscape)
  200. * @access protected
  201. */
  202. protected $CurOrientation;
  203. /**
  204. * @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>
  205. * @access protected
  206. */
  207. protected $pagedim = array();
  208. /**
  209. * @var scale factor (number of points in user unit)
  210. * @access protected
  211. */
  212. protected $k;
  213. /**
  214. * @var width of page format in points
  215. * @access protected
  216. */
  217. protected $fwPt;
  218. /**
  219. * @var height of page format in points
  220. * @access protected
  221. */
  222. protected $fhPt;
  223. /**
  224. * @var current width of page in points
  225. * @access protected
  226. */
  227. protected $wPt;
  228. /**
  229. * @var current height of page in points
  230. * @access protected
  231. */
  232. protected $hPt;
  233. /**
  234. * @var current width of page in user unit
  235. * @access protected
  236. */
  237. protected $w;
  238. /**
  239. * @var current height of page in user unit
  240. * @access protected
  241. */
  242. protected $h;
  243. /**
  244. * @var left margin
  245. * @access protected
  246. */
  247. protected $lMargin;
  248. /**
  249. * @var top margin
  250. * @access protected
  251. */
  252. protected $tMargin;
  253. /**
  254. * @var right margin
  255. * @access protected
  256. */
  257. protected $rMargin;
  258. /**
  259. * @var page break margin
  260. * @access protected
  261. */
  262. protected $bMargin;
  263. /**
  264. * @var cell internal padding
  265. * @access protected
  266. */
  267. //protected
  268. public $cMargin;
  269. /**
  270. * @var cell internal padding (previous value)
  271. * @access protected
  272. */
  273. protected $oldcMargin;
  274. /**
  275. * @var current horizontal position in user unit for cell positioning
  276. * @access protected
  277. */
  278. protected $x;
  279. /**
  280. * @var current vertical position in user unit for cell positioning
  281. * @access protected
  282. */
  283. protected $y;
  284. /**
  285. * @var height of last cell printed
  286. * @access protected
  287. */
  288. protected $lasth;
  289. /**
  290. * @var line width in user unit
  291. * @access protected
  292. */
  293. protected $LineWidth;
  294. /**
  295. * @var array of standard font names
  296. * @access protected
  297. */
  298. protected $CoreFonts;
  299. /**
  300. * @var array of used fonts
  301. * @access protected
  302. */
  303. protected $fonts = array();
  304. /**
  305. * @var array of font files
  306. * @access protected
  307. */
  308. protected $FontFiles = array();
  309. /**
  310. * @var array of encoding differences
  311. * @access protected
  312. */
  313. protected $diffs = array();
  314. /**
  315. * @var array of used images
  316. * @access protected
  317. */
  318. protected $images = array();
  319. /**
  320. * @var array of Annotations in pages
  321. * @access protected
  322. */
  323. protected $PageAnnots = array();
  324. /**
  325. * @var array of internal links
  326. * @access protected
  327. */
  328. protected $links = array();
  329. /**
  330. * @var current font family
  331. * @access protected
  332. */
  333. protected $FontFamily;
  334. /**
  335. * @var current font style
  336. * @access protected
  337. */
  338. protected $FontStyle;
  339. /**
  340. * @var current font ascent (distance between font top and baseline)
  341. * @access protected
  342. * @since 2.8.000 (2007-03-29)
  343. */
  344. protected $FontAscent;
  345. /**
  346. * @var current font descent (distance between font bottom and baseline)
  347. * @access protected
  348. * @since 2.8.000 (2007-03-29)
  349. */
  350. protected $FontDescent;
  351. /**
  352. * @var underlining flag
  353. * @access protected
  354. */
  355. protected $underline;
  356. /**
  357. * @var current font info
  358. * @access protected
  359. */
  360. protected $CurrentFont;
  361. /**
  362. * @var current font size in points
  363. * @access protected
  364. */
  365. protected $FontSizePt;
  366. /**
  367. * @var current font size in user unit
  368. * @access protected
  369. */
  370. protected $FontSize;
  371. /**
  372. * @var commands for drawing color
  373. * @access protected
  374. */
  375. protected $DrawColor;
  376. /**
  377. * @var commands for filling color
  378. * @access protected
  379. */
  380. protected $FillColor;
  381. /**
  382. * @var commands for text color
  383. * @access protected
  384. */
  385. protected $TextColor;
  386. /**
  387. * @var indicates whether fill and text colors are different
  388. * @access protected
  389. */
  390. protected $ColorFlag;
  391. /**
  392. * @var automatic page breaking
  393. * @access protected
  394. */
  395. protected $AutoPageBreak;
  396. /**
  397. * @var threshold used to trigger page breaks
  398. * @access protected
  399. */
  400. protected $PageBreakTrigger;
  401. /**
  402. * @var flag set when processing footer
  403. * @access protected
  404. */
  405. protected $InFooter = false;
  406. /**
  407. * @var zoom display mode
  408. * @access protected
  409. */
  410. protected $ZoomMode;
  411. /**
  412. * @var layout display mode
  413. * @access protected
  414. */
  415. protected $LayoutMode;
  416. /**
  417. * @var title
  418. * @access protected
  419. */
  420. protected $title = '';
  421. /**
  422. * @var subject
  423. * @access protected
  424. */
  425. protected $subject = '';
  426. /**
  427. * @var author
  428. * @access protected
  429. */
  430. protected $author = '';
  431. /**
  432. * @var keywords
  433. * @access protected
  434. */
  435. protected $keywords = '';
  436. /**
  437. * @var creator
  438. * @access protected
  439. */
  440. protected $creator = '';
  441. /**
  442. * @var alias for total number of pages
  443. * @access protected
  444. */
  445. protected $AliasNbPages = '{nb}';
  446. /**
  447. * @var alias for page number
  448. * @access protected
  449. */
  450. protected $AliasNumPage = '{pnb}';
  451. /**
  452. * @var right-bottom corner X coordinate of inserted image
  453. * @since 2002-07-31
  454. * @author Nicola Asuni
  455. * @access protected
  456. */
  457. protected $img_rb_x;
  458. /**
  459. * @var right-bottom corner Y coordinate of inserted image
  460. * @since 2002-07-31
  461. * @author Nicola Asuni
  462. * @access protected
  463. */
  464. protected $img_rb_y;
  465. /**
  466. * @var adjusting factor to convert pixels to user units.
  467. * @since 2004-06-14
  468. * @author Nicola Asuni
  469. * @access protected
  470. */
  471. protected $imgscale = 1;
  472. /**
  473. * @var boolean set to true when the input text is unicode (require unicode fonts)
  474. * @since 2005-01-02
  475. * @author Nicola Asuni
  476. * @access protected
  477. */
  478. protected $isunicode = false;
  479. /**
  480. * @var PDF version
  481. * @since 1.5.3
  482. * @access protected
  483. */
  484. protected $PDFVersion = '1.7';
  485. // ----------------------
  486. /**
  487. * @var Minimum distance between header and top page margin.
  488. * @access protected
  489. */
  490. protected $header_margin;
  491. /**
  492. * @var Minimum distance between footer and bottom page margin.
  493. * @access protected
  494. */
  495. protected $footer_margin;
  496. /**
  497. * @var original left margin value
  498. * @access protected
  499. * @since 1.53.0.TC013
  500. */
  501. protected $original_lMargin;
  502. /**
  503. * @var original right margin value
  504. * @access protected
  505. * @since 1.53.0.TC013
  506. */
  507. protected $original_rMargin;
  508. /**
  509. * @var Header font.
  510. * @access protected
  511. */
  512. protected $header_font;
  513. /**
  514. * @var Footer font.
  515. * @access protected
  516. */
  517. protected $footer_font;
  518. /**
  519. * @var Language templates.
  520. * @access protected
  521. */
  522. protected $l;
  523. /**
  524. * @var Barcode to print on page footer (only if set).
  525. * @access protected
  526. */
  527. protected $barcode = false;
  528. /**
  529. * @var If true prints header
  530. * @access protected
  531. */
  532. protected $print_header = true;
  533. /**
  534. * @var If true prints footer.
  535. * @access protected
  536. */
  537. protected $print_footer = true;
  538. /**
  539. * @var Header image logo.
  540. * @access protected
  541. */
  542. protected $header_logo = '';
  543. /**
  544. * @var Header image logo width in mm.
  545. * @access protected
  546. */
  547. protected $header_logo_width = 30;
  548. /**
  549. * @var String to print as title on document header.
  550. * @access protected
  551. */
  552. protected $header_title = '';
  553. /**
  554. * @var String to print on document header.
  555. * @access protected
  556. */
  557. protected $header_string = '';
  558. /**
  559. * @var Default number of columns for html table.
  560. * @access protected
  561. */
  562. protected $default_table_columns = 4;
  563. // variables for html parser
  564. /**
  565. * @var HTML PARSER: array to store current link and rendering styles.
  566. * @access protected
  567. */
  568. protected $HREF = array();
  569. /**
  570. * @var store a list of available fonts on filesystem.
  571. * @access protected
  572. */
  573. protected $fontlist = array();
  574. /**
  575. * @var current foreground color
  576. * @access protected
  577. */
  578. protected $fgcolor;
  579. /**
  580. * @var HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  581. * @access protected
  582. */
  583. protected $listordered = array();
  584. /**
  585. * @var HTML PARSER: array count list items on nested lists.
  586. * @access protected
  587. */
  588. protected $listcount = array();
  589. /**
  590. * @var HTML PARSER: current list nesting level.
  591. * @access protected
  592. */
  593. protected $listnum = 0;
  594. /**
  595. * @var HTML PARSER: indent amount for lists.
  596. * @access protected
  597. */
  598. protected $listindent;
  599. /**
  600. * @var current background color
  601. * @access protected
  602. */
  603. protected $bgcolor;
  604. /**
  605. * @var Store temporary font size in points.
  606. * @access protected
  607. */
  608. protected $tempfontsize = 10;
  609. /**
  610. * @var spacer for LI tags.
  611. * @access protected
  612. */
  613. protected $lispacer = '';
  614. /**
  615. * @var default encoding
  616. * @access protected
  617. * @since 1.53.0.TC010
  618. */
  619. protected $encoding = 'UTF-8';
  620. /**
  621. * @var PHP internal encoding
  622. * @access protected
  623. * @since 1.53.0.TC016
  624. */
  625. protected $internal_encoding;
  626. /**
  627. * @var indicates if the document language is Right-To-Left
  628. * @access protected
  629. * @since 2.0.000
  630. */
  631. protected $rtl = false;
  632. /**
  633. * @var used to force RTL or LTR string inversion
  634. * @access protected
  635. * @since 2.0.000
  636. */
  637. protected $tmprtl = false;
  638. // --- Variables used for document encryption:
  639. /**
  640. * Indicates whether document is protected
  641. * @access protected
  642. * @since 2.0.000 (2008-01-02)
  643. */
  644. protected $encrypted;
  645. /**
  646. * U entry in pdf document
  647. * @access protected
  648. * @since 2.0.000 (2008-01-02)
  649. */
  650. protected $Uvalue;
  651. /**
  652. * O entry in pdf document
  653. * @access protected
  654. * @since 2.0.000 (2008-01-02)
  655. */
  656. protected $Ovalue;
  657. /**
  658. * P entry in pdf document
  659. * @access protected
  660. * @since 2.0.000 (2008-01-02)
  661. */
  662. protected $Pvalue;
  663. /**
  664. * encryption object id
  665. * @access protected
  666. * @since 2.0.000 (2008-01-02)
  667. */
  668. protected $enc_obj_id;
  669. /**
  670. * last RC4 key encrypted (cached for optimisation)
  671. * @access protected
  672. * @since 2.0.000 (2008-01-02)
  673. */
  674. protected $last_rc4_key;
  675. /**
  676. * last RC4 computed key
  677. * @access protected
  678. * @since 2.0.000 (2008-01-02)
  679. */
  680. protected $last_rc4_key_c;
  681. /**
  682. * RC4 padding
  683. * @access protected
  684. */
  685. 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";
  686. /**
  687. * RC4 encryption key
  688. * @access protected
  689. */
  690. protected $encryption_key;
  691. // --- bookmark ---
  692. /**
  693. * Outlines for bookmark
  694. * @access protected
  695. * @since 2.1.002 (2008-02-12)
  696. */
  697. protected $outlines = array();
  698. /**
  699. * Outline root for bookmark
  700. * @access protected
  701. * @since 2.1.002 (2008-02-12)
  702. */
  703. protected $OutlineRoot;
  704. // --- javascript and form ---
  705. /**
  706. * javascript code
  707. * @access protected
  708. * @since 2.1.002 (2008-02-12)
  709. */
  710. protected $javascript = '';
  711. /**
  712. * javascript counter
  713. * @access protected
  714. * @since 2.1.002 (2008-02-12)
  715. */
  716. protected $n_js;
  717. /**
  718. * line trough state
  719. * @access protected
  720. * @since 2.8.000 (2008-03-19)
  721. */
  722. protected $linethrough;
  723. // --- Variables used for User's Rights ---
  724. // See PDF reference chapter 8.7 Digital Signatures
  725. /**
  726. * If true enables user's rights on PDF reader
  727. * @access protected
  728. * @since 2.9.000 (2008-03-26)
  729. */
  730. protected $ur;
  731. /**
  732. * Names specifying additional document-wide usage rights for the document.
  733. * @access protected
  734. * @since 2.9.000 (2008-03-26)
  735. */
  736. protected $ur_document;
  737. /**
  738. * Names specifying additional annotation-related usage rights for the document.
  739. * @access protected
  740. * @since 2.9.000 (2008-03-26)
  741. */
  742. protected $ur_annots;
  743. /**
  744. * Names specifying additional form-field-related usage rights for the document.
  745. * @access protected
  746. * @since 2.9.000 (2008-03-26)
  747. */
  748. protected $ur_form;
  749. /**
  750. * Names specifying additional signature-related usage rights for the document.
  751. * @access protected
  752. * @since 2.9.000 (2008-03-26)
  753. */
  754. protected $ur_signature;
  755. /**
  756. * Dot Per Inch Document Resolution (do not change)
  757. * @access protected
  758. * @since 3.0.000 (2008-03-27)
  759. */
  760. protected $dpi = 72;
  761. /**
  762. * Array of page numbers were a new page group was started
  763. * @access protected
  764. * @since 3.0.000 (2008-03-27)
  765. */
  766. protected $newpagegroup = array();
  767. /**
  768. * Contains the number of pages of the groups
  769. * @access protected
  770. * @since 3.0.000 (2008-03-27)
  771. */
  772. protected $pagegroups;
  773. /**
  774. * Contains the alias of the current page group
  775. * @access protected
  776. * @since 3.0.000 (2008-03-27)
  777. */
  778. protected $currpagegroup;
  779. /**
  780. * Restrict the rendering of some elements to screen or printout.
  781. * @access protected
  782. * @since 3.0.000 (2008-03-27)
  783. */
  784. protected $visibility = 'all';
  785. /**
  786. * Print visibility.
  787. * @access protected
  788. * @since 3.0.000 (2008-03-27)
  789. */
  790. protected $n_ocg_print;
  791. /**
  792. * View visibility.
  793. * @access protected
  794. * @since 3.0.000 (2008-03-27)
  795. */
  796. protected $n_ocg_view;
  797. /**
  798. * Array of transparency objects and parameters.
  799. * @access protected
  800. * @since 3.0.000 (2008-03-27)
  801. */
  802. protected $extgstates;
  803. /**
  804. * Set the default JPEG compression quality (1-100)
  805. * @access protected
  806. * @since 3.0.000 (2008-03-27)
  807. */
  808. protected $jpeg_quality;
  809. /**
  810. * Default cell height ratio.
  811. * @access protected
  812. * @since 3.0.014 (2008-05-23)
  813. */
  814. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  815. /**
  816. * PDF viewer preferences.
  817. * @access protected
  818. * @since 3.1.000 (2008-06-09)
  819. */
  820. protected $viewer_preferences;
  821. /**
  822. * A name object specifying how the document should be displayed when opened.
  823. * @access protected
  824. * @since 3.1.000 (2008-06-09)
  825. */
  826. protected $PageMode;
  827. /**
  828. * Array for storing gradient information.
  829. * @access protected
  830. * @since 3.1.000 (2008-06-09)
  831. */
  832. protected $gradients = array();
  833. /**
  834. * Array used to store positions inside the pages buffer.
  835. * keys are the page numbers
  836. * @access protected
  837. * @since 3.2.000 (2008-06-26)
  838. */
  839. protected $intmrk = array();
  840. /**
  841. * Array used to store content positions inside the pages buffer.
  842. * keys are the page numbers
  843. * @access protected
  844. * @since 4.6.021 (2009-07-20)
  845. */
  846. protected $cntmrk = array();
  847. /**
  848. * Array used to store footer positions of each page.
  849. * @access protected
  850. * @since 3.2.000 (2008-07-01)
  851. */
  852. protected $footerpos = array();
  853. /**
  854. * Array used to store footer lenght of each page.
  855. * @access protected
  856. * @since 4.0.014 (2008-07-29)
  857. */
  858. protected $footerlen = array();
  859. /**
  860. * True if a newline is created.
  861. * @access protected
  862. * @since 3.2.000 (2008-07-01)
  863. */
  864. protected $newline = true;
  865. /**
  866. * End position of the latest inserted line
  867. * @access protected
  868. * @since 3.2.000 (2008-07-01)
  869. */
  870. protected $endlinex = 0;
  871. /**
  872. * PDF string for last line width
  873. * @access protected
  874. * @since 4.0.006 (2008-07-16)
  875. */
  876. protected $linestyleWidth = '';
  877. /**
  878. * PDF string for last line width
  879. * @access protected
  880. * @since 4.0.006 (2008-07-16)
  881. */
  882. protected $linestyleCap = '0 J';
  883. /**
  884. * PDF string for last line width
  885. * @access protected
  886. * @since 4.0.006 (2008-07-16)
  887. */
  888. protected $linestyleJoin = '0 j';
  889. /**
  890. * PDF string for last line width
  891. * @access protected
  892. * @since 4.0.006 (2008-07-16)
  893. */
  894. protected $linestyleDash = '[] 0 d';
  895. /**
  896. * True if marked-content sequence is open
  897. * @access protected
  898. * @since 4.0.013 (2008-07-28)
  899. */
  900. protected $openMarkedContent = false;
  901. /**
  902. * Count the latest inserted vertical spaces on HTML
  903. * @access protected
  904. * @since 4.0.021 (2008-08-24)
  905. */
  906. protected $htmlvspace = 0;
  907. /**
  908. * Array of Spot colors
  909. * @access protected
  910. * @since 4.0.024 (2008-09-12)
  911. */
  912. protected $spot_colors = array();
  913. /**
  914. * Symbol used for HTML unordered list items
  915. * @access protected
  916. * @since 4.0.028 (2008-09-26)
  917. */
  918. protected $lisymbol = '';
  919. /**
  920. * String used to mark the beginning and end of EPS image blocks
  921. * @access protected
  922. * @since 4.1.000 (2008-10-18)
  923. */
  924. protected $epsmarker = 'x#!#EPS#!#x';
  925. /**
  926. * Array of transformation matrix
  927. * @access protected
  928. * @since 4.2.000 (2008-10-29)
  929. */
  930. protected $transfmatrix = array();
  931. /**
  932. * Current key for transformation matrix
  933. * @access protected
  934. * @since 4.8.005 (2009-09-17)
  935. */
  936. protected $transfmatrix_key = 0;
  937. /**
  938. * Booklet mode for double-sided pages
  939. * @access protected
  940. * @since 4.2.000 (2008-10-29)
  941. */
  942. protected $booklet = false;
  943. /**
  944. * Epsilon value used for float calculations
  945. * @access protected
  946. * @since 4.2.000 (2008-10-29)
  947. */
  948. protected $feps = 0.005;
  949. /**
  950. * Array used for custom vertical spaces for HTML tags
  951. * @access protected
  952. * @since 4.2.001 (2008-10-30)
  953. */
  954. protected $tagvspaces = array();
  955. /**
  956. * @var HTML PARSER: custom indent amount for lists.
  957. * Negative value means disabled.
  958. * @access protected
  959. * @since 4.2.007 (2008-11-12)
  960. */
  961. protected $customlistindent = -1;
  962. /**
  963. * @var if true keeps the border open for the cell sides that cross the page.
  964. * @access protected
  965. * @since 4.2.010 (2008-11-14)
  966. */
  967. protected $opencell = true;
  968. /**
  969. * @var array of files to embedd
  970. * @access protected
  971. * @since 4.4.000 (2008-12-07)
  972. */
  973. protected $embeddedfiles = array();
  974. /**
  975. * @var boolean true when inside html pre tag
  976. * @access protected
  977. * @since 4.4.001 (2008-12-08)
  978. */
  979. protected $premode = false;
  980. /**
  981. * Array used to store positions of graphics transformation blocks inside the page buffer.
  982. * keys are the page numbers
  983. * @access protected
  984. * @since 4.4.002 (2008-12-09)
  985. */
  986. protected $transfmrk = array();
  987. /**
  988. * Default color for html links
  989. * @access protected
  990. * @since 4.4.003 (2008-12-09)
  991. */
  992. protected $htmlLinkColorArray = array(0, 0, 255);
  993. /**
  994. * Default font style to add to html links
  995. * @access protected
  996. * @since 4.4.003 (2008-12-09)
  997. */
  998. protected $htmlLinkFontStyle = 'U';
  999. /**
  1000. * Counts the number of pages.
  1001. * @access protected
  1002. * @since 4.5.000 (2008-12-31)
  1003. */
  1004. protected $numpages = 0;
  1005. /**
  1006. * Array containing page lenghts in bytes.
  1007. * @access protected
  1008. * @since 4.5.000 (2008-12-31)
  1009. */
  1010. protected $pagelen = array();
  1011. /**
  1012. * Counts the number of pages.
  1013. * @access protected
  1014. * @since 4.5.000 (2008-12-31)
  1015. */
  1016. protected $numimages = 0;
  1017. /**
  1018. * Store the image keys.
  1019. * @access protected
  1020. * @since 4.5.000 (2008-12-31)
  1021. */
  1022. protected $imagekeys = array();
  1023. /**
  1024. * Lenght of the buffer in bytes.
  1025. * @access protected
  1026. * @since 4.5.000 (2008-12-31)
  1027. */
  1028. protected $bufferlen = 0;
  1029. /**
  1030. * If true enables disk caching.
  1031. * @access protected
  1032. * @since 4.5.000 (2008-12-31)
  1033. */
  1034. protected $diskcache = false;
  1035. /**
  1036. * Counts the number of fonts.
  1037. * @access protected
  1038. * @since 4.5.000 (2009-01-02)
  1039. */
  1040. protected $numfonts = 0;
  1041. /**
  1042. * Store the font keys.
  1043. * @access protected
  1044. * @since 4.5.000 (2009-01-02)
  1045. */
  1046. protected $fontkeys = array();
  1047. /**
  1048. * Store the font object IDs.
  1049. * @access protected
  1050. * @since 4.8.001 (2009-09-09)
  1051. */
  1052. protected $font_obj_ids = array();
  1053. /**
  1054. * Store the fage status (true when opened, false when closed).
  1055. * @access protected
  1056. * @since 4.5.000 (2009-01-02)
  1057. */
  1058. protected $pageopen = array();
  1059. /**
  1060. * Default monospaced font
  1061. * @access protected
  1062. * @since 4.5.025 (2009-03-10)
  1063. */
  1064. protected $default_monospaced_font = 'courier';
  1065. /**
  1066. * Used to store a cloned copy of the current class object
  1067. * @access protected
  1068. * @since 4.5.029 (2009-03-19)
  1069. */
  1070. protected $objcopy;
  1071. /**
  1072. * Array used to store the lenghts of cache files
  1073. * @access protected
  1074. * @since 4.5.029 (2009-03-19)
  1075. */
  1076. protected $cache_file_lenght = array();
  1077. /**
  1078. * Table header content to be repeated on each new page
  1079. * @access protected
  1080. * @since 4.5.030 (2009-03-20)
  1081. */
  1082. protected $thead = '';
  1083. /**
  1084. * Margins used for table header.
  1085. * @access protected
  1086. * @since 4.5.030 (2009-03-20)
  1087. */
  1088. protected $theadMargins = array();
  1089. /**
  1090. * Cache array for UTF8StringToArray() method.
  1091. * @access protected
  1092. * @since 4.5.037 (2009-04-07)
  1093. */
  1094. protected $cache_UTF8StringToArray = array();
  1095. /**
  1096. * Maximum size of cache array used for UTF8StringToArray() method.
  1097. * @access protected
  1098. * @since 4.5.037 (2009-04-07)
  1099. */
  1100. protected $cache_maxsize_UTF8StringToArray = 8;
  1101. /**
  1102. * Current size of cache array used for UTF8StringToArray() method.
  1103. * @access protected
  1104. * @since 4.5.037 (2009-04-07)
  1105. */
  1106. protected $cache_size_UTF8StringToArray = 0;
  1107. /**
  1108. * If true enables document signing
  1109. * @access protected
  1110. * @since 4.6.005 (2009-04-24)
  1111. */
  1112. protected $sign = false;
  1113. /**
  1114. * Signature data
  1115. * @access protected
  1116. * @since 4.6.005 (2009-04-24)
  1117. */
  1118. protected $signature_data = array();
  1119. /**
  1120. * Signature max lenght
  1121. * @access protected
  1122. * @since 4.6.005 (2009-04-24)
  1123. */
  1124. protected $signature_max_lenght = 11742;
  1125. /**
  1126. * Regular expression used to find blank characters used for word-wrapping.
  1127. * @access protected
  1128. * @since 4.6.006 (2009-04-28)
  1129. */
  1130. protected $re_spaces = '/[\s]/';
  1131. /**
  1132. * Signature object ID
  1133. * @access protected
  1134. * @since 4.6.022 (2009-06-23)
  1135. */
  1136. protected $sig_obj_id = 0;
  1137. /**
  1138. * ByteRange placemark used during signature process.
  1139. * @access protected
  1140. * @since 4.6.028 (2009-08-25)
  1141. */
  1142. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1143. /**
  1144. * Placemark used during signature process.
  1145. * @access protected
  1146. * @since 4.6.028 (2009-08-25)
  1147. */
  1148. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1149. /**
  1150. * ID of page objects
  1151. * @access protected
  1152. * @since 4.7.000 (2009-08-29)
  1153. */
  1154. protected $page_obj_id = array();
  1155. /**
  1156. * Start ID for embedded file objects
  1157. * @access protected
  1158. * @since 4.7.000 (2009-08-29)
  1159. */
  1160. protected $embedded_start_obj_id = 100000;
  1161. /**
  1162. * Start ID for annotation objects
  1163. * @access protected
  1164. * @since 4.7.000 (2009-08-29)
  1165. */
  1166. protected $annots_start_obj_id = 200000;
  1167. /**
  1168. * Max ID of annotation object
  1169. * @access protected
  1170. * @since 4.7.000 (2009-08-29)
  1171. */
  1172. protected $annot_obj_id = 200000;
  1173. /**
  1174. * Current ID of annotation object
  1175. * @access protected
  1176. * @since 4.8.003 (2009-09-15)
  1177. */
  1178. protected $curr_annot_obj_id = 200000;
  1179. /**
  1180. * List of form annotations IDs
  1181. * @access protected
  1182. * @since 4.8.000 (2009-09-07)
  1183. */
  1184. protected $form_obj_id = array();
  1185. /*
  1186. * Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
  1187. * @access protected
  1188. * @since 4.8.000 (2009-09-07)
  1189. */
  1190. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1191. /**
  1192. * Javascript objects array
  1193. * @access protected
  1194. * @since 4.8.000 (2009-09-07)
  1195. */
  1196. protected $js_objects = array();
  1197. /**
  1198. * Start ID for javascript objects
  1199. * @access protected
  1200. * @since 4.8.000 (2009-09-07)
  1201. */
  1202. protected $js_start_obj_id = 300000;
  1203. /**
  1204. * Current ID of javascript object
  1205. * @access protected
  1206. * @since 4.8.000 (2009-09-07)
  1207. */
  1208. protected $js_obj_id = 300000;
  1209. /**
  1210. * Current form action (used during XHTML rendering)
  1211. * @access protected
  1212. * @since 4.8.000 (2009-09-07)
  1213. */
  1214. protected $form_action = '';
  1215. /**
  1216. * Current form encryption type (used during XHTML rendering)
  1217. * @access protected
  1218. * @since 4.8.000 (2009-09-07)
  1219. */
  1220. protected $form_enctype = 'application/x-www-form-urlencoded';
  1221. /**
  1222. * Current method to submit forms.
  1223. * @access protected
  1224. * @since 4.8.000 (2009-09-07)
  1225. */
  1226. protected $form_mode = 'post';
  1227. /**
  1228. * Start ID for appearance streams XObjects
  1229. * @access protected
  1230. * @since 4.8.001 (2009-09-09)
  1231. */
  1232. protected $apxo_start_obj_id = 400000;
  1233. /**
  1234. * Current ID of appearance streams XObjects
  1235. * @access protected
  1236. * @since 4.8.001 (2009-09-09)
  1237. */
  1238. protected $apxo_obj_id = 400000;
  1239. /**
  1240. * List of fonts used on form fields (fontname => fontkey).
  1241. * @access protected
  1242. * @since 4.8.001 (2009-09-09)
  1243. */
  1244. protected $annotation_fonts = array();
  1245. /**
  1246. * List of radio buttons parent objects.
  1247. * @access protected
  1248. * @since 4.8.001 (2009-09-09)
  1249. */
  1250. protected $radiobutton_groups = array();
  1251. /**
  1252. * List of radio group objects IDs
  1253. * @access protected
  1254. * @since 4.8.001 (2009-09-09)
  1255. */
  1256. protected $radio_groups = array();
  1257. /**
  1258. * Text indentation value (used for text-indent CSS attribute)
  1259. * @access protected
  1260. * @since 4.8.006 (2009-09-23)
  1261. */
  1262. protected $textindent = 0;
  1263. /**
  1264. * Store page number when startTransaction() is called.
  1265. * @access protected
  1266. * @since 4.8.006 (2009-09-23)
  1267. */
  1268. protected $start_transaction_page = 0;
  1269. //------------------------------------------------------------
  1270. // METHODS
  1271. //------------------------------------------------------------
  1272. /**
  1273. * This is the class constructor.
  1274. * It allows to set up the page format, the orientation and
  1275. * the measure unit used in all the methods (except for the font sizes).
  1276. * @since 1.0
  1277. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  1278. * @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.
  1279. * @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>
  1280. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  1281. * @param boolean $diskcache if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1282. * @param String $encoding charset encoding; default is UTF-8
  1283. * @access public
  1284. */
  1285. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false) {
  1286. /* Set internal character encoding to ASCII */
  1287. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1288. $this->internal_encoding = mb_internal_encoding();
  1289. mb_internal_encoding('ASCII');
  1290. }
  1291. // set disk caching
  1292. $this->diskcache = $diskcache ? true : false;
  1293. // set language direction
  1294. $this->rtl = false;
  1295. $this->tmprtl = false;
  1296. //Some checks
  1297. $this->_dochecks();
  1298. //Initialization of properties
  1299. $this->isunicode = $unicode;
  1300. $this->page = 0;
  1301. $this->transfmrk[0] = array();
  1302. $this->pagedim = array();
  1303. $this->n = 2;
  1304. $this->buffer = '';
  1305. $this->pages = array();
  1306. $this->state = 0;
  1307. $this->fonts = array();
  1308. $this->FontFiles = array();
  1309. $this->diffs = array();
  1310. $this->images = array();
  1311. $this->links = array();
  1312. $this->gradients = array();
  1313. $this->InFooter = false;
  1314. $this->lasth = 0;
  1315. $this->FontFamily = 'helvetica';
  1316. $this->FontStyle = '';
  1317. $this->FontSizePt = 12;
  1318. $this->underline = false;
  1319. $this->linethrough = false;
  1320. $this->DrawColor = '0 G';
  1321. $this->FillColor = '0 g';
  1322. $this->TextColor = '0 g';
  1323. $this->ColorFlag = false;
  1324. // encryption values
  1325. $this->encrypted = false;
  1326. $this->last_rc4_key = '';
  1327. $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";
  1328. //Standard Unicode fonts
  1329. $this->CoreFonts = array(
  1330. 'courier'=>'Courier',
  1331. 'courierB'=>'Courier-Bold',
  1332. 'courierI'=>'Courier-Oblique',
  1333. 'courierBI'=>'Courier-BoldOblique',
  1334. 'helvetica'=>'Helvetica',
  1335. 'helveticaB'=>'Helvetica-Bold',
  1336. 'helveticaI'=>'Helvetica-Oblique',
  1337. 'helveticaBI'=>'Helvetica-BoldOblique',
  1338. 'times'=>'Times-Roman',
  1339. 'timesB'=>'Times-Bold',
  1340. 'timesI'=>'Times-Italic',
  1341. 'timesBI'=>'Times-BoldItalic',
  1342. 'symbol'=>'Symbol',
  1343. 'zapfdingbats'=>'ZapfDingbats'
  1344. );
  1345. //Set scale factor
  1346. $this->setPageUnit($unit);
  1347. // set page format and orientation
  1348. $this->setPageFormat($format, $orientation);
  1349. //Page margins (1 cm)
  1350. $margin = 28.35 / $this->k;
  1351. $this->SetMargins($margin, $margin);
  1352. //Interior cell margin
  1353. $this->cMargin = $margin / 10;
  1354. //Line width (0.2 mm)
  1355. $this->LineWidth = 0.57 / $this->k;
  1356. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1357. $this->linestyleCap = '0 J';
  1358. $this->linestyleJoin = '0 j';
  1359. $this->linestyleDash = '[] 0 d';
  1360. //Automatic page break
  1361. $this->SetAutoPageBreak(true, (2 * $margin));
  1362. //Full width display mode
  1363. $this->SetDisplayMode('fullwidth');
  1364. //Compression
  1365. $this->SetCompression(true);
  1366. //Set default PDF version number
  1367. $this->PDFVersion = '1.7';
  1368. $this->encoding = $encoding;
  1369. $this->HREF = array();
  1370. $this->getFontsList();
  1371. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1372. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1373. $this->extgstates = array();
  1374. // user's rights
  1375. $this->sign = false;
  1376. $this->ur = false;
  1377. $this->ur_document = '/FullSave';
  1378. $this->ur_annots = '/Create/Delete/Modify/Copy/Import/Export';
  1379. $this->ur_form = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1380. $this->ur_signature = '/Modify';
  1381. // set default JPEG quality
  1382. $this->jpeg_quality = 75;
  1383. // initialize some settings
  1384. $this->utf8Bidi(array(''), '');
  1385. // set default font
  1386. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1387. // check if PCRE Unicode support is enabled
  1388. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1389. // PCRE unicode support is turned ON
  1390. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1391. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1392. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1393. //$this->re_spaces = '/[\s\p{Z}\p{Lo}]/u';
  1394. $this->re_spaces = '/[\s\p{Z}]/u';
  1395. } else {
  1396. // PCRE unicode support is turned OFF
  1397. $this->re_spaces = '/[\s]/';
  1398. }
  1399. $this->annot_obj_id = $this->annots_start_obj_id;
  1400. $this->curr_annot_obj_id = $this->annots_start_obj_id;
  1401. $this->apxo_obj_id = $this->apxo_start_obj_id;
  1402. $this->js_obj_id = $this->js_start_obj_id;
  1403. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1404. }
  1405. /**
  1406. * Default destructor.
  1407. * @access public
  1408. * @since 1.53.0.TC016
  1409. */
  1410. public function __destruct() {
  1411. // restore internal encoding
  1412. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1413. mb_internal_encoding($this->internal_encoding);
  1414. }
  1415. // unset all class variables
  1416. $this->_destroy(true);
  1417. }
  1418. /**
  1419. * Set the units of measure for the document.
  1420. * @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.
  1421. * @access public
  1422. * @since 3.0.015 (2008-06-06)
  1423. */
  1424. public function setPageUnit($unit) {
  1425. //Set scale factor
  1426. switch (strtolower($unit)) {
  1427. // points
  1428. case 'px':
  1429. case 'pt': {
  1430. $this->k = 1;
  1431. break;
  1432. }
  1433. // millimeters
  1434. case 'mm': {
  1435. $this->k = $this->dpi / 25.4;
  1436. break;
  1437. }
  1438. // centimeters
  1439. case 'cm': {
  1440. $this->k = $this->dpi / 2.54;
  1441. break;
  1442. }
  1443. // inches
  1444. case 'in': {
  1445. $this->k = $this->dpi;
  1446. break;
  1447. }
  1448. // unsupported unit
  1449. default : {
  1450. $this->Error('Incorrect unit: '.$unit);
  1451. break;
  1452. }
  1453. }
  1454. if (isset($this->CurOrientation)) {
  1455. $this->setPageOrientation($this->CurOrientation);
  1456. }
  1457. }
  1458. /**
  1459. * Set the page format
  1460. * @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>
  1461. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1462. * @access public
  1463. * @since 3.0.015 (2008-06-06)
  1464. */
  1465. public function setPageFormat($format, $orientation='P') {
  1466. //Page format
  1467. if (is_string($format)) {
  1468. // Page formats (45 standard ISO paper formats and 4 american common formats).
  1469. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  1470. switch (strtoupper($format)) {
  1471. case '4A0': {$format = array(4767.87,6740.79); break;}
  1472. case '2A0': {$format = array(3370.39,4767.87); break;}
  1473. case 'A0': {$format = array(2383.94,3370.39); break;}
  1474. case 'A1': {$format = array(1683.78,2383.94); break;}
  1475. case 'A2': {$format = array(1190.55,1683.78); break;}
  1476. case 'A3': {$format = array(841.89,1190.55); break;}
  1477. case 'A4': default: {$format = array(595.28,841.89); break;}
  1478. case 'A5': {$format = array(419.53,595.28); break;}
  1479. case 'A6': {$format = array(297.64,419.53); break;}
  1480. case 'A7': {$format = array(209.76,297.64); break;}
  1481. case 'A8': {$format = array(147.40,209.76); break;}
  1482. case 'A9': {$format = array(104.88,147.40); break;}
  1483. case 'A10': {$format = array(73.70,104.88); break;}
  1484. case 'B0': {$format = array(2834.65,4008.19); break;}
  1485. case 'B1': {$format = array(2004.09,2834.65); break;}
  1486. case 'B2': {$format = array(1417.32,2004.09); break;}
  1487. case 'B3': {$format = array(1000.63,1417.32); break;}
  1488. case 'B4': {$format = array(708.66,1000.63); break;}
  1489. case 'B5': {$format = array(498.90,708.66); break;}
  1490. case 'B6': {$format = array(354.33,498.90); break;}
  1491. case 'B7': {$format = array(249.45,354.33); break;}
  1492. case 'B8': {$format = array(175.75,249.45); break;}
  1493. case 'B9': {$format = array(124.72,175.75); break;}
  1494. case 'B10': {$format = array(87.87,124.72); break;}
  1495. case 'C0': {$format = array(2599.37,3676.54); break;}
  1496. case 'C1': {$format = array(1836.85,2599.37); break;}
  1497. case 'C2': {$format = array(1298.27,1836.85); break;}
  1498. case 'C3': {$format = array(918.43,1298.27); break;}
  1499. case 'C4': {$format = array(649.13,918.43); break;}
  1500. case 'C5': {$format = array(459.21,649.13); break;}
  1501. case 'C6': {$format = array(323.15,459.21); break;}
  1502. case 'C7': {$format = array(229.61,323.15); break;}
  1503. case 'C8': {$format = array(161.57,229.61); break;}
  1504. case 'C9': {$format = array(113.39,161.57); break;}
  1505. case 'C10': {$format = array(79.37,113.39); break;}
  1506. case 'RA0': {$format = array(2437.80,3458.27); break;}
  1507. case 'RA1': {$format = array(1729.13,2437.80); break;}
  1508. case 'RA2': {$format = array(1218.90,1729.13); break;}
  1509. case 'RA3': {$format = array(864.57,1218.90); break;}
  1510. case 'RA4': {$format = array(609.45,864.57); break;}
  1511. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  1512. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  1513. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  1514. case 'SRA3': {$format = array(907.09,1275.59); break;}
  1515. case 'SRA4': {$format = array(637.80,907.09); break;}
  1516. case 'LETTER': {$format = array(612.00,792.00); break;}
  1517. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  1518. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  1519. case 'FOLIO': {$format = array(612.00,936.00); break;}
  1520. }
  1521. $this->fwPt = $format[0];
  1522. $this->fhPt = $format[1];
  1523. } else {
  1524. $this->fwPt = $format[0] * $this->k;
  1525. $this->fhPt = $format[1] * $this->k;
  1526. }
  1527. $this->setPageOrientation($orientation);
  1528. }
  1529. /**
  1530. * Set page orientation.
  1531. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1532. * @param boolean $autopagebreak Boolean indicating if auto-page-break mode should be on or off.
  1533. * @param float $bottommargin bottom margin of the page.
  1534. * @access public
  1535. * @since 3.0.015 (2008-06-06)
  1536. */
  1537. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1538. $orientation = strtoupper($orientation);
  1539. if (($orientation == 'P') OR ($orientation == 'PORTRAIT')) {
  1540. $this->CurOrientation = 'P';
  1541. $this->wPt = $this->fwPt;
  1542. $this->hPt = $this->fhPt;
  1543. } elseif (($orientation == 'L') OR ($orientation == 'LANDSCAPE')) {
  1544. $this->CurOrientation = 'L';
  1545. $this->wPt = $this->fhPt;
  1546. $this->hPt = $this->fwPt;
  1547. } else {
  1548. $this->Error('Incorrect orientation: '.$orientation);
  1549. }
  1550. $this->w = $this->wPt / $this->k;
  1551. $this->h = $this->hPt / $this->k;
  1552. if ($this->empty_string($autopagebreak)) {
  1553. if (isset($this->AutoPageBreak)) {
  1554. $autopagebreak = $this->AutoPageBreak;
  1555. } else {
  1556. $autopagebreak = true;
  1557. }
  1558. }
  1559. if ($this->empty_string($bottommargin)) {
  1560. if (isset($this->bMargin)) {
  1561. $bottommargin = $this->bMargin;
  1562. } else {
  1563. // default value = 2 cm
  1564. $bottommargin = 2 * 28.35 / $this->k;
  1565. }
  1566. }
  1567. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  1568. // store page dimensions
  1569. $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);
  1570. }
  1571. /**
  1572. * Set regular expression to detect withespaces or word separators.
  1573. * @param string $re regular expression (leave empty for default).
  1574. * @access public
  1575. * @since 4.6.016 (2009-06-15)
  1576. */
  1577. public function setSpacesRE($re='/[\s]/') {
  1578. // if PCRE unicode support is turned ON:
  1579. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1580. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1581. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1582. $this->re_spaces = $re;
  1583. }
  1584. /**
  1585. * Enable or disable Right-To-Left language mode
  1586. * @param Boolean $enable if true enable Right-To-Left language mode.
  1587. * @param Boolean $resetx if true reset the X position on direction change.
  1588. * @access public
  1589. * @since 2.0.000 (2008-01-03)
  1590. */
  1591. public function setRTL($enable, $resetx=true) {
  1592. $enable = $enable ? true : false;
  1593. $resetx = ($resetx AND ($enable != $this->rtl));
  1594. $this->rtl = $enable;
  1595. $this->tmprtl = false;
  1596. if ($resetx) {
  1597. $this->Ln(0);
  1598. }
  1599. }
  1600. /**
  1601. * Return the RTL status
  1602. * @return boolean
  1603. * @access public
  1604. * @since 4.0.012 (2008-07-24)
  1605. */
  1606. public function getRTL() {
  1607. return $this->rtl;
  1608. }
  1609. /**
  1610. * Force temporary RTL language direction
  1611. * @param mixed $mode can be false, 'L' for LTR or 'R' for RTL
  1612. * @access public
  1613. * @since 2.1.000 (2008-01-09)
  1614. */
  1615. public function setTempRTL($mode) {
  1616. $newmode = false;
  1617. switch ($mode) {
  1618. case 'ltr':
  1619. case 'LTR':
  1620. case 'L': {
  1621. if ($this->rtl) {
  1622. $newmode = 'L';
  1623. }
  1624. break;
  1625. }
  1626. case 'rtl':
  1627. case 'RTL':
  1628. case 'R': {
  1629. if (!$this->rtl) {
  1630. $newmode = 'R';
  1631. }
  1632. break;
  1633. }
  1634. case false:
  1635. default: {
  1636. $newmode = false;
  1637. break;
  1638. }
  1639. }
  1640. $this->tmprtl = $newmode;
  1641. }
  1642. /**
  1643. * Return the current temporary RTL status
  1644. * @return boolean
  1645. * @access public
  1646. * @since 4.8.014 (2009-11-04)
  1647. */
  1648. public function isRTLTextDir() {
  1649. return ($this->rtl OR ($this->tmprtl == 'R'));
  1650. }
  1651. /**
  1652. * Set the last cell height.
  1653. * @param float $h cell height.
  1654. * @author Nicola Asuni
  1655. * @access public
  1656. * @since 1.53.0.TC034
  1657. */
  1658. public function setLastH($h) {
  1659. $this->lasth = $h;
  1660. }
  1661. /**
  1662. * Get the last cell height.
  1663. * @return last cell height
  1664. * @access public
  1665. * @since 4.0.017 (2008-08-05)
  1666. */
  1667. public function getLastH() {
  1668. return $this->lasth;
  1669. }
  1670. /**
  1671. * Set the adjusting factor to convert pixels to user units.
  1672. * @param float $scale adjusting factor to convert pixels to user units.
  1673. * @author Nicola Asuni
  1674. * @access public
  1675. * @since 1.5.2
  1676. */
  1677. public function setImageScale($scale) {
  1678. $this->imgscale = $scale;
  1679. }
  1680. /**
  1681. * Returns the adjusting factor to convert pixels to user units.
  1682. * @return float adjusting factor to convert pixels to user units.
  1683. * @author Nicola Asuni
  1684. * @access public
  1685. * @since 1.5.2
  1686. */
  1687. public function getImageScale() {
  1688. return $this->imgscale;
  1689. }
  1690. /**
  1691. * Returns an array of page dimensions:
  1692. * <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>
  1693. * @param int $pagenum page number (empty = current page)
  1694. * @return array of page dimensions.
  1695. * @author Nicola Asuni
  1696. * @access public
  1697. * @since 4.5.027 (2009-03-16)
  1698. */
  1699. public function getPageDimensions($pagenum='') {
  1700. if (empty($pagenum)) {
  1701. $pagenum = $this->page;
  1702. }
  1703. return $this->pagedim[$pagenum];
  1704. }
  1705. /**
  1706. * Returns the page width in units.
  1707. * @param int $pagenum page number (empty = current page)
  1708. * @return int page width.
  1709. * @author Nicola Asuni
  1710. * @access public
  1711. * @since 1.5.2
  1712. * @see getPageDimensions()
  1713. */
  1714. public function getPageWidth($pagenum='') {
  1715. if (empty($pagenum)) {
  1716. return $this->w;
  1717. }
  1718. return $this->pagedim[$pagenum]['w'];
  1719. }
  1720. /**
  1721. * Returns the page height in units.
  1722. * @param int $pagenum page number (empty = current page)
  1723. * @return int page height.
  1724. * @author Nicola Asuni
  1725. * @access public
  1726. * @since 1.5.2
  1727. * @see getPageDimensions()
  1728. */
  1729. public function getPageHeight($pagenum='') {
  1730. if (empty($pagenum)) {
  1731. return $this->h;
  1732. }
  1733. return $this->pagedim[$pagenum]['h'];
  1734. }
  1735. /**
  1736. * Returns the page break margin.
  1737. * @param int $pagenum page number (empty = current page)
  1738. * @return int page break margin.
  1739. * @author Nicola Asuni
  1740. * @access public
  1741. * @since 1.5.2
  1742. * @see getPageDimensions()
  1743. */
  1744. public function getBreakMargin($pagenum='') {
  1745. if (empty($pagenum)) {
  1746. return $this->bMargin;
  1747. }
  1748. return $this->pagedim[$pagenum]['bm'];
  1749. }
  1750. /**
  1751. * Returns the scale factor (number of points in user unit).
  1752. * @return int scale factor.
  1753. * @author Nicola Asuni
  1754. * @access public
  1755. * @since 1.5.2
  1756. */
  1757. public function getScaleFactor() {
  1758. return $this->k;
  1759. }
  1760. /**
  1761. * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
  1762. * @param float $left Left margin.
  1763. * @param float $top Top margin.
  1764. * @param float $right Right margin. Default value is the left one.
  1765. * @access public
  1766. * @since 1.0
  1767. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  1768. */
  1769. public function SetMargins($left, $top, $right=-1) {
  1770. //Set left, top and right margins
  1771. $this->lMargin = $left;
  1772. $this->tMargin = $top;
  1773. if ($right == -1) {
  1774. $right = $left;
  1775. }
  1776. $this->rMargin = $right;
  1777. }
  1778. /**
  1779. * 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.
  1780. * @param float $margin The margin.
  1781. * @access public
  1782. * @since 1.4
  1783. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1784. */
  1785. public function SetLeftMargin($margin) {
  1786. //Set left margin
  1787. $this->lMargin=$margin;
  1788. if (($this->page > 0) AND ($this->x < $margin)) {
  1789. $this->x = $margin;
  1790. }
  1791. }
  1792. /**
  1793. * Defines the top margin. The method can be called before creating the first page.
  1794. * @param float $margin The margin.
  1795. * @access public
  1796. * @since 1.5
  1797. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1798. */
  1799. public function SetTopMargin($margin) {
  1800. //Set top margin
  1801. $this->tMargin=$margin;
  1802. if (($this->page > 0) AND ($this->y < $margin)) {
  1803. $this->y = $margin;
  1804. }
  1805. }
  1806. /**
  1807. * Defines the right margin. The method can be called before creating the first page.
  1808. * @param float $margin The margin.
  1809. * @access public
  1810. * @since 1.5
  1811. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1812. */
  1813. public function SetRightMargin($margin) {
  1814. $this->rMargin=$margin;
  1815. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  1816. $this->x = $this->w - $margin;
  1817. }
  1818. }
  1819. /**
  1820. * Set the internal Cell padding.
  1821. * @param float $pad internal padding.
  1822. * @access public
  1823. * @since 2.1.000 (2008-01-09)
  1824. * @see Cell(), SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1825. */
  1826. public function SetCellPadding($pad) {
  1827. $this->cMargin = $pad;
  1828. }
  1829. /**
  1830. * 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.
  1831. * @param boolean $auto Boolean indicating if mode should be on or off.
  1832. * @param float $margin Distance from the bottom of the page.
  1833. * @access public
  1834. * @since 1.0
  1835. * @see Cell(), MultiCell(), AcceptPageBreak()
  1836. */
  1837. public function SetAutoPageBreak($auto, $margin=0) {
  1838. //Set auto page break mode and triggering margin
  1839. $this->AutoPageBreak = $auto;
  1840. $this->bMargin = $margin;
  1841. $this->PageBreakTrigger = $this->h - $margin;
  1842. }
  1843. /**
  1844. * Defines the way the document is to be displayed by the viewer.
  1845. * @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>
  1846. * @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>
  1847. * @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>
  1848. * @access public
  1849. * @since 1.2
  1850. */
  1851. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  1852. //Set display mode in viewer
  1853. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  1854. $this->ZoomMode = $zoom;
  1855. } else {
  1856. $this->Error('Incorrect zoom display mode: '.$zoom);
  1857. }
  1858. switch ($layout) {
  1859. case 'default':
  1860. case 'single':
  1861. case 'SinglePage': {
  1862. $this->LayoutMode = 'SinglePage';
  1863. break;
  1864. }
  1865. case 'continuous':
  1866. case 'OneColumn': {
  1867. $this->LayoutMode = 'OneColumn';
  1868. break;
  1869. }
  1870. case 'two':
  1871. case 'TwoColumnLeft': {
  1872. $this->LayoutMode = 'TwoColumnLeft';
  1873. break;
  1874. }
  1875. case 'TwoColumnRight': {
  1876. $this->LayoutMode = 'TwoColumnRight';
  1877. break;
  1878. }
  1879. case 'TwoPageLeft': {
  1880. $this->LayoutMode = 'TwoPageLeft';
  1881. break;
  1882. }
  1883. case 'TwoPageRight': {
  1884. $this->LayoutMode = 'TwoPageRight';
  1885. break;
  1886. }
  1887. default: {
  1888. $this->LayoutMode = 'SinglePage';
  1889. }
  1890. }
  1891. switch ($mode) {
  1892. case 'UseNone': {
  1893. $this->PageMode = 'UseNone';
  1894. break;
  1895. }
  1896. case 'UseOutlines': {
  1897. $this->PageMode = 'UseOutlines';
  1898. break;
  1899. }
  1900. case 'UseThumbs': {
  1901. $this->PageMode = 'UseThumbs';
  1902. break;
  1903. }
  1904. case 'FullScreen': {
  1905. $this->PageMode = 'FullScreen';
  1906. break;
  1907. }
  1908. case 'UseOC': {
  1909. $this->PageMode = 'UseOC';
  1910. break;
  1911. }
  1912. case '': {
  1913. $this->PageMode = 'UseAttachments';
  1914. break;
  1915. }
  1916. default: {
  1917. $this->PageMode = 'UseNone';
  1918. }
  1919. }
  1920. }
  1921. /**
  1922. * 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.
  1923. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  1924. * @param boolean $compress Boolean indicating if compression must be enabled.
  1925. * @access public
  1926. * @since 1.4
  1927. */
  1928. public function SetCompression($compress) {
  1929. //Set page compression
  1930. if (function_exists('gzcompress')) {
  1931. $this->compress = $compress;
  1932. } else {
  1933. $this->compress = false;
  1934. }
  1935. }
  1936. /**
  1937. * Defines the title of the document.
  1938. * @param string $title The title.
  1939. * @access public
  1940. * @since 1.2
  1941. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  1942. */
  1943. public function SetTitle($title) {
  1944. //Title of document
  1945. $this->title = $title;
  1946. }
  1947. /**
  1948. * Defines the subject of the document.
  1949. * @param string $subject The subject.
  1950. * @access public
  1951. * @since 1.2
  1952. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  1953. */
  1954. public function SetSubject($subject) {
  1955. //Subject of document
  1956. $this->subject = $subject;
  1957. }
  1958. /**
  1959. * Defines the author of the document.
  1960. * @param string $author The name of the author.
  1961. * @access public
  1962. * @since 1.2
  1963. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  1964. */
  1965. public function SetAuthor($author) {
  1966. //Author of document
  1967. $this->author = $author;
  1968. }
  1969. /**
  1970. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  1971. * @param string $keywords The list of keywords.
  1972. * @access public
  1973. * @since 1.2
  1974. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  1975. */
  1976. public function SetKeywords($keywords) {
  1977. //Keywords of document
  1978. $this->keywords = $keywords;
  1979. }
  1980. /**
  1981. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  1982. * @param string $creator The name of the creator.
  1983. * @access public
  1984. * @since 1.2
  1985. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  1986. */
  1987. public function SetCreator($creator) {
  1988. //Creator of document
  1989. $this->creator = $creator;
  1990. }
  1991. /**
  1992. * 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.
  1993. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  1994. * @param string $msg The error message
  1995. * @access public
  1996. * @since 1.0
  1997. */
  1998. public function Error($msg) {
  1999. // unset all class variables
  2000. $this->_destroy(true);
  2001. // exit program and print error
  2002. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2003. }
  2004. /**
  2005. * This method begins the generation of the PDF document.
  2006. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2007. * Note: no page is created by this method
  2008. * @access public
  2009. * @since 1.0
  2010. * @see AddPage(), Close()
  2011. */
  2012. public function Open() {
  2013. //Begin document
  2014. $this->state = 1;
  2015. }
  2016. /**
  2017. * Terminates the PDF document.
  2018. * It is not necessary to call this method explicitly because Output() does it automatically.
  2019. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2020. * @access public
  2021. * @since 1.0
  2022. * @see Open(), Output()
  2023. */
  2024. public function Close() {
  2025. if ($this->state == 3) {
  2026. return;
  2027. }
  2028. if ($this->page == 0) {
  2029. $this->AddPage();
  2030. }
  2031. // close page
  2032. $this->endPage();
  2033. // close document
  2034. $this->_enddoc();
  2035. // unset all class variables (except critical ones)
  2036. $this->_destroy(false);
  2037. }
  2038. /**
  2039. * Move pointer at the specified document page and update page dimensions.
  2040. * @param int $pnum page number
  2041. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2042. * @access public
  2043. * @since 2.1.000 (2008-01-07)
  2044. * @see getPage(), lastpage(), getNumPages()
  2045. */
  2046. public function setPage($pnum, $resetmargins=false) {
  2047. $bfPage = isset($this->page) ? $this->page : 0;
  2048. $bfNumpages = isset($this->numpages) ? $this->numpages : 0;
  2049. if ($pnum == $bfPage) {
  2050. return;
  2051. }
  2052. if (($pnum > 0) AND ($pnum <= $bfNumpages)) {
  2053. $this->state = 2;
  2054. // save current graphic settings
  2055. //$gvars = $this->getGraphicVars();
  2056. $oldpage = $this->page;
  2057. $this->page = $pnum;
  2058. $this->wPt = $this->pagedim[$this->page]['w'];
  2059. $this->hPt = $this->pagedim[$this->page]['h'];
  2060. $this->w = $this->wPt / $this->k;
  2061. $this->h = $this->hPt / $this->k;
  2062. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2063. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2064. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2065. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2066. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2067. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2068. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2069. // restore graphic settings
  2070. //$this->setGraphicVars($gvars);
  2071. if ($resetmargins) {
  2072. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2073. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2074. $this->SetY($this->tMargin);
  2075. } else {
  2076. // account for booklet mode
  2077. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2078. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2079. $this->lMargin += $deltam;
  2080. $this->rMargin -= $deltam;
  2081. }
  2082. }
  2083. } else {
  2084. $this->Error('Wrong page number on setPage() function.');
  2085. }
  2086. }
  2087. /**
  2088. * Reset pointer to the last document page.
  2089. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2090. * @access public
  2091. * @since 2.0.000 (2008-01-04)
  2092. * @see setPage(), getPage(), getNumPages()
  2093. */
  2094. public function lastPage($resetmargins=false) {
  2095. $this->setPage($this->getNumPages(), $resetmargins);
  2096. }
  2097. /**
  2098. * Get current document page number.
  2099. * @return int page number
  2100. * @access public
  2101. * @since 2.1.000 (2008-01-07)
  2102. * @see setPage(), lastpage(), getNumPages()
  2103. */
  2104. public function getPage() {
  2105. return $this->page;
  2106. }
  2107. /**
  2108. * Get the total number of insered pages.
  2109. * @return int number of pages
  2110. * @access public
  2111. * @since 2.1.000 (2008-01-07)
  2112. * @see setPage(), getPage(), lastpage()
  2113. */
  2114. public function getNumPages() {
  2115. return isset($this->numpages) ? $this->numpages : 0;
  2116. }
  2117. /**
  2118. * 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).
  2119. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2120. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2121. * @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>
  2122. * @access public
  2123. * @since 1.0
  2124. * @see startPage(), endPage()
  2125. */
  2126. public function AddPage($orientation='', $format='') {
  2127. if (!isset($this->original_lMargin)) {
  2128. $this->original_lMargin = $this->lMargin;
  2129. }
  2130. if (!isset($this->original_rMargin)) {
  2131. $this->original_rMargin = $this->rMargin;
  2132. }
  2133. // terminate previous page
  2134. $this->endPage();
  2135. // start new page
  2136. $this->startPage($orientation, $format);
  2137. }
  2138. /**
  2139. * Terminate the current page
  2140. * @access protected
  2141. * @since 4.2.010 (2008-11-14)
  2142. * @see startPage(), AddPage()
  2143. */
  2144. protected function endPage() {
  2145. // check if page is already closed
  2146. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2147. return;
  2148. }
  2149. $this->InFooter = true;
  2150. // print page footer
  2151. $this->setFooter();
  2152. // close page
  2153. $this->_endpage();
  2154. // mark page as closed
  2155. $this->pageopen[$this->page] = false;
  2156. $this->InFooter = false;
  2157. }
  2158. /**
  2159. * Starts a new page to the document. The page must be closed using the endPage() function.
  2160. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2161. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2162. * @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>
  2163. * @access protected
  2164. * @since 4.2.010 (2008-11-14)
  2165. * @see endPage(), AddPage()
  2166. */
  2167. protected function startPage($orientation='', $format='') {
  2168. if ($this->numpages > $this->page) {
  2169. // this page has been already added
  2170. $this->setPage($this->page + 1);
  2171. $this->SetY($this->tMargin);
  2172. return;
  2173. }
  2174. // start a new page
  2175. if ($this->state == 0) {
  2176. $this->Open();
  2177. }
  2178. ++$this->numpages;
  2179. $this->swapMargins($this->booklet);
  2180. // save current graphic settings
  2181. $gvars = $this->getGraphicVars();
  2182. // start new page
  2183. $this->_beginpage($orientation, $format);
  2184. // mark page as open
  2185. $this->pageopen[$this->page] = true;
  2186. // restore graphic settings
  2187. $this->setGraphicVars($gvars);
  2188. // mark this point
  2189. $this->setPageMark();
  2190. // print page header
  2191. $this->setHeader();
  2192. // restore graphic settings
  2193. $this->setGraphicVars($gvars);
  2194. // mark this point
  2195. $this->setPageMark();
  2196. // print table header (if any)
  2197. $this->setTableHeader();
  2198. }
  2199. /**
  2200. * Set start-writing mark on current page for multicell borders and fills.
  2201. * This function must be called after calling Image() function for a background image.
  2202. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2203. * @access public
  2204. * @since 4.0.016 (2008-07-30)
  2205. */
  2206. public function setPageMark() {
  2207. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2208. $this->setContentMark();
  2209. }
  2210. /**
  2211. * Set start-writing mark on selected page.
  2212. * @param int $page page number (default is the current page)
  2213. * @access protected
  2214. * @since 4.6.021 (2009-07-20)
  2215. */
  2216. protected function setContentMark($page=0) {
  2217. if ($page <= 0) {
  2218. $page = $this->page;
  2219. }
  2220. if (isset($this->footerlen[$page])) {
  2221. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2222. } else {
  2223. $this->cntmrk[$page] = $this->pagelen[$page];
  2224. }
  2225. }
  2226. /**
  2227. * Set header data.
  2228. * @param string $ln header image logo
  2229. * @param string $lw header image logo width in mm
  2230. * @param string $ht string to print as title on document header
  2231. * @param string $hs string to print on document header
  2232. * @access public
  2233. */
  2234. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  2235. $this->header_logo = $ln;
  2236. $this->header_logo_width = $lw;
  2237. $this->header_title = $ht;
  2238. $this->header_string = $hs;
  2239. }
  2240. /**
  2241. * Returns header data:
  2242. * <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>
  2243. * @return array()
  2244. * @access public
  2245. * @since 4.0.012 (2008-07-24)
  2246. */
  2247. public function getHeaderData() {
  2248. $ret = array();
  2249. $ret['logo'] = $this->header_logo;
  2250. $ret['logo_width'] = $this->header_logo_width;
  2251. $ret['title'] = $this->header_title;
  2252. $ret['string'] = $this->header_string;
  2253. return $ret;
  2254. }
  2255. /**
  2256. * Set header margin.
  2257. * (minimum distance between header and top page margin)
  2258. * @param int $hm distance in user units
  2259. * @access public
  2260. */
  2261. public function setHeaderMargin($hm=10) {
  2262. $this->header_margin = $hm;
  2263. }
  2264. /**
  2265. * Returns header margin in user units.
  2266. * @return float
  2267. * @since 4.0.012 (2008-07-24)
  2268. * @access public
  2269. */
  2270. public function getHeaderMargin() {
  2271. return $this->header_margin;
  2272. }
  2273. /**
  2274. * Set footer margin.
  2275. * (minimum distance between footer and bottom page margin)
  2276. * @param int $fm distance in user units
  2277. * @access public
  2278. */
  2279. public function setFooterMargin($fm=10) {
  2280. $this->footer_margin = $fm;
  2281. }
  2282. /**
  2283. * Returns footer margin in user units.
  2284. * @return float
  2285. * @since 4.0.012 (2008-07-24)
  2286. * @access public
  2287. */
  2288. public function getFooterMargin() {
  2289. return $this->footer_margin;
  2290. }
  2291. /**
  2292. * Set a flag to print page header.
  2293. * @param boolean $val set to true to print the page header (default), false otherwise.
  2294. * @access public
  2295. */
  2296. public function setPrintHeader($val=true) {
  2297. $this->print_header = $val;
  2298. }
  2299. /**
  2300. * Set a flag to print page footer.
  2301. * @param boolean $value set to true to print the page footer (default), false otherwise.
  2302. * @access public
  2303. */
  2304. public function setPrintFooter($val=true) {
  2305. $this->print_footer = $val;
  2306. }
  2307. /**
  2308. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  2309. * @return float
  2310. * @access public
  2311. */
  2312. public function getImageRBX() {
  2313. return $this->img_rb_x;
  2314. }
  2315. /**
  2316. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  2317. * @return float
  2318. * @access public
  2319. */
  2320. public function getImageRBY() {
  2321. return $this->img_rb_y;
  2322. }
  2323. /**
  2324. * This method is used to render the page header.
  2325. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2326. * @access public
  2327. */
  2328. public function Header() {
  2329. $ormargins = $this->getOriginalMargins();
  2330. $headerfont = $this->getHeaderFont();
  2331. $headerdata = $this->getHeaderData();
  2332. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  2333. $this->Image(K_PATH_IMAGES.$headerdata['logo'], $this->GetX(), $this->getHeaderMargin(), $headerdata['logo_width']);
  2334. $imgy = $this->getImageRBY();
  2335. } else {
  2336. $imgy = $this->GetY();
  2337. }
  2338. $cell_height = round(($this->getCellHeightRatio() * $headerfont[2]) / $this->getScaleFactor(), 2);
  2339. // set starting margin for text data cell
  2340. if ($this->getRTL()) {
  2341. $header_x = $ormargins['right'] + ($headerdata['logo_width'] * 1.1);
  2342. } else {
  2343. $header_x = $ormargins['left'] + ($headerdata['logo_width'] * 1.1);
  2344. }
  2345. $this->SetTextColor(0, 0, 0);
  2346. // header title
  2347. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  2348. $this->SetX($header_x);
  2349. $this->Cell(0, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  2350. // header string
  2351. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  2352. $this->SetX($header_x);
  2353. $this->MultiCell(0, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false);
  2354. // print an ending header line
  2355. $this->SetLineStyle(array('width' => 0.85 / $this->getScaleFactor(), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2356. $this->SetY((2.835 / $this->getScaleFactor()) + max($imgy, $this->GetY()));
  2357. if ($this->getRTL()) {
  2358. $this->SetX($ormargins['right']);
  2359. } else {
  2360. $this->SetX($ormargins['left']);
  2361. }
  2362. $this->Cell(0, 0, '', 'T', 0, 'C');
  2363. }
  2364. /**
  2365. * This method is used to render the page footer.
  2366. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2367. * @access public
  2368. */
  2369. public function Footer() {
  2370. $cur_y = $this->GetY();
  2371. $ormargins = $this->getOriginalMargins();
  2372. $this->SetTextColor(0, 0, 0);
  2373. //set style for cell border
  2374. $line_width = 0.85 / $this->getScaleFactor();
  2375. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2376. //print document barcode
  2377. $barcode = $this->getBarcode();
  2378. if (!empty($barcode)) {
  2379. $this->Ln($line_width);
  2380. $barcode_width = round(($this->getPageWidth() - $ormargins['left'] - $ormargins['right'])/3);
  2381. $this->write1DBarcode($barcode, 'C128B', $this->GetX(), $cur_y + $line_width, $barcode_width, (($this->getFooterMargin() / 3) - $line_width), 0.3, '', '');
  2382. }
  2383. if (empty($this->pagegroups)) {
  2384. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  2385. } else {
  2386. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  2387. }
  2388. $this->SetY($cur_y);
  2389. //Print page number
  2390. if ($this->getRTL()) {
  2391. $this->SetX($ormargins['right']);
  2392. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  2393. } else {
  2394. $this->SetX($ormargins['left']);
  2395. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'R');
  2396. }
  2397. }
  2398. /**
  2399. * This method is used to render the page header.
  2400. * @access protected
  2401. * @since 4.0.012 (2008-07-24)
  2402. */
  2403. protected function setHeader() {
  2404. if ($this->print_header) {
  2405. $temp_thead = $this->thead;
  2406. $temp_theadMargins = $this->theadMargins;
  2407. $lasth = $this->lasth;
  2408. $this->_out('q');
  2409. $this->rMargin = $this->original_rMargin;
  2410. $this->lMargin = $this->original_lMargin;
  2411. $this->cMargin = 0;
  2412. //set current position
  2413. if ($this->rtl) {
  2414. $this->SetXY($this->original_rMargin, $this->header_margin);
  2415. } else {
  2416. $this->SetXY($this->original_lMargin, $this->header_margin);
  2417. }
  2418. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  2419. $this->Header();
  2420. //restore position
  2421. if ($this->rtl) {
  2422. $this->SetXY($this->original_rMargin, $this->tMargin);
  2423. } else {
  2424. $this->SetXY($this->original_lMargin, $this->tMargin);
  2425. }
  2426. $this->_out('Q');
  2427. $this->lasth = $lasth;
  2428. $this->thead = $temp_thead;
  2429. $this->theadMargins = $temp_theadMargins;
  2430. }
  2431. }
  2432. /**
  2433. * This method is used to render the page footer.
  2434. * @access protected
  2435. * @since 4.0.012 (2008-07-24)
  2436. */
  2437. protected function setFooter() {
  2438. //Page footer
  2439. // save current graphic settings
  2440. $gvars = $this->getGraphicVars();
  2441. // mark this point
  2442. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  2443. $this->_out("\n");
  2444. if ($this->print_footer) {
  2445. $temp_thead = $this->thead;
  2446. $temp_theadMargins = $this->theadMargins;
  2447. $lasth = $this->lasth;
  2448. $this->_out('q');
  2449. $this->rMargin = $this->original_rMargin;
  2450. $this->lMargin = $this->original_lMargin;
  2451. $this->cMargin = 0;
  2452. //set current position
  2453. $footer_y = $this->h - $this->footer_margin;
  2454. if ($this->rtl) {
  2455. $this->SetXY($this->original_rMargin, $footer_y);
  2456. } else {
  2457. $this->SetXY($this->original_lMargin, $footer_y);
  2458. }
  2459. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  2460. $this->Footer();
  2461. //restore position
  2462. if ($this->rtl) {
  2463. $this->SetXY($this->original_rMargin, $this->tMargin);
  2464. } else {
  2465. $this->SetXY($this->original_lMargin, $this->tMargin);
  2466. }
  2467. $this->_out('Q');
  2468. $this->lasth = $lasth;
  2469. $this->thead = $temp_thead;
  2470. $this->theadMargins = $temp_theadMargins;
  2471. }
  2472. // restore graphic settings
  2473. $this->setGraphicVars($gvars);
  2474. // calculate footer lenght
  2475. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  2476. }
  2477. /**
  2478. * This method is used to render the table header on new page (if any).
  2479. * @access protected
  2480. * @since 4.5.030 (2009-03-25)
  2481. */
  2482. protected function setTableHeader() {
  2483. if (isset($this->theadMargins['top'])) {
  2484. // restore the original top-margin
  2485. $this->tMargin = $this->theadMargins['top'];
  2486. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2487. $this->y = $this->tMargin;
  2488. }
  2489. if (!$this->empty_string($this->thead)) {
  2490. // set margins
  2491. $prev_lMargin = $this->lMargin;
  2492. $prev_rMargin = $this->rMargin;
  2493. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2494. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2495. $this->cMargin = $this->theadMargins['cmargin'];
  2496. // print table header
  2497. $this->writeHTML($this->thead, false, false, false, false, '');
  2498. // set new top margin to skip the table headers
  2499. if (!isset($this->theadMargins['top'])) {
  2500. $this->theadMargins['top'] = $this->tMargin;
  2501. }
  2502. $this->tMargin = $this->y;
  2503. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2504. $this->lasth = 0;
  2505. $this->lMargin = $prev_lMargin;
  2506. $this->rMargin = $prev_rMargin;
  2507. }
  2508. }
  2509. /**
  2510. * Returns the current page number.
  2511. * @return int page number
  2512. * @access public
  2513. * @since 1.0
  2514. * @see AliasNbPages(), getAliasNbPages()
  2515. */
  2516. public function PageNo() {
  2517. return $this->page;
  2518. }
  2519. /**
  2520. * Defines a new spot color.
  2521. * It can be expressed in RGB components or gray scale.
  2522. * The method can be called before the first page is created and the value is retained from page to page.
  2523. * @param int $c Cyan color for CMYK. Value between 0 and 255
  2524. * @param int $m Magenta color for CMYK. Value between 0 and 255
  2525. * @param int $y Yellow color for CMYK. Value between 0 and 255
  2526. * @param int $k Key (Black) color for CMYK. Value between 0 and 255
  2527. * @access public
  2528. * @since 4.0.024 (2008-09-12)
  2529. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2530. */
  2531. public function AddSpotColor($name, $c, $m, $y, $k) {
  2532. if (!isset($this->spot_colors[$name])) {
  2533. $i = 1 + count($this->spot_colors);
  2534. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  2535. }
  2536. }
  2537. /**
  2538. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  2539. * It can be expressed in RGB components or gray scale.
  2540. * The method can be called before the first page is created and the value is retained from page to page.
  2541. * @param array $color array of colors
  2542. * @access public
  2543. * @since 3.1.000 (2008-06-11)
  2544. * @see SetDrawColor()
  2545. */
  2546. public function SetDrawColorArray($color) {
  2547. if (isset($color)) {
  2548. $color = array_values($color);
  2549. $r = isset($color[0]) ? $color[0] : -1;
  2550. $g = isset($color[1]) ? $color[1] : -1;
  2551. $b = isset($color[2]) ? $color[2] : -1;
  2552. $k = isset($color[3]) ? $color[3] : -1;
  2553. if ($r >= 0) {
  2554. $this->SetDrawColor($r, $g, $b, $k);
  2555. }
  2556. }
  2557. }
  2558. /**
  2559. * 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.
  2560. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2561. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2562. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2563. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2564. * @access public
  2565. * @since 1.3
  2566. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  2567. */
  2568. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2569. // set default values
  2570. if (!is_numeric($col1)) {
  2571. $col1 = 0;
  2572. }
  2573. if (!is_numeric($col2)) {
  2574. $col2 = -1;
  2575. }
  2576. if (!is_numeric($col3)) {
  2577. $col3 = -1;
  2578. }
  2579. if (!is_numeric($col4)) {
  2580. $col4 = -1;
  2581. }
  2582. //Set color for all stroking operations
  2583. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2584. // Grey scale
  2585. $this->DrawColor = sprintf('%.3F G', $col1/255);
  2586. } elseif ($col4 == -1) {
  2587. // RGB
  2588. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $col1/255, $col2/255, $col3/255);
  2589. } else {
  2590. // CMYK
  2591. $this->DrawColor = sprintf('%.3F %.3F %.3F %.3F K', $col1/100, $col2/100, $col3/100, $col4/100);
  2592. }
  2593. if ($this->page > 0) {
  2594. $this->_out($this->DrawColor);
  2595. }
  2596. }
  2597. /**
  2598. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  2599. * @param string $name name of the spot color
  2600. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2601. * @access public
  2602. * @since 4.0.024 (2008-09-12)
  2603. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2604. */
  2605. public function SetDrawSpotColor($name, $tint=100) {
  2606. if (!isset($this->spot_colors[$name])) {
  2607. $this->Error('Undefined spot color: '.$name);
  2608. }
  2609. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], $tint/100);
  2610. if ($this->page > 0) {
  2611. $this->_out($this->DrawColor);
  2612. }
  2613. }
  2614. /**
  2615. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  2616. * It can be expressed in RGB components or gray scale.
  2617. * The method can be called before the first page is created and the value is retained from page to page.
  2618. * @param array $color array of colors
  2619. * @access public
  2620. * @since 3.1.000 (2008-6-11)
  2621. * @see SetFillColor()
  2622. */
  2623. public function SetFillColorArray($color) {
  2624. if (isset($color)) {
  2625. $color = array_values($color);
  2626. $r = isset($color[0]) ? $color[0] : -1;
  2627. $g = isset($color[1]) ? $color[1] : -1;
  2628. $b = isset($color[2]) ? $color[2] : -1;
  2629. $k = isset($color[3]) ? $color[3] : -1;
  2630. if ($r >= 0) {
  2631. $this->SetFillColor($r, $g, $b, $k);
  2632. }
  2633. }
  2634. }
  2635. /**
  2636. * 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.
  2637. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2638. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2639. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2640. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2641. * @access public
  2642. * @since 1.3
  2643. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  2644. */
  2645. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2646. // set default values
  2647. if (!is_numeric($col1)) {
  2648. $col1 = 0;
  2649. }
  2650. if (!is_numeric($col2)) {
  2651. $col2 = -1;
  2652. }
  2653. if (!is_numeric($col3)) {
  2654. $col3 = -1;
  2655. }
  2656. if (!is_numeric($col4)) {
  2657. $col4 = -1;
  2658. }
  2659. //Set color for all filling operations
  2660. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2661. // Grey scale
  2662. $this->FillColor = sprintf('%.3F g', $col1/255);
  2663. $this->bgcolor = array('G' => $col1);
  2664. } elseif ($col4 == -1) {
  2665. // RGB
  2666. $this->FillColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2667. $this->bgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2668. } else {
  2669. // CMYK
  2670. $this->FillColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2671. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2672. }
  2673. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2674. if ($this->page > 0) {
  2675. $this->_out($this->FillColor);
  2676. }
  2677. }
  2678. /**
  2679. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  2680. * @param string $name name of the spot color
  2681. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2682. * @access public
  2683. * @since 4.0.024 (2008-09-12)
  2684. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  2685. */
  2686. public function SetFillSpotColor($name, $tint=100) {
  2687. if (!isset($this->spot_colors[$name])) {
  2688. $this->Error('Undefined spot color: '.$name);
  2689. }
  2690. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2691. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2692. if ($this->page > 0) {
  2693. $this->_out($this->FillColor);
  2694. }
  2695. }
  2696. /**
  2697. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  2698. * The method can be called before the first page is created and the value is retained from page to page.
  2699. * @param array $color array of colors
  2700. * @access public
  2701. * @since 3.1.000 (2008-6-11)
  2702. * @see SetFillColor()
  2703. */
  2704. public function SetTextColorArray($color) {
  2705. if (isset($color)) {
  2706. $color = array_values($color);
  2707. $r = isset($color[0]) ? $color[0] : -1;
  2708. $g = isset($color[1]) ? $color[1] : -1;
  2709. $b = isset($color[2]) ? $color[2] : -1;
  2710. $k = isset($color[3]) ? $color[3] : -1;
  2711. if ($r >= 0) {
  2712. $this->SetTextColor($r, $g, $b, $k);
  2713. }
  2714. }
  2715. }
  2716. /**
  2717. * 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.
  2718. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2719. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2720. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2721. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2722. * @access public
  2723. * @since 1.3
  2724. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  2725. */
  2726. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2727. // set default values
  2728. if (!is_numeric($col1)) {
  2729. $col1 = 0;
  2730. }
  2731. if (!is_numeric($col2)) {
  2732. $col2 = -1;
  2733. }
  2734. if (!is_numeric($col3)) {
  2735. $col3 = -1;
  2736. }
  2737. if (!is_numeric($col4)) {
  2738. $col4 = -1;
  2739. }
  2740. //Set color for text
  2741. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2742. // Grey scale
  2743. $this->TextColor = sprintf('%.3F g', $col1/255);
  2744. $this->fgcolor = array('G' => $col1);
  2745. } elseif ($col4 == -1) {
  2746. // RGB
  2747. $this->TextColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2748. $this->fgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2749. } else {
  2750. // CMYK
  2751. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2752. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2753. }
  2754. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2755. }
  2756. /**
  2757. * Defines the spot color used for text.
  2758. * @param string $name name of the spot color
  2759. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2760. * @access public
  2761. * @since 4.0.024 (2008-09-12)
  2762. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  2763. */
  2764. public function SetTextSpotColor($name, $tint=100) {
  2765. if (!isset($this->spot_colors[$name])) {
  2766. $this->Error('Undefined spot color: '.$name);
  2767. }
  2768. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2769. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2770. if ($this->page > 0) {
  2771. $this->_out($this->TextColor);
  2772. }
  2773. }
  2774. /**
  2775. * Returns the length of a string in user unit. A font must be selected.<br>
  2776. * @param string $s The string whose length is to be computed
  2777. * @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.
  2778. * @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.
  2779. * @param float $fontsize Font size in points. The default value is the current size.
  2780. * @return int string length
  2781. * @author Nicola Asuni
  2782. * @access public
  2783. * @since 1.2
  2784. */
  2785. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0) {
  2786. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize);
  2787. }
  2788. /**
  2789. * Returns the string length of an array of chars in user unit. A font must be selected.<br>
  2790. * @param string $sa The array of chars whose total length is to be computed
  2791. * @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.
  2792. * @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.
  2793. * @param float $fontsize Font size in points. The default value is the current size.
  2794. * @return int string length
  2795. * @author Nicola Asuni
  2796. * @access public
  2797. * @since 2.4.000 (2008-03-06)
  2798. */
  2799. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0) {
  2800. // store current values
  2801. if (!$this->empty_string($fontname)) {
  2802. $prev_FontFamily = $this->FontFamily;
  2803. $prev_FontStyle = $this->FontStyle;
  2804. $prev_FontSizePt = $this->FontSizePt;
  2805. $this->SetFont($fontname, $fontstyle, $fontsize);
  2806. }
  2807. $w = 0;
  2808. foreach ($sa as $char) {
  2809. $w += $this->GetCharWidth($char);
  2810. }
  2811. // restore previous values
  2812. if (!$this->empty_string($fontname)) {
  2813. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt);
  2814. }
  2815. return $w;
  2816. }
  2817. /**
  2818. * Returns the length of the char in user unit for the current font.
  2819. * @param int $char The char code whose length is to be returned
  2820. * @return int char width
  2821. * @author Nicola Asuni
  2822. * @access public
  2823. * @since 2.4.000 (2008-03-06)
  2824. */
  2825. public function GetCharWidth($char) {
  2826. if ($char == 173) {
  2827. // SHY character will not be printed
  2828. return (0);
  2829. }
  2830. $cw = &$this->CurrentFont['cw'];
  2831. if (isset($cw[$char])) {
  2832. $w = $cw[$char];
  2833. } elseif (isset($this->CurrentFont['dw'])) {
  2834. // default width
  2835. $w = $this->CurrentFont['dw'];
  2836. } elseif (isset($cw[32])) {
  2837. // default width
  2838. $dw = $cw[32];
  2839. } else {
  2840. $w = 600;
  2841. }
  2842. return ($w * $this->FontSize / 1000);
  2843. }
  2844. /**
  2845. * Returns the numbero of characters in a string.
  2846. * @param string $s The input string.
  2847. * @return int number of characters
  2848. * @access public
  2849. * @since 2.0.0001 (2008-01-07)
  2850. */
  2851. public function GetNumChars($s) {
  2852. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  2853. return count($this->UTF8StringToArray($s));
  2854. }
  2855. return strlen($s);
  2856. }
  2857. /**
  2858. * Fill the list of available fonts ($this->fontlist).
  2859. * @access protected
  2860. * @since 4.0.013 (2008-07-28)
  2861. */
  2862. protected function getFontsList() {
  2863. $fontsdir = opendir($this->_getfontpath());
  2864. while (($file = readdir($fontsdir)) !== false) {
  2865. if (substr($file, -4) == '.php') {
  2866. array_push($this->fontlist, strtolower(basename($file, '.php')));
  2867. }
  2868. }
  2869. closedir($fontsdir);
  2870. }
  2871. /**
  2872. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  2873. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  2874. * 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.
  2875. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  2876. * @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>
  2877. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  2878. * @return array containing the font data, or false in case of error.
  2879. * @access public
  2880. * @since 1.5
  2881. * @see SetFont()
  2882. */
  2883. public function AddFont($family, $style='', $fontfile='') {
  2884. if ($this->empty_string($family)) {
  2885. if (!$this->empty_string($this->FontFamily)) {
  2886. $family = $this->FontFamily;
  2887. } else {
  2888. $this->Error('Empty font family');
  2889. }
  2890. }
  2891. $family = strtolower($family);
  2892. if ((!$this->isunicode) AND ($family == 'arial')) {
  2893. $family = 'helvetica';
  2894. }
  2895. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  2896. $style = '';
  2897. }
  2898. $tempstyle = strtoupper($style);
  2899. $style = '';
  2900. // underline
  2901. if (strpos($tempstyle, 'U') !== false) {
  2902. $this->underline = true;
  2903. } else {
  2904. $this->underline = false;
  2905. }
  2906. // line through (deleted)
  2907. if (strpos($tempstyle, 'D') !== false) {
  2908. $this->linethrough = true;
  2909. } else {
  2910. $this->linethrough = false;
  2911. }
  2912. // bold
  2913. if (strpos($tempstyle, 'B') !== false) {
  2914. $style .= 'B';
  2915. }
  2916. // oblique
  2917. if (strpos($tempstyle, 'I') !== false) {
  2918. $style .= 'I';
  2919. }
  2920. $bistyle = $style;
  2921. $fontkey = $family.$style;
  2922. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '');
  2923. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  2924. // check if the font has been already added
  2925. if ($this->getFontBuffer($fontkey) !== false) {
  2926. return $fontdata;
  2927. }
  2928. if (isset($type)) {
  2929. unset($type);
  2930. }
  2931. if (isset($cw)) {
  2932. unset($cw);
  2933. }
  2934. // get specified font directory (if any)
  2935. $fontdir = '';
  2936. if (!$this->empty_string($fontfile)) {
  2937. $fontdir = dirname($fontfile);
  2938. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  2939. $fontdir = '';
  2940. } else {
  2941. $fontdir .= '/';
  2942. }
  2943. }
  2944. // search and include font file
  2945. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  2946. // build a standard filenames for specified font
  2947. $fontfile1 = str_replace(' ', '', $family).strtolower($style).'.php';
  2948. $fontfile2 = str_replace(' ', '', $family).'.php';
  2949. // search files on various directories
  2950. if (file_exists($fontdir.$fontfile1)) {
  2951. $fontfile = $fontdir.$fontfile1;
  2952. } elseif (file_exists($this->_getfontpath().$fontfile1)) {
  2953. $fontfile = $this->_getfontpath().$fontfile1;
  2954. } elseif (file_exists($fontfile1)) {
  2955. $fontfile = $fontfile1;
  2956. } elseif (file_exists($fontdir.$fontfile2)) {
  2957. $fontfile = $fontdir.$fontfile2;
  2958. } elseif (file_exists($this->_getfontpath().$fontfile2)) {
  2959. $fontfile = $this->_getfontpath().$fontfile2;
  2960. } else {
  2961. $fontfile = $fontfile2;
  2962. }
  2963. }
  2964. // include font file
  2965. if (file_exists($fontfile)) {
  2966. include($fontfile);
  2967. } else {
  2968. $this->Error('Could not include font definition file: '.$family.'');
  2969. }
  2970. // check font parameters
  2971. if ((!isset($type)) OR (!isset($cw))) {
  2972. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  2973. }
  2974. // SET default parameters
  2975. if (!isset($file) OR $this->empty_string($file)) {
  2976. $file = '';
  2977. }
  2978. if (!isset($enc) OR $this->empty_string($enc)) {
  2979. $enc = '';
  2980. }
  2981. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  2982. $cidinfo = array('Registry'=>'Adobe','Ordering'=>'Identity','Supplement'=>0);
  2983. $cidinfo['uni2cid'] = array();
  2984. }
  2985. if (!isset($ctg) OR $this->empty_string($ctg)) {
  2986. $ctg = '';
  2987. }
  2988. if (!isset($desc) OR $this->empty_string($desc)) {
  2989. $desc = array();
  2990. }
  2991. if (!isset($up) OR $this->empty_string($up)) {
  2992. $up = -100;
  2993. }
  2994. if (!isset($ut) OR $this->empty_string($ut)) {
  2995. $ut = 50;
  2996. }
  2997. if (!isset($cw) OR $this->empty_string($cw)) {
  2998. $cw = array();
  2999. }
  3000. if (!isset($dw) OR $this->empty_string($dw)) {
  3001. // set default width
  3002. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  3003. $dw = $desc['MissingWidth'];
  3004. } elseif (isset($cw[32])) {
  3005. $dw = $cw[32];
  3006. } else {
  3007. $dw = 600;
  3008. }
  3009. }
  3010. ++$this->numfonts;
  3011. if ($type == 'cidfont0') {
  3012. // register CID font (all styles at once)
  3013. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  3014. $sname = $name.$styles[$bistyle];
  3015. if ((strpos($bistyle, 'B') !== false) AND (isset($desc['StemV'])) AND ($desc['StemV'] == 70)) {
  3016. $desc['StemV'] = 120;
  3017. }
  3018. } elseif ($type == 'core') {
  3019. $name = $this->CoreFonts[$fontkey];
  3020. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  3021. // ...
  3022. } elseif ($type == 'TrueTypeUnicode') {
  3023. $enc = 'Identity-H';
  3024. } else {
  3025. $this->Error('Unknow font type: '.$type.'');
  3026. }
  3027. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg));
  3028. if (isset($diff) AND (!empty($diff))) {
  3029. //Search existing encodings
  3030. $d = 0;
  3031. $nb = count($this->diffs);
  3032. for ($i=1; $i <= $nb; ++$i) {
  3033. if ($this->diffs[$i] == $diff) {
  3034. $d = $i;
  3035. break;
  3036. }
  3037. }
  3038. if ($d == 0) {
  3039. $d = $nb + 1;
  3040. $this->diffs[$d] = $diff;
  3041. }
  3042. $this->setFontSubBuffer($fontkey, 'diff', $d);
  3043. }
  3044. if (!$this->empty_string($file)) {
  3045. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  3046. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir);
  3047. } elseif ($type != 'core') {
  3048. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir);
  3049. }
  3050. }
  3051. return $fontdata;
  3052. }
  3053. /**
  3054. * Sets the font used to print character strings.
  3055. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  3056. * The method can be called before the first page is created and the font is retained from page to page.
  3057. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  3058. * 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 />
  3059. * @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.
  3060. * @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.
  3061. * @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
  3062. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3063. * @access public
  3064. * @since 1.0
  3065. * @see AddFont(), SetFontSize()
  3066. */
  3067. public function SetFont($family, $style='', $size=0, $fontfile='') {
  3068. //Select a font; size given in points
  3069. if ($size == 0) {
  3070. $size = $this->FontSizePt;
  3071. }
  3072. // try to add font (if not already added)
  3073. $fontdata = $this->AddFont($family, $style, $fontfile);
  3074. $this->FontFamily = $fontdata['family'];
  3075. $this->FontStyle = $fontdata['style'];
  3076. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  3077. $this->SetFontSize($size);
  3078. }
  3079. /**
  3080. * Defines the size of the current font.
  3081. * @param float $size The size (in points)
  3082. * @access public
  3083. * @since 1.0
  3084. * @see SetFont()
  3085. */
  3086. public function SetFontSize($size) {
  3087. //Set font size in points
  3088. $this->FontSizePt = $size;
  3089. $this->FontSize = $size / $this->k;
  3090. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  3091. $this->FontAscent = $this->CurrentFont['desc']['Ascent'] * $this->FontSize / 1000;
  3092. } else {
  3093. $this->FontAscent = 0.8 * $this->FontSize;
  3094. }
  3095. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] > 0)) {
  3096. $this->FontDescent = - $this->CurrentFont['desc']['Descent'] * $this->FontSize / 1000;
  3097. } else {
  3098. $this->FontDescent = 0.2 * $this->FontSize;
  3099. }
  3100. if (($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  3101. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  3102. }
  3103. }
  3104. /**
  3105. * Defines the default monospaced font.
  3106. * @param string $font Font name.
  3107. * @access public
  3108. * @since 4.5.025
  3109. */
  3110. public function SetDefaultMonospacedFont($font) {
  3111. $this->default_monospaced_font = $font;
  3112. }
  3113. /**
  3114. * 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 />
  3115. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  3116. * @access public
  3117. * @since 1.5
  3118. * @see Cell(), Write(), Image(), Link(), SetLink()
  3119. */
  3120. public function AddLink() {
  3121. //Create a new internal link
  3122. $n = count($this->links) + 1;
  3123. $this->links[$n] = array(0, 0);
  3124. return $n;
  3125. }
  3126. /**
  3127. * Defines the page and position a link points to.
  3128. * @param int $link The link identifier returned by AddLink()
  3129. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  3130. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  3131. * @access public
  3132. * @since 1.5
  3133. * @see AddLink()
  3134. */
  3135. public function SetLink($link, $y=0, $page=-1) {
  3136. if ($y == -1) {
  3137. $y = $this->y;
  3138. }
  3139. if ($page == -1) {
  3140. $page = $this->page;
  3141. }
  3142. $this->links[$link] = array($page, $y);
  3143. }
  3144. /**
  3145. * Puts a link on a rectangular area of the page.
  3146. * 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.
  3147. * @param float $x Abscissa of the upper-left corner of the rectangle
  3148. * @param float $y Ordinate of the upper-left corner of the rectangle
  3149. * @param float $w Width of the rectangle
  3150. * @param float $h Height of the rectangle
  3151. * @param mixed $link URL or identifier returned by AddLink()
  3152. * @param int $spaces number of spaces on the text to link
  3153. * @access public
  3154. * @since 1.5
  3155. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  3156. */
  3157. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  3158. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  3159. }
  3160. /**
  3161. * Puts a markup annotation on a rectangular area of the page.
  3162. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  3163. * @param float $x Abscissa of the upper-left corner of the rectangle
  3164. * @param float $y Ordinate of the upper-left corner of the rectangle
  3165. * @param float $w Width of the rectangle
  3166. * @param float $h Height of the rectangle
  3167. * @param string $text annotation text or alternate content
  3168. * @param array $opt array of options (see section 8.4 of PDF reference 1.7).
  3169. * @param int $spaces number of spaces on the text to link
  3170. * @access public
  3171. * @since 4.0.018 (2008-08-06)
  3172. */
  3173. public function Annotation($x='', $y='', $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  3174. if ($x === '') {
  3175. $x = $this->x;
  3176. }
  3177. if ($y === '') {
  3178. $y = $this->y;
  3179. }
  3180. // recalculate coordinates to account for graphic transformations
  3181. if (isset($this->transfmatrix)) {
  3182. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  3183. $maxid = count($this->transfmatrix[$i]) - 1;
  3184. for ($j=$maxid; $j >= 0; --$j) {
  3185. $ctm = $this->transfmatrix[$i][$j];
  3186. if (isset($ctm['a'])) {
  3187. $x = $x * $this->k;
  3188. $y = ($this->h - $y) * $this->k;
  3189. $w = $w * $this->k;
  3190. $h = $h * $this->k;
  3191. // top left
  3192. $xt = $x;
  3193. $yt = $y;
  3194. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3195. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3196. // top right
  3197. $xt = $x + $w;
  3198. $yt = $y;
  3199. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3200. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3201. // bottom left
  3202. $xt = $x;
  3203. $yt = $y - $h;
  3204. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3205. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3206. // bottom right
  3207. $xt = $x + $w;
  3208. $yt = $y - $h;
  3209. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3210. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3211. // new coordinates (rectangle area)
  3212. $x = min($x1, $x2, $x3, $x4);
  3213. $y = max($y1, $y2, $y3, $y4);
  3214. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  3215. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  3216. $x = $x / $this->k;
  3217. $y = $this->h - ($y / $this->k);
  3218. }
  3219. }
  3220. }
  3221. }
  3222. if ($this->page <= 0) {
  3223. $page = 1;
  3224. } else {
  3225. $page = $this->page;
  3226. }
  3227. if (!isset($this->PageAnnots[$page])) {
  3228. $this->PageAnnots[$page] = array();
  3229. }
  3230. $this->PageAnnots[$page][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  3231. if (($opt['Subtype'] == 'FileAttachment') AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  3232. $this->embeddedfiles[basename($opt['FS'])] = array('file' => $opt['FS'], 'n' => (count($this->embeddedfiles) + $this->embedded_start_obj_id));
  3233. }
  3234. // Add widgets annotation's icons
  3235. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  3236. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  3237. }
  3238. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  3239. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  3240. }
  3241. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  3242. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  3243. }
  3244. ++$this->annot_obj_id;
  3245. }
  3246. /**
  3247. * Embedd the attached files.
  3248. * @since 4.4.000 (2008-12-07)
  3249. * @access protected
  3250. * @see Annotation()
  3251. */
  3252. protected function _putEmbeddedFiles() {
  3253. reset($this->embeddedfiles);
  3254. foreach ($this->embeddedfiles as $filename => $filedata) {
  3255. $data = file_get_contents($filedata['file']);
  3256. $filter = '';
  3257. if ($this->compress) {
  3258. $data = gzcompress($data);
  3259. $filter = ' /Filter /FlateDecode';
  3260. }
  3261. $this->offsets[$filedata['n']] = $this->bufferlen;
  3262. $this->_out($filedata['n'].' 0 obj');
  3263. $this->_out('<</Type /EmbeddedFile'.$filter.' /Length '.strlen($data).' >>');
  3264. $this->_putstream($data);
  3265. $this->_out('endobj');
  3266. }
  3267. }
  3268. /**
  3269. * Prints a character string.
  3270. * The origin is on the left of the first charcter, on the baseline.
  3271. * This method allows to place a string precisely on the page.
  3272. * @param float $x Abscissa of the origin
  3273. * @param float $y Ordinate of the origin
  3274. * @param string $txt String to print
  3275. * @param int $stroke outline size in points (0 = disable)
  3276. * @param boolean $clip if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  3277. * @access public
  3278. * @since 1.0
  3279. * @deprecated deprecated since version 4.3.005 (2008-11-25)
  3280. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  3281. */
  3282. public function Text($x, $y, $txt, $stroke=0, $clip=false) {
  3283. //Output a string
  3284. if ($this->rtl) {
  3285. // bidirectional algorithm (some chars may be changed affecting the line length)
  3286. $s = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  3287. $l = $this->GetArrStringWidth($s);
  3288. $xr = $this->w - $x - $l;
  3289. } else {
  3290. $xr = $x;
  3291. }
  3292. $opt = '';
  3293. if (($stroke > 0) AND (!$clip)) {
  3294. $opt .= '1 Tr '.intval($stroke).' w ';
  3295. } elseif (($stroke > 0) AND $clip) {
  3296. $opt .= '5 Tr '.intval($stroke).' w ';
  3297. } elseif ($clip) {
  3298. $opt .= '7 Tr ';
  3299. }
  3300. $s = sprintf('BT %.2F %.2F Td %s(%s) Tj ET 0 Tr', $xr * $this->k, ($this->h-$y) * $this->k, $opt, $this->_escapetext($txt));
  3301. if ($this->underline AND ($txt!='')) {
  3302. $s .= ' '.$this->_dounderline($xr, $y, $txt);
  3303. }
  3304. if ($this->linethrough AND ($txt!='')) {
  3305. $s .= ' '.$this->_dolinethrough($xr, $y, $txt);
  3306. }
  3307. if ($this->ColorFlag AND (!$clip)) {
  3308. $s='q '.$this->TextColor.' '.$s.' Q';
  3309. }
  3310. $this->_out($s);
  3311. }
  3312. /**
  3313. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  3314. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  3315. * This method is called automatically and should not be called directly by the application.
  3316. * @return boolean
  3317. * @access public
  3318. * @since 1.4
  3319. * @see SetAutoPageBreak()
  3320. */
  3321. public function AcceptPageBreak() {
  3322. return $this->AutoPageBreak;
  3323. }
  3324. /**
  3325. * Add page if needed.
  3326. * @param float $h Cell height. Default value: 0.
  3327. * @param mixed $y starting y position, leave empty for current position.
  3328. * @param boolean $addpage if true add a page, otherwise only return the true/false state
  3329. * @return boolean true in case of page break, false otherwise.
  3330. * @since 3.2.000 (2008-07-01)
  3331. * @access protected
  3332. */
  3333. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  3334. if ($this->empty_string($y)) {
  3335. $y = $this->y;
  3336. }
  3337. if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
  3338. if ($addpage) {
  3339. //Automatic page break
  3340. $x = $this->x;
  3341. $this->AddPage($this->CurOrientation);
  3342. $this->y = $this->tMargin;
  3343. $oldpage = $this->page - 1;
  3344. if ($this->rtl) {
  3345. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  3346. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  3347. } else {
  3348. $this->x = $x;
  3349. }
  3350. } else {
  3351. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3352. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  3353. } else {
  3354. $this->x = $x;
  3355. }
  3356. }
  3357. }
  3358. return true;
  3359. }
  3360. return false;
  3361. }
  3362. /**
  3363. * 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 />
  3364. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3365. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3366. * @param float $h Cell height. Default value: 0.
  3367. * @param string $txt String to print. Default value: empty string.
  3368. * @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>
  3369. * @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>
  3370. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3371. * @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>
  3372. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3373. * @param mixed $link URL or identifier returned by AddLink().
  3374. * @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>
  3375. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3376. * @access public
  3377. * @since 1.0
  3378. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  3379. */
  3380. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3381. //$min_cell_height = $this->FontAscent + $this->FontDescent;
  3382. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3383. if ($h < $min_cell_height) {
  3384. $h = $min_cell_height;
  3385. }
  3386. $this->checkPageBreak($h);
  3387. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height));
  3388. }
  3389. /**
  3390. * Removes SHY characters from text.
  3391. * @param string $txt input string
  3392. * @return string without SHY characters.
  3393. * @access public
  3394. * @since (4.5.019) 2009-02-28
  3395. */
  3396. public function removeSHY($txt='') {
  3397. /*
  3398. * Unicode Data
  3399. * Name : SOFT HYPHEN, commonly abbreviated as SHY
  3400. * HTML Entity (decimal): &#173;
  3401. * HTML Entity (hex): &#xad;
  3402. * HTML Entity (named): &shy;
  3403. * How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]
  3404. * UTF-8 (hex): 0xC2 0xAD (c2ad)
  3405. * UTF-8 character: chr(194).chr(173)
  3406. */
  3407. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  3408. if (!$this->isunicode) {
  3409. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  3410. }
  3411. return $txt;
  3412. }
  3413. /**
  3414. * 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 />
  3415. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3416. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3417. * @param float $h Cell height. Default value: 0.
  3418. * @param string $txt String to print. Default value: empty string.
  3419. * @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>
  3420. * @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.
  3421. * @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>
  3422. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3423. * @param mixed $link URL or identifier returned by AddLink().
  3424. * @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>
  3425. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3426. * @access protected
  3427. * @since 1.0
  3428. * @see Cell()
  3429. */
  3430. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3431. $txt = $this->removeSHY($txt);
  3432. $rs = ''; //string to be returned
  3433. if (!$ignore_min_height) {
  3434. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3435. if ($h < $min_cell_height) {
  3436. $h = $min_cell_height;
  3437. }
  3438. }
  3439. $k = $this->k;
  3440. if ($this->empty_string($w) OR ($w <= 0)) {
  3441. if ($this->rtl) {
  3442. $w = $this->x - $this->lMargin;
  3443. } else {
  3444. $w = $this->w - $this->rMargin - $this->x;
  3445. }
  3446. }
  3447. $s = '';
  3448. // fill and borders
  3449. if (($fill == 1) OR ($border == 1)) {
  3450. if ($fill == 1) {
  3451. $op = ($border == 1) ? 'B' : 'f';
  3452. } else {
  3453. $op = 'S';
  3454. }
  3455. if ($this->rtl) {
  3456. $xk = (($this->x - $w) * $k);
  3457. } else {
  3458. $xk = ($this->x * $k);
  3459. }
  3460. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $this->y) * $k), ($w * $k), (-$h * $k), $op);
  3461. }
  3462. if (is_string($border)) {
  3463. $lm = ($this->LineWidth / 2);
  3464. $x = $this->x;
  3465. $y = $this->y;
  3466. if (strpos($border,'L') !== false) {
  3467. if ($this->rtl) {
  3468. $xk = ($x - $w) * $k;
  3469. } else {
  3470. $xk = $x * $k;
  3471. }
  3472. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm)) * $k));
  3473. }
  3474. if (strpos($border,'T') !== false) {
  3475. if ($this->rtl) {
  3476. $xk = ($x - $w + $lm) * $k;
  3477. $xwk = ($x - $lm) * $k;
  3478. } else {
  3479. $xk = ($x - $lm) * $k;
  3480. $xwk = ($x + $w + $lm) * $k;
  3481. }
  3482. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y) * $k), $xwk, (($this->h - $y) * $k));
  3483. }
  3484. if (strpos($border,'R') !== false) {
  3485. if ($this->rtl) {
  3486. $xk = $x * $k;
  3487. } else {
  3488. $xk = ($x + $w) * $k;
  3489. }
  3490. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm))* $k));
  3491. }
  3492. if (strpos($border,'B') !== false) {
  3493. if ($this->rtl) {
  3494. $xk = ($x - $w + $lm) * $k;
  3495. $xwk = ($x - $lm) * $k;
  3496. } else {
  3497. $xk = ($x - $lm) * $k;
  3498. $xwk = ($x + $w + $lm) * $k;
  3499. }
  3500. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - ($y + $h)) * $k), $xwk, (($this->h - ($y + $h)) * $k));
  3501. }
  3502. }
  3503. if ($txt != '') {
  3504. // text lenght
  3505. $width = $this->GetStringWidth($txt);
  3506. // ratio between cell lenght and text lenght
  3507. if ($width <= 0) {
  3508. $ratio = 1;
  3509. } else {
  3510. $ratio = ($w - (2 * $this->cMargin)) / $width;
  3511. }
  3512. // stretch text if required
  3513. if (($stretch > 0) AND (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0)))) {
  3514. if ($stretch > 2) {
  3515. // spacing
  3516. //Calculate character spacing in points
  3517. $char_space = (($w - $width - (2 * $this->cMargin)) * $this->k) / max($this->GetNumChars($txt)-1,1);
  3518. //Set character spacing
  3519. $rs .= sprintf('BT %.2F Tc ET ', $char_space);
  3520. } else {
  3521. // scaling
  3522. //Calculate horizontal scaling
  3523. $horiz_scale = $ratio * 100.0;
  3524. //Set horizontal scaling
  3525. $rs .= sprintf('BT %.2F Tz ET ', $horiz_scale);
  3526. }
  3527. $align = '';
  3528. $width = $w - (2 * $this->cMargin);
  3529. } else {
  3530. $stretch == 0;
  3531. }
  3532. if ($this->ColorFlag) {
  3533. $s .= 'q '.$this->TextColor.' ';
  3534. }
  3535. $txt2 = $this->_escapetext($txt);
  3536. // Justification
  3537. if ($align == 'J') {
  3538. // count number of spaces
  3539. $ns = substr_count($txt, ' ');
  3540. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  3541. // get string width without spaces
  3542. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  3543. // calculate average space width
  3544. $spacewidth = -1000 * ($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1) / $this->FontSize;
  3545. // set word position to be used with TJ operator
  3546. $txt2 = str_replace(chr(0).' ', ') '.($spacewidth).' (', $txt2);
  3547. } else {
  3548. // get string width
  3549. $width = $this->GetStringWidth($txt);
  3550. $spacewidth = (($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1)) * $this->k;
  3551. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  3552. }
  3553. $width = $w - (2 * $this->cMargin);
  3554. }
  3555. switch ($align) {
  3556. case 'C': {
  3557. $dx = ($w - $width) / 2;
  3558. break;
  3559. }
  3560. case 'R': {
  3561. if ($this->rtl) {
  3562. $dx = $this->cMargin;
  3563. } else {
  3564. $dx = $w - $width - $this->cMargin;
  3565. }
  3566. break;
  3567. }
  3568. case 'L':
  3569. case 'J':
  3570. default: {
  3571. if ($this->rtl) {
  3572. $dx = $w - $width - $this->cMargin;
  3573. } else {
  3574. $dx = $this->cMargin;
  3575. }
  3576. break;
  3577. }
  3578. }
  3579. if ($this->rtl) {
  3580. $xdx = $this->x - $dx - $width;
  3581. } else {
  3582. $xdx = $this->x + $dx;
  3583. }
  3584. $xdk = $xdx * $k;
  3585. // calculate approximate position of the font base line
  3586. //$basefonty = $this->y + (($h + $this->FontAscent - $this->FontDescent)/2);
  3587. $basefonty = $this->y + ($h/2) + ($this->FontSize/3);
  3588. // print text
  3589. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  3590. if ($this->underline) {
  3591. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  3592. }
  3593. if ($this->linethrough) {
  3594. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  3595. }
  3596. if ($this->ColorFlag) {
  3597. $s .= ' Q';
  3598. }
  3599. if ($link) {
  3600. $this->Link($xdx, $this->y + (($h - $this->FontSize)/2), $width, $this->FontSize, $link, substr_count($txt, chr(32)));
  3601. }
  3602. }
  3603. // output cell
  3604. if ($s) {
  3605. // output cell
  3606. $rs .= $s;
  3607. // reset text stretching
  3608. if ($stretch > 2) {
  3609. //Reset character horizontal spacing
  3610. $rs .= ' BT 0 Tc ET';
  3611. } elseif ($stretch > 0) {
  3612. //Reset character horizontal scaling
  3613. $rs .= ' BT 100 Tz ET';
  3614. }
  3615. }
  3616. // reset word spacing
  3617. if (!(($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($align == 'J')) {
  3618. $rs .= ' BT 0 Tw ET';
  3619. }
  3620. $this->lasth = $h;
  3621. if ($ln > 0) {
  3622. //Go to the beginning of the next line
  3623. $this->y += $h;
  3624. if ($ln == 1) {
  3625. if ($this->rtl) {
  3626. $this->x = $this->w - $this->rMargin;
  3627. } else {
  3628. $this->x = $this->lMargin;
  3629. }
  3630. }
  3631. } else {
  3632. // go left or right by case
  3633. if ($this->rtl) {
  3634. $this->x -= $w;
  3635. } else {
  3636. $this->x += $w;
  3637. }
  3638. }
  3639. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  3640. $rs = $gstyles.$rs;
  3641. return $rs;
  3642. }
  3643. /**
  3644. * This method allows printing text with line breaks.
  3645. * 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 />
  3646. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  3647. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  3648. * @param float $h Cell minimum height. The cell extends automatically if needed.
  3649. * @param string $txt String to print
  3650. * @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>
  3651. * @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>
  3652. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3653. * @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>
  3654. * @param float $x x position in user units
  3655. * @param float $y y position in user units
  3656. * @param boolean $reseth if true reset the last cell height (default true).
  3657. * @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>
  3658. * @param boolean $ishtml set to true if $txt is HTML content (default = false).
  3659. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  3660. * @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.
  3661. * @return int Return the number of cells or 1 for html mode.
  3662. * @access public
  3663. * @since 1.3
  3664. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  3665. */
  3666. 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) {
  3667. if ($this->empty_string($this->lasth) OR $reseth) {
  3668. //set row height
  3669. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  3670. }
  3671. if (!$this->empty_string($y)) {
  3672. $this->SetY($y);
  3673. } else {
  3674. $y = $this->GetY();
  3675. }
  3676. // check for page break
  3677. $this->checkPageBreak($h);
  3678. $y = $this->GetY();
  3679. // get current page number
  3680. $startpage = $this->page;
  3681. if (!$this->empty_string($x)) {
  3682. $this->SetX($x);
  3683. } else {
  3684. $x = $this->GetX();
  3685. }
  3686. if ($this->empty_string($w) OR ($w <= 0)) {
  3687. if ($this->rtl) {
  3688. $w = $this->x - $this->lMargin;
  3689. } else {
  3690. $w = $this->w - $this->rMargin - $this->x;
  3691. }
  3692. }
  3693. // store original margin values
  3694. $lMargin = $this->lMargin;
  3695. $rMargin = $this->rMargin;
  3696. if ($this->rtl) {
  3697. $this->SetRightMargin($this->w - $this->x);
  3698. $this->SetLeftMargin($this->x - $w);
  3699. } else {
  3700. $this->SetLeftMargin($this->x);
  3701. $this->SetRightMargin($this->w - $this->x - $w);
  3702. }
  3703. $starty = $this->y;
  3704. if ($autopadding) {
  3705. // Adjust internal padding
  3706. if ($this->cMargin < ($this->LineWidth / 2)) {
  3707. $this->cMargin = ($this->LineWidth / 2);
  3708. }
  3709. // Add top space if needed
  3710. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3711. $this->y += $this->LineWidth / 2;
  3712. }
  3713. // add top padding
  3714. $this->y += $this->cMargin;
  3715. }
  3716. if ($ishtml) {
  3717. // ******* Write HTML text
  3718. $this->writeHTML($txt, true, 0, $reseth, true, $align);
  3719. $nl = 1;
  3720. } else {
  3721. // ******* Write text
  3722. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, false, $maxh);
  3723. }
  3724. if ($autopadding) {
  3725. // add bottom padding
  3726. $this->y += $this->cMargin;
  3727. // Add bottom space if needed
  3728. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3729. $this->y += $this->LineWidth / 2;
  3730. }
  3731. }
  3732. // Get end-of-text Y position
  3733. $currentY = $this->y;
  3734. // get latest page number
  3735. $endpage = $this->page;
  3736. // check if a new page has been created
  3737. if ($endpage > $startpage) {
  3738. // design borders around HTML cells.
  3739. for ($page=$startpage; $page <= $endpage; ++$page) {
  3740. $this->setPage($page);
  3741. if ($page == $startpage) {
  3742. $this->y = $starty; // put cursor at the beginning of cell on the first page
  3743. $h = $this->getPageHeight() - $starty - $this->getBreakMargin();
  3744. $cborder = $this->getBorderMode($border, $position='start');
  3745. } elseif ($page == $endpage) {
  3746. $this->y = $this->tMargin; // put cursor at the beginning of last page
  3747. $h = $currentY - $this->tMargin;
  3748. $cborder = $this->getBorderMode($border, $position='end');
  3749. } else {
  3750. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  3751. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  3752. $cborder = $this->getBorderMode($border, $position='middle');
  3753. }
  3754. $nx = $x;
  3755. // account for margin changes
  3756. if ($page > $startpage) {
  3757. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  3758. $nx = $x + ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  3759. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  3760. $nx = $x + ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  3761. }
  3762. }
  3763. $this->SetX($nx);
  3764. $ccode = $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, false);
  3765. if ($cborder OR $fill) {
  3766. $pagebuff = $this->getPageBuffer($this->page);
  3767. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  3768. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  3769. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3770. $this->intmrk[$this->page] += strlen($ccode."\n");
  3771. }
  3772. }
  3773. } else {
  3774. $h = max($h, ($currentY - $y));
  3775. // put cursor at the beginning of text
  3776. $this->SetY($y);
  3777. $this->SetX($x);
  3778. // design a cell around the text
  3779. $ccode = $this->getCellCode($w, $h, '', $border, 1, '', $fill, '', 0, true);
  3780. if ($border OR $fill) {
  3781. if (end($this->transfmrk[$this->page]) !== false) {
  3782. $pagemarkkey = key($this->transfmrk[$this->page]);
  3783. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  3784. } elseif ($this->InFooter) {
  3785. $pagemark = &$this->footerpos[$this->page];
  3786. } else {
  3787. $pagemark = &$this->intmrk[$this->page];
  3788. }
  3789. $pagebuff = $this->getPageBuffer($this->page);
  3790. $pstart = substr($pagebuff, 0, $pagemark);
  3791. $pend = substr($pagebuff, $pagemark);
  3792. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3793. $pagemark += strlen($ccode."\n");
  3794. }
  3795. }
  3796. // Get end-of-cell Y position
  3797. $currentY = $this->GetY();
  3798. // restore original margin values
  3799. $this->SetLeftMargin($lMargin);
  3800. $this->SetRightMargin($rMargin);
  3801. if ($ln > 0) {
  3802. //Go to the beginning of the next line
  3803. $this->SetY($currentY);
  3804. if ($ln == 2) {
  3805. $this->SetX($x + $w);
  3806. }
  3807. } else {
  3808. // go left or right by case
  3809. $this->setPage($startpage);
  3810. $this->y = $y;
  3811. $this->SetX($x + $w);
  3812. }
  3813. $this->setContentMark();
  3814. return $nl;
  3815. }
  3816. /**
  3817. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  3818. * @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>
  3819. * @param string multicell position: 'start', 'middle', 'end'
  3820. * @return border mode
  3821. * @access protected
  3822. * @since 4.4.002 (2008-12-09)
  3823. */
  3824. protected function getBorderMode($border, $position='start') {
  3825. if ((!$this->opencell) AND ($border == 1)) {
  3826. return 1;
  3827. }
  3828. $cborder = '';
  3829. switch ($position) {
  3830. case 'start': {
  3831. if ($border == 1) {
  3832. $cborder = 'LTR';
  3833. } else {
  3834. if (!(false === strpos($border, 'L'))) {
  3835. $cborder .= 'L';
  3836. }
  3837. if (!(false === strpos($border, 'T'))) {
  3838. $cborder .= 'T';
  3839. }
  3840. if (!(false === strpos($border, 'R'))) {
  3841. $cborder .= 'R';
  3842. }
  3843. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3844. $cborder .= 'B';
  3845. }
  3846. }
  3847. break;
  3848. }
  3849. case 'middle': {
  3850. if ($border == 1) {
  3851. $cborder = 'LR';
  3852. } else {
  3853. if (!(false === strpos($border, 'L'))) {
  3854. $cborder .= 'L';
  3855. }
  3856. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3857. $cborder .= 'T';
  3858. }
  3859. if (!(false === strpos($border, 'R'))) {
  3860. $cborder .= 'R';
  3861. }
  3862. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3863. $cborder .= 'B';
  3864. }
  3865. }
  3866. break;
  3867. }
  3868. case 'end': {
  3869. if ($border == 1) {
  3870. $cborder = 'LRB';
  3871. } else {
  3872. if (!(false === strpos($border, 'L'))) {
  3873. $cborder .= 'L';
  3874. }
  3875. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3876. $cborder .= 'T';
  3877. }
  3878. if (!(false === strpos($border, 'R'))) {
  3879. $cborder .= 'R';
  3880. }
  3881. if (!(false === strpos($border, 'B'))) {
  3882. $cborder .= 'B';
  3883. }
  3884. }
  3885. break;
  3886. }
  3887. default: {
  3888. $cborder = $border;
  3889. break;
  3890. }
  3891. }
  3892. return $cborder;
  3893. }
  3894. /**
  3895. * This method returns the estimated number of lines required to print the text.
  3896. * @param string $txt text to print
  3897. * @param float $w width of cell. If 0, they extend up to the right margin of the page.
  3898. * @return int Return the estimated number of lines.
  3899. * @access public
  3900. * @since 4.5.011
  3901. */
  3902. public function getNumLines($txt, $w=0) {
  3903. $lines = 0;
  3904. if ($this->empty_string($w) OR ($w <= 0)) {
  3905. if ($this->rtl) {
  3906. $w = $this->x - $this->lMargin;
  3907. } else {
  3908. $w = $this->w - $this->rMargin - $this->x;
  3909. }
  3910. }
  3911. // max column width
  3912. $wmax = $w - (2 * $this->cMargin);
  3913. // remove carriage returns
  3914. $txt = str_replace("\r", '', $txt);
  3915. // remove last newline (if any)
  3916. if (substr($txt,-1) == "\n") {
  3917. $txt = substr($txt, 0, -1);
  3918. }
  3919. // divide text in blocks
  3920. $txtblocks = explode("\n", $txt);
  3921. // for each block;
  3922. foreach ($txtblocks as $block) {
  3923. // estimate the number of lines
  3924. $lines += $this->empty_string($block) ? 1 : (ceil($this->GetStringWidth($block) / $wmax));
  3925. }
  3926. return $lines;
  3927. }
  3928. /**
  3929. * This method prints text from the current position.<br />
  3930. * @param float $h Line height
  3931. * @param string $txt String to print
  3932. * @param mixed $link URL or identifier returned by AddLink()
  3933. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  3934. * @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>
  3935. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  3936. * @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>
  3937. * @param boolean $firstline if true prints only the first line and return the remaining string.
  3938. * @param boolean $firstblock if true the string is the starting of a line.
  3939. * @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.
  3940. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  3941. * @access public
  3942. * @since 1.5
  3943. */
  3944. public function Write($h, $txt, $link='', $fill=0, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0) {
  3945. if (strlen($txt) == 0) {
  3946. $txt = ' ';
  3947. }
  3948. // remove carriage returns
  3949. $s = str_replace("\r", '', $txt);
  3950. // check if string contains arabic text
  3951. if (preg_match(K_RE_PATTERN_ARABIC, $s)) {
  3952. $arabic = true;
  3953. } else {
  3954. $arabic = false;
  3955. }
  3956. // check if string contains RTL text
  3957. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(K_RE_PATTERN_RTL, $txt)) {
  3958. $rtlmode = true;
  3959. } else {
  3960. $rtlmode = false;
  3961. }
  3962. // get a char width
  3963. $chrwidth = $this->GetCharWidth('.');
  3964. // get array of unicode values
  3965. $chars = $this->UTF8StringToArray($s);
  3966. // get array of chars
  3967. $uchars = $this->UTF8ArrayToUniArray($chars);
  3968. // get the number of characters
  3969. $nb = count($chars);
  3970. // replacement for SHY character (minus symbol)
  3971. $shy_replacement = 45;
  3972. $shy_replacement_char = $this->unichr($shy_replacement);
  3973. // widht for SHY replacement
  3974. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  3975. // store current position
  3976. $prevx = $this->x;
  3977. $prevy = $this->y;
  3978. // max Y
  3979. $maxy = $this->y + $maxh - $h - (2 * $this->cMargin);
  3980. // calculate remaining line width ($w)
  3981. if ($this->rtl) {
  3982. $w = $this->x - $this->lMargin;
  3983. } else {
  3984. $w = $this->w - $this->rMargin - $this->x;
  3985. }
  3986. // max column width
  3987. $wmax = $w - (2 * $this->cMargin);
  3988. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  3989. // a single character do not fit on column
  3990. return '';
  3991. }
  3992. $i = 0; // character position
  3993. $j = 0; // current starting position
  3994. $sep = -1; // position of the last blank space
  3995. $shy = false; // true if the last blank is a soft hypen (SHY)
  3996. $l = 0; // current string lenght
  3997. $nl = 0; //number of lines
  3998. $linebreak = false;
  3999. $pc = 0; // previous character
  4000. // for each character
  4001. while ($i < $nb) {
  4002. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  4003. $firstline = true;
  4004. }
  4005. //Get the current character
  4006. $c = $chars[$i];
  4007. if ($c == 10) { // 10 = "\n" = new line
  4008. //Explicit line break
  4009. if ($align == 'J') {
  4010. if ($this->rtl) {
  4011. $talign = 'R';
  4012. } else {
  4013. $talign = 'L';
  4014. }
  4015. } else {
  4016. $talign = $align;
  4017. }
  4018. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  4019. if ($firstline) {
  4020. $startx = $this->x;
  4021. $tmparr = array_slice($chars, $j, $i);
  4022. if ($rtlmode) {
  4023. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4024. }
  4025. $linew = $this->GetArrStringWidth($tmparr);
  4026. unset($tmparr);
  4027. if ($this->rtl) {
  4028. $this->endlinex = $startx - $linew;
  4029. } else {
  4030. $this->endlinex = $startx + $linew;
  4031. }
  4032. $w = $linew;
  4033. $tmpcmargin = $this->cMargin;
  4034. if ($maxh == 0) {
  4035. $this->cMargin = 0;
  4036. }
  4037. }
  4038. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  4039. unset($tmpstr);
  4040. if ($firstline) {
  4041. $this->cMargin = $tmpcmargin;
  4042. return ($this->UniArrSubString($uchars, $i));
  4043. }
  4044. ++$nl;
  4045. $j = $i + 1;
  4046. $l = 0;
  4047. $sep = -1;
  4048. $shy = false;
  4049. // account for margin changes
  4050. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  4051. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  4052. $this->AcceptPageBreak();
  4053. }
  4054. $w = $this->getRemainingWidth();
  4055. $wmax = $w - (2 * $this->cMargin);
  4056. } else {
  4057. // 160 is the non-breaking space.
  4058. // 173 is SHY (Soft Hypen).
  4059. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  4060. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  4061. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  4062. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  4063. // update last blank space position
  4064. $sep = $i;
  4065. // check if is a SHY
  4066. if ($c == 173) {
  4067. $shy = true;
  4068. if ($pc == 45) {
  4069. $tmp_shy_replacement_width = 0;
  4070. $tmp_shy_replacement_char = '';
  4071. } else {
  4072. $tmp_shy_replacement_width = $shy_replacement_width;
  4073. $tmp_shy_replacement_char = $shy_replacement_char;
  4074. }
  4075. } else {
  4076. $shy = false;
  4077. }
  4078. }
  4079. // update string length
  4080. if ((($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($arabic)) {
  4081. // with bidirectional algorithm some chars may be changed affecting the line length
  4082. // *** very slow ***
  4083. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, $i-$j+1), '', $this->tmprtl));
  4084. } else {
  4085. $l += $this->GetCharWidth($c);
  4086. }
  4087. if (($l > $wmax) OR ($shy AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  4088. // we have reached the end of column
  4089. if ($sep == -1) {
  4090. // check if the line was already started
  4091. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  4092. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  4093. // print a void cell and go to next line
  4094. $this->Cell($w, $h, '', 0, 1);
  4095. $linebreak = true;
  4096. if ($firstline) {
  4097. return ($this->UniArrSubString($uchars, $j));
  4098. }
  4099. } else {
  4100. // truncate the word because do not fit on column
  4101. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  4102. if ($firstline) {
  4103. $startx = $this->x;
  4104. $tmparr = array_slice($chars, $j, $i);
  4105. if ($rtlmode) {
  4106. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4107. }
  4108. $linew = $this->GetArrStringWidth($tmparr);
  4109. unset($tmparr);
  4110. if ($this->rtl) {
  4111. $this->endlinex = $startx - $linew;
  4112. } else {
  4113. $this->endlinex = $startx + $linew;
  4114. }
  4115. $w = $linew;
  4116. $tmpcmargin = $this->cMargin;
  4117. if ($maxh == 0) {
  4118. $this->cMargin = 0;
  4119. }
  4120. }
  4121. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  4122. unset($tmpstr);
  4123. if ($firstline) {
  4124. $this->cMargin = $tmpcmargin;
  4125. return ($this->UniArrSubString($uchars, $i));
  4126. }
  4127. $j = $i;
  4128. --$i;
  4129. }
  4130. } else {
  4131. // word wrapping
  4132. if ($this->rtl AND (!$firstblock)) {
  4133. $endspace = 1;
  4134. } else {
  4135. $endspace = 0;
  4136. }
  4137. if ($shy) {
  4138. // add hypen (minus symbol) at the end of the line
  4139. $shy_width = $tmp_shy_replacement_width;
  4140. if ($this->rtl) {
  4141. $shy_char_left = $tmp_shy_replacement_char;
  4142. $shy_char_right = '';
  4143. } else {
  4144. $shy_char_left = '';
  4145. $shy_char_right = $tmp_shy_replacement_char;
  4146. }
  4147. } else {
  4148. $shy_width = 0;
  4149. $shy_char_left = '';
  4150. $shy_char_right = '';
  4151. }
  4152. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  4153. if ($firstline) {
  4154. $startx = $this->x;
  4155. $tmparr = array_slice($chars, $j, ($sep + $endspace));
  4156. if ($rtlmode) {
  4157. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4158. }
  4159. $linew = $this->GetArrStringWidth($tmparr);
  4160. unset($tmparr);
  4161. if ($this->rtl) {
  4162. $this->endlinex = $startx - $linew - $shy_width;
  4163. } else {
  4164. $this->endlinex = $startx + $linew + $shy_width;
  4165. }
  4166. $w = $linew;
  4167. $tmpcmargin = $this->cMargin;
  4168. if ($maxh == 0) {
  4169. $this->cMargin = 0;
  4170. }
  4171. }
  4172. // print the line
  4173. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  4174. unset($tmpstr);
  4175. if ($firstline) {
  4176. // return the remaining text
  4177. $this->cMargin = $tmpcmargin;
  4178. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  4179. }
  4180. $i = $sep;
  4181. $sep = -1;
  4182. $shy = false;
  4183. $j = ($i+1);
  4184. }
  4185. // account for margin changes
  4186. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  4187. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  4188. $this->AcceptPageBreak();
  4189. }
  4190. $w = $this->getRemainingWidth();
  4191. $wmax = $w - (2 * $this->cMargin);
  4192. if ($linebreak) {
  4193. $linebreak = false;
  4194. } else {
  4195. ++$nl;
  4196. $l = 0;
  4197. }
  4198. }
  4199. }
  4200. // save last character
  4201. $pc = $c;
  4202. ++$i;
  4203. } // end while i < nb
  4204. // print last substring (if any)
  4205. if ($l > 0) {
  4206. switch ($align) {
  4207. case 'J':
  4208. case 'C': {
  4209. $w = $w;
  4210. break;
  4211. }
  4212. case 'L': {
  4213. if ($this->rtl) {
  4214. $w = $w;
  4215. } else {
  4216. $w = $l;
  4217. }
  4218. break;
  4219. }
  4220. case 'R': {
  4221. if ($this->rtl) {
  4222. $w = $l;
  4223. } else {
  4224. $w = $w;
  4225. }
  4226. break;
  4227. }
  4228. default: {
  4229. $w = $l;
  4230. break;
  4231. }
  4232. }
  4233. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  4234. if ($firstline) {
  4235. $startx = $this->x;
  4236. $tmparr = array_slice($chars, $j, $nb);
  4237. if ($rtlmode) {
  4238. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4239. }
  4240. $linew = $this->GetArrStringWidth($tmparr);
  4241. unset($tmparr);
  4242. if ($this->rtl) {
  4243. $this->endlinex = $startx - $linew;
  4244. } else {
  4245. $this->endlinex = $startx + $linew;
  4246. }
  4247. $w = $linew;
  4248. $tmpcmargin = $this->cMargin;
  4249. if ($maxh == 0) {
  4250. $this->cMargin = 0;
  4251. }
  4252. }
  4253. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  4254. unset($tmpstr);
  4255. if ($firstline) {
  4256. $this->cMargin = $tmpcmargin;
  4257. return ($this->UniArrSubString($uchars, $nb));
  4258. }
  4259. ++$nl;
  4260. }
  4261. if ($firstline) {
  4262. return '';
  4263. }
  4264. return $nl;
  4265. }
  4266. /**
  4267. * Returns the remaining width between the current position and margins.
  4268. * @return int Return the remaining width
  4269. * @access protected
  4270. */
  4271. protected function getRemainingWidth() {
  4272. if ($this->rtl) {
  4273. return ($this->x - $this->lMargin);
  4274. } else {
  4275. return ($this->w - $this->rMargin - $this->x);
  4276. }
  4277. }
  4278. /**
  4279. * Extract a slice of the $strarr array and return it as string.
  4280. * @param string $strarr The input array of characters.
  4281. * @param int $start the starting element of $strarr.
  4282. * @param int $end first element that will not be returned.
  4283. * @return Return part of a string
  4284. * @access public
  4285. */
  4286. public function UTF8ArrSubString($strarr, $start='', $end='') {
  4287. if (strlen($start) == 0) {
  4288. $start = 0;
  4289. }
  4290. if (strlen($end) == 0) {
  4291. $end = count($strarr);
  4292. }
  4293. $string = '';
  4294. for ($i=$start; $i < $end; ++$i) {
  4295. $string .= $this->unichr($strarr[$i]);
  4296. }
  4297. return $string;
  4298. }
  4299. /**
  4300. * Extract a slice of the $uniarr array and return it as string.
  4301. * @param string $uniarr The input array of characters.
  4302. * @param int $start the starting element of $strarr.
  4303. * @param int $end first element that will not be returned.
  4304. * @return Return part of a string
  4305. * @access public
  4306. * @since 4.5.037 (2009-04-07)
  4307. */
  4308. public function UniArrSubString($uniarr, $start='', $end='') {
  4309. if (strlen($start) == 0) {
  4310. $start = 0;
  4311. }
  4312. if (strlen($end) == 0) {
  4313. $end = count($uniarr);
  4314. }
  4315. $string = '';
  4316. for ($i=$start; $i < $end; ++$i) {
  4317. $string .= $uniarr[$i];
  4318. }
  4319. return $string;
  4320. }
  4321. /**
  4322. * Convert an array of UTF8 values to array of unicode characters
  4323. * @param string $ta The input array of UTF8 values.
  4324. * @return Return array of unicode characters
  4325. * @access public
  4326. * @since 4.5.037 (2009-04-07)
  4327. */
  4328. public function UTF8ArrayToUniArray($ta) {
  4329. return array_map(array($this, 'unichr'), $ta);
  4330. }
  4331. /**
  4332. * Returns the unicode caracter specified by UTF-8 code
  4333. * @param int $c UTF-8 code
  4334. * @return Returns the specified character.
  4335. * @author Miguel Perez, Nicola Asuni
  4336. * @access public
  4337. * @since 2.3.000 (2008-03-05)
  4338. */
  4339. public function unichr($c) {
  4340. if (!$this->isunicode) {
  4341. return chr($c);
  4342. } elseif ($c <= 0x7F) {
  4343. // one byte
  4344. return chr($c);
  4345. } elseif ($c <= 0x7FF) {
  4346. // two bytes
  4347. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  4348. } elseif ($c <= 0xFFFF) {
  4349. // three bytes
  4350. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4351. } elseif ($c <= 0x10FFFF) {
  4352. // four bytes
  4353. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4354. } else {
  4355. return '';
  4356. }
  4357. }
  4358. /**
  4359. * Return the image type given the file name and path
  4360. * @param string $imgfile image file name
  4361. * @return string image type
  4362. * @since 4.8.017 (2009-11-27)
  4363. */
  4364. public function getImageFileType($imgfile) {
  4365. $type = ''; // default type
  4366. $fileinfo = pathinfo($imgfile);
  4367. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  4368. $type = strtolower($fileinfo['extension']);
  4369. }
  4370. if ($type == 'jpg') {
  4371. $type = 'jpeg';
  4372. }
  4373. return $type;
  4374. }
  4375. /**
  4376. * Puts an image in the page.
  4377. * The upper-left corner must be given.
  4378. * The dimensions can be specified in different ways:<ul>
  4379. * <li>explicit width and height (expressed in user unit)</li>
  4380. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  4381. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  4382. * 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;
  4383. * The format can be specified explicitly or inferred from the file extension.<br />
  4384. * It is possible to put a link on the image.<br />
  4385. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  4386. * @param string $file Name of the file containing the image.
  4387. * @param float $x Abscissa of the upper-left corner.
  4388. * @param float $y Ordinate of the upper-left corner.
  4389. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4390. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4391. * @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.
  4392. * @param mixed $link URL or identifier returned by AddLink().
  4393. * @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>
  4394. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4395. * @param int $dpi dot-per-inch resolution used on resize
  4396. * @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>
  4397. * @param boolean $ismask true if this image is a mask, false otherwise
  4398. * @param mixed $imgmask image object returned by this function or false
  4399. * @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>
  4400. * @param boolean $fitbox If true scale image dimensions proportionally to fit within the ($w, $h) box.
  4401. * @param boolean $hidden if true do not display the image.
  4402. * @return image information
  4403. * @access public
  4404. * @since 1.1
  4405. */
  4406. public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false) {
  4407. if ($x === '') {
  4408. $x = $this->x;
  4409. }
  4410. if ($y === '') {
  4411. $y = $this->y;
  4412. }
  4413. // get image dimensions
  4414. $imsize = @getimagesize($file);
  4415. if ($imsize === FALSE) {
  4416. // encode spaces on filename
  4417. $file = str_replace(' ', '%20', $file);
  4418. $imsize = @getimagesize($file);
  4419. if ($imsize === FALSE) {
  4420. $this->Error('[Image] No such file or directory in '.$file);
  4421. }
  4422. }
  4423. // get original image width and height in pixels
  4424. list($pixw, $pixh) = $imsize;
  4425. // calculate image width and height on document
  4426. if (($w <= 0) AND ($h <= 0)) {
  4427. // convert image size to document unit
  4428. $w = $this->pixelsToUnits($pixw);
  4429. $h = $this->pixelsToUnits($pixh);
  4430. } elseif ($w <= 0) {
  4431. $w = $h * $pixw / $pixh;
  4432. } elseif ($h <= 0) {
  4433. $h = $w * $pixh / $pixw;
  4434. } elseif ($fitbox AND ($w > 0) AND ($h > 0)) {
  4435. // scale image dimensions proportionally to fit within the ($w, $h) box
  4436. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  4437. $h = $w * $pixh / $pixw;
  4438. } else {
  4439. $w = $h * $pixw / $pixh;
  4440. }
  4441. }
  4442. // calculate new minimum dimensions in pixels
  4443. $neww = round($w * $this->k * $dpi / $this->dpi);
  4444. $newh = round($h * $this->k * $dpi / $this->dpi);
  4445. // check if resize is necessary (resize is used only to reduce the image)
  4446. if (($neww * $newh) >= ($pixw * $pixh)) {
  4447. $resize = false;
  4448. }
  4449. // check if image has been already added on document
  4450. if (!in_array($file, $this->imagekeys)) {
  4451. //First use of image, get info
  4452. if ($type == '') {
  4453. $type = $this->getImageFileType($file);
  4454. }
  4455. $mqr = $this->get_mqr();
  4456. $this->set_mqr(false);
  4457. // Specific image handlers
  4458. $mtd = '_parse'.$type;
  4459. // GD image handler function
  4460. $gdfunction = 'imagecreatefrom'.$type;
  4461. $info = false;
  4462. if ((method_exists($this, $mtd)) AND (!($resize AND function_exists($gdfunction)))) {
  4463. // TCPDF image functions
  4464. $info = $this->$mtd($file);
  4465. if ($info == 'pngalpha') {
  4466. return $this->ImagePngAlpha($file, $x, $y, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign);
  4467. }
  4468. }
  4469. if (!$info) {
  4470. if (function_exists($gdfunction)) {
  4471. // GD library
  4472. $img = $gdfunction($file);
  4473. if ($resize) {
  4474. $imgr = imagecreatetruecolor($neww, $newh);
  4475. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  4476. $info = $this->_toJPEG($imgr);
  4477. } else {
  4478. $info = $this->_toJPEG($img);
  4479. }
  4480. } elseif (extension_loaded('imagick')) {
  4481. // ImageMagick library
  4482. $img = new Imagick();
  4483. $img->readImage($file);
  4484. if ($resize) {
  4485. $img->resizeImage($neww, $newh, 10, 1, false);
  4486. }
  4487. $img->setCompressionQuality($this->jpeg_quality);
  4488. $img->setImageFormat('jpeg');
  4489. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4490. $img->writeImage($tempname);
  4491. $info = $this->_parsejpeg($tempname);
  4492. unlink($tempname);
  4493. $img->destroy();
  4494. } else {
  4495. return;
  4496. }
  4497. }
  4498. if ($info === false) {
  4499. //If false, we cannot process image
  4500. return;
  4501. }
  4502. $this->set_mqr($mqr);
  4503. if ($ismask) {
  4504. // force grayscale
  4505. $info['cs'] = 'DeviceGray';
  4506. }
  4507. $info['i'] = $this->numimages + 1;
  4508. if ($imgmask !== false) {
  4509. $info['masked'] = $imgmask;
  4510. }
  4511. // add image to document
  4512. $this->setImageBuffer($file, $info);
  4513. } else {
  4514. $info = $this->getImageBuffer($file);
  4515. }
  4516. // Check whether we need a new page first as this does not fit
  4517. $prev_x = $this->x;
  4518. if ($this->checkPageBreak($h, $y)) {
  4519. $y = $this->GetY() + $this->cMargin;
  4520. if ($this->rtl) {
  4521. $x += ($prev_x - $this->x);
  4522. } else {
  4523. $x += ($this->x - $prev_x);
  4524. }
  4525. }
  4526. // set bottomcoordinates
  4527. $this->img_rb_y = $y + $h;
  4528. // set alignment
  4529. if ($this->rtl) {
  4530. if ($palign == 'L') {
  4531. $ximg = $this->lMargin;
  4532. // set right side coordinate
  4533. $this->img_rb_x = $ximg + $w;
  4534. } elseif ($palign == 'C') {
  4535. $ximg = ($this->w - $x - $w) / 2;
  4536. // set right side coordinate
  4537. $this->img_rb_x = $ximg + $w;
  4538. } else {
  4539. $ximg = $this->w - $x - $w;
  4540. // set left side coordinate
  4541. $this->img_rb_x = $ximg;
  4542. }
  4543. } else {
  4544. if ($palign == 'R') {
  4545. $ximg = $this->w - $this->rMargin - $w;
  4546. // set left side coordinate
  4547. $this->img_rb_x = $ximg;
  4548. } elseif ($palign == 'C') {
  4549. $ximg = ($this->w - $x - $w) / 2;
  4550. // set right side coordinate
  4551. $this->img_rb_x = $ximg + $w;
  4552. } else {
  4553. $ximg = $x;
  4554. // set right side coordinate
  4555. $this->img_rb_x = $ximg + $w;
  4556. }
  4557. }
  4558. if ($ismask OR $hidden) {
  4559. // image is not displayed
  4560. return $info['i'];
  4561. }
  4562. $xkimg = $ximg * $this->k;
  4563. $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']));
  4564. if (!empty($border)) {
  4565. $bx = $x;
  4566. $by = $y;
  4567. $this->x = $ximg;
  4568. $this->y = $y;
  4569. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  4570. $this->x = $bx;
  4571. $this->y = $by;
  4572. }
  4573. if ($link) {
  4574. $this->Link($ximg, $y, $w, $h, $link, 0);
  4575. }
  4576. // set pointer to align the successive text/objects
  4577. switch($align) {
  4578. case 'T': {
  4579. $this->y = $y;
  4580. $this->x = $this->img_rb_x;
  4581. break;
  4582. }
  4583. case 'M': {
  4584. $this->y = $y + round($h/2);
  4585. $this->x = $this->img_rb_x;
  4586. break;
  4587. }
  4588. case 'B': {
  4589. $this->y = $this->img_rb_y;
  4590. $this->x = $this->img_rb_x;
  4591. break;
  4592. }
  4593. case 'N': {
  4594. $this->SetY($this->img_rb_y);
  4595. break;
  4596. }
  4597. default:{
  4598. break;
  4599. }
  4600. }
  4601. $this->endlinex = $this->img_rb_x;
  4602. return $info['i'];
  4603. }
  4604. /**
  4605. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  4606. * @param boolean $mqr FALSE for off, TRUE for on.
  4607. * @since 4.6.025 (2009-08-17)
  4608. */
  4609. public function set_mqr($mqr) {
  4610. if(!defined('PHP_VERSION_ID')) {
  4611. $version = PHP_VERSION;
  4612. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  4613. }
  4614. if (PHP_VERSION_ID < 50300) {
  4615. @set_magic_quotes_runtime($mqr);
  4616. }
  4617. }
  4618. /**
  4619. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  4620. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  4621. * @since 4.6.025 (2009-08-17)
  4622. */
  4623. public function get_mqr() {
  4624. if(!defined('PHP_VERSION_ID')) {
  4625. $version = PHP_VERSION;
  4626. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  4627. }
  4628. if (PHP_VERSION_ID < 50300) {
  4629. return @get_magic_quotes_runtime();
  4630. }
  4631. return 0;
  4632. }
  4633. /**
  4634. * Convert the loaded php image to a JPEG and then return a structure for the PDF creator.
  4635. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  4636. * @param string $file Image file name.
  4637. * @param image $image Image object.
  4638. * return image JPEG image object.
  4639. * @access protected
  4640. */
  4641. protected function _toJPEG($image) {
  4642. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4643. imagejpeg($image, $tempname, $this->jpeg_quality);
  4644. imagedestroy($image);
  4645. $retvars = $this->_parsejpeg($tempname);
  4646. // tidy up by removing temporary image
  4647. unlink($tempname);
  4648. return $retvars;
  4649. }
  4650. /**
  4651. * Extract info from a JPEG file without using the GD library.
  4652. * @param string $file image file to parse
  4653. * @return array structure containing the image data
  4654. * @access protected
  4655. */
  4656. protected function _parsejpeg($file) {
  4657. $a = getimagesize($file);
  4658. if (empty($a)) {
  4659. $this->Error('Missing or incorrect image file: '.$file);
  4660. }
  4661. if ($a[2] != 2) {
  4662. $this->Error('Not a JPEG file: '.$file);
  4663. }
  4664. if ((!isset($a['channels'])) OR ($a['channels'] == 3)) {
  4665. $colspace = 'DeviceRGB';
  4666. } elseif ($a['channels'] == 4) {
  4667. $colspace = 'DeviceCMYK';
  4668. } else {
  4669. $colspace = 'DeviceGray';
  4670. }
  4671. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  4672. $data = file_get_contents($file);
  4673. return array('w' => $a[0], 'h' => $a[1], 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  4674. }
  4675. /**
  4676. * Extract info from a PNG file without using the GD library.
  4677. * @param string $file image file to parse
  4678. * @return array structure containing the image data
  4679. * @access protected
  4680. */
  4681. protected function _parsepng($file) {
  4682. $f = fopen($file, 'rb');
  4683. if ($f === false) {
  4684. $this->Error('Can\'t open image file: '.$file);
  4685. }
  4686. //Check signature
  4687. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  4688. $this->Error('Not a PNG file: '.$file);
  4689. }
  4690. //Read header chunk
  4691. fread($f, 4);
  4692. if (fread($f, 4) != 'IHDR') {
  4693. $this->Error('Incorrect PNG file: '.$file);
  4694. }
  4695. $w = $this->_freadint($f);
  4696. $h = $this->_freadint($f);
  4697. $bpc = ord(fread($f, 1));
  4698. if ($bpc > 8) {
  4699. //$this->Error('16-bit depth not supported: '.$file);
  4700. fclose($f);
  4701. return false;
  4702. }
  4703. $ct = ord(fread($f, 1));
  4704. if ($ct == 0) {
  4705. $colspace = 'DeviceGray';
  4706. } elseif ($ct == 2) {
  4707. $colspace = 'DeviceRGB';
  4708. } elseif ($ct == 3) {
  4709. $colspace = 'Indexed';
  4710. } else {
  4711. // alpha channel
  4712. fclose($f);
  4713. return 'pngalpha';
  4714. }
  4715. if (ord(fread($f, 1)) != 0) {
  4716. //$this->Error('Unknown compression method: '.$file);
  4717. fclose($f);
  4718. return false;
  4719. }
  4720. if (ord(fread($f, 1)) != 0) {
  4721. //$this->Error('Unknown filter method: '.$file);
  4722. fclose($f);
  4723. return false;
  4724. }
  4725. if (ord(fread($f, 1)) != 0) {
  4726. //$this->Error('Interlacing not supported: '.$file);
  4727. fclose($f);
  4728. return false;
  4729. }
  4730. fread($f, 4);
  4731. $parms = '/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  4732. //Scan chunks looking for palette, transparency and image data
  4733. $pal = '';
  4734. $trns = '';
  4735. $data = '';
  4736. do {
  4737. $n = $this->_freadint($f);
  4738. $type = fread($f, 4);
  4739. if ($type == 'PLTE') {
  4740. //Read palette
  4741. $pal = $this->rfread($f, $n);
  4742. fread($f, 4);
  4743. } elseif ($type == 'tRNS') {
  4744. //Read transparency info
  4745. $t = $this->rfread($f, $n);
  4746. if ($ct == 0) {
  4747. $trns = array(ord(substr($t, 1, 1)));
  4748. } elseif ($ct == 2) {
  4749. $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  4750. } else {
  4751. $pos = strpos($t, chr(0));
  4752. if ($pos !== false) {
  4753. $trns = array($pos);
  4754. }
  4755. }
  4756. fread($f, 4);
  4757. } elseif ($type == 'IDAT') {
  4758. //Read image data block
  4759. $data .= $this->rfread($f, $n);
  4760. fread($f, 4);
  4761. } elseif ($type == 'IEND') {
  4762. break;
  4763. } else {
  4764. $this->rfread($f, $n + 4);
  4765. }
  4766. } while ($n);
  4767. if (($colspace == 'Indexed') AND (empty($pal))) {
  4768. //$this->Error('Missing palette in '.$file);
  4769. fclose($f);
  4770. return false;
  4771. }
  4772. fclose($f);
  4773. return array('w' => $w, 'h' => $h, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  4774. }
  4775. /**
  4776. * Binary-safe and URL-safe file read.
  4777. * 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.
  4778. * @param resource $handle
  4779. * @param int $length
  4780. * @return Returns the read string or FALSE in case of error.
  4781. * @author Nicola Asuni
  4782. * @access protected
  4783. * @since 4.5.027 (2009-03-16)
  4784. */
  4785. protected function rfread($handle, $length) {
  4786. $data = fread($handle, $length);
  4787. if ($data === false) {
  4788. return false;
  4789. }
  4790. $rest = $length - strlen($data);
  4791. if ($rest > 0) {
  4792. $data .= $this->rfread($handle, $rest);
  4793. }
  4794. return $data;
  4795. }
  4796. /**
  4797. * Extract info from a PNG image with alpha channel using the GD library.
  4798. * @param string $file Name of the file containing the image.
  4799. * @param float $x Abscissa of the upper-left corner.
  4800. * @param float $y Ordinate of the upper-left corner.
  4801. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4802. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4803. * @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.
  4804. * @param mixed $link URL or identifier returned by AddLink().
  4805. * @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>
  4806. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4807. * @param int $dpi dot-per-inch resolution used on resize
  4808. * @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>
  4809. * @author Valentin Schmidt, Nicola Asuni
  4810. * @access protected
  4811. * @since 4.3.007 (2008-12-04)
  4812. * @see Image()
  4813. */
  4814. protected function ImagePngAlpha($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='') {
  4815. // get image size
  4816. list($wpx, $hpx) = getimagesize($file);
  4817. // generate images
  4818. $img = imagecreatefrompng($file);
  4819. $imgalpha = imagecreate($wpx, $hpx);
  4820. // generate gray scale pallete
  4821. for ($c = 0; $c < 256; ++$c) {
  4822. ImageColorAllocate($imgalpha, $c, $c, $c);
  4823. }
  4824. // extract alpha channel
  4825. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  4826. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  4827. $colorindex = imagecolorat($img, $xpx, $ypx);
  4828. $col = imagecolorsforindex($img, $colorindex);
  4829. imagesetpixel($imgalpha, $xpx, $ypx, $this->getGDgamma((127 - $col['alpha']) * 255 / 127));
  4830. }
  4831. }
  4832. // create temp alpha file
  4833. $tempfile_alpha = tempnam(K_PATH_CACHE, 'mska_');
  4834. imagepng($imgalpha, $tempfile_alpha);
  4835. imagedestroy($imgalpha);
  4836. // extract image without alpha channel
  4837. $imgplain = imagecreatetruecolor($wpx, $hpx);
  4838. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  4839. // create temp image file
  4840. $tempfile_plain = tempnam(K_PATH_CACHE, 'mskp_');
  4841. imagepng($imgplain, $tempfile_plain);
  4842. imagedestroy($imgplain);
  4843. // embed mask image
  4844. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  4845. // embed image, masked with previously embedded mask
  4846. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  4847. // remove temp files
  4848. unlink($tempfile_alpha);
  4849. unlink($tempfile_plain);
  4850. }
  4851. /**
  4852. * Correct the gamma value to be used with GD library
  4853. * @param float $v the gamma value to be corrected
  4854. * @access protected
  4855. * @since 4.3.007 (2008-12-04)
  4856. */
  4857. protected function getGDgamma($v) {
  4858. return (pow(($v / 255), 2.2) * 255);
  4859. }
  4860. /**
  4861. * Performs a line break.
  4862. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  4863. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  4864. * @param boolean $cell if true add a cMargin to the x coordinate
  4865. * @access public
  4866. * @since 1.0
  4867. * @see Cell()
  4868. */
  4869. public function Ln($h='', $cell=false) {
  4870. //Line feed; default value is last cell height
  4871. if ($cell) {
  4872. $cellmargin = $this->cMargin;
  4873. } else {
  4874. $cellmargin = 0;
  4875. }
  4876. if ($this->rtl) {
  4877. $this->x = $this->w - $this->rMargin - $cellmargin;
  4878. } else {
  4879. $this->x = $this->lMargin + $cellmargin;
  4880. }
  4881. if (is_string($h)) {
  4882. $this->y += $this->lasth;
  4883. } else {
  4884. $this->y += $h;
  4885. }
  4886. $this->newline = true;
  4887. }
  4888. /**
  4889. * Returns the relative X value of current position.
  4890. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  4891. * @return float
  4892. * @access public
  4893. * @since 1.2
  4894. * @see SetX(), GetY(), SetY()
  4895. */
  4896. public function GetX() {
  4897. //Get x position
  4898. if ($this->rtl) {
  4899. return ($this->w - $this->x);
  4900. } else {
  4901. return $this->x;
  4902. }
  4903. }
  4904. /**
  4905. * Returns the absolute X value of current position.
  4906. * @return float
  4907. * @access public
  4908. * @since 1.2
  4909. * @see SetX(), GetY(), SetY()
  4910. */
  4911. public function GetAbsX() {
  4912. return $this->x;
  4913. }
  4914. /**
  4915. * Returns the ordinate of the current position.
  4916. * @return float
  4917. * @access public
  4918. * @since 1.0
  4919. * @see SetY(), GetX(), SetX()
  4920. */
  4921. public function GetY() {
  4922. //Get y position
  4923. return $this->y;
  4924. }
  4925. /**
  4926. * Defines the abscissa of the current position.
  4927. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  4928. * @param float $x The value of the abscissa.
  4929. * @access public
  4930. * @since 1.2
  4931. * @see GetX(), GetY(), SetY(), SetXY()
  4932. */
  4933. public function SetX($x) {
  4934. //Set x position
  4935. if ($this->rtl) {
  4936. if ($x >= 0) {
  4937. $this->x = $this->w - $x;
  4938. } else {
  4939. $this->x = abs($x);
  4940. }
  4941. } else {
  4942. if ($x >= 0) {
  4943. $this->x = $x;
  4944. } else {
  4945. $this->x = $this->w + $x;
  4946. }
  4947. }
  4948. if ($this->x < 0) {
  4949. $this->x = 0;
  4950. }
  4951. if ($this->x > $this->w) {
  4952. $this->x = $this->w;
  4953. }
  4954. }
  4955. /**
  4956. * Moves the current abscissa back to the left margin and sets the ordinate.
  4957. * If the passed value is negative, it is relative to the bottom of the page.
  4958. * @param float $y The value of the ordinate.
  4959. * @param bool $resetx if true (default) reset the X position.
  4960. * @access public
  4961. * @since 1.0
  4962. * @see GetX(), GetY(), SetY(), SetXY()
  4963. */
  4964. public function SetY($y, $resetx=true) {
  4965. if ($resetx) {
  4966. //reset x
  4967. if ($this->rtl) {
  4968. $this->x = $this->w - $this->rMargin;
  4969. } else {
  4970. $this->x = $this->lMargin;
  4971. }
  4972. }
  4973. if ($y >= 0) {
  4974. $this->y = $y;
  4975. } else {
  4976. $this->y = $this->h + $y;
  4977. }
  4978. if ($this->y < 0) {
  4979. $this->y = 0;
  4980. }
  4981. if ($this->y > $this->h) {
  4982. $this->y = $this->h;
  4983. }
  4984. }
  4985. /**
  4986. * Defines the abscissa and ordinate of the current position.
  4987. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  4988. * @param float $x The value of the abscissa
  4989. * @param float $y The value of the ordinate
  4990. * @access public
  4991. * @since 1.2
  4992. * @see SetX(), SetY()
  4993. */
  4994. public function SetXY($x, $y) {
  4995. //Set x and y positions
  4996. $this->SetY($y);
  4997. $this->SetX($x);
  4998. }
  4999. /**
  5000. * Send the document to a given destination: string, local file or browser.
  5001. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  5002. * The method first calls Close() if necessary to terminate the document.
  5003. * @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.
  5004. * @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>
  5005. * @access public
  5006. * @since 1.0
  5007. * @see Close()
  5008. */
  5009. public function Output($name='doc.pdf', $dest='I') {
  5010. //Output PDF to some destination
  5011. //Finish document if necessary
  5012. $this->lastpage();
  5013. if ($this->state < 3) {
  5014. $this->Close();
  5015. }
  5016. //Normalize parameters
  5017. if (is_bool($dest)) {
  5018. $dest = $dest ? 'D' : 'F';
  5019. }
  5020. $dest = strtoupper($dest);
  5021. if ($dest != 'F') {
  5022. $name = preg_replace('/[\s]+/', '_', $name);
  5023. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  5024. }
  5025. if ($this->sign) {
  5026. // *** apply digital signature to the document ***
  5027. // get the document content
  5028. $pdfdoc = $this->getBuffer();
  5029. // remove last newline
  5030. $pdfdoc = substr($pdfdoc, 0, -1);
  5031. // Remove the original buffer
  5032. if (isset($this->diskcache) AND $this->diskcache) {
  5033. // remove buffer file from cache
  5034. unlink($this->buffer);
  5035. }
  5036. unset($this->buffer);
  5037. // remove filler space
  5038. $byterange_string_len = strlen($this->byterange_string);
  5039. // define the ByteRange
  5040. $byte_range = array();
  5041. $byte_range[0] = 0;
  5042. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  5043. $byte_range[2] = $byte_range[1] + $this->signature_max_lenght + 2;
  5044. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  5045. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  5046. // replace the ByteRange
  5047. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  5048. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  5049. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  5050. // write the document to a temporary folder
  5051. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  5052. $f = fopen($tempdoc, 'wb');
  5053. if (!$f) {
  5054. $this->Error('Unable to create temporary file: '.$tempdoc);
  5055. }
  5056. $pdfdoc_lenght = strlen($pdfdoc);
  5057. fwrite($f, $pdfdoc, $pdfdoc_lenght);
  5058. fclose($f);
  5059. // get digital signature via openssl library
  5060. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  5061. if (empty($this->signature_data['extracerts'])) {
  5062. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  5063. } else {
  5064. 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']);
  5065. }
  5066. unlink($tempdoc);
  5067. // read signature
  5068. $signature = file_get_contents($tempsign, false, null, $pdfdoc_lenght);
  5069. unlink($tempsign);
  5070. // extract signature
  5071. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  5072. $tmparr = explode("\n\n", $signature);
  5073. $signature = $tmparr[1];
  5074. unset($tmparr);
  5075. // decode signature
  5076. $signature = base64_decode(trim($signature));
  5077. // convert signature to hex
  5078. $signature = current(unpack('H*', $signature));
  5079. $signature = str_pad($signature, $this->signature_max_lenght, '0');
  5080. // Add signature to the document
  5081. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, ($byte_range[1]));
  5082. $this->diskcache = false;
  5083. $this->buffer = &$pdfdoc;
  5084. $this->bufferlen = strlen($pdfdoc);
  5085. }
  5086. switch($dest) {
  5087. case 'I': {
  5088. // Send PDF to the standard output
  5089. if (ob_get_contents()) {
  5090. $this->Error('Some data has already been output, can\'t send PDF file');
  5091. }
  5092. if (php_sapi_name() != 'cli') {
  5093. //We send to a browser
  5094. header('Content-Type: application/pdf');
  5095. if (headers_sent()) {
  5096. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  5097. }
  5098. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5099. header('Pragma: public');
  5100. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5101. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5102. header('Content-Length: '.$this->bufferlen);
  5103. header('Content-Disposition: inline; filename="'.basename($name).'";');
  5104. }
  5105. echo $this->getBuffer();
  5106. break;
  5107. }
  5108. case 'D': {
  5109. // Download PDF as file
  5110. if (ob_get_contents()) {
  5111. $this->Error('Some data has already been output, can\'t send PDF file');
  5112. }
  5113. header('Content-Description: File Transfer');
  5114. if (headers_sent()) {
  5115. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  5116. }
  5117. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5118. header('Pragma: public');
  5119. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5120. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5121. // force download dialog
  5122. header('Content-Type: application/force-download');
  5123. header('Content-Type: application/octet-stream', false);
  5124. header('Content-Type: application/download', false);
  5125. header('Content-Type: application/pdf', false);
  5126. // use the Content-Disposition header to supply a recommended filename
  5127. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  5128. header('Content-Transfer-Encoding: binary');
  5129. header('Content-Length: '.$this->bufferlen);
  5130. echo $this->getBuffer();
  5131. break;
  5132. }
  5133. case 'F': {
  5134. // Save PDF to a local file
  5135. if ($this->diskcache) {
  5136. copy($this->buffer, $name);
  5137. } else {
  5138. $f = fopen($name, 'wb');
  5139. if (!$f) {
  5140. $this->Error('Unable to create output file: '.$name);
  5141. }
  5142. fwrite($f, $this->getBuffer(), $this->bufferlen);
  5143. fclose($f);
  5144. }
  5145. break;
  5146. }
  5147. case 'S': {
  5148. // Returns PDF as a string
  5149. return $this->getBuffer();
  5150. }
  5151. default: {
  5152. $this->Error('Incorrect output destination: '.$dest);
  5153. }
  5154. }
  5155. return '';
  5156. }
  5157. /**
  5158. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  5159. * @param boolean $destroyall if true destroys all class variables, otherwise preserves critical variables.
  5160. * @param boolean $preserve_objcopy if true preserves the objcopy variable
  5161. * @access public
  5162. * @since 4.5.016 (2009-02-24)
  5163. */
  5164. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  5165. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  5166. // remove buffer file from cache
  5167. unlink($this->buffer);
  5168. }
  5169. foreach (array_keys(get_object_vars($this)) as $val) {
  5170. if ($destroyall OR (
  5171. ($val != 'internal_encoding')
  5172. AND ($val != 'state')
  5173. AND ($val != 'bufferlen')
  5174. AND ($val != 'buffer')
  5175. AND ($val != 'diskcache')
  5176. AND ($val != 'sign')
  5177. AND ($val != 'signature_data')
  5178. AND ($val != 'signature_max_lenght')
  5179. AND ($val != 'byterange_string')
  5180. )) {
  5181. if (!$preserve_objcopy OR ($val != 'objcopy')) {
  5182. unset($this->$val);
  5183. }
  5184. }
  5185. }
  5186. }
  5187. /**
  5188. * Check for locale-related bug
  5189. * @access protected
  5190. */
  5191. protected function _dochecks() {
  5192. //Check for locale-related bug
  5193. if (1.1 == 1) {
  5194. $this->Error('Don\'t alter the locale before including class file');
  5195. }
  5196. //Check for decimal separator
  5197. if (sprintf('%.1F', 1.0) != '1.0') {
  5198. setlocale(LC_NUMERIC, 'C');
  5199. }
  5200. }
  5201. /**
  5202. * Return fonts path
  5203. * @return string
  5204. * @access protected
  5205. */
  5206. protected function _getfontpath() {
  5207. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  5208. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  5209. }
  5210. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  5211. }
  5212. /**
  5213. * Output pages.
  5214. * @access protected
  5215. */
  5216. protected function _putpages() {
  5217. $nb = $this->numpages;
  5218. if (!empty($this->AliasNbPages)) {
  5219. $nbs = $this->formatPageNumber($nb);
  5220. $nbu = $this->UTF8ToUTF16BE($nbs, false); // replacement for unicode font
  5221. $alias_a = $this->_escape($this->AliasNbPages);
  5222. $alias_au = $this->_escape('{'.$this->AliasNbPages.'}');
  5223. if ($this->isunicode) {
  5224. $alias_b = $this->_escape($this->UTF8ToLatin1($this->AliasNbPages));
  5225. $alias_bu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNbPages.'}'));
  5226. $alias_c = $this->_escape($this->utf8StrRev($this->AliasNbPages, false, $this->tmprtl));
  5227. $alias_cu = $this->_escape($this->utf8StrRev('{'.$this->AliasNbPages.'}', false, $this->tmprtl));
  5228. }
  5229. }
  5230. if (!empty($this->AliasNumPage)) {
  5231. $alias_pa = $this->_escape($this->AliasNumPage);
  5232. $alias_pau = $this->_escape('{'.$this->AliasNumPage.'}');
  5233. if ($this->isunicode) {
  5234. $alias_pb = $this->_escape($this->UTF8ToLatin1($this->AliasNumPage));
  5235. $alias_pbu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNumPage.'}'));
  5236. $alias_pc = $this->_escape($this->utf8StrRev($this->AliasNumPage, false, $this->tmprtl));
  5237. $alias_pcu = $this->_escape($this->utf8StrRev('{'.$this->AliasNumPage.'}', false, $this->tmprtl));
  5238. }
  5239. }
  5240. $pagegroupnum = 0;
  5241. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  5242. for ($n=1; $n <= $nb; ++$n) {
  5243. $temppage = $this->getPageBuffer($n);
  5244. if (!empty($this->pagegroups)) {
  5245. if(isset($this->newpagegroup[$n])) {
  5246. $pagegroupnum = 0;
  5247. }
  5248. ++$pagegroupnum;
  5249. foreach ($this->pagegroups as $k => $v) {
  5250. // replace total pages group numbers
  5251. $vs = $this->formatPageNumber($v);
  5252. $vu = $this->UTF8ToUTF16BE($vs, false);
  5253. $alias_ga = $this->_escape($k);
  5254. $alias_gau = $this->_escape('{'.$k.'}');
  5255. if ($this->isunicode) {
  5256. $alias_gb = $this->_escape($this->UTF8ToLatin1($k));
  5257. $alias_gbu = $this->_escape($this->UTF8ToLatin1('{'.$k.'}'));
  5258. $alias_gc = $this->_escape($this->utf8StrRev($k, false, $this->tmprtl));
  5259. $alias_gcu = $this->_escape($this->utf8StrRev('{'.$k.'}', false, $this->tmprtl));
  5260. }
  5261. $temppage = str_replace($alias_gau, $vu, $temppage);
  5262. if ($this->isunicode) {
  5263. $temppage = str_replace($alias_gbu, $vu, $temppage);
  5264. $temppage = str_replace($alias_gcu, $vu, $temppage);
  5265. $temppage = str_replace($alias_gb, $vs, $temppage);
  5266. $temppage = str_replace($alias_gc, $vs, $temppage);
  5267. }
  5268. $temppage = str_replace($alias_ga, $vs, $temppage);
  5269. // replace page group numbers
  5270. $pvs = $this->formatPageNumber($pagegroupnum);
  5271. $pvu = $this->UTF8ToUTF16BE($pvs, false);
  5272. $pk = str_replace('{nb', '{pnb', $k);
  5273. $alias_pga = $this->_escape($pk);
  5274. $alias_pgau = $this->_escape('{'.$pk.'}');
  5275. if ($this->isunicode) {
  5276. $alias_pgb = $this->_escape($this->UTF8ToLatin1($pk));
  5277. $alias_pgbu = $this->_escape($this->UTF8ToLatin1('{'.$pk.'}'));
  5278. $alias_pgc = $this->_escape($this->utf8StrRev($pk, false, $this->tmprtl));
  5279. $alias_pgcu = $this->_escape($this->utf8StrRev('{'.$pk.'}', false, $this->tmprtl));
  5280. }
  5281. $temppage = str_replace($alias_pgau, $pvu, $temppage);
  5282. if ($this->isunicode) {
  5283. $temppage = str_replace($alias_pgbu, $pvu, $temppage);
  5284. $temppage = str_replace($alias_pgcu, $pvu, $temppage);
  5285. $temppage = str_replace($alias_pgb, $pvs, $temppage);
  5286. $temppage = str_replace($alias_pgc, $pvs, $temppage);
  5287. }
  5288. $temppage = str_replace($alias_pga, $pvs, $temppage);
  5289. }
  5290. }
  5291. if (!empty($this->AliasNbPages)) {
  5292. // replace total pages number
  5293. $temppage = str_replace($alias_au, $nbu, $temppage);
  5294. if ($this->isunicode) {
  5295. $temppage = str_replace($alias_bu, $nbu, $temppage);
  5296. $temppage = str_replace($alias_cu, $nbu, $temppage);
  5297. $temppage = str_replace($alias_b, $nbs, $temppage);
  5298. $temppage = str_replace($alias_c, $nbs, $temppage);
  5299. }
  5300. $temppage = str_replace($alias_a, $nbs, $temppage);
  5301. }
  5302. if (!empty($this->AliasNumPage)) {
  5303. // replace page number
  5304. $pnbs = $this->formatPageNumber($n);
  5305. $pnbu = $this->UTF8ToUTF16BE($pnbs, false); // replacement for unicode font
  5306. $temppage = str_replace($alias_pau, $pnbu, $temppage);
  5307. if ($this->isunicode) {
  5308. $temppage = str_replace($alias_pbu, $pnbu, $temppage);
  5309. $temppage = str_replace($alias_pcu, $pnbu, $temppage);
  5310. $temppage = str_replace($alias_pb, $pnbs, $temppage);
  5311. $temppage = str_replace($alias_pc, $pnbs, $temppage);
  5312. }
  5313. $temppage = str_replace($alias_pa, $pnbs, $temppage);
  5314. }
  5315. $temppage = str_replace($this->epsmarker, '', $temppage);
  5316. //Page
  5317. $this->page_obj_id[$n] = $this->_newobj();
  5318. $this->_out('<</Type /Page');
  5319. $this->_out('/Parent 1 0 R');
  5320. $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->pagedim[$n]['w'], $this->pagedim[$n]['h']));
  5321. $this->_out('/Resources 2 0 R');
  5322. $this->_putannotsrefs($n);
  5323. $this->_out('/Contents '.($this->n + 1).' 0 R>>');
  5324. $this->_out('endobj');
  5325. //Page content
  5326. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  5327. $this->_newobj();
  5328. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  5329. $this->_putstream($p);
  5330. $this->_out('endobj');
  5331. if ($this->diskcache) {
  5332. // remove temporary files
  5333. unlink($this->pages[$n]);
  5334. }
  5335. }
  5336. //Pages root
  5337. $this->offsets[1] = $this->bufferlen;
  5338. $this->_out('1 0 obj');
  5339. $this->_out('<</Type /Pages');
  5340. $this->_out('/Kids [');
  5341. foreach($this->page_obj_id as $page_obj) {
  5342. $this->_out($page_obj.' 0 R');
  5343. }
  5344. $this->_out(']');
  5345. $this->_out('/Count '.$nb);
  5346. $this->_out('>>');
  5347. $this->_out('endobj');
  5348. }
  5349. /**
  5350. * Output referencees to page annotations
  5351. * @param int $n page number
  5352. * @access protected
  5353. * @author Nicola Asuni
  5354. * @since 4.7.000 (2008-08-29)
  5355. */
  5356. protected function _putannotsrefs($n) {
  5357. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  5358. return;
  5359. }
  5360. $this->_out('/Annots [');
  5361. if (isset($this->PageAnnots[$n])) {
  5362. $num_annots = count($this->PageAnnots[$n]);
  5363. for ($i = 0; $i < $num_annots; ++$i) {
  5364. ++$this->curr_annot_obj_id;
  5365. if (!in_array($this->curr_annot_obj_id, $this->radio_groups)) {
  5366. $this->_out($this->curr_annot_obj_id.' 0 R');
  5367. } else {
  5368. ++$num_annots;
  5369. }
  5370. }
  5371. }
  5372. if (($n==1) AND $this->sign AND isset($this->signature_data['cert_type'])) {
  5373. // set reference for signature object
  5374. $this->_out($this->sig_annot_ref);
  5375. }
  5376. $this->_out(']');
  5377. }
  5378. /**
  5379. * Output annotations objects for all pages.
  5380. * !!! THIS FUNCTION IS NOT YET COMPLETED !!!
  5381. * See section 12.5 of PDF 32000_2008 reference.
  5382. * @access protected
  5383. * @author Nicola Asuni
  5384. * @since 4.0.018 (2008-08-06)
  5385. */
  5386. protected function _putannotsobjs() {
  5387. // reset object counter
  5388. $this->annot_obj_id = $this->annots_start_obj_id;
  5389. for ($n=1; $n <= $this->numpages; ++$n) {
  5390. if (isset($this->PageAnnots[$n])) {
  5391. // set page annotations
  5392. foreach ($this->PageAnnots[$n] as $key => $pl) {
  5393. // create annotation object for grouping radiobuttons
  5394. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  5395. $annots = '<<';
  5396. $annots .= ' /Type /Annot';
  5397. $annots .= ' /Subtype /Widget';
  5398. $annots .= ' /T '.$this->_datastring($pl['txt']);
  5399. $annots .= ' /FT /Btn';
  5400. $annots .= ' /Ff 49152';
  5401. $annots .= ' /Kids [';
  5402. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $data) {
  5403. $annots .= ' '.$data['kid'].' 0 R';
  5404. if ($data['def'] !== 'Off') {
  5405. $defval = $data['def'];
  5406. }
  5407. }
  5408. $annots .= ' ]';
  5409. if (isset($defval)) {
  5410. $annots .= ' /V /'.$defval;
  5411. }
  5412. $annots .= ' >>';
  5413. ++$this->annot_obj_id;
  5414. $this->offsets[$this->annot_obj_id] = $this->bufferlen;
  5415. $this->_out($this->annot_obj_id.' 0 obj');
  5416. $this->_out($annots);
  5417. $this->_out('endobj');
  5418. $this->form_obj_id[] = $this->annot_obj_id;
  5419. // store object id to be used on Parent entry of Kids
  5420. $this->radiobutton_groups[$n][$pl['txt']] = $this->annot_obj_id;
  5421. }
  5422. $formfield = false;
  5423. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  5424. $a = $pl['x'] * $this->k;
  5425. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  5426. $c = $pl['w'] * $this->k;
  5427. $d = $pl['h'] * $this->k;
  5428. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  5429. // create new annotation object
  5430. $annots = '<</Type /Annot';
  5431. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  5432. $annots .= ' /Rect ['.$rect.']';
  5433. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  5434. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  5435. $annots .= ' /FT /'.$pl['opt']['ft'];
  5436. $formfield = true;
  5437. }
  5438. $annots .= ' /Contents '.$this->_textstring($pl['txt']);
  5439. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  5440. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key));
  5441. $annots .= ' /M '.$this->_datestring();
  5442. if (isset($pl['opt']['f'])) {
  5443. $val = 0;
  5444. if (is_array($pl['opt']['f'])) {
  5445. foreach ($pl['opt']['f'] as $f) {
  5446. switch (strtolower($f)) {
  5447. case 'invisible': {
  5448. $val += 1 << 0;
  5449. break;
  5450. }
  5451. case 'hidden': {
  5452. $val += 1 << 1;
  5453. break;
  5454. }
  5455. case 'print': {
  5456. $val += 1 << 2;
  5457. break;
  5458. }
  5459. case 'nozoom': {
  5460. $val += 1 << 3;
  5461. break;
  5462. }
  5463. case 'norotate': {
  5464. $val += 1 << 4;
  5465. break;
  5466. }
  5467. case 'noview': {
  5468. $val += 1 << 5;
  5469. break;
  5470. }
  5471. case 'readonly': {
  5472. $val += 1 << 6;
  5473. break;
  5474. }
  5475. case 'locked': {
  5476. $val += 1 << 8;
  5477. break;
  5478. }
  5479. case 'togglenoview': {
  5480. $val += 1 << 9;
  5481. break;
  5482. }
  5483. case 'lockedcontents': {
  5484. $val += 1 << 10;
  5485. break;
  5486. }
  5487. default: {
  5488. break;
  5489. }
  5490. }
  5491. }
  5492. } else {
  5493. $val = intval($pl['opt']['f']);
  5494. }
  5495. $annots .= ' /F '.intval($val);
  5496. }
  5497. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  5498. $annots .= ' /AS /'.$pl['opt']['as'];
  5499. }
  5500. if (isset($pl['opt']['ap'])) {
  5501. // appearance stream
  5502. $annots .= ' /AP <<';
  5503. if (is_array($pl['opt']['ap'])) {
  5504. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  5505. // $apmode can be: n = normal; r = rollover; d = down;
  5506. $annots .= ' /'.strtoupper($apmode);
  5507. if (is_array($apdef)) {
  5508. $annots .= ' <<';
  5509. foreach ($apdef as $apstate => $stream) {
  5510. // reference to XObject that define the appearance for this mode-state
  5511. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  5512. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  5513. }
  5514. $annots .= ' >>';
  5515. } else {
  5516. // reference to XObject that define the appearance for this mode
  5517. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  5518. $annots .= ' '.$apsobjid.' 0 R';
  5519. }
  5520. }
  5521. } else {
  5522. $annots .= $pl['opt']['ap'];
  5523. }
  5524. $annots .= ' >>';
  5525. }
  5526. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  5527. $annots .= ' /BS <<';
  5528. $annots .= ' /Type /Border';
  5529. if (isset($pl['opt']['bs']['w'])) {
  5530. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  5531. }
  5532. $bstyles = array('S', 'D', 'B', 'I', 'U');
  5533. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  5534. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5535. }
  5536. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  5537. $annots .= ' /D [';
  5538. foreach ($pl['opt']['bs']['d'] as $cord) {
  5539. $annots .= ' '.intval($cord);
  5540. }
  5541. $annots .= ']';
  5542. }
  5543. $annots .= ' >>';
  5544. } else {
  5545. $annots .= ' /Border [';
  5546. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  5547. $annots .= intval($pl['opt']['border'][0]).' ';
  5548. $annots .= intval($pl['opt']['border'][1]).' ';
  5549. $annots .= intval($pl['opt']['border'][2]);
  5550. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  5551. $annots .= ' [';
  5552. foreach ($pl['opt']['border'][3] as $dash) {
  5553. $annots .= intval($dash).' ';
  5554. }
  5555. $annots .= ']';
  5556. }
  5557. } else {
  5558. $annots .= '0 0 0';
  5559. }
  5560. $annots .= ']';
  5561. }
  5562. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  5563. $annots .= ' /BE <<';
  5564. $bstyles = array('S', 'C');
  5565. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  5566. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5567. } else {
  5568. $annots .= ' /S /S';
  5569. }
  5570. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  5571. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  5572. }
  5573. $annots .= '>>';
  5574. }
  5575. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  5576. $annots .= ' /C [';
  5577. foreach ($pl['opt']['c'] as $col) {
  5578. $col = intval($col);
  5579. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  5580. $annots .= sprintf(' %.4F', $color);
  5581. }
  5582. $annots .= ']';
  5583. }
  5584. //$annots .= ' /StructParent ';
  5585. //$annots .= ' /OC ';
  5586. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  5587. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  5588. // this is a markup type
  5589. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  5590. $annots .= ' /T '.$this->_textstring($pl['opt']['t']);
  5591. }
  5592. //$annots .= ' /Popup ';
  5593. if (isset($pl['opt']['ca'])) {
  5594. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  5595. }
  5596. if (isset($pl['opt']['rc'])) {
  5597. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5598. }
  5599. $annots .= ' /CreationDate '.$this->_datestring();
  5600. //$annots .= ' /IRT ';
  5601. if (isset($pl['opt']['subj'])) {
  5602. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj']);
  5603. }
  5604. //$annots .= ' /RT ';
  5605. //$annots .= ' /IT ';
  5606. //$annots .= ' /ExData ';
  5607. }
  5608. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  5609. switch (strtolower($pl['opt']['subtype'])) {
  5610. case 'text': {
  5611. if (isset($pl['opt']['open'])) {
  5612. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  5613. }
  5614. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  5615. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5616. $annots .= ' /Name /'.$pl['opt']['name'];
  5617. } else {
  5618. $annots .= ' /Name /Note';
  5619. }
  5620. $statemodels = array('Marked', 'Review');
  5621. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  5622. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5623. } else {
  5624. $pl['opt']['statemodel'] = 'Marked';
  5625. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5626. }
  5627. if ($pl['opt']['statemodel'] == 'Marked') {
  5628. $states = array('Accepted', 'Unmarked');
  5629. } else {
  5630. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  5631. }
  5632. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  5633. $annots .= ' /State /'.$pl['opt']['state'];
  5634. } else {
  5635. if ($pl['opt']['statemodel'] == 'Marked') {
  5636. $annots .= ' /State /Unmarked';
  5637. } else {
  5638. $annots .= ' /State /None';
  5639. }
  5640. }
  5641. break;
  5642. }
  5643. case 'link': {
  5644. if(is_string($pl['txt'])) {
  5645. // external URI link
  5646. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt'])).'>>';
  5647. } else {
  5648. // internal link
  5649. $l = $this->links[$pl['txt']];
  5650. $annots .= sprintf(' /Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $l[0])), ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  5651. }
  5652. $hmodes = array('N', 'I', 'O', 'P');
  5653. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  5654. $annots .= ' /H /'.$pl['opt']['h'];
  5655. } else {
  5656. $annots .= ' /H /I';
  5657. }
  5658. //$annots .= ' /PA ';
  5659. //$annots .= ' /Quadpoints ';
  5660. break;
  5661. }
  5662. case 'freetext': {
  5663. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  5664. $annots .= ' /DA ('.$pl['opt']['da'].')';
  5665. }
  5666. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  5667. $annots .= ' /Q '.intval($pl['opt']['q']);
  5668. }
  5669. if (isset($pl['opt']['rc'])) {
  5670. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5671. }
  5672. if (isset($pl['opt']['ds'])) {
  5673. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds']);
  5674. }
  5675. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  5676. $annots .= ' /CL [';
  5677. foreach ($pl['opt']['cl'] as $cl) {
  5678. $annots .= sprintf('%.4F ', $cl * $this->k);
  5679. }
  5680. $annots .= ']';
  5681. }
  5682. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  5683. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  5684. $annots .= ' /IT '.$pl['opt']['it'];
  5685. }
  5686. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  5687. $l = $pl['opt']['rd'][0] * $this->k;
  5688. $r = $pl['opt']['rd'][1] * $this->k;
  5689. $t = $pl['opt']['rd'][2] * $this->k;
  5690. $b = $pl['opt']['rd'][3] * $this->k;
  5691. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  5692. }
  5693. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  5694. $annots .= ' /LE /'.$pl['opt']['le'];
  5695. }
  5696. break;
  5697. }
  5698. case 'line': {
  5699. break;
  5700. }
  5701. case 'square': {
  5702. break;
  5703. }
  5704. case 'circle': {
  5705. break;
  5706. }
  5707. case 'polygon': {
  5708. break;
  5709. }
  5710. case 'polyline': {
  5711. break;
  5712. }
  5713. case 'highlight': {
  5714. break;
  5715. }
  5716. case 'underline': {
  5717. break;
  5718. }
  5719. case 'squiggly': {
  5720. break;
  5721. }
  5722. case 'strikeout': {
  5723. break;
  5724. }
  5725. case 'stamp': {
  5726. break;
  5727. }
  5728. case 'caret': {
  5729. break;
  5730. }
  5731. case 'ink': {
  5732. break;
  5733. }
  5734. case 'popup': {
  5735. break;
  5736. }
  5737. case 'fileattachment': {
  5738. if (!isset($pl['opt']['fs'])) {
  5739. break;
  5740. }
  5741. $filename = basename($pl['opt']['fs']);
  5742. if (isset($this->embeddedfiles[$filename]['n'])) {
  5743. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  5744. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  5745. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5746. $annots .= ' /Name /'.$pl['opt']['name'];
  5747. } else {
  5748. $annots .= ' /Name /PushPin';
  5749. }
  5750. }
  5751. break;
  5752. }
  5753. case 'sound': {
  5754. if (!isset($pl['opt']['sound'])) {
  5755. break;
  5756. }
  5757. $filename = basename($pl['opt']['sound']);
  5758. if (isset($this->embeddedfiles[$filename]['n'])) {
  5759. $annots .= ' /Sound <</Type /Sound';
  5760. // ... TO BE COMPLETED ...
  5761. // /R /C /B /E /CO /CP
  5762. // $annots .= ' /F '.$this->_datastring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  5763. $iconsapp = array('Speaker', 'Mic');
  5764. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5765. $annots .= ' /Name /'.$pl['opt']['name'];
  5766. } else {
  5767. $annots .= ' /Name /Speaker';
  5768. }
  5769. }
  5770. break;
  5771. }
  5772. case 'movie': {
  5773. break;
  5774. }
  5775. case 'widget': {
  5776. $hmode = array('N', 'I', 'O', 'P', 'T');
  5777. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  5778. $annots .= ' /H /'.$pl['opt']['h'];
  5779. }
  5780. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  5781. $annots .= ' /MK <<';
  5782. if (isset($pl['opt']['mk']['r'])) {
  5783. $annots .= ' /R '.$pl['opt']['mk']['r'];
  5784. }
  5785. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  5786. $annots .= ' /BC [';
  5787. foreach($pl['opt']['mk']['bc'] AS $col) {
  5788. $col = intval($col);
  5789. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  5790. $annots .= ' '.$color;
  5791. }
  5792. $annots .= ']';
  5793. }
  5794. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  5795. $annots .= ' /BG [';
  5796. foreach($pl['opt']['mk']['bg'] AS $col) {
  5797. $col = intval($col);
  5798. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  5799. $annots .= ' '.$color;
  5800. }
  5801. $annots .= ']';
  5802. }
  5803. if (isset($pl['opt']['mk']['ca'])) {
  5804. $annots .= ' /CA '.$pl['opt']['mk']['ca'].'';
  5805. }
  5806. if (isset($pl['opt']['mk']['rc'])) {
  5807. $annots .= ' /RC '.$pl['opt']['mk']['ca'].'';
  5808. }
  5809. if (isset($pl['opt']['mk']['ac'])) {
  5810. $annots .= ' /AC '.$pl['opt']['mk']['ca'].'';
  5811. }
  5812. if (isset($pl['opt']['mk']['i'])) {
  5813. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  5814. if ($info !== false) {
  5815. $annots .= ' /I '.$info['n'].' 0 R';
  5816. }
  5817. }
  5818. if (isset($pl['opt']['mk']['ri'])) {
  5819. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  5820. if ($info !== false) {
  5821. $annots .= ' /RI '.$info['n'].' 0 R';
  5822. }
  5823. }
  5824. if (isset($pl['opt']['mk']['ix'])) {
  5825. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  5826. if ($info !== false) {
  5827. $annots .= ' /IX '.$info['n'].' 0 R';
  5828. }
  5829. }
  5830. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  5831. $annots .= ' /IF <<';
  5832. $if_sw = array('A', 'B', 'S', 'N');
  5833. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  5834. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  5835. }
  5836. $if_s = array('A', 'P');
  5837. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  5838. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  5839. }
  5840. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  5841. $annots .= ' /A ['.$pl['opt']['mk']['if']['a'][0].' '.$pl['opt']['mk']['if']['a'][1].']';
  5842. }
  5843. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  5844. $annots .= ' /FB true';
  5845. }
  5846. $annots .= '>>';
  5847. }
  5848. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  5849. $annots .= ' /TP '.$pl['opt']['mk']['tp'];
  5850. } else {
  5851. $annots .= ' /TP 0';
  5852. }
  5853. $annots .= '>>';
  5854. } // end MK
  5855. // --- Entries for field dictionaries ---
  5856. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  5857. // set parent
  5858. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  5859. }
  5860. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  5861. $annots .= ' /T '.$this->_datastring($pl['opt']['t']);
  5862. }
  5863. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  5864. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu']);
  5865. }
  5866. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  5867. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm']);
  5868. }
  5869. if (isset($pl['opt']['ff'])) {
  5870. if (is_array($pl['opt']['ff'])) {
  5871. // array of bit settings
  5872. $flag = 0;
  5873. foreach($pl['opt']['ff'] as $val) {
  5874. $flag += 1 << ($val - 1);
  5875. }
  5876. } else {
  5877. $flag = intval($pl['opt']['ff']);
  5878. }
  5879. $annots .= ' /Ff '.$flag;
  5880. }
  5881. if (isset($pl['opt']['maxlen'])) {
  5882. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  5883. }
  5884. if (isset($pl['opt']['v'])) {
  5885. $annots .= ' /V';
  5886. if (is_array($pl['opt']['v'])) {
  5887. foreach ($pl['opt']['v'] AS $optval) {
  5888. $annots .= ' '.$optval;
  5889. }
  5890. } else {
  5891. $annots .= ' '.$this->_textstring($pl['opt']['v']);
  5892. }
  5893. }
  5894. if (isset($pl['opt']['dv']) AND is_string($pl['opt']['dv'])) {
  5895. $annots .= ' /DV';
  5896. if (is_array($pl['opt']['dv'])) {
  5897. foreach ($pl['opt']['dv'] AS $optval) {
  5898. $annots .= ' '.$optval;
  5899. }
  5900. } else {
  5901. $annots .= ' '.$this->_textstring($pl['opt']['dv']);
  5902. }
  5903. }
  5904. if (isset($pl['opt']['rv']) AND is_string($pl['opt']['rv'])) {
  5905. $annots .= ' /RV';
  5906. if (is_array($pl['opt']['rv'])) {
  5907. foreach ($pl['opt']['rv'] AS $optval) {
  5908. $annots .= ' '.$optval;
  5909. }
  5910. } else {
  5911. $annots .= ' '.$this->_textstring($pl['opt']['rv']);
  5912. }
  5913. }
  5914. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  5915. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  5916. }
  5917. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  5918. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  5919. }
  5920. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  5921. $annots .= ' /DA ('.$pl['opt']['da'].')';
  5922. }
  5923. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  5924. $annots .= ' /Q '.intval($pl['opt']['q']);
  5925. }
  5926. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  5927. $annots .= ' /Opt [';
  5928. foreach($pl['opt']['opt'] AS $copt) {
  5929. if (is_array($copt)) {
  5930. $annots .= ' ['.$this->_textstring($copt[0]).' '.$this->_textstring($copt[1]).']';
  5931. } else {
  5932. $annots .= ' '.$this->_textstring($copt);
  5933. }
  5934. }
  5935. $annots .= ']';
  5936. }
  5937. if (isset($pl['opt']['ti'])) {
  5938. $annots .= ' /TI '.intval($pl['opt']['ti']);
  5939. }
  5940. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  5941. $annots .= ' /I [';
  5942. foreach($pl['opt']['i'] AS $copt) {
  5943. $annots .= intval($copt).' ';
  5944. }
  5945. $annots .= ']';
  5946. }
  5947. break;
  5948. }
  5949. case 'screen': {
  5950. break;
  5951. }
  5952. case 'printermark': {
  5953. break;
  5954. }
  5955. case 'trapnet': {
  5956. break;
  5957. }
  5958. case 'watermark': {
  5959. break;
  5960. }
  5961. case '3d': {
  5962. break;
  5963. }
  5964. default: {
  5965. break;
  5966. }
  5967. }
  5968. $annots .= '>>';
  5969. // create new annotation object
  5970. ++$this->annot_obj_id;
  5971. $this->offsets[$this->annot_obj_id] = $this->bufferlen;
  5972. $this->_out($this->annot_obj_id.' 0 obj');
  5973. $this->_out($annots);
  5974. $this->_out('endobj');
  5975. if ($formfield AND ! isset($this->radiobutton_groups[$n][$pl['txt']])) {
  5976. // store reference of form object
  5977. $this->form_obj_id[] = $this->annot_obj_id;
  5978. }
  5979. }
  5980. }
  5981. } // end for each page
  5982. }
  5983. /**
  5984. * Put appearance streams XObject used to define annotation's appearance states
  5985. * @param int $w annotation width
  5986. * @param int $h annotation height
  5987. * @param string $stream appearance stream
  5988. * @return int object ID
  5989. * @access protected
  5990. * @since 4.8.001 (2009-09-09)
  5991. */
  5992. protected function _putAPXObject($w=0, $h=0, $stream='') {
  5993. $stream = trim($stream);
  5994. ++$this->apxo_obj_id;
  5995. $this->offsets[$this->apxo_obj_id] = $this->bufferlen;
  5996. $this->_out($this->apxo_obj_id.' 0 obj');
  5997. $this->_out('<<');
  5998. $this->_out('/Type /XObject');
  5999. $this->_out('/Subtype /Form');
  6000. $this->_out('/FormType 1');
  6001. if ($this->compress) {
  6002. $stream = gzcompress($stream);
  6003. $this->_out('/Filter /FlateDecode');
  6004. }
  6005. $rect = sprintf('%.2F %.2F', $w, $h);
  6006. $this->_out('/BBox [0 0 '.$rect.']');
  6007. $this->_out('/Matrix [1 0 0 1 0 0]');
  6008. $this->_out('/Resources <</ProcSet [/PDF]>>');
  6009. $this->_out('/Length '.strlen($stream));
  6010. $this->_out('>>');
  6011. $this->_putstream($stream);
  6012. $this->_out('endobj');
  6013. return $this->apxo_obj_id;
  6014. }
  6015. /**
  6016. * Output fonts.
  6017. * @access protected
  6018. */
  6019. protected function _putfonts() {
  6020. $nf = $this->n;
  6021. foreach ($this->diffs as $diff) {
  6022. //Encodings
  6023. $this->_newobj();
  6024. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  6025. $this->_out('endobj');
  6026. }
  6027. $mqr = $this->get_mqr();
  6028. $this->set_mqr(false);
  6029. foreach ($this->FontFiles as $file => $info) {
  6030. // search and get font file to embedd
  6031. $fontdir = $info['fontdir'];
  6032. $file = strtolower($file);
  6033. $fontfile = '';
  6034. // search files on various directories
  6035. if (file_exists($fontdir.$file)) {
  6036. $fontfile = $fontdir.$file;
  6037. } elseif (file_exists($this->_getfontpath().$file)) {
  6038. $fontfile = $this->_getfontpath().$file;
  6039. } elseif (file_exists($file)) {
  6040. $fontfile = $file;
  6041. }
  6042. if (!$this->empty_string($fontfile)) {
  6043. $font = file_get_contents($fontfile);
  6044. $compressed = (substr($file, -2) == '.z');
  6045. if ((!$compressed) AND (isset($info['length2']))) {
  6046. $header = (ord($font{0}) == 128);
  6047. if ($header) {
  6048. //Strip first binary header
  6049. $font = substr($font, 6);
  6050. }
  6051. if ($header AND (ord($font{$info['length1']}) == 128)) {
  6052. //Strip second binary header
  6053. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  6054. }
  6055. }
  6056. $this->_newobj();
  6057. $this->FontFiles[$file]['n'] = $this->n;
  6058. $this->_out('<</Length '.strlen($font));
  6059. if ($compressed) {
  6060. $this->_out('/Filter /FlateDecode');
  6061. }
  6062. $this->_out('/Length1 '.$info['length1']);
  6063. if (isset($info['length2'])) {
  6064. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  6065. }
  6066. $this->_out('>>');
  6067. $this->_putstream($font);
  6068. $this->_out('endobj');
  6069. }
  6070. }
  6071. $this->set_mqr($mqr);
  6072. foreach ($this->fontkeys as $k) {
  6073. //Font objects
  6074. $this->setFontSubBuffer($k, 'n', $this->n + 1);
  6075. $font = $this->getFontBuffer($k);
  6076. $type = $font['type'];
  6077. $name = $font['name'];
  6078. if ($type == 'core') {
  6079. //Standard font
  6080. $obj_id = $this->_newobj();
  6081. $this->_out('<</Type /Font');
  6082. $this->_out('/Subtype /Type1');
  6083. $this->_out('/BaseFont /'.$name);
  6084. $this->_out('/Name /F'.$font['i']);
  6085. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  6086. $this->_out('/Encoding /WinAnsiEncoding');
  6087. }
  6088. if (strtolower($name) == 'helvetica') {
  6089. // add default font for annotations
  6090. $this->annotation_fonts['helvetica'] = $k;
  6091. }
  6092. $this->_out('>>');
  6093. $this->_out('endobj');
  6094. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  6095. //Additional Type1 or TrueType font
  6096. $obj_id = $this->_newobj();
  6097. $this->_out('<</Type /Font');
  6098. $this->_out('/Subtype /'.$type);
  6099. $this->_out('/BaseFont /'.$name);
  6100. $this->_out('/Name /F'.$font['i']);
  6101. $this->_out('/FirstChar 32 /LastChar 255');
  6102. $this->_out('/Widths '.($this->n + 1).' 0 R');
  6103. $this->_out('/FontDescriptor '.($this->n + 2).' 0 R');
  6104. if ($font['enc']) {
  6105. if (isset($font['diff'])) {
  6106. $this->_out('/Encoding '.($nf + $font['diff']).' 0 R');
  6107. } else {
  6108. $this->_out('/Encoding /WinAnsiEncoding');
  6109. }
  6110. }
  6111. $this->_out('>>');
  6112. $this->_out('endobj');
  6113. // Widths
  6114. $this->_newobj();
  6115. $cw = &$font['cw'];
  6116. $s = '[';
  6117. for ($i = 32; $i < 256; ++$i) {
  6118. $s .= $cw[$i].' ';
  6119. }
  6120. $this->_out($s.']');
  6121. $this->_out('endobj');
  6122. //Descriptor
  6123. $this->_newobj();
  6124. $s = '<</Type /FontDescriptor /FontName /'.$name;
  6125. foreach ($font['desc'] as $fdk => $fdv) {
  6126. $s .= ' /'.$fdk.' '.$fdv.'';
  6127. }
  6128. if (!$this->empty_string($font['file'])) {
  6129. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  6130. }
  6131. $this->_out($s.'>>');
  6132. $this->_out('endobj');
  6133. } else {
  6134. //Allow for additional types
  6135. $mtd = '_put'.strtolower($type);
  6136. if (!method_exists($this, $mtd)) {
  6137. $this->Error('Unsupported font type: '.$type);
  6138. }
  6139. $obj_id = $this->$mtd($font);
  6140. }
  6141. // store object ID for current font
  6142. $this->font_obj_ids[$k] = $obj_id;
  6143. }
  6144. }
  6145. /**
  6146. * Outputs font widths
  6147. * @parameter array $font font data
  6148. * @parameter int $cidoffset offset for CID values
  6149. * @author Nicola Asuni
  6150. * @access protected
  6151. * @since 4.4.000 (2008-12-07)
  6152. */
  6153. protected function _putfontwidths($font, $cidoffset=0) {
  6154. ksort($font['cw']);
  6155. $rangeid = 0;
  6156. $range = array();
  6157. $prevcid = -2;
  6158. $prevwidth = -1;
  6159. $interval = false;
  6160. // for each character
  6161. foreach ($font['cw'] as $cid => $width) {
  6162. $cid -= $cidoffset;
  6163. if ($width != $font['dw']) {
  6164. if ($cid == ($prevcid + 1)) {
  6165. // consecutive CID
  6166. if ($width == $prevwidth) {
  6167. if ($width == $range[$rangeid][0]) {
  6168. $range[$rangeid][] = $width;
  6169. } else {
  6170. array_pop($range[$rangeid]);
  6171. // new range
  6172. $rangeid = $prevcid;
  6173. $range[$rangeid] = array();
  6174. $range[$rangeid][] = $prevwidth;
  6175. $range[$rangeid][] = $width;
  6176. }
  6177. $interval = true;
  6178. $range[$rangeid]['interval'] = true;
  6179. } else {
  6180. if ($interval) {
  6181. // new range
  6182. $rangeid = $cid;
  6183. $range[$rangeid] = array();
  6184. $range[$rangeid][] = $width;
  6185. } else {
  6186. $range[$rangeid][] = $width;
  6187. }
  6188. $interval = false;
  6189. }
  6190. } else {
  6191. // new range
  6192. $rangeid = $cid;
  6193. $range[$rangeid] = array();
  6194. $range[$rangeid][] = $width;
  6195. $interval = false;
  6196. }
  6197. $prevcid = $cid;
  6198. $prevwidth = $width;
  6199. }
  6200. }
  6201. // optimize ranges
  6202. $prevk = -1;
  6203. $nextk = -1;
  6204. $prevint = false;
  6205. foreach ($range as $k => $ws) {
  6206. $cws = count($ws);
  6207. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  6208. if (isset($range[$k]['interval'])) {
  6209. unset($range[$k]['interval']);
  6210. }
  6211. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  6212. unset($range[$k]);
  6213. } else {
  6214. $prevk = $k;
  6215. }
  6216. $nextk = $k + $cws;
  6217. if (isset($ws['interval'])) {
  6218. if ($cws > 3) {
  6219. $prevint = true;
  6220. } else {
  6221. $prevint = false;
  6222. }
  6223. unset($range[$k]['interval']);
  6224. --$nextk;
  6225. } else {
  6226. $prevint = false;
  6227. }
  6228. }
  6229. // output data
  6230. $w = '';
  6231. foreach ($range as $k => $ws) {
  6232. if (count(array_count_values($ws)) == 1) {
  6233. // interval mode is more compact
  6234. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  6235. } else {
  6236. // range mode
  6237. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  6238. }
  6239. }
  6240. $this->_out('/W ['.$w.' ]');
  6241. }
  6242. /**
  6243. * Adds unicode fonts.<br>
  6244. * Based on PDF Reference 1.3 (section 5)
  6245. * @parameter array $font font data
  6246. * @return int font object ID
  6247. * @access protected
  6248. * @author Nicola Asuni
  6249. * @since 1.52.0.TC005 (2005-01-05)
  6250. */
  6251. protected function _puttruetypeunicode($font) {
  6252. // Type0 Font
  6253. // A composite font composed of other fonts, organized hierarchically
  6254. $obj_id = $this->_newobj();
  6255. $this->_out('<</Type /Font');
  6256. $this->_out('/Subtype /Type0');
  6257. $this->_out('/BaseFont /'.$font['name'].'');
  6258. $this->_out('/Name /F'.$font['i']);
  6259. $this->_out('/Encoding /'.$font['enc']);
  6260. $this->_out('/ToUnicode /Identity-H');
  6261. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  6262. $this->_out('>>');
  6263. $this->_out('endobj');
  6264. // CIDFontType2
  6265. // A CIDFont whose glyph descriptions are based on TrueType font technology
  6266. $this->_newobj();
  6267. $this->_out('<</Type /Font');
  6268. $this->_out('/Subtype /CIDFontType2');
  6269. $this->_out('/BaseFont /'.$font['name'].'');
  6270. // A dictionary containing entries that define the character collection of the CIDFont.
  6271. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  6272. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  6273. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  6274. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  6275. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  6276. $this->_out('/DW '.$font['dw'].''); // default width
  6277. $this->_putfontwidths($font, 0);
  6278. $this->_out('/CIDToGIDMap '.($this->n + 2).' 0 R');
  6279. $this->_out('>>');
  6280. $this->_out('endobj');
  6281. // Font descriptor
  6282. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  6283. $this->_newobj();
  6284. $this->_out('<</Type /FontDescriptor');
  6285. $this->_out('/FontName /'.$font['name']);
  6286. foreach ($font['desc'] as $key => $value) {
  6287. $this->_out('/'.$key.' '.$value);
  6288. }
  6289. $fontdir = '';
  6290. if (!$this->empty_string($font['file'])) {
  6291. // A stream containing a TrueType font
  6292. $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
  6293. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  6294. }
  6295. $this->_out('>>');
  6296. $this->_out('endobj');
  6297. $this->_newobj();
  6298. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  6299. // Embed CIDToGIDMap
  6300. // A specification of the mapping from CIDs to glyph indices
  6301. // search and get CTG font file to embedd
  6302. $ctgfile = strtolower($font['ctg']);
  6303. // search and get ctg font file to embedd
  6304. $fontfile = '';
  6305. // search files on various directories
  6306. if (file_exists($fontdir.$ctgfile)) {
  6307. $fontfile = $fontdir.$ctgfile;
  6308. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  6309. $fontfile = $this->_getfontpath().$ctgfile;
  6310. } elseif (file_exists($ctgfile)) {
  6311. $fontfile = $ctgfile;
  6312. }
  6313. if ($this->empty_string($fontfile)) {
  6314. $this->Error('Font file not found: '.$ctgfile);
  6315. }
  6316. $size = filesize($fontfile);
  6317. $this->_out('<</Length '.$size.'');
  6318. if (substr($fontfile, -2) == '.z') { // check file extension
  6319. // Decompresses data encoded using the public-domain
  6320. // zlib/deflate compression method, reproducing the
  6321. // original text or binary data
  6322. $this->_out('/Filter /FlateDecode');
  6323. }
  6324. $this->_out('>>');
  6325. $this->_putstream(file_get_contents($fontfile));
  6326. }
  6327. $this->_out('endobj');
  6328. return $obj_id;
  6329. }
  6330. /**
  6331. * Output CID-0 fonts.
  6332. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  6333. * @param array $font font data
  6334. * @return int font object ID
  6335. * @access protected
  6336. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  6337. * @since 3.2.000 (2008-06-23)
  6338. */
  6339. protected function _putcidfont0($font) {
  6340. $cidoffset = 0;
  6341. if (!isset($font['cw'][1])) {
  6342. $cidoffset = 31;
  6343. }
  6344. if (isset($font['cidinfo']['uni2cid'])) {
  6345. // convert unicode to cid.
  6346. $uni2cid = $font['cidinfo']['uni2cid'];
  6347. $cw = array();
  6348. foreach ($font['cw'] as $uni => $width) {
  6349. if (isset($uni2cid[$uni])) {
  6350. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  6351. } elseif ($uni < 256) {
  6352. $cw[$uni] = $width;
  6353. } // else unknown character
  6354. }
  6355. $font = array_merge($font, array('cw' => $cw));
  6356. }
  6357. $name = $font['name'];
  6358. $enc = $font['enc'];
  6359. if ($enc) {
  6360. $longname = $name.'-'.$enc;
  6361. } else {
  6362. $longname = $name;
  6363. }
  6364. $obj_id = $this->_newobj();
  6365. $this->_out('<</Type /Font');
  6366. $this->_out('/Subtype /Type0');
  6367. $this->_out('/BaseFont /'.$longname);
  6368. $this->_out('/Name /F'.$font['i']);
  6369. if ($enc) {
  6370. $this->_out('/Encoding /'.$enc);
  6371. }
  6372. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  6373. $this->_out('>>');
  6374. $this->_out('endobj');
  6375. $this->_newobj();
  6376. $this->_out('<</Type /Font');
  6377. $this->_out('/Subtype /CIDFontType0');
  6378. $this->_out('/BaseFont /'.$name);
  6379. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  6380. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  6381. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  6382. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  6383. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  6384. $this->_out('/DW '.$font['dw']);
  6385. $this->_putfontwidths($font, $cidoffset);
  6386. $this->_out('>>');
  6387. $this->_out('endobj');
  6388. $this->_newobj();
  6389. $s = '<</Type /FontDescriptor /FontName /'.$name;
  6390. foreach ($font['desc'] as $k => $v) {
  6391. if ($k != 'Style') {
  6392. $s .= ' /'.$k.' '.$v.'';
  6393. }
  6394. }
  6395. $this->_out($s.'>>');
  6396. $this->_out('endobj');
  6397. return $obj_id;
  6398. }
  6399. /**
  6400. * Output images.
  6401. * @access protected
  6402. */
  6403. protected function _putimages() {
  6404. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  6405. foreach ($this->imagekeys as $file) {
  6406. $info = $this->getImageBuffer($file);
  6407. $this->_newobj();
  6408. $this->setImageSubBuffer($file, 'n', $this->n);
  6409. $this->_out('<</Type /XObject');
  6410. $this->_out('/Subtype /Image');
  6411. $this->_out('/Width '.$info['w']);
  6412. $this->_out('/Height '.$info['h']);
  6413. if (array_key_exists('masked', $info)) {
  6414. $this->_out('/SMask '.($this->n - 1).' 0 R');
  6415. }
  6416. if ($info['cs'] == 'Indexed') {
  6417. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]');
  6418. } else {
  6419. $this->_out('/ColorSpace /'.$info['cs']);
  6420. if ($info['cs'] == 'DeviceCMYK') {
  6421. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  6422. }
  6423. }
  6424. $this->_out('/BitsPerComponent '.$info['bpc']);
  6425. if (isset($info['f'])) {
  6426. $this->_out('/Filter /'.$info['f']);
  6427. }
  6428. if (isset($info['parms'])) {
  6429. $this->_out($info['parms']);
  6430. }
  6431. if (isset($info['trns']) AND is_array($info['trns'])) {
  6432. $trns='';
  6433. $count_info = count($info['trns']);
  6434. for ($i=0; $i < $count_info; ++$i) {
  6435. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  6436. }
  6437. $this->_out('/Mask ['.$trns.']');
  6438. }
  6439. $this->_out('/Length '.strlen($info['data']).'>>');
  6440. $this->_putstream($info['data']);
  6441. $this->_out('endobj');
  6442. //Palette
  6443. if ($info['cs'] == 'Indexed') {
  6444. $this->_newobj();
  6445. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  6446. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  6447. $this->_putstream($pal);
  6448. $this->_out('endobj');
  6449. }
  6450. }
  6451. }
  6452. /**
  6453. * Output Spot Colors Resources.
  6454. * @access protected
  6455. * @since 4.0.024 (2008-09-12)
  6456. */
  6457. protected function _putspotcolors() {
  6458. foreach ($this->spot_colors as $name => $color) {
  6459. $this->_newobj();
  6460. $this->spot_colors[$name]['n'] = $this->n;
  6461. $this->_out('[/Separation /'.str_replace(' ', '#20', $name));
  6462. $this->_out('/DeviceCMYK <<');
  6463. $this->_out('/Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] ');
  6464. $this->_out(sprintf('/C1 [%.4F %.4F %.4F %.4F] ', $color['c']/100, $color['m']/100, $color['y']/100, $color['k']/100));
  6465. $this->_out('/FunctionType 2 /Domain [0 1] /N 1>>]');
  6466. $this->_out('endobj');
  6467. }
  6468. }
  6469. /**
  6470. * Output object dictionary for images.
  6471. * @access protected
  6472. */
  6473. protected function _putxobjectdict() {
  6474. foreach ($this->imagekeys as $file) {
  6475. $info = $this->getImageBuffer($file);
  6476. $this->_out('/I'.$info['i'].' '.$info['n'].' 0 R');
  6477. }
  6478. }
  6479. /**
  6480. * Output Resources Dictionary.
  6481. * @access protected
  6482. */
  6483. protected function _putresourcedict() {
  6484. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  6485. $this->_out('/Font <<');
  6486. foreach ($this->fontkeys as $fontkey) {
  6487. $font = $this->getFontBuffer($fontkey);
  6488. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  6489. }
  6490. $this->_out('>>');
  6491. $this->_out('/XObject <<');
  6492. $this->_putxobjectdict();
  6493. $this->_out('>>');
  6494. // visibility
  6495. $this->_out('/Properties <</OC1 '.$this->n_ocg_print.' 0 R /OC2 '.$this->n_ocg_view.' 0 R>>');
  6496. // transparency
  6497. $this->_out('/ExtGState <<');
  6498. foreach ($this->extgstates as $k => $extgstate) {
  6499. $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R');
  6500. }
  6501. $this->_out('>>');
  6502. // gradients
  6503. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  6504. $this->_out('/Shading <<');
  6505. foreach ($this->gradients as $id => $grad) {
  6506. $this->_out('/Sh'.$id.' '.$grad['id'].' 0 R');
  6507. }
  6508. $this->_out('>>');
  6509. }
  6510. // spot colors
  6511. if (isset($this->spot_colors) AND (count($this->spot_colors) > 0)) {
  6512. $this->_out('/ColorSpace <<');
  6513. foreach ($this->spot_colors as $color) {
  6514. $this->_out('/CS'.$color['i'].' '.$color['n'].' 0 R');
  6515. }
  6516. $this->_out('>>');
  6517. }
  6518. }
  6519. /**
  6520. * Output Resources.
  6521. * @access protected
  6522. */
  6523. protected function _putresources() {
  6524. $this->_putextgstates();
  6525. $this->_putocg();
  6526. $this->_putfonts();
  6527. $this->_putimages();
  6528. $this->_putspotcolors();
  6529. $this->_putshaders();
  6530. //Resource dictionary
  6531. $this->offsets[2] = $this->bufferlen;
  6532. $this->_out('2 0 obj');
  6533. $this->_out('<<');
  6534. $this->_putresourcedict();
  6535. $this->_out('>>');
  6536. $this->_out('endobj');
  6537. $this->_putbookmarks();
  6538. $this->_putEmbeddedFiles();
  6539. $this->_putannotsobjs();
  6540. $this->_putjavascript();
  6541. // encryption
  6542. if ($this->encrypted) {
  6543. $this->_newobj();
  6544. $this->enc_obj_id = $this->n;
  6545. $this->_out('<<');
  6546. $this->_putencryption();
  6547. $this->_out('>>');
  6548. $this->_out('endobj');
  6549. }
  6550. }
  6551. /**
  6552. * Adds some Metadata information (Document Information Dictionary)
  6553. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  6554. * @access protected
  6555. */
  6556. protected function _putinfo() {
  6557. if (!$this->empty_string($this->title)) {
  6558. // The document's title.
  6559. $this->_out('/Title '.$this->_textstring($this->title));
  6560. }
  6561. if (!$this->empty_string($this->author)) {
  6562. // The name of the person who created the document.
  6563. $this->_out('/Author '.$this->_textstring($this->author));
  6564. }
  6565. if (!$this->empty_string($this->subject)) {
  6566. // The subject of the document.
  6567. $this->_out('/Subject '.$this->_textstring($this->subject));
  6568. }
  6569. if (!$this->empty_string($this->keywords)) {
  6570. // Keywords associated with the document.
  6571. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  6572. }
  6573. if (!$this->empty_string($this->creator)) {
  6574. // If the document was converted to PDF from another format, the name of the conforming product that created the original document from which it was converted.
  6575. $this->_out('/Creator '.$this->_textstring($this->creator));
  6576. }
  6577. if (defined('PDF_PRODUCER')) {
  6578. // If the document was converted to PDF from another format, the name of the conforming product that converted it to PDF.
  6579. $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
  6580. } else {
  6581. // default producer
  6582. $this->_out('/Producer '.$this->_textstring('TCPDF'));
  6583. }
  6584. // The date and time the document was created, in human-readable form
  6585. $this->_out('/CreationDate '.$this->_datestring());
  6586. // The date and time the document was most recently modified, in human-readable form
  6587. $this->_out('/ModDate '.$this->_datestring());
  6588. // A name object indicating whether the document has been modified to include trapping information
  6589. //$this->_out('/Trapped /False');
  6590. }
  6591. /**
  6592. * Output Catalog.
  6593. * @access protected
  6594. */
  6595. protected function _putcatalog() {
  6596. $this->_out('/Type /Catalog');
  6597. $this->_out('/Pages 1 0 R');
  6598. if ($this->ZoomMode == 'fullpage') {
  6599. $this->_out('/OpenAction [3 0 R /Fit]');
  6600. } elseif ($this->ZoomMode == 'fullwidth') {
  6601. $this->_out('/OpenAction [3 0 R /FitH null]');
  6602. } elseif ($this->ZoomMode == 'real') {
  6603. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  6604. } elseif (!is_string($this->ZoomMode)) {
  6605. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode / 100).']');
  6606. }
  6607. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  6608. $this->_out('/PageLayout /'.$this->LayoutMode.'');
  6609. }
  6610. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  6611. $this->_out('/PageMode /'.$this->PageMode);
  6612. }
  6613. if (isset($this->l['a_meta_language'])) {
  6614. $this->_out('/Lang /'.$this->l['a_meta_language']);
  6615. }
  6616. $this->_out('/Names <<');
  6617. if ((!empty($this->javascript)) OR (!empty($this->js_objects))) {
  6618. $this->_out('/JavaScript '.($this->n_js).' 0 R');
  6619. }
  6620. $this->_out('>>');
  6621. if (count($this->outlines) > 0) {
  6622. $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
  6623. $this->_out('/PageMode /UseOutlines');
  6624. }
  6625. $this->_putviewerpreferences();
  6626. $p = $this->n_ocg_print.' 0 R';
  6627. $v = $this->n_ocg_view.' 0 R';
  6628. $as = '<</Event /Print /OCGs ['.$p.' '.$v.'] /Category [/Print]>> <</Event /View /OCGs ['.$p.' '.$v.'] /Category [/View]>>';
  6629. $this->_out('/OCProperties <</OCGs ['.$p.' '.$v.'] /D <</ON ['.$p.'] /OFF ['.$v.'] /AS ['.$as.']>>>>');
  6630. // AcroForm
  6631. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  6632. $this->_out('/AcroForm<<');
  6633. $objrefs = '';
  6634. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6635. $objrefs .= $this->sig_obj_id.' 0 R';
  6636. }
  6637. if (!empty($this->form_obj_id)) {
  6638. foreach($this->form_obj_id as $objid) {
  6639. $objrefs .= ' '.$objid.' 0 R';
  6640. }
  6641. }
  6642. $this->_out('/Fields ['.$objrefs.']');
  6643. $this->_out('/NeedAppearances '.(empty($this->form_obj_id)?'false':'true'));
  6644. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6645. $this->_out('/SigFlags 3');
  6646. }
  6647. //$this->_out('/CO ');
  6648. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  6649. $this->_out('/DR <<');
  6650. $this->_out('/Font <<');
  6651. foreach ($this->annotation_fonts as $font => $fontkey) {
  6652. $this->_out('/F'.($fontkey + 1).' '.$this->font_obj_ids[$font].' 0 R');
  6653. }
  6654. $this->_out('>>');
  6655. $this->_out('>>');
  6656. }
  6657. $this->_out('/DA (/F'.(array_search('helvetica', $this->fontkeys) + 1).' 0 Tf 0 g)');
  6658. $this->_out('/Q '.(($this->rtl)?'2':'0'));
  6659. //$this->_out('/XFA ');
  6660. $this->_out('>>');
  6661. // signatures
  6662. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6663. if ($this->signature_data['cert_type'] > 0) {
  6664. $this->_out('/Perms<</DocMDP '.($this->sig_obj_id + 1).' 0 R>>');
  6665. } else {
  6666. $this->_out('/Perms<</UR3 '.($this->sig_obj_id + 1).' 0 R>>');
  6667. }
  6668. }
  6669. }
  6670. }
  6671. /**
  6672. * Output viewer preferences.
  6673. * @author Nicola asuni
  6674. * @since 3.1.000 (2008-06-09)
  6675. * @access protected
  6676. */
  6677. protected function _putviewerpreferences() {
  6678. $this->_out('/ViewerPreferences<<');
  6679. if ($this->rtl) {
  6680. $this->_out('/Direction /R2L');
  6681. } else {
  6682. $this->_out('/Direction /L2R');
  6683. }
  6684. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  6685. $this->_out('/HideToolbar true');
  6686. }
  6687. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  6688. $this->_out('/HideMenubar true');
  6689. }
  6690. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  6691. $this->_out('/HideWindowUI true');
  6692. }
  6693. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  6694. $this->_out('/FitWindow true');
  6695. }
  6696. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  6697. $this->_out('/CenterWindow true');
  6698. }
  6699. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  6700. $this->_out('/DisplayDocTitle true');
  6701. }
  6702. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  6703. $this->_out('/NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'].'');
  6704. }
  6705. if (isset($this->viewer_preferences['ViewArea'])) {
  6706. $this->_out('/ViewArea /'.$this->viewer_preferences['ViewArea']);
  6707. }
  6708. if (isset($this->viewer_preferences['ViewClip'])) {
  6709. $this->_out('/ViewClip /'.$this->viewer_preferences['ViewClip']);
  6710. }
  6711. if (isset($this->viewer_preferences['PrintArea'])) {
  6712. $this->_out('/PrintArea /'.$this->viewer_preferences['PrintArea']);
  6713. }
  6714. if (isset($this->viewer_preferences['PrintClip'])) {
  6715. $this->_out('/PrintClip /'.$this->viewer_preferences['PrintClip']);
  6716. }
  6717. if (isset($this->viewer_preferences['PrintScaling'])) {
  6718. $this->_out('/PrintScaling /'.$this->viewer_preferences['PrintScaling']);
  6719. }
  6720. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  6721. $this->_out('/Duplex /'.$this->viewer_preferences['Duplex']);
  6722. }
  6723. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  6724. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  6725. $this->_out('/PickTrayByPDFSize true');
  6726. } else {
  6727. $this->_out('/PickTrayByPDFSize false');
  6728. }
  6729. }
  6730. if (isset($this->viewer_preferences['PrintPageRange'])) {
  6731. $PrintPageRangeNum = '';
  6732. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  6733. $PrintPageRangeNum .= ' '.($v - 1).'';
  6734. }
  6735. $this->_out('/PrintPageRange ['.substr($PrintPageRangeNum,1).']');
  6736. }
  6737. if (isset($this->viewer_preferences['NumCopies'])) {
  6738. $this->_out('/NumCopies '.intval($this->viewer_preferences['NumCopies']));
  6739. }
  6740. $this->_out('>>');
  6741. }
  6742. /**
  6743. * Output trailer.
  6744. * @access protected
  6745. */
  6746. protected function _puttrailer() {
  6747. $this->_out('/Size '.($this->n + 1));
  6748. $this->_out('/Root '.$this->n.' 0 R');
  6749. $this->_out('/Info '.($this->n - 1).' 0 R');
  6750. if ($this->encrypted) {
  6751. $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
  6752. $this->_out('/ID [()()]');
  6753. }
  6754. }
  6755. /**
  6756. * Output PDF header.
  6757. * @access protected
  6758. */
  6759. protected function _putheader() {
  6760. $this->_out('%PDF-'.$this->PDFVersion);
  6761. }
  6762. /**
  6763. * Output end of document (EOF).
  6764. * @access protected
  6765. */
  6766. protected function _enddoc() {
  6767. $this->state = 1;
  6768. $this->_putheader();
  6769. $this->_putpages();
  6770. $this->_putresources();
  6771. // Signature
  6772. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6773. // widget annotation for signature
  6774. $this->sig_obj_id = $this->_newobj();
  6775. // --- replace signature ID on the first page ---
  6776. // get the document content
  6777. $pdfdoc = $this->getBuffer();
  6778. // Remove the original buffer
  6779. if (isset($this->diskcache) AND $this->diskcache) {
  6780. // remove buffer file from cache
  6781. unlink($this->buffer);
  6782. }
  6783. unset($this->buffer);
  6784. $signature_widget_ref = sprintf('%u 0 R', $this->sig_obj_id);
  6785. $signature_widget_ref .= str_repeat(' ', (strlen($this->sig_annot_ref) - strlen($signature_widget_ref)));
  6786. $pdfdoc = str_replace($this->sig_annot_ref, $signature_widget_ref, $pdfdoc);
  6787. $this->diskcache = false;
  6788. $this->buffer = &$pdfdoc;
  6789. $this->bufferlen = strlen($pdfdoc);
  6790. // ---
  6791. $this->_out('<<');
  6792. $this->_out('/Type /Annot /Subtype /Widget /Rect [0 0 0 0]');
  6793. $this->_out('/P 3 0 R'); // link to first page object
  6794. $this->_out('/FT /Sig');
  6795. $this->_out('/T '.$this->_textstring('Signature'));
  6796. $this->_out('/Ff 0');
  6797. $this->_out('/V '.($this->sig_obj_id + 1).' 0 R');
  6798. $this->_out('>>');
  6799. $this->_out('endobj');
  6800. // signature
  6801. $this->_newobj();
  6802. $this->_out('<<');
  6803. $this->_putsignature();
  6804. $this->_out('>>');
  6805. $this->_out('endobj');
  6806. }
  6807. // Info
  6808. $this->_newobj();
  6809. $this->_out('<<');
  6810. $this->_putinfo();
  6811. $this->_out('>>');
  6812. $this->_out('endobj');
  6813. // Catalog
  6814. $this->_newobj();
  6815. $this->_out('<<');
  6816. $this->_putcatalog();
  6817. $this->_out('>>');
  6818. $this->_out('endobj');
  6819. // Cross-ref
  6820. $o = $this->bufferlen;
  6821. $this->_out('xref');
  6822. $this->_out('0 '.($this->n + 1));
  6823. $this->_out('0000000000 65535 f ');
  6824. for ($i=1; $i <= $this->n; ++$i) {
  6825. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6826. }
  6827. // Embedded Files
  6828. if (isset($this->embeddedfiles) AND count($this->embeddedfiles) > 0) {
  6829. $this->_out($this->embedded_start_obj_id.' '.count($this->embeddedfiles));
  6830. foreach ($this->embeddedfiles as $filename => $filedata) {
  6831. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$filedata['n']]));
  6832. }
  6833. }
  6834. // Annotation Objects
  6835. if ($this->annot_obj_id > $this->annots_start_obj_id) {
  6836. $this->_out(($this->annots_start_obj_id + 1).' '.($this->annot_obj_id - $this->annots_start_obj_id));
  6837. for ($i = ($this->annots_start_obj_id + 1); $i <= $this->annot_obj_id; ++$i) {
  6838. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6839. }
  6840. }
  6841. // Javascript Objects
  6842. if ($this->js_obj_id > $this->js_start_obj_id) {
  6843. $this->_out(($this->js_start_obj_id + 1).' '.($this->js_obj_id - $this->js_start_obj_id));
  6844. for ($i = ($this->js_start_obj_id + 1); $i <= $this->js_obj_id; ++$i) {
  6845. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6846. }
  6847. }
  6848. // Appearance streams XObjects
  6849. if ($this->apxo_obj_id > $this->apxo_start_obj_id) {
  6850. $this->_out(($this->apxo_start_obj_id + 1).' '.($this->apxo_obj_id - $this->apxo_start_obj_id));
  6851. for ($i = ($this->apxo_start_obj_id + 1); $i <= $this->apxo_obj_id; ++$i) {
  6852. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6853. }
  6854. }
  6855. //Trailer
  6856. $this->_out('trailer');
  6857. $this->_out('<<');
  6858. $this->_puttrailer();
  6859. $this->_out('>>');
  6860. $this->_out('startxref');
  6861. $this->_out($o);
  6862. $this->_out('%%EOF');
  6863. $this->state = 3; // end-of-doc
  6864. if ($this->diskcache) {
  6865. // remove temporary files used for images
  6866. foreach ($this->imagekeys as $key) {
  6867. // remove temporary files
  6868. unlink($this->images[$key]);
  6869. }
  6870. foreach ($this->fontkeys as $key) {
  6871. // remove temporary files
  6872. unlink($this->fonts[$key]);
  6873. }
  6874. }
  6875. }
  6876. /**
  6877. * Initialize a new page.
  6878. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  6879. * @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>
  6880. * @access protected
  6881. */
  6882. protected function _beginpage($orientation='', $format='') {
  6883. ++$this->page;
  6884. $this->setPageBuffer($this->page, '');
  6885. // initialize array for graphics tranformation positions inside a page buffer
  6886. $this->transfmrk[$this->page] = array();
  6887. $this->state = 2;
  6888. if ($this->empty_string($orientation)) {
  6889. if (isset($this->CurOrientation)) {
  6890. $orientation = $this->CurOrientation;
  6891. } else {
  6892. $orientation = 'P';
  6893. }
  6894. }
  6895. if ($this->empty_string($format)) {
  6896. $this->setPageOrientation($orientation);
  6897. } else {
  6898. $this->setPageFormat($format, $orientation);
  6899. }
  6900. if ($this->rtl) {
  6901. $this->x = $this->w - $this->rMargin;
  6902. } else {
  6903. $this->x = $this->lMargin;
  6904. }
  6905. $this->y = $this->tMargin;
  6906. if (isset($this->newpagegroup[$this->page])) {
  6907. // start a new group
  6908. $n = sizeof($this->pagegroups) + 1;
  6909. $alias = '{nb'.$n.'}';
  6910. $this->pagegroups[$alias] = 1;
  6911. $this->currpagegroup = $alias;
  6912. } elseif ($this->currpagegroup) {
  6913. ++$this->pagegroups[$this->currpagegroup];
  6914. }
  6915. }
  6916. /**
  6917. * Mark end of page.
  6918. * @access protected
  6919. */
  6920. protected function _endpage() {
  6921. $this->setVisibility('all');
  6922. $this->state = 1;
  6923. }
  6924. /**
  6925. * Begin a new object and return the object number.
  6926. * @return int object number
  6927. * @access protected
  6928. */
  6929. protected function _newobj() {
  6930. ++$this->n;
  6931. $this->offsets[$this->n] = $this->bufferlen;
  6932. $this->_out($this->n.' 0 obj');
  6933. return $this->n;
  6934. }
  6935. /**
  6936. * Underline text.
  6937. * @param int $x X coordinate
  6938. * @param int $y Y coordinate
  6939. * @param string $txt text to underline
  6940. * @access protected
  6941. */
  6942. protected function _dounderline($x, $y, $txt) {
  6943. $w = $this->GetStringWidth($txt);
  6944. return $this->_dounderlinew($x, $y, $w);
  6945. }
  6946. /**
  6947. * Line through text.
  6948. * @param int $x X coordinate
  6949. * @param int $y Y coordinate
  6950. * @param string $txt text to linethrough
  6951. * @access protected
  6952. */
  6953. protected function _dolinethrough($x, $y, $txt) {
  6954. $w = $this->GetStringWidth($txt);
  6955. return $this->_dolinethroughw($x, $y, $w);
  6956. }
  6957. /**
  6958. * Underline for rectangular text area.
  6959. * @param int $x X coordinate
  6960. * @param int $y Y coordinate
  6961. * @param int $w width to underline
  6962. * @access protected
  6963. * @since 4.8.008 (2009-09-29)
  6964. */
  6965. protected function _dounderlinew($x, $y, $w) {
  6966. $up = $this->CurrentFont['up'];
  6967. $ut = $this->CurrentFont['ut'];
  6968. 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);
  6969. }
  6970. /**
  6971. * Line through for rectangular text area.
  6972. * @param int $x X coordinate
  6973. * @param int $y Y coordinate
  6974. * @param string $txt text to linethrough
  6975. * @access protected
  6976. * @since 4.8.008 (2009-09-29)
  6977. */
  6978. protected function _dolinethroughw($x, $y, $w) {
  6979. $up = $this->CurrentFont['up'];
  6980. $ut = $this->CurrentFont['ut'];
  6981. 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);
  6982. }
  6983. /**
  6984. * Read a 4-byte integer from file.
  6985. * @param string $f file name.
  6986. * @return 4-byte integer
  6987. * @access protected
  6988. */
  6989. protected function _freadint($f) {
  6990. $a = unpack('Ni', fread($f, 4));
  6991. return $a['i'];
  6992. }
  6993. /**
  6994. * Add "\" before "\", "(" and ")"
  6995. * @param string $s string to escape.
  6996. * @return string escaped string.
  6997. * @access protected
  6998. */
  6999. protected function _escape($s) {
  7000. // the chr(13) substitution fixes the Bugs item #1421290.
  7001. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  7002. }
  7003. /**
  7004. * Format a data string for meta information
  7005. * @param string $s date string to escape.
  7006. * @return string escaped string.
  7007. * @access protected
  7008. */
  7009. protected function _datastring($s) {
  7010. if ($this->encrypted) {
  7011. $s = $this->_RC4($this->_objectkey($this->n), $s);
  7012. }
  7013. return '('. $this->_escape($s).')';
  7014. }
  7015. /**
  7016. * Returns a formatted date for meta information
  7017. * @return string escaped date string.
  7018. * @access protected
  7019. * @since 4.6.028 (2009-08-25)
  7020. */
  7021. protected function _datestring() {
  7022. $current_time = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  7023. return $this->_datastring('D:'.$current_time);
  7024. }
  7025. /**
  7026. * Format a text string for meta information
  7027. * @param string $s string to escape.
  7028. * @return string escaped string.
  7029. * @access protected
  7030. */
  7031. protected function _textstring($s) {
  7032. if ($this->isunicode) {
  7033. //Convert string to UTF-16BE
  7034. $s = $this->UTF8ToUTF16BE($s, true);
  7035. }
  7036. return $this->_datastring($s);
  7037. }
  7038. /**
  7039. * Format a text string
  7040. * @param string $s string to escape.
  7041. * @return string escaped string.
  7042. * @access protected
  7043. */
  7044. protected function _escapetext($s) {
  7045. if ($this->isunicode) {
  7046. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  7047. $s = $this->UTF8ToLatin1($s);
  7048. } else {
  7049. //Convert string to UTF-16BE and reverse RTL language
  7050. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  7051. }
  7052. }
  7053. return $this->_escape($s);
  7054. }
  7055. /**
  7056. * Output a stream.
  7057. * @param string $s string to output.
  7058. * @access protected
  7059. */
  7060. protected function _putstream($s) {
  7061. if ($this->encrypted) {
  7062. $s = $this->_RC4($this->_objectkey($this->n), $s);
  7063. }
  7064. $this->_out('stream');
  7065. $this->_out($s);
  7066. $this->_out('endstream');
  7067. }
  7068. /**
  7069. * Output a string to the document.
  7070. * @param string $s string to output.
  7071. * @access protected
  7072. */
  7073. protected function _out($s) {
  7074. if ($this->state == 2) {
  7075. if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  7076. // puts data before page footer
  7077. $pagebuff = $this->getPageBuffer($this->page);
  7078. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  7079. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  7080. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  7081. // update footer position
  7082. $this->footerpos[$this->page] += strlen($s."\n");
  7083. } else {
  7084. $this->setPageBuffer($this->page, $s."\n", true);
  7085. }
  7086. } else {
  7087. $this->setBuffer($s."\n");
  7088. }
  7089. }
  7090. /**
  7091. * Converts UTF-8 strings to codepoints array.<br>
  7092. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  7093. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  7094. * <pre>
  7095. * Char. number range | UTF-8 octet sequence
  7096. * (hexadecimal) | (binary)
  7097. * --------------------+-----------------------------------------------
  7098. * 0000 0000-0000 007F | 0xxxxxxx
  7099. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  7100. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  7101. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  7102. * ---------------------------------------------------------------------
  7103. *
  7104. * ABFN notation:
  7105. * ---------------------------------------------------------------------
  7106. * UTF8-octets = *( UTF8-char )
  7107. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  7108. * UTF8-1 = %x00-7F
  7109. * UTF8-2 = %xC2-DF UTF8-tail
  7110. *
  7111. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  7112. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  7113. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  7114. * %xF4 %x80-8F 2( UTF8-tail )
  7115. * UTF8-tail = %x80-BF
  7116. * ---------------------------------------------------------------------
  7117. * </pre>
  7118. * @param string $str string to process.
  7119. * @return array containing codepoints (UTF-8 characters values)
  7120. * @access protected
  7121. * @author Nicola Asuni
  7122. * @since 1.53.0.TC005 (2005-01-05)
  7123. */
  7124. protected function UTF8StringToArray($str) {
  7125. if (isset($this->cache_UTF8StringToArray['_'.$str])) {
  7126. // return cached value
  7127. return($this->cache_UTF8StringToArray['_'.$str]);
  7128. }
  7129. // check cache size
  7130. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  7131. // remove first element
  7132. array_shift($this->cache_UTF8StringToArray);
  7133. }
  7134. ++$this->cache_size_UTF8StringToArray;
  7135. if (!$this->isunicode) {
  7136. // split string into array of equivalent codes
  7137. $strarr = array();
  7138. $strlen = strlen($str);
  7139. for ($i=0; $i < $strlen; ++$i) {
  7140. $strarr[] = ord($str{$i});
  7141. }
  7142. // insert new value on cache
  7143. $this->cache_UTF8StringToArray['_'.$str] = $strarr;
  7144. return $strarr;
  7145. }
  7146. $unicode = array(); // array containing unicode values
  7147. $bytes = array(); // array containing single character byte sequences
  7148. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  7149. $str .= ''; // force $str to be a string
  7150. $length = strlen($str);
  7151. for ($i = 0; $i < $length; ++$i) {
  7152. $char = ord($str{$i}); // get one string character at time
  7153. if (count($bytes) == 0) { // get starting octect
  7154. if ($char <= 0x7F) {
  7155. $unicode[] = $char; // use the character "as is" because is ASCII
  7156. $numbytes = 1;
  7157. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  7158. $bytes[] = ($char - 0xC0) << 0x06;
  7159. $numbytes = 2;
  7160. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  7161. $bytes[] = ($char - 0xE0) << 0x0C;
  7162. $numbytes = 3;
  7163. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  7164. $bytes[] = ($char - 0xF0) << 0x12;
  7165. $numbytes = 4;
  7166. } else {
  7167. // use replacement character for other invalid sequences
  7168. $unicode[] = 0xFFFD;
  7169. $bytes = array();
  7170. $numbytes = 1;
  7171. }
  7172. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  7173. $bytes[] = $char - 0x80;
  7174. if (count($bytes) == $numbytes) {
  7175. // compose UTF-8 bytes to a single unicode value
  7176. $char = $bytes[0];
  7177. for ($j = 1; $j < $numbytes; ++$j) {
  7178. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  7179. }
  7180. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  7181. /* The definition of UTF-8 prohibits encoding character numbers between
  7182. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  7183. encoding form (as surrogate pairs) and do not directly represent
  7184. characters. */
  7185. $unicode[] = 0xFFFD; // use replacement character
  7186. } else {
  7187. $unicode[] = $char; // add char to array
  7188. }
  7189. // reset data for next char
  7190. $bytes = array();
  7191. $numbytes = 1;
  7192. }
  7193. } else {
  7194. // use replacement character for other invalid sequences
  7195. $unicode[] = 0xFFFD;
  7196. $bytes = array();
  7197. $numbytes = 1;
  7198. }
  7199. }
  7200. // insert new value on cache
  7201. $this->cache_UTF8StringToArray['_'.$str] = $unicode;
  7202. return $unicode;
  7203. }
  7204. /**
  7205. * Converts UTF-8 strings to UTF16-BE.<br>
  7206. * @param string $str string to process.
  7207. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  7208. * @return string
  7209. * @access protected
  7210. * @author Nicola Asuni
  7211. * @since 1.53.0.TC005 (2005-01-05)
  7212. * @uses UTF8StringToArray(), arrUTF8ToUTF16BE()
  7213. */
  7214. protected function UTF8ToUTF16BE($str, $setbom=true) {
  7215. if (!$this->isunicode) {
  7216. return $str; // string is not in unicode
  7217. }
  7218. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  7219. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  7220. }
  7221. /**
  7222. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  7223. * @param string $str string to process.
  7224. * @return string
  7225. * @author Andrew Whitehead, Nicola Asuni
  7226. * @access protected
  7227. * @since 3.2.000 (2008-06-23)
  7228. */
  7229. protected function UTF8ToLatin1($str) {
  7230. global $utf8tolatin;
  7231. if (!$this->isunicode) {
  7232. return $str; // string is not in unicode
  7233. }
  7234. $outstr = ''; // string to be returned
  7235. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  7236. foreach ($unicode as $char) {
  7237. if ($char < 256) {
  7238. $outstr .= chr($char);
  7239. } elseif (array_key_exists($char, $utf8tolatin)) {
  7240. // map from UTF-8
  7241. $outstr .= chr($utf8tolatin[$char]);
  7242. } elseif ($char == 0xFFFD) {
  7243. // skip
  7244. } else {
  7245. $outstr .= '?';
  7246. }
  7247. }
  7248. return $outstr;
  7249. }
  7250. /**
  7251. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  7252. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  7253. * <pre>
  7254. * Encoding UTF-16:
  7255. *
  7256. * Encoding of a single character from an ISO 10646 character value to
  7257. * UTF-16 proceeds as follows. Let U be the character number, no greater
  7258. * than 0x10FFFF.
  7259. *
  7260. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  7261. * terminate.
  7262. *
  7263. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  7264. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  7265. * represented in 20 bits.
  7266. *
  7267. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  7268. * 0xDC00, respectively. These integers each have 10 bits free to
  7269. * encode the character value, for a total of 20 bits.
  7270. *
  7271. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  7272. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  7273. * bits of W2. Terminate.
  7274. *
  7275. * Graphically, steps 2 through 4 look like:
  7276. * U' = yyyyyyyyyyxxxxxxxxxx
  7277. * W1 = 110110yyyyyyyyyy
  7278. * W2 = 110111xxxxxxxxxx
  7279. * </pre>
  7280. * @param array $unicode array containing UTF-8 unicode values
  7281. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  7282. * @return string
  7283. * @access protected
  7284. * @author Nicola Asuni
  7285. * @since 2.1.000 (2008-01-08)
  7286. * @see UTF8ToUTF16BE()
  7287. */
  7288. protected function arrUTF8ToUTF16BE($unicode, $setbom=true) {
  7289. $outstr = ''; // string to be returned
  7290. if ($setbom) {
  7291. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  7292. }
  7293. foreach ($unicode as $char) {
  7294. if ($char == 0xFFFD) {
  7295. $outstr .= "\xFF\xFD"; // replacement character
  7296. } elseif ($char < 0x10000) {
  7297. $outstr .= chr($char >> 0x08);
  7298. $outstr .= chr($char & 0xFF);
  7299. } else {
  7300. $char -= 0x10000;
  7301. $w1 = 0xD800 | ($char >> 0x10);
  7302. $w2 = 0xDC00 | ($char & 0x3FF);
  7303. $outstr .= chr($w1 >> 0x08);
  7304. $outstr .= chr($w1 & 0xFF);
  7305. $outstr .= chr($w2 >> 0x08);
  7306. $outstr .= chr($w2 & 0xFF);
  7307. }
  7308. }
  7309. return $outstr;
  7310. }
  7311. // ====================================================
  7312. /**
  7313. * Set header font.
  7314. * @param array $font font
  7315. * @access public
  7316. * @since 1.1
  7317. */
  7318. public function setHeaderFont($font) {
  7319. $this->header_font = $font;
  7320. }
  7321. /**
  7322. * Get header font.
  7323. * @return array()
  7324. * @access public
  7325. * @since 4.0.012 (2008-07-24)
  7326. */
  7327. public function getHeaderFont() {
  7328. return $this->header_font;
  7329. }
  7330. /**
  7331. * Set footer font.
  7332. * @param array $font font
  7333. * @access public
  7334. * @since 1.1
  7335. */
  7336. public function setFooterFont($font) {
  7337. $this->footer_font = $font;
  7338. }
  7339. /**
  7340. * Get Footer font.
  7341. * @return array()
  7342. * @access public
  7343. * @since 4.0.012 (2008-07-24)
  7344. */
  7345. public function getFooterFont() {
  7346. return $this->footer_font;
  7347. }
  7348. /**
  7349. * Set language array.
  7350. * @param array $language
  7351. * @access public
  7352. * @since 1.1
  7353. */
  7354. public function setLanguageArray($language) {
  7355. $this->l = $language;
  7356. if (isset($this->l['a_meta_dir'])) {
  7357. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  7358. } else {
  7359. $this->rtl = false;
  7360. }
  7361. }
  7362. /**
  7363. * Returns the PDF data.
  7364. * @access public
  7365. */
  7366. public function getPDFData() {
  7367. if ($this->state < 3) {
  7368. $this->Close();
  7369. }
  7370. return $this->buffer;
  7371. }
  7372. /**
  7373. * Output anchor link.
  7374. * @param string $url link URL or internal link (i.e.: &lt;a href="#23"&gt;link to page 23&lt;/a&gt;)
  7375. * @param string $name link name
  7376. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  7377. * @param boolean $firstline if true prints only the first line and return the remaining string.
  7378. * @param array $color array of RGB text color
  7379. * @param string $style font style (U, D, B, I)
  7380. * @return the number of cells used or the remaining text if $firstline = true;
  7381. * @access public
  7382. */
  7383. public function addHtmlLink($url, $name, $fill=0, $firstline=false, $color='', $style=-1) {
  7384. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  7385. // convert url to internal link
  7386. $page = intval(substr($url, 1));
  7387. $url = $this->AddLink();
  7388. $this->SetLink($url, 0, $page);
  7389. }
  7390. // store current settings
  7391. $prevcolor = $this->fgcolor;
  7392. $prevstyle = $this->FontStyle;
  7393. if (empty($color)) {
  7394. $this->SetTextColorArray($this->htmlLinkColorArray);
  7395. } else {
  7396. $this->SetTextColorArray($color);
  7397. }
  7398. if ($style == -1) {
  7399. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  7400. } else {
  7401. $this->SetFont('', $this->FontStyle.$style);
  7402. }
  7403. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline);
  7404. // restore settings
  7405. $this->SetFont('', $prevstyle);
  7406. $this->SetTextColorArray($prevcolor);
  7407. return $ret;
  7408. }
  7409. /**
  7410. * 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).
  7411. * @param string $color html color
  7412. * @return array RGB color or false in case of error.
  7413. * @access public
  7414. */
  7415. public function convertHTMLColorToDec($color='#FFFFFF') {
  7416. global $webcolor;
  7417. $returncolor = false;
  7418. $color = preg_replace('/[\s]*/', '', $color); // remove extra spaces
  7419. $color = strtolower($color);
  7420. if (($dotpos = strpos($color, '.')) !== false) {
  7421. // remove class parent (i.e.: color.red)
  7422. $color = substr($color, ($dotpos + 1));
  7423. }
  7424. if (strlen($color) == 0) {
  7425. return false;
  7426. }
  7427. if (substr($color, 0, 3) == 'rgb') {
  7428. $codes = substr($color, 4);
  7429. $codes = str_replace(')', '', $codes);
  7430. $returncolor = explode(',', $codes, 3);
  7431. return $returncolor;
  7432. }
  7433. if (substr($color, 0, 1) != '#') {
  7434. // decode color name
  7435. if (isset($webcolor[$color])) {
  7436. $color_code = $webcolor[$color];
  7437. } else {
  7438. return false;
  7439. }
  7440. } else {
  7441. $color_code = substr($color, 1);
  7442. }
  7443. switch (strlen($color_code)) {
  7444. case 3: {
  7445. // three-digit hexadecimal representation
  7446. $r = substr($color_code, 0, 1);
  7447. $g = substr($color_code, 1, 1);
  7448. $b = substr($color_code, 2, 1);
  7449. $returncolor['R'] = hexdec($r.$r);
  7450. $returncolor['G'] = hexdec($g.$g);
  7451. $returncolor['B'] = hexdec($b.$b);
  7452. break;
  7453. }
  7454. case 6: {
  7455. // six-digit hexadecimal representation
  7456. $returncolor['R'] = hexdec(substr($color_code, 0, 2));
  7457. $returncolor['G'] = hexdec(substr($color_code, 2, 2));
  7458. $returncolor['B'] = hexdec(substr($color_code, 4, 2));
  7459. break;
  7460. }
  7461. }
  7462. return $returncolor;
  7463. }
  7464. /**
  7465. * Converts pixels to User's Units.
  7466. * @param int $px pixels
  7467. * @return float value in user's unit
  7468. * @access public
  7469. * @see setImageScale(), getImageScale()
  7470. */
  7471. public function pixelsToUnits($px) {
  7472. return ($px / ($this->imgscale * $this->k));
  7473. }
  7474. /**
  7475. * Reverse function for htmlentities.
  7476. * Convert entities in UTF-8.
  7477. * @param $text_to_convert Text to convert.
  7478. * @return string converted
  7479. * @access public
  7480. */
  7481. public function unhtmlentities($text_to_convert) {
  7482. return html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  7483. }
  7484. // ENCRYPTION METHODS ----------------------------------
  7485. // SINCE 2.0.000 (2008-01-02)
  7486. /**
  7487. * Compute encryption key depending on object number where the encrypted data is stored
  7488. * @param int $n object number
  7489. * @access protected
  7490. * @since 2.0.000 (2008-01-02)
  7491. */
  7492. protected function _objectkey($n) {
  7493. return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10);
  7494. }
  7495. /**
  7496. * Put encryption on PDF document.
  7497. * @access protected
  7498. * @since 2.0.000 (2008-01-02)
  7499. */
  7500. protected function _putencryption() {
  7501. $this->_out('/Filter /Standard');
  7502. $this->_out('/V 1');
  7503. $this->_out('/R 2');
  7504. $this->_out('/O ('.$this->_escape($this->Ovalue).')');
  7505. $this->_out('/U ('.$this->_escape($this->Uvalue).')');
  7506. $this->_out('/P '.$this->Pvalue);
  7507. }
  7508. /**
  7509. * Returns the input text exrypted using RC4 algorithm and the specified key.
  7510. * RC4 is the standard encryption algorithm used in PDF format
  7511. * @param string $key encryption key
  7512. * @param String $text input text to be encrypted
  7513. * @return String encrypted text
  7514. * @access protected
  7515. * @since 2.0.000 (2008-01-02)
  7516. * @author Klemen Vodopivec
  7517. */
  7518. protected function _RC4($key, $text) {
  7519. if ($this->last_rc4_key != $key) {
  7520. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  7521. $rc4 = range(0, 255);
  7522. $j = 0;
  7523. for ($i = 0; $i < 256; ++$i) {
  7524. $t = $rc4[$i];
  7525. $j = ($j + $t + ord($k{$i})) % 256;
  7526. $rc4[$i] = $rc4[$j];
  7527. $rc4[$j] = $t;
  7528. }
  7529. $this->last_rc4_key = $key;
  7530. $this->last_rc4_key_c = $rc4;
  7531. } else {
  7532. $rc4 = $this->last_rc4_key_c;
  7533. }
  7534. $len = strlen($text);
  7535. $a = 0;
  7536. $b = 0;
  7537. $out = '';
  7538. for ($i = 0; $i < $len; ++$i) {
  7539. $a = ($a + 1) % 256;
  7540. $t = $rc4[$a];
  7541. $b = ($b + $t) % 256;
  7542. $rc4[$a] = $rc4[$b];
  7543. $rc4[$b] = $t;
  7544. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  7545. $out .= chr(ord($text{$i}) ^ $k);
  7546. }
  7547. return $out;
  7548. }
  7549. /**
  7550. * Encrypts a string using MD5 and returns it's value as a binary string.
  7551. * @param string $str input string
  7552. * @return String MD5 encrypted binary string
  7553. * @access protected
  7554. * @since 2.0.000 (2008-01-02)
  7555. * @author Klemen Vodopivec
  7556. */
  7557. protected function _md5_16($str) {
  7558. return pack('H*', md5($str));
  7559. }
  7560. /**
  7561. * Compute O value (used for RC4 encryption)
  7562. * @param String $user_pass user password
  7563. * @param String $owner_pass user password
  7564. * @return String O value
  7565. * @access protected
  7566. * @since 2.0.000 (2008-01-02)
  7567. * @author Klemen Vodopivec
  7568. */
  7569. protected function _Ovalue($user_pass, $owner_pass) {
  7570. $tmp = $this->_md5_16($owner_pass);
  7571. $owner_RC4_key = substr($tmp, 0, 5);
  7572. return $this->_RC4($owner_RC4_key, $user_pass);
  7573. }
  7574. /**
  7575. * Compute U value (used for RC4 encryption)
  7576. * @return String U value
  7577. * @access protected
  7578. * @since 2.0.000 (2008-01-02)
  7579. * @author Klemen Vodopivec
  7580. */
  7581. protected function _Uvalue() {
  7582. return $this->_RC4($this->encryption_key, $this->padding);
  7583. }
  7584. /**
  7585. * Compute encryption key
  7586. * @param String $user_pass user password
  7587. * @param String $owner_pass user password
  7588. * @param String $protection protection type
  7589. * @access protected
  7590. * @since 2.0.000 (2008-01-02)
  7591. * @author Klemen Vodopivec
  7592. */
  7593. protected function _generateencryptionkey($user_pass, $owner_pass, $protection) {
  7594. // Pad passwords
  7595. $user_pass = substr($user_pass.$this->padding, 0, 32);
  7596. $owner_pass = substr($owner_pass.$this->padding, 0, 32);
  7597. // Compute O value
  7598. $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass);
  7599. // Compute encyption key
  7600. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  7601. $this->encryption_key = substr($tmp, 0, 5);
  7602. // Compute U value
  7603. $this->Uvalue = $this->_Uvalue();
  7604. // Compute P value
  7605. $this->Pvalue = -(($protection^255) + 1);
  7606. }
  7607. /**
  7608. * Set document protection
  7609. * The permission array is composed of values taken from the following ones:
  7610. * - copy: copy text and images to the clipboard
  7611. * - print: print the document
  7612. * - modify: modify it (except for annotations and forms)
  7613. * - annot-forms: add annotations and forms
  7614. * Remark: the protection against modification is for people who have the full Acrobat product.
  7615. * 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.
  7616. * 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.
  7617. * @param Array $permissions the set of permissions. Empty by default (only viewing is allowed). (print, modify, copy, annot-forms)
  7618. * @param String $user_pass user password. Empty by default.
  7619. * @param String $owner_pass owner password. If not specified, a random value is used.
  7620. * @access public
  7621. * @since 2.0.000 (2008-01-02)
  7622. * @author Klemen Vodopivec
  7623. */
  7624. public function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) {
  7625. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
  7626. $protection = 192;
  7627. foreach ($permissions as $permission) {
  7628. if (!isset($options[$permission])) {
  7629. $this->Error('Incorrect permission: '.$permission);
  7630. }
  7631. $protection += $options[$permission];
  7632. }
  7633. if ($owner_pass === null) {
  7634. $owner_pass = uniqid(rand());
  7635. }
  7636. $this->encrypted = true;
  7637. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  7638. }
  7639. // END OF ENCRYPTION FUNCTIONS -------------------------
  7640. // START TRANSFORMATIONS SECTION -----------------------
  7641. /**
  7642. * Starts a 2D tranformation saving current graphic state.
  7643. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  7644. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  7645. * @access public
  7646. * @since 2.1.000 (2008-01-07)
  7647. * @see StartTransform(), StopTransform()
  7648. */
  7649. public function StartTransform() {
  7650. $this->_out('q');
  7651. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  7652. ++$this->transfmatrix_key;
  7653. $this->transfmatrix[$this->transfmatrix_key] = array();
  7654. }
  7655. /**
  7656. * Stops a 2D tranformation restoring previous graphic state.
  7657. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  7658. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  7659. * @access public
  7660. * @since 2.1.000 (2008-01-07)
  7661. * @see StartTransform(), StopTransform()
  7662. */
  7663. public function StopTransform() {
  7664. $this->_out('Q');
  7665. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  7666. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  7667. --$this->transfmatrix_key;
  7668. }
  7669. array_pop($this->transfmrk[$this->page]);
  7670. }
  7671. /**
  7672. * Horizontal Scaling.
  7673. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  7674. * @param int $x abscissa of the scaling center. Default is current x position
  7675. * @param int $y ordinate of the scaling center. Default is current y position
  7676. * @access public
  7677. * @since 2.1.000 (2008-01-07)
  7678. * @see StartTransform(), StopTransform()
  7679. */
  7680. public function ScaleX($s_x, $x='', $y='') {
  7681. $this->Scale($s_x, 100, $x, $y);
  7682. }
  7683. /**
  7684. * Vertical Scaling.
  7685. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  7686. * @param int $x abscissa of the scaling center. Default is current x position
  7687. * @param int $y ordinate of the scaling center. Default is current y position
  7688. * @access public
  7689. * @since 2.1.000 (2008-01-07)
  7690. * @see StartTransform(), StopTransform()
  7691. */
  7692. public function ScaleY($s_y, $x='', $y='') {
  7693. $this->Scale(100, $s_y, $x, $y);
  7694. }
  7695. /**
  7696. * Vertical and horizontal proportional Scaling.
  7697. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  7698. * @param int $x abscissa of the scaling center. Default is current x position
  7699. * @param int $y ordinate of the scaling center. Default is current y position
  7700. * @access public
  7701. * @since 2.1.000 (2008-01-07)
  7702. * @see StartTransform(), StopTransform()
  7703. */
  7704. public function ScaleXY($s, $x='', $y='') {
  7705. $this->Scale($s, $s, $x, $y);
  7706. }
  7707. /**
  7708. * Vertical and horizontal non-proportional Scaling.
  7709. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  7710. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  7711. * @param int $x abscissa of the scaling center. Default is current x position
  7712. * @param int $y ordinate of the scaling center. Default is current y position
  7713. * @access public
  7714. * @since 2.1.000 (2008-01-07)
  7715. * @see StartTransform(), StopTransform()
  7716. */
  7717. public function Scale($s_x, $s_y, $x='', $y='') {
  7718. if ($x === '') {
  7719. $x = $this->x;
  7720. }
  7721. if ($y === '') {
  7722. $y = $this->y;
  7723. }
  7724. if ($this->rtl) {
  7725. $x = $this->w - $x;
  7726. }
  7727. if (($s_x == 0) OR ($s_y == 0)) {
  7728. $this->Error('Please do not use values equal to zero for scaling');
  7729. }
  7730. $y = ($this->h - $y) * $this->k;
  7731. $x *= $this->k;
  7732. //calculate elements of transformation matrix
  7733. $s_x /= 100;
  7734. $s_y /= 100;
  7735. $tm[0] = $s_x;
  7736. $tm[1] = 0;
  7737. $tm[2] = 0;
  7738. $tm[3] = $s_y;
  7739. $tm[4] = $x * (1 - $s_x);
  7740. $tm[5] = $y * (1 - $s_y);
  7741. //scale the coordinate system
  7742. $this->Transform($tm);
  7743. }
  7744. /**
  7745. * Horizontal Mirroring.
  7746. * @param int $x abscissa of the point. Default is current x position
  7747. * @access public
  7748. * @since 2.1.000 (2008-01-07)
  7749. * @see StartTransform(), StopTransform()
  7750. */
  7751. public function MirrorH($x='') {
  7752. $this->Scale(-100, 100, $x);
  7753. }
  7754. /**
  7755. * Verical Mirroring.
  7756. * @param int $y ordinate of the point. Default is current y position
  7757. * @access public
  7758. * @since 2.1.000 (2008-01-07)
  7759. * @see StartTransform(), StopTransform()
  7760. */
  7761. public function MirrorV($y='') {
  7762. $this->Scale(100, -100, '', $y);
  7763. }
  7764. /**
  7765. * Point reflection mirroring.
  7766. * @param int $x abscissa of the point. Default is current x position
  7767. * @param int $y ordinate of the point. Default is current y position
  7768. * @access public
  7769. * @since 2.1.000 (2008-01-07)
  7770. * @see StartTransform(), StopTransform()
  7771. */
  7772. public function MirrorP($x='',$y='') {
  7773. $this->Scale(-100, -100, $x, $y);
  7774. }
  7775. /**
  7776. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  7777. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  7778. * @param int $x abscissa of the point. Default is current x position
  7779. * @param int $y ordinate of the point. Default is current y position
  7780. * @access public
  7781. * @since 2.1.000 (2008-01-07)
  7782. * @see StartTransform(), StopTransform()
  7783. */
  7784. public function MirrorL($angle=0, $x='',$y='') {
  7785. $this->Scale(-100, 100, $x, $y);
  7786. $this->Rotate(-2*($angle-90), $x, $y);
  7787. }
  7788. /**
  7789. * Translate graphic object horizontally.
  7790. * @param int $t_x movement to the right (or left for RTL)
  7791. * @access public
  7792. * @since 2.1.000 (2008-01-07)
  7793. * @see StartTransform(), StopTransform()
  7794. */
  7795. public function TranslateX($t_x) {
  7796. $this->Translate($t_x, 0);
  7797. }
  7798. /**
  7799. * Translate graphic object vertically.
  7800. * @param int $t_y movement to the bottom
  7801. * @access public
  7802. * @since 2.1.000 (2008-01-07)
  7803. * @see StartTransform(), StopTransform()
  7804. */
  7805. public function TranslateY($t_y) {
  7806. $this->Translate(0, $t_y);
  7807. }
  7808. /**
  7809. * Translate graphic object horizontally and vertically.
  7810. * @param int $t_x movement to the right
  7811. * @param int $t_y movement to the bottom
  7812. * @access public
  7813. * @since 2.1.000 (2008-01-07)
  7814. * @see StartTransform(), StopTransform()
  7815. */
  7816. public function Translate($t_x, $t_y) {
  7817. if ($this->rtl) {
  7818. $t_x = -$t_x;
  7819. }
  7820. //calculate elements of transformation matrix
  7821. $tm[0] = 1;
  7822. $tm[1] = 0;
  7823. $tm[2] = 0;
  7824. $tm[3] = 1;
  7825. $tm[4] = $t_x * $this->k;
  7826. $tm[5] = -$t_y * $this->k;
  7827. //translate the coordinate system
  7828. $this->Transform($tm);
  7829. }
  7830. /**
  7831. * Rotate object.
  7832. * @param float $angle angle in degrees for counter-clockwise rotation
  7833. * @param int $x abscissa of the rotation center. Default is current x position
  7834. * @param int $y ordinate of the rotation center. Default is current y position
  7835. * @access public
  7836. * @since 2.1.000 (2008-01-07)
  7837. * @see StartTransform(), StopTransform()
  7838. */
  7839. public function Rotate($angle, $x='', $y='') {
  7840. if ($x === '') {
  7841. $x = $this->x;
  7842. }
  7843. if ($y === '') {
  7844. $y = $this->y;
  7845. }
  7846. if ($this->rtl) {
  7847. $x = $this->w - $x;
  7848. $angle = -$angle;
  7849. }
  7850. $y = ($this->h - $y) * $this->k;
  7851. $x *= $this->k;
  7852. //calculate elements of transformation matrix
  7853. $tm[0] = cos(deg2rad($angle));
  7854. $tm[1] = sin(deg2rad($angle));
  7855. $tm[2] = -$tm[1];
  7856. $tm[3] = $tm[0];
  7857. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  7858. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  7859. //rotate the coordinate system around ($x,$y)
  7860. $this->Transform($tm);
  7861. }
  7862. /**
  7863. * Skew horizontally.
  7864. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7865. * @param int $x abscissa of the skewing center. default is current x position
  7866. * @param int $y ordinate of the skewing center. default is current y position
  7867. * @access public
  7868. * @since 2.1.000 (2008-01-07)
  7869. * @see StartTransform(), StopTransform()
  7870. */
  7871. public function SkewX($angle_x, $x='', $y='') {
  7872. $this->Skew($angle_x, 0, $x, $y);
  7873. }
  7874. /**
  7875. * Skew vertically.
  7876. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7877. * @param int $x abscissa of the skewing center. default is current x position
  7878. * @param int $y ordinate of the skewing center. default is current y position
  7879. * @access public
  7880. * @since 2.1.000 (2008-01-07)
  7881. * @see StartTransform(), StopTransform()
  7882. */
  7883. public function SkewY($angle_y, $x='', $y='') {
  7884. $this->Skew(0, $angle_y, $x, $y);
  7885. }
  7886. /**
  7887. * Skew.
  7888. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7889. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7890. * @param int $x abscissa of the skewing center. default is current x position
  7891. * @param int $y ordinate of the skewing center. default is current y position
  7892. * @access public
  7893. * @since 2.1.000 (2008-01-07)
  7894. * @see StartTransform(), StopTransform()
  7895. */
  7896. public function Skew($angle_x, $angle_y, $x='', $y='') {
  7897. if ($x === '') {
  7898. $x = $this->x;
  7899. }
  7900. if ($y === '') {
  7901. $y = $this->y;
  7902. }
  7903. if ($this->rtl) {
  7904. $x = $this->w - $x;
  7905. $angle_x = -$angle_x;
  7906. }
  7907. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  7908. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  7909. }
  7910. $x *= $this->k;
  7911. $y = ($this->h - $y) * $this->k;
  7912. //calculate elements of transformation matrix
  7913. $tm[0] = 1;
  7914. $tm[1] = tan(deg2rad($angle_y));
  7915. $tm[2] = tan(deg2rad($angle_x));
  7916. $tm[3] = 1;
  7917. $tm[4] = -$tm[2] * $y;
  7918. $tm[5] = -$tm[1] * $x;
  7919. //skew the coordinate system
  7920. $this->Transform($tm);
  7921. }
  7922. /**
  7923. * Apply graphic transformations.
  7924. * @access protected
  7925. * @since 2.1.000 (2008-01-07)
  7926. * @see StartTransform(), StopTransform()
  7927. */
  7928. protected function Transform($tm) {
  7929. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  7930. // add tranformation matrix
  7931. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  7932. // update tranformation mark
  7933. if (end($this->transfmrk[$this->page]) !== false) {
  7934. $key = key($this->transfmrk[$this->page]);
  7935. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  7936. }
  7937. }
  7938. // END TRANSFORMATIONS SECTION -------------------------
  7939. // START GRAPHIC FUNCTIONS SECTION ---------------------
  7940. // The following section is based on the code provided by David Hernandez Sanz
  7941. /**
  7942. * 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.
  7943. * @param float $width The width.
  7944. * @access public
  7945. * @since 1.0
  7946. * @see Line(), Rect(), Cell(), MultiCell()
  7947. */
  7948. public function SetLineWidth($width) {
  7949. //Set line width
  7950. $this->LineWidth = $width;
  7951. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  7952. if ($this->page > 0) {
  7953. $this->_out($this->linestyleWidth);
  7954. }
  7955. }
  7956. /**
  7957. * Returns the current the line width.
  7958. * @return int Line width
  7959. * @access public
  7960. * @since 2.1.000 (2008-01-07)
  7961. * @see Line(), SetLineWidth()
  7962. */
  7963. public function GetLineWidth() {
  7964. return $this->LineWidth;
  7965. }
  7966. /**
  7967. * Set line style.
  7968. * @param array $style Line style. Array with keys among the following:
  7969. * <ul>
  7970. * <li>width (float): Width of the line in user units.</li>
  7971. * <li>cap (string): Type of cap to put on the line. Possible values are:
  7972. * butt, round, square. The difference between "square" and "butt" is that
  7973. * "square" projects a flat end past the end of the line.</li>
  7974. * <li>join (string): Type of join. Possible values are: miter, round,
  7975. * bevel.</li>
  7976. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  7977. * series of length values, which are the lengths of the on and off dashes.
  7978. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  7979. * 1 off, 2 on, 1 off, ...</li>
  7980. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  7981. * the point at which the pattern starts.</li>
  7982. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  7983. * </ul>
  7984. * @access public
  7985. * @since 2.1.000 (2008-01-08)
  7986. */
  7987. public function SetLineStyle($style) {
  7988. if (!is_array($style)) {
  7989. return;
  7990. }
  7991. extract($style);
  7992. if (isset($width)) {
  7993. $width_prev = $this->LineWidth;
  7994. $this->SetLineWidth($width);
  7995. $this->LineWidth = $width_prev;
  7996. }
  7997. if (isset($cap)) {
  7998. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  7999. if (isset($ca[$cap])) {
  8000. $this->linestyleCap = $ca[$cap].' J';
  8001. $this->_out($this->linestyleCap);
  8002. }
  8003. }
  8004. if (isset($join)) {
  8005. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  8006. if (isset($ja[$join])) {
  8007. $this->linestyleJoin = $ja[$join].' j';
  8008. $this->_out($this->linestyleJoin);
  8009. }
  8010. }
  8011. if (isset($dash)) {
  8012. $dash_string = '';
  8013. if ($dash) {
  8014. if (preg_match('/^.+,/', $dash) > 0) {
  8015. $tab = explode(',', $dash);
  8016. } else {
  8017. $tab = array($dash);
  8018. }
  8019. $dash_string = '';
  8020. foreach ($tab as $i => $v) {
  8021. if ($i) {
  8022. $dash_string .= ' ';
  8023. }
  8024. $dash_string .= sprintf('%.2F', $v);
  8025. }
  8026. }
  8027. if (!isset($phase) OR !$dash) {
  8028. $phase = 0;
  8029. }
  8030. $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $phase);
  8031. $this->_out($this->linestyleDash);
  8032. }
  8033. if (isset($color)) {
  8034. $this->SetDrawColorArray($color);
  8035. }
  8036. }
  8037. /*
  8038. * Set a draw point.
  8039. * @param float $x Abscissa of point.
  8040. * @param float $y Ordinate of point.
  8041. * @access protected
  8042. * @since 2.1.000 (2008-01-08)
  8043. */
  8044. protected function _outPoint($x, $y) {
  8045. if ($this->rtl) {
  8046. $x = $this->w - $x;
  8047. }
  8048. $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
  8049. }
  8050. /*
  8051. * Draws a line from last draw point.
  8052. * @param float $x Abscissa of end point.
  8053. * @param float $y Ordinate of end point.
  8054. * @access protected
  8055. * @since 2.1.000 (2008-01-08)
  8056. */
  8057. protected function _outLine($x, $y) {
  8058. if ($this->rtl) {
  8059. $x = $this->w - $x;
  8060. }
  8061. $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
  8062. }
  8063. /**
  8064. * Draws a rectangle.
  8065. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  8066. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  8067. * @param float $w Width.
  8068. * @param float $h Height.
  8069. * @param string $op options
  8070. * @access protected
  8071. * @since 2.1.000 (2008-01-08)
  8072. */
  8073. protected function _outRect($x, $y, $w, $h, $op) {
  8074. if ($this->rtl) {
  8075. $x = $this->w - $x - $w;
  8076. }
  8077. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  8078. }
  8079. /*
  8080. * Draws a Bezier curve from last draw point.
  8081. * The Bezier curve is a tangent to the line between the control points at either end of the curve.
  8082. * @param float $x1 Abscissa of control point 1.
  8083. * @param float $y1 Ordinate of control point 1.
  8084. * @param float $x2 Abscissa of control point 2.
  8085. * @param float $y2 Ordinate of control point 2.
  8086. * @param float $x3 Abscissa of end point.
  8087. * @param float $y3 Ordinate of end point.
  8088. * @access protected
  8089. * @since 2.1.000 (2008-01-08)
  8090. */
  8091. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  8092. if ($this->rtl) {
  8093. $x1 = $this->w - $x1;
  8094. $x2 = $this->w - $x2;
  8095. $x3 = $this->w - $x3;
  8096. }
  8097. $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));
  8098. }
  8099. /**
  8100. * Draws a line between two points.
  8101. * @param float $x1 Abscissa of first point.
  8102. * @param float $y1 Ordinate of first point.
  8103. * @param float $x2 Abscissa of second point.
  8104. * @param float $y2 Ordinate of second point.
  8105. * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8106. * @access public
  8107. * @since 1.0
  8108. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  8109. */
  8110. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  8111. if (is_array($style)) {
  8112. $this->SetLineStyle($style);
  8113. }
  8114. $this->_outPoint($x1, $y1);
  8115. $this->_outLine($x2, $y2);
  8116. $this->_out(' S');
  8117. }
  8118. /**
  8119. * Draws a rectangle.
  8120. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  8121. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  8122. * @param float $w Width.
  8123. * @param float $h Height.
  8124. * @param string $style Style of rendering. Possible values are:
  8125. * <ul>
  8126. * <li>D or empty string: Draw (default).</li>
  8127. * <li>F: Fill.</li>
  8128. * <li>DF or FD: Draw and fill.</li>
  8129. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8130. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8131. * </ul>
  8132. * @param array $border_style Border style of rectangle. Array with keys among the following:
  8133. * <ul>
  8134. * <li>all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8135. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8136. * </ul>
  8137. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  8138. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8139. * @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).
  8140. * @access public
  8141. * @since 1.0
  8142. * @see SetLineStyle()
  8143. */
  8144. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  8145. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8146. $this->SetFillColorArray($fill_color);
  8147. }
  8148. switch ($style) {
  8149. case 'F': {
  8150. $op = 'f';
  8151. $border_style = array();
  8152. $this->_outRect($x, $y, $w, $h, $op);
  8153. break;
  8154. }
  8155. case 'DF':
  8156. case 'FD': {
  8157. if ((!$border_style) OR (isset($border_style['all']))) {
  8158. $op = 'B';
  8159. if (isset($border_style['all'])) {
  8160. $this->SetLineStyle($border_style['all']);
  8161. $border_style = array();
  8162. }
  8163. } else {
  8164. $op = 'f';
  8165. }
  8166. $this->_outRect($x, $y, $w, $h, $op);
  8167. break;
  8168. }
  8169. case 'CNZ': {
  8170. $op = 'W n';
  8171. $this->_outRect($x, $y, $w, $h, $op);
  8172. break;
  8173. }
  8174. case 'CEO': {
  8175. $op = 'W* n';
  8176. $this->_outRect($x, $y, $w, $h, $op);
  8177. break;
  8178. }
  8179. default: {
  8180. $op = 'S';
  8181. if ((!$border_style) OR (isset($border_style['all']))) {
  8182. if (isset($border_style['all']) AND $border_style['all']) {
  8183. $this->SetLineStyle($border_style['all']);
  8184. $border_style = array();
  8185. }
  8186. $this->_outRect($x, $y, $w, $h, $op);
  8187. }
  8188. break;
  8189. }
  8190. }
  8191. if ($border_style) {
  8192. $border_style2 = array();
  8193. foreach ($border_style as $line => $value) {
  8194. $lenght = strlen($line);
  8195. for ($i = 0; $i < $lenght; ++$i) {
  8196. $border_style2[$line[$i]] = $value;
  8197. }
  8198. }
  8199. $border_style = $border_style2;
  8200. if (isset($border_style['L']) AND $border_style['L']) {
  8201. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  8202. }
  8203. if (isset($border_style['T']) AND $border_style['T']) {
  8204. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  8205. }
  8206. if (isset($border_style['R']) AND $border_style['R']) {
  8207. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  8208. }
  8209. if (isset($border_style['B']) AND $border_style['B']) {
  8210. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  8211. }
  8212. }
  8213. }
  8214. /**
  8215. * Draws a Bezier curve.
  8216. * The Bezier curve is a tangent to the line between the control points at
  8217. * either end of the curve.
  8218. * @param float $x0 Abscissa of start point.
  8219. * @param float $y0 Ordinate of start point.
  8220. * @param float $x1 Abscissa of control point 1.
  8221. * @param float $y1 Ordinate of control point 1.
  8222. * @param float $x2 Abscissa of control point 2.
  8223. * @param float $y2 Ordinate of control point 2.
  8224. * @param float $x3 Abscissa of end point.
  8225. * @param float $y3 Ordinate of end point.
  8226. * @param string $style Style of rendering. Possible values are:
  8227. * <ul>
  8228. * <li>D or empty string: Draw (default).</li>
  8229. * <li>F: Fill.</li>
  8230. * <li>DF or FD: Draw and fill.</li>
  8231. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8232. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8233. * </ul>
  8234. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8235. * @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).
  8236. * @access public
  8237. * @see SetLineStyle()
  8238. * @since 2.1.000 (2008-01-08)
  8239. */
  8240. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  8241. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8242. $this->SetFillColorArray($fill_color);
  8243. }
  8244. switch ($style) {
  8245. case 'F': {
  8246. $op = 'f';
  8247. $line_style = array();
  8248. break;
  8249. }
  8250. case 'FD':
  8251. case 'DF': {
  8252. $op = 'B';
  8253. break;
  8254. }
  8255. case 'CNZ': {
  8256. $op = 'W n';
  8257. break;
  8258. }
  8259. case 'CEO': {
  8260. $op = 'W* n';
  8261. break;
  8262. }
  8263. default: {
  8264. $op = 'S';
  8265. break;
  8266. }
  8267. }
  8268. if ($line_style) {
  8269. $this->SetLineStyle($line_style);
  8270. }
  8271. $this->_outPoint($x0, $y0);
  8272. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  8273. $this->_out($op);
  8274. }
  8275. /**
  8276. * Draws a poly-Bezier curve.
  8277. * Each Bezier curve segment is a tangent to the line between the control points at
  8278. * either end of the curve.
  8279. * @param float $x0 Abscissa of start point.
  8280. * @param float $y0 Ordinate of start point.
  8281. * @param float $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  8282. * @param string $style Style of rendering. Possible values are:
  8283. * <ul>
  8284. * <li>D or empty string: Draw (default).</li>
  8285. * <li>F: Fill.</li>
  8286. * <li>DF or FD: Draw and fill.</li>
  8287. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8288. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8289. * </ul>
  8290. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8291. * @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).
  8292. * @access public
  8293. * @see SetLineStyle()
  8294. * @since 3.0008 (2008-05-12)
  8295. */
  8296. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  8297. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8298. $this->SetFillColorArray($fill_color);
  8299. }
  8300. switch ($style) {
  8301. case 'F': {
  8302. $op = 'f';
  8303. $line_style = array();
  8304. break;
  8305. }
  8306. case 'FD':
  8307. case 'DF': {
  8308. $op = 'B';
  8309. break;
  8310. }
  8311. case 'CNZ': {
  8312. $op = 'W n';
  8313. break;
  8314. }
  8315. case 'CEO': {
  8316. $op = 'W* n';
  8317. break;
  8318. }
  8319. default: {
  8320. $op = 'S';
  8321. break;
  8322. }
  8323. }
  8324. if ($line_style) {
  8325. $this->SetLineStyle($line_style);
  8326. }
  8327. $this->_outPoint($x0, $y0);
  8328. foreach ($segments as $segment) {
  8329. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  8330. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  8331. }
  8332. $this->_out($op);
  8333. }
  8334. /**
  8335. * Draws an ellipse.
  8336. * An ellipse is formed from n Bezier curves.
  8337. * @param float $x0 Abscissa of center point.
  8338. * @param float $y0 Ordinate of center point.
  8339. * @param float $rx Horizontal radius.
  8340. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  8341. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  8342. * @param float $astart: Angle start of draw line. Default value: 0.
  8343. * @param float $afinish: Angle finish of draw line. Default value: 360.
  8344. * @param string $style Style of rendering. Possible values are:
  8345. * <ul>
  8346. * <li>D or empty string: Draw (default).</li>
  8347. * <li>F: Fill.</li>
  8348. * <li>DF or FD: Draw and fill.</li>
  8349. * <li>C: Draw close.</li>
  8350. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8351. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8352. * </ul>
  8353. * @param array $line_style Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8354. * @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).
  8355. * @param integer $nc Number of curves used in ellipse. Default value: 8.
  8356. * @access public
  8357. * @since 2.1.000 (2008-01-08)
  8358. */
  8359. public function Ellipse($x0, $y0, $rx, $ry=0, $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  8360. if ($angle) {
  8361. $this->StartTransform();
  8362. $this->Rotate($angle, $x0, $y0);
  8363. $this->Ellipse($x0, $y0, $rx, $ry, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  8364. $this->StopTransform();
  8365. return;
  8366. }
  8367. if ($rx) {
  8368. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8369. $this->SetFillColorArray($fill_color);
  8370. }
  8371. switch ($style) {
  8372. case 'F': {
  8373. $op = 'f';
  8374. $line_style = array();
  8375. break;
  8376. }
  8377. case 'FD':
  8378. case 'DF': {
  8379. $op = 'B';
  8380. break;
  8381. }
  8382. case 'C': {
  8383. $op = 's'; // Small 's' signifies closing the path as well
  8384. break;
  8385. }
  8386. case 'CNZ': {
  8387. $op = 'W n';
  8388. break;
  8389. }
  8390. case 'CEO': {
  8391. $op = 'W* n';
  8392. break;
  8393. }
  8394. default: {
  8395. $op = 'S';
  8396. break;
  8397. }
  8398. }
  8399. if ($line_style) {
  8400. $this->SetLineStyle($line_style);
  8401. }
  8402. if (!$ry) {
  8403. $ry = $rx;
  8404. }
  8405. $rx *= $this->k;
  8406. $ry *= $this->k;
  8407. if ($nc < 2) {
  8408. $nc = 2;
  8409. }
  8410. $astart = deg2rad((float) $astart);
  8411. $afinish = deg2rad((float) $afinish);
  8412. $total_angle = $afinish - $astart;
  8413. $dt = $total_angle / $nc;
  8414. $dtm = $dt / 3;
  8415. $x0 *= $this->k;
  8416. $y0 = ($this->h - $y0) * $this->k;
  8417. $t1 = $astart;
  8418. $a0 = $x0 + ($rx * cos($t1));
  8419. $b0 = $y0 + ($ry * sin($t1));
  8420. $c0 = -$rx * sin($t1);
  8421. $d0 = $ry * cos($t1);
  8422. $this->_outPoint($a0 / $this->k, $this->h - ($b0 / $this->k));
  8423. for ($i = 1; $i <= $nc; ++$i) {
  8424. // Draw this bit of the total curve
  8425. $t1 = ($i * $dt) + $astart;
  8426. $a1 = $x0 + ($rx * cos($t1));
  8427. $b1 = $y0 + ($ry * sin($t1));
  8428. $c1 = -$rx * sin($t1);
  8429. $d1 = $ry * cos($t1);
  8430. $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));
  8431. $a0 = $a1;
  8432. $b0 = $b1;
  8433. $c0 = $c1;
  8434. $d0 = $d1;
  8435. }
  8436. $this->_out($op);
  8437. }
  8438. }
  8439. /**
  8440. * Draws a circle.
  8441. * A circle is formed from n Bezier curves.
  8442. * @param float $x0 Abscissa of center point.
  8443. * @param float $y0 Ordinate of center point.
  8444. * @param float $r Radius.
  8445. * @param float $astart: Angle start of draw line. Default value: 0.
  8446. * @param float $afinish: Angle finish of draw line. Default value: 360.
  8447. * @param string $style Style of rendering. Possible values are:
  8448. * <ul>
  8449. * <li>D or empty string: Draw (default).</li>
  8450. * <li>F: Fill.</li>
  8451. * <li>DF or FD: Draw and fill.</li>
  8452. * <li>C: Draw close.</li>
  8453. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8454. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8455. * </ul>
  8456. * @param array $line_style Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8457. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  8458. * @param integer $nc Number of curves used in circle. Default value: 8.
  8459. * @access public
  8460. * @since 2.1.000 (2008-01-08)
  8461. */
  8462. public function Circle($x0, $y0, $r, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  8463. $this->Ellipse($x0, $y0, $r, 0, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  8464. }
  8465. /**
  8466. * Draws a polygonal line
  8467. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  8468. * @param string $style Style of rendering. Possible values are:
  8469. * <ul>
  8470. * <li>D or empty string: Draw (default).</li>
  8471. * <li>F: Fill.</li>
  8472. * <li>DF or FD: Draw and fill.</li>
  8473. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8474. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8475. * </ul>
  8476. * @param array $line_style Line style of polygon. Array with keys among the following:
  8477. * <ul>
  8478. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8479. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8480. * </ul>
  8481. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  8482. * @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).
  8483. * @param boolean $closed if true the polygon is closes, otherwise will remain open
  8484. * @access public
  8485. * @since 4.8.003 (2009-09-15)
  8486. */
  8487. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  8488. $this->Polygon($p, $style, $line_style, $fill_color, false);
  8489. }
  8490. /**
  8491. * Draws a polygon.
  8492. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  8493. * @param string $style Style of rendering. Possible values are:
  8494. * <ul>
  8495. * <li>D or empty string: Draw (default).</li>
  8496. * <li>F: Fill.</li>
  8497. * <li>DF or FD: Draw and fill.</li>
  8498. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8499. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8500. * </ul>
  8501. * @param array $line_style Line style of polygon. Array with keys among the following:
  8502. * <ul>
  8503. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8504. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8505. * </ul>
  8506. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  8507. * @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).
  8508. * @param boolean $closed if true the polygon is closes, otherwise will remain open
  8509. * @access public
  8510. * @since 2.1.000 (2008-01-08)
  8511. */
  8512. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  8513. $nc = count($p); // number of coordinates
  8514. $np = $nc / 2; // number of points
  8515. if ($closed) {
  8516. // close polygon by adding the first 2 points at the end (one line)
  8517. for ($i = 0; $i < 4; ++$i) {
  8518. $p[$nc + $i] = $p[$i];
  8519. }
  8520. // copy style for the last added line
  8521. if (isset($line_style[0])) {
  8522. $line_style[$np] = $line_style[0];
  8523. }
  8524. $nc += 4;
  8525. }
  8526. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8527. $this->SetFillColorArray($fill_color);
  8528. }
  8529. switch ($style) {
  8530. case 'F': {
  8531. $line_style = array();
  8532. $op = 'f';
  8533. break;
  8534. }
  8535. case 'FD':
  8536. case 'DF': {
  8537. $op = 'B';
  8538. break;
  8539. }
  8540. case 'CNZ': {
  8541. $op = 'W n';
  8542. break;
  8543. }
  8544. case 'CEO': {
  8545. $op = 'W* n';
  8546. break;
  8547. }
  8548. default: {
  8549. $op = 'S';
  8550. break;
  8551. }
  8552. }
  8553. $draw = true;
  8554. if ($line_style) {
  8555. if (isset($line_style['all'])) {
  8556. $this->SetLineStyle($line_style['all']);
  8557. } else {
  8558. $draw = false;
  8559. if ($op == 'B') {
  8560. // draw fill
  8561. $op = 'f';
  8562. $this->_outPoint($p[0], $p[1]);
  8563. for ($i = 2; $i < $nc; $i = $i + 2) {
  8564. $this->_outLine($p[$i], $p[$i + 1]);
  8565. }
  8566. $this->_out($op);
  8567. }
  8568. // draw outline
  8569. $this->_outPoint($p[0], $p[1]);
  8570. for ($i = 2; $i < $nc; $i = $i + 2) {
  8571. $line_num = ($i / 2) - 1;
  8572. if (isset($line_style[$line_num])) {
  8573. if ($line_style[$line_num] != 0) {
  8574. if (is_array($line_style[$line_num])) {
  8575. $this->_out('S');
  8576. $this->SetLineStyle($line_style[$line_num]);
  8577. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  8578. $this->_outLine($p[$i], $p[$i + 1]);
  8579. $this->_out('S');
  8580. $this->_outPoint($p[$i], $p[$i + 1]);
  8581. } else {
  8582. $this->_outLine($p[$i], $p[$i + 1]);
  8583. }
  8584. }
  8585. } else {
  8586. $this->_outLine($p[$i], $p[$i + 1]);
  8587. }
  8588. }
  8589. $this->_out($op);
  8590. }
  8591. }
  8592. if ($draw) {
  8593. $this->_outPoint($p[0], $p[1]);
  8594. for ($i = 2; $i < $nc; $i = $i + 2) {
  8595. $this->_outLine($p[$i], $p[$i + 1]);
  8596. }
  8597. $this->_out($op);
  8598. }
  8599. }
  8600. /**
  8601. * Draws a regular polygon.
  8602. * @param float $x0 Abscissa of center point.
  8603. * @param float $y0 Ordinate of center point.
  8604. * @param float $r: Radius of inscribed circle.
  8605. * @param integer $ns Number of sides.
  8606. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  8607. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  8608. * @param string $style Style of rendering. Possible values are:
  8609. * <ul>
  8610. * <li>D or empty string: Draw (default).</li>
  8611. * <li>F: Fill.</li>
  8612. * <li>DF or FD: Draw and fill.</li>
  8613. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8614. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8615. * </ul>
  8616. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  8617. * <ul>
  8618. * <li>all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8619. * <li>0 to ($ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8620. * </ul>
  8621. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  8622. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  8623. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  8624. * <ul>
  8625. * <li>D or empty string: Draw (default).</li>
  8626. * <li>F: Fill.</li>
  8627. * <li>DF or FD: Draw and fill.</li>
  8628. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8629. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8630. * </ul>
  8631. * @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).
  8632. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  8633. * @access public
  8634. * @since 2.1.000 (2008-01-08)
  8635. */
  8636. 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()) {
  8637. if (3 > $ns) {
  8638. $ns = 3;
  8639. }
  8640. if ($draw_circle) {
  8641. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  8642. }
  8643. $p = array();
  8644. for ($i = 0; $i < $ns; ++$i) {
  8645. $a = $angle + ($i * 360 / $ns);
  8646. $a_rad = deg2rad((float) $a);
  8647. $p[] = $x0 + ($r * sin($a_rad));
  8648. $p[] = $y0 + ($r * cos($a_rad));
  8649. }
  8650. $this->Polygon($p, $style, $line_style, $fill_color);
  8651. }
  8652. /**
  8653. * Draws a star polygon
  8654. * @param float $x0 Abscissa of center point.
  8655. * @param float $y0 Ordinate of center point.
  8656. * @param float $r Radius of inscribed circle.
  8657. * @param integer $nv Number of vertices.
  8658. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  8659. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  8660. * @param boolean $draw_circle: Draw inscribed circle or not. Default value is false.
  8661. * @param string $style Style of rendering. Possible values are:
  8662. * <ul>
  8663. * <li>D or empty string: Draw (default).</li>
  8664. * <li>F: Fill.</li>
  8665. * <li>DF or FD: Draw and fill.</li>
  8666. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8667. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8668. * </ul>
  8669. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  8670. * <ul>
  8671. * <li>all: Line style of all sides. Array like for
  8672. * {@link SetLineStyle SetLineStyle}.</li>
  8673. * <li>0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8674. * </ul>
  8675. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  8676. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  8677. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  8678. * <ul>
  8679. * <li>D or empty string: Draw (default).</li>
  8680. * <li>F: Fill.</li>
  8681. * <li>DF or FD: Draw and fill.</li>
  8682. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8683. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8684. * </ul>
  8685. * @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).
  8686. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  8687. * @access public
  8688. * @since 2.1.000 (2008-01-08)
  8689. */
  8690. 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()) {
  8691. if ($nv < 2) {
  8692. $nv = 2;
  8693. }
  8694. if ($draw_circle) {
  8695. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  8696. }
  8697. $p2 = array();
  8698. $visited = array();
  8699. for ($i = 0; $i < $nv; ++$i) {
  8700. $a = $angle + ($i * 360 / $nv);
  8701. $a_rad = deg2rad((float) $a);
  8702. $p2[] = $x0 + ($r * sin($a_rad));
  8703. $p2[] = $y0 + ($r * cos($a_rad));
  8704. $visited[] = false;
  8705. }
  8706. $p = array();
  8707. $i = 0;
  8708. do {
  8709. $p[] = $p2[$i * 2];
  8710. $p[] = $p2[($i * 2) + 1];
  8711. $visited[$i] = true;
  8712. $i += $ng;
  8713. $i %= $nv;
  8714. } while (!$visited[$i]);
  8715. $this->Polygon($p, $style, $line_style, $fill_color);
  8716. }
  8717. /**
  8718. * Draws a rounded rectangle.
  8719. * @param float $x Abscissa of upper-left corner.
  8720. * @param float $y Ordinate of upper-left corner.
  8721. * @param float $w Width.
  8722. * @param float $h Height.
  8723. * @param float $r Radius of the rounded corners.
  8724. * @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").
  8725. * @param string $style Style of rendering. Possible values are:
  8726. * <ul>
  8727. * <li>D or empty string: Draw (default).</li>
  8728. * <li>F: Fill.</li>
  8729. * <li>DF or FD: Draw and fill.</li>
  8730. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8731. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8732. * </ul>
  8733. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8734. * @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).
  8735. * @access public
  8736. * @since 2.1.000 (2008-01-08)
  8737. */
  8738. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  8739. if ('0000' == $round_corner) { // Not rounded
  8740. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  8741. } else { // Rounded
  8742. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8743. $this->SetFillColorArray($fill_color);
  8744. }
  8745. switch ($style) {
  8746. case 'F': {
  8747. $border_style = array();
  8748. $op = 'f';
  8749. break;
  8750. }
  8751. case 'FD':
  8752. case 'DF': {
  8753. $op = 'B';
  8754. break;
  8755. }
  8756. case 'CNZ': {
  8757. $op = 'W n';
  8758. break;
  8759. }
  8760. case 'CEO': {
  8761. $op = 'W* n';
  8762. break;
  8763. }
  8764. default: {
  8765. $op = 'S';
  8766. break;
  8767. }
  8768. }
  8769. if ($border_style) {
  8770. $this->SetLineStyle($border_style);
  8771. }
  8772. $MyArc = 4 / 3 * (sqrt(2) - 1);
  8773. $this->_outPoint($x + $r, $y);
  8774. $xc = $x + $w - $r;
  8775. $yc = $y + $r;
  8776. $this->_outLine($xc, $y);
  8777. if ($round_corner[0]) {
  8778. $this->_outCurve($xc + ($r * $MyArc), $yc - $r, $xc + $r, $yc - ($r * $MyArc), $xc + $r, $yc);
  8779. } else {
  8780. $this->_outLine($x + $w, $y);
  8781. }
  8782. $xc = $x + $w - $r;
  8783. $yc = $y + $h - $r;
  8784. $this->_outLine($x + $w, $yc);
  8785. if ($round_corner[1]) {
  8786. $this->_outCurve($xc + $r, $yc + ($r * $MyArc), $xc + ($r * $MyArc), $yc + $r, $xc, $yc + $r);
  8787. } else {
  8788. $this->_outLine($x + $w, $y + $h);
  8789. }
  8790. $xc = $x + $r;
  8791. $yc = $y + $h - $r;
  8792. $this->_outLine($xc, $y + $h);
  8793. if ($round_corner[2]) {
  8794. $this->_outCurve($xc - ($r * $MyArc), $yc + $r, $xc - $r, $yc + ($r * $MyArc), $xc - $r, $yc);
  8795. } else {
  8796. $this->_outLine($x, $y + $h);
  8797. }
  8798. $xc = $x + $r;
  8799. $yc = $y + $r;
  8800. $this->_outLine($x, $yc);
  8801. if ($round_corner[3]) {
  8802. $this->_outCurve($xc - $r, $yc - ($r * $MyArc), $xc - ($r * $MyArc), $yc - $r, $xc, $yc - $r);
  8803. } else {
  8804. $this->_outLine($x, $y);
  8805. $this->_outLine($x + $r, $y);
  8806. }
  8807. $this->_out($op);
  8808. }
  8809. }
  8810. /**
  8811. * Draws a grahic arrow.
  8812. * @parameter float $x0 Abscissa of first point.
  8813. * @parameter float $y0 Ordinate of first point.
  8814. * @parameter float $x0 Abscissa of second point.
  8815. * @parameter float $y1 Ordinate of second point.
  8816. * @parameter int $head_style (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  8817. * @parameter float $arm_size length of arrowhead arms
  8818. * @parameter int $arm_angle angle between an arm and the shaft
  8819. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  8820. * @since 4.6.018 (2009-07-10)
  8821. */
  8822. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  8823. // getting arrow direction angle
  8824. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  8825. $dir_angle = rad2deg(atan2(($y0 - $y1), ($x0 - $x1)));
  8826. $sx1 = $x1;
  8827. $sy1 = $y1;
  8828. if ($head_style > 0) {
  8829. // calculate the stopping point for the arrow shaft
  8830. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos(deg2rad($dir_angle)));
  8831. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin(deg2rad($dir_angle)));
  8832. }
  8833. // main arrow line / shaft
  8834. $this->Line($x0, $y0, $sx1, $sy1);
  8835. // left arrowhead arm tip
  8836. $x2L = $x1 + ($arm_size * cos(deg2rad($dir_angle + $arm_angle)));
  8837. $y2L = $y1 + ($arm_size * sin(deg2rad($dir_angle + $arm_angle)));
  8838. // right arrowhead arm tip
  8839. $x2R = $x1 + ($arm_size * cos(deg2rad($dir_angle - $arm_angle)));
  8840. $y2R = $y1 + ($arm_size * sin(deg2rad($dir_angle - $arm_angle)));
  8841. $mode = 'D';
  8842. $style = array();
  8843. switch ($head_style) {
  8844. case 0: {
  8845. // draw only arrowhead arms
  8846. $mode = 'D';
  8847. $style = array(1, 1, 0);
  8848. break;
  8849. }
  8850. case 1: {
  8851. // draw closed arrowhead, but no fill
  8852. $mode = 'D';
  8853. break;
  8854. }
  8855. case 2: {
  8856. // closed and filled arrowhead
  8857. $mode = 'DF';
  8858. break;
  8859. }
  8860. case 3: {
  8861. // filled arrowhead
  8862. $mode = 'F';
  8863. break;
  8864. }
  8865. }
  8866. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  8867. }
  8868. // END GRAPHIC FUNCTIONS SECTION -----------------------
  8869. // BIDIRECTIONAL TEXT SECTION --------------------------
  8870. /**
  8871. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  8872. * @param string $str string to manipulate.
  8873. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  8874. * @return string
  8875. * @access protected
  8876. * @author Nicola Asuni
  8877. * @since 2.1.000 (2008-01-08)
  8878. */
  8879. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  8880. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($this->UTF8StringToArray($str), $str, $forcertl), $setbom);
  8881. }
  8882. /**
  8883. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  8884. * @param array $ta array of characters composing the string.
  8885. * @param string $str string to process
  8886. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  8887. * @return string
  8888. * @author Nicola Asuni
  8889. * @access protected
  8890. * @since 2.4.000 (2008-03-06)
  8891. */
  8892. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  8893. global $unicode, $unicode_mirror, $unicode_arlet, $laa_array, $diacritics;
  8894. // paragraph embedding level
  8895. $pel = 0;
  8896. // max level
  8897. $maxlevel = 0;
  8898. if ($this->empty_string($str)) {
  8899. // create string from array
  8900. $str = $this->UTF8ArrSubString($ta);
  8901. }
  8902. // check if string contains arabic text
  8903. if (preg_match(K_RE_PATTERN_ARABIC, $str)) {
  8904. $arabic = true;
  8905. } else {
  8906. $arabic = false;
  8907. }
  8908. // check if string contains RTL text
  8909. if (!($forcertl OR $arabic OR preg_match(K_RE_PATTERN_RTL, $str))) {
  8910. return $ta;
  8911. }
  8912. // get number of chars
  8913. $numchars = count($ta);
  8914. if ($forcertl == 'R') {
  8915. $pel = 1;
  8916. } elseif ($forcertl == 'L') {
  8917. $pel = 0;
  8918. } else {
  8919. // P2. In each paragraph, find the first character of type L, AL, or R.
  8920. // 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.
  8921. for ($i=0; $i < $numchars; ++$i) {
  8922. $type = $unicode[$ta[$i]];
  8923. if ($type == 'L') {
  8924. $pel = 0;
  8925. break;
  8926. } elseif (($type == 'AL') OR ($type == 'R')) {
  8927. $pel = 1;
  8928. break;
  8929. }
  8930. }
  8931. }
  8932. // Current Embedding Level
  8933. $cel = $pel;
  8934. // directional override status
  8935. $dos = 'N';
  8936. $remember = array();
  8937. // start-of-level-run
  8938. $sor = $pel % 2 ? 'R' : 'L';
  8939. $eor = $sor;
  8940. // Array of characters data
  8941. $chardata = Array();
  8942. // 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.
  8943. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  8944. for ($i=0; $i < $numchars; ++$i) {
  8945. if ($ta[$i] == K_RLE) {
  8946. // X2. With each RLE, compute the least greater odd embedding level.
  8947. // 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.
  8948. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8949. $next_level = $cel + ($cel % 2) + 1;
  8950. if ($next_level < 62) {
  8951. $remember[] = array('num' => K_RLE, 'cel' => $cel, 'dos' => $dos);
  8952. $cel = $next_level;
  8953. $dos = 'N';
  8954. $sor = $eor;
  8955. $eor = $cel % 2 ? 'R' : 'L';
  8956. }
  8957. } elseif ($ta[$i] == K_LRE) {
  8958. // X3. With each LRE, compute the least greater even embedding level.
  8959. // 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.
  8960. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8961. $next_level = $cel + 2 - ($cel % 2);
  8962. if ( $next_level < 62 ) {
  8963. $remember[] = array('num' => K_LRE, 'cel' => $cel, 'dos' => $dos);
  8964. $cel = $next_level;
  8965. $dos = 'N';
  8966. $sor = $eor;
  8967. $eor = $cel % 2 ? 'R' : 'L';
  8968. }
  8969. } elseif ($ta[$i] == K_RLO) {
  8970. // X4. With each RLO, compute the least greater odd embedding level.
  8971. // 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.
  8972. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8973. $next_level = $cel + ($cel % 2) + 1;
  8974. if ($next_level < 62) {
  8975. $remember[] = array('num' => K_RLO, 'cel' => $cel, 'dos' => $dos);
  8976. $cel = $next_level;
  8977. $dos = 'R';
  8978. $sor = $eor;
  8979. $eor = $cel % 2 ? 'R' : 'L';
  8980. }
  8981. } elseif ($ta[$i] == K_LRO) {
  8982. // X5. With each LRO, compute the least greater even embedding level.
  8983. // 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.
  8984. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8985. $next_level = $cel + 2 - ($cel % 2);
  8986. if ( $next_level < 62 ) {
  8987. $remember[] = array('num' => K_LRO, 'cel' => $cel, 'dos' => $dos);
  8988. $cel = $next_level;
  8989. $dos = 'L';
  8990. $sor = $eor;
  8991. $eor = $cel % 2 ? 'R' : 'L';
  8992. }
  8993. } elseif ($ta[$i] == K_PDF) {
  8994. // 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.
  8995. if (count($remember)) {
  8996. $last = count($remember ) - 1;
  8997. if (($remember[$last]['num'] == K_RLE) OR
  8998. ($remember[$last]['num'] == K_LRE) OR
  8999. ($remember[$last]['num'] == K_RLO) OR
  9000. ($remember[$last]['num'] == K_LRO)) {
  9001. $match = array_pop($remember);
  9002. $cel = $match['cel'];
  9003. $dos = $match['dos'];
  9004. $sor = $eor;
  9005. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  9006. }
  9007. }
  9008. } elseif (($ta[$i] != K_RLE) AND
  9009. ($ta[$i] != K_LRE) AND
  9010. ($ta[$i] != K_RLO) AND
  9011. ($ta[$i] != K_LRO) AND
  9012. ($ta[$i] != K_PDF)) {
  9013. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  9014. // a. Set the level of the current character to the current embedding level.
  9015. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  9016. if ($dos != 'N') {
  9017. $chardir = $dos;
  9018. } else {
  9019. if (isset($unicode[$ta[$i]])) {
  9020. $chardir = $unicode[$ta[$i]];
  9021. } else {
  9022. $chardir = 'L';
  9023. }
  9024. }
  9025. // stores string characters and other information
  9026. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  9027. }
  9028. } // end for each char
  9029. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  9030. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  9031. // 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.
  9032. // 3.3.3 Resolving Weak Types
  9033. // 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.
  9034. // Nonspacing marks are now resolved based on the previous characters.
  9035. $numchars = count($chardata);
  9036. // 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.
  9037. $prevlevel = -1; // track level changes
  9038. $levcount = 0; // counts consecutive chars at the same level
  9039. for ($i=0; $i < $numchars; ++$i) {
  9040. if ($chardata[$i]['type'] == 'NSM') {
  9041. if ($levcount) {
  9042. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9043. } elseif ($i > 0) {
  9044. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  9045. }
  9046. }
  9047. if ($chardata[$i]['level'] != $prevlevel) {
  9048. $levcount = 0;
  9049. } else {
  9050. ++$levcount;
  9051. }
  9052. $prevlevel = $chardata[$i]['level'];
  9053. }
  9054. // 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.
  9055. $prevlevel = -1;
  9056. $levcount = 0;
  9057. for ($i=0; $i < $numchars; ++$i) {
  9058. if ($chardata[$i]['char'] == 'EN') {
  9059. for ($j=$levcount; $j >= 0; $j--) {
  9060. if ($chardata[$j]['type'] == 'AL') {
  9061. $chardata[$i]['type'] = 'AN';
  9062. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  9063. break;
  9064. }
  9065. }
  9066. }
  9067. if ($chardata[$i]['level'] != $prevlevel) {
  9068. $levcount = 0;
  9069. } else {
  9070. ++$levcount;
  9071. }
  9072. $prevlevel = $chardata[$i]['level'];
  9073. }
  9074. // W3. Change all ALs to R.
  9075. for ($i=0; $i < $numchars; ++$i) {
  9076. if ($chardata[$i]['type'] == 'AL') {
  9077. $chardata[$i]['type'] = 'R';
  9078. }
  9079. }
  9080. // 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.
  9081. $prevlevel = -1;
  9082. $levcount = 0;
  9083. for ($i=0; $i < $numchars; ++$i) {
  9084. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9085. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  9086. $chardata[$i]['type'] = 'EN';
  9087. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  9088. $chardata[$i]['type'] = 'EN';
  9089. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  9090. $chardata[$i]['type'] = 'AN';
  9091. }
  9092. }
  9093. if ($chardata[$i]['level'] != $prevlevel) {
  9094. $levcount = 0;
  9095. } else {
  9096. ++$levcount;
  9097. }
  9098. $prevlevel = $chardata[$i]['level'];
  9099. }
  9100. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  9101. $prevlevel = -1;
  9102. $levcount = 0;
  9103. for ($i=0; $i < $numchars; ++$i) {
  9104. if ($chardata[$i]['type'] == 'ET') {
  9105. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  9106. $chardata[$i]['type'] = 'EN';
  9107. } else {
  9108. $j = $i+1;
  9109. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  9110. if ($chardata[$j]['type'] == 'EN') {
  9111. $chardata[$i]['type'] = 'EN';
  9112. break;
  9113. } elseif ($chardata[$j]['type'] != 'ET') {
  9114. break;
  9115. }
  9116. ++$j;
  9117. }
  9118. }
  9119. }
  9120. if ($chardata[$i]['level'] != $prevlevel) {
  9121. $levcount = 0;
  9122. } else {
  9123. ++$levcount;
  9124. }
  9125. $prevlevel = $chardata[$i]['level'];
  9126. }
  9127. // W6. Otherwise, separators and terminators change to Other Neutral.
  9128. $prevlevel = -1;
  9129. $levcount = 0;
  9130. for ($i=0; $i < $numchars; ++$i) {
  9131. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  9132. $chardata[$i]['type'] = 'ON';
  9133. }
  9134. if ($chardata[$i]['level'] != $prevlevel) {
  9135. $levcount = 0;
  9136. } else {
  9137. ++$levcount;
  9138. }
  9139. $prevlevel = $chardata[$i]['level'];
  9140. }
  9141. //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.
  9142. $prevlevel = -1;
  9143. $levcount = 0;
  9144. for ($i=0; $i < $numchars; ++$i) {
  9145. if ($chardata[$i]['char'] == 'EN') {
  9146. for ($j=$levcount; $j >= 0; $j--) {
  9147. if ($chardata[$j]['type'] == 'L') {
  9148. $chardata[$i]['type'] = 'L';
  9149. } elseif ($chardata[$j]['type'] == 'R') {
  9150. break;
  9151. }
  9152. }
  9153. }
  9154. if ($chardata[$i]['level'] != $prevlevel) {
  9155. $levcount = 0;
  9156. } else {
  9157. ++$levcount;
  9158. }
  9159. $prevlevel = $chardata[$i]['level'];
  9160. }
  9161. // 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.
  9162. $prevlevel = -1;
  9163. $levcount = 0;
  9164. for ($i=0; $i < $numchars; ++$i) {
  9165. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9166. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  9167. $chardata[$i]['type'] = 'L';
  9168. } elseif (($chardata[$i]['type'] == 'N') AND
  9169. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  9170. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  9171. $chardata[$i]['type'] = 'R';
  9172. } elseif ($chardata[$i]['type'] == 'N') {
  9173. // N2. Any remaining neutrals take the embedding direction
  9174. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9175. }
  9176. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9177. // first char
  9178. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  9179. $chardata[$i]['type'] = 'L';
  9180. } elseif (($chardata[$i]['type'] == 'N') AND
  9181. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  9182. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  9183. $chardata[$i]['type'] = 'R';
  9184. } elseif ($chardata[$i]['type'] == 'N') {
  9185. // N2. Any remaining neutrals take the embedding direction
  9186. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9187. }
  9188. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  9189. //last char
  9190. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  9191. $chardata[$i]['type'] = 'L';
  9192. } elseif (($chardata[$i]['type'] == 'N') AND
  9193. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  9194. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  9195. $chardata[$i]['type'] = 'R';
  9196. } elseif ($chardata[$i]['type'] == 'N') {
  9197. // N2. Any remaining neutrals take the embedding direction
  9198. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9199. }
  9200. } elseif ($chardata[$i]['type'] == 'N') {
  9201. // N2. Any remaining neutrals take the embedding direction
  9202. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9203. }
  9204. if ($chardata[$i]['level'] != $prevlevel) {
  9205. $levcount = 0;
  9206. } else {
  9207. ++$levcount;
  9208. }
  9209. $prevlevel = $chardata[$i]['level'];
  9210. }
  9211. // 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.
  9212. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  9213. for ($i=0; $i < $numchars; ++$i) {
  9214. $odd = $chardata[$i]['level'] % 2;
  9215. if ($odd) {
  9216. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  9217. $chardata[$i]['level'] += 1;
  9218. }
  9219. } else {
  9220. if ($chardata[$i]['type'] == 'R') {
  9221. $chardata[$i]['level'] += 1;
  9222. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  9223. $chardata[$i]['level'] += 2;
  9224. }
  9225. }
  9226. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  9227. }
  9228. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  9229. // 1. Segment separators,
  9230. // 2. Paragraph separators,
  9231. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  9232. // 4. Any sequence of white space characters at the end of the line.
  9233. for ($i=0; $i < $numchars; ++$i) {
  9234. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  9235. $chardata[$i]['level'] = $pel;
  9236. } elseif ($chardata[$i]['type'] == 'WS') {
  9237. $j = $i+1;
  9238. while ($j < $numchars) {
  9239. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  9240. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  9241. $chardata[$i]['level'] = $pel;
  9242. break;
  9243. } elseif ($chardata[$j]['type'] != 'WS') {
  9244. break;
  9245. }
  9246. ++$j;
  9247. }
  9248. }
  9249. }
  9250. // Arabic Shaping
  9251. // 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.
  9252. if ($arabic) {
  9253. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  9254. $alfletter = array(1570,1571,1573,1575);
  9255. $chardata2 = $chardata;
  9256. $laaletter = false;
  9257. $charAL = array();
  9258. $x = 0;
  9259. for ($i=0; $i < $numchars; ++$i) {
  9260. if (($unicode[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  9261. $charAL[$x] = $chardata[$i];
  9262. $charAL[$x]['i'] = $i;
  9263. $chardata[$i]['x'] = $x;
  9264. ++$x;
  9265. }
  9266. }
  9267. $numAL = $x;
  9268. for ($i=0; $i < $numchars; ++$i) {
  9269. $thischar = $chardata[$i];
  9270. if ($i > 0) {
  9271. $prevchar = $chardata[($i-1)];
  9272. } else {
  9273. $prevchar = false;
  9274. }
  9275. if (($i+1) < $numchars) {
  9276. $nextchar = $chardata[($i+1)];
  9277. } else {
  9278. $nextchar = false;
  9279. }
  9280. if ($unicode[$thischar['char']] == 'AL') {
  9281. $x = $thischar['x'];
  9282. if ($x > 0) {
  9283. $prevchar = $charAL[($x-1)];
  9284. } else {
  9285. $prevchar = false;
  9286. }
  9287. if (($x+1) < $numAL) {
  9288. $nextchar = $charAL[($x+1)];
  9289. } else {
  9290. $nextchar = false;
  9291. }
  9292. // if laa letter
  9293. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  9294. $arabicarr = $laa_array;
  9295. $laaletter = true;
  9296. if ($x > 1) {
  9297. $prevchar = $charAL[($x-2)];
  9298. } else {
  9299. $prevchar = false;
  9300. }
  9301. } else {
  9302. $arabicarr = $unicode_arlet;
  9303. $laaletter = false;
  9304. }
  9305. if (($prevchar !== false) AND ($nextchar !== false) AND
  9306. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  9307. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  9308. ($prevchar['type'] == $thischar['type']) AND
  9309. ($nextchar['type'] == $thischar['type']) AND
  9310. ($nextchar['char'] != 1567)) {
  9311. if (in_array($prevchar['char'], $endedletter)) {
  9312. if (isset($arabicarr[$thischar['char']][2])) {
  9313. // initial
  9314. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  9315. }
  9316. } else {
  9317. if (isset($arabicarr[$thischar['char']][3])) {
  9318. // medial
  9319. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  9320. }
  9321. }
  9322. } elseif (($nextchar !== false) AND
  9323. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  9324. ($nextchar['type'] == $thischar['type']) AND
  9325. ($nextchar['char'] != 1567)) {
  9326. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  9327. // initial
  9328. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  9329. }
  9330. } elseif ((($prevchar !== false) AND
  9331. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  9332. ($prevchar['type'] == $thischar['type'])) OR
  9333. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  9334. // final
  9335. if (($i > 1) AND ($thischar['char'] == 1607) AND
  9336. ($chardata[$i-1]['char'] == 1604) AND
  9337. ($chardata[$i-2]['char'] == 1604)) {
  9338. //Allah Word
  9339. // mark characters to delete with false
  9340. $chardata2[$i-2]['char'] = false;
  9341. $chardata2[$i-1]['char'] = false;
  9342. $chardata2[$i]['char'] = 65010;
  9343. } else {
  9344. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  9345. if (isset($arabicarr[$thischar['char']][0])) {
  9346. // isolated
  9347. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  9348. }
  9349. } else {
  9350. if (isset($arabicarr[$thischar['char']][1])) {
  9351. // final
  9352. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  9353. }
  9354. }
  9355. }
  9356. } elseif (isset($arabicarr[$thischar['char']][0])) {
  9357. // isolated
  9358. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  9359. }
  9360. // if laa letter
  9361. if ($laaletter) {
  9362. // mark characters to delete with false
  9363. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  9364. }
  9365. } // end if AL (Arabic Letter)
  9366. } // end for each char
  9367. /*
  9368. * Combining characters that can occur with Shadda (0651 HEX, 1617 DEC) are placed in UE586-UE594.
  9369. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  9370. */
  9371. $cw = &$this->CurrentFont['cw'];
  9372. for ($i = 0; $i < ($numchars-1); ++$i) {
  9373. if (($chardata2[$i]['char'] == 1617) AND (isset($diacritics[($chardata2[$i+1]['char'])]))) {
  9374. // check if the subtitution font is defined on current font
  9375. if (isset($cw[($diacritics[($chardata2[$i+1]['char'])])])) {
  9376. $chardata2[$i]['char'] = false;
  9377. $chardata2[$i+1]['char'] = $diacritics[($chardata2[$i+1]['char'])];
  9378. }
  9379. }
  9380. }
  9381. // remove marked characters
  9382. foreach ($chardata2 as $key => $value) {
  9383. if ($value['char'] === false) {
  9384. unset($chardata2[$key]);
  9385. }
  9386. }
  9387. $chardata = array_values($chardata2);
  9388. $numchars = count($chardata);
  9389. unset($chardata2);
  9390. unset($arabicarr);
  9391. unset($laaletter);
  9392. unset($charAL);
  9393. }
  9394. // 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.
  9395. for ($j=$maxlevel; $j > 0; $j--) {
  9396. $ordarray = Array();
  9397. $revarr = Array();
  9398. $onlevel = false;
  9399. for ($i=0; $i < $numchars; ++$i) {
  9400. if ($chardata[$i]['level'] >= $j) {
  9401. $onlevel = true;
  9402. if (isset($unicode_mirror[$chardata[$i]['char']])) {
  9403. // 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.
  9404. $chardata[$i]['char'] = $unicode_mirror[$chardata[$i]['char']];
  9405. }
  9406. $revarr[] = $chardata[$i];
  9407. } else {
  9408. if ($onlevel) {
  9409. $revarr = array_reverse($revarr);
  9410. $ordarray = array_merge($ordarray, $revarr);
  9411. $revarr = Array();
  9412. $onlevel = false;
  9413. }
  9414. $ordarray[] = $chardata[$i];
  9415. }
  9416. }
  9417. if ($onlevel) {
  9418. $revarr = array_reverse($revarr);
  9419. $ordarray = array_merge($ordarray, $revarr);
  9420. }
  9421. $chardata = $ordarray;
  9422. }
  9423. $ordarray = array();
  9424. for ($i=0; $i < $numchars; ++$i) {
  9425. $ordarray[] = $chardata[$i]['char'];
  9426. }
  9427. return $ordarray;
  9428. }
  9429. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  9430. /*
  9431. * Adds a bookmark.
  9432. * @param string $txt bookmark description.
  9433. * @param int $level bookmark level (minimum value is 0).
  9434. * @param float $y Ordinate of the boorkmark position (default = -1 = current position).
  9435. * @param int $page target page number (leave empty for current page).
  9436. * @access public
  9437. * @author Olivier Plathey, Nicola Asuni
  9438. * @since 2.1.002 (2008-02-12)
  9439. */
  9440. public function Bookmark($txt, $level=0, $y=-1, $page='') {
  9441. if ($level < 0) {
  9442. $level = 0;
  9443. }
  9444. if (isset($this->outlines[0])) {
  9445. $lastoutline = end($this->outlines);
  9446. $maxlevel = $lastoutline['l'] + 1;
  9447. } else {
  9448. $maxlevel = 0;
  9449. }
  9450. if ($level > $maxlevel) {
  9451. $level = $maxlevel;
  9452. }
  9453. if ($y == -1) {
  9454. $y = $this->GetY();
  9455. }
  9456. if (empty($page)) {
  9457. $page = $this->PageNo();
  9458. }
  9459. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page);
  9460. }
  9461. /*
  9462. * Create a bookmark PDF string.
  9463. * @access protected
  9464. * @author Olivier Plathey, Nicola Asuni
  9465. * @since 2.1.002 (2008-02-12)
  9466. */
  9467. protected function _putbookmarks() {
  9468. $nb = count($this->outlines);
  9469. if ($nb == 0) {
  9470. return;
  9471. }
  9472. // get sorting columns
  9473. $outline_p = array();
  9474. $outline_y = array();
  9475. foreach ($this->outlines as $key => $row) {
  9476. $outline_p[$key] = $row['p'];
  9477. $outline_k[$key] = $key;
  9478. }
  9479. // sort outlines by page and original position
  9480. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  9481. $lru = array();
  9482. $level = 0;
  9483. foreach ($this->outlines as $i => $o) {
  9484. if ($o['l'] > 0) {
  9485. $parent = $lru[($o['l'] - 1)];
  9486. //Set parent and last pointers
  9487. $this->outlines[$i]['parent'] = $parent;
  9488. $this->outlines[$parent]['last'] = $i;
  9489. if ($o['l'] > $level) {
  9490. //Level increasing: set first pointer
  9491. $this->outlines[$parent]['first'] = $i;
  9492. }
  9493. } else {
  9494. $this->outlines[$i]['parent'] = $nb;
  9495. }
  9496. if (($o['l'] <= $level) AND ($i > 0)) {
  9497. //Set prev and next pointers
  9498. $prev = $lru[$o['l']];
  9499. $this->outlines[$prev]['next'] = $i;
  9500. $this->outlines[$i]['prev'] = $prev;
  9501. }
  9502. $lru[$o['l']] = $i;
  9503. $level = $o['l'];
  9504. }
  9505. //Outline items
  9506. $n = $this->n + 1;
  9507. foreach ($this->outlines as $i => $o) {
  9508. $this->_newobj();
  9509. $this->_out('<</Title '.$this->_textstring($o['t']));
  9510. $this->_out('/Parent '.($n + $o['parent']).' 0 R');
  9511. if (isset($o['prev']))
  9512. $this->_out('/Prev '.($n + $o['prev']).' 0 R');
  9513. if (isset($o['next']))
  9514. $this->_out('/Next '.($n + $o['next']).' 0 R');
  9515. if (isset($o['first']))
  9516. $this->_out('/First '.($n + $o['first']).' 0 R');
  9517. if (isset($o['last']))
  9518. $this->_out('/Last '.($n + $o['last']).' 0 R');
  9519. $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $o['p'])), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k))));
  9520. $this->_out('/Count 0>>');
  9521. $this->_out('endobj');
  9522. }
  9523. //Outline root
  9524. $this->_newobj();
  9525. $this->OutlineRoot = $this->n;
  9526. $this->_out('<</Type /Outlines /First '.$n.' 0 R');
  9527. $this->_out('/Last '.($n + $lru[0]).' 0 R>>');
  9528. $this->_out('endobj');
  9529. }
  9530. // --- JAVASCRIPT ------------------------------------------------------
  9531. /*
  9532. * Adds a javascript
  9533. * @param string $script Javascript code
  9534. * @access public
  9535. * @author Johannes G�ntert, Nicola Asuni
  9536. * @since 2.1.002 (2008-02-12)
  9537. */
  9538. public function IncludeJS($script) {
  9539. $this->javascript .= $script;
  9540. }
  9541. /*
  9542. * Adds a javascript object and return object ID
  9543. * @param string $script Javascript code
  9544. * @param boolean $onload if true executes this object when opening the document
  9545. * @return int internal object ID
  9546. * @access public
  9547. * @author Nicola Asuni
  9548. * @since 4.8.000 (2009-09-07)
  9549. */
  9550. public function addJavascriptObject($script, $onload=false) {
  9551. ++$this->js_obj_id;
  9552. $this->js_objects[$this->js_obj_id] = array('js' => $script, 'onload' => $onload);
  9553. return $this->js_obj_id;
  9554. }
  9555. /*
  9556. * Create a javascript PDF string.
  9557. * @access protected
  9558. * @author Johannes G�ntert, Nicola Asuni
  9559. * @since 2.1.002 (2008-02-12)
  9560. */
  9561. protected function _putjavascript() {
  9562. if (empty($this->javascript) AND empty($this->js_objects)) {
  9563. return;
  9564. }
  9565. if (strpos($this->javascript, 'this.addField') > 0) {
  9566. if (!$this->ur) {
  9567. //$this->setUserRights();
  9568. }
  9569. // the following two lines are used to avoid form fields duplication after saving
  9570. // The addField method only works on Acrobat Writer, unless the document is signed with Adobe private key (UR3)
  9571. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  9572. $jsb = "getField('tcpdfdocsaved').value='saved';";
  9573. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  9574. }
  9575. $this->n_js = $this->_newobj();
  9576. $this->_out('<<');
  9577. $this->_out('/Names [');
  9578. if (!empty($this->javascript)) {
  9579. $this->_out('(EmbeddedJS) '.($this->n + 1).' 0 R');
  9580. }
  9581. if (!empty($this->js_objects)) {
  9582. foreach ($this->js_objects as $key => $val) {
  9583. if ($val['onload']) {
  9584. $this->_out('(JS'.$key.') '.$key.' 0 R');
  9585. }
  9586. }
  9587. }
  9588. $this->_out(']');
  9589. $this->_out('>>');
  9590. $this->_out('endobj');
  9591. // default Javascript object
  9592. if (!empty($this->javascript)) {
  9593. $this->_newobj();
  9594. $this->_out('<<');
  9595. $this->_out('/S /JavaScript');
  9596. $this->_out('/JS '.$this->_textstring($this->javascript));
  9597. $this->_out('>>');
  9598. $this->_out('endobj');
  9599. }
  9600. // additional Javascript objects
  9601. if (!empty($this->js_objects)) {
  9602. foreach ($this->js_objects as $key => $val) {
  9603. $this->offsets[$key] = $this->bufferlen;
  9604. $this->_out($key.' 0 obj');
  9605. $this->_out('<<');
  9606. $this->_out('/S /JavaScript');
  9607. $this->_out('/JS '.$this->_textstring($val['js']));
  9608. $this->_out('>>');
  9609. $this->_out('endobj');
  9610. }
  9611. }
  9612. }
  9613. /*
  9614. * Convert color to javascript color.
  9615. * @param string $color color name or #RRGGBB
  9616. * @access protected
  9617. * @author Denis Van Nuffelen, Nicola Asuni
  9618. * @since 2.1.002 (2008-02-12)
  9619. */
  9620. protected function _JScolor($color) {
  9621. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  9622. if (substr($color,0,1) == '#') {
  9623. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  9624. }
  9625. if (!in_array($color,$aColors)) {
  9626. $this->Error('Invalid color: '.$color);
  9627. }
  9628. return 'color.'.$color;
  9629. }
  9630. /*
  9631. * Adds a javascript form field.
  9632. * @param string $type field type
  9633. * @param string $name field name
  9634. * @param int $x horizontal position
  9635. * @param int $y vertical position
  9636. * @param int $w width
  9637. * @param int $h height
  9638. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  9639. * @access protected
  9640. * @author Denis Van Nuffelen, Nicola Asuni
  9641. * @since 2.1.002 (2008-02-12)
  9642. */
  9643. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  9644. if ($this->rtl) {
  9645. $x = $x - $w;
  9646. }
  9647. // the followind avoid fields duplication after saving the document
  9648. $this->javascript .= "if(getField('tcpdfdocsaved').value != 'saved') {";
  9649. $k = $this->k;
  9650. $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";
  9651. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  9652. while (list($key, $val) = each($prop)) {
  9653. if (strcmp(substr($key, -5), 'Color') == 0) {
  9654. $val = $this->_JScolor($val);
  9655. } else {
  9656. $val = "'".$val."'";
  9657. }
  9658. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  9659. }
  9660. if ($this->rtl) {
  9661. $this->x -= $w;
  9662. } else {
  9663. $this->x += $w;
  9664. }
  9665. $this->javascript .= '}';
  9666. }
  9667. // --- FORM FIELDS -----------------------------------------------------
  9668. /*
  9669. * Convert JavaScript form fields properties array to Annotation Properties array.
  9670. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  9671. * @return array of annotation properties
  9672. * @access protected
  9673. * @author Nicola Asuni
  9674. * @since 4.8.000 (2009-09-06)
  9675. */
  9676. protected function getAnnotOptFromJSProp($prop) {
  9677. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  9678. // the annotation options area lready defined
  9679. return $prop['aopt'];
  9680. }
  9681. $opt = array(); // value to be returned
  9682. // alignment: Controls how the text is laid out within the text field.
  9683. if (isset($prop['alignment'])) {
  9684. switch ($prop['alignment']) {
  9685. case 'left': {
  9686. $opt['q'] = 0;
  9687. break;
  9688. }
  9689. case 'center': {
  9690. $opt['q'] = 1;
  9691. break;
  9692. }
  9693. case 'right': {
  9694. $opt['q'] = 2;
  9695. break;
  9696. }
  9697. default: {
  9698. $opt['q'] = ($this->rtl)?2:0;
  9699. break;
  9700. }
  9701. }
  9702. }
  9703. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  9704. if (isset($prop['lineWidth'])) {
  9705. $linewidth = intval($prop['lineWidth']);
  9706. } else {
  9707. $linewidth = 1;
  9708. }
  9709. // borderStyle: The border style for a field.
  9710. if (isset($prop['borderStyle'])) {
  9711. switch ($prop['borderStyle']) {
  9712. case 'border.d':
  9713. case 'dashed': {
  9714. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  9715. $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
  9716. break;
  9717. }
  9718. case 'border.b':
  9719. case 'beveled': {
  9720. $opt['border'] = array(0, 0, $linewidth);
  9721. $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
  9722. break;
  9723. }
  9724. case 'border.i':
  9725. case 'inset': {
  9726. $opt['border'] = array(0, 0, $linewidth);
  9727. $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
  9728. break;
  9729. }
  9730. case 'border.u':
  9731. case 'underline': {
  9732. $opt['border'] = array(0, 0, $linewidth);
  9733. $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
  9734. break;
  9735. }
  9736. default:
  9737. case 'border.s':
  9738. case 'solid': {
  9739. $opt['border'] = array(0, 0, $linewidth);
  9740. $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
  9741. break;
  9742. }
  9743. }
  9744. }
  9745. if (isset($prop['border']) AND is_array($prop['border'])) {
  9746. $opt['border'] = $prop['border'];
  9747. }
  9748. if (!isset($opt['mk'])) {
  9749. $opt['mk'] = array();
  9750. }
  9751. if (!isset($opt['mk']['if'])) {
  9752. $opt['mk']['if'] = array();
  9753. }
  9754. $opt['mk']['if']['a'] = array(0.5, 0.5);
  9755. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  9756. if (isset($prop['buttonAlignX'])) {
  9757. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  9758. }
  9759. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  9760. if (isset($prop['buttonAlignY'])) {
  9761. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  9762. }
  9763. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  9764. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  9765. $opt['mk']['if']['fb'] = true;
  9766. }
  9767. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  9768. if (isset($prop['buttonScaleHow'])) {
  9769. switch ($prop['buttonScaleHow']) {
  9770. case 'scaleHow.proportional': {
  9771. $opt['mk']['if']['s'] = 'P';
  9772. break;
  9773. }
  9774. case 'scaleHow.anamorphic': {
  9775. $opt['mk']['if']['s'] = 'A';
  9776. break;
  9777. }
  9778. }
  9779. }
  9780. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  9781. if (isset($prop['buttonScaleWhen'])) {
  9782. switch ($prop['buttonScaleWhen']) {
  9783. case 'scaleWhen.always': {
  9784. $opt['mk']['if']['sw'] = 'A';
  9785. break;
  9786. }
  9787. case 'scaleWhen.never': {
  9788. $opt['mk']['if']['sw'] = 'N';
  9789. break;
  9790. }
  9791. case 'scaleWhen.tooBig': {
  9792. $opt['mk']['if']['sw'] = 'B';
  9793. break;
  9794. }
  9795. case 'scaleWhen.tooSmall': {
  9796. $opt['mk']['if']['sw'] = 'S';
  9797. break;
  9798. }
  9799. }
  9800. }
  9801. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  9802. if (isset($prop['buttonPosition'])) {
  9803. switch ($prop['buttonPosition']) {
  9804. case 0:
  9805. case 'position.textOnly': {
  9806. $opt['mk']['tp'] = 0;
  9807. break;
  9808. }
  9809. case 1:
  9810. case 'position.iconOnly': {
  9811. $opt['mk']['tp'] = 1;
  9812. break;
  9813. }
  9814. case 2:
  9815. case 'position.iconTextV': {
  9816. $opt['mk']['tp'] = 2;
  9817. break;
  9818. }
  9819. case 3:
  9820. case 'position.textIconV': {
  9821. $opt['mk']['tp'] = 3;
  9822. break;
  9823. }
  9824. case 4:
  9825. case 'position.iconTextH': {
  9826. $opt['mk']['tp'] = 4;
  9827. break;
  9828. }
  9829. case 5:
  9830. case 'position.textIconH': {
  9831. $opt['mk']['tp'] = 5;
  9832. break;
  9833. }
  9834. case 6:
  9835. case 'position.overlay': {
  9836. $opt['mk']['tp'] = 6;
  9837. break;
  9838. }
  9839. }
  9840. }
  9841. // fillColor: Specifies the background color for a field.
  9842. if (isset($prop['fillColor'])) {
  9843. if (is_array($prop['fillColor'])) {
  9844. $opt['mk']['bg'] = $prop['fillColor'];
  9845. } else {
  9846. $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
  9847. }
  9848. }
  9849. // strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
  9850. if (isset($prop['strokeColor'])) {
  9851. if (is_array($prop['strokeColor'])) {
  9852. $opt['mk']['bc'] = $prop['strokeColor'];
  9853. } else {
  9854. $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
  9855. }
  9856. }
  9857. // rotation: The rotation of a widget in counterclockwise increments.
  9858. if (isset($prop['rotation'])) {
  9859. $opt['mk']['r'] = $prop['rotation'];
  9860. }
  9861. // charLimit: Limits the number of characters that a user can type into a text field.
  9862. if (isset($prop['charLimit'])) {
  9863. $opt['maxlen'] = intval($prop['charLimit']);
  9864. }
  9865. if (!isset($ff)) {
  9866. $ff = 0;
  9867. }
  9868. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  9869. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  9870. $ff += 1 << 0;
  9871. }
  9872. // required: Specifies whether a field requires a value.
  9873. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  9874. $ff += 1 << 1;
  9875. }
  9876. // multiline: Controls how text is wrapped within the field.
  9877. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  9878. $ff += 1 << 12;
  9879. }
  9880. // password: Specifies whether the field should display asterisks when data is entered in the field.
  9881. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  9882. $ff += 1 << 13;
  9883. }
  9884. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  9885. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  9886. $ff += 1 << 14;
  9887. }
  9888. // Radio: If set, the field is a set of radio buttons.
  9889. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  9890. $ff += 1 << 15;
  9891. }
  9892. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  9893. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  9894. $ff += 1 << 16;
  9895. }
  9896. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  9897. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  9898. $ff += 1 << 17;
  9899. }
  9900. // editable: Controls whether a combo box is editable.
  9901. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  9902. $ff += 1 << 18;
  9903. }
  9904. // Sort: If set, the field's option items shall be sorted alphabetically.
  9905. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  9906. $ff += 1 << 19;
  9907. }
  9908. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  9909. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  9910. $ff += 1 << 20;
  9911. }
  9912. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  9913. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  9914. $ff += 1 << 21;
  9915. }
  9916. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  9917. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  9918. $ff += 1 << 22;
  9919. }
  9920. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  9921. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  9922. $ff += 1 << 23;
  9923. }
  9924. // comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
  9925. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  9926. $ff += 1 << 24;
  9927. }
  9928. // radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
  9929. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  9930. $ff += 1 << 25;
  9931. }
  9932. // richText: If true, the field allows rich text formatting.
  9933. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  9934. $ff += 1 << 25;
  9935. }
  9936. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  9937. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  9938. $ff += 1 << 26;
  9939. }
  9940. $opt['ff'] = $ff;
  9941. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  9942. if (isset($prop['defaultValue'])) {
  9943. $opt['dv'] = $prop['defaultValue'];
  9944. }
  9945. $f = 4; // default value for annotation flags
  9946. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  9947. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  9948. $f += 1 << 6;
  9949. }
  9950. // display: Controls whether the field is hidden or visible on screen and in print.
  9951. if (isset($prop['display'])) {
  9952. if ($prop['display'] == 'display.visible') {
  9953. //
  9954. } elseif ($prop['display'] == 'display.hidden') {
  9955. $f += 1 << 1;
  9956. } elseif ($prop['display'] == 'display.noPrint') {
  9957. $f -= 1 << 2;
  9958. } elseif ($prop['display'] == 'display.noView') {
  9959. $f += 1 << 5;
  9960. }
  9961. }
  9962. $opt['f'] = $f;
  9963. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  9964. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  9965. $opt['i'] = $prop['currentValueIndices'];
  9966. }
  9967. // value: The value of the field data that the user has entered.
  9968. if (isset($prop['value'])) {
  9969. if (is_array($prop['value'])) {
  9970. $opt['opt'] = array();
  9971. foreach ($prop['value'] AS $key => $optval) {
  9972. // exportValues: An array of strings representing the export values for the field.
  9973. if (isset($prop['exportValues'][$key])) {
  9974. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  9975. } else {
  9976. $opt['opt'][$key] = $prop['value'][$key];
  9977. }
  9978. }
  9979. } else {
  9980. $opt['v'] = $prop['value'];
  9981. }
  9982. }
  9983. // richValue: This property specifies the text contents and formatting of a rich text field.
  9984. if (isset($prop['richValue'])) {
  9985. $opt['rv'] = $prop['richValue'];
  9986. }
  9987. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  9988. if (isset($prop['submitName'])) {
  9989. $opt['tm'] = $prop['submitName'];
  9990. }
  9991. // name: Fully qualified field name.
  9992. if (isset($prop['name'])) {
  9993. $opt['t'] = $prop['name'];
  9994. }
  9995. // userName: The user name (short description string) of the field.
  9996. if (isset($prop['userName'])) {
  9997. $opt['tu'] = $prop['userName'];
  9998. }
  9999. // highlight: Defines how a button reacts when a user clicks it.
  10000. if (isset($prop['highlight'])) {
  10001. switch ($prop['highlight']) {
  10002. case 'none':
  10003. case 'highlight.n': {
  10004. $opt['h'] = 'N';
  10005. break;
  10006. }
  10007. case 'invert':
  10008. case 'highlight.i': {
  10009. $opt['h'] = 'i';
  10010. break;
  10011. }
  10012. case 'push':
  10013. case 'highlight.p': {
  10014. $opt['h'] = 'P';
  10015. break;
  10016. }
  10017. case 'outline':
  10018. case 'highlight.o': {
  10019. $opt['h'] = 'O';
  10020. break;
  10021. }
  10022. }
  10023. }
  10024. // Unsupported options:
  10025. // - calcOrderIndex: Changes the calculation order of fields in the document.
  10026. // - delay: Delays the redrawing of a field's appearance.
  10027. // - defaultStyle: This property defines the default style attributes for the form field.
  10028. // - style: Allows the user to set the glyph style of a check box or radio button.
  10029. // - textColor, textFont, textSize
  10030. return $opt;
  10031. }
  10032. /*
  10033. * Set default properties for form fields.
  10034. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10035. * @access public
  10036. * @author Nicola Asuni
  10037. * @since 4.8.000 (2009-09-06)
  10038. */
  10039. public function setFormDefaultProp($prop=array()) {
  10040. $this->default_form_prop = $prop;
  10041. }
  10042. /*
  10043. * Return the default properties for form fields.
  10044. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10045. * @access public
  10046. * @author Nicola Asuni
  10047. * @since 4.8.000 (2009-09-06)
  10048. */
  10049. public function getFormDefaultProp() {
  10050. return $this->default_form_prop;
  10051. }
  10052. /*
  10053. * Creates a text field
  10054. * @param string $name field name
  10055. * @param float $w Width of the rectangle
  10056. * @param float $h Height of the rectangle
  10057. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10058. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10059. * @param float $x Abscissa of the upper-left corner of the rectangle
  10060. * @param float $y Ordinate of the upper-left corner of the rectangle
  10061. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10062. * @access public
  10063. * @author Nicola Asuni
  10064. * @since 4.8.000 (2009-09-07)
  10065. */
  10066. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10067. if ($x === '') {
  10068. $x = $this->x;
  10069. }
  10070. if ($y === '') {
  10071. $y = $this->y;
  10072. }
  10073. if ($js) {
  10074. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  10075. return;
  10076. }
  10077. // get default style
  10078. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10079. // get annotation data
  10080. $popt = $this->getAnnotOptFromJSProp($prop);
  10081. // set default appearance stream
  10082. $font = $this->FontFamily;
  10083. $fontkey = array_search($font, $this->fontkeys);
  10084. if (!in_array($fontkey, $this->annotation_fonts)) {
  10085. $this->annotation_fonts[$font] = $fontkey;
  10086. }
  10087. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10088. $popt['da'] = $fontstyle;
  10089. $popt['ap'] = array();
  10090. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10091. // merge options
  10092. $opt = array_merge($popt, $opt);
  10093. // remove some conflicting options
  10094. unset($opt['bs']);
  10095. // set remaining annotation data
  10096. $opt['Subtype'] = 'Widget';
  10097. $opt['ft'] = 'Tx';
  10098. $opt['t'] = $name;
  10099. /*
  10100. Additional annotation's parameters (check _putannotsobj() method):
  10101. //$opt['f']
  10102. //$opt['ap']
  10103. //$opt['as']
  10104. //$opt['bs']
  10105. //$opt['be']
  10106. //$opt['c']
  10107. //$opt['border']
  10108. //$opt['h']
  10109. //$opt['mk']
  10110. //$opt['mk']['r']
  10111. //$opt['mk']['bc']
  10112. //$opt['mk']['bg']
  10113. //$opt['mk']['ca']
  10114. //$opt['mk']['rc']
  10115. //$opt['mk']['ac']
  10116. //$opt['mk']['i']
  10117. //$opt['mk']['ri']
  10118. //$opt['mk']['ix']
  10119. //$opt['mk']['if']
  10120. //$opt['mk']['if']['sw']
  10121. //$opt['mk']['if']['s']
  10122. //$opt['mk']['if']['a']
  10123. //$opt['mk']['if']['fb']
  10124. //$opt['mk']['tp']
  10125. //$opt['tu']
  10126. //$opt['tm']
  10127. //$opt['ff']
  10128. //$opt['v']
  10129. //$opt['dv']
  10130. //$opt['a']
  10131. //$opt['aa']
  10132. //$opt['q']
  10133. */
  10134. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10135. if ($this->rtl) {
  10136. $this->x -= $w;
  10137. } else {
  10138. $this->x += $w;
  10139. }
  10140. }
  10141. /*
  10142. * Creates a RadioButton field
  10143. * @param string $name field name
  10144. * @param int $w width
  10145. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10146. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10147. * @param string $onvalue value to be returned if selected.
  10148. * @param boolean $checked define the initial state.
  10149. * @param float $x Abscissa of the upper-left corner of the rectangle
  10150. * @param float $y Ordinate of the upper-left corner of the rectangle
  10151. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10152. * @access public
  10153. * @author Nicola Asuni
  10154. * @since 4.8.000 (2009-09-07)
  10155. */
  10156. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  10157. if ($x === '') {
  10158. $x = $this->x;
  10159. }
  10160. if ($y === '') {
  10161. $y = $this->y;
  10162. }
  10163. if ($js) {
  10164. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  10165. return;
  10166. }
  10167. if ($this->empty_string($onvalue)) {
  10168. $onvalue = 'On';
  10169. }
  10170. if ($checked) {
  10171. $defval = $onvalue;
  10172. } else {
  10173. $defval = 'Off';
  10174. }
  10175. // set data for parent group
  10176. if (!isset($this->radiobutton_groups[$this->page])) {
  10177. $this->radiobutton_groups[$this->page] = array();
  10178. }
  10179. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  10180. $this->radiobutton_groups[$this->page][$name] = array();
  10181. ++$this->annot_obj_id;
  10182. $this->radio_groups[] = $this->annot_obj_id;
  10183. }
  10184. // save object ID to be added on Kids entry on parent object
  10185. $this->radiobutton_groups[$this->page][$name][] = array('kid' => ($this->annot_obj_id + 1), 'def' => $defval);
  10186. // get default style
  10187. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10188. $prop['NoToggleToOff'] = 'true';
  10189. $prop['Radio'] = 'true';
  10190. $prop['borderStyle'] = 'inset';
  10191. // get annotation data
  10192. $popt = $this->getAnnotOptFromJSProp($prop);
  10193. // set additional default values
  10194. $font = 'zapfdingbats';
  10195. $this->AddFont($font);
  10196. $fontkey = array_search($font, $this->fontkeys);
  10197. if (!in_array($fontkey, $this->annotation_fonts)) {
  10198. $this->annotation_fonts[$font] = $fontkey;
  10199. }
  10200. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10201. $popt['da'] = $fontstyle;
  10202. $popt['ap'] = array();
  10203. $popt['ap']['n'] = array();
  10204. $popt['ap']['n'][$onvalue] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10205. $popt['ap']['n']['Off'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10206. if (!isset($popt['mk'])) {
  10207. $popt['mk'] = array();
  10208. }
  10209. $popt['mk']['ca'] = '(l)';
  10210. // merge options
  10211. $opt = array_merge($popt, $opt);
  10212. // set remaining annotation data
  10213. $opt['Subtype'] = 'Widget';
  10214. $opt['ft'] = 'Btn';
  10215. if ($checked) {
  10216. $opt['v'] = array('/'.$onvalue);
  10217. $opt['as'] = $onvalue;
  10218. } else {
  10219. $opt['as'] = 'Off';
  10220. }
  10221. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  10222. if ($this->rtl) {
  10223. $this->x -= $w;
  10224. } else {
  10225. $this->x += $w;
  10226. }
  10227. }
  10228. /*
  10229. * Creates a List-box field
  10230. * @param string $name field name
  10231. * @param int $w width
  10232. * @param int $h height
  10233. * @param array $values array containing the list of values.
  10234. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10235. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10236. * @param float $x Abscissa of the upper-left corner of the rectangle
  10237. * @param float $y Ordinate of the upper-left corner of the rectangle
  10238. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10239. * @access public
  10240. * @author Nicola Asuni
  10241. * @since 4.8.000 (2009-09-07)
  10242. */
  10243. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10244. if ($x === '') {
  10245. $x = $this->x;
  10246. }
  10247. if ($y === '') {
  10248. $y = $this->y;
  10249. }
  10250. if ($js) {
  10251. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  10252. $s = '';
  10253. foreach ($values as $value) {
  10254. $s .= "'".addslashes($value)."',";
  10255. }
  10256. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  10257. return;
  10258. }
  10259. // get default style
  10260. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10261. // get annotation data
  10262. $popt = $this->getAnnotOptFromJSProp($prop);
  10263. // set additional default values
  10264. $font = $this->FontFamily;
  10265. $fontkey = array_search($font, $this->fontkeys);
  10266. if (!in_array($fontkey, $this->annotation_fonts)) {
  10267. $this->annotation_fonts[$font] = $fontkey;
  10268. }
  10269. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10270. $popt['da'] = $fontstyle;
  10271. $popt['ap'] = array();
  10272. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10273. // merge options
  10274. $opt = array_merge($popt, $opt);
  10275. // set remaining annotation data
  10276. $opt['Subtype'] = 'Widget';
  10277. $opt['ft'] = 'Ch';
  10278. $opt['t'] = $name;
  10279. $opt['opt'] = $values;
  10280. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10281. if ($this->rtl) {
  10282. $this->x -= $w;
  10283. } else {
  10284. $this->x += $w;
  10285. }
  10286. }
  10287. /*
  10288. * Creates a Combo-box field
  10289. * @param string $name field name
  10290. * @param int $w width
  10291. * @param int $h height
  10292. * @param array $values array containing the list of values.
  10293. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10294. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10295. * @param float $x Abscissa of the upper-left corner of the rectangle
  10296. * @param float $y Ordinate of the upper-left corner of the rectangle
  10297. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10298. * @access public
  10299. * @author Nicola Asuni
  10300. * @since 4.8.000 (2009-09-07)
  10301. */
  10302. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10303. if ($x === '') {
  10304. $x = $this->x;
  10305. }
  10306. if ($y === '') {
  10307. $y = $this->y;
  10308. }
  10309. if ($js) {
  10310. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  10311. $s = '';
  10312. foreach ($values as $value) {
  10313. $s .= "'".addslashes($value)."',";
  10314. }
  10315. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  10316. return;
  10317. }
  10318. // get default style
  10319. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10320. $prop['Combo'] = true;
  10321. // get annotation data
  10322. $popt = $this->getAnnotOptFromJSProp($prop);
  10323. // set additional default options
  10324. $font = $this->FontFamily;
  10325. $fontkey = array_search($font, $this->fontkeys);
  10326. if (!in_array($fontkey, $this->annotation_fonts)) {
  10327. $this->annotation_fonts[$font] = $fontkey;
  10328. }
  10329. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10330. $popt['da'] = $fontstyle;
  10331. $popt['ap'] = array();
  10332. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10333. // merge options
  10334. $opt = array_merge($popt, $opt);
  10335. // set remaining annotation data
  10336. $opt['Subtype'] = 'Widget';
  10337. $opt['ft'] = 'Ch';
  10338. $opt['t'] = $name;
  10339. $opt['opt'] = $values;
  10340. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10341. if ($this->rtl) {
  10342. $this->x -= $w;
  10343. } else {
  10344. $this->x += $w;
  10345. }
  10346. }
  10347. /*
  10348. * Creates a CheckBox field
  10349. * @param string $name field name
  10350. * @param int $w width
  10351. * @param boolean $checked define the initial state.
  10352. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10353. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10354. * @param string $onvalue value to be returned if selected.
  10355. * @param float $x Abscissa of the upper-left corner of the rectangle
  10356. * @param float $y Ordinate of the upper-left corner of the rectangle
  10357. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10358. * @access public
  10359. * @author Nicola Asuni
  10360. * @since 4.8.000 (2009-09-07)
  10361. */
  10362. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  10363. if ($x === '') {
  10364. $x = $this->x;
  10365. }
  10366. if ($y === '') {
  10367. $y = $this->y;
  10368. }
  10369. if ($js) {
  10370. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  10371. return;
  10372. }
  10373. if (!isset($prop['value'])) {
  10374. $prop['value'] = array('Yes');
  10375. }
  10376. // get default style
  10377. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10378. $prop['borderStyle'] = 'inset';
  10379. // get annotation data
  10380. $popt = $this->getAnnotOptFromJSProp($prop);
  10381. // set additional default options
  10382. $font = 'zapfdingbats';
  10383. $this->AddFont($font);
  10384. $fontkey = array_search($font, $this->fontkeys);
  10385. if (!in_array($fontkey, $this->annotation_fonts)) {
  10386. $this->annotation_fonts[$font] = $fontkey;
  10387. }
  10388. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10389. $popt['da'] = $fontstyle;
  10390. $popt['ap'] = array();
  10391. $popt['ap']['n'] = array();
  10392. $popt['ap']['n']['Yes'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10393. $popt['ap']['n']['Off'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10394. // merge options
  10395. $opt = array_merge($popt, $opt);
  10396. // set remaining annotation data
  10397. $opt['Subtype'] = 'Widget';
  10398. $opt['ft'] = 'Btn';
  10399. $opt['t'] = $name;
  10400. $opt['opt'] = array($onvalue);
  10401. if ($checked) {
  10402. $opt['v'] = array('/0');
  10403. $opt['as'] = 'Yes';
  10404. } else {
  10405. $opt['v'] = array('/Off');
  10406. $opt['as'] = 'Off';
  10407. }
  10408. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  10409. if ($this->rtl) {
  10410. $this->x -= $w;
  10411. } else {
  10412. $this->x += $w;
  10413. }
  10414. }
  10415. /*
  10416. * Creates a button field
  10417. * @param string $name field name
  10418. * @param int $w width
  10419. * @param int $h height
  10420. * @param string $caption caption.
  10421. * @param mixed $action action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
  10422. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10423. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10424. * @param float $x Abscissa of the upper-left corner of the rectangle
  10425. * @param float $y Ordinate of the upper-left corner of the rectangle
  10426. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10427. * @access public
  10428. * @author Nicola Asuni
  10429. * @since 4.8.000 (2009-09-07)
  10430. */
  10431. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10432. if ($x === '') {
  10433. $x = $this->x;
  10434. }
  10435. if ($y === '') {
  10436. $y = $this->y;
  10437. }
  10438. if ($js) {
  10439. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  10440. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  10441. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  10442. $this->javascript .= 'f'.$name.".highlight='push';\n";
  10443. $this->javascript .= 'f'.$name.".print=false;\n";
  10444. return;
  10445. }
  10446. // get default style
  10447. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10448. $prop['Pushbutton'] = 'true';
  10449. $prop['highlight'] = 'push';
  10450. $prop['display'] = 'display.noPrint';
  10451. // get annotation data
  10452. $popt = $this->getAnnotOptFromJSProp($prop);
  10453. // set additional default options
  10454. if (!isset($popt['mk'])) {
  10455. $popt['mk'] = array();
  10456. }
  10457. $popt['mk']['ca'] = $this->_textstring($caption);
  10458. $popt['mk']['rc'] = $this->_textstring($caption);
  10459. $popt['mk']['ac'] = $this->_textstring($caption);
  10460. $font = $this->FontFamily;
  10461. $fontkey = array_search($font, $this->fontkeys);
  10462. if (!in_array($fontkey, $this->annotation_fonts)) {
  10463. $this->annotation_fonts[$font] = $fontkey;
  10464. }
  10465. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10466. $popt['da'] = $fontstyle;
  10467. $popt['ap'] = array();
  10468. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10469. // merge options
  10470. $opt = array_merge($popt, $opt);
  10471. // set remaining annotation data
  10472. $opt['Subtype'] = 'Widget';
  10473. $opt['ft'] = 'Btn';
  10474. $opt['t'] = $caption;
  10475. $opt['v'] = $name;
  10476. if (!empty($action)) {
  10477. if (is_array($action)) {
  10478. // form action options as on section 12.7.5 of PDF32000_2008.
  10479. $opt['aa'] = '/D <<';
  10480. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  10481. foreach ($action AS $key => $val) {
  10482. if (($key == 'S') AND in_array($val, $bmode)) {
  10483. $opt['aa'] .= ' /S /'.$val;
  10484. } elseif (($key == 'F') AND (!empty($val))) {
  10485. $opt['aa'] .= ' /F '.$this->_datastring($val);
  10486. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  10487. $opt['aa'] .= ' /Fields [';
  10488. foreach ($val AS $field) {
  10489. $opt['aa'] .= ' '.$this->_textstring($field);
  10490. }
  10491. $opt['aa'] .= ']';
  10492. } elseif (($key == 'Flags')) {
  10493. $ff = 0;
  10494. if (is_array($val)) {
  10495. foreach ($val AS $flag) {
  10496. switch ($flag) {
  10497. case 'Include/Exclude': {
  10498. $ff += 1 << 0;
  10499. break;
  10500. }
  10501. case 'IncludeNoValueFields': {
  10502. $ff += 1 << 1;
  10503. break;
  10504. }
  10505. case 'ExportFormat': {
  10506. $ff += 1 << 2;
  10507. break;
  10508. }
  10509. case 'GetMethod': {
  10510. $ff += 1 << 3;
  10511. break;
  10512. }
  10513. case 'SubmitCoordinates': {
  10514. $ff += 1 << 4;
  10515. break;
  10516. }
  10517. case 'XFDF': {
  10518. $ff += 1 << 5;
  10519. break;
  10520. }
  10521. case 'IncludeAppendSaves': {
  10522. $ff += 1 << 6;
  10523. break;
  10524. }
  10525. case 'IncludeAnnotations': {
  10526. $ff += 1 << 7;
  10527. break;
  10528. }
  10529. case 'SubmitPDF': {
  10530. $ff += 1 << 8;
  10531. break;
  10532. }
  10533. case 'CanonicalFormat': {
  10534. $ff += 1 << 9;
  10535. break;
  10536. }
  10537. case 'ExclNonUserAnnots': {
  10538. $ff += 1 << 10;
  10539. break;
  10540. }
  10541. case 'ExclFKey': {
  10542. $ff += 1 << 11;
  10543. break;
  10544. }
  10545. case 'EmbedForm': {
  10546. $ff += 1 << 13;
  10547. break;
  10548. }
  10549. }
  10550. }
  10551. } else {
  10552. $ff = intval($val);
  10553. }
  10554. $opt['aa'] .= ' /Flags '.$ff;
  10555. }
  10556. }
  10557. $opt['aa'] .= ' >>';
  10558. } else {
  10559. // Javascript action or raw action command
  10560. $js_obj_id = $this->addJavascriptObject($action);
  10561. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  10562. }
  10563. }
  10564. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10565. if ($this->rtl) {
  10566. $this->x -= $w;
  10567. } else {
  10568. $this->x += $w;
  10569. }
  10570. }
  10571. // --- END FORMS FIELDS ------------------------------------------------
  10572. /*
  10573. * Add certification signature (DocMDP or UR3)
  10574. * You can set only one signature type
  10575. * @access protected
  10576. * @author Nicola Asuni
  10577. * @since 4.6.008 (2009-05-07)
  10578. */
  10579. protected function _putsignature() {
  10580. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  10581. return;
  10582. }
  10583. $this->_out('/Type /Sig');
  10584. $this->_out('/Filter /Adobe.PPKLite');
  10585. $this->_out('/SubFilter /adbe.pkcs7.detached');
  10586. $this->_out($this->byterange_string);
  10587. $this->_out('/Contents<>'.str_repeat(' ', $this->signature_max_lenght));
  10588. $this->_out('/Reference');
  10589. $this->_out('[');
  10590. $this->_out('<<');
  10591. $this->_out('/Type /SigRef');
  10592. if ($this->signature_data['cert_type'] > 0) {
  10593. $this->_out('/TransformMethod /DocMDP');
  10594. $this->_out('/TransformParams');
  10595. $this->_out('<<');
  10596. $this->_out('/Type /TransformParams');
  10597. $this->_out('/V /1.2');
  10598. $this->_out('/P '.$this->signature_data['cert_type'].'');
  10599. } else {
  10600. $this->_out('/TransformMethod /UR3');
  10601. $this->_out('/TransformParams');
  10602. $this->_out('<<');
  10603. $this->_out('/Type /TransformParams');
  10604. $this->_out('/V /2.2');
  10605. if (!$this->empty_string($this->ur_document)) {
  10606. $this->_out('/Document['.$this->ur_document.']');
  10607. }
  10608. if (!$this->empty_string($this->ur_annots)) {
  10609. $this->_out('/Annots['.$this->ur_annots.']');
  10610. }
  10611. if (!$this->empty_string($this->ur_form)) {
  10612. $this->_out('/Form['.$this->ur_form.']');
  10613. }
  10614. if (!$this->empty_string($this->ur_signature)) {
  10615. $this->_out('/Signature['.$this->ur_signature.']');
  10616. }
  10617. }
  10618. $this->_out('>>');
  10619. $this->_out('>>');
  10620. $this->_out(']');
  10621. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  10622. $this->_out('/Name '.$this->_textstring($this->signature_data['info']['Name']).'');
  10623. }
  10624. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  10625. $this->_out('/Location '.$this->_textstring($this->signature_data['info']['Location']).'');
  10626. }
  10627. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  10628. $this->_out('/Reason '.$this->_textstring($this->signature_data['info']['Reason']).'');
  10629. }
  10630. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  10631. $this->_out('/ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo']).'');
  10632. }
  10633. $this->_out('/M '.$this->_datestring());
  10634. }
  10635. /*
  10636. * Set User's Rights for PDF Reader
  10637. * WARNING: This works only using the Adobe private key with the setSignature() method!.
  10638. * Check the PDF Reference 8.7.1 Transform Methods,
  10639. * Table 8.105 Entries in the UR transform parameters dictionary
  10640. * @param boolean $enable if true enable user's rights on PDF reader
  10641. * @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.
  10642. * @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.
  10643. * @param string $form Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  10644. * @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.
  10645. * @access public
  10646. * @author Nicola Asuni
  10647. * @since 2.9.000 (2008-03-26)
  10648. */
  10649. public function setUserRights(
  10650. $enable=true,
  10651. $document='/FullSave',
  10652. $annots='/Create/Delete/Modify/Copy/Import/Export',
  10653. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  10654. $signature='/Modify') {
  10655. $this->ur = $enable;
  10656. $this->ur_document = $document;
  10657. $this->ur_annots = $annots;
  10658. $this->ur_form = $form;
  10659. $this->ur_signature = $signature;
  10660. if (!$this->sign) {
  10661. // This signature only works using the Adobe Private key that is unavailable!
  10662. $this->setSignature('', '', '', '', 0, array());
  10663. }
  10664. }
  10665. /*
  10666. * Enable document signature (requires the OpenSSL Library).
  10667. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  10668. * @param mixed $signing_cert signing certificate (string or filename prefixed with 'file://')
  10669. * @param mixed $private_key private key (string or filename prefixed with 'file://')
  10670. * @param string $private_key_password password
  10671. * @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.
  10672. * @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.
  10673. * @parm array $info array of option information: Name, Location, Reason, ContactInfo.
  10674. * @access public
  10675. * @author Nicola Asuni
  10676. * @since 4.6.005 (2009-04-24)
  10677. */
  10678. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  10679. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  10680. // to convert pfx certificate to pem: openssl
  10681. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  10682. $this->sign = true;
  10683. $this->signature_data = array();
  10684. if (strlen($signing_cert) == 0) {
  10685. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
  10686. $private_key_password = 'tcpdfdemo';
  10687. }
  10688. if (strlen($private_key) == 0) {
  10689. $private_key = $signing_cert;
  10690. }
  10691. $this->signature_data['signcert'] = $signing_cert;
  10692. $this->signature_data['privkey'] = $private_key;
  10693. $this->signature_data['password'] = $private_key_password;
  10694. $this->signature_data['extracerts'] = $extracerts;
  10695. $this->signature_data['cert_type'] = $cert_type;
  10696. $this->signature_data['info'] = $info;
  10697. }
  10698. /*
  10699. * Create a new page group.
  10700. * NOTE: call this function before calling AddPage()
  10701. * @param int $page starting group page (leave empty for next page).
  10702. * @access public
  10703. * @since 3.0.000 (2008-03-27)
  10704. */
  10705. public function startPageGroup($page='') {
  10706. if (empty($page)) {
  10707. $page = $this->page + 1;
  10708. }
  10709. $this->newpagegroup[$page] = true;
  10710. }
  10711. /**
  10712. * Defines an alias for the total number of pages.
  10713. * It will be substituted as the document is closed.
  10714. * @param string $alias The alias.
  10715. * @access public
  10716. * @since 1.4
  10717. * @see getAliasNbPages(), PageNo(), Footer()
  10718. */
  10719. public function AliasNbPages($alias='{nb}') {
  10720. $this->AliasNbPages = $alias;
  10721. }
  10722. /**
  10723. * Returns the string alias used for the total number of pages.
  10724. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  10725. * @return string
  10726. * @access public
  10727. * @since 4.0.018 (2008-08-08)
  10728. * @see AliasNbPages(), PageNo(), Footer()
  10729. */
  10730. public function getAliasNbPages() {
  10731. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  10732. return '{'.$this->AliasNbPages.'}';
  10733. }
  10734. return $this->AliasNbPages;
  10735. }
  10736. /**
  10737. * Defines an alias for the page number.
  10738. * It will be substituted as the document is closed.
  10739. * @param string $alias The alias.
  10740. * @access public
  10741. * @since 4.5.000 (2009-01-02)
  10742. * @see getAliasNbPages(), PageNo(), Footer()
  10743. */
  10744. public function AliasNumPage($alias='{pnb}') {
  10745. //Define an alias for total number of pages
  10746. $this->AliasNumPage = $alias;
  10747. }
  10748. /**
  10749. * Returns the string alias used for the page number.
  10750. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  10751. * @return string
  10752. * @access public
  10753. * @since 4.5.000 (2009-01-02)
  10754. * @see AliasNbPages(), PageNo(), Footer()
  10755. */
  10756. public function getAliasNumPage() {
  10757. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  10758. return '{'.$this->AliasNumPage.'}';
  10759. }
  10760. return $this->AliasNumPage;
  10761. }
  10762. /*
  10763. * Return the current page in the group.
  10764. * @return current page in the group
  10765. * @access public
  10766. * @since 3.0.000 (2008-03-27)
  10767. */
  10768. public function getGroupPageNo() {
  10769. return $this->pagegroups[$this->currpagegroup];
  10770. }
  10771. /**
  10772. * Returns the current group page number formatted as a string.
  10773. * @access public
  10774. * @since 4.3.003 (2008-11-18)
  10775. * @see PaneNo(), formatPageNumber()
  10776. */
  10777. public function getGroupPageNoFormatted() {
  10778. return $this->formatPageNumber($this->getGroupPageNo());
  10779. }
  10780. /*
  10781. * Return the alias of the current page group
  10782. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  10783. * (will be replaced by the total number of pages in this group).
  10784. * @return alias of the current page group
  10785. * @access public
  10786. * @since 3.0.000 (2008-03-27)
  10787. */
  10788. public function getPageGroupAlias() {
  10789. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  10790. return '{'.$this->currpagegroup.'}';
  10791. }
  10792. return $this->currpagegroup;
  10793. }
  10794. /*
  10795. * Return the alias for the page number on the current page group
  10796. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  10797. * (will be replaced by the total number of pages in this group).
  10798. * @return alias of the current page group
  10799. * @access public
  10800. * @since 4.5.000 (2009-01-02)
  10801. */
  10802. public function getPageNumGroupAlias() {
  10803. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  10804. return '{'.str_replace('{nb', '{pnb', $this->currpagegroup).'}';
  10805. }
  10806. return str_replace('{nb', '{pnb', $this->currpagegroup);
  10807. }
  10808. /**
  10809. * Format the page numbers.
  10810. * This method can be overriden for custom formats.
  10811. * @param int $num page number
  10812. * @access protected
  10813. * @since 4.2.005 (2008-11-06)
  10814. */
  10815. protected function formatPageNumber($num) {
  10816. return number_format((float)$num, 0, '', '.');
  10817. }
  10818. /**
  10819. * Format the page numbers on the Table Of Content.
  10820. * This method can be overriden for custom formats.
  10821. * @param int $num page number
  10822. * @access protected
  10823. * @since 4.5.001 (2009-01-04)
  10824. * @see addTOC()
  10825. */
  10826. protected function formatTOCPageNumber($num) {
  10827. return number_format((float)$num, 0, '', '.');
  10828. }
  10829. /**
  10830. * Returns the current page number formatted as a string.
  10831. * @access public
  10832. * @since 4.2.005 (2008-11-06)
  10833. * @see PaneNo(), formatPageNumber()
  10834. */
  10835. public function PageNoFormatted() {
  10836. return $this->formatPageNumber($this->PageNo());
  10837. }
  10838. /*
  10839. * Put visibility settings.
  10840. * @access protected
  10841. * @since 3.0.000 (2008-03-27)
  10842. */
  10843. protected function _putocg() {
  10844. $this->_newobj();
  10845. $this->n_ocg_print = $this->n;
  10846. $this->_out('<</Type /OCG /Name '.$this->_textstring('print'));
  10847. $this->_out('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>>');
  10848. $this->_out('endobj');
  10849. $this->_newobj();
  10850. $this->n_ocg_view = $this->n;
  10851. $this->_out('<</Type /OCG /Name '.$this->_textstring('view'));
  10852. $this->_out('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>>');
  10853. $this->_out('endobj');
  10854. }
  10855. /*
  10856. * Set the visibility of the successive elements.
  10857. * This can be useful, for instance, to put a background
  10858. * image or color that will show on screen but won't print.
  10859. * @param string $v visibility mode. Legal values are: all, print, screen.
  10860. * @access public
  10861. * @since 3.0.000 (2008-03-27)
  10862. */
  10863. public function setVisibility($v) {
  10864. if ($this->openMarkedContent) {
  10865. // close existing open marked-content
  10866. $this->_out('EMC');
  10867. $this->openMarkedContent = false;
  10868. }
  10869. switch($v) {
  10870. case 'print': {
  10871. $this->_out('/OC /OC1 BDC');
  10872. $this->openMarkedContent = true;
  10873. break;
  10874. }
  10875. case 'screen': {
  10876. $this->_out('/OC /OC2 BDC');
  10877. $this->openMarkedContent = true;
  10878. break;
  10879. }
  10880. case 'all': {
  10881. $this->_out('');
  10882. break;
  10883. }
  10884. default: {
  10885. $this->Error('Incorrect visibility: '.$v);
  10886. break;
  10887. }
  10888. }
  10889. $this->visibility = $v;
  10890. }
  10891. /*
  10892. * Add transparency parameters to the current extgstate
  10893. * @param array $params parameters
  10894. * @return the number of extgstates
  10895. * @access protected
  10896. * @since 3.0.000 (2008-03-27)
  10897. */
  10898. protected function addExtGState($parms) {
  10899. $n = count($this->extgstates) + 1;
  10900. $this->extgstates[$n]['parms'] = $parms;
  10901. return $n;
  10902. }
  10903. /*
  10904. * Add an extgstate
  10905. * @param array $gs extgstate
  10906. * @access protected
  10907. * @since 3.0.000 (2008-03-27)
  10908. */
  10909. protected function setExtGState($gs) {
  10910. $this->_out(sprintf('/GS%d gs', $gs));
  10911. }
  10912. /*
  10913. * Put extgstates for object transparency
  10914. * @param array $gs extgstate
  10915. * @access protected
  10916. * @since 3.0.000 (2008-03-27)
  10917. */
  10918. protected function _putextgstates() {
  10919. $ne = count($this->extgstates);
  10920. for ($i = 1; $i <= $ne; ++$i) {
  10921. $this->_newobj();
  10922. $this->extgstates[$i]['n'] = $this->n;
  10923. $this->_out('<</Type /ExtGState');
  10924. foreach ($this->extgstates[$i]['parms'] as $k => $v) {
  10925. $this->_out('/'.$k.' '.$v);
  10926. }
  10927. $this->_out('>>');
  10928. $this->_out('endobj');
  10929. }
  10930. }
  10931. /*
  10932. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  10933. * @param float $alpha real value from 0 (transparent) to 1 (opaque)
  10934. * @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
  10935. * @access public
  10936. * @since 3.0.000 (2008-03-27)
  10937. */
  10938. public function setAlpha($alpha, $bm='Normal') {
  10939. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm));
  10940. $this->setExtGState($gs);
  10941. }
  10942. /*
  10943. * Set the default JPEG compression quality (1-100)
  10944. * @param int $quality JPEG quality, integer between 1 and 100
  10945. * @access public
  10946. * @since 3.0.000 (2008-03-27)
  10947. */
  10948. public function setJPEGQuality($quality) {
  10949. if (($quality < 1) OR ($quality > 100)) {
  10950. $quality = 75;
  10951. }
  10952. $this->jpeg_quality = intval($quality);
  10953. }
  10954. /*
  10955. * Set the default number of columns in a row for HTML tables.
  10956. * @param int $cols number of columns
  10957. * @access public
  10958. * @since 3.0.014 (2008-06-04)
  10959. */
  10960. public function setDefaultTableColumns($cols=4) {
  10961. $this->default_table_columns = intval($cols);
  10962. }
  10963. /*
  10964. * Set the height of the cell (line height) respect the font height.
  10965. * @param int $h cell proportion respect font height (typical value = 1.25).
  10966. * @access public
  10967. * @since 3.0.014 (2008-06-04)
  10968. */
  10969. public function setCellHeightRatio($h) {
  10970. $this->cell_height_ratio = $h;
  10971. }
  10972. /*
  10973. * return the height of cell repect font height.
  10974. * @access public
  10975. * @since 4.0.012 (2008-07-24)
  10976. */
  10977. public function getCellHeightRatio() {
  10978. return $this->cell_height_ratio;
  10979. }
  10980. /*
  10981. * Set the PDF version (check PDF reference for valid values).
  10982. * Default value is 1.t
  10983. * @access public
  10984. * @since 3.1.000 (2008-06-09)
  10985. */
  10986. public function setPDFVersion($version='1.7') {
  10987. $this->PDFVersion = $version;
  10988. }
  10989. /*
  10990. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  10991. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  10992. * <ul>
  10993. * <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>
  10994. * <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>
  10995. * <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>
  10996. * <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>
  10997. * <li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li>
  10998. * <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>
  10999. * <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>
  11000. * <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>
  11001. * <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>
  11002. * <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>
  11003. * <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>
  11004. * <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>
  11005. * <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>
  11006. * <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>
  11007. * <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>
  11008. * <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>
  11009. * </ul>
  11010. * @param array $preferences array of options.
  11011. * @author Nicola Asuni
  11012. * @access public
  11013. * @since 3.1.000 (2008-06-09)
  11014. */
  11015. public function setViewerPreferences($preferences) {
  11016. $this->viewer_preferences = $preferences;
  11017. }
  11018. /**
  11019. * Paints a linear colour gradient.
  11020. * @param float $x abscissa of the top left corner of the rectangle.
  11021. * @param float $y ordinate of the top left corner of the rectangle.
  11022. * @param float $w width of the rectangle.
  11023. * @param float $h height of the rectangle.
  11024. * @param array $col1 first color (RGB components).
  11025. * @param array $col2 second color (RGB components).
  11026. * @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).
  11027. * @author Andreas W�rmser, Nicola Asuni
  11028. * @since 3.1.000 (2008-06-09)
  11029. * @access public
  11030. */
  11031. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  11032. $this->Clip($x, $y, $w, $h);
  11033. $this->Gradient(2, $col1, $col2, $coords);
  11034. }
  11035. /**
  11036. * Paints a radial colour gradient.
  11037. * @param float $x abscissa of the top left corner of the rectangle.
  11038. * @param float $y ordinate of the top left corner of the rectangle.
  11039. * @param float $w width of the rectangle.
  11040. * @param float $h height of the rectangle.
  11041. * @param array $col1 first color (RGB components).
  11042. * @param array $col2 second color (RGB components).
  11043. * @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.
  11044. * @author Andreas W�rmser, Nicola Asuni
  11045. * @since 3.1.000 (2008-06-09)
  11046. * @access public
  11047. */
  11048. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  11049. $this->Clip($x, $y, $w, $h);
  11050. $this->Gradient(3, $col1, $col2, $coords);
  11051. }
  11052. /**
  11053. * Paints a coons patch mesh.
  11054. * @param float $x abscissa of the top left corner of the rectangle.
  11055. * @param float $y ordinate of the top left corner of the rectangle.
  11056. * @param float $w width of the rectangle.
  11057. * @param float $h height of the rectangle.
  11058. * @param array $col1 first color (lower left corner) (RGB components).
  11059. * @param array $col2 second color (lower right corner) (RGB components).
  11060. * @param array $col3 third color (upper right corner) (RGB components).
  11061. * @param array $col4 fourth color (upper left corner) (RGB components).
  11062. * @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>
  11063. * @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
  11064. * @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
  11065. * @author Andreas W�rmser, Nicola Asuni
  11066. * @since 3.1.000 (2008-06-09)
  11067. * @access public
  11068. */
  11069. 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) {
  11070. $this->Clip($x, $y, $w, $h);
  11071. $n = count($this->gradients) + 1;
  11072. $this->gradients[$n]['type'] = 6; //coons patch mesh
  11073. //check the coords array if it is the simple array or the multi patch array
  11074. if (!isset($coords[0]['f'])) {
  11075. //simple array -> convert to multi patch array
  11076. if (!isset($col1[1])) {
  11077. $col1[1] = $col1[2] = $col1[0];
  11078. }
  11079. if (!isset($col2[1])) {
  11080. $col2[1] = $col2[2] = $col2[0];
  11081. }
  11082. if (!isset($col3[1])) {
  11083. $col3[1] = $col3[2] = $col3[0];
  11084. }
  11085. if (!isset($col4[1])) {
  11086. $col4[1] = $col4[2] = $col4[0];
  11087. }
  11088. $patch_array[0]['f'] = 0;
  11089. $patch_array[0]['points'] = $coords;
  11090. $patch_array[0]['colors'][0]['r'] = $col1[0];
  11091. $patch_array[0]['colors'][0]['g'] = $col1[1];
  11092. $patch_array[0]['colors'][0]['b'] = $col1[2];
  11093. $patch_array[0]['colors'][1]['r'] = $col2[0];
  11094. $patch_array[0]['colors'][1]['g'] = $col2[1];
  11095. $patch_array[0]['colors'][1]['b'] = $col2[2];
  11096. $patch_array[0]['colors'][2]['r'] = $col3[0];
  11097. $patch_array[0]['colors'][2]['g'] = $col3[1];
  11098. $patch_array[0]['colors'][2]['b'] = $col3[2];
  11099. $patch_array[0]['colors'][3]['r'] = $col4[0];
  11100. $patch_array[0]['colors'][3]['g'] = $col4[1];
  11101. $patch_array[0]['colors'][3]['b'] = $col4[2];
  11102. } else {
  11103. //multi patch array
  11104. $patch_array = $coords;
  11105. }
  11106. $bpcd = 65535; //16 BitsPerCoordinate
  11107. //build the data stream
  11108. $this->gradients[$n]['stream'] = '';
  11109. $count_patch = count($patch_array);
  11110. for ($i=0; $i < $count_patch; ++$i) {
  11111. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  11112. $count_points = count($patch_array[$i]['points']);
  11113. for ($j=0; $j < $count_points; ++$j) {
  11114. //each point as 16 bit
  11115. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  11116. if ($patch_array[$i]['points'][$j] < 0) {
  11117. $patch_array[$i]['points'][$j] = 0;
  11118. }
  11119. if ($patch_array[$i]['points'][$j] > $bpcd) {
  11120. $patch_array[$i]['points'][$j] = $bpcd;
  11121. }
  11122. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  11123. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  11124. }
  11125. $count_cols = count($patch_array[$i]['colors']);
  11126. for ($j=0; $j < $count_cols; ++$j) {
  11127. //each color component as 8 bit
  11128. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  11129. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  11130. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  11131. }
  11132. }
  11133. //paint the gradient
  11134. $this->_out('/Sh'.$n.' sh');
  11135. //restore previous Graphic State
  11136. $this->_out('Q');
  11137. }
  11138. /**
  11139. * Set a rectangular clipping area.
  11140. * @param float $x abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  11141. * @param float $y ordinate of the top left corner of the rectangle.
  11142. * @param float $w width of the rectangle.
  11143. * @param float $h height of the rectangle.
  11144. * @author Andreas W�rmser, Nicola Asuni
  11145. * @since 3.1.000 (2008-06-09)
  11146. * @access protected
  11147. */
  11148. protected function Clip($x, $y, $w, $h) {
  11149. if ($this->rtl) {
  11150. $x = $this->w - $x - $w;
  11151. }
  11152. //save current Graphic State
  11153. $s = 'q';
  11154. //set clipping area
  11155. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  11156. //set up transformation matrix for gradient
  11157. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  11158. $this->_out($s);
  11159. }
  11160. /**
  11161. * Output gradient.
  11162. * @param int $type type of gradient.
  11163. * @param array $col1 first color (RGB components).
  11164. * @param array $col2 second color (RGB components).
  11165. * @param array $coords array of coordinates.
  11166. * @author Andreas W�rmser, Nicola Asuni
  11167. * @since 3.1.000 (2008-06-09)
  11168. * @access protected
  11169. */
  11170. protected function Gradient($type, $col1, $col2, $coords) {
  11171. $n = count($this->gradients) + 1;
  11172. $this->gradients[$n]['type'] = $type;
  11173. if (!isset($col1[1])) {
  11174. $col1[1]=$col1[2]=$col1[0];
  11175. }
  11176. $this->gradients[$n]['col1'] = sprintf('%.3F %.3F %.3F', ($col1[0]/255), ($col1[1]/255), ($col1[2]/255));
  11177. if (!isset($col2[1])) {
  11178. $col2[1] = $col2[2] = $col2[0];
  11179. }
  11180. $this->gradients[$n]['col2'] = sprintf('%.3F %.3F %.3F', ($col2[0]/255), ($col2[1]/255), ($col2[2]/255));
  11181. $this->gradients[$n]['coords'] = $coords;
  11182. //paint the gradient
  11183. $this->_out('/Sh'.$n.' sh');
  11184. //restore previous Graphic State
  11185. $this->_out('Q');
  11186. }
  11187. /**
  11188. * Output shaders.
  11189. * @author Andreas W�rmser, Nicola Asuni
  11190. * @since 3.1.000 (2008-06-09)
  11191. * @access protected
  11192. */
  11193. function _putshaders() {
  11194. foreach ($this->gradients as $id => $grad) {
  11195. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  11196. $this->_newobj();
  11197. $this->_out('<<');
  11198. $this->_out('/FunctionType 2');
  11199. $this->_out('/Domain [0.0 1.0]');
  11200. $this->_out('/C0 ['.$grad['col1'].']');
  11201. $this->_out('/C1 ['.$grad['col2'].']');
  11202. $this->_out('/N 1');
  11203. $this->_out('>>');
  11204. $this->_out('endobj');
  11205. $f1 = $this->n;
  11206. }
  11207. $this->_newobj();
  11208. $this->_out('<<');
  11209. $this->_out('/ShadingType '.$grad['type']);
  11210. $this->_out('/ColorSpace /DeviceRGB');
  11211. if ($grad['type'] == 2) {
  11212. $this->_out(sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]));
  11213. $this->_out('/Function '.$f1.' 0 R');
  11214. $this->_out('/Extend [true true] ');
  11215. $this->_out('>>');
  11216. } elseif ($grad['type'] == 3) {
  11217. //x0, y0, r0, x1, y1, r1
  11218. //at this this time radius of inner circle is 0
  11219. $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]));
  11220. $this->_out('/Function '.$f1.' 0 R');
  11221. $this->_out('/Extend [true true] ');
  11222. $this->_out('>>');
  11223. } elseif ($grad['type'] == 6) {
  11224. $this->_out('/BitsPerCoordinate 16');
  11225. $this->_out('/BitsPerComponent 8');
  11226. $this->_out('/Decode[0 1 0 1 0 1 0 1 0 1]');
  11227. $this->_out('/BitsPerFlag 8');
  11228. $this->_out('/Length '.strlen($grad['stream']));
  11229. $this->_out('>>');
  11230. $this->_putstream($grad['stream']);
  11231. }
  11232. $this->_out('endobj');
  11233. $this->gradients[$id]['id'] = $this->n;
  11234. }
  11235. }
  11236. /**
  11237. * Output an arc
  11238. * @author Maxime Delorme, Nicola Asuni
  11239. * @since 3.1.000 (2008-06-09)
  11240. * @access protected
  11241. */
  11242. protected function _outarc($x1, $y1, $x2, $y2, $x3, $y3 ) {
  11243. $h = $this->h;
  11244. $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));
  11245. }
  11246. /**
  11247. * Draw the sector of a circle.
  11248. * It can be used for instance to render pie charts.
  11249. * @param float $xc abscissa of the center.
  11250. * @param float $yc ordinate of the center.
  11251. * @param float $r radius.
  11252. * @param float $a start angle (in degrees).
  11253. * @param float $b end angle (in degrees).
  11254. * @param string $style: D, F, FD or DF (draw, fill, fill and draw). Default: FD.
  11255. * @param float $cw: indicates whether to go clockwise (default: true).
  11256. * @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.
  11257. * @author Maxime Delorme, Nicola Asuni
  11258. * @since 3.1.000 (2008-06-09)
  11259. * @access public
  11260. */
  11261. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  11262. if ($this->rtl) {
  11263. $xc = $this->w - $xc;
  11264. }
  11265. if ($cw) {
  11266. $d = $b;
  11267. $b = $o - $a;
  11268. $a = $o - $d;
  11269. } else {
  11270. $b += $o;
  11271. $a += $o;
  11272. }
  11273. $a = ($a % 360) + 360;
  11274. $b = ($b % 360) + 360;
  11275. if ($a > $b) {
  11276. $b +=360;
  11277. }
  11278. $b = $b / 360 * 2 * M_PI;
  11279. $a = $a / 360 * 2 * M_PI;
  11280. $d = $b - $a;
  11281. if ($d == 0 ) {
  11282. $d = 2 * M_PI;
  11283. }
  11284. $k = $this->k;
  11285. $hp = $this->h;
  11286. if ($style=='F') {
  11287. $op = 'f';
  11288. } elseif ($style=='FD' or $style=='DF') {
  11289. $op = 'b';
  11290. } else {
  11291. $op = 's';
  11292. }
  11293. if (sin($d/2)) {
  11294. $MyArc = 4/3 * (1 - cos($d/2)) / sin($d/2) * $r;
  11295. }
  11296. //first put the center
  11297. $this->_out(sprintf('%.2F %.2F m', ($xc)*$k, ($hp-$yc)*$k));
  11298. //put the first point
  11299. $this->_out(sprintf('%.2F %.2F l', ($xc+$r*cos($a))*$k, (($hp-($yc-$r*sin($a)))*$k)));
  11300. //draw the arc
  11301. if ($d < (M_PI/2)) {
  11302. $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));
  11303. } else {
  11304. $b = $a + $d/4;
  11305. $MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
  11306. $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));
  11307. $a = $b;
  11308. $b = $a + $d/4;
  11309. $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));
  11310. $a = $b;
  11311. $b = $a + $d/4;
  11312. $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) );
  11313. $a = $b;
  11314. $b = $a + $d/4;
  11315. $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));
  11316. }
  11317. //terminate drawing
  11318. $this->_out($op);
  11319. }
  11320. /**
  11321. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  11322. * Only vector drawing is supported, not text or bitmap.
  11323. * 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).
  11324. * @param string $file Name of the file containing the image.
  11325. * @param float $x Abscissa of the upper-left corner.
  11326. * @param float $y Ordinate of the upper-left corner.
  11327. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  11328. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  11329. * @param mixed $link URL or identifier returned by AddLink().
  11330. * @param boolean useBoundingBox specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  11331. * @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>
  11332. * @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>
  11333. * @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>
  11334. * @author Valentin Schmidt, Nicola Asuni
  11335. * @since 3.1.000 (2008-06-09)
  11336. * @access public
  11337. */
  11338. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0) {
  11339. if ($x === '') {
  11340. $x = $this->x;
  11341. }
  11342. if ($y === '') {
  11343. $y = $this->y;
  11344. }
  11345. $k = $this->k;
  11346. $data = file_get_contents($file);
  11347. if ($data === false) {
  11348. $this->Error('EPS file not found: '.$file);
  11349. }
  11350. $regs = array();
  11351. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  11352. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  11353. if (count($regs) > 1) {
  11354. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  11355. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  11356. $versexp = explode(' ', $version_str);
  11357. $version = (float)array_pop($versexp);
  11358. if ($version >= 9) {
  11359. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  11360. }
  11361. }
  11362. }
  11363. // strip binary bytes in front of PS-header
  11364. $start = strpos($data, '%!PS-Adobe');
  11365. if ($start > 0) {
  11366. $data = substr($data, $start);
  11367. }
  11368. // find BoundingBox params
  11369. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  11370. if (count($regs) > 1) {
  11371. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  11372. } else {
  11373. $this->Error('No BoundingBox found in EPS file: '.$file);
  11374. }
  11375. $start = strpos($data, '%%EndSetup');
  11376. if ($start === false) {
  11377. $start = strpos($data, '%%EndProlog');
  11378. }
  11379. if ($start === false) {
  11380. $start = strpos($data, '%%BoundingBox');
  11381. }
  11382. $data = substr($data, $start);
  11383. $end = strpos($data, '%%PageTrailer');
  11384. if ($end===false) {
  11385. $end = strpos($data, 'showpage');
  11386. }
  11387. if ($end) {
  11388. $data = substr($data, 0, $end);
  11389. }
  11390. if ($w > 0) {
  11391. $scale_x = $w / (($x2 - $x1) / $k);
  11392. if ($h > 0) {
  11393. $scale_y = $h / (($y2 - $y1) / $k);
  11394. } else {
  11395. $scale_y = $scale_x;
  11396. $h = ($y2 - $y1) / $k * $scale_y;
  11397. }
  11398. } else {
  11399. if ($h > 0) {
  11400. $scale_y = $h / (($y2 - $y1) / $k);
  11401. $scale_x = $scale_y;
  11402. $w = ($x2-$x1) / $k * $scale_x;
  11403. } else {
  11404. $w = ($x2 - $x1) / $k;
  11405. $h = ($y2 - $y1) / $k;
  11406. }
  11407. }
  11408. // Check whether we need a new page first as this does not fit
  11409. $prev_x = $this->x;
  11410. if ($this->checkPageBreak($h, $y)) {
  11411. $y = $this->GetY() + $this->cMargin;
  11412. if ($this->rtl) {
  11413. $x += ($prev_x - $this->x);
  11414. } else {
  11415. $x += ($this->x - $prev_x);
  11416. }
  11417. }
  11418. // set bottomcoordinates
  11419. $this->img_rb_y = $y + $h;
  11420. // set alignment
  11421. if ($this->rtl) {
  11422. if ($palign == 'L') {
  11423. $ximg = $this->lMargin;
  11424. // set right side coordinate
  11425. $this->img_rb_x = $ximg + $w;
  11426. } elseif ($palign == 'C') {
  11427. $ximg = ($this->w - $x - $w) / 2;
  11428. // set right side coordinate
  11429. $this->img_rb_x = $ximg + $w;
  11430. } else {
  11431. $ximg = $this->w - $x - $w;
  11432. // set left side coordinate
  11433. $this->img_rb_x = $ximg;
  11434. }
  11435. } else {
  11436. if ($palign == 'R') {
  11437. $ximg = $this->w - $this->rMargin - $w;
  11438. // set left side coordinate
  11439. $this->img_rb_x = $ximg;
  11440. } elseif ($palign == 'C') {
  11441. $ximg = ($this->w - $x - $w) / 2;
  11442. // set right side coordinate
  11443. $this->img_rb_x = $ximg + $w;
  11444. } else {
  11445. $ximg = $x;
  11446. // set right side coordinate
  11447. $this->img_rb_x = $ximg + $w;
  11448. }
  11449. }
  11450. if ($useBoundingBox) {
  11451. $dx = $ximg * $k - $x1;
  11452. $dy = $y * $k - $y1;
  11453. } else {
  11454. $dx = $ximg * $k;
  11455. $dy = $y * $k;
  11456. }
  11457. // save the current graphic state
  11458. $this->_out('q'.$this->epsmarker);
  11459. // translate
  11460. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  11461. // scale
  11462. if (isset($scale_x)) {
  11463. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  11464. }
  11465. // handle pc/unix/mac line endings
  11466. preg_match('/[\r\n]+/s', $data, $regs);
  11467. $lines = explode($regs[0], $data);
  11468. $u=0;
  11469. $cnt = count($lines);
  11470. for ($i=0; $i < $cnt; ++$i) {
  11471. $line = $lines[$i];
  11472. if (($line == '') OR ($line{0} == '%')) {
  11473. continue;
  11474. }
  11475. $len = strlen($line);
  11476. $chunks = explode(' ', $line);
  11477. $cmd = array_pop($chunks);
  11478. // RGB
  11479. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  11480. $b = array_pop($chunks);
  11481. $g = array_pop($chunks);
  11482. $r = array_pop($chunks);
  11483. $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!
  11484. continue;
  11485. }
  11486. switch ($cmd) {
  11487. case 'm':
  11488. case 'l':
  11489. case 'v':
  11490. case 'y':
  11491. case 'c':
  11492. case 'k':
  11493. case 'K':
  11494. case 'g':
  11495. case 'G':
  11496. case 's':
  11497. case 'S':
  11498. case 'J':
  11499. case 'j':
  11500. case 'w':
  11501. case 'M':
  11502. case 'd':
  11503. case 'n':
  11504. case 'v': {
  11505. $this->_out($line);
  11506. break;
  11507. }
  11508. case 'x': {// custom fill color
  11509. list($c,$m,$y,$k) = $chunks;
  11510. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' k');
  11511. break;
  11512. }
  11513. case 'X': { // custom stroke color
  11514. list($c,$m,$y,$k) = $chunks;
  11515. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' K');
  11516. break;
  11517. }
  11518. case 'Y':
  11519. case 'N':
  11520. case 'V':
  11521. case 'L':
  11522. case 'C': {
  11523. $line{$len-1} = strtolower($cmd);
  11524. $this->_out($line);
  11525. break;
  11526. }
  11527. case 'b':
  11528. case 'B': {
  11529. $this->_out($cmd . '*');
  11530. break;
  11531. }
  11532. case 'f':
  11533. case 'F': {
  11534. if ($u > 0) {
  11535. $isU = false;
  11536. $max = min($i+5, $cnt);
  11537. for ($j=$i+1; $j < $max; ++$j)
  11538. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  11539. if ($isU) {
  11540. $this->_out('f*');
  11541. }
  11542. } else {
  11543. $this->_out('f*');
  11544. }
  11545. break;
  11546. }
  11547. case '*u': {
  11548. ++$u;
  11549. break;
  11550. }
  11551. case '*U': {
  11552. --$u;
  11553. break;
  11554. }
  11555. }
  11556. }
  11557. // restore previous graphic state
  11558. $this->_out($this->epsmarker.'Q');
  11559. if (!empty($border)) {
  11560. $bx = $x;
  11561. $by = $y;
  11562. $this->x = $x;
  11563. $this->y = $y;
  11564. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  11565. $this->x = $bx;
  11566. $this->y = $by;
  11567. }
  11568. if ($link) {
  11569. $this->Link($ximg, $y, $w, $h, $link, 0);
  11570. }
  11571. // set pointer to align the successive text/objects
  11572. switch($align) {
  11573. case 'T':{
  11574. $this->y = $y;
  11575. $this->x = $this->img_rb_x;
  11576. break;
  11577. }
  11578. case 'M':{
  11579. $this->y = $y + round($h/2);
  11580. $this->x = $this->img_rb_x;
  11581. break;
  11582. }
  11583. case 'B':{
  11584. $this->y = $this->img_rb_y;
  11585. $this->x = $this->img_rb_x;
  11586. break;
  11587. }
  11588. case 'N':{
  11589. $this->SetY($this->img_rb_y);
  11590. break;
  11591. }
  11592. default:{
  11593. break;
  11594. }
  11595. }
  11596. $this->endlinex = $this->img_rb_x;
  11597. }
  11598. /**
  11599. * Set document barcode.
  11600. * @param string $bc barcode
  11601. * @access public
  11602. */
  11603. public function setBarcode($bc='') {
  11604. $this->barcode = $bc;
  11605. }
  11606. /**
  11607. * Get current barcode.
  11608. * @return string
  11609. * @access public
  11610. * @since 4.0.012 (2008-07-24)
  11611. */
  11612. public function getBarcode() {
  11613. return $this->barcode;
  11614. }
  11615. /**
  11616. * Print a Linear Barcode.
  11617. * @param string $code code to print
  11618. * @param string $type type of barcode.
  11619. * @param int $x x position in user units
  11620. * @param int $y y position in user units
  11621. * @param int $w width in user units
  11622. * @param int $h height in user units
  11623. * @param float $xres width of the smallest bar in user units
  11624. * @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>
  11625. * @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>
  11626. * @author Nicola Asuni
  11627. * @since 3.1.000 (2008-06-09)
  11628. * @access public
  11629. */
  11630. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='') {
  11631. if ($this->empty_string($code)) {
  11632. return;
  11633. }
  11634. require_once(dirname(__FILE__).'/barcodes.php');
  11635. // save current graphic settings
  11636. $gvars = $this->getGraphicVars();
  11637. // create new barcode object
  11638. $barcodeobj = new TCPDFBarcode($code, $type);
  11639. $arrcode = $barcodeobj->getBarcodeArray();
  11640. if ($arrcode === false) {
  11641. $this->Error('Error in 1D barcode string');
  11642. }
  11643. // set default values
  11644. if (!isset($style['position'])) {
  11645. if ($this->rtl) {
  11646. $style['position'] = 'R';
  11647. } else {
  11648. $style['position'] = 'L';
  11649. }
  11650. }
  11651. if (!isset($style['padding'])) {
  11652. $style['padding'] = 0;
  11653. }
  11654. if (!isset($style['fgcolor'])) {
  11655. $style['fgcolor'] = array(0,0,0); // default black
  11656. }
  11657. if (!isset($style['bgcolor'])) {
  11658. $style['bgcolor'] = false; // default transparent
  11659. }
  11660. if (!isset($style['border'])) {
  11661. $style['border'] = false;
  11662. }
  11663. $fontsize = 0;
  11664. if (!isset($style['text'])) {
  11665. $style['text'] = false;
  11666. }
  11667. if ($style['text'] AND isset($style['font'])) {
  11668. if (isset($style['fontsize'])) {
  11669. $fontsize = $style['fontsize'];
  11670. }
  11671. $this->SetFont($style['font'], '', $fontsize);
  11672. }
  11673. if (!isset($style['stretchtext'])) {
  11674. $style['stretchtext'] = 4;
  11675. }
  11676. // set foreground color
  11677. $this->SetDrawColorArray($style['fgcolor']);
  11678. $this->SetTextColorArray($style['fgcolor']);
  11679. if ($this->empty_string($w) OR ($w <= 0)) {
  11680. if ($this->rtl) {
  11681. $w = $this->x - $this->lMargin;
  11682. } else {
  11683. $w = $this->w - $this->rMargin - $this->x;
  11684. }
  11685. }
  11686. if ($this->empty_string($x)) {
  11687. $x = $this->GetX();
  11688. }
  11689. if ($this->rtl) {
  11690. $x = $this->w - $x;
  11691. }
  11692. if ($this->empty_string($y)) {
  11693. $y = $this->GetY();
  11694. }
  11695. if ($this->empty_string($xres)) {
  11696. $xres = 0.4;
  11697. }
  11698. $fbw = ($arrcode['maxw'] * $xres) + (2 * $style['padding']);
  11699. $extraspace = ($this->cell_height_ratio * $fontsize / $this->k) + (2 * $style['padding']);
  11700. if ($this->empty_string($h) OR ($h <= 0)) {
  11701. $h = 10 + $extraspace;
  11702. }
  11703. $prev_x = $this->x;
  11704. if ($this->checkPageBreak($h, $y)) {
  11705. $y = $this->GetY() + $this->cMargin;
  11706. if ($this->rtl) {
  11707. $x += ($prev_x - $this->x);
  11708. } else {
  11709. $x += ($this->x - $prev_x);
  11710. }
  11711. }
  11712. // maximum bar heigth
  11713. $barh = $h - $extraspace;
  11714. switch ($style['position']) {
  11715. case 'L': { // left
  11716. if ($this->rtl) {
  11717. $xpos = $x - $w;
  11718. } else {
  11719. $xpos = $x;
  11720. }
  11721. break;
  11722. }
  11723. case 'C': { // center
  11724. $xdiff = (($w - $fbw) / 2);
  11725. if ($this->rtl) {
  11726. $xpos = $x - $w + $xdiff;
  11727. } else {
  11728. $xpos = $x + $xdiff;
  11729. }
  11730. break;
  11731. }
  11732. case 'R': { // right
  11733. if ($this->rtl) {
  11734. $xpos = $x - $fbw;
  11735. } else {
  11736. $xpos = $x + $w - $fbw;
  11737. }
  11738. break;
  11739. }
  11740. case 'S': { // stretch
  11741. $fbw = $w;
  11742. $xres = ($w - (2 * $style['padding'])) / $arrcode['maxw'];
  11743. if ($this->rtl) {
  11744. $xpos = $x - $w;
  11745. } else {
  11746. $xpos = $x;
  11747. }
  11748. break;
  11749. }
  11750. }
  11751. $xpos_rect = $xpos;
  11752. $xpos = $xpos_rect + $style['padding'];
  11753. $xpos_text = $xpos;
  11754. // barcode is always printed in LTR direction
  11755. $tempRTL = $this->rtl;
  11756. $this->rtl = false;
  11757. // print background color
  11758. if ($style['bgcolor']) {
  11759. $this->Rect($xpos_rect, $y, $fbw, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  11760. } elseif ($style['border']) {
  11761. $this->Rect($xpos_rect, $y, $fbw, $h, 'D');
  11762. }
  11763. // print bars
  11764. if ($arrcode !== false) {
  11765. foreach ($arrcode['bcode'] as $k => $v) {
  11766. $bw = ($v['w'] * $xres);
  11767. if ($v['t']) {
  11768. // draw a vertical bar
  11769. $ypos = $y + $style['padding'] + ($v['p'] * $barh / $arrcode['maxh']);
  11770. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  11771. }
  11772. $xpos += $bw;
  11773. }
  11774. }
  11775. // print text
  11776. if ($style['text']) {
  11777. // print text
  11778. $this->x = $xpos_text;
  11779. $this->y = $y + $style['padding'] + $barh;
  11780. $this->Cell(($arrcode['maxw'] * $xres), ($this->cell_height_ratio * $fontsize / $this->k), $code, 0, 0, 'C', 0, '', $style['stretchtext']);
  11781. }
  11782. // restore original direction
  11783. $this->rtl = $tempRTL;
  11784. // restore previous settings
  11785. $this->setGraphicVars($gvars);
  11786. // set bottomcoordinates
  11787. $this->img_rb_y = $y + $h;
  11788. if ($this->rtl) {
  11789. // set left side coordinate
  11790. $this->img_rb_x = ($this->w - $x - $w);
  11791. } else {
  11792. // set right side coordinate
  11793. $this->img_rb_x = $x + $w;
  11794. }
  11795. // set pointer to align the successive text/objects
  11796. switch($align) {
  11797. case 'T':{
  11798. $this->y = $y;
  11799. $this->x = $this->img_rb_x;
  11800. break;
  11801. }
  11802. case 'M':{
  11803. $this->y = $y + round($h/2);
  11804. $this->x = $this->img_rb_x;
  11805. break;
  11806. }
  11807. case 'B':{
  11808. $this->y = $this->img_rb_y;
  11809. $this->x = $this->img_rb_x;
  11810. break;
  11811. }
  11812. case 'N':{
  11813. $this->SetY($this->img_rb_y);
  11814. break;
  11815. }
  11816. default:{
  11817. break;
  11818. }
  11819. }
  11820. }
  11821. /**
  11822. * This function is DEPRECATED, please use the new write1DBarcode() function.
  11823. * @param int $x x position in user units
  11824. * @param int $y y position in user units
  11825. * @param int $w width in user units
  11826. * @param int $h height position in user units
  11827. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  11828. * @param string $style barcode style
  11829. * @param string $font font for text
  11830. * @param int $xres x resolution
  11831. * @param string $code code to print
  11832. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  11833. * @access public
  11834. * @see write1DBarcode()
  11835. */
  11836. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  11837. // convert old settings for the new write1DBarcode() function.
  11838. $xres = 1 / $xres;
  11839. $newstyle = array(
  11840. 'position' => 'L',
  11841. 'border' => false,
  11842. 'padding' => 0,
  11843. 'fgcolor' => array(0,0,0),
  11844. 'bgcolor' => false,
  11845. 'text' => true,
  11846. 'font' => $font,
  11847. 'fontsize' => 8,
  11848. 'stretchtext' => 4
  11849. );
  11850. if ($style & 1) {
  11851. $newstyle['border'] = true;
  11852. }
  11853. if ($style & 2) {
  11854. $newstyle['bgcolor'] = false;
  11855. }
  11856. if ($style & 4) {
  11857. $newstyle['position'] = 'C';
  11858. } elseif ($style & 8) {
  11859. $newstyle['position'] = 'L';
  11860. } elseif ($style & 16) {
  11861. $newstyle['position'] = 'R';
  11862. }
  11863. if ($style & 128) {
  11864. $newstyle['text'] = true;
  11865. }
  11866. if ($style & 256) {
  11867. $newstyle['stretchtext'] = 4;
  11868. }
  11869. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  11870. }
  11871. /**
  11872. * Print 2D Barcode.
  11873. * @param string $code code to print
  11874. * @param string $type type of barcode.
  11875. * @param int $x x position in user units
  11876. * @param int $y y position in user units
  11877. * @param int $w width in user units
  11878. * @param int $h height in user units
  11879. * @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>
  11880. * @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>
  11881. * @author Nicola Asuni
  11882. * @since 4.5.037 (2009-04-07)
  11883. * @access public
  11884. */
  11885. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='') {
  11886. if ($this->empty_string($code)) {
  11887. return;
  11888. }
  11889. require_once(dirname(__FILE__).'/2dbarcodes.php');
  11890. // save current graphic settings
  11891. $gvars = $this->getGraphicVars();
  11892. // create new barcode object
  11893. $barcodeobj = new TCPDF2DBarcode($code, $type);
  11894. $arrcode = $barcodeobj->getBarcodeArray();
  11895. if ($arrcode === false) {
  11896. $this->Error('Error in 2D barcode string');
  11897. }
  11898. // set default values
  11899. if (!isset($style['padding'])) {
  11900. $style['padding'] = 0;
  11901. }
  11902. if (!isset($style['fgcolor'])) {
  11903. $style['fgcolor'] = array(0,0,0); // default black
  11904. }
  11905. if (!isset($style['bgcolor'])) {
  11906. $style['bgcolor'] = false; // default transparent
  11907. }
  11908. if (!isset($style['border'])) {
  11909. $style['border'] = false;
  11910. }
  11911. // set foreground color
  11912. $this->SetDrawColorArray($style['fgcolor']);
  11913. if ($this->empty_string($x)) {
  11914. $x = $this->GetX();
  11915. }
  11916. if ($this->rtl) {
  11917. $x = $this->w - $x;
  11918. }
  11919. if ($this->empty_string($y)) {
  11920. $y = $this->GetY();
  11921. }
  11922. if ($this->empty_string($w) OR ($w <= 0)) {
  11923. if ($this->rtl) {
  11924. $w = $x - $this->lMargin;
  11925. } else {
  11926. $w = $this->w - $this->rMargin - $x;
  11927. }
  11928. }
  11929. if ($this->empty_string($h) OR ($h <= 0)) {
  11930. // 2d barcodes are square by default
  11931. $h = $w;
  11932. }
  11933. $prev_x = $this->x;
  11934. if ($this->checkPageBreak($h, $y)) {
  11935. $y = $this->GetY() + $this->cMargin;
  11936. if ($this->rtl) {
  11937. $x += ($prev_x - $this->x);
  11938. } else {
  11939. $x += ($this->x - $prev_x);
  11940. }
  11941. }
  11942. // calculate barcode size (excluding padding)
  11943. $bw = $w - (2 * $style['padding']);
  11944. $bh = $h - (2 * $style['padding']);
  11945. // calculate starting coordinates
  11946. if ($this->rtl) {
  11947. $xpos = $x - $w;
  11948. } else {
  11949. $xpos = $x;
  11950. }
  11951. $xpos += $style['padding'];
  11952. $ypos = $y + $style['padding'];
  11953. // barcode is always printed in LTR direction
  11954. $tempRTL = $this->rtl;
  11955. $this->rtl = false;
  11956. // print background color
  11957. if ($style['bgcolor']) {
  11958. $this->Rect($x, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  11959. } elseif ($style['border']) {
  11960. $this->Rect($x, $y, $w, $h, 'D');
  11961. }
  11962. // print barcode cells
  11963. if ($arrcode !== false) {
  11964. $rows = $arrcode['num_rows'];
  11965. $cols = $arrcode['num_cols'];
  11966. // calculate dimension of single barcode cell
  11967. $cw = $bw / $cols;
  11968. $ch = $bh / $rows;
  11969. // for each row
  11970. for ($r = 0; $r < $rows; ++$r) {
  11971. $xr = $xpos;
  11972. // for each column
  11973. for ($c = 0; $c < $cols; ++$c) {
  11974. if ($arrcode['bcode'][$r][$c] == 1) {
  11975. // draw a single barcode cell
  11976. $this->Rect($xr, $ypos, $cw, $ch, 'F', array(), $style['fgcolor']);
  11977. }
  11978. $xr += $cw;
  11979. }
  11980. $ypos += $ch;
  11981. }
  11982. }
  11983. // restore original direction
  11984. $this->rtl = $tempRTL;
  11985. // restore previous settings
  11986. $this->setGraphicVars($gvars);
  11987. // set bottomcoordinates
  11988. $this->img_rb_y = $y + $h;
  11989. if ($this->rtl) {
  11990. // set left side coordinate
  11991. $this->img_rb_x = ($this->w - $x - $w);
  11992. } else {
  11993. // set right side coordinate
  11994. $this->img_rb_x = $x + $w;
  11995. }
  11996. // set pointer to align the successive text/objects
  11997. switch($align) {
  11998. case 'T':{
  11999. $this->y = $y;
  12000. $this->x = $this->img_rb_x;
  12001. break;
  12002. }
  12003. case 'M':{
  12004. $this->y = $y + round($h/2);
  12005. $this->x = $this->img_rb_x;
  12006. break;
  12007. }
  12008. case 'B':{
  12009. $this->y = $this->img_rb_y;
  12010. $this->x = $this->img_rb_x;
  12011. break;
  12012. }
  12013. case 'N':{
  12014. $this->SetY($this->img_rb_y);
  12015. break;
  12016. }
  12017. default:{
  12018. break;
  12019. }
  12020. }
  12021. }
  12022. /**
  12023. * Returns an array containing current margins:
  12024. * <ul>
  12025. <li>$ret['left'] = left margin</li>
  12026. <li>$ret['right'] = right margin</li>
  12027. <li>$ret['top'] = top margin</li>
  12028. <li>$ret['bottom'] = bottom margin</li>
  12029. <li>$ret['header'] = header margin</li>
  12030. <li>$ret['footer'] = footer margin</li>
  12031. <li>$ret['cell'] = cell margin</li>
  12032. * </ul>
  12033. * @return array containing all margins measures
  12034. * @access public
  12035. * @since 3.2.000 (2008-06-23)
  12036. */
  12037. public function getMargins() {
  12038. $ret = array(
  12039. 'left' => $this->lMargin,
  12040. 'right' => $this->rMargin,
  12041. 'top' => $this->tMargin,
  12042. 'bottom' => $this->bMargin,
  12043. 'header' => $this->header_margin,
  12044. 'footer' => $this->footer_margin,
  12045. 'cell' => $this->cMargin,
  12046. );
  12047. return $ret;
  12048. }
  12049. /**
  12050. * Returns an array containing original margins:
  12051. * <ul>
  12052. <li>$ret['left'] = left margin</li>
  12053. <li>$ret['right'] = right margin</li>
  12054. * </ul>
  12055. * @return array containing all margins measures
  12056. * @access public
  12057. * @since 4.0.012 (2008-07-24)
  12058. */
  12059. public function getOriginalMargins() {
  12060. $ret = array(
  12061. 'left' => $this->original_lMargin,
  12062. 'right' => $this->original_rMargin
  12063. );
  12064. return $ret;
  12065. }
  12066. /**
  12067. * Returns the current font size.
  12068. * @return current font size
  12069. * @access public
  12070. * @since 3.2.000 (2008-06-23)
  12071. */
  12072. public function getFontSize() {
  12073. return $this->FontSize;
  12074. }
  12075. /**
  12076. * Returns the current font size in points unit.
  12077. * @return current font size in points unit
  12078. * @access public
  12079. * @since 3.2.000 (2008-06-23)
  12080. */
  12081. public function getFontSizePt() {
  12082. return $this->FontSizePt;
  12083. }
  12084. /**
  12085. * Returns the current font family name.
  12086. * @return string current font family name
  12087. * @access public
  12088. * @since 4.3.008 (2008-12-05)
  12089. */
  12090. public function getFontFamily() {
  12091. return $this->FontFamily;
  12092. }
  12093. /**
  12094. * Returns the current font style.
  12095. * @return string current font style
  12096. * @access public
  12097. * @since 4.3.008 (2008-12-05)
  12098. */
  12099. public function getFontStyle() {
  12100. return $this->FontStyle;
  12101. }
  12102. /**
  12103. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  12104. * 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 />
  12105. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  12106. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  12107. * @param float $h Cell minimum height. The cell extends automatically if needed.
  12108. * @param float $x upper-left corner X coordinate
  12109. * @param float $y upper-left corner Y coordinate
  12110. * @param string $html html text to print. Default value: empty string.
  12111. * @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>
  12112. * @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>
  12113. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  12114. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  12115. * @param boolean $reseth if true reset the last cell height (default true).
  12116. * @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>
  12117. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  12118. * @access public
  12119. * @uses MultiCell()
  12120. * @see Multicell(), writeHTML()
  12121. */
  12122. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true) {
  12123. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0);
  12124. }
  12125. /**
  12126. * Returns the HTML DOM array.
  12127. * <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>
  12128. * @param string $html html code
  12129. * @return array
  12130. * @access protected
  12131. * @since 3.2.000 (2008-06-20)
  12132. */
  12133. protected function getHtmlDomArray($html) {
  12134. // remove all unsupported tags (the line below lists all supported tags)
  12135. $html = strip_tags($html, '<marker/><a><b><blockquote><br><br/><dd><del><div><dl><dt><em><font><form><h1><h2><h3><h4><h5><h6><hr><i><img><input><label><li><ol><option><p><pre><select><small><span><strong><sub><sup><table><tablehead><tcpdf><td><textarea><th><thead><tr><tt><u><ul>');
  12136. //replace some blank characters
  12137. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  12138. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dt|form|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  12139. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  12140. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  12141. $html = strtr($html, $repTable);
  12142. $offset = 0;
  12143. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  12144. $html_a = substr($html, 0, $offset);
  12145. $html_b = substr($html, $offset, ($pos - $offset + 6));
  12146. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  12147. // preserve newlines on <pre> tag
  12148. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  12149. }
  12150. $html = $html_a.$html_b.substr($html, $pos + 6);
  12151. $offset = strlen($html_a.$html_b);
  12152. }
  12153. $offset = 0;
  12154. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  12155. $html_a = substr($html, 0, $offset);
  12156. $html_b = substr($html, $offset, ($pos - $offset + 11));
  12157. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  12158. // preserve newlines on <textarea> tag
  12159. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  12160. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  12161. }
  12162. $html = $html_a.$html_b.substr($html, $pos + 11);
  12163. $offset = strlen($html_a.$html_b);
  12164. }
  12165. $html = preg_replace("'([\s]*)<option'si", "<option", $html);
  12166. $html = preg_replace("'</option>([\s]*)'si", "</option>", $html);
  12167. $offset = 0;
  12168. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  12169. $html_a = substr($html, 0, $offset);
  12170. $html_b = substr($html, $offset, ($pos - $offset + 9));
  12171. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  12172. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2\t\\4\r", $html_b);
  12173. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2\r", $html_b);
  12174. }
  12175. $html = $html_a.$html_b.substr($html, $pos + 9);
  12176. $offset = strlen($html_a.$html_b);
  12177. }
  12178. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  12179. $html = preg_replace("'([\s]+)</select>'si", "\" />", $html);
  12180. $html = str_replace("\n", ' ', $html);
  12181. // restore textarea newlines
  12182. $html = str_replace('<TBR>', "\n", $html);
  12183. // remove extra spaces from code
  12184. $html = preg_replace('/[\s]+<\/(table|tr|td|th|ul|ol|li)>/', '</\\1>', $html);
  12185. $html = preg_replace('/[\s]+<(tr|td|th|ul|ol|li|br)/', '<\\1', $html);
  12186. $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);
  12187. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  12188. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  12189. $html = preg_replace('/<img/', ' <img', $html);
  12190. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  12191. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  12192. $html = preg_replace('/<textarea([^\>]*)>/xi', '<textarea\\1 value="', $html);
  12193. $html = preg_replace('/<\/textarea>/', '" />', $html);
  12194. // trim string
  12195. $html = preg_replace('/^[\s]+/', '', $html);
  12196. $html = preg_replace('/[\s]+$/', '', $html);
  12197. // pattern for generic tag
  12198. $tagpattern = '/(<[^>]+>)/';
  12199. // explodes the string
  12200. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  12201. // count elements
  12202. $maxel = count($a);
  12203. $elkey = 0;
  12204. $key = 0;
  12205. // create an array of elements
  12206. $dom = array();
  12207. $dom[$key] = array();
  12208. // set first void element
  12209. $dom[$key]['tag'] = false;
  12210. $dom[$key]['value'] = '';
  12211. $dom[$key]['parent'] = 0;
  12212. $dom[$key]['fontname'] = $this->FontFamily;
  12213. $dom[$key]['fontstyle'] = $this->FontStyle;
  12214. $dom[$key]['fontsize'] = $this->FontSizePt;
  12215. $dom[$key]['bgcolor'] = false;
  12216. $dom[$key]['fgcolor'] = $this->fgcolor;
  12217. $dom[$key]['align'] = '';
  12218. $dom[$key]['listtype'] = '';
  12219. $dom[$key]['text-indent'] = 0;
  12220. $thead = false; // true when we are inside the THEAD tag
  12221. ++$key;
  12222. $level = array();
  12223. array_push($level, 0); // root
  12224. while ($elkey < $maxel) {
  12225. $dom[$key] = array();
  12226. $element = $a[$elkey];
  12227. $dom[$key]['elkey'] = $elkey;
  12228. if (preg_match($tagpattern, $element)) {
  12229. // html tag
  12230. $element = substr($element, 1, -1);
  12231. // get tag name
  12232. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  12233. $tagname = strtolower($tag[1]);
  12234. // check if we are inside a table header
  12235. if ($tagname == 'thead') {
  12236. if ($element{0} == '/') {
  12237. $thead = false;
  12238. } else {
  12239. $thead = true;
  12240. }
  12241. ++$elkey;
  12242. continue;
  12243. }
  12244. $dom[$key]['tag'] = true;
  12245. $dom[$key]['value'] = $tagname;
  12246. if ($element{0} == '/') {
  12247. // closing html tag
  12248. $dom[$key]['opening'] = false;
  12249. $dom[$key]['parent'] = end($level);
  12250. array_pop($level);
  12251. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  12252. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  12253. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  12254. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  12255. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  12256. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  12257. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  12258. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  12259. }
  12260. // set the number of columns in table tag
  12261. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  12262. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  12263. }
  12264. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  12265. $dom[($dom[$key]['parent'])]['content'] = '';
  12266. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  12267. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  12268. }
  12269. $key = $i;
  12270. }
  12271. // store header rows on a new table
  12272. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  12273. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  12274. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  12275. }
  12276. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  12277. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  12278. }
  12279. }
  12280. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  12281. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  12282. }
  12283. } else {
  12284. // opening html tag
  12285. $dom[$key]['opening'] = true;
  12286. $dom[$key]['parent'] = end($level);
  12287. if (substr($element, -1, 1) != '/') {
  12288. // not self-closing tag
  12289. array_push($level, $key);
  12290. $dom[$key]['self'] = false;
  12291. } else {
  12292. $dom[$key]['self'] = true;
  12293. }
  12294. // copy some values from parent
  12295. $parentkey = 0;
  12296. if ($key > 0) {
  12297. $parentkey = $dom[$key]['parent'];
  12298. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  12299. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  12300. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  12301. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  12302. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  12303. $dom[$key]['align'] = $dom[$parentkey]['align'];
  12304. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  12305. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  12306. }
  12307. // get attributes
  12308. preg_match_all('/([^=\s]*)=["]?([^"]*)["]?/', $element, $attr_array, PREG_PATTERN_ORDER);
  12309. $dom[$key]['attribute'] = array(); // reset attribute array
  12310. while (list($id, $name) = each($attr_array[1])) {
  12311. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  12312. }
  12313. // split style attributes
  12314. if (isset($dom[$key]['attribute']['style'])) {
  12315. // get style attributes
  12316. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  12317. $dom[$key]['style'] = array(); // reset style attribute array
  12318. while (list($id, $name) = each($style_array[1])) {
  12319. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  12320. }
  12321. // --- get some style attributes ---
  12322. if (isset($dom[$key]['style']['font-family'])) {
  12323. // font family
  12324. if (isset($dom[$key]['style']['font-family'])) {
  12325. $fontslist = preg_split('/[,]/', strtolower($dom[$key]['style']['font-family']));
  12326. foreach ($fontslist as $font) {
  12327. $font = trim(strtolower($font));
  12328. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  12329. $dom[$key]['fontname'] = $font;
  12330. break;
  12331. }
  12332. }
  12333. }
  12334. }
  12335. // list-style-type
  12336. if (isset($dom[$key]['style']['list-style-type'])) {
  12337. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  12338. if ($dom[$key]['listtype'] == 'inherit') {
  12339. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  12340. }
  12341. }
  12342. // text-indent
  12343. if (isset($dom[$key]['style']['text-indent'])) {
  12344. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  12345. if ($dom[$key]['text-indent'] == 'inherit') {
  12346. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  12347. }
  12348. }
  12349. // font size
  12350. if (isset($dom[$key]['style']['font-size'])) {
  12351. $fsize = trim($dom[$key]['style']['font-size']);
  12352. switch ($fsize) {
  12353. // absolute-size
  12354. case 'xx-small': {
  12355. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  12356. break;
  12357. }
  12358. case 'x-small': {
  12359. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  12360. break;
  12361. }
  12362. case 'small': {
  12363. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  12364. break;
  12365. }
  12366. case 'medium': {
  12367. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  12368. break;
  12369. }
  12370. case 'large': {
  12371. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  12372. break;
  12373. }
  12374. case 'x-large': {
  12375. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  12376. break;
  12377. }
  12378. case 'xx-large': {
  12379. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  12380. break;
  12381. }
  12382. // relative-size
  12383. case 'smaller': {
  12384. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  12385. break;
  12386. }
  12387. case 'larger': {
  12388. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  12389. break;
  12390. }
  12391. default: {
  12392. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  12393. }
  12394. }
  12395. }
  12396. // font style
  12397. if (isset($dom[$key]['style']['font-weight']) AND (strtolower($dom[$key]['style']['font-weight']{0}) == 'b')) {
  12398. $dom[$key]['fontstyle'] .= 'B';
  12399. }
  12400. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
  12401. $dom[$key]['fontstyle'] .= '"I';
  12402. }
  12403. // font color
  12404. if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
  12405. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
  12406. }
  12407. // background color
  12408. if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
  12409. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
  12410. }
  12411. // text-decoration
  12412. if (isset($dom[$key]['style']['text-decoration'])) {
  12413. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  12414. foreach ($decors as $dec) {
  12415. $dec = trim($dec);
  12416. if (!$this->empty_string($dec)) {
  12417. if ($dec{0} == 'u') {
  12418. $dom[$key]['fontstyle'] .= 'U';
  12419. } elseif ($dec{0} == 'l') {
  12420. $dom[$key]['fontstyle'] .= 'D';
  12421. }
  12422. }
  12423. }
  12424. }
  12425. // check for width attribute
  12426. if (isset($dom[$key]['style']['width'])) {
  12427. $dom[$key]['width'] = $dom[$key]['style']['width'];
  12428. }
  12429. // check for height attribute
  12430. if (isset($dom[$key]['style']['height'])) {
  12431. $dom[$key]['height'] = $dom[$key]['style']['height'];
  12432. }
  12433. // check for text alignment
  12434. if (isset($dom[$key]['style']['text-align'])) {
  12435. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
  12436. }
  12437. // check for border attribute
  12438. if (isset($dom[$key]['style']['border'])) {
  12439. $dom[$key]['attribute']['border'] = $dom[$key]['style']['border'];
  12440. }
  12441. // page-break-inside
  12442. if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
  12443. $dom[$key]['attribute']['nobr'] = 'true';
  12444. }
  12445. // page-break-before
  12446. if (isset($dom[$key]['style']['page-break-before'])) {
  12447. if ($dom[$key]['style']['page-break-before'] == 'always') {
  12448. $dom[$key]['attribute']['pagebreak'] = 'true';
  12449. } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
  12450. $dom[$key]['attribute']['pagebreak'] = 'left';
  12451. } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
  12452. $dom[$key]['attribute']['pagebreak'] = 'right';
  12453. }
  12454. }
  12455. // page-break-after
  12456. if (isset($dom[$key]['style']['page-break-after'])) {
  12457. if ($dom[$key]['style']['page-break-after'] == 'always') {
  12458. $dom[$key]['attribute']['pagebreakafter'] = 'true';
  12459. } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
  12460. $dom[$key]['attribute']['pagebreakafter'] = 'left';
  12461. } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
  12462. $dom[$key]['attribute']['pagebreakafter'] = 'right';
  12463. }
  12464. }
  12465. }
  12466. // check for font tag
  12467. if ($dom[$key]['value'] == 'font') {
  12468. // font family
  12469. if (isset($dom[$key]['attribute']['face'])) {
  12470. $fontslist = preg_split('/[,]/', strtolower($dom[$key]['attribute']['face']));
  12471. foreach ($fontslist as $font) {
  12472. $font = trim(strtolower($font));
  12473. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  12474. $dom[$key]['fontname'] = $font;
  12475. break;
  12476. }
  12477. }
  12478. }
  12479. // font size
  12480. if (isset($dom[$key]['attribute']['size'])) {
  12481. if ($key > 0) {
  12482. if ($dom[$key]['attribute']['size']{0} == '+') {
  12483. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  12484. } elseif ($dom[$key]['attribute']['size']{0} == '-') {
  12485. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  12486. } else {
  12487. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  12488. }
  12489. } else {
  12490. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  12491. }
  12492. }
  12493. }
  12494. // force natural alignment for lists
  12495. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  12496. AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  12497. if ($this->rtl) {
  12498. $dom[$key]['align'] = 'R';
  12499. } else {
  12500. $dom[$key]['align'] = 'L';
  12501. }
  12502. }
  12503. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  12504. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  12505. }
  12506. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  12507. $dom[$key]['fontstyle'] .= 'B';
  12508. }
  12509. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  12510. $dom[$key]['fontstyle'] .= 'I';
  12511. }
  12512. if ($dom[$key]['value'] == 'u') {
  12513. $dom[$key]['fontstyle'] .= 'U';
  12514. }
  12515. if ($dom[$key]['value'] == 'del') {
  12516. $dom[$key]['fontstyle'] .= 'D';
  12517. }
  12518. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  12519. $dom[$key]['fontname'] = $this->default_monospaced_font;
  12520. }
  12521. if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
  12522. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  12523. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  12524. $dom[$key]['fontstyle'] .= 'B';
  12525. }
  12526. if (($dom[$key]['value'] == 'table')) {
  12527. $dom[$key]['rows'] = 0; // number of rows
  12528. $dom[$key]['trids'] = array(); // IDs of TR elements
  12529. $dom[$key]['thead'] = ''; // table header rows
  12530. }
  12531. if (($dom[$key]['value'] == 'tr')) {
  12532. $dom[$key]['cols'] = 0;
  12533. // store the number of rows on table element
  12534. ++$dom[($dom[$key]['parent'])]['rows'];
  12535. // store the TR elements IDs on table element
  12536. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  12537. if ($thead) {
  12538. $dom[$key]['thead'] = true;
  12539. } else {
  12540. $dom[$key]['thead'] = false;
  12541. }
  12542. }
  12543. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  12544. if (isset($dom[$key]['attribute']['colspan'])) {
  12545. $colspan = intval($dom[$key]['attribute']['colspan']);
  12546. } else {
  12547. $colspan = 1;
  12548. }
  12549. $dom[$key]['attribute']['colspan'] = $colspan;
  12550. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  12551. }
  12552. // set foreground color attribute
  12553. if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
  12554. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
  12555. }
  12556. // set background color attribute
  12557. if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
  12558. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
  12559. }
  12560. // check for width attribute
  12561. if (isset($dom[$key]['attribute']['width'])) {
  12562. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  12563. }
  12564. // check for height attribute
  12565. if (isset($dom[$key]['attribute']['height'])) {
  12566. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  12567. }
  12568. // check for text alignment
  12569. if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  12570. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
  12571. }
  12572. } // end opening tag
  12573. } else {
  12574. // text
  12575. $dom[$key]['tag'] = false;
  12576. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  12577. $dom[$key]['parent'] = end($level);
  12578. }
  12579. ++$elkey;
  12580. ++$key;
  12581. }
  12582. return $dom;
  12583. }
  12584. /**
  12585. * Allows to preserve some HTML formatting (limited support).<br />
  12586. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  12587. * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul
  12588. * @param string $html text to display
  12589. * @param boolean $ln if true add a new line after text (default = true)
  12590. * @param int $fill Indicates if the background must be painted (true) or transparent (false).
  12591. * @param boolean $reseth if true reset the last cell height (default false).
  12592. * @param boolean $cell if true add the default cMargin space to each Write (default false).
  12593. * @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>
  12594. * @access public
  12595. */
  12596. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  12597. $gvars = $this->getGraphicVars();
  12598. // store current values
  12599. $prevPage = $this->page;
  12600. $prevlMargin = $this->lMargin;
  12601. $prevrMargin = $this->rMargin;
  12602. $curfontname = $this->FontFamily;
  12603. $curfontstyle = $this->FontStyle;
  12604. $curfontsize = $this->FontSizePt;
  12605. $this->newline = true;
  12606. $startlinepage = $this->page;
  12607. $minstartliney = $this->y;
  12608. $startlinex = $this->x;
  12609. $startliney = $this->y;
  12610. $yshift = 0;
  12611. $newline = true;
  12612. $loop = 0;
  12613. $curpos = 0;
  12614. $this_method_vars = array();
  12615. $undo = false;
  12616. $blocktags = array('blockquote','br','dd','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','ul','tcpdf');
  12617. $this->premode = false;
  12618. if (isset($this->PageAnnots[$this->page])) {
  12619. $pask = count($this->PageAnnots[$this->page]);
  12620. } else {
  12621. $pask = 0;
  12622. }
  12623. if (isset($this->footerlen[$this->page])) {
  12624. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  12625. } else {
  12626. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  12627. }
  12628. $startlinepos = $this->footerpos[$this->page];
  12629. $lalign = $align;
  12630. $plalign = $align;
  12631. if ($this->rtl) {
  12632. $w = $this->x - $this->lMargin;
  12633. } else {
  12634. $w = $this->w - $this->rMargin - $this->x;
  12635. }
  12636. $w -= (2 * $this->cMargin);
  12637. if ($cell) {
  12638. if ($this->rtl) {
  12639. $this->x -= $this->cMargin;
  12640. } else {
  12641. $this->x += $this->cMargin;
  12642. }
  12643. }
  12644. if ($this->customlistindent >= 0) {
  12645. $this->listindent = $this->customlistindent;
  12646. } else {
  12647. $this->listindent = $this->GetStringWidth('0000');
  12648. }
  12649. // save previous states
  12650. $prev_listnum = $this->listnum;
  12651. $prev_listordered = $this->listordered;
  12652. $prev_listcount = $this->listcount;
  12653. $prev_lispacer = $this->lispacer;
  12654. $this->listnum = 0;
  12655. $this->listordered = array();
  12656. $this->listcount = array();
  12657. $this->lispacer = '';
  12658. if (($this->empty_string($this->lasth)) OR ($reseth)) {
  12659. //set row height
  12660. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12661. }
  12662. $dom = $this->getHtmlDomArray($html);
  12663. $maxel = count($dom);
  12664. $key = 0;
  12665. while ($key < $maxel) {
  12666. if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
  12667. // check for pagebreak
  12668. if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
  12669. $this->AddPage();
  12670. }
  12671. if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  12672. OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  12673. $this->AddPage();
  12674. }
  12675. }
  12676. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
  12677. if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  12678. $dom[$key]['attribute']['nobr'] = false;
  12679. } else {
  12680. // store current object
  12681. $this->startTransaction();
  12682. // save this method vars
  12683. $this_method_vars['html'] = $html;
  12684. $this_method_vars['ln'] = $ln;
  12685. $this_method_vars['fill'] = $fill;
  12686. $this_method_vars['reseth'] = $reseth;
  12687. $this_method_vars['cell'] = $cell;
  12688. $this_method_vars['align'] = $align;
  12689. $this_method_vars['gvars'] = $gvars;
  12690. $this_method_vars['prevPage'] = $prevPage;
  12691. $this_method_vars['prevlMargin'] = $prevlMargin;
  12692. $this_method_vars['prevrMargin'] = $prevrMargin;
  12693. $this_method_vars['curfontname'] = $curfontname;
  12694. $this_method_vars['curfontstyle'] = $curfontstyle;
  12695. $this_method_vars['curfontsize'] = $curfontsize;
  12696. $this_method_vars['minstartliney'] = $minstartliney;
  12697. $this_method_vars['yshift'] = $yshift;
  12698. $this_method_vars['startlinepage'] = $startlinepage;
  12699. $this_method_vars['startlinepos'] = $startlinepos;
  12700. $this_method_vars['startlinex'] = $startlinex;
  12701. $this_method_vars['startliney'] = $startliney;
  12702. $this_method_vars['newline'] = $newline;
  12703. $this_method_vars['loop'] = $loop;
  12704. $this_method_vars['curpos'] = $curpos;
  12705. $this_method_vars['pask'] = $pask;
  12706. $this_method_vars['lalign'] = $lalign;
  12707. $this_method_vars['plalign'] = $plalign;
  12708. $this_method_vars['w'] = $w;
  12709. $this_method_vars['prev_listnum'] = $prev_listnum;
  12710. $this_method_vars['prev_listordered'] = $prev_listordered;
  12711. $this_method_vars['prev_listcount'] = $prev_listcount;
  12712. $this_method_vars['prev_lispacer'] = $prev_lispacer;
  12713. $this_method_vars['key'] = $key;
  12714. $this_method_vars['dom'] = $dom;
  12715. }
  12716. }
  12717. if ($dom[$key]['tag'] OR ($key == 0)) {
  12718. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  12719. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  12720. }
  12721. // vertically align image in line
  12722. if ((!$this->newline)
  12723. AND ($dom[$key]['value'] == 'img')
  12724. AND (isset($dom[$key]['attribute']['height']))
  12725. AND ($dom[$key]['attribute']['height'] > 0)) {
  12726. // get image height
  12727. $imgh = $this->getHTMLUnitToUnits($dom[$key]['attribute']['height'], $this->lasth, 'px');
  12728. if (!$this->InFooter) {
  12729. // check for page break
  12730. $this->checkPageBreak($imgh);
  12731. }
  12732. if ($this->page > $startlinepage) {
  12733. // fix line splitted over two pages
  12734. if (isset($this->footerlen[$startlinepage])) {
  12735. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  12736. }
  12737. // line to be moved one page forward
  12738. $pagebuff = $this->getPageBuffer($startlinepage);
  12739. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  12740. $tstart = substr($pagebuff, 0, $startlinepos);
  12741. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  12742. // remove line from previous page
  12743. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  12744. $pagebuff = $this->getPageBuffer($this->page);
  12745. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  12746. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  12747. // add line start to current page
  12748. $yshift = $minstartliney - $this->y;
  12749. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  12750. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  12751. // shift the annotations and links
  12752. if (isset($this->PageAnnots[$this->page])) {
  12753. $next_pask = count($this->PageAnnots[$this->page]);
  12754. } else {
  12755. $next_pask = 0;
  12756. }
  12757. if (isset($this->PageAnnots[$startlinepage])) {
  12758. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  12759. if ($pak >= $pask) {
  12760. $this->PageAnnots[$this->page][] = $pac;
  12761. unset($this->PageAnnots[$startlinepage][$pak]);
  12762. $npak = count($this->PageAnnots[$this->page]) - 1;
  12763. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  12764. }
  12765. }
  12766. }
  12767. $pask = $next_pask;
  12768. $startlinepos = $this->cntmrk[$this->page];
  12769. $startlinepage = $this->page;
  12770. $startliney = $this->y;
  12771. }
  12772. $this->y += (($curfontsize / $this->k) - $imgh);
  12773. $minstartliney = min($this->y, $minstartliney);
  12774. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize'])) {
  12775. // account for different font size
  12776. $pfontname = $curfontname;
  12777. $pfontstyle = $curfontstyle;
  12778. $pfontsize = $curfontsize;
  12779. $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
  12780. $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
  12781. $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
  12782. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)) {
  12783. $this->SetFont($fontname, $fontstyle, $fontsize);
  12784. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12785. if (is_numeric($fontsize) AND ($fontsize > 0)
  12786. AND is_numeric($curfontsize) AND ($curfontsize > 0)
  12787. AND ($fontsize != $curfontsize) AND (!$this->newline)
  12788. AND ($key < ($maxel - 1))
  12789. ) {
  12790. if ((!$this->newline) AND ($this->page > $startlinepage)) {
  12791. // fix lines splitted over two pages
  12792. if (isset($this->footerlen[$startlinepage])) {
  12793. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  12794. }
  12795. // line to be moved one page forward
  12796. $pagebuff = $this->getPageBuffer($startlinepage);
  12797. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  12798. $tstart = substr($pagebuff, 0, $startlinepos);
  12799. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  12800. // remove line start from previous page
  12801. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  12802. $pagebuff = $this->getPageBuffer($this->page);
  12803. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  12804. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  12805. // add line start to current page
  12806. $yshift = $minstartliney - $this->y;
  12807. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  12808. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  12809. // shift the annotations and links
  12810. if (isset($this->PageAnnots[$this->page])) {
  12811. $next_pask = count($this->PageAnnots[$this->page]);
  12812. } else {
  12813. $next_pask = 0;
  12814. }
  12815. if (isset($this->PageAnnots[$startlinepage])) {
  12816. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  12817. if ($pak >= $pask) {
  12818. $this->PageAnnots[$this->page][] = $pac;
  12819. unset($this->PageAnnots[$startlinepage][$pak]);
  12820. $npak = count($this->PageAnnots[$this->page]) - 1;
  12821. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  12822. }
  12823. }
  12824. }
  12825. $pask = $next_pask;
  12826. }
  12827. if (($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  12828. $this->y += (($curfontsize - $fontsize) / $this->k);
  12829. }
  12830. $minstartliney = min($this->y, $minstartliney);
  12831. }
  12832. $curfontname = $fontname;
  12833. $curfontstyle = $fontstyle;
  12834. $curfontsize = $fontsize;
  12835. }
  12836. }
  12837. if (($plalign == 'J') AND (in_array($dom[$key]['value'], $blocktags))) {
  12838. $plalign = '';
  12839. }
  12840. // get current position on page buffer
  12841. $curpos = $this->pagelen[$startlinepage];
  12842. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  12843. $this->SetFillColorArray($dom[$key]['bgcolor']);
  12844. $wfill = true;
  12845. } else {
  12846. $wfill = $fill | false;
  12847. }
  12848. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  12849. $this->SetTextColorArray($dom[$key]['fgcolor']);
  12850. }
  12851. if (isset($dom[$key]['align'])) {
  12852. $lalign = $dom[$key]['align'];
  12853. }
  12854. if ($this->empty_string($lalign)) {
  12855. $lalign = $align;
  12856. }
  12857. }
  12858. // align lines
  12859. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  12860. $newline = true;
  12861. // we are at the beginning of a new line
  12862. if (isset($startlinex)) {
  12863. $yshift = $minstartliney - $startliney;
  12864. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  12865. $yshift = 0;
  12866. }
  12867. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl))))) OR ($yshift < 0)) {
  12868. // the last line must be shifted to be aligned as requested
  12869. $linew = abs($this->endlinex - $startlinex);
  12870. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  12871. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  12872. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  12873. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  12874. } elseif (isset($opentagpos)) {
  12875. $midpos = $opentagpos;
  12876. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  12877. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  12878. $midpos = $this->footerpos[$startlinepage];
  12879. } else {
  12880. $midpos = 0;
  12881. }
  12882. if ($midpos > 0) {
  12883. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  12884. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  12885. } else {
  12886. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  12887. $pend = '';
  12888. }
  12889. // calculate shifting amount
  12890. $tw = $w;
  12891. if ($this->lMargin != $prevlMargin) {
  12892. $tw += ($prevlMargin - $this->lMargin);
  12893. }
  12894. if ($this->rMargin != $prevrMargin) {
  12895. $tw += ($prevrMargin - $this->rMargin);
  12896. }
  12897. $mdiff = abs($tw - $linew);
  12898. $t_x = 0;
  12899. if ($plalign == 'C') {
  12900. if ($this->rtl) {
  12901. $t_x = -($mdiff / 2);
  12902. } else {
  12903. $t_x = ($mdiff / 2);
  12904. }
  12905. } elseif (($plalign == 'R') AND (!$this->rtl)) {
  12906. // right alignment on LTR document
  12907. $t_x = $mdiff;
  12908. } elseif (($plalign == 'L') AND ($this->rtl)) {
  12909. // left alignment on RTL document
  12910. $t_x = -$mdiff;
  12911. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  12912. // Justification
  12913. if ($this->isRTLTextDir()) {
  12914. $t_x = $this->lMargin - $this->endlinex;
  12915. }
  12916. $one_space_width = $this->GetStringWidth(chr(32));
  12917. $no = 0; // spaces without trim
  12918. $ns = 0; // spaces with trim
  12919. $pmidtemp = $pmid;
  12920. // escape special characters
  12921. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  12922. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  12923. // search spaces
  12924. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  12925. $maxkk = count($lnstring[1]) - 1;
  12926. for ($kk=0; $kk <= $maxkk; ++$kk) {
  12927. // restore special characters
  12928. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  12929. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  12930. if ($kk == $maxkk) {
  12931. if ($this->isRTLTextDir()) {
  12932. $tvalue = ltrim($lnstring[1][$kk]);
  12933. } else {
  12934. $tvalue = rtrim($lnstring[1][$kk]);
  12935. }
  12936. } else {
  12937. $tvalue = $lnstring[1][$kk];
  12938. }
  12939. // store number of spaces on the strings
  12940. $lnstring[2][$kk] = substr_count($lnstring[1][$kk], chr(32));
  12941. $lnstring[3][$kk] = substr_count($tvalue, chr(32));
  12942. // count total spaces on line
  12943. $no += $lnstring[2][$kk];
  12944. $ns += $lnstring[3][$kk];
  12945. $lnstring[4][$kk] = $no;
  12946. $lnstring[5][$kk] = $ns;
  12947. }
  12948. if ($this->isRTLTextDir()) {
  12949. $t_x = $this->lMargin - $this->endlinex - (($no - $ns - 1) * $one_space_width);
  12950. }
  12951. // calculate additional space to add to each space
  12952. $spacelen = $one_space_width;
  12953. $spacewidth = ((($tw - $linew) + (($no - $ns) * $spacelen)) / ($ns?$ns:1)) * $this->k;
  12954. $spacewidthu = -1000 * (($tw - $linew) + ($ns * $spacelen)) / ($ns?$ns:1) / $this->FontSize;
  12955. $nsmax = $ns;
  12956. $ns = 0;
  12957. reset($lnstring);
  12958. $offset = 0;
  12959. $strcount = 0;
  12960. $prev_epsposbeg = 0;
  12961. $textpos = 0;
  12962. if ($this->isRTLTextDir()) {
  12963. $textpos = $this->wPt;
  12964. }
  12965. global $spacew;
  12966. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  12967. // check if we are inside a string section '[( ... )]'
  12968. $stroffset = strpos($pmid, '[(', $offset);
  12969. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  12970. // set offset to the end of string section
  12971. $offset = strpos($pmid, ')]', $stroffset);
  12972. while (($offset !== false) AND ($pmid{($offset - 1)} == '\\')) {
  12973. $offset = strpos($pmid, ')]', ($offset + 1));
  12974. }
  12975. if ($offset === false) {
  12976. $this->Error('HTML Justification: malformed PDF code.');
  12977. }
  12978. continue;
  12979. }
  12980. if ($this->isRTLTextDir()) {
  12981. $spacew = ($spacewidth * ($nsmax - $ns));
  12982. } else {
  12983. $spacew = ($spacewidth * $ns);
  12984. }
  12985. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  12986. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  12987. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
  12988. if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
  12989. OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
  12990. // shift EPS images
  12991. $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
  12992. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  12993. $pmid_b = substr($pmid, 0, $epsposbeg);
  12994. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  12995. $pmid_e = substr($pmid, $epsposend);
  12996. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  12997. $offset = $epsposend;
  12998. continue;
  12999. }
  13000. $prev_epsposbeg = $epsposbeg;
  13001. $currentxpos = 0;
  13002. // shift blocks of code
  13003. switch ($strpiece[2][0]) {
  13004. case 'Td':
  13005. case 'cm':
  13006. case 'm':
  13007. case 'l': {
  13008. // get current X position
  13009. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  13010. $currentxpos = $xmatches[1];
  13011. $textpos = $currentxpos;
  13012. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  13013. if ($strcount == $maxkk) {
  13014. if ($this->isRTLTextDir()) {
  13015. $tvalue = $lnstring[1][$strcount];
  13016. } else {
  13017. $tvalue = rtrim($lnstring[1][$strcount]);
  13018. }
  13019. } else {
  13020. $tvalue = $lnstring[1][$strcount];
  13021. }
  13022. $ns += substr_count($tvalue, chr(32));
  13023. ++$strcount;
  13024. }
  13025. if ($this->isRTLTextDir()) {
  13026. $spacew = ($spacewidth * ($nsmax - $ns));
  13027. }
  13028. // justify block
  13029. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  13030. create_function('$matches', 'global $spacew;
  13031. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  13032. return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
  13033. break;
  13034. }
  13035. case 're': {
  13036. // justify block
  13037. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
  13038. $currentxpos = $xmatches[1];
  13039. $x_diff = 0;
  13040. $w_diff = 0;
  13041. if ($this->isRTLTextDir()) { // RTL
  13042. if ($currentxpos < $textpos) {
  13043. $x_diff = ($spacewidth * ($nsmax - $lnstring[5][$strcount]));
  13044. $w_diff = ($spacewidth * $lnstring[3][$strcount]);
  13045. } else {
  13046. if ($strcount > 0) {
  13047. $x_diff = ($spacewidth * ($nsmax - $lnstring[5][($strcount - 1)]));
  13048. $w_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  13049. }
  13050. }
  13051. } else { // LTR
  13052. if ($currentxpos > $textpos) {
  13053. if ($strcount > 0) {
  13054. $x_diff = ($spacewidth * $lnstring[4][($strcount - 1)]);
  13055. }
  13056. $w_diff = ($spacewidth * $lnstring[3][$strcount]);
  13057. } else {
  13058. if ($strcount > 1) {
  13059. $x_diff = ($spacewidth * $lnstring[4][($strcount - 2)]);
  13060. }
  13061. if ($strcount > 0) {
  13062. $w_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  13063. }
  13064. }
  13065. }
  13066. $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x',
  13067. create_function('$matches', '
  13068. $newx = sprintf("%.2F",(floatval($matches[1]) + '.$x_diff.'));
  13069. $neww = sprintf("%.2F",(floatval($matches[3]) + '.$w_diff.'));
  13070. return "".$newx." ".$matches[2]." ".$neww." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
  13071. break;
  13072. }
  13073. case 'c': {
  13074. // get current X position
  13075. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x', $pmid, $xmatches);
  13076. $currentxpos = $xmatches[1];
  13077. // justify block
  13078. $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$xmatches[4].')[\s]('.$xmatches[5].')[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x',
  13079. create_function('$matches', 'global $spacew;
  13080. $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
  13081. $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
  13082. $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
  13083. return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
  13084. break;
  13085. }
  13086. }
  13087. // shift the annotations and links
  13088. if (isset($this->PageAnnots[$this->page])) {
  13089. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  13090. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  13091. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  13092. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  13093. break;
  13094. }
  13095. }
  13096. }
  13097. } // end of while
  13098. // remove markers
  13099. $pmid = str_replace('x*#!#*x', '', $pmid);
  13100. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  13101. // multibyte characters
  13102. $spacew = $spacewidthu;
  13103. $pmidtemp = $pmid;
  13104. // escape special characters
  13105. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  13106. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  13107. $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
  13108. create_function('$matches', 'global $spacew;
  13109. $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
  13110. $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
  13111. return "[(".str_replace(chr(0).chr(32), ") ".($spacew)." (", $matches[1]).")]";'), $pmidtemp);
  13112. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  13113. $endlinepos = strlen($pstart."\n".$pmid."\n");
  13114. } else {
  13115. // non-unicode (single-byte characters)
  13116. $rs = sprintf('%.3F Tw', $spacewidth);
  13117. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  13118. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  13119. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  13120. }
  13121. }
  13122. } // end of J
  13123. if (($t_x != 0) OR ($yshift < 0)) {
  13124. // shift the line
  13125. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  13126. $this->setPageBuffer($startlinepage, $pstart."\nq\n".$trx."\n".$pmid."\nQ\n".$pend);
  13127. $endlinepos = strlen($pstart."\nq\n".$trx."\n".$pmid."\nQ\n");
  13128. // shift the annotations and links
  13129. if (isset($this->PageAnnots[$this->page])) {
  13130. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  13131. if ($pak >= $pask) {
  13132. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  13133. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  13134. }
  13135. }
  13136. }
  13137. $this->y -= $yshift;
  13138. }
  13139. }
  13140. }
  13141. $this->newline = false;
  13142. $pbrk = $this->checkPageBreak($this->lasth);
  13143. $this->SetFont($fontname, $fontstyle, $fontsize);
  13144. if ($wfill) {
  13145. $this->SetFillColorArray($this->bgcolor);
  13146. }
  13147. $startlinex = $this->x;
  13148. $startliney = $this->y;
  13149. $minstartliney = $this->y;
  13150. $startlinepage = $this->page;
  13151. if (isset($endlinepos) AND (!$pbrk)) {
  13152. $startlinepos = $endlinepos;
  13153. unset($endlinepos);
  13154. } else {
  13155. if (isset($this->footerlen[$this->page])) {
  13156. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  13157. } else {
  13158. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  13159. }
  13160. $startlinepos = $this->footerpos[$this->page];
  13161. }
  13162. $plalign = $lalign;
  13163. if (isset($this->PageAnnots[$this->page])) {
  13164. $pask = count($this->PageAnnots[$this->page]);
  13165. } else {
  13166. $pask = 0;
  13167. }
  13168. }
  13169. if (isset($opentagpos)) {
  13170. unset($opentagpos);
  13171. }
  13172. if ($dom[$key]['tag']) {
  13173. if ($dom[$key]['opening']) {
  13174. // get text indentation (if any)
  13175. if (isset($dom[$key]['text-indent']) AND in_array($dom[$key]['value'], array('blockquote','dd','div','dt','h1','h2','h3','h4','h5','h6','li','ol','p','ul','table','tr','td'))) {
  13176. $this->textindent = $dom[$key]['text-indent'];
  13177. }
  13178. if ($dom[$key]['value'] == 'table') {
  13179. if ($this->rtl) {
  13180. $wtmp = $this->x - $this->lMargin;
  13181. } else {
  13182. $wtmp = $this->w - $this->rMargin - $this->x;
  13183. }
  13184. $wtmp -= (2 * $this->cMargin);
  13185. // calculate cell width
  13186. if (isset($dom[$key]['width'])) {
  13187. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  13188. } else {
  13189. $table_width = $wtmp;
  13190. }
  13191. }
  13192. // table content is handled in a special way
  13193. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  13194. $trid = $dom[$key]['parent'];
  13195. $table_el = $dom[$trid]['parent'];
  13196. if (!isset($dom[$table_el]['cols'])) {
  13197. $dom[$table_el]['cols'] = $trid['cols'];
  13198. }
  13199. $oldmargin = $this->cMargin;
  13200. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  13201. $currentcmargin = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  13202. } else {
  13203. $currentcmargin = 0;
  13204. }
  13205. $this->cMargin = $currentcmargin;
  13206. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'])) {
  13207. $cellspacing = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'], 1, 'px');
  13208. } else {
  13209. $cellspacing = 0;
  13210. }
  13211. if ($this->rtl) {
  13212. $cellspacingx = -$cellspacing;
  13213. } else {
  13214. $cellspacingx = $cellspacing;
  13215. }
  13216. $colspan = $dom[$key]['attribute']['colspan'];
  13217. $wtmp = ($colspan * ($table_width / $dom[$table_el]['cols']));
  13218. if (isset($dom[$key]['width'])) {
  13219. $cellw = $this->getHTMLUnitToUnits($dom[$key]['width'], $table_width, 'px');
  13220. } else {
  13221. $cellw = $wtmp;
  13222. }
  13223. if (isset($dom[$key]['height'])) {
  13224. // minimum cell height
  13225. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  13226. } else {
  13227. $cellh = 0;
  13228. }
  13229. $cellw -= $cellspacing;
  13230. if (isset($dom[$key]['content'])) {
  13231. $cell_content = $dom[$key]['content'];
  13232. } else {
  13233. $cell_content = '&nbsp;';
  13234. }
  13235. $tagtype = $dom[$key]['value'];
  13236. $parentid = $key;
  13237. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  13238. // move $key index forward
  13239. ++$key;
  13240. }
  13241. if (!isset($dom[$trid]['startpage'])) {
  13242. $dom[$trid]['startpage'] = $this->page;
  13243. } else {
  13244. $this->setPage($dom[$trid]['startpage']);
  13245. }
  13246. if (!isset($dom[$trid]['starty'])) {
  13247. $dom[$trid]['starty'] = $this->y;
  13248. } else {
  13249. $this->y = $dom[$trid]['starty'];
  13250. }
  13251. if (!isset($dom[$trid]['startx'])) {
  13252. $dom[$trid]['startx'] = $this->x;
  13253. }
  13254. $this->x += ($cellspacingx / 2);
  13255. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  13256. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  13257. } else {
  13258. $rowspan = 1;
  13259. }
  13260. // skip row-spanned cells started on the previous rows
  13261. if (isset($dom[$table_el]['rowspans'])) {
  13262. $rsk = 0;
  13263. $rskmax = count($dom[$table_el]['rowspans']);
  13264. while ($rsk < $rskmax) {
  13265. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  13266. $rsstartx = $trwsp['startx'];
  13267. $rsendx = $trwsp['endx'];
  13268. // account for margin changes
  13269. if ($trwsp['startpage'] < $this->page) {
  13270. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  13271. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  13272. $rsstartx -= $dl;
  13273. $rsendx -= $dl;
  13274. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  13275. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  13276. $rsstartx += $dl;
  13277. $rsendx += $dl;
  13278. }
  13279. }
  13280. if (($trwsp['rowspan'] > 0)
  13281. AND ($rsstartx > ($this->x - $cellspacing - $currentcmargin - $this->feps))
  13282. AND ($rsstartx < ($this->x + $cellspacing + $currentcmargin + $this->feps))
  13283. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page))) {
  13284. // set the starting X position of the current cell
  13285. $this->x = $rsendx + $cellspacingx;
  13286. if (($trwsp['rowspan'] == 1)
  13287. AND (isset($dom[$trid]['endy']))
  13288. AND (isset($dom[$trid]['endpage']))
  13289. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])) {
  13290. // set ending Y position for row
  13291. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  13292. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  13293. }
  13294. $rsk = 0;
  13295. } else {
  13296. ++$rsk;
  13297. }
  13298. }
  13299. }
  13300. // add rowspan information to table element
  13301. if ($rowspan > 1) {
  13302. if (isset($this->footerlen[$this->page])) {
  13303. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  13304. } else {
  13305. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  13306. }
  13307. $trintmrkpos = $this->footerpos[$this->page];
  13308. $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));
  13309. }
  13310. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  13311. if ($rowspan > 1) {
  13312. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  13313. }
  13314. // push background colors
  13315. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  13316. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  13317. }
  13318. $prevLastH = $this->lasth;
  13319. // ****** write the cell content ******
  13320. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true);
  13321. $this->lasth = $prevLastH;
  13322. $this->cMargin = $oldmargin;
  13323. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  13324. // update the end of row position
  13325. if ($rowspan <= 1) {
  13326. if (isset($dom[$trid]['endy'])) {
  13327. if ($this->page == $dom[$trid]['endpage']) {
  13328. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  13329. } elseif ($this->page > $dom[$trid]['endpage']) {
  13330. $dom[$trid]['endy'] = $this->y;
  13331. }
  13332. } else {
  13333. $dom[$trid]['endy'] = $this->y;
  13334. }
  13335. if (isset($dom[$trid]['endpage'])) {
  13336. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  13337. } else {
  13338. $dom[$trid]['endpage'] = $this->page;
  13339. }
  13340. } else {
  13341. // account for row-spanned cells
  13342. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  13343. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  13344. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  13345. }
  13346. if (isset($dom[$table_el]['rowspans'])) {
  13347. // update endy and endpage on rowspanned cells
  13348. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  13349. if ($trwsp['rowspan'] > 0) {
  13350. if (isset($dom[$trid]['endpage'])) {
  13351. if ($trwsp['endpage'] == $dom[$trid]['endpage']) {
  13352. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  13353. } elseif ($trwsp['endpage'] < $dom[$trid]['endpage']) {
  13354. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  13355. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  13356. } else {
  13357. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  13358. }
  13359. }
  13360. }
  13361. }
  13362. }
  13363. $this->x += ($cellspacingx / 2);
  13364. } else {
  13365. // opening tag (or self-closing tag)
  13366. if (!isset($opentagpos)) {
  13367. if (!$this->InFooter) {
  13368. if (isset($this->footerlen[$this->page])) {
  13369. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  13370. } else {
  13371. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  13372. }
  13373. $opentagpos = $this->footerpos[$this->page];
  13374. }
  13375. }
  13376. $this->openHTMLTagHandler($dom, $key, $cell);
  13377. }
  13378. } else {
  13379. // closing tag
  13380. $this->closeHTMLTagHandler($dom, $key, $cell);
  13381. }
  13382. } elseif (strlen($dom[$key]['value']) > 0) {
  13383. // print list-item
  13384. if (!$this->empty_string($this->lispacer)) {
  13385. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  13386. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  13387. $minstartliney = $this->y;
  13388. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  13389. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  13390. $th