PageRenderTime 132ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 4ms

/include/html2pdf_v4.03/_tcpdf_5.0.002/tcpdf.php

https://bitbucket.org/sleininger/stock_online
PHP | 13982 lines | 8551 code | 572 blank | 4859 comment | 1886 complexity | 742835021c7d2f2e9b509f4bf78b6826 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Begin : 2002-08-03
  5. // Last Update : 2010-05-06
  6. // Author : Nicola Asuni - info@tecnick.com - http://www.tcpdf.org
  7. // Version : 5.0.002
  8. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  9. // ----------------------------------------------------------------------------
  10. // Copyright (C) 2002-2010 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU Lesser General Public License as published by
  14. // the Free Software Foundation, either version 2.1 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. // See LICENSE.TXT file for more information.
  26. // ----------------------------------------------------------------------------
  27. //
  28. // Description : This is a PHP class for generating PDF documents without
  29. // requiring external extensions.
  30. //
  31. // NOTE:
  32. // This class was originally derived in 2002 from the Public
  33. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  34. // but now is almost entirely rewritten.
  35. //
  36. // Main features:
  37. // * no external libraries are required for the basic functions;
  38. // * all ISO page formats, custom page formats, custom margins and units of measure;
  39. // * UTF-8 Unicode and Right-To-Left languages;
  40. // * TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  41. // * methods to publish some XHTML code, Javascript and Forms;
  42. // * images, graphic (geometric figures) and transformation methods;
  43. // * supports JPEG, PNG and SVG 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)
  44. // * 1D and 2D barcodes: 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, QR-Code;
  45. // * Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  46. // * automatic page header and footer management;
  47. // * document encryption and digital signature certifications;
  48. // * transactions to UNDO commands;
  49. // * PDF annotations, including links, text and file attachments;
  50. // * text rendering modes (fill, stroke and clipping);
  51. // * multiple columns mode;
  52. // * bookmarks and table of content;
  53. // * text hyphenation;
  54. // * automatic page break, line break and text alignments including justification;
  55. // * automatic page numbering and page groups;
  56. // * move and delete pages;
  57. // * page compression (requires php-zlib extension);
  58. //
  59. // -----------------------------------------------------------
  60. // THANKS TO:
  61. //
  62. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  63. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  64. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  65. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  66. // dullus for text Justification.
  67. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  68. // Patrick Benny for text stretch suggestion on Cell().
  69. // Johannes Güntert for JavaScript support.
  70. // Denis Van Nuffelen for Dynamic Form.
  71. // Jacek Czekaj for multibyte justification
  72. // Anthony Ferrara for the reintroduction of legacy image methods.
  73. // Sourceforge user 1707880 (hucste) for line-trough mode.
  74. // Larry Stanbery for page groups.
  75. // Martin Hall-May for transparency.
  76. // Aaron C. Spike for Polycurve method.
  77. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  78. // Moritz Wagner and Andreas Wurmser for graphic functions.
  79. // Andrew Whitehead for core fonts support.
  80. // Esteban Joël Marín for OpenType font conversion.
  81. // Teus Hagen for several suggestions and fixes.
  82. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  83. // Kosmas Papachristos for some CSS improvements.
  84. // Marcel Partap for some fixes.
  85. // Won Kyu Park for several suggestions, fixes and patches.
  86. // Dominik Dzienia for QR-code support.
  87. // Laurent Minguet for some suggestions.
  88. // Anyone that has reported a bug or sent a suggestion.
  89. //============================================================+
  90. /**
  91. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  92. * 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>
  93. * <h3>TCPDF main features are:</h3>
  94. * <ul>
  95. * <li>no external libraries are required for the basic functions;</li>
  96. * <li>all ISO page formats, custom page formats, custom margins and units of measure;</li>
  97. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  98. * <li>TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;</li>
  99. * <li>methods to publish some XHTML code, Javascript and Forms;</li>
  100. * <li>images, graphic (geometric figures) and transformation methods;
  101. * <li>supports JPEG, PNG and SVG 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>
  102. * <li>1D and 2D barcodes: 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, QR-Code;</li>
  103. * <li>Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  104. * <li>automatic page header and footer management;</li>
  105. * <li>document encryption and digital signature certifications;</li>
  106. * <li>transactions to UNDO commands;</li>
  107. * <li>PDF annotations, including links, text and file attachments;</li>
  108. * <li>text rendering modes (fill, stroke and clipping);</li>
  109. * <li>multiple columns mode;</li>
  110. * <li>bookmarks and table of content;</li>
  111. * <li>text hyphenation;</li>
  112. * <li>automatic page break, line break and text alignments including justification;</li>
  113. * <li>automatic page numbering and page groups;</li>
  114. * <li>move and delete pages;</li>
  115. * <li>page compression (requires php-zlib extension);</li>
  116. * </ul>
  117. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  118. * @package com.tecnick.tcpdf
  119. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  120. * @author Nicola Asuni
  121. * @copyright 2002-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  122. * @link http://www.tcpdf.org
  123. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  124. * @version 5.0.002
  125. */
  126. /**
  127. * main configuration file
  128. */
  129. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  130. // includes some support files
  131. /**
  132. * unicode data
  133. */
  134. require_once(dirname(__FILE__).'/unicode_data.php');
  135. /**
  136. * html colors table
  137. */
  138. require_once(dirname(__FILE__).'/htmlcolors.php');
  139. if (!class_exists('TCPDF', false)) {
  140. /**
  141. * define default PDF document producer
  142. */
  143. define('PDF_PRODUCER', 'TCPDF 5.0.002 (http://www.tcpdf.org)');
  144. /**
  145. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  146. * 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>
  147. * @name TCPDF
  148. * @package com.tecnick.tcpdf
  149. * @version 5.0.002
  150. * @author Nicola Asuni - info@tecnick.com
  151. * @link http://www.tcpdf.org
  152. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  153. */
  154. class TCPDF {
  155. // protected or Protected properties
  156. /**
  157. * @var current page number
  158. * @access protected
  159. */
  160. protected $page;
  161. /**
  162. * @var current object number
  163. * @access protected
  164. */
  165. protected $n;
  166. /**
  167. * @var array of object offsets
  168. * @access protected
  169. */
  170. protected $offsets;
  171. /**
  172. * @var buffer holding in-memory PDF
  173. * @access protected
  174. */
  175. protected $buffer;
  176. /**
  177. * @var array containing pages
  178. * @access protected
  179. */
  180. protected $pages = array();
  181. /**
  182. * @var current document state
  183. * @access protected
  184. */
  185. protected $state;
  186. /**
  187. * @var compression flag
  188. * @access protected
  189. */
  190. protected $compress;
  191. /**
  192. * @var current page orientation (P = Portrait, L = Landscape)
  193. * @access protected
  194. */
  195. protected $CurOrientation;
  196. /**
  197. * @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>
  198. * @access protected
  199. */
  200. protected $pagedim = array();
  201. /**
  202. * @var scale factor (number of points in user unit)
  203. * @access protected
  204. */
  205. protected $k;
  206. /**
  207. * @var width of page format in points
  208. * @access protected
  209. */
  210. protected $fwPt;
  211. /**
  212. * @var height of page format in points
  213. * @access protected
  214. */
  215. protected $fhPt;
  216. /**
  217. * @var current width of page in points
  218. * @access protected
  219. */
  220. protected $wPt;
  221. /**
  222. * @var current height of page in points
  223. * @access protected
  224. */
  225. protected $hPt;
  226. /**
  227. * @var current width of page in user unit
  228. * @access protected
  229. */
  230. protected $w;
  231. /**
  232. * @var current height of page in user unit
  233. * @access protected
  234. */
  235. protected $h;
  236. /**
  237. * @var left margin
  238. * @access protected
  239. */
  240. protected $lMargin;
  241. /**
  242. * @var top margin
  243. * @access protected
  244. */
  245. protected $tMargin;
  246. /**
  247. * @var right margin
  248. * @access protected
  249. */
  250. protected $rMargin;
  251. /**
  252. * @var page break margin
  253. * @access protected
  254. */
  255. protected $bMargin;
  256. /**
  257. * @var cell internal padding
  258. * @access protected
  259. */
  260. //protected
  261. public $cMargin;
  262. /**
  263. * @var cell internal padding (previous value)
  264. * @access protected
  265. */
  266. protected $oldcMargin;
  267. /**
  268. * @var current horizontal position in user unit for cell positioning
  269. * @access protected
  270. */
  271. protected $x;
  272. /**
  273. * @var current vertical position in user unit for cell positioning
  274. * @access protected
  275. */
  276. protected $y;
  277. /**
  278. * @var height of last cell printed
  279. * @access protected
  280. */
  281. protected $lasth;
  282. /**
  283. * @var line width in user unit
  284. * @access protected
  285. */
  286. protected $LineWidth;
  287. /**
  288. * @var array of standard font names
  289. * @access protected
  290. */
  291. protected $CoreFonts;
  292. /**
  293. * @var array of used fonts
  294. * @access protected
  295. */
  296. protected $fonts = array();
  297. /**
  298. * @var array of font files
  299. * @access protected
  300. */
  301. protected $FontFiles = array();
  302. /**
  303. * @var array of encoding differences
  304. * @access protected
  305. */
  306. protected $diffs = array();
  307. /**
  308. * @var array of used images
  309. * @access protected
  310. */
  311. protected $images = array();
  312. /**
  313. * @var array of Annotations in pages
  314. * @access protected
  315. */
  316. protected $PageAnnots = array();
  317. /**
  318. * @var array of internal links
  319. * @access protected
  320. */
  321. protected $links = array();
  322. /**
  323. * @var current font family
  324. * @access protected
  325. */
  326. protected $FontFamily;
  327. /**
  328. * @var current font style
  329. * @access protected
  330. */
  331. protected $FontStyle;
  332. /**
  333. * @var current font ascent (distance between font top and baseline)
  334. * @access protected
  335. * @since 2.8.000 (2007-03-29)
  336. */
  337. protected $FontAscent;
  338. /**
  339. * @var current font descent (distance between font bottom and baseline)
  340. * @access protected
  341. * @since 2.8.000 (2007-03-29)
  342. */
  343. protected $FontDescent;
  344. /**
  345. * @var underlining flag
  346. * @access protected
  347. */
  348. protected $underline;
  349. /**
  350. * @var overlining flag
  351. * @access protected
  352. */
  353. protected $overline;
  354. /**
  355. * @var current font info
  356. * @access protected
  357. */
  358. protected $CurrentFont;
  359. /**
  360. * @var current font size in points
  361. * @access protected
  362. */
  363. protected $FontSizePt;
  364. /**
  365. * @var current font size in user unit
  366. * @access protected
  367. */
  368. protected $FontSize;
  369. /**
  370. * @var commands for drawing color
  371. * @access protected
  372. */
  373. protected $DrawColor;
  374. /**
  375. * @var commands for filling color
  376. * @access protected
  377. */
  378. protected $FillColor;
  379. /**
  380. * @var commands for text color
  381. * @access protected
  382. */
  383. protected $TextColor;
  384. /**
  385. * @var indicates whether fill and text colors are different
  386. * @access protected
  387. */
  388. protected $ColorFlag;
  389. /**
  390. * @var automatic page breaking
  391. * @access protected
  392. */
  393. protected $AutoPageBreak;
  394. /**
  395. * @var threshold used to trigger page breaks
  396. * @access protected
  397. */
  398. protected $PageBreakTrigger;
  399. /**
  400. * @var flag set when processing footer
  401. * @access protected
  402. */
  403. protected $InFooter = false;
  404. /**
  405. * @var zoom display mode
  406. * @access protected
  407. */
  408. protected $ZoomMode;
  409. /**
  410. * @var layout display mode
  411. * @access protected
  412. */
  413. protected $LayoutMode;
  414. /**
  415. * @var title
  416. * @access protected
  417. */
  418. protected $title = '';
  419. /**
  420. * @var subject
  421. * @access protected
  422. */
  423. protected $subject = '';
  424. /**
  425. * @var author
  426. * @access protected
  427. */
  428. protected $author = '';
  429. /**
  430. * @var keywords
  431. * @access protected
  432. */
  433. protected $keywords = '';
  434. /**
  435. * @var creator
  436. * @access protected
  437. */
  438. protected $creator = '';
  439. /**
  440. * @var alias for total number of pages
  441. * @access protected
  442. */
  443. protected $AliasNbPages = '{nb}';
  444. /**
  445. * @var alias for page number
  446. * @access protected
  447. */
  448. protected $AliasNumPage = '{pnb}';
  449. /**
  450. * @var right-bottom corner X coordinate of inserted image
  451. * @since 2002-07-31
  452. * @author Nicola Asuni
  453. * @access protected
  454. */
  455. protected $img_rb_x;
  456. /**
  457. * @var right-bottom corner Y coordinate of inserted image
  458. * @since 2002-07-31
  459. * @author Nicola Asuni
  460. * @access protected
  461. */
  462. protected $img_rb_y;
  463. /**
  464. * @var adjusting factor to convert pixels to user units.
  465. * @since 2004-06-14
  466. * @author Nicola Asuni
  467. * @access protected
  468. */
  469. protected $imgscale = 1;
  470. /**
  471. * @var boolean set to true when the input text is unicode (require unicode fonts)
  472. * @since 2005-01-02
  473. * @author Nicola Asuni
  474. * @access protected
  475. */
  476. protected $isunicode = false;
  477. /**
  478. * @var PDF version
  479. * @since 1.5.3
  480. * @access protected
  481. */
  482. protected $PDFVersion = '1.7';
  483. /**
  484. * @var Minimum distance between header and top page margin.
  485. * @access protected
  486. */
  487. protected $header_margin;
  488. /**
  489. * @var Minimum distance between footer and bottom page margin.
  490. * @access protected
  491. */
  492. protected $footer_margin;
  493. /**
  494. * @var original left margin value
  495. * @access protected
  496. * @since 1.53.0.TC013
  497. */
  498. protected $original_lMargin;
  499. /**
  500. * @var original right margin value
  501. * @access protected
  502. * @since 1.53.0.TC013
  503. */
  504. protected $original_rMargin;
  505. /**
  506. * @var Header font.
  507. * @access protected
  508. */
  509. protected $header_font;
  510. /**
  511. * @var Footer font.
  512. * @access protected
  513. */
  514. protected $footer_font;
  515. /**
  516. * @var Language templates.
  517. * @access protected
  518. */
  519. protected $l;
  520. /**
  521. * @var Barcode to print on page footer (only if set).
  522. * @access protected
  523. */
  524. protected $barcode = false;
  525. /**
  526. * @var If true prints header
  527. * @access protected
  528. */
  529. protected $print_header = true;
  530. /**
  531. * @var If true prints footer.
  532. * @access protected
  533. */
  534. protected $print_footer = true;
  535. /**
  536. * @var Header image logo.
  537. * @access protected
  538. */
  539. protected $header_logo = '';
  540. /**
  541. * @var Header image logo width in mm.
  542. * @access protected
  543. */
  544. protected $header_logo_width = 30;
  545. /**
  546. * @var String to print as title on document header.
  547. * @access protected
  548. */
  549. protected $header_title = '';
  550. /**
  551. * @var String to print on document header.
  552. * @access protected
  553. */
  554. protected $header_string = '';
  555. /**
  556. * @var Default number of columns for html table.
  557. * @access protected
  558. */
  559. protected $default_table_columns = 4;
  560. // variables for html parser
  561. /**
  562. * @var HTML PARSER: array to store current link and rendering styles.
  563. * @access protected
  564. */
  565. protected $HREF = array();
  566. /**
  567. * @var store a list of available fonts on filesystem.
  568. * @access protected
  569. */
  570. protected $fontlist = array();
  571. /**
  572. * @var current foreground color
  573. * @access protected
  574. */
  575. protected $fgcolor;
  576. /**
  577. * @var HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  578. * @access protected
  579. */
  580. protected $listordered = array();
  581. /**
  582. * @var HTML PARSER: array count list items on nested lists.
  583. * @access protected
  584. */
  585. protected $listcount = array();
  586. /**
  587. * @var HTML PARSER: current list nesting level.
  588. * @access protected
  589. */
  590. protected $listnum = 0;
  591. /**
  592. * @var HTML PARSER: indent amount for lists.
  593. * @access protected
  594. */
  595. protected $listindent = 0;
  596. /**
  597. * @var HTML PARSER: current list indententation level.
  598. * @access protected
  599. */
  600. protected $listindentlevel = 0;
  601. /**
  602. * @var current background color
  603. * @access protected
  604. */
  605. protected $bgcolor;
  606. /**
  607. * @var Store temporary font size in points.
  608. * @access protected
  609. */
  610. protected $tempfontsize = 10;
  611. /**
  612. * @var spacer for LI tags.
  613. * @access protected
  614. */
  615. protected $lispacer = '';
  616. /**
  617. * @var default encoding
  618. * @access protected
  619. * @since 1.53.0.TC010
  620. */
  621. protected $encoding = 'UTF-8';
  622. /**
  623. * @var PHP internal encoding
  624. * @access protected
  625. * @since 1.53.0.TC016
  626. */
  627. protected $internal_encoding;
  628. /**
  629. * @var indicates if the document language is Right-To-Left
  630. * @access protected
  631. * @since 2.0.000
  632. */
  633. protected $rtl = false;
  634. /**
  635. * @var used to force RTL or LTR string inversion
  636. * @access protected
  637. * @since 2.0.000
  638. */
  639. protected $tmprtl = false;
  640. // --- Variables used for document encryption:
  641. /**
  642. * Indicates whether document is protected
  643. * @access protected
  644. * @since 2.0.000 (2008-01-02)
  645. */
  646. protected $encrypted;
  647. /**
  648. * U entry in pdf document
  649. * @access protected
  650. * @since 2.0.000 (2008-01-02)
  651. */
  652. protected $Uvalue;
  653. /**
  654. * O entry in pdf document
  655. * @access protected
  656. * @since 2.0.000 (2008-01-02)
  657. */
  658. protected $Ovalue;
  659. /**
  660. * P entry in pdf document
  661. * @access protected
  662. * @since 2.0.000 (2008-01-02)
  663. */
  664. protected $Pvalue;
  665. /**
  666. * encryption object id
  667. * @access protected
  668. * @since 2.0.000 (2008-01-02)
  669. */
  670. protected $enc_obj_id;
  671. /**
  672. * last RC4 key encrypted (cached for optimisation)
  673. * @access protected
  674. * @since 2.0.000 (2008-01-02)
  675. */
  676. protected $last_rc4_key;
  677. /**
  678. * last RC4 computed key
  679. * @access protected
  680. * @since 2.0.000 (2008-01-02)
  681. */
  682. protected $last_rc4_key_c;
  683. /**
  684. * RC4 padding
  685. * @access protected
  686. */
  687. 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";
  688. /**
  689. * RC4 encryption key
  690. * @access protected
  691. */
  692. protected $encryption_key;
  693. // --- bookmark ---
  694. /**
  695. * Outlines for bookmark
  696. * @access protected
  697. * @since 2.1.002 (2008-02-12)
  698. */
  699. protected $outlines = array();
  700. /**
  701. * Outline root for bookmark
  702. * @access protected
  703. * @since 2.1.002 (2008-02-12)
  704. */
  705. protected $OutlineRoot;
  706. // --- javascript and form ---
  707. /**
  708. * javascript code
  709. * @access protected
  710. * @since 2.1.002 (2008-02-12)
  711. */
  712. protected $javascript = '';
  713. /**
  714. * javascript counter
  715. * @access protected
  716. * @since 2.1.002 (2008-02-12)
  717. */
  718. protected $n_js;
  719. /**
  720. * line trough state
  721. * @access protected
  722. * @since 2.8.000 (2008-03-19)
  723. */
  724. protected $linethrough;
  725. // --- Variables used for User's Rights ---
  726. // See PDF reference chapter 8.7 Digital Signatures
  727. /**
  728. * If true enables user's rights on PDF reader
  729. * @access protected
  730. * @since 2.9.000 (2008-03-26)
  731. */
  732. protected $ur;
  733. /**
  734. * Names specifying additional document-wide usage rights for the document.
  735. * @access protected
  736. * @since 2.9.000 (2008-03-26)
  737. */
  738. protected $ur_document;
  739. /**
  740. * Names specifying additional annotation-related usage rights for the document.
  741. * @access protected
  742. * @since 2.9.000 (2008-03-26)
  743. */
  744. protected $ur_annots;
  745. /**
  746. * Names specifying additional form-field-related usage rights for the document.
  747. * @access protected
  748. * @since 2.9.000 (2008-03-26)
  749. */
  750. protected $ur_form;
  751. /**
  752. * Names specifying additional signature-related usage rights for the document.
  753. * @access protected
  754. * @since 2.9.000 (2008-03-26)
  755. */
  756. protected $ur_signature;
  757. /**
  758. * Dot Per Inch Document Resolution (do not change)
  759. * @access protected
  760. * @since 3.0.000 (2008-03-27)
  761. */
  762. protected $dpi = 72;
  763. /**
  764. * Array of page numbers were a new page group was started
  765. * @access protected
  766. * @since 3.0.000 (2008-03-27)
  767. */
  768. protected $newpagegroup = array();
  769. /**
  770. * Contains the number of pages of the groups
  771. * @access protected
  772. * @since 3.0.000 (2008-03-27)
  773. */
  774. protected $pagegroups;
  775. /**
  776. * Contains the alias of the current page group
  777. * @access protected
  778. * @since 3.0.000 (2008-03-27)
  779. */
  780. protected $currpagegroup;
  781. /**
  782. * Restrict the rendering of some elements to screen or printout.
  783. * @access protected
  784. * @since 3.0.000 (2008-03-27)
  785. */
  786. protected $visibility = 'all';
  787. /**
  788. * Print visibility.
  789. * @access protected
  790. * @since 3.0.000 (2008-03-27)
  791. */
  792. protected $n_ocg_print;
  793. /**
  794. * View visibility.
  795. * @access protected
  796. * @since 3.0.000 (2008-03-27)
  797. */
  798. protected $n_ocg_view;
  799. /**
  800. * Array of transparency objects and parameters.
  801. * @access protected
  802. * @since 3.0.000 (2008-03-27)
  803. */
  804. protected $extgstates;
  805. /**
  806. * Set the default JPEG compression quality (1-100)
  807. * @access protected
  808. * @since 3.0.000 (2008-03-27)
  809. */
  810. protected $jpeg_quality;
  811. /**
  812. * Default cell height ratio.
  813. * @access protected
  814. * @since 3.0.014 (2008-05-23)
  815. */
  816. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  817. /**
  818. * PDF viewer preferences.
  819. * @access protected
  820. * @since 3.1.000 (2008-06-09)
  821. */
  822. protected $viewer_preferences;
  823. /**
  824. * A name object specifying how the document should be displayed when opened.
  825. * @access protected
  826. * @since 3.1.000 (2008-06-09)
  827. */
  828. protected $PageMode;
  829. /**
  830. * Array for storing gradient information.
  831. * @access protected
  832. * @since 3.1.000 (2008-06-09)
  833. */
  834. protected $gradients = array();
  835. /**
  836. * Array used to store positions inside the pages buffer.
  837. * keys are the page numbers
  838. * @access protected
  839. * @since 3.2.000 (2008-06-26)
  840. */
  841. protected $intmrk = array();
  842. /**
  843. * Array used to store content positions inside the pages buffer.
  844. * keys are the page numbers
  845. * @access protected
  846. * @since 4.6.021 (2009-07-20)
  847. */
  848. protected $cntmrk = array();
  849. /**
  850. * Array used to store footer positions of each page.
  851. * @access protected
  852. * @since 3.2.000 (2008-07-01)
  853. */
  854. protected $footerpos = array();
  855. /**
  856. * Array used to store footer length of each page.
  857. * @access protected
  858. * @since 4.0.014 (2008-07-29)
  859. */
  860. protected $footerlen = array();
  861. /**
  862. * True if a newline is created.
  863. * @access protected
  864. * @since 3.2.000 (2008-07-01)
  865. */
  866. protected $newline = true;
  867. /**
  868. * End position of the latest inserted line
  869. * @access protected
  870. * @since 3.2.000 (2008-07-01)
  871. */
  872. protected $endlinex = 0;
  873. /**
  874. * PDF string for last line width
  875. * @access protected
  876. * @since 4.0.006 (2008-07-16)
  877. */
  878. protected $linestyleWidth = '';
  879. /**
  880. * PDF string for last line width
  881. * @access protected
  882. * @since 4.0.006 (2008-07-16)
  883. */
  884. protected $linestyleCap = '0 J';
  885. /**
  886. * PDF string for last line width
  887. * @access protected
  888. * @since 4.0.006 (2008-07-16)
  889. */
  890. protected $linestyleJoin = '0 j';
  891. /**
  892. * PDF string for last line width
  893. * @access protected
  894. * @since 4.0.006 (2008-07-16)
  895. */
  896. protected $linestyleDash = '[] 0 d';
  897. /**
  898. * True if marked-content sequence is open
  899. * @access protected
  900. * @since 4.0.013 (2008-07-28)
  901. */
  902. protected $openMarkedContent = false;
  903. /**
  904. * Count the latest inserted vertical spaces on HTML
  905. * @access protected
  906. * @since 4.0.021 (2008-08-24)
  907. */
  908. protected $htmlvspace = 0;
  909. /**
  910. * Array of Spot colors
  911. * @access protected
  912. * @since 4.0.024 (2008-09-12)
  913. */
  914. protected $spot_colors = array();
  915. /**
  916. * Symbol used for HTML unordered list items
  917. * @access protected
  918. * @since 4.0.028 (2008-09-26)
  919. */
  920. protected $lisymbol = '';
  921. /**
  922. * String used to mark the beginning and end of EPS image blocks
  923. * @access protected
  924. * @since 4.1.000 (2008-10-18)
  925. */
  926. protected $epsmarker = 'x#!#EPS#!#x';
  927. /**
  928. * Array of transformation matrix
  929. * @access protected
  930. * @since 4.2.000 (2008-10-29)
  931. */
  932. protected $transfmatrix = array();
  933. /**
  934. * Current key for transformation matrix
  935. * @access protected
  936. * @since 4.8.005 (2009-09-17)
  937. */
  938. protected $transfmatrix_key = 0;
  939. /**
  940. * Booklet mode for double-sided pages
  941. * @access protected
  942. * @since 4.2.000 (2008-10-29)
  943. */
  944. protected $booklet = false;
  945. /**
  946. * Epsilon value used for float calculations
  947. * @access protected
  948. * @since 4.2.000 (2008-10-29)
  949. */
  950. protected $feps = 0.005;
  951. /**
  952. * Array used for custom vertical spaces for HTML tags
  953. * @access protected
  954. * @since 4.2.001 (2008-10-30)
  955. */
  956. protected $tagvspaces = array();
  957. /**
  958. * @var HTML PARSER: custom indent amount for lists.
  959. * Negative value means disabled.
  960. * @access protected
  961. * @since 4.2.007 (2008-11-12)
  962. */
  963. protected $customlistindent = -1;
  964. /**
  965. * @var if true keeps the border open for the cell sides that cross the page.
  966. * @access protected
  967. * @since 4.2.010 (2008-11-14)
  968. */
  969. protected $opencell = true;
  970. /**
  971. * @var array of files to embedd
  972. * @access protected
  973. * @since 4.4.000 (2008-12-07)
  974. */
  975. protected $embeddedfiles = array();
  976. /**
  977. * @var boolean true when inside html pre tag
  978. * @access protected
  979. * @since 4.4.001 (2008-12-08)
  980. */
  981. protected $premode = false;
  982. /**
  983. * Array used to store positions of graphics transformation blocks inside the page buffer.
  984. * keys are the page numbers
  985. * @access protected
  986. * @since 4.4.002 (2008-12-09)
  987. */
  988. protected $transfmrk = array();
  989. /**
  990. * Default color for html links
  991. * @access protected
  992. * @since 4.4.003 (2008-12-09)
  993. */
  994. protected $htmlLinkColorArray = array(0, 0, 255);
  995. /**
  996. * Default font style to add to html links
  997. * @access protected
  998. * @since 4.4.003 (2008-12-09)
  999. */
  1000. protected $htmlLinkFontStyle = 'U';
  1001. /**
  1002. * Counts the number of pages.
  1003. * @access protected
  1004. * @since 4.5.000 (2008-12-31)
  1005. */
  1006. protected $numpages = 0;
  1007. /**
  1008. * Array containing page lengths in bytes.
  1009. * @access protected
  1010. * @since 4.5.000 (2008-12-31)
  1011. */
  1012. protected $pagelen = array();
  1013. /**
  1014. * Counts the number of pages.
  1015. * @access protected
  1016. * @since 4.5.000 (2008-12-31)
  1017. */
  1018. protected $numimages = 0;
  1019. /**
  1020. * Store the image keys.
  1021. * @access protected
  1022. * @since 4.5.000 (2008-12-31)
  1023. */
  1024. protected $imagekeys = array();
  1025. /**
  1026. * Length of the buffer in bytes.
  1027. * @access protected
  1028. * @since 4.5.000 (2008-12-31)
  1029. */
  1030. protected $bufferlen = 0;
  1031. /**
  1032. * If true enables disk caching.
  1033. * @access protected
  1034. * @since 4.5.000 (2008-12-31)
  1035. */
  1036. protected $diskcache = false;
  1037. /**
  1038. * Counts the number of fonts.
  1039. * @access protected
  1040. * @since 4.5.000 (2009-01-02)
  1041. */
  1042. protected $numfonts = 0;
  1043. /**
  1044. * Store the font keys.
  1045. * @access protected
  1046. * @since 4.5.000 (2009-01-02)
  1047. */
  1048. protected $fontkeys = array();
  1049. /**
  1050. * Store the font object IDs.
  1051. * @access protected
  1052. * @since 4.8.001 (2009-09-09)
  1053. */
  1054. protected $font_obj_ids = array();
  1055. /**
  1056. * Store the fage status (true when opened, false when closed).
  1057. * @access protected
  1058. * @since 4.5.000 (2009-01-02)
  1059. */
  1060. protected $pageopen = array();
  1061. /**
  1062. * Default monospaced font
  1063. * @access protected
  1064. * @since 4.5.025 (2009-03-10)
  1065. */
  1066. protected $default_monospaced_font = 'courier';
  1067. /**
  1068. * Used to store a cloned copy of the current class object
  1069. * @access protected
  1070. * @since 4.5.029 (2009-03-19)
  1071. */
  1072. protected $objcopy;
  1073. /**
  1074. * Array used to store the lengths of cache files
  1075. * @access protected
  1076. * @since 4.5.029 (2009-03-19)
  1077. */
  1078. protected $cache_file_length = array();
  1079. /**
  1080. * Table header content to be repeated on each new page
  1081. * @access protected
  1082. * @since 4.5.030 (2009-03-20)
  1083. */
  1084. protected $thead = '';
  1085. /**
  1086. * Margins used for table header.
  1087. * @access protected
  1088. * @since 4.5.030 (2009-03-20)
  1089. */
  1090. protected $theadMargins = array();
  1091. /**
  1092. * Cache array for UTF8StringToArray() method.
  1093. * @access protected
  1094. * @since 4.5.037 (2009-04-07)
  1095. */
  1096. protected $cache_UTF8StringToArray = array();
  1097. /**
  1098. * Maximum size of cache array used for UTF8StringToArray() method.
  1099. * @access protected
  1100. * @since 4.5.037 (2009-04-07)
  1101. */
  1102. protected $cache_maxsize_UTF8StringToArray = 8;
  1103. /**
  1104. * Current size of cache array used for UTF8StringToArray() method.
  1105. * @access protected
  1106. * @since 4.5.037 (2009-04-07)
  1107. */
  1108. protected $cache_size_UTF8StringToArray = 0;
  1109. /**
  1110. * If true enables document signing
  1111. * @access protected
  1112. * @since 4.6.005 (2009-04-24)
  1113. */
  1114. protected $sign = false;
  1115. /**
  1116. * Signature data
  1117. * @access protected
  1118. * @since 4.6.005 (2009-04-24)
  1119. */
  1120. protected $signature_data = array();
  1121. /**
  1122. * Signature max length
  1123. * @access protected
  1124. * @since 4.6.005 (2009-04-24)
  1125. */
  1126. protected $signature_max_length = 11742;
  1127. /**
  1128. * Regular expression used to find blank characters used for word-wrapping.
  1129. * @access protected
  1130. * @since 4.6.006 (2009-04-28)
  1131. */
  1132. protected $re_spaces = '/[\s]/';
  1133. /**
  1134. * Signature object ID
  1135. * @access protected
  1136. * @since 4.6.022 (2009-06-23)
  1137. */
  1138. protected $sig_obj_id = 0;
  1139. /**
  1140. * ByteRange placemark used during signature process.
  1141. * @access protected
  1142. * @since 4.6.028 (2009-08-25)
  1143. */
  1144. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1145. /**
  1146. * Placemark used during signature process.
  1147. * @access protected
  1148. * @since 4.6.028 (2009-08-25)
  1149. */
  1150. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1151. /**
  1152. * ID of page objects
  1153. * @access protected
  1154. * @since 4.7.000 (2009-08-29)
  1155. */
  1156. protected $page_obj_id = array();
  1157. /**
  1158. * Start ID for embedded file objects
  1159. * @access protected
  1160. * @since 4.7.000 (2009-08-29)
  1161. */
  1162. protected $embedded_start_obj_id = 100000;
  1163. /**
  1164. * Start ID for annotation objects
  1165. * @access protected
  1166. * @since 4.7.000 (2009-08-29)
  1167. */
  1168. protected $annots_start_obj_id = 200000;
  1169. /**
  1170. * Max ID of annotation object
  1171. * @access protected
  1172. * @since 4.7.000 (2009-08-29)
  1173. */
  1174. protected $annot_obj_id = 200000;
  1175. /**
  1176. * Current ID of annotation object
  1177. * @access protected
  1178. * @since 4.8.003 (2009-09-15)
  1179. */
  1180. protected $curr_annot_obj_id = 200000;
  1181. /**
  1182. * List of form annotations IDs
  1183. * @access protected
  1184. * @since 4.8.000 (2009-09-07)
  1185. */
  1186. protected $form_obj_id = array();
  1187. /**
  1188. * 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.
  1189. * @access protected
  1190. * @since 4.8.000 (2009-09-07)
  1191. */
  1192. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1193. /**
  1194. * Javascript objects array
  1195. * @access protected
  1196. * @since 4.8.000 (2009-09-07)
  1197. */
  1198. protected $js_objects = array();
  1199. /**
  1200. * Start ID for javascript objects
  1201. * @access protected
  1202. * @since 4.8.000 (2009-09-07)
  1203. */
  1204. protected $js_start_obj_id = 300000;
  1205. /**
  1206. * Current ID of javascript object
  1207. * @access protected
  1208. * @since 4.8.000 (2009-09-07)
  1209. */
  1210. protected $js_obj_id = 300000;
  1211. /**
  1212. * Current form action (used during XHTML rendering)
  1213. * @access protected
  1214. * @since 4.8.000 (2009-09-07)
  1215. */
  1216. protected $form_action = '';
  1217. /**
  1218. * Current form encryption type (used during XHTML rendering)
  1219. * @access protected
  1220. * @since 4.8.000 (2009-09-07)
  1221. */
  1222. protected $form_enctype = 'application/x-www-form-urlencoded';
  1223. /**
  1224. * Current method to submit forms.
  1225. * @access protected
  1226. * @since 4.8.000 (2009-09-07)
  1227. */
  1228. protected $form_mode = 'post';
  1229. /**
  1230. * Start ID for appearance streams XObjects
  1231. * @access protected
  1232. * @since 4.8.001 (2009-09-09)
  1233. */
  1234. protected $apxo_start_obj_id = 400000;
  1235. /**
  1236. * Current ID of appearance streams XObjects
  1237. * @access protected
  1238. * @since 4.8.001 (2009-09-09)
  1239. */
  1240. protected $apxo_obj_id = 400000;
  1241. /**
  1242. * List of fonts used on form fields (fontname => fontkey).
  1243. * @access protected
  1244. * @since 4.8.001 (2009-09-09)
  1245. */
  1246. protected $annotation_fonts = array();
  1247. /**
  1248. * List of radio buttons parent objects.
  1249. * @access protected
  1250. * @since 4.8.001 (2009-09-09)
  1251. */
  1252. protected $radiobutton_groups = array();
  1253. /**
  1254. * List of radio group objects IDs
  1255. * @access protected
  1256. * @since 4.8.001 (2009-09-09)
  1257. */
  1258. protected $radio_groups = array();
  1259. /**
  1260. * Text indentation value (used for text-indent CSS attribute)
  1261. * @access protected
  1262. * @since 4.8.006 (2009-09-23)
  1263. */
  1264. protected $textindent = 0;
  1265. /**
  1266. * Store page number when startTransaction() is called.
  1267. * @access protected
  1268. * @since 4.8.006 (2009-09-23)
  1269. */
  1270. protected $start_transaction_page = 0;
  1271. /**
  1272. * Store Y position when startTransaction() is called.
  1273. * @access protected
  1274. * @since 4.9.001 (2010-03-28)
  1275. */
  1276. protected $start_transaction_y = 0;
  1277. /**
  1278. * True when we are printing the thead section on a new page
  1279. * @access protected
  1280. * @since 4.8.027 (2010-01-25)
  1281. */
  1282. protected $inthead = false;
  1283. /**
  1284. * Array of column measures (width, space, starting Y position)
  1285. * @access protected
  1286. * @since 4.9.001 (2010-03-28)
  1287. */
  1288. protected $columns = array();
  1289. /**
  1290. * Number of colums
  1291. * @access protected
  1292. * @since 4.9.001 (2010-03-28)
  1293. */
  1294. protected $num_columns = 0;
  1295. /**
  1296. * Current column number
  1297. * @access protected
  1298. * @since 4.9.001 (2010-03-28)
  1299. */
  1300. protected $current_column = 0;
  1301. /**
  1302. * Starting page for columns
  1303. * @access protected
  1304. * @since 4.9.001 (2010-03-28)
  1305. */
  1306. protected $column_start_page = 0;
  1307. /**
  1308. * Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
  1309. * @access protected
  1310. * @since 4.9.008 (2010-04-03)
  1311. */
  1312. protected $textrendermode = 0;
  1313. /**
  1314. * Text stroke width in doc units
  1315. * @access protected
  1316. * @since 4.9.008 (2010-04-03)
  1317. */
  1318. protected $textstrokewidth = 0;
  1319. /**
  1320. * @var current stroke color
  1321. * @access protected
  1322. * @since 4.9.008 (2010-04-03)
  1323. */
  1324. protected $strokecolor;
  1325. /**
  1326. * @var default unit of measure for document
  1327. * @access protected
  1328. * @since 5.0.000 (2010-04-22)
  1329. */
  1330. protected $pdfunit = 'mm';
  1331. /**
  1332. * @var true when we are on TOC (Table Of Content) page
  1333. * @access protected
  1334. */
  1335. protected $tocpage = false;
  1336. /**
  1337. * @var If true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1338. * @access protected
  1339. * @since 5.0.000 (2010-04-26)
  1340. */
  1341. protected $rasterize_vector_images = false;
  1342. /**
  1343. * @var directory used for the last SVG image
  1344. * @access protected
  1345. * @since 5.0.000 (2010-05-05)
  1346. */
  1347. protected $svgdir = '';
  1348. /**
  1349. * @var Deafult unit of measure for SVG
  1350. * @access protected
  1351. * @since 5.0.000 (2010-05-02)
  1352. */
  1353. protected $svgunit = 'px';
  1354. /**
  1355. * @var array of SVG gradients
  1356. * @access protected
  1357. * @since 5.0.000 (2010-05-02)
  1358. */
  1359. protected $svggradients = array();
  1360. /**
  1361. * @var ID of last SVG gradient
  1362. * @access protected
  1363. * @since 5.0.000 (2010-05-02)
  1364. */
  1365. protected $svggradientid = 0;
  1366. /**
  1367. * @var true when in SVG defs group
  1368. * @access protected
  1369. * @since 5.0.000 (2010-05-02)
  1370. */
  1371. protected $svgdefsmode = false;
  1372. /**
  1373. * @var array of SVG defs
  1374. * @access protected
  1375. * @since 5.0.000 (2010-05-02)
  1376. */
  1377. protected $svgdefs = array();
  1378. /**
  1379. * @var true when in SVG clipPath tag
  1380. * @access protected
  1381. * @since 5.0.000 (2010-04-26)
  1382. */
  1383. protected $svgclipmode = false;
  1384. /**
  1385. * @var array of SVG clipPath commands
  1386. * @access protected
  1387. * @since 5.0.000 (2010-05-02)
  1388. */
  1389. protected $svgclippaths = array();
  1390. /**
  1391. * @var ID of last SVG clipPath
  1392. * @access protected
  1393. * @since 5.0.000 (2010-05-02)
  1394. */
  1395. protected $svgclipid = 0;
  1396. /**
  1397. * @var svg text
  1398. * @access protected
  1399. * @since 5.0.000 (2010-05-02)
  1400. */
  1401. protected $svgtext = '';
  1402. /**
  1403. * @var array of hinheritable SVG properties
  1404. * @access protected
  1405. * @since 5.0.000 (2010-05-02)
  1406. */
  1407. protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
  1408. /**
  1409. * @var array of SVG properties
  1410. * @access protected
  1411. * @since 5.0.000 (2010-05-02)
  1412. */
  1413. protected $svgstyles = array(array(
  1414. 'alignment-baseline' => 'auto',
  1415. 'baseline-shift' => 'baseline',
  1416. 'clip' => 'auto',
  1417. 'clip-path' => 'none',
  1418. 'clip-rule' => 'nonzero',
  1419. 'color' => 'black',
  1420. 'color-interpolation' => 'sRGB',
  1421. 'color-interpolation-filters' => 'linearRGB',
  1422. 'color-profile' => 'auto',
  1423. 'color-rendering' => 'auto',
  1424. 'cursor' => 'auto',
  1425. 'direction' => 'ltr',
  1426. 'display' => 'inline',
  1427. 'dominant-baseline' => 'auto',
  1428. 'enable-background' => 'accumulate',
  1429. 'fill' => 'black',
  1430. 'fill-opacity' => 1,
  1431. 'fill-rule' => 'nonzero',
  1432. 'filter' => 'none',
  1433. 'flood-color' => 'black',
  1434. 'flood-opacity' => 1,
  1435. 'font' => '',
  1436. 'font-family' => 'helvetica',
  1437. 'font-size' => 'medium',
  1438. 'font-size-adjust' => 'none',
  1439. 'font-stretch' => 'normal',
  1440. 'font-style' => 'normal',
  1441. 'font-variant' => 'normal',
  1442. 'font-weight' => 'normal',
  1443. 'glyph-orientation-horizontal' => '0deg',
  1444. 'glyph-orientation-vertical' => 'auto',
  1445. 'image-rendering' => 'auto',
  1446. 'kerning' => 'auto',
  1447. 'letter-spacing' => 'normal',
  1448. 'lighting-color' => 'white',
  1449. 'marker' => '',
  1450. 'marker-end' => 'none',
  1451. 'marker-mid' => 'none',
  1452. 'marker-start' => 'none',
  1453. 'mask' => 'none',
  1454. 'opacity' => 1,
  1455. 'overflow' => 'auto',
  1456. 'pointer-events' => 'visiblePainted',
  1457. 'shape-rendering' => 'auto',
  1458. 'stop-color' => 'black',
  1459. 'stop-opacity' => 1,
  1460. 'stroke' => 'none',
  1461. 'stroke-dasharray' => 'none',
  1462. 'stroke-dashoffset' => 0,
  1463. 'stroke-linecap' => 'butt',
  1464. 'stroke-linejoin' => 'miter',
  1465. 'stroke-miterlimit' => 4,
  1466. 'stroke-opacity' => 1,
  1467. 'stroke-width' => 1,
  1468. 'text-anchor' => 'start',
  1469. 'text-decoration' => 'none',
  1470. 'text-rendering' => 'auto',
  1471. 'unicode-bidi' => 'normal',
  1472. 'visibility' => 'visible',
  1473. 'word-spacing' => 'normal',
  1474. 'writing-mode' => 'lr-tb',
  1475. 'text-color' => 'black',
  1476. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1477. ));
  1478. //------------------------------------------------------------
  1479. // METHODS
  1480. //------------------------------------------------------------
  1481. /**
  1482. * This is the class constructor.
  1483. * It allows to set up the page format, the orientation and
  1484. * the measure unit used in all the methods (except for the font sizes).
  1485. * @since 1.0
  1486. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  1487. * @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.
  1488. * @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>
  1489. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  1490. * @param boolean $diskcache if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1491. * @param String $encoding charset encoding; default is UTF-8
  1492. * @access public
  1493. */
  1494. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false) {
  1495. /* Set internal character encoding to ASCII */
  1496. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1497. $this->internal_encoding = mb_internal_encoding();
  1498. mb_internal_encoding('ASCII');
  1499. }
  1500. // set disk caching
  1501. $this->diskcache = $diskcache ? true : false;
  1502. // set language direction
  1503. $this->rtl = false;
  1504. $this->tmprtl = false;
  1505. //Some checks
  1506. $this->_dochecks();
  1507. //Initialization of properties
  1508. $this->isunicode = $unicode;
  1509. $this->page = 0;
  1510. $this->transfmrk[0] = array();
  1511. $this->pagedim = array();
  1512. $this->n = 2;
  1513. $this->buffer = '';
  1514. $this->pages = array();
  1515. $this->state = 0;
  1516. $this->fonts = array();
  1517. $this->FontFiles = array();
  1518. $this->diffs = array();
  1519. $this->images = array();
  1520. $this->links = array();
  1521. $this->gradients = array();
  1522. $this->InFooter = false;
  1523. $this->lasth = 0;
  1524. $this->FontFamily = 'helvetica';
  1525. $this->FontStyle = '';
  1526. $this->FontSizePt = 12;
  1527. $this->underline = false;
  1528. $this->overline = false;
  1529. $this->linethrough = false;
  1530. $this->DrawColor = '0 G';
  1531. $this->FillColor = '0 g';
  1532. $this->TextColor = '0 g';
  1533. $this->ColorFlag = false;
  1534. // encryption values
  1535. $this->encrypted = false;
  1536. $this->last_rc4_key = '';
  1537. $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";
  1538. //Standard Unicode fonts
  1539. $this->CoreFonts = array(
  1540. 'courier'=>'Courier',
  1541. 'courierB'=>'Courier-Bold',
  1542. 'courierI'=>'Courier-Oblique',
  1543. 'courierBI'=>'Courier-BoldOblique',
  1544. 'helvetica'=>'Helvetica',
  1545. 'helveticaB'=>'Helvetica-Bold',
  1546. 'helveticaI'=>'Helvetica-Oblique',
  1547. 'helveticaBI'=>'Helvetica-BoldOblique',
  1548. 'times'=>'Times-Roman',
  1549. 'timesB'=>'Times-Bold',
  1550. 'timesI'=>'Times-Italic',
  1551. 'timesBI'=>'Times-BoldItalic',
  1552. 'symbol'=>'Symbol',
  1553. 'zapfdingbats'=>'ZapfDingbats'
  1554. );
  1555. //Set scale factor
  1556. $this->setPageUnit($unit);
  1557. // set page format and orientation
  1558. $this->setPageFormat($format, $orientation);
  1559. //Page margins (1 cm)
  1560. $margin = 28.35 / $this->k;
  1561. $this->SetMargins($margin, $margin);
  1562. //Interior cell margin
  1563. $this->cMargin = $margin / 10;
  1564. //Line width (0.2 mm)
  1565. $this->LineWidth = 0.57 / $this->k;
  1566. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1567. $this->linestyleCap = '0 J';
  1568. $this->linestyleJoin = '0 j';
  1569. $this->linestyleDash = '[] 0 d';
  1570. //Automatic page break
  1571. $this->SetAutoPageBreak(true, (2 * $margin));
  1572. //Full width display mode
  1573. $this->SetDisplayMode('fullwidth');
  1574. //Compression
  1575. $this->SetCompression(true);
  1576. //Set default PDF version number
  1577. $this->PDFVersion = '1.7';
  1578. $this->encoding = $encoding;
  1579. $this->HREF = array();
  1580. $this->getFontsList();
  1581. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1582. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1583. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1584. $this->extgstates = array();
  1585. // user's rights
  1586. $this->sign = false;
  1587. $this->ur = false;
  1588. $this->ur_document = '/FullSave';
  1589. $this->ur_annots = '/Create/Delete/Modify/Copy/Import/Export';
  1590. $this->ur_form = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1591. $this->ur_signature = '/Modify';
  1592. // set default JPEG quality
  1593. $this->jpeg_quality = 75;
  1594. // initialize some settings
  1595. $this->utf8Bidi(array(''), '');
  1596. // set default font
  1597. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1598. // check if PCRE Unicode support is enabled
  1599. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1600. // PCRE unicode support is turned ON
  1601. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1602. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1603. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1604. //$this->re_spaces = '/[\s\p{Z}\p{Lo}]/u';
  1605. $this->re_spaces = '/[\s\p{Z}]/u';
  1606. } else {
  1607. // PCRE unicode support is turned OFF
  1608. $this->re_spaces = '/[\s]/';
  1609. }
  1610. $this->annot_obj_id = $this->annots_start_obj_id;
  1611. $this->curr_annot_obj_id = $this->annots_start_obj_id;
  1612. $this->apxo_obj_id = $this->apxo_start_obj_id;
  1613. $this->js_obj_id = $this->js_start_obj_id;
  1614. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1615. }
  1616. /**
  1617. * Default destructor.
  1618. * @access public
  1619. * @since 1.53.0.TC016
  1620. */
  1621. public function __destruct() {
  1622. // restore internal encoding
  1623. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1624. mb_internal_encoding($this->internal_encoding);
  1625. }
  1626. // unset all class variables
  1627. $this->_destroy(true);
  1628. }
  1629. /**
  1630. * Set the units of measure for the document.
  1631. * @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.
  1632. * @access public
  1633. * @since 3.0.015 (2008-06-06)
  1634. */
  1635. public function setPageUnit($unit) {
  1636. $unit = strtolower($unit);
  1637. //Set scale factor
  1638. switch ($unit) {
  1639. // points
  1640. case 'px':
  1641. case 'pt': {
  1642. $this->k = 1;
  1643. break;
  1644. }
  1645. // millimeters
  1646. case 'mm': {
  1647. $this->k = $this->dpi / 25.4;
  1648. break;
  1649. }
  1650. // centimeters
  1651. case 'cm': {
  1652. $this->k = $this->dpi / 2.54;
  1653. break;
  1654. }
  1655. // inches
  1656. case 'in': {
  1657. $this->k = $this->dpi;
  1658. break;
  1659. }
  1660. // unsupported unit
  1661. default : {
  1662. $this->Error('Incorrect unit: '.$unit);
  1663. break;
  1664. }
  1665. }
  1666. $this->pdfunit = $unit;
  1667. if (isset($this->CurOrientation)) {
  1668. $this->setPageOrientation($this->CurOrientation);
  1669. }
  1670. }
  1671. /**
  1672. * Set the page format
  1673. * @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>
  1674. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1675. * @access public
  1676. * @since 3.0.015 (2008-06-06)
  1677. */
  1678. public function setPageFormat($format, $orientation='P') {
  1679. //Page format
  1680. if (is_string($format)) {
  1681. // Page formats (45 standard ISO paper formats and 4 american common formats).
  1682. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  1683. switch (strtoupper($format)) {
  1684. case '4A0': {$format = array(4767.87,6740.79); break;}
  1685. case '2A0': {$format = array(3370.39,4767.87); break;}
  1686. case 'A0': {$format = array(2383.94,3370.39); break;}
  1687. case 'A1': {$format = array(1683.78,2383.94); break;}
  1688. case 'A2': {$format = array(1190.55,1683.78); break;}
  1689. case 'A3': {$format = array(841.89,1190.55); break;}
  1690. case 'A4': default: {$format = array(595.28,841.89); break;}
  1691. case 'A5': {$format = array(419.53,595.28); break;}
  1692. case 'A6': {$format = array(297.64,419.53); break;}
  1693. case 'A7': {$format = array(209.76,297.64); break;}
  1694. case 'A8': {$format = array(147.40,209.76); break;}
  1695. case 'A9': {$format = array(104.88,147.40); break;}
  1696. case 'A10': {$format = array(73.70,104.88); break;}
  1697. case 'B0': {$format = array(2834.65,4008.19); break;}
  1698. case 'B1': {$format = array(2004.09,2834.65); break;}
  1699. case 'B2': {$format = array(1417.32,2004.09); break;}
  1700. case 'B3': {$format = array(1000.63,1417.32); break;}
  1701. case 'B4': {$format = array(708.66,1000.63); break;}
  1702. case 'B5': {$format = array(498.90,708.66); break;}
  1703. case 'B6': {$format = array(354.33,498.90); break;}
  1704. case 'B7': {$format = array(249.45,354.33); break;}
  1705. case 'B8': {$format = array(175.75,249.45); break;}
  1706. case 'B9': {$format = array(124.72,175.75); break;}
  1707. case 'B10': {$format = array(87.87,124.72); break;}
  1708. case 'C0': {$format = array(2599.37,3676.54); break;}
  1709. case 'C1': {$format = array(1836.85,2599.37); break;}
  1710. case 'C2': {$format = array(1298.27,1836.85); break;}
  1711. case 'C3': {$format = array(918.43,1298.27); break;}
  1712. case 'C4': {$format = array(649.13,918.43); break;}
  1713. case 'C5': {$format = array(459.21,649.13); break;}
  1714. case 'C6': {$format = array(323.15,459.21); break;}
  1715. case 'C7': {$format = array(229.61,323.15); break;}
  1716. case 'C8': {$format = array(161.57,229.61); break;}
  1717. case 'C9': {$format = array(113.39,161.57); break;}
  1718. case 'C10': {$format = array(79.37,113.39); break;}
  1719. case 'RA0': {$format = array(2437.80,3458.27); break;}
  1720. case 'RA1': {$format = array(1729.13,2437.80); break;}
  1721. case 'RA2': {$format = array(1218.90,1729.13); break;}
  1722. case 'RA3': {$format = array(864.57,1218.90); break;}
  1723. case 'RA4': {$format = array(609.45,864.57); break;}
  1724. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  1725. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  1726. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  1727. case 'SRA3': {$format = array(907.09,1275.59); break;}
  1728. case 'SRA4': {$format = array(637.80,907.09); break;}
  1729. case 'LETTER': {$format = array(612.00,792.00); break;}
  1730. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  1731. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  1732. case 'FOLIO': {$format = array(612.00,936.00); break;}
  1733. }
  1734. $this->fwPt = $format[0];
  1735. $this->fhPt = $format[1];
  1736. } else {
  1737. $this->fwPt = $format[0] * $this->k;
  1738. $this->fhPt = $format[1] * $this->k;
  1739. }
  1740. $this->setPageOrientation($orientation);
  1741. }
  1742. /**
  1743. * Set page orientation.
  1744. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1745. * @param boolean $autopagebreak Boolean indicating if auto-page-break mode should be on or off.
  1746. * @param float $bottommargin bottom margin of the page.
  1747. * @access public
  1748. * @since 3.0.015 (2008-06-06)
  1749. */
  1750. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1751. if ($this->fwPt > $this->fhPt) {
  1752. // landscape
  1753. $default_orientation = 'L';
  1754. } else {
  1755. // portrait
  1756. $default_orientation = 'P';
  1757. }
  1758. $valid_orientations = array('P', 'L');
  1759. if (empty($orientation)) {
  1760. $orientation = $default_orientation;
  1761. } else {
  1762. $orientation = $orientation{0};
  1763. $orientation = strtoupper($orientation);
  1764. }
  1765. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  1766. $this->CurOrientation = $orientation;
  1767. $this->wPt = $this->fhPt;
  1768. $this->hPt = $this->fwPt;
  1769. } else {
  1770. $this->CurOrientation = $default_orientation;
  1771. $this->wPt = $this->fwPt;
  1772. $this->hPt = $this->fhPt;
  1773. }
  1774. $this->w = $this->wPt / $this->k;
  1775. $this->h = $this->hPt / $this->k;
  1776. if ($this->empty_string($autopagebreak)) {
  1777. if (isset($this->AutoPageBreak)) {
  1778. $autopagebreak = $this->AutoPageBreak;
  1779. } else {
  1780. $autopagebreak = true;
  1781. }
  1782. }
  1783. if ($this->empty_string($bottommargin)) {
  1784. if (isset($this->bMargin)) {
  1785. $bottommargin = $this->bMargin;
  1786. } else {
  1787. // default value = 2 cm
  1788. $bottommargin = 2 * 28.35 / $this->k;
  1789. }
  1790. }
  1791. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  1792. // store page dimensions
  1793. $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);
  1794. }
  1795. /**
  1796. * Set regular expression to detect withespaces or word separators.
  1797. * @param string $re regular expression (leave empty for default).
  1798. * @access public
  1799. * @since 4.6.016 (2009-06-15)
  1800. */
  1801. public function setSpacesRE($re='/[\s]/') {
  1802. // if PCRE unicode support is turned ON:
  1803. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1804. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1805. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1806. $this->re_spaces = $re;
  1807. }
  1808. /**
  1809. * Enable or disable Right-To-Left language mode
  1810. * @param Boolean $enable if true enable Right-To-Left language mode.
  1811. * @param Boolean $resetx if true reset the X position on direction change.
  1812. * @access public
  1813. * @since 2.0.000 (2008-01-03)
  1814. */
  1815. public function setRTL($enable, $resetx=true) {
  1816. $enable = $enable ? true : false;
  1817. $resetx = ($resetx AND ($enable != $this->rtl));
  1818. $this->rtl = $enable;
  1819. $this->tmprtl = false;
  1820. if ($resetx) {
  1821. $this->Ln(0);
  1822. }
  1823. }
  1824. /**
  1825. * Return the RTL status
  1826. * @return boolean
  1827. * @access public
  1828. * @since 4.0.012 (2008-07-24)
  1829. */
  1830. public function getRTL() {
  1831. return $this->rtl;
  1832. }
  1833. /**
  1834. * Force temporary RTL language direction
  1835. * @param mixed $mode can be false, 'L' for LTR or 'R' for RTL
  1836. * @access public
  1837. * @since 2.1.000 (2008-01-09)
  1838. */
  1839. public function setTempRTL($mode) {
  1840. $newmode = false;
  1841. switch ($mode) {
  1842. case 'ltr':
  1843. case 'LTR':
  1844. case 'L': {
  1845. if ($this->rtl) {
  1846. $newmode = 'L';
  1847. }
  1848. break;
  1849. }
  1850. case 'rtl':
  1851. case 'RTL':
  1852. case 'R': {
  1853. if (!$this->rtl) {
  1854. $newmode = 'R';
  1855. }
  1856. break;
  1857. }
  1858. case false:
  1859. default: {
  1860. $newmode = false;
  1861. break;
  1862. }
  1863. }
  1864. $this->tmprtl = $newmode;
  1865. }
  1866. /**
  1867. * Return the current temporary RTL status
  1868. * @return boolean
  1869. * @access public
  1870. * @since 4.8.014 (2009-11-04)
  1871. */
  1872. public function isRTLTextDir() {
  1873. return ($this->rtl OR ($this->tmprtl == 'R'));
  1874. }
  1875. /**
  1876. * Set the last cell height.
  1877. * @param float $h cell height.
  1878. * @author Nicola Asuni
  1879. * @access public
  1880. * @since 1.53.0.TC034
  1881. */
  1882. public function setLastH($h) {
  1883. $this->lasth = $h;
  1884. }
  1885. /**
  1886. * Get the last cell height.
  1887. * @return last cell height
  1888. * @access public
  1889. * @since 4.0.017 (2008-08-05)
  1890. */
  1891. public function getLastH() {
  1892. return $this->lasth;
  1893. }
  1894. /**
  1895. * Set the adjusting factor to convert pixels to user units.
  1896. * @param float $scale adjusting factor to convert pixels to user units.
  1897. * @author Nicola Asuni
  1898. * @access public
  1899. * @since 1.5.2
  1900. */
  1901. public function setImageScale($scale) {
  1902. $this->imgscale = $scale;
  1903. }
  1904. /**
  1905. * Returns the adjusting factor to convert pixels to user units.
  1906. * @return float adjusting factor to convert pixels to user units.
  1907. * @author Nicola Asuni
  1908. * @access public
  1909. * @since 1.5.2
  1910. */
  1911. public function getImageScale() {
  1912. return $this->imgscale;
  1913. }
  1914. /**
  1915. * Returns an array of page dimensions:
  1916. * <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>
  1917. * @param int $pagenum page number (empty = current page)
  1918. * @return array of page dimensions.
  1919. * @author Nicola Asuni
  1920. * @access public
  1921. * @since 4.5.027 (2009-03-16)
  1922. */
  1923. public function getPageDimensions($pagenum='') {
  1924. if (empty($pagenum)) {
  1925. $pagenum = $this->page;
  1926. }
  1927. return $this->pagedim[$pagenum];
  1928. }
  1929. /**
  1930. * Returns the page width in units.
  1931. * @param int $pagenum page number (empty = current page)
  1932. * @return int page width.
  1933. * @author Nicola Asuni
  1934. * @access public
  1935. * @since 1.5.2
  1936. * @see getPageDimensions()
  1937. */
  1938. public function getPageWidth($pagenum='') {
  1939. if (empty($pagenum)) {
  1940. return $this->w;
  1941. }
  1942. return $this->pagedim[$pagenum]['w'];
  1943. }
  1944. /**
  1945. * Returns the page height in units.
  1946. * @param int $pagenum page number (empty = current page)
  1947. * @return int page height.
  1948. * @author Nicola Asuni
  1949. * @access public
  1950. * @since 1.5.2
  1951. * @see getPageDimensions()
  1952. */
  1953. public function getPageHeight($pagenum='') {
  1954. if (empty($pagenum)) {
  1955. return $this->h;
  1956. }
  1957. return $this->pagedim[$pagenum]['h'];
  1958. }
  1959. /**
  1960. * Returns the page break margin.
  1961. * @param int $pagenum page number (empty = current page)
  1962. * @return int page break margin.
  1963. * @author Nicola Asuni
  1964. * @access public
  1965. * @since 1.5.2
  1966. * @see getPageDimensions()
  1967. */
  1968. public function getBreakMargin($pagenum='') {
  1969. if (empty($pagenum)) {
  1970. return $this->bMargin;
  1971. }
  1972. return $this->pagedim[$pagenum]['bm'];
  1973. }
  1974. /**
  1975. * Returns the scale factor (number of points in user unit).
  1976. * @return int scale factor.
  1977. * @author Nicola Asuni
  1978. * @access public
  1979. * @since 1.5.2
  1980. */
  1981. public function getScaleFactor() {
  1982. return $this->k;
  1983. }
  1984. /**
  1985. * Defines the left, top and right margins.
  1986. * @param float $left Left margin.
  1987. * @param float $top Top margin.
  1988. * @param float $right Right margin. Default value is the left one.
  1989. * @param boolean $keepmargins if true overwrites the default page margins
  1990. * @access public
  1991. * @since 1.0
  1992. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  1993. */
  1994. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  1995. //Set left, top and right margins
  1996. $this->lMargin = $left;
  1997. $this->tMargin = $top;
  1998. if ($right == -1) {
  1999. $right = $left;
  2000. }
  2001. $this->rMargin = $right;
  2002. if ($keepmargins) {
  2003. // overwrite original values
  2004. $this->original_lMargin = $this->lMargin;
  2005. $this->original_rMargin = $this->rMargin;
  2006. }
  2007. }
  2008. /**
  2009. * 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.
  2010. * @param float $margin The margin.
  2011. * @access public
  2012. * @since 1.4
  2013. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2014. */
  2015. public function SetLeftMargin($margin) {
  2016. //Set left margin
  2017. $this->lMargin=$margin;
  2018. if (($this->page > 0) AND ($this->x < $margin)) {
  2019. $this->x = $margin;
  2020. }
  2021. }
  2022. /**
  2023. * Defines the top margin. The method can be called before creating the first page.
  2024. * @param float $margin The margin.
  2025. * @access public
  2026. * @since 1.5
  2027. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2028. */
  2029. public function SetTopMargin($margin) {
  2030. //Set top margin
  2031. $this->tMargin=$margin;
  2032. if (($this->page > 0) AND ($this->y < $margin)) {
  2033. $this->y = $margin;
  2034. }
  2035. }
  2036. /**
  2037. * Defines the right margin. The method can be called before creating the first page.
  2038. * @param float $margin The margin.
  2039. * @access public
  2040. * @since 1.5
  2041. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2042. */
  2043. public function SetRightMargin($margin) {
  2044. $this->rMargin=$margin;
  2045. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  2046. $this->x = $this->w - $margin;
  2047. }
  2048. }
  2049. /**
  2050. * Set the internal Cell padding.
  2051. * @param float $pad internal padding.
  2052. * @access public
  2053. * @since 2.1.000 (2008-01-09)
  2054. * @see Cell(), SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2055. */
  2056. public function SetCellPadding($pad) {
  2057. $this->cMargin = $pad;
  2058. }
  2059. /**
  2060. * 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.
  2061. * @param boolean $auto Boolean indicating if mode should be on or off.
  2062. * @param float $margin Distance from the bottom of the page.
  2063. * @access public
  2064. * @since 1.0
  2065. * @see Cell(), MultiCell(), AcceptPageBreak()
  2066. */
  2067. public function SetAutoPageBreak($auto, $margin=0) {
  2068. //Set auto page break mode and triggering margin
  2069. $this->AutoPageBreak = $auto;
  2070. $this->bMargin = $margin;
  2071. $this->PageBreakTrigger = $this->h - $margin;
  2072. }
  2073. /**
  2074. * Defines the way the document is to be displayed by the viewer.
  2075. * @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>
  2076. * @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>
  2077. * @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>
  2078. * @access public
  2079. * @since 1.2
  2080. */
  2081. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  2082. //Set display mode in viewer
  2083. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  2084. $this->ZoomMode = $zoom;
  2085. } else {
  2086. $this->Error('Incorrect zoom display mode: '.$zoom);
  2087. }
  2088. switch ($layout) {
  2089. case 'default':
  2090. case 'single':
  2091. case 'SinglePage': {
  2092. $this->LayoutMode = 'SinglePage';
  2093. break;
  2094. }
  2095. case 'continuous':
  2096. case 'OneColumn': {
  2097. $this->LayoutMode = 'OneColumn';
  2098. break;
  2099. }
  2100. case 'two':
  2101. case 'TwoColumnLeft': {
  2102. $this->LayoutMode = 'TwoColumnLeft';
  2103. break;
  2104. }
  2105. case 'TwoColumnRight': {
  2106. $this->LayoutMode = 'TwoColumnRight';
  2107. break;
  2108. }
  2109. case 'TwoPageLeft': {
  2110. $this->LayoutMode = 'TwoPageLeft';
  2111. break;
  2112. }
  2113. case 'TwoPageRight': {
  2114. $this->LayoutMode = 'TwoPageRight';
  2115. break;
  2116. }
  2117. default: {
  2118. $this->LayoutMode = 'SinglePage';
  2119. }
  2120. }
  2121. switch ($mode) {
  2122. case 'UseNone': {
  2123. $this->PageMode = 'UseNone';
  2124. break;
  2125. }
  2126. case 'UseOutlines': {
  2127. $this->PageMode = 'UseOutlines';
  2128. break;
  2129. }
  2130. case 'UseThumbs': {
  2131. $this->PageMode = 'UseThumbs';
  2132. break;
  2133. }
  2134. case 'FullScreen': {
  2135. $this->PageMode = 'FullScreen';
  2136. break;
  2137. }
  2138. case 'UseOC': {
  2139. $this->PageMode = 'UseOC';
  2140. break;
  2141. }
  2142. case '': {
  2143. $this->PageMode = 'UseAttachments';
  2144. break;
  2145. }
  2146. default: {
  2147. $this->PageMode = 'UseNone';
  2148. }
  2149. }
  2150. }
  2151. /**
  2152. * 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.
  2153. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  2154. * @param boolean $compress Boolean indicating if compression must be enabled.
  2155. * @access public
  2156. * @since 1.4
  2157. */
  2158. public function SetCompression($compress) {
  2159. //Set page compression
  2160. if (function_exists('gzcompress')) {
  2161. $this->compress = $compress;
  2162. } else {
  2163. $this->compress = false;
  2164. }
  2165. }
  2166. /**
  2167. * Defines the title of the document.
  2168. * @param string $title The title.
  2169. * @access public
  2170. * @since 1.2
  2171. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  2172. */
  2173. public function SetTitle($title) {
  2174. //Title of document
  2175. $this->title = $title;
  2176. }
  2177. /**
  2178. * Defines the subject of the document.
  2179. * @param string $subject The subject.
  2180. * @access public
  2181. * @since 1.2
  2182. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  2183. */
  2184. public function SetSubject($subject) {
  2185. //Subject of document
  2186. $this->subject = $subject;
  2187. }
  2188. /**
  2189. * Defines the author of the document.
  2190. * @param string $author The name of the author.
  2191. * @access public
  2192. * @since 1.2
  2193. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  2194. */
  2195. public function SetAuthor($author) {
  2196. //Author of document
  2197. $this->author = $author;
  2198. }
  2199. /**
  2200. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  2201. * @param string $keywords The list of keywords.
  2202. * @access public
  2203. * @since 1.2
  2204. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  2205. */
  2206. public function SetKeywords($keywords) {
  2207. //Keywords of document
  2208. $this->keywords = $keywords;
  2209. }
  2210. /**
  2211. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  2212. * @param string $creator The name of the creator.
  2213. * @access public
  2214. * @since 1.2
  2215. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  2216. */
  2217. public function SetCreator($creator) {
  2218. //Creator of document
  2219. $this->creator = $creator;
  2220. }
  2221. /**
  2222. * 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.
  2223. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  2224. * @param string $msg The error message
  2225. * @access public
  2226. * @since 1.0
  2227. */
  2228. public function Error($msg) {
  2229. // unset all class variables
  2230. $this->_destroy(true);
  2231. // exit program and print error
  2232. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2233. }
  2234. /**
  2235. * This method begins the generation of the PDF document.
  2236. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2237. * Note: no page is created by this method
  2238. * @access public
  2239. * @since 1.0
  2240. * @see AddPage(), Close()
  2241. */
  2242. public function Open() {
  2243. //Begin document
  2244. $this->state = 1;
  2245. }
  2246. /**
  2247. * Terminates the PDF document.
  2248. * It is not necessary to call this method explicitly because Output() does it automatically.
  2249. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2250. * @access public
  2251. * @since 1.0
  2252. * @see Open(), Output()
  2253. */
  2254. public function Close() {
  2255. if ($this->state == 3) {
  2256. return;
  2257. }
  2258. if ($this->page == 0) {
  2259. $this->AddPage();
  2260. }
  2261. // close page
  2262. $this->endPage();
  2263. // close document
  2264. $this->_enddoc();
  2265. // unset all class variables (except critical ones)
  2266. $this->_destroy(false);
  2267. }
  2268. /**
  2269. * Move pointer at the specified document page and update page dimensions.
  2270. * @param int $pnum page number (1 ... numpages)
  2271. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2272. * @access public
  2273. * @since 2.1.000 (2008-01-07)
  2274. * @see getPage(), lastpage(), getNumPages()
  2275. */
  2276. public function setPage($pnum, $resetmargins=false) {
  2277. if ($pnum == $this->page) {
  2278. return;
  2279. }
  2280. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  2281. $this->state = 2;
  2282. // save current graphic settings
  2283. //$gvars = $this->getGraphicVars();
  2284. $oldpage = $this->page;
  2285. $this->page = $pnum;
  2286. $this->wPt = $this->pagedim[$this->page]['w'];
  2287. $this->hPt = $this->pagedim[$this->page]['h'];
  2288. $this->w = $this->wPt / $this->k;
  2289. $this->h = $this->hPt / $this->k;
  2290. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2291. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2292. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2293. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2294. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2295. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2296. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2297. // restore graphic settings
  2298. //$this->setGraphicVars($gvars);
  2299. if ($resetmargins) {
  2300. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2301. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2302. $this->SetY($this->tMargin);
  2303. } else {
  2304. // account for booklet mode
  2305. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2306. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2307. $this->lMargin += $deltam;
  2308. $this->rMargin -= $deltam;
  2309. }
  2310. }
  2311. } else {
  2312. $this->Error('Wrong page number on setPage() function.');
  2313. }
  2314. }
  2315. /**
  2316. * Reset pointer to the last document page.
  2317. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2318. * @access public
  2319. * @since 2.0.000 (2008-01-04)
  2320. * @see setPage(), getPage(), getNumPages()
  2321. */
  2322. public function lastPage($resetmargins=false) {
  2323. $this->setPage($this->getNumPages(), $resetmargins);
  2324. }
  2325. /**
  2326. * Get current document page number.
  2327. * @return int page number
  2328. * @access public
  2329. * @since 2.1.000 (2008-01-07)
  2330. * @see setPage(), lastpage(), getNumPages()
  2331. */
  2332. public function getPage() {
  2333. return $this->page;
  2334. }
  2335. /**
  2336. * Get the total number of insered pages.
  2337. * @return int number of pages
  2338. * @access public
  2339. * @since 2.1.000 (2008-01-07)
  2340. * @see setPage(), getPage(), lastpage()
  2341. */
  2342. public function getNumPages() {
  2343. return $this->numpages;
  2344. }
  2345. /**
  2346. * Adds a new TOC (Table Of Content) page to the document.
  2347. * @param string $orientation page orientation.
  2348. * @param boolean $keepmargins if true overwrites the default page margins with the current margins
  2349. * @access public
  2350. * @since 5.0.001 (2010-05-06)
  2351. * @see AddPage(), startPage(), endPage(), endTOCPage()
  2352. */
  2353. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  2354. $this->AddPage($orientation, $format, $keepmargins, true);
  2355. }
  2356. /**
  2357. * Terminate the current TOC (Table Of Content) page
  2358. * @access public
  2359. * @since 5.0.001 (2010-05-06)
  2360. * @see AddPage(), startPage(), endPage(), addTOCPage()
  2361. */
  2362. public function endTOCPage() {
  2363. $this->endPage(true);
  2364. }
  2365. /**
  2366. * 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).
  2367. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2368. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2369. * @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>
  2370. * @param boolean $keepmargins if true overwrites the default page margins with the current margins
  2371. * @param boolean $tocpage if true set the tocpage state to true (the added page will be used to display Table Of Content).
  2372. * @access public
  2373. * @since 1.0
  2374. * @see startPage(), endPage(), addTOCPage(), endTOCPage()
  2375. */
  2376. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  2377. if (!isset($this->original_lMargin) OR $keepmargins) {
  2378. $this->original_lMargin = $this->lMargin;
  2379. }
  2380. if (!isset($this->original_rMargin) OR $keepmargins) {
  2381. $this->original_rMargin = $this->rMargin;
  2382. }
  2383. // terminate previous page
  2384. $this->endPage();
  2385. // start new page
  2386. $this->startPage($orientation, $format, $tocpage);
  2387. }
  2388. /**
  2389. * Terminate the current page
  2390. * @param boolean $tocpage if true set the tocpage state to false (end the page used to display Table Of Content).
  2391. * @access public
  2392. * @since 4.2.010 (2008-11-14)
  2393. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  2394. */
  2395. public function endPage($tocpage=false) {
  2396. // check if page is already closed
  2397. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2398. return;
  2399. }
  2400. $this->InFooter = true;
  2401. // print page footer
  2402. $this->setFooter();
  2403. // close page
  2404. $this->_endpage();
  2405. // mark page as closed
  2406. $this->pageopen[$this->page] = false;
  2407. $this->InFooter = false;
  2408. if ($tocpage) {
  2409. $this->tocpage = false;
  2410. }
  2411. }
  2412. /**
  2413. * Starts a new page to the document. The page must be closed using the endPage() function.
  2414. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2415. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2416. * @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>
  2417. * @param boolean $tocpage if true set the tocpage state to true (the added page will be used to display Table of Content).
  2418. * @access public
  2419. * @since 4.2.010 (2008-11-14)
  2420. * @see AddPage(), endPage(), addTOCPage(), endTOCPage()
  2421. */
  2422. public function startPage($orientation='', $format='', $tocpage=false) {
  2423. if ($tocpage) {
  2424. $this->tocpage = true;
  2425. }
  2426. if ($this->numpages > $this->page) {
  2427. // this page has been already added
  2428. $this->setPage($this->page + 1);
  2429. $this->SetY($this->tMargin);
  2430. return;
  2431. }
  2432. // start a new page
  2433. if ($this->state == 0) {
  2434. $this->Open();
  2435. }
  2436. ++$this->numpages;
  2437. $this->swapMargins($this->booklet);
  2438. // save current graphic settings
  2439. $gvars = $this->getGraphicVars();
  2440. // start new page
  2441. $this->_beginpage($orientation, $format);
  2442. // mark page as open
  2443. $this->pageopen[$this->page] = true;
  2444. // restore graphic settings
  2445. $this->setGraphicVars($gvars);
  2446. // mark this point
  2447. $this->setPageMark();
  2448. // print page header
  2449. $this->setHeader();
  2450. // restore graphic settings
  2451. $this->setGraphicVars($gvars);
  2452. // mark this point
  2453. $this->setPageMark();
  2454. // print table header (if any)
  2455. $this->setTableHeader();
  2456. }
  2457. /**
  2458. * Set start-writing mark on current page stream used to put borders and fills.
  2459. * Borders and fills are always created after content and inserted on the position marked by this method.
  2460. * This function must be called after calling Image() function for a background image.
  2461. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2462. * @access public
  2463. * @since 4.0.016 (2008-07-30)
  2464. */
  2465. public function setPageMark() {
  2466. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2467. $this->setContentMark();
  2468. }
  2469. /**
  2470. * Set start-writing mark on selected page.
  2471. * Borders and fills are always created after content and inserted on the position marked by this method.
  2472. * @param int $page page number (default is the current page)
  2473. * @access protected
  2474. * @since 4.6.021 (2009-07-20)
  2475. */
  2476. protected function setContentMark($page=0) {
  2477. if ($page <= 0) {
  2478. $page = $this->page;
  2479. }
  2480. if (isset($this->footerlen[$page])) {
  2481. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2482. } else {
  2483. $this->cntmrk[$page] = $this->pagelen[$page];
  2484. }
  2485. }
  2486. /**
  2487. * Set header data.
  2488. * @param string $ln header image logo
  2489. * @param string $lw header image logo width in mm
  2490. * @param string $ht string to print as title on document header
  2491. * @param string $hs string to print on document header
  2492. * @access public
  2493. */
  2494. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  2495. $this->header_logo = $ln;
  2496. $this->header_logo_width = $lw;
  2497. $this->header_title = $ht;
  2498. $this->header_string = $hs;
  2499. }
  2500. /**
  2501. * Returns header data:
  2502. * <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>
  2503. * @return array()
  2504. * @access public
  2505. * @since 4.0.012 (2008-07-24)
  2506. */
  2507. public function getHeaderData() {
  2508. $ret = array();
  2509. $ret['logo'] = $this->header_logo;
  2510. $ret['logo_width'] = $this->header_logo_width;
  2511. $ret['title'] = $this->header_title;
  2512. $ret['string'] = $this->header_string;
  2513. return $ret;
  2514. }
  2515. /**
  2516. * Set header margin.
  2517. * (minimum distance between header and top page margin)
  2518. * @param int $hm distance in user units
  2519. * @access public
  2520. */
  2521. public function setHeaderMargin($hm=10) {
  2522. $this->header_margin = $hm;
  2523. }
  2524. /**
  2525. * Returns header margin in user units.
  2526. * @return float
  2527. * @since 4.0.012 (2008-07-24)
  2528. * @access public
  2529. */
  2530. public function getHeaderMargin() {
  2531. return $this->header_margin;
  2532. }
  2533. /**
  2534. * Set footer margin.
  2535. * (minimum distance between footer and bottom page margin)
  2536. * @param int $fm distance in user units
  2537. * @access public
  2538. */
  2539. public function setFooterMargin($fm=10) {
  2540. $this->footer_margin = $fm;
  2541. }
  2542. /**
  2543. * Returns footer margin in user units.
  2544. * @return float
  2545. * @since 4.0.012 (2008-07-24)
  2546. * @access public
  2547. */
  2548. public function getFooterMargin() {
  2549. return $this->footer_margin;
  2550. }
  2551. /**
  2552. * Set a flag to print page header.
  2553. * @param boolean $val set to true to print the page header (default), false otherwise.
  2554. * @access public
  2555. */
  2556. public function setPrintHeader($val=true) {
  2557. $this->print_header = $val;
  2558. }
  2559. /**
  2560. * Set a flag to print page footer.
  2561. * @param boolean $value set to true to print the page footer (default), false otherwise.
  2562. * @access public
  2563. */
  2564. public function setPrintFooter($val=true) {
  2565. $this->print_footer = $val;
  2566. }
  2567. /**
  2568. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  2569. * @return float
  2570. * @access public
  2571. */
  2572. public function getImageRBX() {
  2573. return $this->img_rb_x;
  2574. }
  2575. /**
  2576. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  2577. * @return float
  2578. * @access public
  2579. */
  2580. public function getImageRBY() {
  2581. return $this->img_rb_y;
  2582. }
  2583. /**
  2584. * This method is used to render the page header.
  2585. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2586. * @access public
  2587. */
  2588. public function Header() {
  2589. $ormargins = $this->getOriginalMargins();
  2590. $headerfont = $this->getHeaderFont();
  2591. $headerdata = $this->getHeaderData();
  2592. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  2593. $this->Image(K_PATH_IMAGES.$headerdata['logo'], $this->GetX(), $this->getHeaderMargin(), $headerdata['logo_width']);
  2594. $imgy = $this->getImageRBY();
  2595. } else {
  2596. $imgy = $this->GetY();
  2597. }
  2598. $cell_height = round(($this->getCellHeightRatio() * $headerfont[2]) / $this->getScaleFactor(), 2);
  2599. // set starting margin for text data cell
  2600. if ($this->getRTL()) {
  2601. $header_x = $ormargins['right'] + ($headerdata['logo_width'] * 1.1);
  2602. } else {
  2603. $header_x = $ormargins['left'] + ($headerdata['logo_width'] * 1.1);
  2604. }
  2605. $this->SetTextColor(0, 0, 0);
  2606. // header title
  2607. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  2608. $this->SetX($header_x);
  2609. $this->Cell(0, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  2610. // header string
  2611. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  2612. $this->SetX($header_x);
  2613. $this->MultiCell(0, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false);
  2614. // print an ending header line
  2615. $this->SetLineStyle(array('width' => 0.85 / $this->getScaleFactor(), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2616. $this->SetY((2.835 / $this->getScaleFactor()) + max($imgy, $this->GetY()));
  2617. if ($this->getRTL()) {
  2618. $this->SetX($ormargins['right']);
  2619. } else {
  2620. $this->SetX($ormargins['left']);
  2621. }
  2622. $this->Cell(0, 0, '', 'T', 0, 'C');
  2623. }
  2624. /**
  2625. * This method is used to render the page footer.
  2626. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2627. * @access public
  2628. */
  2629. public function Footer() {
  2630. $cur_y = $this->GetY();
  2631. $ormargins = $this->getOriginalMargins();
  2632. $this->SetTextColor(0, 0, 0);
  2633. //set style for cell border
  2634. $line_width = 0.85 / $this->getScaleFactor();
  2635. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2636. //print document barcode
  2637. $barcode = $this->getBarcode();
  2638. if (!empty($barcode)) {
  2639. $this->Ln($line_width);
  2640. $barcode_width = round(($this->getPageWidth() - $ormargins['left'] - $ormargins['right'])/3);
  2641. $this->write1DBarcode($barcode, 'C128B', $this->GetX(), $cur_y + $line_width, $barcode_width, (($this->getFooterMargin() / 3) - $line_width), 0.3, '', '');
  2642. }
  2643. if (empty($this->pagegroups)) {
  2644. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  2645. } else {
  2646. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  2647. }
  2648. $this->SetY($cur_y);
  2649. //Print page number
  2650. if ($this->getRTL()) {
  2651. $this->SetX($ormargins['right']);
  2652. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  2653. } else {
  2654. $this->SetX($ormargins['left']);
  2655. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'R');
  2656. }
  2657. }
  2658. /**
  2659. * This method is used to render the page header.
  2660. * @access protected
  2661. * @since 4.0.012 (2008-07-24)
  2662. */
  2663. protected function setHeader() {
  2664. if ($this->print_header) {
  2665. $temp_thead = $this->thead;
  2666. $temp_theadMargins = $this->theadMargins;
  2667. $lasth = $this->lasth;
  2668. $this->_out('q');
  2669. $this->rMargin = $this->original_rMargin;
  2670. $this->lMargin = $this->original_lMargin;
  2671. $this->cMargin = 0;
  2672. //set current position
  2673. if ($this->rtl) {
  2674. $this->SetXY($this->original_rMargin, $this->header_margin);
  2675. } else {
  2676. $this->SetXY($this->original_lMargin, $this->header_margin);
  2677. }
  2678. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  2679. $this->Header();
  2680. //restore position
  2681. if ($this->rtl) {
  2682. $this->SetXY($this->original_rMargin, $this->tMargin);
  2683. } else {
  2684. $this->SetXY($this->original_lMargin, $this->tMargin);
  2685. }
  2686. $this->_out('Q');
  2687. $this->lasth = $lasth;
  2688. $this->thead = $temp_thead;
  2689. $this->theadMargins = $temp_theadMargins;
  2690. $this->newline = false;
  2691. }
  2692. }
  2693. /**
  2694. * This method is used to render the page footer.
  2695. * @access protected
  2696. * @since 4.0.012 (2008-07-24)
  2697. */
  2698. protected function setFooter() {
  2699. //Page footer
  2700. // save current graphic settings
  2701. $gvars = $this->getGraphicVars();
  2702. // mark this point
  2703. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  2704. $this->_out("\n");
  2705. if ($this->print_footer) {
  2706. $temp_thead = $this->thead;
  2707. $temp_theadMargins = $this->theadMargins;
  2708. $lasth = $this->lasth;
  2709. $this->_out('q');
  2710. $this->rMargin = $this->original_rMargin;
  2711. $this->lMargin = $this->original_lMargin;
  2712. $this->cMargin = 0;
  2713. //set current position
  2714. $footer_y = $this->h - $this->footer_margin;
  2715. if ($this->rtl) {
  2716. $this->SetXY($this->original_rMargin, $footer_y);
  2717. } else {
  2718. $this->SetXY($this->original_lMargin, $footer_y);
  2719. }
  2720. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  2721. $this->Footer();
  2722. //restore position
  2723. if ($this->rtl) {
  2724. $this->SetXY($this->original_rMargin, $this->tMargin);
  2725. } else {
  2726. $this->SetXY($this->original_lMargin, $this->tMargin);
  2727. }
  2728. $this->_out('Q');
  2729. $this->lasth = $lasth;
  2730. $this->thead = $temp_thead;
  2731. $this->theadMargins = $temp_theadMargins;
  2732. }
  2733. // restore graphic settings
  2734. $this->setGraphicVars($gvars);
  2735. // calculate footer length
  2736. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  2737. }
  2738. /**
  2739. * This method is used to render the table header on new page (if any).
  2740. * @access protected
  2741. * @since 4.5.030 (2009-03-25)
  2742. */
  2743. protected function setTableHeader() {
  2744. if ($this->num_columns > 1) {
  2745. // multi column mode
  2746. return;
  2747. }
  2748. if (isset($this->theadMargins['top'])) {
  2749. // restore the original top-margin
  2750. $this->tMargin = $this->theadMargins['top'];
  2751. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2752. $this->y = $this->tMargin;
  2753. }
  2754. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  2755. // set margins
  2756. $prev_lMargin = $this->lMargin;
  2757. $prev_rMargin = $this->rMargin;
  2758. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2759. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2760. $this->cMargin = $this->theadMargins['cmargin'];
  2761. if ($this->rtl) {
  2762. $this->x = $this->w - $this->rMargin;
  2763. } else {
  2764. $this->x = $this->lMargin;
  2765. }
  2766. // print table header
  2767. $this->writeHTML($this->thead, false, false, false, false, '');
  2768. // set new top margin to skip the table headers
  2769. if (!isset($this->theadMargins['top'])) {
  2770. $this->theadMargins['top'] = $this->tMargin;
  2771. }
  2772. $this->tMargin = $this->y;
  2773. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2774. $this->lasth = 0;
  2775. $this->lMargin = $prev_lMargin;
  2776. $this->rMargin = $prev_rMargin;
  2777. }
  2778. }
  2779. /**
  2780. * Returns the current page number.
  2781. * @return int page number
  2782. * @access public
  2783. * @since 1.0
  2784. * @see AliasNbPages(), getAliasNbPages()
  2785. */
  2786. public function PageNo() {
  2787. return $this->page;
  2788. }
  2789. /**
  2790. * Defines a new spot color.
  2791. * It can be expressed in RGB components or gray scale.
  2792. * The method can be called before the first page is created and the value is retained from page to page.
  2793. * @param int $c Cyan color for CMYK. Value between 0 and 255
  2794. * @param int $m Magenta color for CMYK. Value between 0 and 255
  2795. * @param int $y Yellow color for CMYK. Value between 0 and 255
  2796. * @param int $k Key (Black) color for CMYK. Value between 0 and 255
  2797. * @access public
  2798. * @since 4.0.024 (2008-09-12)
  2799. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2800. */
  2801. public function AddSpotColor($name, $c, $m, $y, $k) {
  2802. if (!isset($this->spot_colors[$name])) {
  2803. $i = 1 + count($this->spot_colors);
  2804. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  2805. }
  2806. }
  2807. /**
  2808. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  2809. * It can be expressed in RGB components or gray scale.
  2810. * The method can be called before the first page is created and the value is retained from page to page.
  2811. * @param array $color array of colors
  2812. * @access public
  2813. * @since 3.1.000 (2008-06-11)
  2814. * @see SetDrawColor()
  2815. */
  2816. public function SetDrawColorArray($color) {
  2817. if (isset($color)) {
  2818. $color = array_values($color);
  2819. $r = isset($color[0]) ? $color[0] : -1;
  2820. $g = isset($color[1]) ? $color[1] : -1;
  2821. $b = isset($color[2]) ? $color[2] : -1;
  2822. $k = isset($color[3]) ? $color[3] : -1;
  2823. if ($r >= 0) {
  2824. $this->SetDrawColor($r, $g, $b, $k);
  2825. }
  2826. }
  2827. }
  2828. /**
  2829. * 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.
  2830. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2831. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2832. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2833. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2834. * @access public
  2835. * @since 1.3
  2836. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  2837. */
  2838. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2839. // set default values
  2840. if (!is_numeric($col1)) {
  2841. $col1 = 0;
  2842. }
  2843. if (!is_numeric($col2)) {
  2844. $col2 = -1;
  2845. }
  2846. if (!is_numeric($col3)) {
  2847. $col3 = -1;
  2848. }
  2849. if (!is_numeric($col4)) {
  2850. $col4 = -1;
  2851. }
  2852. //Set color for all stroking operations
  2853. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2854. // Grey scale
  2855. $this->DrawColor = sprintf('%.3F G', $col1/255);
  2856. $this->strokecolor = array('G' => $col1);
  2857. } elseif ($col4 == -1) {
  2858. // RGB
  2859. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $col1/255, $col2/255, $col3/255);
  2860. $this->strokecolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2861. } else {
  2862. // CMYK
  2863. $this->DrawColor = sprintf('%.3F %.3F %.3F %.3F K', $col1/100, $col2/100, $col3/100, $col4/100);
  2864. $this->strokecolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2865. }
  2866. if ($this->page > 0) {
  2867. $this->_out($this->DrawColor);
  2868. }
  2869. }
  2870. /**
  2871. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  2872. * @param string $name name of the spot color
  2873. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2874. * @access public
  2875. * @since 4.0.024 (2008-09-12)
  2876. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2877. */
  2878. public function SetDrawSpotColor($name, $tint=100) {
  2879. if (!isset($this->spot_colors[$name])) {
  2880. $this->Error('Undefined spot color: '.$name);
  2881. }
  2882. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], $tint/100);
  2883. if ($this->page > 0) {
  2884. $this->_out($this->DrawColor);
  2885. }
  2886. }
  2887. /**
  2888. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  2889. * It can be expressed in RGB components or gray scale.
  2890. * The method can be called before the first page is created and the value is retained from page to page.
  2891. * @param array $color array of colors
  2892. * @access public
  2893. * @since 3.1.000 (2008-6-11)
  2894. * @see SetFillColor()
  2895. */
  2896. public function SetFillColorArray($color) {
  2897. if (isset($color)) {
  2898. $color = array_values($color);
  2899. $r = isset($color[0]) ? $color[0] : -1;
  2900. $g = isset($color[1]) ? $color[1] : -1;
  2901. $b = isset($color[2]) ? $color[2] : -1;
  2902. $k = isset($color[3]) ? $color[3] : -1;
  2903. if ($r >= 0) {
  2904. $this->SetFillColor($r, $g, $b, $k);
  2905. }
  2906. }
  2907. }
  2908. /**
  2909. * 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.
  2910. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2911. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2912. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2913. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2914. * @access public
  2915. * @since 1.3
  2916. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  2917. */
  2918. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2919. // set default values
  2920. if (!is_numeric($col1)) {
  2921. $col1 = 0;
  2922. }
  2923. if (!is_numeric($col2)) {
  2924. $col2 = -1;
  2925. }
  2926. if (!is_numeric($col3)) {
  2927. $col3 = -1;
  2928. }
  2929. if (!is_numeric($col4)) {
  2930. $col4 = -1;
  2931. }
  2932. //Set color for all filling operations
  2933. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2934. // Grey scale
  2935. $this->FillColor = sprintf('%.3F g', $col1/255);
  2936. $this->bgcolor = array('G' => $col1);
  2937. } elseif ($col4 == -1) {
  2938. // RGB
  2939. $this->FillColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2940. $this->bgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2941. } else {
  2942. // CMYK
  2943. $this->FillColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2944. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2945. }
  2946. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2947. if ($this->page > 0) {
  2948. $this->_out($this->FillColor);
  2949. }
  2950. }
  2951. /**
  2952. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  2953. * @param string $name name of the spot color
  2954. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2955. * @access public
  2956. * @since 4.0.024 (2008-09-12)
  2957. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  2958. */
  2959. public function SetFillSpotColor($name, $tint=100) {
  2960. if (!isset($this->spot_colors[$name])) {
  2961. $this->Error('Undefined spot color: '.$name);
  2962. }
  2963. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2964. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2965. if ($this->page > 0) {
  2966. $this->_out($this->FillColor);
  2967. }
  2968. }
  2969. /**
  2970. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  2971. * The method can be called before the first page is created and the value is retained from page to page.
  2972. * @param array $color array of colors
  2973. * @access public
  2974. * @since 3.1.000 (2008-6-11)
  2975. * @see SetFillColor()
  2976. */
  2977. public function SetTextColorArray($color) {
  2978. if (isset($color)) {
  2979. $color = array_values($color);
  2980. $r = isset($color[0]) ? $color[0] : -1;
  2981. $g = isset($color[1]) ? $color[1] : -1;
  2982. $b = isset($color[2]) ? $color[2] : -1;
  2983. $k = isset($color[3]) ? $color[3] : -1;
  2984. if ($r >= 0) {
  2985. $this->SetTextColor($r, $g, $b, $k);
  2986. }
  2987. }
  2988. }
  2989. /**
  2990. * 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.
  2991. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2992. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2993. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2994. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2995. * @access public
  2996. * @since 1.3
  2997. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  2998. */
  2999. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  3000. // set default values
  3001. if (!is_numeric($col1)) {
  3002. $col1 = 0;
  3003. }
  3004. if (!is_numeric($col2)) {
  3005. $col2 = -1;
  3006. }
  3007. if (!is_numeric($col3)) {
  3008. $col3 = -1;
  3009. }
  3010. if (!is_numeric($col4)) {
  3011. $col4 = -1;
  3012. }
  3013. //Set color for text
  3014. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  3015. // Grey scale
  3016. $this->TextColor = sprintf('%.3F g', $col1/255);
  3017. $this->fgcolor = array('G' => $col1);
  3018. } elseif ($col4 == -1) {
  3019. // RGB
  3020. $this->TextColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  3021. $this->fgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  3022. } else {
  3023. // CMYK
  3024. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  3025. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  3026. }
  3027. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3028. }
  3029. /**
  3030. * Defines the spot color used for text.
  3031. * @param string $name name of the spot color
  3032. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3033. * @access public
  3034. * @since 4.0.024 (2008-09-12)
  3035. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  3036. */
  3037. public function SetTextSpotColor($name, $tint=100) {
  3038. if (!isset($this->spot_colors[$name])) {
  3039. $this->Error('Undefined spot color: '.$name);
  3040. }
  3041. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  3042. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3043. if ($this->page > 0) {
  3044. $this->_out($this->TextColor);
  3045. }
  3046. }
  3047. /**
  3048. * Returns the length of a string in user unit. A font must be selected.<br>
  3049. * @param string $s The string whose length is to be computed
  3050. * @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.
  3051. * @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><li>O: overline</li></ul> or any combination. The default value is regular.
  3052. * @param float $fontsize Font size in points. The default value is the current size.
  3053. * @param boolean $getarray if true returns an array of characters widths, if false returns the total length.
  3054. * @return mixed int total string length or array of characted widths
  3055. * @author Nicola Asuni
  3056. * @access public
  3057. * @since 1.2
  3058. */
  3059. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3060. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  3061. }
  3062. /**
  3063. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  3064. * @param string $sa The array of chars whose total length is to be computed
  3065. * @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.
  3066. * @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><li>O: overline</li></ul> or any combination. The default value is regular.
  3067. * @param float $fontsize Font size in points. The default value is the current size.
  3068. * @param boolean $getarray if true returns an array of characters widths, if false returns the total length.
  3069. * @return mixed int total string length or array of characted widths
  3070. * @author Nicola Asuni
  3071. * @access public
  3072. * @since 2.4.000 (2008-03-06)
  3073. */
  3074. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3075. // store current values
  3076. if (!$this->empty_string($fontname)) {
  3077. $prev_FontFamily = $this->FontFamily;
  3078. $prev_FontStyle = $this->FontStyle;
  3079. $prev_FontSizePt = $this->FontSizePt;
  3080. $this->SetFont($fontname, $fontstyle, $fontsize);
  3081. }
  3082. // convert UTF-8 array to Latin1 if required
  3083. $sa = $this->UTF8ArrToLatin1($sa);
  3084. $w = 0; // total width
  3085. $wa = array(); // array of characters widths
  3086. foreach ($sa as $char) {
  3087. // character width
  3088. $cw = $this->GetCharWidth($char);
  3089. $wa[] = $cw;
  3090. $w += $cw;
  3091. }
  3092. // restore previous values
  3093. if (!$this->empty_string($fontname)) {
  3094. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt);
  3095. }
  3096. if ($getarray) {
  3097. return $wa;
  3098. }
  3099. return $w;
  3100. }
  3101. /**
  3102. * Returns the length of the char in user unit for the current font.
  3103. * @param int $char The char code whose length is to be returned
  3104. * @return int char width
  3105. * @author Nicola Asuni
  3106. * @access public
  3107. * @since 2.4.000 (2008-03-06)
  3108. */
  3109. public function GetCharWidth($char) {
  3110. if ($char == 173) {
  3111. // SHY character will not be printed
  3112. return (0);
  3113. }
  3114. $cw = &$this->CurrentFont['cw'];
  3115. if (isset($cw[$char])) {
  3116. $w = $cw[$char];
  3117. } elseif (isset($this->CurrentFont['dw'])) {
  3118. // default width
  3119. $w = $this->CurrentFont['dw'];
  3120. } elseif (isset($cw[32])) {
  3121. // default width
  3122. $w = $cw[32];
  3123. } else {
  3124. $w = 600;
  3125. }
  3126. return ($w * $this->FontSize / 1000);
  3127. }
  3128. /**
  3129. * Returns the numbero of characters in a string.
  3130. * @param string $s The input string.
  3131. * @return int number of characters
  3132. * @access public
  3133. * @since 2.0.0001 (2008-01-07)
  3134. */
  3135. public function GetNumChars($s) {
  3136. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  3137. return count($this->UTF8StringToArray($s));
  3138. }
  3139. return strlen($s);
  3140. }
  3141. /**
  3142. * Fill the list of available fonts ($this->fontlist).
  3143. * @access protected
  3144. * @since 4.0.013 (2008-07-28)
  3145. */
  3146. protected function getFontsList() {
  3147. $fontsdir = opendir($this->_getfontpath());
  3148. while (($file = readdir($fontsdir)) !== false) {
  3149. if (substr($file, -4) == '.php') {
  3150. array_push($this->fontlist, strtolower(basename($file, '.php')));
  3151. }
  3152. }
  3153. closedir($fontsdir);
  3154. }
  3155. /**
  3156. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  3157. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  3158. * 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.
  3159. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  3160. * @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>
  3161. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3162. * @return array containing the font data, or false in case of error.
  3163. * @access public
  3164. * @since 1.5
  3165. * @see SetFont()
  3166. */
  3167. public function AddFont($family, $style='', $fontfile='') {
  3168. if ($this->empty_string($family)) {
  3169. if (!$this->empty_string($this->FontFamily)) {
  3170. $family = $this->FontFamily;
  3171. } else {
  3172. $this->Error('Empty font family');
  3173. }
  3174. }
  3175. $family = strtolower($family);
  3176. if ((!$this->isunicode) AND ($family == 'arial')) {
  3177. $family = 'helvetica';
  3178. }
  3179. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  3180. $style = '';
  3181. }
  3182. $tempstyle = strtoupper($style);
  3183. $style = '';
  3184. // underline
  3185. if (strpos($tempstyle, 'U') !== false) {
  3186. $this->underline = true;
  3187. } else {
  3188. $this->underline = false;
  3189. }
  3190. // line-through (deleted)
  3191. if (strpos($tempstyle, 'D') !== false) {
  3192. $this->linethrough = true;
  3193. } else {
  3194. $this->linethrough = false;
  3195. }
  3196. // overline
  3197. if (strpos($tempstyle, 'O') !== false) {
  3198. $this->overline = true;
  3199. } else {
  3200. $this->overline = false;
  3201. }
  3202. // bold
  3203. if (strpos($tempstyle, 'B') !== false) {
  3204. $style .= 'B';
  3205. }
  3206. // oblique
  3207. if (strpos($tempstyle, 'I') !== false) {
  3208. $style .= 'I';
  3209. }
  3210. $bistyle = $style;
  3211. $fontkey = $family.$style;
  3212. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  3213. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  3214. // check if the font has been already added
  3215. if ($this->getFontBuffer($fontkey) !== false) {
  3216. return $fontdata;
  3217. }
  3218. if (isset($type)) {
  3219. unset($type);
  3220. }
  3221. if (isset($cw)) {
  3222. unset($cw);
  3223. }
  3224. // get specified font directory (if any)
  3225. $fontdir = false;
  3226. if (!$this->empty_string($fontfile)) {
  3227. $fontdir = dirname($fontfile);
  3228. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  3229. $fontdir = '';
  3230. } else {
  3231. $fontdir .= '/';
  3232. }
  3233. }
  3234. // search and include font file
  3235. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  3236. // build a standard filenames for specified font
  3237. $fontfile1 = str_replace(' ', '', $family).strtolower($style).'.php';
  3238. $fontfile2 = str_replace(' ', '', $family).'.php';
  3239. // search files on various directories
  3240. if (($fontdir !== false) AND file_exists($fontdir.$fontfile1)) {
  3241. $fontfile = $fontdir.$fontfile1;
  3242. } elseif (file_exists($this->_getfontpath().$fontfile1)) {
  3243. $fontfile = $this->_getfontpath().$fontfile1;
  3244. } elseif (file_exists($fontfile1)) {
  3245. $fontfile = $fontfile1;
  3246. } elseif (($fontdir !== false) AND file_exists($fontdir.$fontfile2)) {
  3247. $fontfile = $fontdir.$fontfile2;
  3248. } elseif (file_exists($this->_getfontpath().$fontfile2)) {
  3249. $fontfile = $this->_getfontpath().$fontfile2;
  3250. } else {
  3251. $fontfile = $fontfile2;
  3252. }
  3253. }
  3254. // include font file
  3255. if (file_exists($fontfile)) {
  3256. include($fontfile);
  3257. } else {
  3258. $this->Error('Could not include font definition file: '.$family.'');
  3259. }
  3260. // check font parameters
  3261. if ((!isset($type)) OR (!isset($cw))) {
  3262. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  3263. }
  3264. // SET default parameters
  3265. if (!isset($file) OR $this->empty_string($file)) {
  3266. $file = '';
  3267. }
  3268. if (!isset($enc) OR $this->empty_string($enc)) {
  3269. $enc = '';
  3270. }
  3271. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  3272. $cidinfo = array('Registry'=>'Adobe','Ordering'=>'Identity','Supplement'=>0);
  3273. $cidinfo['uni2cid'] = array();
  3274. }
  3275. if (!isset($ctg) OR $this->empty_string($ctg)) {
  3276. $ctg = '';
  3277. }
  3278. if (!isset($desc) OR $this->empty_string($desc)) {
  3279. $desc = array();
  3280. }
  3281. if (!isset($up) OR $this->empty_string($up)) {
  3282. $up = -100;
  3283. }
  3284. if (!isset($ut) OR $this->empty_string($ut)) {
  3285. $ut = 50;
  3286. }
  3287. if (!isset($cw) OR $this->empty_string($cw)) {
  3288. $cw = array();
  3289. }
  3290. if (!isset($dw) OR $this->empty_string($dw)) {
  3291. // set default width
  3292. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  3293. $dw = $desc['MissingWidth'];
  3294. } elseif (isset($cw[32])) {
  3295. $dw = $cw[32];
  3296. } else {
  3297. $dw = 600;
  3298. }
  3299. }
  3300. ++$this->numfonts;
  3301. if ($type == 'cidfont0') {
  3302. // register CID font (all styles at once)
  3303. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  3304. $sname = $name.$styles[$bistyle];
  3305. // artificial bold
  3306. if (strpos($bistyle, 'B') !== false) {
  3307. if (isset($desc['StemV'])) {
  3308. $desc['StemV'] *= 2;
  3309. } else {
  3310. $desc['StemV'] = 120;
  3311. }
  3312. }
  3313. // artificial italic
  3314. if (strpos($bistyle, 'I') !== false) {
  3315. if (isset($desc['ItalicAngle'])) {
  3316. $desc['ItalicAngle'] -= 11;
  3317. } else {
  3318. $desc['ItalicAngle'] = -11;
  3319. }
  3320. }
  3321. } elseif ($type == 'core') {
  3322. $name = $this->CoreFonts[$fontkey];
  3323. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  3324. // ...
  3325. } elseif ($type == 'TrueTypeUnicode') {
  3326. $enc = 'Identity-H';
  3327. } else {
  3328. $this->Error('Unknow font type: '.$type.'');
  3329. }
  3330. $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));
  3331. if (isset($diff) AND (!empty($diff))) {
  3332. //Search existing encodings
  3333. $d = 0;
  3334. $nb = count($this->diffs);
  3335. for ($i=1; $i <= $nb; ++$i) {
  3336. if ($this->diffs[$i] == $diff) {
  3337. $d = $i;
  3338. break;
  3339. }
  3340. }
  3341. if ($d == 0) {
  3342. $d = $nb + 1;
  3343. $this->diffs[$d] = $diff;
  3344. }
  3345. $this->setFontSubBuffer($fontkey, 'diff', $d);
  3346. }
  3347. if (!$this->empty_string($file)) {
  3348. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  3349. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir);
  3350. } elseif ($type != 'core') {
  3351. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir);
  3352. }
  3353. }
  3354. return $fontdata;
  3355. }
  3356. /**
  3357. * Sets the font used to print character strings.
  3358. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  3359. * The method can be called before the first page is created and the font is retained from page to page.
  3360. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  3361. * 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 />
  3362. * @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.
  3363. * @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><li>O: overline</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.
  3364. * @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
  3365. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3366. * @access public
  3367. * @since 1.0
  3368. * @see AddFont(), SetFontSize()
  3369. */
  3370. public function SetFont($family, $style='', $size=0, $fontfile='') {
  3371. //Select a font; size given in points
  3372. if ($size == 0) {
  3373. $size = $this->FontSizePt;
  3374. }
  3375. // try to add font (if not already added)
  3376. $fontdata = $this->AddFont($family, $style, $fontfile);
  3377. $this->FontFamily = $fontdata['family'];
  3378. $this->FontStyle = $fontdata['style'];
  3379. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  3380. $this->SetFontSize($size);
  3381. }
  3382. /**
  3383. * Defines the size of the current font.
  3384. * @param float $size The size (in points)
  3385. * @access public
  3386. * @since 1.0
  3387. * @see SetFont()
  3388. */
  3389. public function SetFontSize($size) {
  3390. //Set font size in points
  3391. $this->FontSizePt = $size;
  3392. $this->FontSize = $size / $this->k;
  3393. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  3394. $this->FontAscent = $this->CurrentFont['desc']['Ascent'] * $this->FontSize / 1000;
  3395. } else {
  3396. $this->FontAscent = 0.85 * $this->FontSize;
  3397. }
  3398. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  3399. $this->FontDescent = - $this->CurrentFont['desc']['Descent'] * $this->FontSize / 1000;
  3400. } else {
  3401. $this->FontDescent = 0.15 * $this->FontSize;
  3402. }
  3403. if (($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  3404. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  3405. }
  3406. }
  3407. /**
  3408. * Return the font descent value
  3409. * @param string $font font name
  3410. * @param string $style font style
  3411. * @param float $size The size (in points)
  3412. * @return int font descent
  3413. * @access public
  3414. * @since 4.9.003 (2010-03-30)
  3415. */
  3416. public function getFontDescent($font, $style='', $size=0) {
  3417. //Set font size in points
  3418. $sizek = $size / $this->k;
  3419. $fontdata = $this->AddFont($font, $style);
  3420. if (isset($fontdata['desc']['Descent']) AND ($fontdata['desc']['Descent'] <= 0)) {
  3421. $descent = - $fontdata['desc']['Descent'] * $sizek / 1000;
  3422. } else {
  3423. $descent = 0.15 * $sizek;
  3424. }
  3425. return $descent;
  3426. }
  3427. /**
  3428. * Return the font ascent value
  3429. * @param string $font font name
  3430. * @param string $style font style
  3431. * @param float $size The size (in points)
  3432. * @return int font ascent
  3433. * @access public
  3434. * @since 4.9.003 (2010-03-30)
  3435. */
  3436. public function getFontAscent($font, $style='', $size=0) {
  3437. //Set font size in points
  3438. $sizek = $size / $this->k;
  3439. $fontdata = $this->AddFont($font, $style);
  3440. if (isset($fontdata['desc']['Ascent']) AND ($fontdata['desc']['Ascent'] > 0)) {
  3441. $ascent = $fontdata['desc']['Ascent'] * $sizek / 1000;
  3442. } else {
  3443. $ascent = 0.85 * $sizek;
  3444. }
  3445. return $ascent;
  3446. }
  3447. /**
  3448. * Defines the default monospaced font.
  3449. * @param string $font Font name.
  3450. * @access public
  3451. * @since 4.5.025
  3452. */
  3453. public function SetDefaultMonospacedFont($font) {
  3454. $this->default_monospaced_font = $font;
  3455. }
  3456. /**
  3457. * 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 />
  3458. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  3459. * @access public
  3460. * @since 1.5
  3461. * @see Cell(), Write(), Image(), Link(), SetLink()
  3462. */
  3463. public function AddLink() {
  3464. //Create a new internal link
  3465. $n = count($this->links) + 1;
  3466. $this->links[$n] = array(0, 0);
  3467. return $n;
  3468. }
  3469. /**
  3470. * Defines the page and position a link points to.
  3471. * @param int $link The link identifier returned by AddLink()
  3472. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  3473. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  3474. * @access public
  3475. * @since 1.5
  3476. * @see AddLink()
  3477. */
  3478. public function SetLink($link, $y=0, $page=-1) {
  3479. if ($y == -1) {
  3480. $y = $this->y;
  3481. }
  3482. if ($page == -1) {
  3483. $page = $this->page;
  3484. }
  3485. $this->links[$link] = array($page, $y);
  3486. }
  3487. /**
  3488. * Puts a link on a rectangular area of the page.
  3489. * 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.
  3490. * @param float $x Abscissa of the upper-left corner of the rectangle
  3491. * @param float $y Ordinate of the upper-left corner of the rectangle
  3492. * @param float $w Width of the rectangle
  3493. * @param float $h Height of the rectangle
  3494. * @param mixed $link URL or identifier returned by AddLink()
  3495. * @param int $spaces number of spaces on the text to link
  3496. * @access public
  3497. * @since 1.5
  3498. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  3499. */
  3500. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  3501. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  3502. }
  3503. /**
  3504. * Puts a markup annotation on a rectangular area of the page.
  3505. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  3506. * @param float $x Abscissa of the upper-left corner of the rectangle
  3507. * @param float $y Ordinate of the upper-left corner of the rectangle
  3508. * @param float $w Width of the rectangle
  3509. * @param float $h Height of the rectangle
  3510. * @param string $text annotation text or alternate content
  3511. * @param array $opt array of options (see section 8.4 of PDF reference 1.7).
  3512. * @param int $spaces number of spaces on the text to link
  3513. * @access public
  3514. * @since 4.0.018 (2008-08-06)
  3515. */
  3516. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  3517. if ($x === '') {
  3518. $x = $this->x;
  3519. }
  3520. if ($y === '') {
  3521. $y = $this->y;
  3522. }
  3523. // recalculate coordinates to account for graphic transformations
  3524. if (isset($this->transfmatrix)) {
  3525. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  3526. $maxid = count($this->transfmatrix[$i]) - 1;
  3527. for ($j=$maxid; $j >= 0; --$j) {
  3528. $ctm = $this->transfmatrix[$i][$j];
  3529. if (isset($ctm['a'])) {
  3530. $x = $x * $this->k;
  3531. $y = ($this->h - $y) * $this->k;
  3532. $w = $w * $this->k;
  3533. $h = $h * $this->k;
  3534. // top left
  3535. $xt = $x;
  3536. $yt = $y;
  3537. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3538. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3539. // top right
  3540. $xt = $x + $w;
  3541. $yt = $y;
  3542. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3543. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3544. // bottom left
  3545. $xt = $x;
  3546. $yt = $y - $h;
  3547. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3548. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3549. // bottom right
  3550. $xt = $x + $w;
  3551. $yt = $y - $h;
  3552. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3553. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3554. // new coordinates (rectangle area)
  3555. $x = min($x1, $x2, $x3, $x4);
  3556. $y = max($y1, $y2, $y3, $y4);
  3557. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  3558. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  3559. $x = $x / $this->k;
  3560. $y = $this->h - ($y / $this->k);
  3561. }
  3562. }
  3563. }
  3564. }
  3565. if ($this->page <= 0) {
  3566. $page = 1;
  3567. } else {
  3568. $page = $this->page;
  3569. }
  3570. if (!isset($this->PageAnnots[$page])) {
  3571. $this->PageAnnots[$page] = array();
  3572. }
  3573. $this->PageAnnots[$page][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  3574. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  3575. $this->embeddedfiles[basename($opt['FS'])] = array('file' => $opt['FS'], 'n' => (count($this->embeddedfiles) + $this->embedded_start_obj_id));
  3576. }
  3577. // Add widgets annotation's icons
  3578. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  3579. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  3580. }
  3581. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  3582. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  3583. }
  3584. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  3585. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  3586. }
  3587. ++$this->annot_obj_id;
  3588. }
  3589. /**
  3590. * Embedd the attached files.
  3591. * @since 4.4.000 (2008-12-07)
  3592. * @access protected
  3593. * @see Annotation()
  3594. */
  3595. protected function _putEmbeddedFiles() {
  3596. reset($this->embeddedfiles);
  3597. foreach ($this->embeddedfiles as $filename => $filedata) {
  3598. $data = file_get_contents($filedata['file']);
  3599. $filter = '';
  3600. if ($this->compress) {
  3601. $data = gzcompress($data);
  3602. $filter = ' /Filter /FlateDecode';
  3603. }
  3604. $this->offsets[$filedata['n']] = $this->bufferlen;
  3605. $out = $filedata['n'].' 0 obj';
  3606. $out .= ' <</Type /EmbeddedFile'.$filter.' /Length '.strlen($data).' >>';
  3607. $out .= ' '.$this->_getstream($data, $filedata['n']);
  3608. $out .= ' endobj';
  3609. $this->_out($out);
  3610. }
  3611. }
  3612. /**
  3613. * Prints a text cell at the specified position.
  3614. * The origin is on the left of the first charcter, on the baseline.
  3615. * This method allows to place a string precisely on the page.
  3616. * @param float $x Abscissa of the cell origin
  3617. * @param float $y Ordinate of the cell origin
  3618. * @param string $txt String to print
  3619. * @param int $fstroke outline size in user units (false = disable)
  3620. * @param boolean $fclip if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  3621. * @param boolean $ffill if true fills the text
  3622. * @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>
  3623. * @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.
  3624. * @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>
  3625. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3626. * @param mixed $link URL or identifier returned by AddLink().
  3627. * @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>
  3628. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3629. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li><li>B : cell bottom</li></ul>
  3630. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  3631. * @param boolean $rtloff if true uses the page top-left corner as origin of axis for $x and $y initial position.
  3632. * @access public
  3633. * @since 1.0
  3634. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  3635. */
  3636. public function Text($x, $y, $txt, $fstroke=false, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
  3637. $textrendermode = $this->textrendermode;
  3638. $textstrokewidth = $this->textstrokewidth;
  3639. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  3640. $this->SetXY($x, $y, $rtloff);
  3641. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  3642. // restore previous rendering mode
  3643. $this->textrendermode = $textrendermode;
  3644. $this->textstrokewidth = $textstrokewidth;
  3645. }
  3646. /**
  3647. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  3648. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  3649. * This method is called automatically and should not be called directly by the application.
  3650. * @return boolean
  3651. * @access public
  3652. * @since 1.4
  3653. * @see SetAutoPageBreak()
  3654. */
  3655. public function AcceptPageBreak() {
  3656. if ($this->num_columns > 1) {
  3657. // multi column mode
  3658. if($this->current_column < ($this->num_columns - 1)) {
  3659. // go to next column
  3660. $this->selectColumn($this->current_column + 1);
  3661. } else {
  3662. // add a new page
  3663. $this->AddPage();
  3664. // set first column
  3665. $this->selectColumn(0);
  3666. }
  3667. // avoid page breaking from checkPageBreak()
  3668. return false;
  3669. }
  3670. return $this->AutoPageBreak;
  3671. }
  3672. /**
  3673. * Add page if needed.
  3674. * @param float $h Cell height. Default value: 0.
  3675. * @param mixed $y starting y position, leave empty for current position.
  3676. * @param boolean $addpage if true add a page, otherwise only return the true/false state
  3677. * @return boolean true in case of page break, false otherwise.
  3678. * @since 3.2.000 (2008-07-01)
  3679. * @access protected
  3680. */
  3681. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  3682. if ($this->empty_string($y)) {
  3683. $y = $this->y;
  3684. }
  3685. if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
  3686. if ($addpage) {
  3687. //Automatic page break
  3688. $x = $this->x;
  3689. $this->AddPage($this->CurOrientation);
  3690. $this->y = $this->tMargin;
  3691. $oldpage = $this->page - 1;
  3692. if ($this->rtl) {
  3693. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  3694. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  3695. } else {
  3696. $this->x = $x;
  3697. }
  3698. } else {
  3699. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3700. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  3701. } else {
  3702. $this->x = $x;
  3703. }
  3704. }
  3705. }
  3706. return true;
  3707. }
  3708. return false;
  3709. }
  3710. /**
  3711. * 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 />
  3712. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3713. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3714. * @param float $h Cell height. Default value: 0.
  3715. * @param string $txt String to print. Default value: empty string.
  3716. * @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>
  3717. * @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>
  3718. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3719. * @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>
  3720. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3721. * @param mixed $link URL or identifier returned by AddLink().
  3722. * @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>
  3723. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3724. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  3725. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  3726. * @access public
  3727. * @since 1.0
  3728. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  3729. */
  3730. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  3731. if (!$ignore_min_height) {
  3732. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3733. if ($h < $min_cell_height) {
  3734. $h = $min_cell_height;
  3735. }
  3736. }
  3737. $this->checkPageBreak($h);
  3738. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign));
  3739. }
  3740. /**
  3741. * Removes SHY characters from text.
  3742. * @param string $txt input string
  3743. * @return string without SHY characters.
  3744. * @access public
  3745. * @since (4.5.019) 2009-02-28
  3746. */
  3747. public function removeSHY($txt='') {
  3748. /*
  3749. * Unicode Data
  3750. * Name : SOFT HYPHEN, commonly abbreviated as SHY
  3751. * HTML Entity (decimal): &#173;
  3752. * HTML Entity (hex): &#xad;
  3753. * HTML Entity (named): &shy;
  3754. * How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]
  3755. * UTF-8 (hex): 0xC2 0xAD (c2ad)
  3756. * UTF-8 character: chr(194).chr(173)
  3757. */
  3758. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  3759. if (!$this->isunicode) {
  3760. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  3761. }
  3762. return $txt;
  3763. }
  3764. /**
  3765. * 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 />
  3766. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3767. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3768. * @param float $h Cell height. Default value: 0.
  3769. * @param string $txt String to print. Default value: empty string.
  3770. * @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>
  3771. * @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.
  3772. * @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>
  3773. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3774. * @param mixed $link URL or identifier returned by AddLink().
  3775. * @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>
  3776. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3777. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  3778. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  3779. * @access protected
  3780. * @since 1.0
  3781. * @see Cell()
  3782. */
  3783. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  3784. $txt = $this->removeSHY($txt);
  3785. $rs = ''; //string to be returned
  3786. if (!$ignore_min_height) {
  3787. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3788. if ($h < $min_cell_height) {
  3789. $h = $min_cell_height;
  3790. }
  3791. }
  3792. $k = $this->k;
  3793. $x = $this->x;
  3794. $y = $this->y;
  3795. // cell vertical alignment
  3796. switch ($calign) {
  3797. case 'A': {
  3798. // font top
  3799. switch ($valign) {
  3800. case 'T': {
  3801. // top
  3802. $y -= ($this->LineWidth / 2);
  3803. break;
  3804. }
  3805. case 'B': {
  3806. // bottom
  3807. $y -= ($h - $this->FontAscent - $this->FontDescent - ($this->LineWidth / 2));
  3808. break;
  3809. }
  3810. default:
  3811. case 'M': {
  3812. // center
  3813. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  3814. break;
  3815. }
  3816. }
  3817. break;
  3818. }
  3819. case 'L': {
  3820. // font baseline
  3821. switch ($valign) {
  3822. case 'T': {
  3823. // top
  3824. $y -= ($this->FontAscent + ($this->LineWidth / 2));
  3825. break;
  3826. }
  3827. case 'B': {
  3828. // bottom
  3829. $y -= ($h - $this->FontDescent - ($this->LineWidth / 2));
  3830. break;
  3831. }
  3832. default:
  3833. case 'M': {
  3834. // center
  3835. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  3836. break;
  3837. }
  3838. }
  3839. break;
  3840. }
  3841. case 'D': {
  3842. // font bottom
  3843. switch ($valign) {
  3844. case 'T': {
  3845. // top
  3846. $y -= ($this->FontAscent + $this->FontDescent + ($this->LineWidth / 2));
  3847. break;
  3848. }
  3849. case 'B': {
  3850. // bottom
  3851. $y -= ($h - ($this->LineWidth / 2));
  3852. break;
  3853. }
  3854. default:
  3855. case 'M': {
  3856. // center
  3857. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  3858. break;
  3859. }
  3860. }
  3861. break;
  3862. }
  3863. case 'B': {
  3864. // cell bottom
  3865. $y -= $h;
  3866. break;
  3867. }
  3868. case 'C': {
  3869. // cell center
  3870. $y -= ($h / 2);
  3871. break;
  3872. }
  3873. default:
  3874. case 'T': {
  3875. // cell top
  3876. break;
  3877. }
  3878. }
  3879. // text vertical alignment
  3880. switch ($valign) {
  3881. case 'T': {
  3882. // top
  3883. $basefonty = $y + $this->FontAscent + ($this->LineWidth / 2);
  3884. break;
  3885. }
  3886. case 'B': {
  3887. // bottom
  3888. $basefonty = $y + $h - $this->FontDescent - ($this->LineWidth / 2);
  3889. break;
  3890. }
  3891. default:
  3892. case 'M': {
  3893. // center
  3894. $basefonty = $y + (($h + $this->FontAscent - $this->FontDescent) / 2);
  3895. break;
  3896. }
  3897. }
  3898. if ($this->empty_string($w) OR ($w <= 0)) {
  3899. if ($this->rtl) {
  3900. $w = $x - $this->lMargin;
  3901. } else {
  3902. $w = $this->w - $this->rMargin - $x;
  3903. }
  3904. }
  3905. $s = '';
  3906. // fill and borders
  3907. if (($fill == 1) OR ($border == 1)) {
  3908. if ($fill == 1) {
  3909. $op = ($border == 1) ? 'B' : 'f';
  3910. } else {
  3911. $op = 'S';
  3912. }
  3913. if ($this->rtl) {
  3914. $xk = (($this->x - $w) * $k);
  3915. } else {
  3916. $xk = ($this->x * $k);
  3917. }
  3918. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  3919. }
  3920. if (is_string($border)) {
  3921. $lm = ($this->LineWidth / 2);
  3922. if (strpos($border,'L') !== false) {
  3923. if ($this->rtl) {
  3924. $xk = ($x - $w) * $k;
  3925. } else {
  3926. $xk = $x * $k;
  3927. }
  3928. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm)) * $k));
  3929. }
  3930. if (strpos($border,'T') !== false) {
  3931. if ($this->rtl) {
  3932. $xk = ($x - $w + $lm) * $k;
  3933. $xwk = ($x - $lm) * $k;
  3934. } else {
  3935. $xk = ($x - $lm) * $k;
  3936. $xwk = ($x + $w + $lm) * $k;
  3937. }
  3938. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y) * $k), $xwk, (($this->h - $y) * $k));
  3939. }
  3940. if (strpos($border,'R') !== false) {
  3941. if ($this->rtl) {
  3942. $xk = $x * $k;
  3943. } else {
  3944. $xk = ($x + $w) * $k;
  3945. }
  3946. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm))* $k));
  3947. }
  3948. if (strpos($border,'B') !== false) {
  3949. if ($this->rtl) {
  3950. $xk = ($x - $w + $lm) * $k;
  3951. $xwk = ($x - $lm) * $k;
  3952. } else {
  3953. $xk = ($x - $lm) * $k;
  3954. $xwk = ($x + $w + $lm) * $k;
  3955. }
  3956. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - ($y + $h)) * $k), $xwk, (($this->h - ($y + $h)) * $k));
  3957. }
  3958. }
  3959. if ($txt != '') {
  3960. $txt2 = $txt;
  3961. if ($this->isunicode) {
  3962. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  3963. $txt2 = $this->UTF8ToLatin1($txt2);
  3964. } else {
  3965. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  3966. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  3967. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  3968. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  3969. // NOTE: this doesn't work with HTML justification
  3970. // Symbols that could overlap on the font top (only works in LTR)
  3971. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  3972. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  3973. $numchars = count($unicode); // number of chars
  3974. $unik = 0;
  3975. $uniblock = array();
  3976. $uniblock[$unik] = array();
  3977. $uniblock[$unik][] = $unicode[0];
  3978. // resolve overlapping conflicts by splitting the string in several parts
  3979. for ($i = 1; $i < $numchars; ++$i) {
  3980. // check if symbols overlaps at top
  3981. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  3982. // move symbols to another array
  3983. ++$unik;
  3984. $uniblock[$unik] = array();
  3985. $uniblock[$unik][] = $unicode[$i];
  3986. ++$unik;
  3987. $uniblock[$unik] = array();
  3988. $unicode[$i] = 8203; // Unicode Character 'ZERO WIDTH SPACE' (U+200B)
  3989. } else {
  3990. $uniblock[$unik][] = $unicode[$i];
  3991. }
  3992. }
  3993. // ---- END OF Fix for bug #2977340
  3994. }
  3995. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  3996. }
  3997. }
  3998. $txt2 = $this->_escape($txt2);
  3999. // text length
  4000. $txwidth = $this->GetStringWidth($txt);
  4001. $width = $txwidth;
  4002. // ratio between cell length and text length
  4003. if ($width <= 0) {
  4004. $ratio = 1;
  4005. } else {
  4006. $ratio = ($w - (2 * $this->cMargin)) / $width;
  4007. }
  4008. // stretch text if required
  4009. if (($stretch > 0) AND (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0)))) {
  4010. if ($stretch > 2) {
  4011. // spacing
  4012. //Calculate character spacing in points
  4013. $char_space = (($w - $width - (2 * $this->cMargin)) * $this->k) / max($this->GetNumChars($txt)-1,1);
  4014. //Set character spacing
  4015. $rs .= sprintf('BT %.2F Tc ET ', $char_space);
  4016. } else {
  4017. // scaling
  4018. //Calculate horizontal scaling
  4019. $horiz_scale = $ratio * 100.0;
  4020. //Set horizontal scaling
  4021. $rs .= sprintf('BT %.2F Tz ET ', $horiz_scale);
  4022. }
  4023. $align = '';
  4024. $width = $w - (2 * $this->cMargin);
  4025. } else {
  4026. $stretch == 0;
  4027. }
  4028. if ($this->ColorFlag) {
  4029. $s .= 'q '.$this->TextColor.' ';
  4030. }
  4031. // rendering mode
  4032. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  4033. // count number of spaces
  4034. $ns = substr_count($txt, ' ');
  4035. // Justification
  4036. $spacewidth = 0;
  4037. if (($align == 'J') AND ($ns > 0)) {
  4038. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  4039. // get string width without spaces
  4040. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  4041. // calculate average space width
  4042. $spacewidth = -1000 * ($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1) / $this->FontSize;
  4043. // set word position to be used with TJ operator
  4044. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  4045. $unicode_justification = true;
  4046. } else {
  4047. // get string width
  4048. $width = $txwidth;
  4049. $spacewidth = (($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1)) * $this->k;
  4050. // set word spacing
  4051. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  4052. }
  4053. $width = $w - (2 * $this->cMargin);
  4054. }
  4055. // replace carriage return characters
  4056. $txt2 = str_replace("\r", ' ', $txt2);
  4057. switch ($align) {
  4058. case 'C': {
  4059. $dx = ($w - $width) / 2;
  4060. break;
  4061. }
  4062. case 'R': {
  4063. if ($this->rtl) {
  4064. $dx = $this->cMargin;
  4065. } else {
  4066. $dx = $w - $width - $this->cMargin;
  4067. }
  4068. break;
  4069. }
  4070. case 'L': {
  4071. if ($this->rtl) {
  4072. $dx = $w - $width - $this->cMargin;
  4073. } else {
  4074. $dx = $this->cMargin;
  4075. }
  4076. break;
  4077. }
  4078. case 'J':
  4079. default: {
  4080. $dx = $this->cMargin;
  4081. break;
  4082. }
  4083. }
  4084. if ($this->rtl) {
  4085. $xdx = $this->x - $dx - $width;
  4086. } else {
  4087. $xdx = $this->x + $dx;
  4088. }
  4089. $xdk = $xdx * $k;
  4090. // print text
  4091. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  4092. if (isset($uniblock)) {
  4093. // print overlapping characters as separate string
  4094. $xshift = 0; // horizontal shift
  4095. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  4096. $spw = (($w - $txwidth - (2 * $this->cMargin)) / ($ns?$ns:1));
  4097. foreach ($uniblock as $uk => $uniarr) {
  4098. if (($uk % 2) == 0) {
  4099. // x space to skip
  4100. if ($spacewidth != 0) {
  4101. // justification shift
  4102. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  4103. }
  4104. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  4105. } else {
  4106. // character to print
  4107. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  4108. $topchr = $this->_escape($topchr);
  4109. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  4110. }
  4111. }
  4112. }
  4113. if ($this->underline) {
  4114. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  4115. }
  4116. if ($this->linethrough) {
  4117. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  4118. }
  4119. if ($this->overline) {
  4120. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  4121. }
  4122. if ($this->ColorFlag) {
  4123. $s .= ' Q';
  4124. }
  4125. if ($link) {
  4126. $this->Link($xdx, $y + (($h - $this->FontSize)/2), $width, $this->FontSize, $link, $ns);
  4127. }
  4128. }
  4129. // output cell
  4130. if ($s) {
  4131. // output cell
  4132. $rs .= $s;
  4133. // reset text stretching
  4134. if ($stretch > 2) {
  4135. //Reset character horizontal spacing
  4136. $rs .= ' BT 0 Tc ET';
  4137. } elseif ($stretch > 0) {
  4138. //Reset character horizontal scaling
  4139. $rs .= ' BT 100 Tz ET';
  4140. }
  4141. }
  4142. // reset word spacing
  4143. if (!(($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($align == 'J')) {
  4144. $rs .= ' BT 0 Tw ET';
  4145. }
  4146. $this->lasth = $h;
  4147. if ($ln > 0) {
  4148. //Go to the beginning of the next line
  4149. $this->y = $y + $h;
  4150. if ($ln == 1) {
  4151. if ($this->rtl) {
  4152. $this->x = $this->w - $this->rMargin;
  4153. } else {
  4154. $this->x = $this->lMargin;
  4155. }
  4156. }
  4157. } else {
  4158. // go left or right by case
  4159. if ($this->rtl) {
  4160. $this->x -= $w;
  4161. } else {
  4162. $this->x += $w;
  4163. }
  4164. }
  4165. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  4166. $rs = $gstyles.$rs;
  4167. return $rs;
  4168. }
  4169. /**
  4170. * This method allows printing text with line breaks.
  4171. * 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 />
  4172. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  4173. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  4174. * @param float $h Cell minimum height. The cell extends automatically if needed.
  4175. * @param string $txt String to print
  4176. * @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>
  4177. * @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>
  4178. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  4179. * @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>
  4180. * @param float $x x position in user units
  4181. * @param float $y y position in user units
  4182. * @param boolean $reseth if true reset the last cell height (default true).
  4183. * @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>
  4184. * @param boolean $ishtml set to true if $txt is HTML content (default = false).
  4185. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  4186. * @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.
  4187. * @return int Return the number of cells or 1 for html mode.
  4188. * @access public
  4189. * @since 1.3
  4190. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  4191. */
  4192. 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) {
  4193. if ($this->empty_string($this->lasth) OR $reseth) {
  4194. //set row height
  4195. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  4196. }
  4197. if (!$this->empty_string($y)) {
  4198. $this->SetY($y);
  4199. } else {
  4200. $y = $this->GetY();
  4201. }
  4202. // check for page break
  4203. $this->checkPageBreak($h);
  4204. $y = $this->GetY();
  4205. // get current page number
  4206. $startpage = $this->page;
  4207. if (!$this->empty_string($x)) {
  4208. $this->SetX($x);
  4209. } else {
  4210. $x = $this->GetX();
  4211. }
  4212. if ($this->empty_string($w) OR ($w <= 0)) {
  4213. if ($this->rtl) {
  4214. $w = $this->x - $this->lMargin;
  4215. } else {
  4216. $w = $this->w - $this->rMargin - $this->x;
  4217. }
  4218. }
  4219. // store original margin values
  4220. $lMargin = $this->lMargin;
  4221. $rMargin = $this->rMargin;
  4222. if ($this->rtl) {
  4223. $this->SetRightMargin($this->w - $this->x);
  4224. $this->SetLeftMargin($this->x - $w);
  4225. } else {
  4226. $this->SetLeftMargin($this->x);
  4227. $this->SetRightMargin($this->w - $this->x - $w);
  4228. }
  4229. $starty = $this->y;
  4230. if ($autopadding) {
  4231. // Adjust internal padding
  4232. if ($this->cMargin < ($this->LineWidth / 2)) {
  4233. $this->cMargin = ($this->LineWidth / 2);
  4234. }
  4235. // Add top space if needed
  4236. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  4237. $this->y += $this->LineWidth / 2;
  4238. }
  4239. // add top padding
  4240. $this->y += $this->cMargin;
  4241. }
  4242. if ($ishtml) {
  4243. // ******* Write HTML text
  4244. $this->writeHTML($txt, true, 0, $reseth, true, $align);
  4245. $nl = 1;
  4246. } else {
  4247. // ******* Write text
  4248. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh);
  4249. }
  4250. if ($autopadding) {
  4251. // add bottom padding
  4252. $this->y += $this->cMargin;
  4253. // Add bottom space if needed
  4254. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  4255. $this->y += $this->LineWidth / 2;
  4256. }
  4257. }
  4258. // Get end-of-text Y position
  4259. $currentY = $this->y;
  4260. // get latest page number
  4261. $endpage = $this->page;
  4262. // check if a new page has been created
  4263. if ($endpage > $startpage) {
  4264. // design borders around HTML cells.
  4265. for ($page=$startpage; $page <= $endpage; ++$page) {
  4266. $this->setPage($page);
  4267. if ($page == $startpage) {
  4268. $this->y = $starty; // put cursor at the beginning of cell on the first page
  4269. $h = $this->getPageHeight() - $starty - $this->getBreakMargin();
  4270. $cborder = $this->getBorderMode($border, $position='start');
  4271. } elseif ($page == $endpage) {
  4272. $this->y = $this->tMargin; // put cursor at the beginning of last page
  4273. $h = $currentY - $this->tMargin;
  4274. $cborder = $this->getBorderMode($border, $position='end');
  4275. } else {
  4276. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  4277. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  4278. $cborder = $this->getBorderMode($border, $position='middle');
  4279. }
  4280. $nx = $x;
  4281. // account for margin changes
  4282. if ($page > $startpage) {
  4283. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  4284. $nx = $x + ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  4285. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  4286. $nx = $x + ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  4287. }
  4288. }
  4289. $this->SetX($nx);
  4290. $ccode = $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, false);
  4291. if ($cborder OR $fill) {
  4292. $pagebuff = $this->getPageBuffer($this->page);
  4293. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  4294. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  4295. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  4296. $this->intmrk[$this->page] += strlen($ccode."\n");
  4297. }
  4298. }
  4299. } else {
  4300. $h = max($h, ($currentY - $y));
  4301. // put cursor at the beginning of text
  4302. $this->SetY($y);
  4303. $this->SetX($x);
  4304. // design a cell around the text
  4305. $ccode = $this->getCellCode($w, $h, '', $border, 1, '', $fill, '', 0, true);
  4306. if ($border OR $fill) {
  4307. if (end($this->transfmrk[$this->page]) !== false) {
  4308. $pagemarkkey = key($this->transfmrk[$this->page]);
  4309. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  4310. } elseif ($this->InFooter) {
  4311. $pagemark = &$this->footerpos[$this->page];
  4312. } else {
  4313. $pagemark = &$this->intmrk[$this->page];
  4314. }
  4315. $pagebuff = $this->getPageBuffer($this->page);
  4316. $pstart = substr($pagebuff, 0, $pagemark);
  4317. $pend = substr($pagebuff, $pagemark);
  4318. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  4319. $pagemark += strlen($ccode."\n");
  4320. }
  4321. }
  4322. // Get end-of-cell Y position
  4323. $currentY = $this->GetY();
  4324. // restore original margin values
  4325. $this->SetLeftMargin($lMargin);
  4326. $this->SetRightMargin($rMargin);
  4327. if ($ln > 0) {
  4328. //Go to the beginning of the next line
  4329. $this->SetY($currentY);
  4330. if ($ln == 2) {
  4331. $this->SetX($x + $w);
  4332. }
  4333. } else {
  4334. // go left or right by case
  4335. $this->setPage($startpage);
  4336. $this->y = $y;
  4337. $this->SetX($x + $w);
  4338. }
  4339. $this->setContentMark();
  4340. return $nl;
  4341. }
  4342. /**
  4343. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  4344. * @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>
  4345. * @param string multicell position: 'start', 'middle', 'end'
  4346. * @return border mode
  4347. * @access protected
  4348. * @since 4.4.002 (2008-12-09)
  4349. */
  4350. protected function getBorderMode($border, $position='start') {
  4351. if ((!$this->opencell) AND ($border == 1)) {
  4352. return 1;
  4353. }
  4354. $cborder = '';
  4355. switch ($position) {
  4356. case 'start': {
  4357. if ($border == 1) {
  4358. $cborder = 'LTR';
  4359. } else {
  4360. if (!(false === strpos($border, 'L'))) {
  4361. $cborder .= 'L';
  4362. }
  4363. if (!(false === strpos($border, 'T'))) {
  4364. $cborder .= 'T';
  4365. }
  4366. if (!(false === strpos($border, 'R'))) {
  4367. $cborder .= 'R';
  4368. }
  4369. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  4370. $cborder .= 'B';
  4371. }
  4372. }
  4373. break;
  4374. }
  4375. case 'middle': {
  4376. if ($border == 1) {
  4377. $cborder = 'LR';
  4378. } else {
  4379. if (!(false === strpos($border, 'L'))) {
  4380. $cborder .= 'L';
  4381. }
  4382. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  4383. $cborder .= 'T';
  4384. }
  4385. if (!(false === strpos($border, 'R'))) {
  4386. $cborder .= 'R';
  4387. }
  4388. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  4389. $cborder .= 'B';
  4390. }
  4391. }
  4392. break;
  4393. }
  4394. case 'end': {
  4395. if ($border == 1) {
  4396. $cborder = 'LRB';
  4397. } else {
  4398. if (!(false === strpos($border, 'L'))) {
  4399. $cborder .= 'L';
  4400. }
  4401. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  4402. $cborder .= 'T';
  4403. }
  4404. if (!(false === strpos($border, 'R'))) {
  4405. $cborder .= 'R';
  4406. }
  4407. if (!(false === strpos($border, 'B'))) {
  4408. $cborder .= 'B';
  4409. }
  4410. }
  4411. break;
  4412. }
  4413. default: {
  4414. $cborder = $border;
  4415. break;
  4416. }
  4417. }
  4418. return $cborder;
  4419. }
  4420. /**
  4421. * This method returns the estimated number of lines required to print the text (not the real number just a quick estimation).
  4422. * If you want o know the exact number of lines you have to use the following technique:
  4423. * <pre>
  4424. * // store current object
  4425. * $pdf->startTransaction();
  4426. * // get the number of lines for multicell
  4427. * $lines = $pdf->MultiCell($w, 0, $txt, 0, 'L', 0, 0, '', '', true, 0, false, true, 0);
  4428. * // restore previous object
  4429. * $pdf = $pdf->rollbackTransaction();
  4430. * </pre>
  4431. * @param string $txt text to print
  4432. * @param float $w width of cell. If 0, they extend up to the right margin of the page.
  4433. * @return int Return the estimated number of lines.
  4434. * @access public
  4435. * @since 4.5.011
  4436. */
  4437. public function getNumLines($txt, $w=0) {
  4438. $lines = 0;
  4439. if ($this->empty_string($w) OR ($w <= 0)) {
  4440. if ($this->rtl) {
  4441. $w = $this->x - $this->lMargin;
  4442. } else {
  4443. $w = $this->w - $this->rMargin - $this->x;
  4444. }
  4445. }
  4446. // max column width
  4447. $wmax = $w - (2 * $this->cMargin);
  4448. // remove carriage returns
  4449. $txt = str_replace("\r", '', $txt);
  4450. // remove last newline (if any)
  4451. if (substr($txt,-1) == "\n") {
  4452. $txt = substr($txt, 0, -1);
  4453. }
  4454. // divide text in blocks
  4455. $txtblocks = explode("\n", $txt);
  4456. // for each text block
  4457. foreach ($txtblocks as $block) {
  4458. // estimate the number of lines
  4459. $lines += $this->empty_string($block) ? 1 : (ceil($this->GetStringWidth($block) / $wmax));
  4460. }
  4461. // return the number of lines
  4462. return $lines;
  4463. }
  4464. /**
  4465. * This method prints text from the current position.<br />
  4466. * @param float $h Line height
  4467. * @param string $txt String to print
  4468. * @param mixed $link URL or identifier returned by AddLink()
  4469. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  4470. * @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>
  4471. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  4472. * @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>
  4473. * @param boolean $firstline if true prints only the first line and return the remaining string.
  4474. * @param boolean $firstblock if true the string is the starting of a line.
  4475. * @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.
  4476. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  4477. * @access public
  4478. * @since 1.5
  4479. */
  4480. public function Write($h, $txt, $link='', $fill=0, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0) {
  4481. if (strlen($txt) == 0) {
  4482. $txt = ' ';
  4483. }
  4484. // remove carriage returns
  4485. $s = str_replace("\r", '', $txt);
  4486. // check if string contains arabic text
  4487. if (preg_match(K_RE_PATTERN_ARABIC, $s)) {
  4488. $arabic = true;
  4489. } else {
  4490. $arabic = false;
  4491. }
  4492. // check if string contains RTL text
  4493. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(K_RE_PATTERN_RTL, $txt)) {
  4494. $rtlmode = true;
  4495. } else {
  4496. $rtlmode = false;
  4497. }
  4498. // get a char width
  4499. $chrwidth = $this->GetCharWidth('.');
  4500. // get array of unicode values
  4501. $chars = $this->UTF8StringToArray($s);
  4502. // get array of chars
  4503. $uchars = $this->UTF8ArrayToUniArray($chars);
  4504. // get the number of characters
  4505. $nb = count($chars);
  4506. // replacement for SHY character (minus symbol)
  4507. $shy_replacement = 45;
  4508. $shy_replacement_char = $this->unichr($shy_replacement);
  4509. // widht for SHY replacement
  4510. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  4511. // store current position
  4512. $prevx = $this->x;
  4513. $prevy = $this->y;
  4514. // max Y
  4515. $maxy = $this->y + $maxh - $h - (2 * $this->cMargin);
  4516. // calculate remaining line width ($w)
  4517. if ($this->rtl) {
  4518. $w = $this->x - $this->lMargin;
  4519. } else {
  4520. $w = $this->w - $this->rMargin - $this->x;
  4521. }
  4522. // max column width
  4523. $wmax = $w - (2 * $this->cMargin);
  4524. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  4525. // a single character do not fit on column
  4526. return '';
  4527. }
  4528. $i = 0; // character position
  4529. $j = 0; // current starting position
  4530. $sep = -1; // position of the last blank space
  4531. $shy = false; // true if the last blank is a soft hypen (SHY)
  4532. $l = 0; // current string length
  4533. $nl = 0; //number of lines
  4534. $linebreak = false;
  4535. $pc = 0; // previous character
  4536. // for each character
  4537. while ($i < $nb) {
  4538. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  4539. break;
  4540. }
  4541. //Get the current character
  4542. $c = $chars[$i];
  4543. if ($c == 10) { // 10 = "\n" = new line
  4544. //Explicit line break
  4545. if ($align == 'J') {
  4546. if ($this->rtl) {
  4547. $talign = 'R';
  4548. } else {
  4549. $talign = 'L';
  4550. }
  4551. } else {
  4552. $talign = $align;
  4553. }
  4554. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  4555. if ($firstline) {
  4556. $startx = $this->x;
  4557. $tmparr = array_slice($chars, $j, ($i - $j));
  4558. if ($rtlmode) {
  4559. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4560. }
  4561. $linew = $this->GetArrStringWidth($tmparr);
  4562. unset($tmparr);
  4563. if ($this->rtl) {
  4564. $this->endlinex = $startx - $linew;
  4565. } else {
  4566. $this->endlinex = $startx + $linew;
  4567. }
  4568. $w = $linew;
  4569. $tmpcmargin = $this->cMargin;
  4570. if ($maxh == 0) {
  4571. $this->cMargin = 0;
  4572. }
  4573. }
  4574. if ($firstblock AND $this->isRTLTextDir()) {
  4575. $tmpstr = rtrim($tmpstr);
  4576. }
  4577. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  4578. unset($tmpstr);
  4579. if ($firstline) {
  4580. $this->cMargin = $tmpcmargin;
  4581. return ($this->UniArrSubString($uchars, $i));
  4582. }
  4583. ++$nl;
  4584. $j = $i + 1;
  4585. $l = 0;
  4586. $sep = -1;
  4587. $shy = false;
  4588. // account for margin changes
  4589. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  4590. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  4591. $this->AcceptPageBreak();
  4592. }
  4593. $w = $this->getRemainingWidth();
  4594. $wmax = $w - (2 * $this->cMargin);
  4595. } else {
  4596. // 160 is the non-breaking space.
  4597. // 173 is SHY (Soft Hypen).
  4598. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  4599. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  4600. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  4601. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  4602. // update last blank space position
  4603. $sep = $i;
  4604. // check if is a SHY
  4605. if ($c == 173) {
  4606. $shy = true;
  4607. if ($pc == 45) {
  4608. $tmp_shy_replacement_width = 0;
  4609. $tmp_shy_replacement_char = '';
  4610. } else {
  4611. $tmp_shy_replacement_width = $shy_replacement_width;
  4612. $tmp_shy_replacement_char = $shy_replacement_char;
  4613. }
  4614. } else {
  4615. $shy = false;
  4616. }
  4617. }
  4618. // update string length
  4619. if ((($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($arabic)) {
  4620. // with bidirectional algorithm some chars may be changed affecting the line length
  4621. // *** very slow ***
  4622. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  4623. } else {
  4624. $l += $this->GetCharWidth($c);
  4625. }
  4626. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  4627. // we have reached the end of column
  4628. if ($sep == -1) {
  4629. // check if the line was already started
  4630. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  4631. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  4632. // print a void cell and go to next line
  4633. $this->Cell($w, $h, '', 0, 1);
  4634. $linebreak = true;
  4635. if ($firstline) {
  4636. return ($this->UniArrSubString($uchars, $j));
  4637. }
  4638. } else {
  4639. // truncate the word because do not fit on column
  4640. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  4641. if ($firstline) {
  4642. $startx = $this->x;
  4643. $tmparr = array_slice($chars, $j, ($i - $j));
  4644. if ($rtlmode) {
  4645. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4646. }
  4647. $linew = $this->GetArrStringWidth($tmparr);
  4648. unset($tmparr);
  4649. if ($this->rtl) {
  4650. $this->endlinex = $startx - $linew;
  4651. } else {
  4652. $this->endlinex = $startx + $linew;
  4653. }
  4654. $w = $linew;
  4655. $tmpcmargin = $this->cMargin;
  4656. if ($maxh == 0) {
  4657. $this->cMargin = 0;
  4658. }
  4659. }
  4660. if ($firstblock AND $this->isRTLTextDir()) {
  4661. $tmpstr = rtrim($tmpstr);
  4662. }
  4663. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  4664. unset($tmpstr);
  4665. if ($firstline) {
  4666. $this->cMargin = $tmpcmargin;
  4667. return ($this->UniArrSubString($uchars, $i));
  4668. }
  4669. $j = $i;
  4670. --$i;
  4671. }
  4672. } else {
  4673. // word wrapping
  4674. if ($this->rtl AND (!$firstblock)) {
  4675. $endspace = 1;
  4676. } else {
  4677. $endspace = 0;
  4678. }
  4679. if ($shy) {
  4680. // add hypen (minus symbol) at the end of the line
  4681. $shy_width = $tmp_shy_replacement_width;
  4682. if ($this->rtl) {
  4683. $shy_char_left = $tmp_shy_replacement_char;
  4684. $shy_char_right = '';
  4685. } else {
  4686. $shy_char_left = '';
  4687. $shy_char_right = $tmp_shy_replacement_char;
  4688. }
  4689. } else {
  4690. $shy_width = 0;
  4691. $shy_char_left = '';
  4692. $shy_char_right = '';
  4693. }
  4694. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  4695. if ($firstline) {
  4696. $startx = $this->x;
  4697. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  4698. if ($rtlmode) {
  4699. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4700. }
  4701. $linew = $this->GetArrStringWidth($tmparr);
  4702. unset($tmparr);
  4703. if ($this->rtl) {
  4704. $this->endlinex = $startx - $linew - $shy_width;
  4705. } else {
  4706. $this->endlinex = $startx + $linew + $shy_width;
  4707. }
  4708. $w = $linew;
  4709. $tmpcmargin = $this->cMargin;
  4710. if ($maxh == 0) {
  4711. $this->cMargin = 0;
  4712. }
  4713. }
  4714. // print the line
  4715. if ($firstblock AND $this->isRTLTextDir()) {
  4716. $tmpstr = rtrim($tmpstr);
  4717. }
  4718. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  4719. unset($tmpstr);
  4720. if ($firstline) {
  4721. // return the remaining text
  4722. $this->cMargin = $tmpcmargin;
  4723. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  4724. }
  4725. $i = $sep;
  4726. $sep = -1;
  4727. $shy = false;
  4728. $j = ($i+1);
  4729. }
  4730. // account for margin changes
  4731. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  4732. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  4733. $this->AcceptPageBreak();
  4734. }
  4735. $w = $this->getRemainingWidth();
  4736. $wmax = $w - (2 * $this->cMargin);
  4737. if ($linebreak) {
  4738. $linebreak = false;
  4739. } else {
  4740. ++$nl;
  4741. $l = 0;
  4742. }
  4743. }
  4744. }
  4745. // save last character
  4746. $pc = $c;
  4747. ++$i;
  4748. } // end while i < nb
  4749. // print last substring (if any)
  4750. if ($l > 0) {
  4751. switch ($align) {
  4752. case 'J':
  4753. case 'C': {
  4754. $w = $w;
  4755. break;
  4756. }
  4757. case 'L': {
  4758. if ($this->rtl) {
  4759. $w = $w;
  4760. } else {
  4761. $w = $l;
  4762. }
  4763. break;
  4764. }
  4765. case 'R': {
  4766. if ($this->rtl) {
  4767. $w = $l;
  4768. } else {
  4769. $w = $w;
  4770. }
  4771. break;
  4772. }
  4773. default: {
  4774. $w = $l;
  4775. break;
  4776. }
  4777. }
  4778. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  4779. if ($firstline) {
  4780. $startx = $this->x;
  4781. $tmparr = array_slice($chars, $j, ($nb - $j));
  4782. if ($rtlmode) {
  4783. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4784. }
  4785. $linew = $this->GetArrStringWidth($tmparr);
  4786. unset($tmparr);
  4787. if ($this->rtl) {
  4788. $this->endlinex = $startx - $linew;
  4789. } else {
  4790. $this->endlinex = $startx + $linew;
  4791. }
  4792. $w = $linew;
  4793. $tmpcmargin = $this->cMargin;
  4794. if ($maxh == 0) {
  4795. $this->cMargin = 0;
  4796. }
  4797. }
  4798. if ($firstblock AND $this->isRTLTextDir()) {
  4799. $tmpstr = rtrim($tmpstr);
  4800. }
  4801. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  4802. unset($tmpstr);
  4803. if ($firstline) {
  4804. $this->cMargin = $tmpcmargin;
  4805. return ($this->UniArrSubString($uchars, $nb));
  4806. }
  4807. ++$nl;
  4808. }
  4809. if ($firstline) {
  4810. return '';
  4811. }
  4812. return $nl;
  4813. }
  4814. /**
  4815. * Returns the remaining width between the current position and margins.
  4816. * @return int Return the remaining width
  4817. * @access protected
  4818. */
  4819. protected function getRemainingWidth() {
  4820. if ($this->rtl) {
  4821. return ($this->x - $this->lMargin);
  4822. } else {
  4823. return ($this->w - $this->rMargin - $this->x);
  4824. }
  4825. }
  4826. /**
  4827. * Extract a slice of the $strarr array and return it as string.
  4828. * @param string $strarr The input array of characters.
  4829. * @param int $start the starting element of $strarr.
  4830. * @param int $end first element that will not be returned.
  4831. * @return Return part of a string
  4832. * @access public
  4833. */
  4834. public function UTF8ArrSubString($strarr, $start='', $end='') {
  4835. if (strlen($start) == 0) {
  4836. $start = 0;
  4837. }
  4838. if (strlen($end) == 0) {
  4839. $end = count($strarr);
  4840. }
  4841. $string = '';
  4842. for ($i=$start; $i < $end; ++$i) {
  4843. $string .= $this->unichr($strarr[$i]);
  4844. }
  4845. return $string;
  4846. }
  4847. /**
  4848. * Extract a slice of the $uniarr array and return it as string.
  4849. * @param string $uniarr The input array of characters.
  4850. * @param int $start the starting element of $strarr.
  4851. * @param int $end first element that will not be returned.
  4852. * @return Return part of a string
  4853. * @access public
  4854. * @since 4.5.037 (2009-04-07)
  4855. */
  4856. public function UniArrSubString($uniarr, $start='', $end='') {
  4857. if (strlen($start) == 0) {
  4858. $start = 0;
  4859. }
  4860. if (strlen($end) == 0) {
  4861. $end = count($uniarr);
  4862. }
  4863. $string = '';
  4864. for ($i=$start; $i < $end; ++$i) {
  4865. $string .= $uniarr[$i];
  4866. }
  4867. return $string;
  4868. }
  4869. /**
  4870. * Convert an array of UTF8 values to array of unicode characters
  4871. * @param string $ta The input array of UTF8 values.
  4872. * @return Return array of unicode characters
  4873. * @access public
  4874. * @since 4.5.037 (2009-04-07)
  4875. */
  4876. public function UTF8ArrayToUniArray($ta) {
  4877. return array_map(array($this, 'unichr'), $ta);
  4878. }
  4879. /**
  4880. * Returns the unicode caracter specified by UTF-8 value
  4881. * @param int $c UTF-8 value
  4882. * @return Returns the specified character.
  4883. * @author Miguel Perez, Nicola Asuni
  4884. * @access public
  4885. * @since 2.3.000 (2008-03-05)
  4886. */
  4887. public function unichr($c) {
  4888. if (!$this->isunicode) {
  4889. return chr($c);
  4890. } elseif ($c <= 0x7F) {
  4891. // one byte
  4892. return chr($c);
  4893. } elseif ($c <= 0x7FF) {
  4894. // two bytes
  4895. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  4896. } elseif ($c <= 0xFFFF) {
  4897. // three bytes
  4898. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4899. } elseif ($c <= 0x10FFFF) {
  4900. // four bytes
  4901. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4902. } else {
  4903. return '';
  4904. }
  4905. }
  4906. /**
  4907. * Return the image type given the file name or array returned by getimagesize() function.
  4908. * @param string $imgfile image file name
  4909. * @param array $iminfo array of image information returned by getimagesize() function.
  4910. * @return string image type
  4911. * @since 4.8.017 (2009-11-27)
  4912. */
  4913. public function getImageFileType($imgfile, $iminfo=array()) {
  4914. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  4915. $mime = explode('/', $iminfo['mime']);
  4916. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  4917. return trim($mime[1]);
  4918. }
  4919. }
  4920. $type = '';
  4921. $fileinfo = pathinfo($imgfile);
  4922. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  4923. $type = strtolower(trim($fileinfo['extension']));
  4924. }
  4925. if ($type == 'jpg') {
  4926. $type = 'jpeg';
  4927. }
  4928. return $type;
  4929. }
  4930. /**
  4931. * Puts an image in the page.
  4932. * The upper-left corner must be given.
  4933. * The dimensions can be specified in different ways:<ul>
  4934. * <li>explicit width and height (expressed in user unit)</li>
  4935. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  4936. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  4937. * 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;
  4938. * The format can be specified explicitly or inferred from the file extension.<br />
  4939. * It is possible to put a link on the image.<br />
  4940. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  4941. * @param string $file Name of the file containing the image.
  4942. * @param float $x Abscissa of the upper-left corner.
  4943. * @param float $y Ordinate of the upper-left corner.
  4944. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4945. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4946. * @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.
  4947. * @param mixed $link URL or identifier returned by AddLink().
  4948. * @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>
  4949. * @param mixed $resize If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
  4950. * @param int $dpi dot-per-inch resolution used on resize
  4951. * @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>
  4952. * @param boolean $ismask true if this image is a mask, false otherwise
  4953. * @param mixed $imgmask image object returned by this function or false
  4954. * @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>
  4955. * @param boolean $fitbox If true scale image dimensions proportionally to fit within the ($w, $h) box.
  4956. * @param boolean $hidden if true do not display the image.
  4957. * @param boolean $fitonpage if true the image is resized to not exceed page dimensions.
  4958. * @return image information
  4959. * @access public
  4960. * @since 1.1
  4961. */
  4962. 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, $fitonpage=false) {
  4963. if ($x === '') {
  4964. $x = $this->x;
  4965. }
  4966. if ($y === '') {
  4967. $y = $this->y;
  4968. }
  4969. // get image dimensions
  4970. $imsize = @getimagesize($file);
  4971. if ($imsize === FALSE) {
  4972. // encode spaces on filename
  4973. $file = str_replace(' ', '%20', $file);
  4974. $imsize = @getimagesize($file);
  4975. if ($imsize === FALSE) {
  4976. if (($w > 0) AND ($h > 0)) {
  4977. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  4978. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  4979. $imsize = array($pw, $ph);
  4980. } else {
  4981. $this->Error('[Image] Unable to get image width and height: '.$file);
  4982. }
  4983. }
  4984. }
  4985. // get original image width and height in pixels
  4986. list($pixw, $pixh) = $imsize;
  4987. // calculate image width and height on document
  4988. if (($w <= 0) AND ($h <= 0)) {
  4989. // convert image size to document unit
  4990. $w = $this->pixelsToUnits($pixw);
  4991. $h = $this->pixelsToUnits($pixh);
  4992. } elseif ($w <= 0) {
  4993. $w = $h * $pixw / $pixh;
  4994. } elseif ($h <= 0) {
  4995. $h = $w * $pixh / $pixw;
  4996. } elseif ($fitbox AND ($w > 0) AND ($h > 0)) {
  4997. // scale image dimensions proportionally to fit within the ($w, $h) box
  4998. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  4999. $h = $w * $pixh / $pixw;
  5000. } else {
  5001. $w = $h * $pixw / $pixh;
  5002. }
  5003. }
  5004. // Check whether we need a new page first as this does not fit
  5005. $prev_x = $this->x;
  5006. if ($this->checkPageBreak($h, $y)) {
  5007. $y = $this->y;
  5008. if ($this->rtl) {
  5009. $x += ($prev_x - $this->x);
  5010. } else {
  5011. $x += ($this->x - $prev_x);
  5012. }
  5013. }
  5014. // resize image to be contained on a single page
  5015. if ($fitonpage) {
  5016. $ratio_wh = $w / $h;
  5017. if (($y + $h) > $this->PageBreakTrigger) {
  5018. $h = $this->PageBreakTrigger - $y;
  5019. $w = $h * $ratio_wh;
  5020. }
  5021. if (($x + $w) > ($this->w - $this->rMargin)) {
  5022. $w = $this->w - $this->rMargin - $x;
  5023. $h = $w / $ratio_wh;
  5024. }
  5025. }
  5026. // calculate new minimum dimensions in pixels
  5027. $neww = round($w * $this->k * $dpi / $this->dpi);
  5028. $newh = round($h * $this->k * $dpi / $this->dpi);
  5029. // check if resize is necessary (resize is used only to reduce the image)
  5030. $newsize = ($neww * $newh);
  5031. $pixsize = ($pixw * $pixh);
  5032. if (intval($resize) == 2) {
  5033. $resize = true;
  5034. } elseif ($newsize >= $pixsize) {
  5035. $resize = false;
  5036. }
  5037. // check if image has been already added on document
  5038. $newimage = true;
  5039. if (in_array($file, $this->imagekeys)) {
  5040. $newimage = false;
  5041. // get existing image data
  5042. $info = $this->getImageBuffer($file);
  5043. // check if the newer image is larger
  5044. $oldsize = ($info['w'] * $info['h']);
  5045. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  5046. $newimage = true;
  5047. }
  5048. }
  5049. if ($newimage) {
  5050. //First use of image, get info
  5051. if ($type == '') {
  5052. $type = $this->getImageFileType($file, $imsize);
  5053. }
  5054. $mqr = $this->get_mqr();
  5055. $this->set_mqr(false);
  5056. // Specific image handlers
  5057. $mtd = '_parse'.$type;
  5058. // GD image handler function
  5059. $gdfunction = 'imagecreatefrom'.$type;
  5060. $info = false;
  5061. if ((method_exists($this, $mtd)) AND (!($resize AND function_exists($gdfunction)))) {
  5062. // TCPDF image functions
  5063. $info = $this->$mtd($file);
  5064. if ($info == 'pngalpha') {
  5065. return $this->ImagePngAlpha($file, $x, $y, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign);
  5066. }
  5067. }
  5068. if (!$info) {
  5069. if (function_exists($gdfunction)) {
  5070. // GD library
  5071. $img = $gdfunction($file);
  5072. if ($resize) {
  5073. $imgr = imagecreatetruecolor($neww, $newh);
  5074. if (($type == 'gif') OR ($type == 'png')) {
  5075. $imgr = $this->_setGDImageTransparency($imgr, $img);
  5076. }
  5077. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  5078. if (($type == 'gif') OR ($type == 'png')) {
  5079. $info = $this->_toPNG($imgr);
  5080. } else {
  5081. $info = $this->_toJPEG($imgr);
  5082. }
  5083. } else {
  5084. if (($type == 'gif') OR ($type == 'png')) {
  5085. $info = $this->_toPNG($img);
  5086. } else {
  5087. $info = $this->_toJPEG($img);
  5088. }
  5089. }
  5090. } elseif (extension_loaded('imagick')) {
  5091. // ImageMagick library
  5092. $img = new Imagick();
  5093. if ($type == 'SVG') {
  5094. // get SBG file content
  5095. $svgimg = file_get_contents($file);
  5096. // get width and height
  5097. $regs = array();
  5098. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  5099. $tmp = array();
  5100. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  5101. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false) * $dpi / 72;
  5102. $svgimg = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$ow.$this->pdfunit.'"', $svgimg);
  5103. }
  5104. $tmp = array();
  5105. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  5106. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false) * $dpi / 72;
  5107. $svgimg = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$oh.$this->pdfunit.'"', $svgimg);
  5108. }
  5109. $tmp = array();
  5110. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
  5111. $vbw = $ow * (72 / $dpi) * $this->imgscale * $this->k;
  5112. $vbh = $oh * (72 / $dpi) * $this->imgscale * $this->k;
  5113. $svgimg = preg_replace('/<svg/si', '<svg viewBox="0 0 '.$vbw.' '.$vbh.'"', $svgimg);
  5114. }
  5115. }
  5116. $img->readImageBlob($svgimg);
  5117. } else {
  5118. $img->readImage($file);
  5119. }
  5120. if ($resize) {
  5121. $img->resizeImage($neww, $newh, 10, 1, false);
  5122. }
  5123. $img->setCompressionQuality($this->jpeg_quality);
  5124. $img->setImageFormat('jpeg');
  5125. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  5126. $img->writeImage($tempname);
  5127. $info = $this->_parsejpeg($tempname);
  5128. unlink($tempname);
  5129. $img->destroy();
  5130. } else {
  5131. return;
  5132. }
  5133. }
  5134. if ($info === false) {
  5135. //If false, we cannot process image
  5136. return;
  5137. }
  5138. $this->set_mqr($mqr);
  5139. if ($ismask) {
  5140. // force grayscale
  5141. $info['cs'] = 'DeviceGray';
  5142. }
  5143. $info['i'] = $this->numimages;
  5144. if (!in_array($file, $this->imagekeys)) {
  5145. ++$info['i'];
  5146. }
  5147. if ($imgmask !== false) {
  5148. $info['masked'] = $imgmask;
  5149. }
  5150. // add image to document
  5151. $this->setImageBuffer($file, $info);
  5152. }
  5153. // set alignment
  5154. $this->img_rb_y = $y + $h;
  5155. // set alignment
  5156. if ($this->rtl) {
  5157. if ($palign == 'L') {
  5158. $ximg = $this->lMargin;
  5159. } elseif ($palign == 'C') {
  5160. $ximg = ($this->w - $w) / 2;
  5161. } elseif ($palign == 'R') {
  5162. $ximg = $this->w - $this->rMargin - $w;
  5163. } else {
  5164. $ximg = $this->w - $x - $w;
  5165. }
  5166. $this->img_rb_x = $ximg;
  5167. } else {
  5168. if ($palign == 'L') {
  5169. $ximg = $this->lMargin;
  5170. } elseif ($palign == 'C') {
  5171. $ximg = ($this->w - $w) / 2;
  5172. } elseif ($palign == 'R') {
  5173. $ximg = $this->w - $this->rMargin - $w;
  5174. } else {
  5175. $ximg = $x;
  5176. }
  5177. $this->img_rb_x = $ximg + $w;
  5178. }
  5179. if ($ismask OR $hidden) {
  5180. // image is not displayed
  5181. return $info['i'];
  5182. }
  5183. $xkimg = $ximg * $this->k;
  5184. $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']));
  5185. if (!empty($border)) {
  5186. $bx = $x;
  5187. $by = $y;
  5188. $this->x = $ximg;
  5189. if ($this->rtl) {
  5190. $this->x += $w;
  5191. }
  5192. $this->y = $y;
  5193. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  5194. $this->x = $bx;
  5195. $this->y = $by;
  5196. }
  5197. if ($link) {
  5198. $this->Link($ximg, $y, $w, $h, $link, 0);
  5199. }
  5200. // set pointer to align the successive text/objects
  5201. switch($align) {
  5202. case 'T': {
  5203. $this->y = $y;
  5204. $this->x = $this->img_rb_x;
  5205. break;
  5206. }
  5207. case 'M': {
  5208. $this->y = $y + round($h/2);
  5209. $this->x = $this->img_rb_x;
  5210. break;
  5211. }
  5212. case 'B': {
  5213. $this->y = $this->img_rb_y;
  5214. $this->x = $this->img_rb_x;
  5215. break;
  5216. }
  5217. case 'N': {
  5218. $this->SetY($this->img_rb_y);
  5219. break;
  5220. }
  5221. default:{
  5222. break;
  5223. }
  5224. }
  5225. $this->endlinex = $this->img_rb_x;
  5226. return $info['i'];
  5227. }
  5228. /**
  5229. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  5230. * @param boolean $mqr FALSE for off, TRUE for on.
  5231. * @since 4.6.025 (2009-08-17)
  5232. */
  5233. public function set_mqr($mqr) {
  5234. if(!defined('PHP_VERSION_ID')) {
  5235. $version = PHP_VERSION;
  5236. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  5237. }
  5238. if (PHP_VERSION_ID < 50300) {
  5239. @set_magic_quotes_runtime($mqr);
  5240. }
  5241. }
  5242. /**
  5243. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  5244. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  5245. * @since 4.6.025 (2009-08-17)
  5246. */
  5247. public function get_mqr() {
  5248. if(!defined('PHP_VERSION_ID')) {
  5249. $version = PHP_VERSION;
  5250. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  5251. }
  5252. if (PHP_VERSION_ID < 50300) {
  5253. return @get_magic_quotes_runtime();
  5254. }
  5255. return 0;
  5256. }
  5257. /**
  5258. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  5259. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  5260. * @param string $file Image file name.
  5261. * @param image $image Image object.
  5262. * return image JPEG image object.
  5263. * @access protected
  5264. */
  5265. protected function _toJPEG($image) {
  5266. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  5267. imagejpeg($image, $tempname, $this->jpeg_quality);
  5268. imagedestroy($image);
  5269. $retvars = $this->_parsejpeg($tempname);
  5270. // tidy up by removing temporary image
  5271. unlink($tempname);
  5272. return $retvars;
  5273. }
  5274. /**
  5275. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  5276. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  5277. * @param string $file Image file name.
  5278. * @param image $image Image object.
  5279. * return image PNG image object.
  5280. * @access protected
  5281. * @since 4.9.016 (2010-04-20)
  5282. */
  5283. protected function _toPNG($image) {
  5284. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  5285. imagepng($image, $tempname);
  5286. imagedestroy($image);
  5287. $retvars = $this->_parsepng($tempname);
  5288. // tidy up by removing temporary image
  5289. unlink($tempname);
  5290. return $retvars;
  5291. }
  5292. /**
  5293. * Set the transparency for the given GD image.
  5294. * @param image $new_image GD image object
  5295. * @param image $image GD image object.
  5296. * return GD image object.
  5297. * @access protected
  5298. * @since 4.9.016 (2010-04-20)
  5299. */
  5300. protected function _setGDImageTransparency($new_image, $image) {
  5301. // transparency index
  5302. $tid = imagecolortransparent($image);
  5303. // default transparency color
  5304. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  5305. if ($tid >= 0) {
  5306. // get the colors for the transparency index
  5307. $tcol = imagecolorsforindex($image, $tid);
  5308. }
  5309. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  5310. imagefill($new_image, 0, 0, $tid);
  5311. imagecolortransparent($new_image, $tid);
  5312. return $new_image;
  5313. }
  5314. /**
  5315. * Extract info from a JPEG file without using the GD library.
  5316. * @param string $file image file to parse
  5317. * @return array structure containing the image data
  5318. * @access protected
  5319. */
  5320. protected function _parsejpeg($file) {
  5321. $a = getimagesize($file);
  5322. if (empty($a)) {
  5323. $this->Error('Missing or incorrect image file: '.$file);
  5324. }
  5325. if ($a[2] != 2) {
  5326. $this->Error('Not a JPEG file: '.$file);
  5327. }
  5328. if ((!isset($a['channels'])) OR ($a['channels'] == 3)) {
  5329. $colspace = 'DeviceRGB';
  5330. } elseif ($a['channels'] == 4) {
  5331. $colspace = 'DeviceCMYK';
  5332. } else {
  5333. $colspace = 'DeviceGray';
  5334. }
  5335. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  5336. $data = file_get_contents($file);
  5337. return array('w' => $a[0], 'h' => $a[1], 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  5338. }
  5339. /**
  5340. * Extract info from a PNG file without using the GD library.
  5341. * @param string $file image file to parse
  5342. * @return array structure containing the image data
  5343. * @access protected
  5344. */
  5345. protected function _parsepng($file) {
  5346. $f = fopen($file, 'rb');
  5347. if ($f === false) {
  5348. $this->Error('Can\'t open image file: '.$file);
  5349. }
  5350. //Check signature
  5351. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  5352. $this->Error('Not a PNG file: '.$file);
  5353. }
  5354. //Read header chunk
  5355. fread($f, 4);
  5356. if (fread($f, 4) != 'IHDR') {
  5357. $this->Error('Incorrect PNG file: '.$file);
  5358. }
  5359. $w = $this->_freadint($f);
  5360. $h = $this->_freadint($f);
  5361. $bpc = ord(fread($f, 1));
  5362. if ($bpc > 8) {
  5363. //$this->Error('16-bit depth not supported: '.$file);
  5364. fclose($f);
  5365. return false;
  5366. }
  5367. $ct = ord(fread($f, 1));
  5368. if ($ct == 0) {
  5369. $colspace = 'DeviceGray';
  5370. } elseif ($ct == 2) {
  5371. $colspace = 'DeviceRGB';
  5372. } elseif ($ct == 3) {
  5373. $colspace = 'Indexed';
  5374. } else {
  5375. // alpha channel
  5376. fclose($f);
  5377. return 'pngalpha';
  5378. }
  5379. if (ord(fread($f, 1)) != 0) {
  5380. //$this->Error('Unknown compression method: '.$file);
  5381. fclose($f);
  5382. return false;
  5383. }
  5384. if (ord(fread($f, 1)) != 0) {
  5385. //$this->Error('Unknown filter method: '.$file);
  5386. fclose($f);
  5387. return false;
  5388. }
  5389. if (ord(fread($f, 1)) != 0) {
  5390. //$this->Error('Interlacing not supported: '.$file);
  5391. fclose($f);
  5392. return false;
  5393. }
  5394. fread($f, 4);
  5395. $parms = '/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  5396. //Scan chunks looking for palette, transparency and image data
  5397. $pal = '';
  5398. $trns = '';
  5399. $data = '';
  5400. do {
  5401. $n = $this->_freadint($f);
  5402. $type = fread($f, 4);
  5403. if ($type == 'PLTE') {
  5404. //Read palette
  5405. $pal = $this->rfread($f, $n);
  5406. fread($f, 4);
  5407. } elseif ($type == 'tRNS') {
  5408. //Read transparency info
  5409. $t = $this->rfread($f, $n);
  5410. if ($ct == 0) {
  5411. $trns = array(ord(substr($t, 1, 1)));
  5412. } elseif ($ct == 2) {
  5413. $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  5414. } else {
  5415. $pos = strpos($t, chr(0));
  5416. if ($pos !== false) {
  5417. $trns = array($pos);
  5418. }
  5419. }
  5420. fread($f, 4);
  5421. } elseif ($type == 'IDAT') {
  5422. //Read image data block
  5423. $data .= $this->rfread($f, $n);
  5424. fread($f, 4);
  5425. } elseif ($type == 'IEND') {
  5426. break;
  5427. } else {
  5428. $this->rfread($f, $n + 4);
  5429. }
  5430. } while ($n);
  5431. if (($colspace == 'Indexed') AND (empty($pal))) {
  5432. //$this->Error('Missing palette in '.$file);
  5433. fclose($f);
  5434. return false;
  5435. }
  5436. fclose($f);
  5437. return array('w' => $w, 'h' => $h, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  5438. }
  5439. /**
  5440. * Binary-safe and URL-safe file read.
  5441. * 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.
  5442. * @param resource $handle
  5443. * @param int $length
  5444. * @return Returns the read string or FALSE in case of error.
  5445. * @author Nicola Asuni
  5446. * @access protected
  5447. * @since 4.5.027 (2009-03-16)
  5448. */
  5449. protected function rfread($handle, $length) {
  5450. $data = fread($handle, $length);
  5451. if ($data === false) {
  5452. return false;
  5453. }
  5454. $rest = $length - strlen($data);
  5455. if ($rest > 0) {
  5456. $data .= $this->rfread($handle, $rest);
  5457. }
  5458. return $data;
  5459. }
  5460. /**
  5461. * Extract info from a PNG image with alpha channel using the GD library.
  5462. * @param string $file Name of the file containing the image.
  5463. * @param float $x Abscissa of the upper-left corner.
  5464. * @param float $y Ordinate of the upper-left corner.
  5465. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  5466. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  5467. * @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.
  5468. * @param mixed $link URL or identifier returned by AddLink().
  5469. * @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>
  5470. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  5471. * @param int $dpi dot-per-inch resolution used on resize
  5472. * @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>
  5473. * @author Valentin Schmidt, Nicola Asuni
  5474. * @access protected
  5475. * @since 4.3.007 (2008-12-04)
  5476. * @see Image()
  5477. */
  5478. protected function ImagePngAlpha($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='') {
  5479. // get image size
  5480. list($wpx, $hpx) = getimagesize($file);
  5481. // generate images
  5482. $img = imagecreatefrompng($file);
  5483. $imgalpha = imagecreate($wpx, $hpx);
  5484. // generate gray scale pallete
  5485. for ($c = 0; $c < 256; ++$c) {
  5486. ImageColorAllocate($imgalpha, $c, $c, $c);
  5487. }
  5488. // extract alpha channel
  5489. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  5490. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  5491. $colorindex = imagecolorat($img, $xpx, $ypx);
  5492. $col = imagecolorsforindex($img, $colorindex);
  5493. imagesetpixel($imgalpha, $xpx, $ypx, $this->getGDgamma((127 - $col['alpha']) * 255 / 127));
  5494. }
  5495. }
  5496. // create temp alpha file
  5497. $tempfile_alpha = tempnam(K_PATH_CACHE, 'mska_');
  5498. imagepng($imgalpha, $tempfile_alpha);
  5499. imagedestroy($imgalpha);
  5500. // extract image without alpha channel
  5501. $imgplain = imagecreatetruecolor($wpx, $hpx);
  5502. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  5503. // create temp image file
  5504. $tempfile_plain = tempnam(K_PATH_CACHE, 'mskp_');
  5505. imagepng($imgplain, $tempfile_plain);
  5506. imagedestroy($imgplain);
  5507. // embed mask image
  5508. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  5509. // embed image, masked with previously embedded mask
  5510. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  5511. // remove temp files
  5512. unlink($tempfile_alpha);
  5513. unlink($tempfile_plain);
  5514. }
  5515. /**
  5516. * Correct the gamma value to be used with GD library
  5517. * @param float $v the gamma value to be corrected
  5518. * @access protected
  5519. * @since 4.3.007 (2008-12-04)
  5520. */
  5521. protected function getGDgamma($v) {
  5522. return (pow(($v / 255), 2.2) * 255);
  5523. }
  5524. /**
  5525. * Performs a line break.
  5526. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  5527. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  5528. * @param boolean $cell if true add a cMargin to the x coordinate
  5529. * @access public
  5530. * @since 1.0
  5531. * @see Cell()
  5532. */
  5533. public function Ln($h='', $cell=false) {
  5534. if (($this->num_columns > 0) AND ($this->y == $this->columns[$this->current_column]['y']) AND isset($this->columns[$this->current_column]['x']) AND ($this->x == $this->columns[$this->current_column]['x'])) {
  5535. // revove vertical space from the top of the column
  5536. return;
  5537. }
  5538. if ($cell) {
  5539. $cellmargin = $this->cMargin;
  5540. } else {
  5541. $cellmargin = 0;
  5542. }
  5543. if ($this->rtl) {
  5544. $this->x = $this->w - $this->rMargin - $cellmargin;
  5545. } else {
  5546. $this->x = $this->lMargin + $cellmargin;
  5547. }
  5548. if (is_string($h)) {
  5549. $this->y += $this->lasth;
  5550. } else {
  5551. $this->y += $h;
  5552. }
  5553. $this->newline = true;
  5554. }
  5555. /**
  5556. * Returns the relative X value of current position.
  5557. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  5558. * @return float
  5559. * @access public
  5560. * @since 1.2
  5561. * @see SetX(), GetY(), SetY()
  5562. */
  5563. public function GetX() {
  5564. //Get x position
  5565. if ($this->rtl) {
  5566. return ($this->w - $this->x);
  5567. } else {
  5568. return $this->x;
  5569. }
  5570. }
  5571. /**
  5572. * Returns the absolute X value of current position.
  5573. * @return float
  5574. * @access public
  5575. * @since 1.2
  5576. * @see SetX(), GetY(), SetY()
  5577. */
  5578. public function GetAbsX() {
  5579. return $this->x;
  5580. }
  5581. /**
  5582. * Returns the ordinate of the current position.
  5583. * @return float
  5584. * @access public
  5585. * @since 1.0
  5586. * @see SetY(), GetX(), SetX()
  5587. */
  5588. public function GetY() {
  5589. return $this->y;
  5590. }
  5591. /**
  5592. * Defines the abscissa of the current position.
  5593. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  5594. * @param float $x The value of the abscissa.
  5595. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  5596. * @access public
  5597. * @since 1.2
  5598. * @see GetX(), GetY(), SetY(), SetXY()
  5599. */
  5600. public function SetX($x, $rtloff=false) {
  5601. if (!$rtloff AND $this->rtl) {
  5602. if ($x >= 0) {
  5603. $this->x = $this->w - $x;
  5604. } else {
  5605. $this->x = abs($x);
  5606. }
  5607. } else {
  5608. if ($x >= 0) {
  5609. $this->x = $x;
  5610. } else {
  5611. $this->x = $this->w + $x;
  5612. }
  5613. }
  5614. if ($this->x < 0) {
  5615. $this->x = 0;
  5616. }
  5617. if ($this->x > $this->w) {
  5618. $this->x = $this->w;
  5619. }
  5620. }
  5621. /**
  5622. * Moves the current abscissa back to the left margin and sets the ordinate.
  5623. * If the passed value is negative, it is relative to the bottom of the page.
  5624. * @param float $y The value of the ordinate.
  5625. * @param bool $resetx if true (default) reset the X position.
  5626. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  5627. * @access public
  5628. * @since 1.0
  5629. * @see GetX(), GetY(), SetY(), SetXY()
  5630. */
  5631. public function SetY($y, $resetx=true, $rtloff=false) {
  5632. if ($resetx) {
  5633. //reset x
  5634. if (!$rtloff AND $this->rtl) {
  5635. $this->x = $this->w - $this->rMargin;
  5636. } else {
  5637. $this->x = $this->lMargin;
  5638. }
  5639. }
  5640. if ($y >= 0) {
  5641. $this->y = $y;
  5642. } else {
  5643. $this->y = $this->h + $y;
  5644. }
  5645. if ($this->y < 0) {
  5646. $this->y = 0;
  5647. }
  5648. if ($this->y > $this->h) {
  5649. $this->y = $this->h;
  5650. }
  5651. }
  5652. /**
  5653. * Defines the abscissa and ordinate of the current position.
  5654. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  5655. * @param float $x The value of the abscissa.
  5656. * @param float $y The value of the ordinate.
  5657. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  5658. * @access public
  5659. * @since 1.2
  5660. * @see SetX(), SetY()
  5661. */
  5662. public function SetXY($x, $y, $rtloff=false) {
  5663. $this->SetY($y, false, $rtloff);
  5664. $this->SetX($x, $rtloff);
  5665. }
  5666. /**
  5667. * Send the document to a given destination: string, local file or browser.
  5668. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  5669. * The method first calls Close() if necessary to terminate the document.
  5670. * @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.
  5671. * @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 server file with the name given by name.</li><li>S: return the document as a string. name is ignored.</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li></ul>
  5672. * @access public
  5673. * @since 1.0
  5674. * @see Close()
  5675. */
  5676. public function Output($name='doc.pdf', $dest='I') {
  5677. //Output PDF to some destination
  5678. //Finish document if necessary
  5679. $this->lastpage();
  5680. if ($this->state < 3) {
  5681. $this->Close();
  5682. }
  5683. //Normalize parameters
  5684. if (is_bool($dest)) {
  5685. $dest = $dest ? 'D' : 'F';
  5686. }
  5687. $dest = strtoupper($dest);
  5688. if ($dest != 'F') {
  5689. $name = preg_replace('/[\s]+/', '_', $name);
  5690. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  5691. }
  5692. if ($this->sign) {
  5693. // *** apply digital signature to the document ***
  5694. // get the document content
  5695. $pdfdoc = $this->getBuffer();
  5696. // remove last newline
  5697. $pdfdoc = substr($pdfdoc, 0, -1);
  5698. // Remove the original buffer
  5699. if (isset($this->diskcache) AND $this->diskcache) {
  5700. // remove buffer file from cache
  5701. unlink($this->buffer);
  5702. }
  5703. unset($this->buffer);
  5704. // remove filler space
  5705. $byterange_string_len = strlen($this->byterange_string);
  5706. // define the ByteRange
  5707. $byte_range = array();
  5708. $byte_range[0] = 0;
  5709. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  5710. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  5711. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  5712. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  5713. // replace the ByteRange
  5714. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  5715. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  5716. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  5717. // write the document to a temporary folder
  5718. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  5719. $f = fopen($tempdoc, 'wb');
  5720. if (!$f) {
  5721. $this->Error('Unable to create temporary file: '.$tempdoc);
  5722. }
  5723. $pdfdoc_length = strlen($pdfdoc);
  5724. fwrite($f, $pdfdoc, $pdfdoc_length);
  5725. fclose($f);
  5726. // get digital signature via openssl library
  5727. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  5728. if (empty($this->signature_data['extracerts'])) {
  5729. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  5730. } else {
  5731. 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']);
  5732. }
  5733. unlink($tempdoc);
  5734. // read signature
  5735. $signature = file_get_contents($tempsign, false, null, $pdfdoc_length);
  5736. unlink($tempsign);
  5737. // extract signature
  5738. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  5739. $tmparr = explode("\n\n", $signature);
  5740. $signature = $tmparr[1];
  5741. unset($tmparr);
  5742. // decode signature
  5743. $signature = base64_decode(trim($signature));
  5744. // convert signature to hex
  5745. $signature = current(unpack('H*', $signature));
  5746. $signature = str_pad($signature, $this->signature_max_length, '0');
  5747. // Add signature to the document
  5748. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, ($byte_range[1]));
  5749. $this->diskcache = false;
  5750. $this->buffer = &$pdfdoc;
  5751. $this->bufferlen = strlen($pdfdoc);
  5752. }
  5753. switch($dest) {
  5754. case 'I': {
  5755. // Send PDF to the standard output
  5756. if (ob_get_contents()) {
  5757. $this->Error('Some data has already been output, can\'t send PDF file');
  5758. }
  5759. if (php_sapi_name() != 'cli') {
  5760. //We send to a browser
  5761. header('Content-Type: application/pdf');
  5762. if (headers_sent()) {
  5763. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  5764. }
  5765. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5766. header('Pragma: public');
  5767. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5768. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5769. header('Content-Length: '.$this->bufferlen);
  5770. header('Content-Disposition: inline; filename="'.basename($name).'";');
  5771. }
  5772. echo $this->getBuffer();
  5773. break;
  5774. }
  5775. case 'D': {
  5776. // Download PDF as file
  5777. if (ob_get_contents()) {
  5778. $this->Error('Some data has already been output, can\'t send PDF file');
  5779. }
  5780. header('Content-Description: File Transfer');
  5781. if (headers_sent()) {
  5782. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  5783. }
  5784. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5785. header('Pragma: public');
  5786. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5787. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5788. // force download dialog
  5789. header('Content-Type: application/force-download');
  5790. header('Content-Type: application/octet-stream', false);
  5791. header('Content-Type: application/download', false);
  5792. header('Content-Type: application/pdf', false);
  5793. // use the Content-Disposition header to supply a recommended filename
  5794. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  5795. header('Content-Transfer-Encoding: binary');
  5796. header('Content-Length: '.$this->bufferlen);
  5797. echo $this->getBuffer();
  5798. break;
  5799. }
  5800. case 'F':
  5801. case 'FI':
  5802. case 'FD': {
  5803. // Save PDF to a local file
  5804. if ($this->diskcache) {
  5805. copy($this->buffer, $name);
  5806. } else {
  5807. $f = fopen($name, 'wb');
  5808. if (!$f) {
  5809. $this->Error('Unable to create output file: '.$name);
  5810. }
  5811. fwrite($f, $this->getBuffer(), $this->bufferlen);
  5812. fclose($f);
  5813. }
  5814. if ($dest == 'FI') {
  5815. // send headers to browser
  5816. header('Content-Type: application/pdf');
  5817. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5818. header('Pragma: public');
  5819. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5820. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5821. header('Content-Length: '.filesize($name));
  5822. header('Content-Disposition: inline; filename="'.basename($name).'";');
  5823. // send document to the browser
  5824. echo file_get_contents($name);
  5825. } elseif ($dest == 'FD') {
  5826. // send headers to browser
  5827. if (ob_get_contents()) {
  5828. $this->Error('Some data has already been output, can\'t send PDF file');
  5829. }
  5830. header('Content-Description: File Transfer');
  5831. if (headers_sent()) {
  5832. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  5833. }
  5834. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  5835. header('Pragma: public');
  5836. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  5837. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  5838. // force download dialog
  5839. header('Content-Type: application/force-download');
  5840. header('Content-Type: application/octet-stream', false);
  5841. header('Content-Type: application/download', false);
  5842. header('Content-Type: application/pdf', false);
  5843. // use the Content-Disposition header to supply a recommended filename
  5844. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  5845. header('Content-Transfer-Encoding: binary');
  5846. header('Content-Length: '.filesize($name));
  5847. // send document to the browser
  5848. echo file_get_contents($name);
  5849. }
  5850. break;
  5851. }
  5852. case 'S': {
  5853. // Returns PDF as a string
  5854. return $this->getBuffer();
  5855. }
  5856. default: {
  5857. $this->Error('Incorrect output destination: '.$dest);
  5858. }
  5859. }
  5860. return '';
  5861. }
  5862. /**
  5863. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  5864. * @param boolean $destroyall if true destroys all class variables, otherwise preserves critical variables.
  5865. * @param boolean $preserve_objcopy if true preserves the objcopy variable
  5866. * @access public
  5867. * @since 4.5.016 (2009-02-24)
  5868. */
  5869. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  5870. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  5871. // remove buffer file from cache
  5872. unlink($this->buffer);
  5873. }
  5874. foreach (array_keys(get_object_vars($this)) as $val) {
  5875. if ($destroyall OR (
  5876. ($val != 'internal_encoding')
  5877. AND ($val != 'state')
  5878. AND ($val != 'bufferlen')
  5879. AND ($val != 'buffer')
  5880. AND ($val != 'diskcache')
  5881. AND ($val != 'sign')
  5882. AND ($val != 'signature_data')
  5883. AND ($val != 'signature_max_length')
  5884. AND ($val != 'byterange_string')
  5885. )) {
  5886. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  5887. unset($this->$val);
  5888. }
  5889. }
  5890. }
  5891. }
  5892. /**
  5893. * Check for locale-related bug
  5894. * @access protected
  5895. */
  5896. protected function _dochecks() {
  5897. //Check for locale-related bug
  5898. if (1.1 == 1) {
  5899. $this->Error('Don\'t alter the locale before including class file');
  5900. }
  5901. //Check for decimal separator
  5902. if (sprintf('%.1F', 1.0) != '1.0') {
  5903. setlocale(LC_NUMERIC, 'C');
  5904. }
  5905. }
  5906. /**
  5907. * Return fonts path
  5908. * @return string
  5909. * @access protected
  5910. */
  5911. protected function _getfontpath() {
  5912. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  5913. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  5914. }
  5915. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  5916. }
  5917. /**
  5918. * Output pages.
  5919. * @access protected
  5920. */
  5921. protected function _putpages() {
  5922. $nb = $this->numpages;
  5923. if (!empty($this->AliasNbPages)) {
  5924. $nbs = $this->formatPageNumber($nb);
  5925. $nbu = $this->UTF8ToUTF16BE($nbs, false); // replacement for unicode font
  5926. $alias_a = $this->_escape($this->AliasNbPages);
  5927. $alias_au = $this->_escape('{'.$this->AliasNbPages.'}');
  5928. if ($this->isunicode) {
  5929. $alias_b = $this->_escape($this->UTF8ToLatin1($this->AliasNbPages));
  5930. $alias_bu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNbPages.'}'));
  5931. $alias_c = $this->_escape($this->utf8StrRev($this->AliasNbPages, false, $this->tmprtl));
  5932. $alias_cu = $this->_escape($this->utf8StrRev('{'.$this->AliasNbPages.'}', false, $this->tmprtl));
  5933. }
  5934. }
  5935. if (!empty($this->AliasNumPage)) {
  5936. $alias_pa = $this->_escape($this->AliasNumPage);
  5937. $alias_pau = $this->_escape('{'.$this->AliasNumPage.'}');
  5938. if ($this->isunicode) {
  5939. $alias_pb = $this->_escape($this->UTF8ToLatin1($this->AliasNumPage));
  5940. $alias_pbu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNumPage.'}'));
  5941. $alias_pc = $this->_escape($this->utf8StrRev($this->AliasNumPage, false, $this->tmprtl));
  5942. $alias_pcu = $this->_escape($this->utf8StrRev('{'.$this->AliasNumPage.'}', false, $this->tmprtl));
  5943. }
  5944. }
  5945. $pagegroupnum = 0;
  5946. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  5947. for ($n=1; $n <= $nb; ++$n) {
  5948. $temppage = $this->getPageBuffer($n);
  5949. if (!empty($this->pagegroups)) {
  5950. if(isset($this->newpagegroup[$n])) {
  5951. $pagegroupnum = 0;
  5952. }
  5953. ++$pagegroupnum;
  5954. foreach ($this->pagegroups as $k => $v) {
  5955. // replace total pages group numbers
  5956. $vs = $this->formatPageNumber($v);
  5957. $vu = $this->UTF8ToUTF16BE($vs, false);
  5958. $alias_ga = $this->_escape($k);
  5959. $alias_gau = $this->_escape('{'.$k.'}');
  5960. if ($this->isunicode) {
  5961. $alias_gb = $this->_escape($this->UTF8ToLatin1($k));
  5962. $alias_gbu = $this->_escape($this->UTF8ToLatin1('{'.$k.'}'));
  5963. $alias_gc = $this->_escape($this->utf8StrRev($k, false, $this->tmprtl));
  5964. $alias_gcu = $this->_escape($this->utf8StrRev('{'.$k.'}', false, $this->tmprtl));
  5965. }
  5966. $temppage = str_replace($alias_gau, $vu, $temppage);
  5967. if ($this->isunicode) {
  5968. $temppage = str_replace($alias_gbu, $vu, $temppage);
  5969. $temppage = str_replace($alias_gcu, $vu, $temppage);
  5970. $temppage = str_replace($alias_gb, $vs, $temppage);
  5971. $temppage = str_replace($alias_gc, $vs, $temppage);
  5972. }
  5973. $temppage = str_replace($alias_ga, $vs, $temppage);
  5974. // replace page group numbers
  5975. $pvs = $this->formatPageNumber($pagegroupnum);
  5976. $pvu = $this->UTF8ToUTF16BE($pvs, false);
  5977. $pk = str_replace('{nb', '{pnb', $k);
  5978. $alias_pga = $this->_escape($pk);
  5979. $alias_pgau = $this->_escape('{'.$pk.'}');
  5980. if ($this->isunicode) {
  5981. $alias_pgb = $this->_escape($this->UTF8ToLatin1($pk));
  5982. $alias_pgbu = $this->_escape($this->UTF8ToLatin1('{'.$pk.'}'));
  5983. $alias_pgc = $this->_escape($this->utf8StrRev($pk, false, $this->tmprtl));
  5984. $alias_pgcu = $this->_escape($this->utf8StrRev('{'.$pk.'}', false, $this->tmprtl));
  5985. }
  5986. $temppage = str_replace($alias_pgau, $pvu, $temppage);
  5987. if ($this->isunicode) {
  5988. $temppage = str_replace($alias_pgbu, $pvu, $temppage);
  5989. $temppage = str_replace($alias_pgcu, $pvu, $temppage);
  5990. $temppage = str_replace($alias_pgb, $pvs, $temppage);
  5991. $temppage = str_replace($alias_pgc, $pvs, $temppage);
  5992. }
  5993. $temppage = str_replace($alias_pga, $pvs, $temppage);
  5994. }
  5995. }
  5996. if (!empty($this->AliasNbPages)) {
  5997. // replace total pages number
  5998. $temppage = str_replace($alias_au, $nbu, $temppage);
  5999. if ($this->isunicode) {
  6000. $temppage = str_replace($alias_bu, $nbu, $temppage);
  6001. $temppage = str_replace($alias_cu, $nbu, $temppage);
  6002. $temppage = str_replace($alias_b, $nbs, $temppage);
  6003. $temppage = str_replace($alias_c, $nbs, $temppage);
  6004. }
  6005. $temppage = str_replace($alias_a, $nbs, $temppage);
  6006. }
  6007. if (!empty($this->AliasNumPage)) {
  6008. // replace page number
  6009. $pnbs = $this->formatPageNumber($n);
  6010. $pnbu = $this->UTF8ToUTF16BE($pnbs, false); // replacement for unicode font
  6011. $temppage = str_replace($alias_pau, $pnbu, $temppage);
  6012. if ($this->isunicode) {
  6013. $temppage = str_replace($alias_pbu, $pnbu, $temppage);
  6014. $temppage = str_replace($alias_pcu, $pnbu, $temppage);
  6015. $temppage = str_replace($alias_pb, $pnbs, $temppage);
  6016. $temppage = str_replace($alias_pc, $pnbs, $temppage);
  6017. }
  6018. $temppage = str_replace($alias_pa, $pnbs, $temppage);
  6019. }
  6020. $temppage = str_replace($this->epsmarker, '', $temppage);
  6021. //Page
  6022. $this->page_obj_id[$n] = $this->_newobj();
  6023. $out = '<</Type /Page';
  6024. $out .= ' /Parent 1 0 R';
  6025. $out .= ' '.sprintf('/MediaBox [0 0 %.2F %.2F]', $this->pagedim[$n]['w'], $this->pagedim[$n]['h']);
  6026. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  6027. $out .= ' /Resources 2 0 R';
  6028. $this->_out($out);
  6029. $this->_putannotsrefs($n);
  6030. $this->_out('/Contents '.($this->n + 1).' 0 R>> endobj');
  6031. //Page content
  6032. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  6033. $this->_newobj();
  6034. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> '.$this->_getstream($p).' endobj');
  6035. if ($this->diskcache) {
  6036. // remove temporary files
  6037. unlink($this->pages[$n]);
  6038. }
  6039. }
  6040. //Pages root
  6041. $this->offsets[1] = $this->bufferlen;
  6042. $out = '1 0 obj <</Type /Pages /Kids [';
  6043. foreach($this->page_obj_id as $page_obj) {
  6044. $out .= ' '.$page_obj.' 0 R';
  6045. }
  6046. $out .= ' ] /Count '.$nb.' >> endobj';
  6047. $this->_out($out);
  6048. }
  6049. /**
  6050. * Output references to page annotations
  6051. * @param int $n page number
  6052. * @access protected
  6053. * @author Nicola Asuni
  6054. * @since 4.7.000 (2008-08-29)
  6055. */
  6056. protected function _putannotsrefs($n) {
  6057. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  6058. return;
  6059. }
  6060. $out = '/Annots [';
  6061. if (isset($this->PageAnnots[$n])) {
  6062. $num_annots = count($this->PageAnnots[$n]);
  6063. for ($i = 0; $i < $num_annots; ++$i) {
  6064. ++$this->curr_annot_obj_id;
  6065. if (!in_array($this->curr_annot_obj_id, $this->radio_groups)) {
  6066. $out .= ' '.$this->curr_annot_obj_id.' 0 R';
  6067. } else {
  6068. ++$num_annots;
  6069. }
  6070. }
  6071. }
  6072. if (($n==1) AND $this->sign AND isset($this->signature_data['cert_type'])) {
  6073. // set reference for signature object
  6074. $out .= ' '.$this->sig_annot_ref;
  6075. }
  6076. $out .= ' ]';
  6077. $this->_out($out);
  6078. }
  6079. /**
  6080. * Output annotations objects for all pages.
  6081. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  6082. * See section 12.5 of PDF 32000_2008 reference.
  6083. * @access protected
  6084. * @author Nicola Asuni
  6085. * @since 4.0.018 (2008-08-06)
  6086. */
  6087. protected function _putannotsobjs() {
  6088. // reset object counter
  6089. $this->annot_obj_id = $this->annots_start_obj_id;
  6090. for ($n=1; $n <= $this->numpages; ++$n) {
  6091. if (isset($this->PageAnnots[$n])) {
  6092. // set page annotations
  6093. foreach ($this->PageAnnots[$n] as $key => $pl) {
  6094. // create annotation object for grouping radiobuttons
  6095. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  6096. $annots = '<<';
  6097. $annots .= ' /Type /Annot';
  6098. $annots .= ' /Subtype /Widget';
  6099. $annots .= ' /T '.$this->_dataannobjstring($pl['txt']);
  6100. $annots .= ' /FT /Btn';
  6101. $annots .= ' /Ff 49152';
  6102. $annots .= ' /Kids [';
  6103. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $data) {
  6104. $annots .= ' '.$data['kid'].' 0 R';
  6105. if ($data['def'] !== 'Off') {
  6106. $defval = $data['def'];
  6107. }
  6108. }
  6109. $annots .= ' ]';
  6110. if (isset($defval)) {
  6111. $annots .= ' /V /'.$defval;
  6112. }
  6113. $annots .= ' >>';
  6114. ++$this->annot_obj_id;
  6115. $this->offsets[$this->annot_obj_id] = $this->bufferlen;
  6116. $this->_out($this->annot_obj_id.' 0 obj '.$annots.' endobj');
  6117. $this->form_obj_id[] = $this->annot_obj_id;
  6118. // store object id to be used on Parent entry of Kids
  6119. $this->radiobutton_groups[$n][$pl['txt']] = $this->annot_obj_id;
  6120. }
  6121. $formfield = false;
  6122. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  6123. $a = $pl['x'] * $this->k;
  6124. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  6125. $c = $pl['w'] * $this->k;
  6126. $d = $pl['h'] * $this->k;
  6127. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  6128. // create new annotation object
  6129. $annots = '<</Type /Annot';
  6130. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  6131. $annots .= ' /Rect ['.$rect.']';
  6132. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  6133. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  6134. $annots .= ' /FT /'.$pl['opt']['ft'];
  6135. $formfield = true;
  6136. }
  6137. $annots .= ' /Contents '.$this->_textannobjstring($pl['txt']);
  6138. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  6139. $annots .= ' /NM '.$this->_dataannobjstring(sprintf('%04u-%04u', $n, $key));
  6140. $annots .= ' /M '.$this->_datestring();
  6141. if (isset($pl['opt']['f'])) {
  6142. $val = 0;
  6143. if (is_array($pl['opt']['f'])) {
  6144. foreach ($pl['opt']['f'] as $f) {
  6145. switch (strtolower($f)) {
  6146. case 'invisible': {
  6147. $val += 1 << 0;
  6148. break;
  6149. }
  6150. case 'hidden': {
  6151. $val += 1 << 1;
  6152. break;
  6153. }
  6154. case 'print': {
  6155. $val += 1 << 2;
  6156. break;
  6157. }
  6158. case 'nozoom': {
  6159. $val += 1 << 3;
  6160. break;
  6161. }
  6162. case 'norotate': {
  6163. $val += 1 << 4;
  6164. break;
  6165. }
  6166. case 'noview': {
  6167. $val += 1 << 5;
  6168. break;
  6169. }
  6170. case 'readonly': {
  6171. $val += 1 << 6;
  6172. break;
  6173. }
  6174. case 'locked': {
  6175. $val += 1 << 8;
  6176. break;
  6177. }
  6178. case 'togglenoview': {
  6179. $val += 1 << 9;
  6180. break;
  6181. }
  6182. case 'lockedcontents': {
  6183. $val += 1 << 10;
  6184. break;
  6185. }
  6186. default: {
  6187. break;
  6188. }
  6189. }
  6190. }
  6191. } else {
  6192. $val = intval($pl['opt']['f']);
  6193. }
  6194. $annots .= ' /F '.intval($val);
  6195. }
  6196. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  6197. $annots .= ' /AS /'.$pl['opt']['as'];
  6198. }
  6199. if (isset($pl['opt']['ap'])) {
  6200. // appearance stream
  6201. $annots .= ' /AP <<';
  6202. if (is_array($pl['opt']['ap'])) {
  6203. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  6204. // $apmode can be: n = normal; r = rollover; d = down;
  6205. $annots .= ' /'.strtoupper($apmode);
  6206. if (is_array($apdef)) {
  6207. $annots .= ' <<';
  6208. foreach ($apdef as $apstate => $stream) {
  6209. // reference to XObject that define the appearance for this mode-state
  6210. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  6211. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  6212. }
  6213. $annots .= ' >>';
  6214. } else {
  6215. // reference to XObject that define the appearance for this mode
  6216. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  6217. $annots .= ' '.$apsobjid.' 0 R';
  6218. }
  6219. }
  6220. } else {
  6221. $annots .= $pl['opt']['ap'];
  6222. }
  6223. $annots .= ' >>';
  6224. }
  6225. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  6226. $annots .= ' /BS <<';
  6227. $annots .= ' /Type /Border';
  6228. if (isset($pl['opt']['bs']['w'])) {
  6229. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  6230. }
  6231. $bstyles = array('S', 'D', 'B', 'I', 'U');
  6232. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  6233. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  6234. }
  6235. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  6236. $annots .= ' /D [';
  6237. foreach ($pl['opt']['bs']['d'] as $cord) {
  6238. $annots .= ' '.intval($cord);
  6239. }
  6240. $annots .= ']';
  6241. }
  6242. $annots .= ' >>';
  6243. } else {
  6244. $annots .= ' /Border [';
  6245. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  6246. $annots .= intval($pl['opt']['border'][0]).' ';
  6247. $annots .= intval($pl['opt']['border'][1]).' ';
  6248. $annots .= intval($pl['opt']['border'][2]);
  6249. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  6250. $annots .= ' [';
  6251. foreach ($pl['opt']['border'][3] as $dash) {
  6252. $annots .= intval($dash).' ';
  6253. }
  6254. $annots .= ']';
  6255. }
  6256. } else {
  6257. $annots .= '0 0 0';
  6258. }
  6259. $annots .= ']';
  6260. }
  6261. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  6262. $annots .= ' /BE <<';
  6263. $bstyles = array('S', 'C');
  6264. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  6265. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  6266. } else {
  6267. $annots .= ' /S /S';
  6268. }
  6269. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  6270. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  6271. }
  6272. $annots .= '>>';
  6273. }
  6274. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  6275. $annots .= ' /C [';
  6276. foreach ($pl['opt']['c'] as $col) {
  6277. $col = intval($col);
  6278. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  6279. $annots .= sprintf(' %.4F', $color);
  6280. }
  6281. $annots .= ']';
  6282. }
  6283. //$annots .= ' /StructParent ';
  6284. //$annots .= ' /OC ';
  6285. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  6286. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  6287. // this is a markup type
  6288. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  6289. $annots .= ' /T '.$this->_textannobjstring($pl['opt']['t']);
  6290. }
  6291. //$annots .= ' /Popup ';
  6292. if (isset($pl['opt']['ca'])) {
  6293. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  6294. }
  6295. if (isset($pl['opt']['rc'])) {
  6296. $annots .= ' /RC '.$this->_textannobjstring($pl['opt']['rc']);
  6297. }
  6298. $annots .= ' /CreationDate '.$this->_datestring();
  6299. //$annots .= ' /IRT ';
  6300. if (isset($pl['opt']['subj'])) {
  6301. $annots .= ' /Subj '.$this->_textannobjstring($pl['opt']['subj']);
  6302. }
  6303. //$annots .= ' /RT ';
  6304. //$annots .= ' /IT ';
  6305. //$annots .= ' /ExData ';
  6306. }
  6307. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  6308. switch (strtolower($pl['opt']['subtype'])) {
  6309. case 'text': {
  6310. if (isset($pl['opt']['open'])) {
  6311. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  6312. }
  6313. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  6314. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  6315. $annots .= ' /Name /'.$pl['opt']['name'];
  6316. } else {
  6317. $annots .= ' /Name /Note';
  6318. }
  6319. $statemodels = array('Marked', 'Review');
  6320. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  6321. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  6322. } else {
  6323. $pl['opt']['statemodel'] = 'Marked';
  6324. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  6325. }
  6326. if ($pl['opt']['statemodel'] == 'Marked') {
  6327. $states = array('Accepted', 'Unmarked');
  6328. } else {
  6329. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  6330. }
  6331. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  6332. $annots .= ' /State /'.$pl['opt']['state'];
  6333. } else {
  6334. if ($pl['opt']['statemodel'] == 'Marked') {
  6335. $annots .= ' /State /Unmarked';
  6336. } else {
  6337. $annots .= ' /State /None';
  6338. }
  6339. }
  6340. break;
  6341. }
  6342. case 'link': {
  6343. if(is_string($pl['txt'])) {
  6344. // external URI link
  6345. $annots .= ' /A <</S /URI /URI '.$this->_dataannobjstring($this->unhtmlentities($pl['txt'])).'>>';
  6346. } else {
  6347. // internal link
  6348. $l = $this->links[$pl['txt']];
  6349. $annots .= sprintf(' /Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $l[0])), ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  6350. }
  6351. $hmodes = array('N', 'I', 'O', 'P');
  6352. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  6353. $annots .= ' /H /'.$pl['opt']['h'];
  6354. } else {
  6355. $annots .= ' /H /I';
  6356. }
  6357. //$annots .= ' /PA ';
  6358. //$annots .= ' /Quadpoints ';
  6359. break;
  6360. }
  6361. case 'freetext': {
  6362. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  6363. $annots .= ' /DA ('.$pl['opt']['da'].')';
  6364. }
  6365. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  6366. $annots .= ' /Q '.intval($pl['opt']['q']);
  6367. }
  6368. if (isset($pl['opt']['rc'])) {
  6369. $annots .= ' /RC '.$this->_textannobjstring($pl['opt']['rc']);
  6370. }
  6371. if (isset($pl['opt']['ds'])) {
  6372. $annots .= ' /DS '.$this->_textannobjstring($pl['opt']['ds']);
  6373. }
  6374. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  6375. $annots .= ' /CL [';
  6376. foreach ($pl['opt']['cl'] as $cl) {
  6377. $annots .= sprintf('%.4F ', $cl * $this->k);
  6378. }
  6379. $annots .= ']';
  6380. }
  6381. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  6382. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  6383. $annots .= ' /IT '.$pl['opt']['it'];
  6384. }
  6385. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  6386. $l = $pl['opt']['rd'][0] * $this->k;
  6387. $r = $pl['opt']['rd'][1] * $this->k;
  6388. $t = $pl['opt']['rd'][2] * $this->k;
  6389. $b = $pl['opt']['rd'][3] * $this->k;
  6390. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  6391. }
  6392. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  6393. $annots .= ' /LE /'.$pl['opt']['le'];
  6394. }
  6395. break;
  6396. }
  6397. case 'line': {
  6398. break;
  6399. }
  6400. case 'square': {
  6401. break;
  6402. }
  6403. case 'circle': {
  6404. break;
  6405. }
  6406. case 'polygon': {
  6407. break;
  6408. }
  6409. case 'polyline': {
  6410. break;
  6411. }
  6412. case 'highlight': {
  6413. break;
  6414. }
  6415. case 'underline': {
  6416. break;
  6417. }
  6418. case 'squiggly': {
  6419. break;
  6420. }
  6421. case 'strikeout': {
  6422. break;
  6423. }
  6424. case 'stamp': {
  6425. break;
  6426. }
  6427. case 'caret': {
  6428. break;
  6429. }
  6430. case 'ink': {
  6431. break;
  6432. }
  6433. case 'popup': {
  6434. break;
  6435. }
  6436. case 'fileattachment': {
  6437. if (!isset($pl['opt']['fs'])) {
  6438. break;
  6439. }
  6440. $filename = basename($pl['opt']['fs']);
  6441. if (isset($this->embeddedfiles[$filename]['n'])) {
  6442. $annots .= ' /FS <</Type /Filespec /F '.$this->_dataannobjstring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  6443. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  6444. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  6445. $annots .= ' /Name /'.$pl['opt']['name'];
  6446. } else {
  6447. $annots .= ' /Name /PushPin';
  6448. }
  6449. }
  6450. break;
  6451. }
  6452. case 'sound': {
  6453. if (!isset($pl['opt']['fs'])) {
  6454. break;
  6455. }
  6456. $filename = basename($pl['opt']['fs']);
  6457. if (isset($this->embeddedfiles[$filename]['n'])) {
  6458. // ... TO BE COMPLETED ...
  6459. // /R /C /B /E /CO /CP
  6460. $annots .= ' /Sound <</Type /Filespec /F '.$this->_dataannobjstring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  6461. $iconsapp = array('Speaker', 'Mic');
  6462. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  6463. $annots .= ' /Name /'.$pl['opt']['name'];
  6464. } else {
  6465. $annots .= ' /Name /Speaker';
  6466. }
  6467. }
  6468. break;
  6469. }
  6470. case 'movie': {
  6471. break;
  6472. }
  6473. case 'widget': {
  6474. $hmode = array('N', 'I', 'O', 'P', 'T');
  6475. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  6476. $annots .= ' /H /'.$pl['opt']['h'];
  6477. }
  6478. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  6479. $annots .= ' /MK <<';
  6480. if (isset($pl['opt']['mk']['r'])) {
  6481. $annots .= ' /R '.$pl['opt']['mk']['r'];
  6482. }
  6483. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  6484. $annots .= ' /BC [';
  6485. foreach($pl['opt']['mk']['bc'] AS $col) {
  6486. $col = intval($col);
  6487. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  6488. $annots .= sprintf(' %.2F', $color);
  6489. }
  6490. $annots .= ']';
  6491. }
  6492. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  6493. $annots .= ' /BG [';
  6494. foreach($pl['opt']['mk']['bg'] AS $col) {
  6495. $col = intval($col);
  6496. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  6497. $annots .= sprintf(' %.2F', $color);
  6498. }
  6499. $annots .= ']';
  6500. }
  6501. if (isset($pl['opt']['mk']['ca'])) {
  6502. $annots .= ' /CA '.$pl['opt']['mk']['ca'].'';
  6503. }
  6504. if (isset($pl['opt']['mk']['rc'])) {
  6505. $annots .= ' /RC '.$pl['opt']['mk']['ca'].'';
  6506. }
  6507. if (isset($pl['opt']['mk']['ac'])) {
  6508. $annots .= ' /AC '.$pl['opt']['mk']['ca'].'';
  6509. }
  6510. if (isset($pl['opt']['mk']['i'])) {
  6511. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  6512. if ($info !== false) {
  6513. $annots .= ' /I '.$info['n'].' 0 R';
  6514. }
  6515. }
  6516. if (isset($pl['opt']['mk']['ri'])) {
  6517. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  6518. if ($info !== false) {
  6519. $annots .= ' /RI '.$info['n'].' 0 R';
  6520. }
  6521. }
  6522. if (isset($pl['opt']['mk']['ix'])) {
  6523. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  6524. if ($info !== false) {
  6525. $annots .= ' /IX '.$info['n'].' 0 R';
  6526. }
  6527. }
  6528. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  6529. $annots .= ' /IF <<';
  6530. $if_sw = array('A', 'B', 'S', 'N');
  6531. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  6532. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  6533. }
  6534. $if_s = array('A', 'P');
  6535. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  6536. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  6537. }
  6538. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  6539. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  6540. }
  6541. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  6542. $annots .= ' /FB true';
  6543. }
  6544. $annots .= '>>';
  6545. }
  6546. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  6547. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  6548. } else {
  6549. $annots .= ' /TP 0';
  6550. }
  6551. $annots .= '>>';
  6552. } // end MK
  6553. // --- Entries for field dictionaries ---
  6554. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  6555. // set parent
  6556. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  6557. }
  6558. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  6559. $annots .= ' /T '.$this->_dataannobjstring($pl['opt']['t']);
  6560. }
  6561. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  6562. $annots .= ' /TU '.$this->_dataannobjstring($pl['opt']['tu']);
  6563. }
  6564. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  6565. $annots .= ' /TM '.$this->_dataannobjstring($pl['opt']['tm']);
  6566. }
  6567. if (isset($pl['opt']['ff'])) {
  6568. if (is_array($pl['opt']['ff'])) {
  6569. // array of bit settings
  6570. $flag = 0;
  6571. foreach($pl['opt']['ff'] as $val) {
  6572. $flag += 1 << ($val - 1);
  6573. }
  6574. } else {
  6575. $flag = intval($pl['opt']['ff']);
  6576. }
  6577. $annots .= ' /Ff '.$flag;
  6578. }
  6579. if (isset($pl['opt']['maxlen'])) {
  6580. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  6581. }
  6582. if (isset($pl['opt']['v'])) {
  6583. $annots .= ' /V';
  6584. if (is_array($pl['opt']['v'])) {
  6585. foreach ($pl['opt']['v'] AS $optval) {
  6586. if (is_float($optval)) {
  6587. $optval = sprintf('%.2F', $optval);
  6588. }
  6589. $annots .= ' '.$optval;
  6590. }
  6591. } else {
  6592. $annots .= ' '.$this->_textannobjstring($pl['opt']['v']);
  6593. }
  6594. }
  6595. if (isset($pl['opt']['dv'])) {
  6596. $annots .= ' /DV';
  6597. if (is_array($pl['opt']['dv'])) {
  6598. foreach ($pl['opt']['dv'] AS $optval) {
  6599. if (is_float($optval)) {
  6600. $optval = sprintf('%.2F', $optval);
  6601. }
  6602. $annots .= ' '.$optval;
  6603. }
  6604. } else {
  6605. $annots .= ' '.$this->_textannobjstring($pl['opt']['dv']);
  6606. }
  6607. }
  6608. if (isset($pl['opt']['rv'])) {
  6609. $annots .= ' /RV';
  6610. if (is_array($pl['opt']['rv'])) {
  6611. foreach ($pl['opt']['rv'] AS $optval) {
  6612. if (is_float($optval)) {
  6613. $optval = sprintf('%.2F', $optval);
  6614. }
  6615. $annots .= ' '.$optval;
  6616. }
  6617. } else {
  6618. $annots .= ' '.$this->_textannobjstring($pl['opt']['rv']);
  6619. }
  6620. }
  6621. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  6622. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  6623. }
  6624. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  6625. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  6626. }
  6627. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  6628. $annots .= ' /DA ('.$pl['opt']['da'].')';
  6629. }
  6630. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  6631. $annots .= ' /Q '.intval($pl['opt']['q']);
  6632. }
  6633. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  6634. $annots .= ' /Opt [';
  6635. foreach($pl['opt']['opt'] AS $copt) {
  6636. if (is_array($copt)) {
  6637. $annots .= ' ['.$this->_textannobjstring($copt[0]).' '.$this->_textannobjstring($copt[1]).']';
  6638. } else {
  6639. $annots .= ' '.$this->_textannobjstring($copt);
  6640. }
  6641. }
  6642. $annots .= ']';
  6643. }
  6644. if (isset($pl['opt']['ti'])) {
  6645. $annots .= ' /TI '.intval($pl['opt']['ti']);
  6646. }
  6647. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  6648. $annots .= ' /I [';
  6649. foreach($pl['opt']['i'] AS $copt) {
  6650. $annots .= intval($copt).' ';
  6651. }
  6652. $annots .= ']';
  6653. }
  6654. break;
  6655. }
  6656. case 'screen': {
  6657. break;
  6658. }
  6659. case 'printermark': {
  6660. break;
  6661. }
  6662. case 'trapnet': {
  6663. break;
  6664. }
  6665. case 'watermark': {
  6666. break;
  6667. }
  6668. case '3d': {
  6669. break;
  6670. }
  6671. default: {
  6672. break;
  6673. }
  6674. }
  6675. $annots .= '>>';
  6676. // create new annotation object
  6677. ++$this->annot_obj_id;
  6678. $this->offsets[$this->annot_obj_id] = $this->bufferlen;
  6679. $this->_out($this->annot_obj_id.' 0 obj '.$annots.' endobj');
  6680. if ($formfield AND ! isset($this->radiobutton_groups[$n][$pl['txt']])) {
  6681. // store reference of form object
  6682. $this->form_obj_id[] = $this->annot_obj_id;
  6683. }
  6684. }
  6685. }
  6686. } // end for each page
  6687. }
  6688. /**
  6689. * Put appearance streams XObject used to define annotation's appearance states
  6690. * @param int $w annotation width
  6691. * @param int $h annotation height
  6692. * @param string $stream appearance stream
  6693. * @return int object ID
  6694. * @access protected
  6695. * @since 4.8.001 (2009-09-09)
  6696. */
  6697. protected function _putAPXObject($w=0, $h=0, $stream='') {
  6698. $stream = trim($stream);
  6699. ++$this->apxo_obj_id;
  6700. $this->offsets[$this->apxo_obj_id] = $this->bufferlen;
  6701. $out = $this->apxo_obj_id.' 0 obj';
  6702. $out .= ' <<';
  6703. $out .= ' /Type /XObject';
  6704. $out .= ' /Subtype /Form';
  6705. $out .= ' /FormType 1';
  6706. if ($this->compress) {
  6707. $stream = gzcompress($stream);
  6708. $out .= ' /Filter /FlateDecode';
  6709. }
  6710. $rect = sprintf('%.2F %.2F', $w, $h);
  6711. $out .= ' /BBox [0 0 '.$rect.']';
  6712. $out .= ' /Matrix [1 0 0 1 0 0]';
  6713. $out .= ' /Resources <</ProcSet [/PDF]>>';
  6714. $out .= ' /Length '.strlen($stream);
  6715. $out .= ' >>';
  6716. $out .= ' '.$this->_getstream($stream);
  6717. $out .= ' endobj';
  6718. $this->_out($out);
  6719. return $this->apxo_obj_id;
  6720. }
  6721. /**
  6722. * Output fonts.
  6723. * @access protected
  6724. */
  6725. protected function _putfonts() {
  6726. $nf = $this->n;
  6727. foreach ($this->diffs as $diff) {
  6728. //Encodings
  6729. $this->_newobj();
  6730. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>> endobj');
  6731. }
  6732. $mqr = $this->get_mqr();
  6733. $this->set_mqr(false);
  6734. foreach ($this->FontFiles as $file => $info) {
  6735. // search and get font file to embedd
  6736. $fontdir = $info['fontdir'];
  6737. $file = strtolower($file);
  6738. $fontfile = '';
  6739. // search files on various directories
  6740. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  6741. $fontfile = $fontdir.$file;
  6742. } elseif (file_exists($this->_getfontpath().$file)) {
  6743. $fontfile = $this->_getfontpath().$file;
  6744. } elseif (file_exists($file)) {
  6745. $fontfile = $file;
  6746. }
  6747. if (!$this->empty_string($fontfile)) {
  6748. $font = file_get_contents($fontfile);
  6749. $compressed = (substr($file, -2) == '.z');
  6750. if ((!$compressed) AND (isset($info['length2']))) {
  6751. $header = (ord($font{0}) == 128);
  6752. if ($header) {
  6753. //Strip first binary header
  6754. $font = substr($font, 6);
  6755. }
  6756. if ($header AND (ord($font{$info['length1']}) == 128)) {
  6757. //Strip second binary header
  6758. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  6759. }
  6760. }
  6761. $this->_newobj();
  6762. $this->FontFiles[$file]['n'] = $this->n;
  6763. $out = '<</Length '.strlen($font);
  6764. if ($compressed) {
  6765. $out .= ' /Filter /FlateDecode';
  6766. }
  6767. $out .= ' /Length1 '.$info['length1'];
  6768. if (isset($info['length2'])) {
  6769. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  6770. }
  6771. $out .= ' >>';
  6772. $out .= ' '.$this->_getstream($font);
  6773. $out .= ' endobj';
  6774. $this->_out($out);
  6775. }
  6776. }
  6777. $this->set_mqr($mqr);
  6778. foreach ($this->fontkeys as $k) {
  6779. //Font objects
  6780. $this->setFontSubBuffer($k, 'n', $this->n + 1);
  6781. $font = $this->getFontBuffer($k);
  6782. $type = $font['type'];
  6783. $name = $font['name'];
  6784. if ($type == 'core') {
  6785. //Standard font
  6786. $obj_id = $this->_newobj();
  6787. $out = '<</Type /Font';
  6788. $out .= ' /Subtype /Type1';
  6789. $out .= ' /BaseFont /'.$name;
  6790. $out .= ' /Name /F'.$font['i'];
  6791. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  6792. $out .= ' /Encoding /WinAnsiEncoding';
  6793. }
  6794. if (strtolower($name) == 'helvetica') {
  6795. // add default font for annotations
  6796. $this->annotation_fonts['helvetica'] = $k;
  6797. }
  6798. $out .= ' >> endobj';
  6799. $this->_out($out);
  6800. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  6801. //Additional Type1 or TrueType font
  6802. $obj_id = $this->_newobj();
  6803. $out = '<</Type /Font';
  6804. $out .= ' /Subtype /'.$type;
  6805. $out .= ' /BaseFont /'.$name;
  6806. $out .= ' /Name /F'.$font['i'];
  6807. $out .= ' /FirstChar 32 /LastChar 255';
  6808. $out .= ' /Widths '.($this->n + 1).' 0 R';
  6809. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  6810. if ($font['enc']) {
  6811. if (isset($font['diff'])) {
  6812. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  6813. } else {
  6814. $out .= ' /Encoding /WinAnsiEncoding';
  6815. }
  6816. }
  6817. $out .= ' >> endobj';
  6818. $this->_out($out);
  6819. // Widths
  6820. $this->_newobj();
  6821. $cw = &$font['cw'];
  6822. $s = '[';
  6823. for ($i = 32; $i < 256; ++$i) {
  6824. $s .= $cw[$i].' ';
  6825. }
  6826. $this->_out($s.'] endobj');
  6827. //Descriptor
  6828. $this->_newobj();
  6829. $s = '<</Type /FontDescriptor /FontName /'.$name;
  6830. foreach ($font['desc'] as $fdk => $fdv) {
  6831. if(is_float($fdv)) {
  6832. $fdv = sprintf('%.3F', $fdv);
  6833. }
  6834. $s .= ' /'.$fdk.' '.$fdv.'';
  6835. }
  6836. if (!$this->empty_string($font['file'])) {
  6837. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  6838. }
  6839. $this->_out($s.'>> endobj');
  6840. } else {
  6841. //Allow for additional types
  6842. $mtd = '_put'.strtolower($type);
  6843. if (!method_exists($this, $mtd)) {
  6844. $this->Error('Unsupported font type: '.$type);
  6845. }
  6846. $obj_id = $this->$mtd($font);
  6847. }
  6848. // store object ID for current font
  6849. $this->font_obj_ids[$k] = $obj_id;
  6850. }
  6851. }
  6852. /**
  6853. * Outputs font widths
  6854. * @parameter array $font font data
  6855. * @parameter int $cidoffset offset for CID values
  6856. * @return PDF command string for font widths
  6857. * @author Nicola Asuni
  6858. * @access protected
  6859. * @since 4.4.000 (2008-12-07)
  6860. */
  6861. protected function _putfontwidths($font, $cidoffset=0) {
  6862. ksort($font['cw']);
  6863. $rangeid = 0;
  6864. $range = array();
  6865. $prevcid = -2;
  6866. $prevwidth = -1;
  6867. $interval = false;
  6868. // for each character
  6869. foreach ($font['cw'] as $cid => $width) {
  6870. $cid -= $cidoffset;
  6871. if ($width != $font['dw']) {
  6872. if ($cid == ($prevcid + 1)) {
  6873. // consecutive CID
  6874. if ($width == $prevwidth) {
  6875. if ($width == $range[$rangeid][0]) {
  6876. $range[$rangeid][] = $width;
  6877. } else {
  6878. array_pop($range[$rangeid]);
  6879. // new range
  6880. $rangeid = $prevcid;
  6881. $range[$rangeid] = array();
  6882. $range[$rangeid][] = $prevwidth;
  6883. $range[$rangeid][] = $width;
  6884. }
  6885. $interval = true;
  6886. $range[$rangeid]['interval'] = true;
  6887. } else {
  6888. if ($interval) {
  6889. // new range
  6890. $rangeid = $cid;
  6891. $range[$rangeid] = array();
  6892. $range[$rangeid][] = $width;
  6893. } else {
  6894. $range[$rangeid][] = $width;
  6895. }
  6896. $interval = false;
  6897. }
  6898. } else {
  6899. // new range
  6900. $rangeid = $cid;
  6901. $range[$rangeid] = array();
  6902. $range[$rangeid][] = $width;
  6903. $interval = false;
  6904. }
  6905. $prevcid = $cid;
  6906. $prevwidth = $width;
  6907. }
  6908. }
  6909. // optimize ranges
  6910. $prevk = -1;
  6911. $nextk = -1;
  6912. $prevint = false;
  6913. foreach ($range as $k => $ws) {
  6914. $cws = count($ws);
  6915. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  6916. if (isset($range[$k]['interval'])) {
  6917. unset($range[$k]['interval']);
  6918. }
  6919. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  6920. unset($range[$k]);
  6921. } else {
  6922. $prevk = $k;
  6923. }
  6924. $nextk = $k + $cws;
  6925. if (isset($ws['interval'])) {
  6926. if ($cws > 3) {
  6927. $prevint = true;
  6928. } else {
  6929. $prevint = false;
  6930. }
  6931. unset($range[$k]['interval']);
  6932. --$nextk;
  6933. } else {
  6934. $prevint = false;
  6935. }
  6936. }
  6937. // output data
  6938. $w = '';
  6939. foreach ($range as $k => $ws) {
  6940. if (count(array_count_values($ws)) == 1) {
  6941. // interval mode is more compact
  6942. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  6943. } else {
  6944. // range mode
  6945. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  6946. }
  6947. }
  6948. return '/W ['.$w.' ]';
  6949. }
  6950. /**
  6951. * Adds unicode fonts.<br>
  6952. * Based on PDF Reference 1.3 (section 5)
  6953. * @parameter array $font font data
  6954. * @return int font object ID
  6955. * @access protected
  6956. * @author Nicola Asuni
  6957. * @since 1.52.0.TC005 (2005-01-05)
  6958. */
  6959. protected function _puttruetypeunicode($font) {
  6960. // Type0 Font
  6961. // A composite font composed of other fonts, organized hierarchically
  6962. $obj_id = $this->_newobj();
  6963. $out = '<</Type /Font';
  6964. $out .= ' /Subtype /Type0';
  6965. $out .= ' /BaseFont /'.$font['name'].'';
  6966. $out .= ' /Name /F'.$font['i'];
  6967. $out .= ' /Encoding /'.$font['enc'];
  6968. $out .= ' /ToUnicode /Identity-H';
  6969. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  6970. $out .= ' >>';
  6971. $out .= ' endobj';
  6972. $this->_out($out);
  6973. // CIDFontType2
  6974. // A CIDFont whose glyph descriptions are based on TrueType font technology
  6975. $this->_newobj();
  6976. $out = '<</Type /Font';
  6977. $out .= ' /Subtype /CIDFontType2';
  6978. $out .= ' /BaseFont /'.$font['name'];
  6979. // A dictionary containing entries that define the character collection of the CIDFont.
  6980. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  6981. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  6982. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  6983. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  6984. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  6985. $out .= ' /DW '.$font['dw']; // default width
  6986. $out .= "\n".$this->_putfontwidths($font, 0);
  6987. $out .= ' /CIDToGIDMap '.($this->n + 2).' 0 R >> endobj';
  6988. $this->_out($out);
  6989. // Font descriptor
  6990. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  6991. $this->_newobj();
  6992. $out = '<</Type /FontDescriptor';
  6993. $out .= ' /FontName /'.$font['name'];
  6994. foreach ($font['desc'] as $key => $value) {
  6995. if(is_float($value)) {
  6996. $value = sprintf('%.3F', $value);
  6997. }
  6998. $out .= ' /'.$key.' '.$value;
  6999. }
  7000. $fontdir = false;
  7001. if (!$this->empty_string($font['file'])) {
  7002. // A stream containing a TrueType font
  7003. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  7004. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  7005. }
  7006. $out .= ' >> endobj';
  7007. $this->_out($out);
  7008. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  7009. $this->_newobj();
  7010. // Embed CIDToGIDMap
  7011. // A specification of the mapping from CIDs to glyph indices
  7012. // search and get CTG font file to embedd
  7013. $ctgfile = strtolower($font['ctg']);
  7014. // search and get ctg font file to embedd
  7015. $fontfile = '';
  7016. // search files on various directories
  7017. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  7018. $fontfile = $fontdir.$ctgfile;
  7019. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  7020. $fontfile = $this->_getfontpath().$ctgfile;
  7021. } elseif (file_exists($ctgfile)) {
  7022. $fontfile = $ctgfile;
  7023. }
  7024. if ($this->empty_string($fontfile)) {
  7025. $this->Error('Font file not found: '.$ctgfile);
  7026. }
  7027. $size = filesize($fontfile);
  7028. $out = '<</Length '.$size.'';
  7029. if (substr($fontfile, -2) == '.z') { // check file extension
  7030. // Decompresses data encoded using the public-domain
  7031. // zlib/deflate compression method, reproducing the
  7032. // original text or binary data
  7033. $out .= ' /Filter /FlateDecode';
  7034. }
  7035. $out .= ' >>';
  7036. $out .= ' '.$this->_getstream(file_get_contents($fontfile));
  7037. $out .= ' endobj';
  7038. $this->_out($out);
  7039. }
  7040. return $obj_id;
  7041. }
  7042. /**
  7043. * Output CID-0 fonts.
  7044. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  7045. * @param array $font font data
  7046. * @return int font object ID
  7047. * @access protected
  7048. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  7049. * @since 3.2.000 (2008-06-23)
  7050. */
  7051. protected function _putcidfont0($font) {
  7052. $cidoffset = 0;
  7053. if (!isset($font['cw'][1])) {
  7054. $cidoffset = 31;
  7055. }
  7056. if (isset($font['cidinfo']['uni2cid'])) {
  7057. // convert unicode to cid.
  7058. $uni2cid = $font['cidinfo']['uni2cid'];
  7059. $cw = array();
  7060. foreach ($font['cw'] as $uni => $width) {
  7061. if (isset($uni2cid[$uni])) {
  7062. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  7063. } elseif ($uni < 256) {
  7064. $cw[$uni] = $width;
  7065. } // else unknown character
  7066. }
  7067. $font = array_merge($font, array('cw' => $cw));
  7068. }
  7069. $name = $font['name'];
  7070. $enc = $font['enc'];
  7071. if ($enc) {
  7072. $longname = $name.'-'.$enc;
  7073. } else {
  7074. $longname = $name;
  7075. }
  7076. $obj_id = $this->_newobj();
  7077. $out = '<</Type /Font';
  7078. $out .= ' /Subtype /Type0';
  7079. $out .= ' /BaseFont /'.$longname;
  7080. $out .= ' /Name /F'.$font['i'];
  7081. if ($enc) {
  7082. $out .= ' /Encoding /'.$enc;
  7083. }
  7084. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  7085. $out .= ' >> endobj';
  7086. $this->_out($out);
  7087. $this->_newobj();
  7088. $out = '<</Type /Font';
  7089. $out .= ' /Subtype /CIDFontType0';
  7090. $out .= ' /BaseFont /'.$name;
  7091. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  7092. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  7093. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  7094. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  7095. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  7096. $out .= ' /DW '.$font['dw'];
  7097. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  7098. $out .= ' >> endobj';
  7099. $this->_out($out);
  7100. $this->_newobj();
  7101. $s = '<</Type /FontDescriptor /FontName /'.$name;
  7102. foreach ($font['desc'] as $k => $v) {
  7103. if ($k != 'Style') {
  7104. if(is_float($v)) {
  7105. $v = sprintf('%.3F', $v);
  7106. }
  7107. $s .= ' /'.$k.' '.$v.'';
  7108. }
  7109. }
  7110. $this->_out($s.'>> endobj');
  7111. return $obj_id;
  7112. }
  7113. /**
  7114. * Output images.
  7115. * @access protected
  7116. */
  7117. protected function _putimages() {
  7118. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  7119. foreach ($this->imagekeys as $file) {
  7120. $info = $this->getImageBuffer($file);
  7121. $this->_newobj();
  7122. $this->setImageSubBuffer($file, 'n', $this->n);
  7123. $out = '<</Type /XObject';
  7124. $out .= ' /Subtype /Image';
  7125. $out .= ' /Width '.$info['w'];
  7126. $out .= ' /Height '.$info['h'];
  7127. if (array_key_exists('masked', $info)) {
  7128. $out .= ' /SMask '.($this->n - 1).' 0 R';
  7129. }
  7130. if ($info['cs'] == 'Indexed') {
  7131. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  7132. } else {
  7133. $out .= ' /ColorSpace /'.$info['cs'];
  7134. if ($info['cs'] == 'DeviceCMYK') {
  7135. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  7136. }
  7137. }
  7138. $out .= ' /BitsPerComponent '.$info['bpc'];
  7139. if (isset($info['f'])) {
  7140. $out .= ' /Filter /'.$info['f'];
  7141. }
  7142. if (isset($info['parms'])) {
  7143. $out .= ' '.$info['parms'];
  7144. }
  7145. if (isset($info['trns']) AND is_array($info['trns'])) {
  7146. $trns='';
  7147. $count_info = count($info['trns']);
  7148. for ($i=0; $i < $count_info; ++$i) {
  7149. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  7150. }
  7151. $out .= ' /Mask ['.$trns.']';
  7152. }
  7153. $out .= ' /Length '.strlen($info['data']).' >>';
  7154. $out .= ' '.$this->_getstream($info['data']);
  7155. $out .= ' endobj';
  7156. $this->_out($out);
  7157. //Palette
  7158. if ($info['cs'] == 'Indexed') {
  7159. $this->_newobj();
  7160. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  7161. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> '.$this->_getstream($pal).' endobj');
  7162. }
  7163. }
  7164. }
  7165. /**
  7166. * Output Spot Colors Resources.
  7167. * @access protected
  7168. * @since 4.0.024 (2008-09-12)
  7169. */
  7170. protected function _putspotcolors() {
  7171. foreach ($this->spot_colors as $name => $color) {
  7172. $this->_newobj();
  7173. $this->spot_colors[$name]['n'] = $this->n;
  7174. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  7175. $out .= ' /DeviceCMYK <<';
  7176. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  7177. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', $color['c']/100, $color['m']/100, $color['y']/100, $color['k']/100);
  7178. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  7179. $out .= ' endobj';
  7180. $this->_out($out);
  7181. }
  7182. }
  7183. /**
  7184. * Output Resources Dictionary.
  7185. * @access protected
  7186. */
  7187. protected function _putresourcedict() {
  7188. $out = '2 0 obj';
  7189. $out .= ' << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  7190. $out .= ' /Font <<';
  7191. foreach ($this->fontkeys as $fontkey) {
  7192. $font = $this->getFontBuffer($fontkey);
  7193. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  7194. }
  7195. $out .= ' >>';
  7196. $out .= ' /XObject <<';
  7197. foreach ($this->imagekeys as $file) {
  7198. $info = $this->getImageBuffer($file);
  7199. $out .= ' /I'.$info['i'].' '.$info['n'].' 0 R';
  7200. }
  7201. $out .= ' >>';
  7202. // visibility
  7203. $out .= ' /Properties <</OC1 '.$this->n_ocg_print.' 0 R /OC2 '.$this->n_ocg_view.' 0 R>>';
  7204. // transparency
  7205. $out .= ' /ExtGState <<';
  7206. foreach ($this->extgstates as $k => $extgstate) {
  7207. if (isset($extgstate['name'])) {
  7208. $out .= ' /'.$extgstate['name'];
  7209. } else {
  7210. $out .= ' /GS'.$k;
  7211. }
  7212. $out .= ' '.$extgstate['n'].' 0 R';
  7213. }
  7214. $out .= ' >>';
  7215. // gradient patterns
  7216. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  7217. $out .= ' /Pattern <<';
  7218. foreach ($this->gradients as $id => $grad) {
  7219. $out .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  7220. }
  7221. $out .= ' >>';
  7222. }
  7223. // gradient shadings
  7224. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  7225. $out .= ' /Shading <<';
  7226. foreach ($this->gradients as $id => $grad) {
  7227. $out .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  7228. }
  7229. $out .= ' >>';
  7230. }
  7231. // spot colors
  7232. if (isset($this->spot_colors) AND (count($this->spot_colors) > 0)) {
  7233. $out .= ' /ColorSpace <<';
  7234. foreach ($this->spot_colors as $color) {
  7235. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  7236. }
  7237. $out .= ' >>';
  7238. }
  7239. $out .= ' >> endobj';
  7240. $this->_out($out);
  7241. }
  7242. /**
  7243. * Output Resources.
  7244. * @access protected
  7245. */
  7246. protected function _putresources() {
  7247. $this->_putextgstates();
  7248. $this->_putocg();
  7249. $this->_putfonts();
  7250. $this->_putimages();
  7251. $this->_putspotcolors();
  7252. $this->_putshaders();
  7253. //Resource dictionary
  7254. $this->offsets[2] = $this->bufferlen;
  7255. $this->_putresourcedict();
  7256. $this->_putbookmarks();
  7257. $this->_putEmbeddedFiles();
  7258. $this->_putannotsobjs();
  7259. $this->_putjavascript();
  7260. // encryption
  7261. $this->_putencryption();
  7262. }
  7263. /**
  7264. * Adds some Metadata information (Document Information Dictionary)
  7265. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  7266. * @access protected
  7267. */
  7268. protected function _putinfo() {
  7269. $this->_newobj();
  7270. $out = '<<';
  7271. if (!$this->empty_string($this->title)) {
  7272. // The document's title.
  7273. $out .= ' /Title '.$this->_textstring($this->title);
  7274. }
  7275. if (!$this->empty_string($this->author)) {
  7276. // The name of the person who created the document.
  7277. $out .= ' /Author '.$this->_textstring($this->author);
  7278. }
  7279. if (!$this->empty_string($this->subject)) {
  7280. // The subject of the document.
  7281. $out .= ' /Subject '.$this->_textstring($this->subject);
  7282. }
  7283. if (!$this->empty_string($this->keywords)) {
  7284. // Keywords associated with the document.
  7285. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCP'.'DF');
  7286. }
  7287. if (!$this->empty_string($this->creator)) {
  7288. // 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.
  7289. $out .= ' /Creator '.$this->_textstring($this->creator);
  7290. }
  7291. if (defined('PDF_PRODUCER')) {
  7292. // If the document was converted to PDF from another format, the name of the conforming product that converted it to PDF.
  7293. $out .= ' /Producer '.$this->_textstring(PDF_PRODUCER.' (TCP'.'DF)');
  7294. } else {
  7295. // default producer
  7296. $out .= ' /Producer '.$this->_textstring('TCP'.'DF');
  7297. }
  7298. // The date and time the document was created, in human-readable form
  7299. $out .= ' /CreationDate '.$this->_datestring();
  7300. // The date and time the document was most recently modified, in human-readable form
  7301. $out .= ' /ModDate '.$this->_datestring();
  7302. // A name object indicating whether the document has been modified to include trapping information
  7303. //$out .= ' /Trapped /False');
  7304. $out .= ' >> endobj';
  7305. $this->_out($out);
  7306. }
  7307. /**
  7308. * Output Catalog.
  7309. * @access protected
  7310. */
  7311. protected function _putcatalog() {
  7312. $this->_newobj();
  7313. $out = '<< /Type /Catalog';
  7314. $out .= ' /Pages 1 0 R';
  7315. if ($this->ZoomMode == 'fullpage') {
  7316. $out .= ' /OpenAction [3 0 R /Fit]';
  7317. } elseif ($this->ZoomMode == 'fullwidth') {
  7318. $out .= ' /OpenAction [3 0 R /FitH null]';
  7319. } elseif ($this->ZoomMode == 'real') {
  7320. $out .= ' /OpenAction [3 0 R /XYZ null null 1]';
  7321. } elseif (!is_string($this->ZoomMode)) {
  7322. $out .= ' /OpenAction [3 0 R /XYZ null null '.($this->ZoomMode / 100).']';
  7323. }
  7324. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  7325. $out .= ' /PageLayout /'.$this->LayoutMode;
  7326. }
  7327. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  7328. $out .= ' /PageMode /'.$this->PageMode;
  7329. }
  7330. if (isset($this->l['a_meta_language'])) {
  7331. $out .= ' /Lang /'.$this->l['a_meta_language'];
  7332. }
  7333. $out .= ' /Names <<';
  7334. if ((!empty($this->javascript)) OR (!empty($this->js_objects))) {
  7335. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  7336. }
  7337. $out .= ' >>';
  7338. if (count($this->outlines) > 0) {
  7339. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  7340. $out .= ' /PageMode /UseOutlines';
  7341. }
  7342. $out .= ' '.$this->_putviewerpreferences();
  7343. $p = $this->n_ocg_print.' 0 R';
  7344. $v = $this->n_ocg_view.' 0 R';
  7345. $as = '<</Event /Print /OCGs ['.$p.' '.$v.'] /Category [/Print]>> <</Event /View /OCGs ['.$p.' '.$v.'] /Category [/View]>>';
  7346. $out .= ' /OCProperties <</OCGs ['.$p.' '.$v.'] /D <</ON ['.$p.'] /OFF ['.$v.'] /AS ['.$as.']>>>>';
  7347. // AcroForm
  7348. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  7349. $out .= ' /AcroForm<<';
  7350. $objrefs = '';
  7351. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  7352. $objrefs .= $this->sig_obj_id.' 0 R';
  7353. }
  7354. if (!empty($this->form_obj_id)) {
  7355. foreach($this->form_obj_id as $objid) {
  7356. $objrefs .= ' '.$objid.' 0 R';
  7357. }
  7358. }
  7359. $out .= ' /Fields ['.$objrefs.']';
  7360. $out .= ' /NeedAppearances '.(empty($this->form_obj_id)?'false':'true');
  7361. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  7362. $out .= ' /SigFlags 3';
  7363. }
  7364. //$out .= ' /CO ';
  7365. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  7366. $out .= ' /DR <<';
  7367. $out .= ' /Font <<';
  7368. foreach ($this->annotation_fonts as $font => $fontkey) {
  7369. $out .= ' /F'.($fontkey + 1).' '.$this->font_obj_ids[$font].' 0 R';
  7370. }
  7371. $out .= ' >> >>';
  7372. }
  7373. $out .= ' /DA (/F'.(array_search('helvetica', $this->fontkeys) + 1).' 0 Tf 0 g)';
  7374. $out .= ' /Q '.(($this->rtl)?'2':'0');
  7375. //$out .= ' /XFA ';
  7376. $out .= ' >>';
  7377. // signatures
  7378. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  7379. if ($this->signature_data['cert_type'] > 0) {
  7380. $out .= ' /Perms<</DocMDP '.($this->sig_obj_id + 1).' 0 R>>';
  7381. } else {
  7382. $out .= ' /Perms<</UR3 '.($this->sig_obj_id + 1).' 0 R>>';
  7383. }
  7384. }
  7385. }
  7386. $out .= ' >> endobj';
  7387. $this->_out($out);
  7388. }
  7389. /**
  7390. * Output viewer preferences.
  7391. * @return string for viewer preferences
  7392. * @author Nicola asuni
  7393. * @since 3.1.000 (2008-06-09)
  7394. * @access protected
  7395. */
  7396. protected function _putviewerpreferences() {
  7397. $out = '/ViewerPreferences <<';
  7398. if ($this->rtl) {
  7399. $out .= ' /Direction /R2L';
  7400. } else {
  7401. $out .= ' /Direction /L2R';
  7402. }
  7403. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  7404. $out .= ' /HideToolbar true';
  7405. }
  7406. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  7407. $out .= ' /HideMenubar true';
  7408. }
  7409. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  7410. $out .= ' /HideWindowUI true';
  7411. }
  7412. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  7413. $out .= ' /FitWindow true';
  7414. }
  7415. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  7416. $out .= ' /CenterWindow true';
  7417. }
  7418. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  7419. $out .= ' /DisplayDocTitle true';
  7420. }
  7421. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  7422. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  7423. }
  7424. if (isset($this->viewer_preferences['ViewArea'])) {
  7425. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  7426. }
  7427. if (isset($this->viewer_preferences['ViewClip'])) {
  7428. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  7429. }
  7430. if (isset($this->viewer_preferences['PrintArea'])) {
  7431. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  7432. }
  7433. if (isset($this->viewer_preferences['PrintClip'])) {
  7434. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  7435. }
  7436. if (isset($this->viewer_preferences['PrintScaling'])) {
  7437. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  7438. }
  7439. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  7440. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  7441. }
  7442. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  7443. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  7444. $out .= ' /PickTrayByPDFSize true';
  7445. } else {
  7446. $out .= ' /PickTrayByPDFSize false';
  7447. }
  7448. }
  7449. if (isset($this->viewer_preferences['PrintPageRange'])) {
  7450. $PrintPageRangeNum = '';
  7451. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  7452. $PrintPageRangeNum .= ' '.($v - 1).'';
  7453. }
  7454. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  7455. }
  7456. if (isset($this->viewer_preferences['NumCopies'])) {
  7457. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  7458. }
  7459. $out .= ' >>';
  7460. return $out;
  7461. }
  7462. /**
  7463. * Output trailer.
  7464. * @access protected
  7465. */
  7466. protected function _puttrailer() {
  7467. $out = 'trailer <<';
  7468. $out .= ' /Size '.($this->n + 1);
  7469. $out .= ' /Root '.$this->n.' 0 R';
  7470. $out .= ' /Info '.($this->n - 1).' 0 R';
  7471. if ($this->encrypted) {
  7472. $out .= ' /Encrypt '.$this->enc_obj_id.' 0 R';
  7473. $out .= ' /ID [()()]';
  7474. }
  7475. $out .= ' >>';
  7476. $this->_out($out);
  7477. }
  7478. /**
  7479. * Output PDF header.
  7480. * @access protected
  7481. */
  7482. protected function _putheader() {
  7483. $this->_out('%PDF-'.$this->PDFVersion);
  7484. }
  7485. /**
  7486. * Output end of document (EOF).
  7487. * @access protected
  7488. */
  7489. protected function _enddoc() {
  7490. $this->state = 1;
  7491. $this->_putheader();
  7492. $this->_putpages();
  7493. $this->_putresources();
  7494. // Signature
  7495. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  7496. // widget annotation for signature
  7497. $this->sig_obj_id = $this->_newobj();
  7498. // --- replace signature ID on the first page ---
  7499. // get the document content
  7500. $pdfdoc = $this->getBuffer();
  7501. // Remove the original buffer
  7502. if (isset($this->diskcache) AND $this->diskcache) {
  7503. // remove buffer file from cache
  7504. unlink($this->buffer);
  7505. }
  7506. unset($this->buffer);
  7507. $signature_widget_ref = sprintf('%u 0 R', $this->sig_obj_id);
  7508. $signature_widget_ref .= str_repeat(' ', (strlen($this->sig_annot_ref) - strlen($signature_widget_ref)));
  7509. $pdfdoc = str_replace($this->sig_annot_ref, $signature_widget_ref, $pdfdoc);
  7510. $this->diskcache = false;
  7511. $this->buffer = &$pdfdoc;
  7512. $this->bufferlen = strlen($pdfdoc);
  7513. // ---
  7514. $out = '<< /Type /Annot /Subtype /Widget /Rect [0 0 0 0]';
  7515. $out .= ' /P 3 0 R'; // link to first page object
  7516. $out .= ' /FT /Sig';
  7517. $out .= ' /T '.$this->_textstring('Signature');
  7518. $out .= ' /Ff 0';
  7519. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  7520. $out .= ' >> endobj';
  7521. $this->_out($out);
  7522. // signature
  7523. $this->_putsignature();
  7524. }
  7525. // Info
  7526. $this->_putinfo();
  7527. // Catalog
  7528. $this->_putcatalog();
  7529. // Cross-ref
  7530. $o = $this->bufferlen;
  7531. $this->_out('xref');
  7532. $this->_out('0 '.($this->n + 1));
  7533. $this->_out('0000000000 65535 f ');
  7534. for ($i=1; $i <= $this->n; ++$i) {
  7535. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  7536. }
  7537. // Embedded Files
  7538. if (isset($this->embeddedfiles) AND count($this->embeddedfiles) > 0) {
  7539. $this->_out($this->embedded_start_obj_id.' '.count($this->embeddedfiles));
  7540. foreach ($this->embeddedfiles as $filename => $filedata) {
  7541. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$filedata['n']]));
  7542. }
  7543. }
  7544. // Annotation Objects
  7545. if ($this->annot_obj_id > $this->annots_start_obj_id) {
  7546. $this->_out(($this->annots_start_obj_id + 1).' '.($this->annot_obj_id - $this->annots_start_obj_id));
  7547. for ($i = ($this->annots_start_obj_id + 1); $i <= $this->annot_obj_id; ++$i) {
  7548. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  7549. }
  7550. }
  7551. // Javascript Objects
  7552. if ($this->js_obj_id > $this->js_start_obj_id) {
  7553. $this->_out(($this->js_start_obj_id + 1).' '.($this->js_obj_id - $this->js_start_obj_id));
  7554. for ($i = ($this->js_start_obj_id + 1); $i <= $this->js_obj_id; ++$i) {
  7555. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  7556. }
  7557. }
  7558. // Appearance streams XObjects
  7559. if ($this->apxo_obj_id > $this->apxo_start_obj_id) {
  7560. $this->_out(($this->apxo_start_obj_id + 1).' '.($this->apxo_obj_id - $this->apxo_start_obj_id));
  7561. for ($i = ($this->apxo_start_obj_id + 1); $i <= $this->apxo_obj_id; ++$i) {
  7562. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  7563. }
  7564. }
  7565. //Trailer
  7566. $this->_puttrailer();
  7567. $this->_out('startxref');
  7568. $this->_out($o);
  7569. $this->_out('%%EOF');
  7570. $this->state = 3; // end-of-doc
  7571. if ($this->diskcache) {
  7572. // remove temporary files used for images
  7573. foreach ($this->imagekeys as $key) {
  7574. // remove temporary files
  7575. unlink($this->images[$key]);
  7576. }
  7577. foreach ($this->fontkeys as $key) {
  7578. // remove temporary files
  7579. unlink($this->fonts[$key]);
  7580. }
  7581. }
  7582. }
  7583. /**
  7584. * Initialize a new page.
  7585. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  7586. * @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>
  7587. * @access protected
  7588. */
  7589. protected function _beginpage($orientation='', $format='') {
  7590. ++$this->page;
  7591. $this->setPageBuffer($this->page, '');
  7592. // initialize array for graphics tranformation positions inside a page buffer
  7593. $this->transfmrk[$this->page] = array();
  7594. $this->state = 2;
  7595. if ($this->empty_string($orientation)) {
  7596. if (isset($this->CurOrientation)) {
  7597. $orientation = $this->CurOrientation;
  7598. } else {
  7599. $orientation = 'P';
  7600. }
  7601. }
  7602. if ($this->empty_string($format)) {
  7603. $this->setPageOrientation($orientation);
  7604. } else {
  7605. $this->setPageFormat($format, $orientation);
  7606. }
  7607. if ($this->rtl) {
  7608. $this->x = $this->w - $this->rMargin;
  7609. } else {
  7610. $this->x = $this->lMargin;
  7611. }
  7612. $this->y = $this->tMargin;
  7613. if (isset($this->newpagegroup[$this->page])) {
  7614. // start a new group
  7615. $n = sizeof($this->pagegroups) + 1;
  7616. $alias = '{nb'.$n.'}';
  7617. $this->pagegroups[$alias] = 1;
  7618. $this->currpagegroup = $alias;
  7619. } elseif ($this->currpagegroup) {
  7620. ++$this->pagegroups[$this->currpagegroup];
  7621. }
  7622. }
  7623. /**
  7624. * Mark end of page.
  7625. * @access protected
  7626. */
  7627. protected function _endpage() {
  7628. $this->setVisibility('all');
  7629. $this->state = 1;
  7630. }
  7631. /**
  7632. * Begin a new object and return the object number.
  7633. * @return int object number
  7634. * @access protected
  7635. */
  7636. protected function _newobj() {
  7637. ++$this->n;
  7638. $this->offsets[$this->n] = $this->bufferlen;
  7639. $this->_out($this->n.' 0 obj');
  7640. return $this->n;
  7641. }
  7642. /**
  7643. * Underline text.
  7644. * @param int $x X coordinate
  7645. * @param int $y Y coordinate
  7646. * @param string $txt text to underline
  7647. * @access protected
  7648. */
  7649. protected function _dounderline($x, $y, $txt) {
  7650. $w = $this->GetStringWidth($txt);
  7651. return $this->_dounderlinew($x, $y, $w);
  7652. }
  7653. /**
  7654. * Underline for rectangular text area.
  7655. * @param int $x X coordinate
  7656. * @param int $y Y coordinate
  7657. * @param int $w width to underline
  7658. * @access protected
  7659. * @since 4.8.008 (2009-09-29)
  7660. */
  7661. protected function _dounderlinew($x, $y, $w) {
  7662. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  7663. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - $y + ($linew / 2)) * $this->k, $w * $this->k, $linew);
  7664. }
  7665. /**
  7666. * Line through text.
  7667. * @param int $x X coordinate
  7668. * @param int $y Y coordinate
  7669. * @param string $txt text to linethrough
  7670. * @access protected
  7671. */
  7672. protected function _dolinethrough($x, $y, $txt) {
  7673. $w = $this->GetStringWidth($txt);
  7674. return $this->_dolinethroughw($x, $y, $w);
  7675. }
  7676. /**
  7677. * Line through for rectangular text area.
  7678. * @param int $x X coordinate
  7679. * @param int $y Y coordinate
  7680. * @param string $txt text to linethrough
  7681. * @access protected
  7682. * @since 4.9.008 (2009-09-29)
  7683. */
  7684. protected function _dolinethroughw($x, $y, $w) {
  7685. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  7686. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - $y + ($this->FontSize / 3) + ($linew / 2)) * $this->k, $w * $this->k, $linew);
  7687. }
  7688. /**
  7689. * Overline text.
  7690. * @param int $x X coordinate
  7691. * @param int $y Y coordinate
  7692. * @param string $txt text to overline
  7693. * @access protected
  7694. * @since 4.9.015 (2010-04-19)
  7695. */
  7696. protected function _dooverline($x, $y, $txt) {
  7697. $w = $this->GetStringWidth($txt);
  7698. return $this->_dooverlinew($x, $y, $w);
  7699. }
  7700. /**
  7701. * Overline for rectangular text area.
  7702. * @param int $x X coordinate
  7703. * @param int $y Y coordinate
  7704. * @param int $w width to overline
  7705. * @access protected
  7706. * @since 4.9.015 (2010-04-19)
  7707. */
  7708. protected function _dooverlinew($x, $y, $w) {
  7709. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  7710. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - $y + $this->FontAscent - ($linew / 2)) * $this->k, $w * $this->k, $linew);
  7711. }
  7712. /**
  7713. * Read a 4-byte integer from file.
  7714. * @param string $f file name.
  7715. * @return 4-byte integer
  7716. * @access protected
  7717. */
  7718. protected function _freadint($f) {
  7719. $a = unpack('Ni', fread($f, 4));
  7720. return $a['i'];
  7721. }
  7722. /**
  7723. * Add "\" before "\", "(" and ")"
  7724. * @param string $s string to escape.
  7725. * @return string escaped string.
  7726. * @access protected
  7727. */
  7728. protected function _escape($s) {
  7729. // the chr(13) substitution fixes the Bugs item #1421290.
  7730. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  7731. }
  7732. /**
  7733. * Format a data string for meta information
  7734. * @param string $s data string to escape.
  7735. * @return string escaped string.
  7736. * @access protected
  7737. */
  7738. protected function _datastring($s) {
  7739. if ($this->encrypted) {
  7740. $s = $this->_RC4($this->_objectkey($this->n), $s);
  7741. }
  7742. return '('. $this->_escape($s).')';
  7743. }
  7744. /**
  7745. * Format a data string for annotation objects
  7746. * @param string $s data string to escape.
  7747. * @return string escaped string.
  7748. * @access protected
  7749. */
  7750. protected function _dataannobjstring($s) {
  7751. if ($this->encrypted) {
  7752. $s = $this->_RC4($this->_objectkey($this->annot_obj_id + 1), $s);
  7753. }
  7754. return '('. $this->_escape($s).')';
  7755. }
  7756. /**
  7757. * Returns a formatted date for meta information
  7758. * @return string escaped date string.
  7759. * @access protected
  7760. * @since 4.6.028 (2009-08-25)
  7761. */
  7762. protected function _datestring() {
  7763. $current_time = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  7764. return $this->_datastring('D:'.$current_time);
  7765. }
  7766. /**
  7767. * Format a text string for meta information
  7768. * @param string $s string to escape.
  7769. * @return string escaped string.
  7770. * @access protected
  7771. */
  7772. protected function _textstring($s) {
  7773. if ($this->isunicode) {
  7774. //Convert string to UTF-16BE
  7775. $s = $this->UTF8ToUTF16BE($s, true);
  7776. }
  7777. return $this->_datastring($s);
  7778. }
  7779. /**
  7780. * Format a UTF-8 text string for meta information on annotations
  7781. * @param string $s string to escape.
  7782. * @return string escaped string.
  7783. * @access protected
  7784. */
  7785. protected function _textannobjstring($s) {
  7786. if ($this->isunicode) {
  7787. //Convert string to UTF-16BE
  7788. $s = $this->UTF8ToUTF16BE($s, true);
  7789. }
  7790. return $this->_dataannobjstring($s);
  7791. }
  7792. /**
  7793. * Format a text string
  7794. * @param string $s string to escape.
  7795. * @return string escaped string.
  7796. * @access protected
  7797. */
  7798. protected function _escapetext($s) {
  7799. if ($this->isunicode) {
  7800. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  7801. $s = $this->UTF8ToLatin1($s);
  7802. } else {
  7803. //Convert string to UTF-16BE and reverse RTL language
  7804. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  7805. }
  7806. }
  7807. return $this->_escape($s);
  7808. }
  7809. /**
  7810. * Format output stream.
  7811. * @param string $s string to output.
  7812. * @param int $n object reference for encryption mode
  7813. * @access protected
  7814. */
  7815. protected function _getstream($s, $n=0) {
  7816. if ($this->encrypted) {
  7817. if ($n <= 0) {
  7818. // default to current object
  7819. $n = $this->n;
  7820. }
  7821. $s = $this->_RC4($this->_objectkey($n), $s);
  7822. }
  7823. return "stream\n".$s."\nendstream";
  7824. }
  7825. /**
  7826. * Output a stream.
  7827. * @param string $s string to output.
  7828. * @param int $n object reference for encryption mode
  7829. * @access protected
  7830. */
  7831. protected function _putstream($s, $n=0) {
  7832. $this->_out($this->_getstream($s, $n));
  7833. }
  7834. /**
  7835. * Output a string to the document.
  7836. * @param string $s string to output.
  7837. * @access protected
  7838. */
  7839. protected function _out($s) {
  7840. if ($this->state == 2) {
  7841. if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  7842. // puts data before page footer
  7843. $pagebuff = $this->getPageBuffer($this->page);
  7844. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  7845. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  7846. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  7847. // update footer position
  7848. $this->footerpos[$this->page] += strlen($s."\n");
  7849. } else {
  7850. $this->setPageBuffer($this->page, $s."\n", true);
  7851. }
  7852. } else {
  7853. $this->setBuffer($s."\n");
  7854. }
  7855. }
  7856. /**
  7857. * Converts UTF-8 strings to codepoints array.<br>
  7858. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  7859. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  7860. * <pre>
  7861. * Char. number range | UTF-8 octet sequence
  7862. * (hexadecimal) | (binary)
  7863. * --------------------+-----------------------------------------------
  7864. * 0000 0000-0000 007F | 0xxxxxxx
  7865. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  7866. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  7867. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  7868. * ---------------------------------------------------------------------
  7869. *
  7870. * ABFN notation:
  7871. * ---------------------------------------------------------------------
  7872. * UTF8-octets = *( UTF8-char )
  7873. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  7874. * UTF8-1 = %x00-7F
  7875. * UTF8-2 = %xC2-DF UTF8-tail
  7876. *
  7877. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  7878. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  7879. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  7880. * %xF4 %x80-8F 2( UTF8-tail )
  7881. * UTF8-tail = %x80-BF
  7882. * ---------------------------------------------------------------------
  7883. * </pre>
  7884. * @param string $str string to process.
  7885. * @return array containing codepoints (UTF-8 characters values)
  7886. * @access protected
  7887. * @author Nicola Asuni
  7888. * @since 1.53.0.TC005 (2005-01-05)
  7889. */
  7890. protected function UTF8StringToArray($str) {
  7891. if (isset($this->cache_UTF8StringToArray['_'.$str])) {
  7892. // return cached value
  7893. return($this->cache_UTF8StringToArray['_'.$str]);
  7894. }
  7895. // check cache size
  7896. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  7897. // remove first element
  7898. array_shift($this->cache_UTF8StringToArray);
  7899. }
  7900. ++$this->cache_size_UTF8StringToArray;
  7901. if (!$this->isunicode) {
  7902. // split string into array of equivalent codes
  7903. $strarr = array();
  7904. $strlen = strlen($str);
  7905. for ($i=0; $i < $strlen; ++$i) {
  7906. $strarr[] = ord($str{$i});
  7907. }
  7908. // insert new value on cache
  7909. $this->cache_UTF8StringToArray['_'.$str] = $strarr;
  7910. return $strarr;
  7911. }
  7912. $unicode = array(); // array containing unicode values
  7913. $bytes = array(); // array containing single character byte sequences
  7914. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  7915. $str .= ''; // force $str to be a string
  7916. $length = strlen($str);
  7917. for ($i = 0; $i < $length; ++$i) {
  7918. $char = ord($str{$i}); // get one string character at time
  7919. if (count($bytes) == 0) { // get starting octect
  7920. if ($char <= 0x7F) {
  7921. $unicode[] = $char; // use the character "as is" because is ASCII
  7922. $numbytes = 1;
  7923. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  7924. $bytes[] = ($char - 0xC0) << 0x06;
  7925. $numbytes = 2;
  7926. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  7927. $bytes[] = ($char - 0xE0) << 0x0C;
  7928. $numbytes = 3;
  7929. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  7930. $bytes[] = ($char - 0xF0) << 0x12;
  7931. $numbytes = 4;
  7932. } else {
  7933. // use replacement character for other invalid sequences
  7934. $unicode[] = 0xFFFD;
  7935. $bytes = array();
  7936. $numbytes = 1;
  7937. }
  7938. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  7939. $bytes[] = $char - 0x80;
  7940. if (count($bytes) == $numbytes) {
  7941. // compose UTF-8 bytes to a single unicode value
  7942. $char = $bytes[0];
  7943. for ($j = 1; $j < $numbytes; ++$j) {
  7944. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  7945. }
  7946. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  7947. /* The definition of UTF-8 prohibits encoding character numbers between
  7948. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  7949. encoding form (as surrogate pairs) and do not directly represent
  7950. characters. */
  7951. $unicode[] = 0xFFFD; // use replacement character
  7952. } else {
  7953. $unicode[] = $char; // add char to array
  7954. }
  7955. // reset data for next char
  7956. $bytes = array();
  7957. $numbytes = 1;
  7958. }
  7959. } else {
  7960. // use replacement character for other invalid sequences
  7961. $unicode[] = 0xFFFD;
  7962. $bytes = array();
  7963. $numbytes = 1;
  7964. }
  7965. }
  7966. // insert new value on cache
  7967. $this->cache_UTF8StringToArray['_'.$str] = $unicode;
  7968. return $unicode;
  7969. }
  7970. /**
  7971. * Converts UTF-8 strings to UTF16-BE.<br>
  7972. * @param string $str string to process.
  7973. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  7974. * @return string
  7975. * @access protected
  7976. * @author Nicola Asuni
  7977. * @since 1.53.0.TC005 (2005-01-05)
  7978. * @uses UTF8StringToArray(), arrUTF8ToUTF16BE()
  7979. */
  7980. protected function UTF8ToUTF16BE($str, $setbom=true) {
  7981. if (!$this->isunicode) {
  7982. return $str; // string is not in unicode
  7983. }
  7984. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  7985. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  7986. }
  7987. /**
  7988. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  7989. * @param string $str string to process.
  7990. * @return string
  7991. * @author Andrew Whitehead, Nicola Asuni
  7992. * @access protected
  7993. * @since 3.2.000 (2008-06-23)
  7994. */
  7995. protected function UTF8ToLatin1($str) {
  7996. global $utf8tolatin;
  7997. if (!$this->isunicode) {
  7998. return $str; // string is not in unicode
  7999. }
  8000. $outstr = ''; // string to be returned
  8001. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  8002. foreach ($unicode as $char) {
  8003. if ($char < 256) {
  8004. $outstr .= chr($char);
  8005. } elseif (array_key_exists($char, $utf8tolatin)) {
  8006. // map from UTF-8
  8007. $outstr .= chr($utf8tolatin[$char]);
  8008. } elseif ($char == 0xFFFD) {
  8009. // skip
  8010. } else {
  8011. $outstr .= '?';
  8012. }
  8013. }
  8014. return $outstr;
  8015. }
  8016. /**
  8017. * Converts UTF-8 characters array to array of Latin1 characters<br>
  8018. * @param array $unicode array containing UTF-8 unicode values
  8019. * @return array
  8020. * @author Nicola Asuni
  8021. * @access protected
  8022. * @since 4.8.023 (2010-01-15)
  8023. */
  8024. protected function UTF8ArrToLatin1($unicode) {
  8025. global $utf8tolatin;
  8026. if ((!$this->isunicode) OR ($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  8027. return $unicode; // string is not in unicode
  8028. }
  8029. $outarr = array(); // array to be returned
  8030. foreach ($unicode as $char) {
  8031. if ($char < 256) {
  8032. $outarr[] = $char;
  8033. } elseif (array_key_exists($char, $utf8tolatin)) {
  8034. // map from UTF-8
  8035. $outarr[] = $utf8tolatin[$char];
  8036. } elseif ($char == 0xFFFD) {
  8037. // skip
  8038. } else {
  8039. $outarr[] = 63; // '?' character
  8040. }
  8041. }
  8042. return $outarr;
  8043. }
  8044. /**
  8045. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  8046. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  8047. * <pre>
  8048. * Encoding UTF-16:
  8049. *
  8050. * Encoding of a single character from an ISO 10646 character value to
  8051. * UTF-16 proceeds as follows. Let U be the character number, no greater
  8052. * than 0x10FFFF.
  8053. *
  8054. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  8055. * terminate.
  8056. *
  8057. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  8058. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  8059. * represented in 20 bits.
  8060. *
  8061. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  8062. * 0xDC00, respectively. These integers each have 10 bits free to
  8063. * encode the character value, for a total of 20 bits.
  8064. *
  8065. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  8066. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  8067. * bits of W2. Terminate.
  8068. *
  8069. * Graphically, steps 2 through 4 look like:
  8070. * U' = yyyyyyyyyyxxxxxxxxxx
  8071. * W1 = 110110yyyyyyyyyy
  8072. * W2 = 110111xxxxxxxxxx
  8073. * </pre>
  8074. * @param array $unicode array containing UTF-8 unicode values
  8075. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  8076. * @return string
  8077. * @access protected
  8078. * @author Nicola Asuni
  8079. * @since 2.1.000 (2008-01-08)
  8080. * @see UTF8ToUTF16BE()
  8081. */
  8082. protected function arrUTF8ToUTF16BE($unicode, $setbom=true) {
  8083. $outstr = ''; // string to be returned
  8084. if ($setbom) {
  8085. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  8086. }
  8087. foreach ($unicode as $char) {
  8088. if ($char == 0xFFFD) {
  8089. $outstr .= "\xFF\xFD"; // replacement character
  8090. } elseif ($char < 0x10000) {
  8091. $outstr .= chr($char >> 0x08);
  8092. $outstr .= chr($char & 0xFF);
  8093. } else {
  8094. $char -= 0x10000;
  8095. $w1 = 0xD800 | ($char >> 0x10);
  8096. $w2 = 0xDC00 | ($char & 0x3FF);
  8097. $outstr .= chr($w1 >> 0x08);
  8098. $outstr .= chr($w1 & 0xFF);
  8099. $outstr .= chr($w2 >> 0x08);
  8100. $outstr .= chr($w2 & 0xFF);
  8101. }
  8102. }
  8103. return $outstr;
  8104. }
  8105. // ====================================================
  8106. /**
  8107. * Set header font.
  8108. * @param array $font font
  8109. * @access public
  8110. * @since 1.1
  8111. */
  8112. public function setHeaderFont($font) {
  8113. $this->header_font = $font;
  8114. }
  8115. /**
  8116. * Get header font.
  8117. * @return array()
  8118. * @access public
  8119. * @since 4.0.012 (2008-07-24)
  8120. */
  8121. public function getHeaderFont() {
  8122. return $this->header_font;
  8123. }
  8124. /**
  8125. * Set footer font.
  8126. * @param array $font font
  8127. * @access public
  8128. * @since 1.1
  8129. */
  8130. public function setFooterFont($font) {
  8131. $this->footer_font = $font;
  8132. }
  8133. /**
  8134. * Get Footer font.
  8135. * @return array()
  8136. * @access public
  8137. * @since 4.0.012 (2008-07-24)
  8138. */
  8139. public function getFooterFont() {
  8140. return $this->footer_font;
  8141. }
  8142. /**
  8143. * Set language array.
  8144. * @param array $language
  8145. * @access public
  8146. * @since 1.1
  8147. */
  8148. public function setLanguageArray($language) {
  8149. $this->l = $language;
  8150. if (isset($this->l['a_meta_dir'])) {
  8151. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  8152. } else {
  8153. $this->rtl = false;
  8154. }
  8155. }
  8156. /**
  8157. * Returns the PDF data.
  8158. * @access public
  8159. */
  8160. public function getPDFData() {
  8161. if ($this->state < 3) {
  8162. $this->Close();
  8163. }
  8164. return $this->buffer;
  8165. }
  8166. /**
  8167. * Output anchor link.
  8168. * @param string $url link URL or internal link (i.e.: &lt;a href="#23"&gt;link to page 23&lt;/a&gt;)
  8169. * @param string $name link name
  8170. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  8171. * @param boolean $firstline if true prints only the first line and return the remaining string.
  8172. * @param array $color array of RGB text color
  8173. * @param string $style font style (U, D, B, I)
  8174. * @param boolean $firstblock if true the string is the starting of a line.
  8175. * @return the number of cells used or the remaining text if $firstline = true;
  8176. * @access public
  8177. */
  8178. public function addHtmlLink($url, $name, $fill=0, $firstline=false, $color='', $style=-1, $firstblock=false) {
  8179. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  8180. // convert url to internal link
  8181. $page = intval(substr($url, 1));
  8182. $url = $this->AddLink();
  8183. $this->SetLink($url, 0, $page);
  8184. }
  8185. // store current settings
  8186. $prevcolor = $this->fgcolor;
  8187. $prevstyle = $this->FontStyle;
  8188. if (empty($color)) {
  8189. $this->SetTextColorArray($this->htmlLinkColorArray);
  8190. } else {
  8191. $this->SetTextColorArray($color);
  8192. }
  8193. if ($style == -1) {
  8194. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  8195. } else {
  8196. $this->SetFont('', $this->FontStyle.$style);
  8197. }
  8198. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  8199. // restore settings
  8200. $this->SetFont('', $prevstyle);
  8201. $this->SetTextColorArray($prevcolor);
  8202. return $ret;
  8203. }
  8204. /**
  8205. * 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).
  8206. * @param string $color html color
  8207. * @return array RGB color or false in case of error.
  8208. * @access public
  8209. */
  8210. public function convertHTMLColorToDec($color='#FFFFFF') {
  8211. global $webcolor;
  8212. $returncolor = false;
  8213. $color = preg_replace('/[\s]*/', '', $color); // remove extra spaces
  8214. $color = strtolower($color);
  8215. if (($dotpos = strpos($color, '.')) !== false) {
  8216. // remove class parent (i.e.: color.red)
  8217. $color = substr($color, ($dotpos + 1));
  8218. }
  8219. if (strlen($color) == 0) {
  8220. return false;
  8221. }
  8222. if (substr($color, 0, 3) == 'rgb') {
  8223. $codes = substr($color, 4);
  8224. $codes = str_replace(')', '', $codes);
  8225. $returncolor = explode(',', $codes, 3);
  8226. return $returncolor;
  8227. }
  8228. if (substr($color, 0, 1) != '#') {
  8229. // decode color name
  8230. if (isset($webcolor[$color])) {
  8231. $color_code = $webcolor[$color];
  8232. } else {
  8233. return false;
  8234. }
  8235. } else {
  8236. $color_code = substr($color, 1);
  8237. }
  8238. switch (strlen($color_code)) {
  8239. case 3: {
  8240. // three-digit hexadecimal representation
  8241. $r = substr($color_code, 0, 1);
  8242. $g = substr($color_code, 1, 1);
  8243. $b = substr($color_code, 2, 1);
  8244. $returncolor['R'] = hexdec($r.$r);
  8245. $returncolor['G'] = hexdec($g.$g);
  8246. $returncolor['B'] = hexdec($b.$b);
  8247. break;
  8248. }
  8249. case 6: {
  8250. // six-digit hexadecimal representation
  8251. $returncolor['R'] = hexdec(substr($color_code, 0, 2));
  8252. $returncolor['G'] = hexdec(substr($color_code, 2, 2));
  8253. $returncolor['B'] = hexdec(substr($color_code, 4, 2));
  8254. break;
  8255. }
  8256. }
  8257. return $returncolor;
  8258. }
  8259. /**
  8260. * Converts pixels to User's Units.
  8261. * @param int $px pixels
  8262. * @return float value in user's unit
  8263. * @access public
  8264. * @see setImageScale(), getImageScale()
  8265. */
  8266. public function pixelsToUnits($px) {
  8267. return ($px / ($this->imgscale * $this->k));
  8268. }
  8269. /**
  8270. * Reverse function for htmlentities.
  8271. * Convert entities in UTF-8.
  8272. * @param $text_to_convert Text to convert.
  8273. * @return string converted
  8274. * @access public
  8275. */
  8276. public function unhtmlentities($text_to_convert) {
  8277. return html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  8278. }
  8279. // ENCRYPTION METHODS ----------------------------------
  8280. // SINCE 2.0.000 (2008-01-02)
  8281. /**
  8282. * Compute encryption key depending on object number where the encrypted data is stored
  8283. * @param int $n object number
  8284. * @access protected
  8285. * @since 2.0.000 (2008-01-02)
  8286. */
  8287. protected function _objectkey($n) {
  8288. return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10);
  8289. }
  8290. /**
  8291. * Put encryption on PDF document.
  8292. * @access protected
  8293. * @since 2.0.000 (2008-01-02)
  8294. */
  8295. protected function _putencryption() {
  8296. if (!$this->encrypted) {
  8297. return;
  8298. }
  8299. $this->_newobj();
  8300. $this->enc_obj_id = $this->n;
  8301. $out = '<< /Filter /Standard /V 1 /R 2';
  8302. $out .= ' /O ('.$this->_escape($this->Ovalue).')';
  8303. $out .= ' /U ('.$this->_escape($this->Uvalue).')';
  8304. $out .= ' /P '.$this->Pvalue;
  8305. $out .= ' >> endobj';
  8306. $this->_out($out);
  8307. }
  8308. /**
  8309. * Returns the input text exrypted using RC4 algorithm and the specified key.
  8310. * RC4 is the standard encryption algorithm used in PDF format
  8311. * @param string $key encryption key
  8312. * @param String $text input text to be encrypted
  8313. * @return String encrypted text
  8314. * @access protected
  8315. * @since 2.0.000 (2008-01-02)
  8316. * @author Klemen Vodopivec
  8317. */
  8318. protected function _RC4($key, $text) {
  8319. if ($this->last_rc4_key != $key) {
  8320. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  8321. $rc4 = range(0, 255);
  8322. $j = 0;
  8323. for ($i = 0; $i < 256; ++$i) {
  8324. $t = $rc4[$i];
  8325. $j = ($j + $t + ord($k{$i})) % 256;
  8326. $rc4[$i] = $rc4[$j];
  8327. $rc4[$j] = $t;
  8328. }
  8329. $this->last_rc4_key = $key;
  8330. $this->last_rc4_key_c = $rc4;
  8331. } else {
  8332. $rc4 = $this->last_rc4_key_c;
  8333. }
  8334. $len = strlen($text);
  8335. $a = 0;
  8336. $b = 0;
  8337. $out = '';
  8338. for ($i = 0; $i < $len; ++$i) {
  8339. $a = ($a + 1) % 256;
  8340. $t = $rc4[$a];
  8341. $b = ($b + $t) % 256;
  8342. $rc4[$a] = $rc4[$b];
  8343. $rc4[$b] = $t;
  8344. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  8345. $out .= chr(ord($text{$i}) ^ $k);
  8346. }
  8347. return $out;
  8348. }
  8349. /**
  8350. * Encrypts a string using MD5 and returns it's value as a binary string.
  8351. * @param string $str input string
  8352. * @return String MD5 encrypted binary string
  8353. * @access protected
  8354. * @since 2.0.000 (2008-01-02)
  8355. * @author Klemen Vodopivec
  8356. */
  8357. protected function _md5_16($str) {
  8358. return pack('H*', md5($str));
  8359. }
  8360. /**
  8361. * Compute O value (used for RC4 encryption)
  8362. * @param String $user_pass user password
  8363. * @param String $owner_pass user password
  8364. * @return String O value
  8365. * @access protected
  8366. * @since 2.0.000 (2008-01-02)
  8367. * @author Klemen Vodopivec
  8368. */
  8369. protected function _Ovalue($user_pass, $owner_pass) {
  8370. $tmp = $this->_md5_16($owner_pass);
  8371. $owner_RC4_key = substr($tmp, 0, 5);
  8372. return $this->_RC4($owner_RC4_key, $user_pass);
  8373. }
  8374. /**
  8375. * Compute U value (used for RC4 encryption)
  8376. * @return String U value
  8377. * @access protected
  8378. * @since 2.0.000 (2008-01-02)
  8379. * @author Klemen Vodopivec
  8380. */
  8381. protected function _Uvalue() {
  8382. return $this->_RC4($this->encryption_key, $this->padding);
  8383. }
  8384. /**
  8385. * Compute encryption key
  8386. * @param String $user_pass user password
  8387. * @param String $owner_pass user password
  8388. * @param String $protection protection type
  8389. * @access protected
  8390. * @since 2.0.000 (2008-01-02)
  8391. * @author Klemen Vodopivec
  8392. */
  8393. protected function _generateencryptionkey($user_pass, $owner_pass, $protection) {
  8394. // Pad passwords
  8395. $user_pass = substr($user_pass.$this->padding, 0, 32);
  8396. $owner_pass = substr($owner_pass.$this->padding, 0, 32);
  8397. // Compute O value
  8398. $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass);
  8399. // Compute encyption key
  8400. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  8401. $this->encryption_key = substr($tmp, 0, 5);
  8402. // Compute U value
  8403. $this->Uvalue = $this->_Uvalue();
  8404. // Compute P value
  8405. $this->Pvalue = -(($protection^255) + 1);
  8406. }
  8407. /**
  8408. * Set document protection
  8409. * The permission array is composed of values taken from the following ones:
  8410. * - copy: copy text and images to the clipboard
  8411. * - print: print the document
  8412. * - modify: modify it (except for annotations and forms)
  8413. * - annot-forms: add annotations and forms
  8414. * Remark: the protection against modification is for people who have the full Acrobat product.
  8415. * 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.
  8416. * 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.
  8417. * @param Array $permissions the set of permissions. Empty by default (only viewing is allowed). (print, modify, copy, annot-forms)
  8418. * @param String $user_pass user password. Empty by default.
  8419. * @param String $owner_pass owner password. If not specified, a random value is used.
  8420. * @access public
  8421. * @since 2.0.000 (2008-01-02)
  8422. * @author Klemen Vodopivec
  8423. */
  8424. public function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) {
  8425. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
  8426. $protection = 192;
  8427. foreach ($permissions as $permission) {
  8428. if (!isset($options[$permission])) {
  8429. $this->Error('Incorrect permission: '.$permission);
  8430. }
  8431. $protection += $options[$permission];
  8432. }
  8433. if ($owner_pass === null) {
  8434. $owner_pass = uniqid(rand());
  8435. }
  8436. $this->encrypted = true;
  8437. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  8438. }
  8439. // END OF ENCRYPTION FUNCTIONS -------------------------
  8440. // START TRANSFORMATIONS SECTION -----------------------
  8441. /**
  8442. * Starts a 2D tranformation saving current graphic state.
  8443. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  8444. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  8445. * @access public
  8446. * @since 2.1.000 (2008-01-07)
  8447. * @see StartTransform(), StopTransform()
  8448. */
  8449. public function StartTransform() {
  8450. $this->_out('q');
  8451. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  8452. ++$this->transfmatrix_key;
  8453. $this->transfmatrix[$this->transfmatrix_key] = array();
  8454. }
  8455. /**
  8456. * Stops a 2D tranformation restoring previous graphic state.
  8457. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  8458. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  8459. * @access public
  8460. * @since 2.1.000 (2008-01-07)
  8461. * @see StartTransform(), StopTransform()
  8462. */
  8463. public function StopTransform() {
  8464. $this->_out('Q');
  8465. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  8466. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  8467. --$this->transfmatrix_key;
  8468. }
  8469. array_pop($this->transfmrk[$this->page]);
  8470. }
  8471. /**
  8472. * Horizontal Scaling.
  8473. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  8474. * @param int $x abscissa of the scaling center. Default is current x position
  8475. * @param int $y ordinate of the scaling center. Default is current y position
  8476. * @access public
  8477. * @since 2.1.000 (2008-01-07)
  8478. * @see StartTransform(), StopTransform()
  8479. */
  8480. public function ScaleX($s_x, $x='', $y='') {
  8481. $this->Scale($s_x, 100, $x, $y);
  8482. }
  8483. /**
  8484. * Vertical Scaling.
  8485. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  8486. * @param int $x abscissa of the scaling center. Default is current x position
  8487. * @param int $y ordinate of the scaling center. Default is current y position
  8488. * @access public
  8489. * @since 2.1.000 (2008-01-07)
  8490. * @see StartTransform(), StopTransform()
  8491. */
  8492. public function ScaleY($s_y, $x='', $y='') {
  8493. $this->Scale(100, $s_y, $x, $y);
  8494. }
  8495. /**
  8496. * Vertical and horizontal proportional Scaling.
  8497. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  8498. * @param int $x abscissa of the scaling center. Default is current x position
  8499. * @param int $y ordinate of the scaling center. Default is current y position
  8500. * @access public
  8501. * @since 2.1.000 (2008-01-07)
  8502. * @see StartTransform(), StopTransform()
  8503. */
  8504. public function ScaleXY($s, $x='', $y='') {
  8505. $this->Scale($s, $s, $x, $y);
  8506. }
  8507. /**
  8508. * Vertical and horizontal non-proportional Scaling.
  8509. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  8510. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  8511. * @param int $x abscissa of the scaling center. Default is current x position
  8512. * @param int $y ordinate of the scaling center. Default is current y position
  8513. * @access public
  8514. * @since 2.1.000 (2008-01-07)
  8515. * @see StartTransform(), StopTransform()
  8516. */
  8517. public function Scale($s_x, $s_y, $x='', $y='') {
  8518. if ($x === '') {
  8519. $x = $this->x;
  8520. }
  8521. if ($y === '') {
  8522. $y = $this->y;
  8523. }
  8524. if (($s_x == 0) OR ($s_y == 0)) {
  8525. $this->Error('Please do not use values equal to zero for scaling');
  8526. }
  8527. $y = ($this->h - $y) * $this->k;
  8528. $x *= $this->k;
  8529. //calculate elements of transformation matrix
  8530. $s_x /= 100;
  8531. $s_y /= 100;
  8532. $tm[0] = $s_x;
  8533. $tm[1] = 0;
  8534. $tm[2] = 0;
  8535. $tm[3] = $s_y;
  8536. $tm[4] = $x * (1 - $s_x);
  8537. $tm[5] = $y * (1 - $s_y);
  8538. //scale the coordinate system
  8539. $this->Transform($tm);
  8540. }
  8541. /**
  8542. * Horizontal Mirroring.
  8543. * @param int $x abscissa of the point. Default is current x position
  8544. * @access public
  8545. * @since 2.1.000 (2008-01-07)
  8546. * @see StartTransform(), StopTransform()
  8547. */
  8548. public function MirrorH($x='') {
  8549. $this->Scale(-100, 100, $x);
  8550. }
  8551. /**
  8552. * Verical Mirroring.
  8553. * @param int $y ordinate of the point. Default is current y position
  8554. * @access public
  8555. * @since 2.1.000 (2008-01-07)
  8556. * @see StartTransform(), StopTransform()
  8557. */
  8558. public function MirrorV($y='') {
  8559. $this->Scale(100, -100, '', $y);
  8560. }
  8561. /**
  8562. * Point reflection mirroring.
  8563. * @param int $x abscissa of the point. Default is current x position
  8564. * @param int $y ordinate of the point. Default is current y position
  8565. * @access public
  8566. * @since 2.1.000 (2008-01-07)
  8567. * @see StartTransform(), StopTransform()
  8568. */
  8569. public function MirrorP($x='',$y='') {
  8570. $this->Scale(-100, -100, $x, $y);
  8571. }
  8572. /**
  8573. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  8574. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  8575. * @param int $x abscissa of the point. Default is current x position
  8576. * @param int $y ordinate of the point. Default is current y position
  8577. * @access public
  8578. * @since 2.1.000 (2008-01-07)
  8579. * @see StartTransform(), StopTransform()
  8580. */
  8581. public function MirrorL($angle=0, $x='',$y='') {
  8582. $this->Scale(-100, 100, $x, $y);
  8583. $this->Rotate(-2*($angle-90), $x, $y);
  8584. }
  8585. /**
  8586. * Translate graphic object horizontally.
  8587. * @param int $t_x movement to the right (or left for RTL)
  8588. * @access public
  8589. * @since 2.1.000 (2008-01-07)
  8590. * @see StartTransform(), StopTransform()
  8591. */
  8592. public function TranslateX($t_x) {
  8593. $this->Translate($t_x, 0);
  8594. }
  8595. /**
  8596. * Translate graphic object vertically.
  8597. * @param int $t_y movement to the bottom
  8598. * @access public
  8599. * @since 2.1.000 (2008-01-07)
  8600. * @see StartTransform(), StopTransform()
  8601. */
  8602. public function TranslateY($t_y) {
  8603. $this->Translate(0, $t_y);
  8604. }
  8605. /**
  8606. * Translate graphic object horizontally and vertically.
  8607. * @param int $t_x movement to the right
  8608. * @param int $t_y movement to the bottom
  8609. * @access public
  8610. * @since 2.1.000 (2008-01-07)
  8611. * @see StartTransform(), StopTransform()
  8612. */
  8613. public function Translate($t_x, $t_y) {
  8614. //calculate elements of transformation matrix
  8615. $tm[0] = 1;
  8616. $tm[1] = 0;
  8617. $tm[2] = 0;
  8618. $tm[3] = 1;
  8619. $tm[4] = $t_x * $this->k;
  8620. $tm[5] = -$t_y * $this->k;
  8621. //translate the coordinate system
  8622. $this->Transform($tm);
  8623. }
  8624. /**
  8625. * Rotate object.
  8626. * @param float $angle angle in degrees for counter-clockwise rotation
  8627. * @param int $x abscissa of the rotation center. Default is current x position
  8628. * @param int $y ordinate of the rotation center. Default is current y position
  8629. * @access public
  8630. * @since 2.1.000 (2008-01-07)
  8631. * @see StartTransform(), StopTransform()
  8632. */
  8633. public function Rotate($angle, $x='', $y='') {
  8634. if ($x === '') {
  8635. $x = $this->x;
  8636. }
  8637. if ($y === '') {
  8638. $y = $this->y;
  8639. }
  8640. $y = ($this->h - $y) * $this->k;
  8641. $x *= $this->k;
  8642. //calculate elements of transformation matrix
  8643. $tm[0] = cos(deg2rad($angle));
  8644. $tm[1] = sin(deg2rad($angle));
  8645. $tm[2] = -$tm[1];
  8646. $tm[3] = $tm[0];
  8647. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  8648. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  8649. //rotate the coordinate system around ($x,$y)
  8650. $this->Transform($tm);
  8651. }
  8652. /**
  8653. * Skew horizontally.
  8654. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  8655. * @param int $x abscissa of the skewing center. default is current x position
  8656. * @param int $y ordinate of the skewing center. default is current y position
  8657. * @access public
  8658. * @since 2.1.000 (2008-01-07)
  8659. * @see StartTransform(), StopTransform()
  8660. */
  8661. public function SkewX($angle_x, $x='', $y='') {
  8662. $this->Skew($angle_x, 0, $x, $y);
  8663. }
  8664. /**
  8665. * Skew vertically.
  8666. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  8667. * @param int $x abscissa of the skewing center. default is current x position
  8668. * @param int $y ordinate of the skewing center. default is current y position
  8669. * @access public
  8670. * @since 2.1.000 (2008-01-07)
  8671. * @see StartTransform(), StopTransform()
  8672. */
  8673. public function SkewY($angle_y, $x='', $y='') {
  8674. $this->Skew(0, $angle_y, $x, $y);
  8675. }
  8676. /**
  8677. * Skew.
  8678. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  8679. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  8680. * @param int $x abscissa of the skewing center. default is current x position
  8681. * @param int $y ordinate of the skewing center. default is current y position
  8682. * @access public
  8683. * @since 2.1.000 (2008-01-07)
  8684. * @see StartTransform(), StopTransform()
  8685. */
  8686. public function Skew($angle_x, $angle_y, $x='', $y='') {
  8687. if ($x === '') {
  8688. $x = $this->x;
  8689. }
  8690. if ($y === '') {
  8691. $y = $this->y;
  8692. }
  8693. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  8694. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  8695. }
  8696. $x *= $this->k;
  8697. $y = ($this->h - $y) * $this->k;
  8698. //calculate elements of transformation matrix
  8699. $tm[0] = 1;
  8700. $tm[1] = tan(deg2rad($angle_y));
  8701. $tm[2] = tan(deg2rad($angle_x));
  8702. $tm[3] = 1;
  8703. $tm[4] = -$tm[2] * $y;
  8704. $tm[5] = -$tm[1] * $x;
  8705. //skew the coordinate system
  8706. $this->Transform($tm);
  8707. }
  8708. /**
  8709. * Apply graphic transformations.
  8710. * @param array $tm transformation matrix
  8711. * @access protected
  8712. * @since 2.1.000 (2008-01-07)
  8713. * @see StartTransform(), StopTransform()
  8714. */
  8715. protected function Transform($tm) {
  8716. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  8717. // add tranformation matrix
  8718. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  8719. // update tranformation mark
  8720. if (end($this->transfmrk[$this->page]) !== false) {
  8721. $key = key($this->transfmrk[$this->page]);
  8722. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  8723. }
  8724. }
  8725. // END TRANSFORMATIONS SECTION -------------------------
  8726. // START GRAPHIC FUNCTIONS SECTION ---------------------
  8727. // The following section is based on the code provided by David Hernandez Sanz
  8728. /**
  8729. * 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.
  8730. * @param float $width The width.
  8731. * @access public
  8732. * @since 1.0
  8733. * @see Line(), Rect(), Cell(), MultiCell()
  8734. */
  8735. public function SetLineWidth($width) {
  8736. //Set line width
  8737. $this->LineWidth = $width;
  8738. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  8739. if ($this->page > 0) {
  8740. $this->_out($this->linestyleWidth);
  8741. }
  8742. }
  8743. /**
  8744. * Returns the current the line width.
  8745. * @return int Line width
  8746. * @access public
  8747. * @since 2.1.000 (2008-01-07)
  8748. * @see Line(), SetLineWidth()
  8749. */
  8750. public function GetLineWidth() {
  8751. return $this->LineWidth;
  8752. }
  8753. /**
  8754. * Set line style.
  8755. * @param array $style Line style. Array with keys among the following:
  8756. * <ul>
  8757. * <li>width (float): Width of the line in user units.</li>
  8758. * <li>cap (string): Type of cap to put on the line. Possible values are:
  8759. * butt, round, square. The difference between "square" and "butt" is that
  8760. * "square" projects a flat end past the end of the line.</li>
  8761. * <li>join (string): Type of join. Possible values are: miter, round,
  8762. * bevel.</li>
  8763. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  8764. * series of length values, which are the lengths of the on and off dashes.
  8765. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  8766. * 1 off, 2 on, 1 off, ...</li>
  8767. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  8768. * the point at which the pattern starts.</li>
  8769. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  8770. * </ul>
  8771. * @access public
  8772. * @since 2.1.000 (2008-01-08)
  8773. */
  8774. public function SetLineStyle($style) {
  8775. if (!is_array($style)) {
  8776. return;
  8777. }
  8778. extract($style);
  8779. if (isset($width)) {
  8780. $width_prev = $this->LineWidth;
  8781. $this->SetLineWidth($width);
  8782. $this->LineWidth = $width_prev;
  8783. }
  8784. if (isset($cap)) {
  8785. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  8786. if (isset($ca[$cap])) {
  8787. $this->linestyleCap = $ca[$cap].' J';
  8788. $this->_out($this->linestyleCap);
  8789. }
  8790. }
  8791. if (isset($join)) {
  8792. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  8793. if (isset($ja[$join])) {
  8794. $this->linestyleJoin = $ja[$join].' j';
  8795. $this->_out($this->linestyleJoin);
  8796. }
  8797. }
  8798. if (isset($dash)) {
  8799. $dash_string = '';
  8800. if ($dash) {
  8801. if (preg_match('/^.+,/', $dash) > 0) {
  8802. $tab = explode(',', $dash);
  8803. } else {
  8804. $tab = array($dash);
  8805. }
  8806. $dash_string = '';
  8807. foreach ($tab as $i => $v) {
  8808. if ($i) {
  8809. $dash_string .= ' ';
  8810. }
  8811. $dash_string .= sprintf('%.2F', $v);
  8812. }
  8813. }
  8814. if (!isset($phase) OR !$dash) {
  8815. $phase = 0;
  8816. }
  8817. $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $phase);
  8818. $this->_out($this->linestyleDash);
  8819. }
  8820. if (isset($color)) {
  8821. $this->SetDrawColorArray($color);
  8822. }
  8823. }
  8824. /**
  8825. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  8826. * @param float $x Abscissa of point.
  8827. * @param float $y Ordinate of point.
  8828. * @access protected
  8829. * @since 2.1.000 (2008-01-08)
  8830. */
  8831. protected function _outPoint($x, $y) {
  8832. $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
  8833. }
  8834. /**
  8835. * Append a straight line segment from the current point to the point (x, y).
  8836. * The new current point shall be (x, y).
  8837. * @param float $x Abscissa of end point.
  8838. * @param float $y Ordinate of end point.
  8839. * @access protected
  8840. * @since 2.1.000 (2008-01-08)
  8841. */
  8842. protected function _outLine($x, $y) {
  8843. $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
  8844. }
  8845. /**
  8846. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  8847. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  8848. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  8849. * @param float $w Width.
  8850. * @param float $h Height.
  8851. * @param string $op options
  8852. * @access protected
  8853. * @since 2.1.000 (2008-01-08)
  8854. */
  8855. protected function _outRect($x, $y, $w, $h, $op) {
  8856. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  8857. }
  8858. /**
  8859. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bézier control points.
  8860. * The new current point shall be (x3, y3).
  8861. * @param float $x1 Abscissa of control point 1.
  8862. * @param float $y1 Ordinate of control point 1.
  8863. * @param float $x2 Abscissa of control point 2.
  8864. * @param float $y2 Ordinate of control point 2.
  8865. * @param float $x3 Abscissa of end point.
  8866. * @param float $y3 Ordinate of end point.
  8867. * @access protected
  8868. * @since 2.1.000 (2008-01-08)
  8869. */
  8870. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  8871. $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));
  8872. }
  8873. /**
  8874. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the Bézier control points.
  8875. * The new current point shall be (x3, y3).
  8876. * @param float $x2 Abscissa of control point 2.
  8877. * @param float $y2 Ordinate of control point 2.
  8878. * @param float $x3 Abscissa of end point.
  8879. * @param float $y3 Ordinate of end point.
  8880. * @access protected
  8881. * @since 4.9.019 (2010-04-26)
  8882. */
  8883. protected function _outCurveV($x2, $y2, $x3, $y3) {
  8884. $this->_out(sprintf('%.2F %.2F %.2F %.2F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  8885. }
  8886. /**
  8887. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the Bézier control points.
  8888. * The new current point shall be (x3, y3).
  8889. * @param float $x1 Abscissa of control point 1.
  8890. * @param float $y1 Ordinate of control point 1.
  8891. * @param float $x2 Abscissa of control point 2.
  8892. * @param float $y2 Ordinate of control point 2.
  8893. * @param float $x3 Abscissa of end point.
  8894. * @param float $y3 Ordinate of end point.
  8895. * @access protected
  8896. * @since 2.1.000 (2008-01-08)
  8897. */
  8898. protected function _outCurveY($x1, $y1, $x3, $y3) {
  8899. $this->_out(sprintf('%.2F %.2F %.2F %.2F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  8900. }
  8901. /**
  8902. * Draws a line between two points.
  8903. * @param float $x1 Abscissa of first point.
  8904. * @param float $y1 Ordinate of first point.
  8905. * @param float $x2 Abscissa of second point.
  8906. * @param float $y2 Ordinate of second point.
  8907. * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8908. * @access public
  8909. * @since 1.0
  8910. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  8911. */
  8912. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  8913. if (is_array($style)) {
  8914. $this->SetLineStyle($style);
  8915. }
  8916. $this->_outPoint($x1, $y1);
  8917. $this->_outLine($x2, $y2);
  8918. $this->_out('S');
  8919. }
  8920. /**
  8921. * Draws a rectangle.
  8922. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  8923. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  8924. * @param float $w Width.
  8925. * @param float $h Height.
  8926. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  8927. * @param array $border_style Border style of rectangle. Array with keys among the following:
  8928. * <ul>
  8929. * <li>all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8930. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8931. * </ul>
  8932. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  8933. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8934. * @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).
  8935. * @access public
  8936. * @since 1.0
  8937. * @see SetLineStyle()
  8938. */
  8939. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  8940. if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
  8941. $this->SetFillColorArray($fill_color);
  8942. }
  8943. $op = $this->getPathPaintOperator($style);
  8944. if ((!$border_style) OR (isset($border_style['all']))) {
  8945. if (isset($border_style['all']) AND $border_style['all']) {
  8946. $this->SetLineStyle($border_style['all']);
  8947. $border_style = array();
  8948. }
  8949. }
  8950. $this->_outRect($x, $y, $w, $h, $op);
  8951. if ($border_style) {
  8952. $border_style2 = array();
  8953. foreach ($border_style as $line => $value) {
  8954. $length = strlen($line);
  8955. for ($i = 0; $i < $length; ++$i) {
  8956. $border_style2[$line[$i]] = $value;
  8957. }
  8958. }
  8959. $border_style = $border_style2;
  8960. if (isset($border_style['L']) AND $border_style['L']) {
  8961. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  8962. }
  8963. if (isset($border_style['T']) AND $border_style['T']) {
  8964. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  8965. }
  8966. if (isset($border_style['R']) AND $border_style['R']) {
  8967. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  8968. }
  8969. if (isset($border_style['B']) AND $border_style['B']) {
  8970. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  8971. }
  8972. }
  8973. }
  8974. /**
  8975. * Draws a Bezier curve.
  8976. * The Bezier curve is a tangent to the line between the control points at
  8977. * either end of the curve.
  8978. * @param float $x0 Abscissa of start point.
  8979. * @param float $y0 Ordinate of start point.
  8980. * @param float $x1 Abscissa of control point 1.
  8981. * @param float $y1 Ordinate of control point 1.
  8982. * @param float $x2 Abscissa of control point 2.
  8983. * @param float $y2 Ordinate of control point 2.
  8984. * @param float $x3 Abscissa of end point.
  8985. * @param float $y3 Ordinate of end point.
  8986. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  8987. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8988. * @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).
  8989. * @access public
  8990. * @see SetLineStyle()
  8991. * @since 2.1.000 (2008-01-08)
  8992. */
  8993. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  8994. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8995. $this->SetFillColorArray($fill_color);
  8996. }
  8997. $op = $this->getPathPaintOperator($style);
  8998. if ($line_style) {
  8999. $this->SetLineStyle($line_style);
  9000. }
  9001. $this->_outPoint($x0, $y0);
  9002. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  9003. $this->_out($op);
  9004. }
  9005. /**
  9006. * Draws a poly-Bezier curve.
  9007. * Each Bezier curve segment is a tangent to the line between the control points at
  9008. * either end of the curve.
  9009. * @param float $x0 Abscissa of start point.
  9010. * @param float $y0 Ordinate of start point.
  9011. * @param float $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  9012. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9013. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  9014. * @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).
  9015. * @access public
  9016. * @see SetLineStyle()
  9017. * @since 3.0008 (2008-05-12)
  9018. */
  9019. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  9020. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  9021. $this->SetFillColorArray($fill_color);
  9022. }
  9023. $op = $this->getPathPaintOperator($style);
  9024. if ($op == 'f') {
  9025. $line_style = array();
  9026. }
  9027. if ($line_style) {
  9028. $this->SetLineStyle($line_style);
  9029. }
  9030. $this->_outPoint($x0, $y0);
  9031. foreach ($segments as $segment) {
  9032. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  9033. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  9034. }
  9035. $this->_out($op);
  9036. }
  9037. /**
  9038. * Draws an ellipse.
  9039. * An ellipse is formed from n Bezier curves.
  9040. * @param float $x0 Abscissa of center point.
  9041. * @param float $y0 Ordinate of center point.
  9042. * @param float $rx Horizontal radius.
  9043. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  9044. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  9045. * @param float $astart: Angle start of draw line. Default value: 0.
  9046. * @param float $afinish: Angle finish of draw line. Default value: 360.
  9047. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9048. * @param array $line_style Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  9049. * @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).
  9050. * @param integer $nc Number of curves used to draw a 90 degrees portion of ellipse.
  9051. * @author Nicola Asuni
  9052. * @access public
  9053. * @since 2.1.000 (2008-01-08)
  9054. */
  9055. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  9056. if ($this->empty_string($ry) OR ($ry == 0)) {
  9057. $ry = $rx;
  9058. }
  9059. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  9060. $this->SetFillColorArray($fill_color);
  9061. }
  9062. $op = $this->getPathPaintOperator($style);
  9063. if ($op == 'f') {
  9064. $line_style = array();
  9065. }
  9066. if ($line_style) {
  9067. $this->SetLineStyle($line_style);
  9068. }
  9069. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc);
  9070. $this->_out($op);
  9071. }
  9072. /**
  9073. * Append an elliptical arc to the current path.
  9074. * An ellipse is formed from n Bezier curves.
  9075. * @param float $xc Abscissa of center point.
  9076. * @param float $yc Ordinate of center point.
  9077. * @param float $rx Horizontal radius.
  9078. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  9079. * @param float $xang: Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  9080. * @param float $angs: Angle start of draw line. Default value: 0.
  9081. * @param float $angf: Angle finish of draw line. Default value: 360.
  9082. * @param boolean $pie if true do not mark the border point (used to draw pie sectors).
  9083. * @param integer $nc Number of curves used to draw a 90 degrees portion of ellipse.
  9084. * @author Nicola Asuni
  9085. * @access protected
  9086. * @since 4.9.019 (2010-04-26)
  9087. */
  9088. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2) {
  9089. $k = $this->k;
  9090. if ($nc < 2) {
  9091. $nc = 2;
  9092. }
  9093. if ($pie) {
  9094. // center of the arc
  9095. $this->_outPoint($xc, $yc);
  9096. }
  9097. $xang = deg2rad((float) $xang);
  9098. $angs = deg2rad((float) $angs);
  9099. $angf = deg2rad((float) $angf);
  9100. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  9101. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  9102. if ($as < 0) {
  9103. $as += (2 * M_PI);
  9104. }
  9105. if ($af < 0) {
  9106. $af += (2 * M_PI);
  9107. }
  9108. if ($as > $af) {
  9109. // reverse rotation go clockwise
  9110. $as -= (2 * M_PI);
  9111. }
  9112. $total_angle = ($af - $as);
  9113. if ($nc < 2) {
  9114. $nc = 2;
  9115. }
  9116. // total arcs to draw
  9117. $nc *= (2 * abs($total_angle) / M_PI);
  9118. $nc = round($nc) + 1;
  9119. // angle of each arc
  9120. $arcang = $total_angle / $nc;
  9121. // center point in PDF coordiantes
  9122. $x0 = $xc;
  9123. $y0 = ($this->h - $yc);
  9124. // starting angle
  9125. $ang = $as;
  9126. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  9127. $cos_xang = cos($xang);
  9128. $sin_xang = sin($xang);
  9129. $cos_ang = cos($ang);
  9130. $sin_ang = sin($ang);
  9131. // first arc point
  9132. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  9133. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  9134. // first Bezier control point
  9135. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  9136. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  9137. if ($pie) {
  9138. $this->_outLine($px1, $this->h - $py1);
  9139. } else {
  9140. $this->_outPoint($px1, $this->h - $py1);
  9141. }
  9142. // draw arcs
  9143. for ($i = 1; $i <= $nc; ++$i) {
  9144. // starting angle
  9145. $ang = $as + ($i * $arcang);
  9146. $cos_xang = cos($xang);
  9147. $sin_xang = sin($xang);
  9148. $cos_ang = cos($ang);
  9149. $sin_ang = sin($ang);
  9150. // second arc point
  9151. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  9152. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  9153. // second Bezier control point
  9154. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  9155. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  9156. // draw arc
  9157. $this->_outCurve(($px1 + $qx1), ($this->h - ($py1 + $qy1)), ($px2 - $qx2), ($this->h - ($py2 - $qy2)), $px2, ($this->h - $py2));
  9158. // move to next point
  9159. $px1 = $px2;
  9160. $py1 = $py2;
  9161. $qx1 = $qx2;
  9162. $qy1 = $qy2;
  9163. }
  9164. if ($pie) {
  9165. $this->_outLine($xc, $yc);
  9166. }
  9167. }
  9168. /**
  9169. * Draws a circle.
  9170. * A circle is formed from n Bezier curves.
  9171. * @param float $x0 Abscissa of center point.
  9172. * @param float $y0 Ordinate of center point.
  9173. * @param float $r Radius.
  9174. * @param float $angstr: Angle start of draw line. Default value: 0.
  9175. * @param float $angend: Angle finish of draw line. Default value: 360.
  9176. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9177. * @param array $line_style Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  9178. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  9179. * @param integer $nc Number of curves used to draw a 90 degrees portion of circle.
  9180. * @access public
  9181. * @since 2.1.000 (2008-01-08)
  9182. */
  9183. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  9184. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  9185. }
  9186. /**
  9187. * Draws a polygonal line
  9188. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  9189. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9190. * @param array $line_style Line style of polygon. Array with keys among the following:
  9191. * <ul>
  9192. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9193. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9194. * </ul>
  9195. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  9196. * @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).
  9197. * @param boolean $closed if true the polygon is closes, otherwise will remain open
  9198. * @access public
  9199. * @since 4.8.003 (2009-09-15)
  9200. */
  9201. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  9202. $this->Polygon($p, $style, $line_style, $fill_color, false);
  9203. }
  9204. /**
  9205. * Draws a polygon.
  9206. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  9207. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9208. * @param array $line_style Line style of polygon. Array with keys among the following:
  9209. * <ul>
  9210. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9211. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9212. * </ul>
  9213. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  9214. * @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).
  9215. * @param boolean $closed if true the polygon is closes, otherwise will remain open
  9216. * @access public
  9217. * @since 2.1.000 (2008-01-08)
  9218. */
  9219. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  9220. $nc = count($p); // number of coordinates
  9221. $np = $nc / 2; // number of points
  9222. if ($closed) {
  9223. // close polygon by adding the first 2 points at the end (one line)
  9224. for ($i = 0; $i < 4; ++$i) {
  9225. $p[$nc + $i] = $p[$i];
  9226. }
  9227. // copy style for the last added line
  9228. if (isset($line_style[0])) {
  9229. $line_style[$np] = $line_style[0];
  9230. }
  9231. $nc += 4;
  9232. }
  9233. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  9234. $this->SetFillColorArray($fill_color);
  9235. }
  9236. $op = $this->getPathPaintOperator($style);
  9237. if ($op == 'f') {
  9238. $line_style = array();
  9239. }
  9240. $draw = true;
  9241. if ($line_style) {
  9242. if (isset($line_style['all'])) {
  9243. $this->SetLineStyle($line_style['all']);
  9244. } else {
  9245. $draw = false;
  9246. if ($op == 'B') {
  9247. // draw fill
  9248. $op = 'f';
  9249. $this->_outPoint($p[0], $p[1]);
  9250. for ($i = 2; $i < $nc; $i = $i + 2) {
  9251. $this->_outLine($p[$i], $p[$i + 1]);
  9252. }
  9253. $this->_out($op);
  9254. }
  9255. // draw outline
  9256. $this->_outPoint($p[0], $p[1]);
  9257. for ($i = 2; $i < $nc; $i = $i + 2) {
  9258. $line_num = ($i / 2) - 1;
  9259. if (isset($line_style[$line_num])) {
  9260. if ($line_style[$line_num] != 0) {
  9261. if (is_array($line_style[$line_num])) {
  9262. $this->_out('S');
  9263. $this->SetLineStyle($line_style[$line_num]);
  9264. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  9265. $this->_outLine($p[$i], $p[$i + 1]);
  9266. $this->_out('S');
  9267. $this->_outPoint($p[$i], $p[$i + 1]);
  9268. } else {
  9269. $this->_outLine($p[$i], $p[$i + 1]);
  9270. }
  9271. }
  9272. } else {
  9273. $this->_outLine($p[$i], $p[$i + 1]);
  9274. }
  9275. }
  9276. $this->_out($op);
  9277. }
  9278. }
  9279. if ($draw) {
  9280. $this->_outPoint($p[0], $p[1]);
  9281. for ($i = 2; $i < $nc; $i = $i + 2) {
  9282. $this->_outLine($p[$i], $p[$i + 1]);
  9283. }
  9284. $this->_out($op);
  9285. }
  9286. }
  9287. /**
  9288. * Draws a regular polygon.
  9289. * @param float $x0 Abscissa of center point.
  9290. * @param float $y0 Ordinate of center point.
  9291. * @param float $r: Radius of inscribed circle.
  9292. * @param integer $ns Number of sides.
  9293. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  9294. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  9295. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9296. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  9297. * <ul>
  9298. * <li>all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9299. * <li>0 to ($ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9300. * </ul>
  9301. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  9302. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  9303. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  9304. * <ul>
  9305. * <li>D or empty string: Draw (default).</li>
  9306. * <li>F: Fill.</li>
  9307. * <li>DF or FD: Draw and fill.</li>
  9308. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  9309. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  9310. * </ul>
  9311. * @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).
  9312. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  9313. * @access public
  9314. * @since 2.1.000 (2008-01-08)
  9315. */
  9316. 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()) {
  9317. if (3 > $ns) {
  9318. $ns = 3;
  9319. }
  9320. if ($draw_circle) {
  9321. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  9322. }
  9323. $p = array();
  9324. for ($i = 0; $i < $ns; ++$i) {
  9325. $a = $angle + ($i * 360 / $ns);
  9326. $a_rad = deg2rad((float) $a);
  9327. $p[] = $x0 + ($r * sin($a_rad));
  9328. $p[] = $y0 + ($r * cos($a_rad));
  9329. }
  9330. $this->Polygon($p, $style, $line_style, $fill_color);
  9331. }
  9332. /**
  9333. * Draws a star polygon
  9334. * @param float $x0 Abscissa of center point.
  9335. * @param float $y0 Ordinate of center point.
  9336. * @param float $r Radius of inscribed circle.
  9337. * @param integer $nv Number of vertices.
  9338. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  9339. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  9340. * @param boolean $draw_circle: Draw inscribed circle or not. Default value is false.
  9341. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9342. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  9343. * <ul>
  9344. * <li>all: Line style of all sides. Array like for
  9345. * {@link SetLineStyle SetLineStyle}.</li>
  9346. * <li>0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  9347. * </ul>
  9348. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  9349. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  9350. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  9351. * <ul>
  9352. * <li>D or empty string: Draw (default).</li>
  9353. * <li>F: Fill.</li>
  9354. * <li>DF or FD: Draw and fill.</li>
  9355. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  9356. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  9357. * </ul>
  9358. * @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).
  9359. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  9360. * @access public
  9361. * @since 2.1.000 (2008-01-08)
  9362. */
  9363. 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()) {
  9364. if ($nv < 2) {
  9365. $nv = 2;
  9366. }
  9367. if ($draw_circle) {
  9368. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  9369. }
  9370. $p2 = array();
  9371. $visited = array();
  9372. for ($i = 0; $i < $nv; ++$i) {
  9373. $a = $angle + ($i * 360 / $nv);
  9374. $a_rad = deg2rad((float) $a);
  9375. $p2[] = $x0 + ($r * sin($a_rad));
  9376. $p2[] = $y0 + ($r * cos($a_rad));
  9377. $visited[] = false;
  9378. }
  9379. $p = array();
  9380. $i = 0;
  9381. do {
  9382. $p[] = $p2[$i * 2];
  9383. $p[] = $p2[($i * 2) + 1];
  9384. $visited[$i] = true;
  9385. $i += $ng;
  9386. $i %= $nv;
  9387. } while (!$visited[$i]);
  9388. $this->Polygon($p, $style, $line_style, $fill_color);
  9389. }
  9390. /**
  9391. * Draws a rounded rectangle.
  9392. * @param float $x Abscissa of upper-left corner.
  9393. * @param float $y Ordinate of upper-left corner.
  9394. * @param float $w Width.
  9395. * @param float $h Height.
  9396. * @param float $r the radius of the circle used to round off the corners of the rectangle.
  9397. * @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").
  9398. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9399. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  9400. * @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).
  9401. * @access public
  9402. * @since 2.1.000 (2008-01-08)
  9403. */
  9404. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  9405. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  9406. }
  9407. /**
  9408. * Draws a rounded rectangle.
  9409. * @param float $x Abscissa of upper-left corner.
  9410. * @param float $y Ordinate of upper-left corner.
  9411. * @param float $w Width.
  9412. * @param float $h Height.
  9413. * @param float $rx the x-axis radius of the ellipse used to round off the corners of the rectangle.
  9414. * @param float $ry the y-axis radius of the ellipse used to round off the corners of the rectangle.
  9415. * @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").
  9416. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  9417. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  9418. * @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).
  9419. * @access public
  9420. * @since 4.9.019 (2010-04-22)
  9421. */
  9422. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  9423. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  9424. // Not rounded
  9425. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  9426. return;
  9427. }
  9428. // Rounded
  9429. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  9430. $this->SetFillColorArray($fill_color);
  9431. }
  9432. $op = $this->getPathPaintOperator($style);
  9433. if ($op == 'f') {
  9434. $border_style = array();
  9435. }
  9436. if ($border_style) {
  9437. $this->SetLineStyle($border_style);
  9438. }
  9439. $MyArc = 4 / 3 * (sqrt(2) - 1);
  9440. $this->_outPoint($x + $rx, $y);
  9441. $xc = $x + $w - $rx;
  9442. $yc = $y + $ry;
  9443. $this->_outLine($xc, $y);
  9444. if ($round_corner[0]) {
  9445. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  9446. } else {
  9447. $this->_outLine($x + $w, $y);
  9448. }
  9449. $xc = $x + $w - $rx;
  9450. $yc = $y + $h - $ry;
  9451. $this->_outLine($x + $w, $yc);
  9452. if ($round_corner[1]) {
  9453. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  9454. } else {
  9455. $this->_outLine($x + $w, $y + $h);
  9456. }
  9457. $xc = $x + $rx;
  9458. $yc = $y + $h - $ry;
  9459. $this->_outLine($xc, $y + $h);
  9460. if ($round_corner[2]) {
  9461. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  9462. } else {
  9463. $this->_outLine($x, $y + $h);
  9464. }
  9465. $xc = $x + $rx;
  9466. $yc = $y + $ry;
  9467. $this->_outLine($x, $yc);
  9468. if ($round_corner[3]) {
  9469. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  9470. } else {
  9471. $this->_outLine($x, $y);
  9472. $this->_outLine($x + $rx, $y);
  9473. }
  9474. $this->_out($op);
  9475. }
  9476. /**
  9477. * Draws a grahic arrow.
  9478. * @parameter float $x0 Abscissa of first point.
  9479. * @parameter float $y0 Ordinate of first point.
  9480. * @parameter float $x0 Abscissa of second point.
  9481. * @parameter float $y1 Ordinate of second point.
  9482. * @parameter int $head_style (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  9483. * @parameter float $arm_size length of arrowhead arms
  9484. * @parameter int $arm_angle angle between an arm and the shaft
  9485. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  9486. * @since 4.6.018 (2009-07-10)
  9487. */
  9488. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  9489. // getting arrow direction angle
  9490. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  9491. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  9492. if ($dir_angle < 0) {
  9493. $dir_angle += (2 * M_PI);
  9494. }
  9495. $arm_angle = deg2rad($arm_angle);
  9496. $sx1 = $x1;
  9497. $sy1 = $y1;
  9498. if ($head_style > 0) {
  9499. // calculate the stopping point for the arrow shaft
  9500. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  9501. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  9502. }
  9503. // main arrow line / shaft
  9504. $this->Line($x0, $y0, $sx1, $sy1);
  9505. // left arrowhead arm tip
  9506. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  9507. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  9508. // right arrowhead arm tip
  9509. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  9510. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  9511. $mode = 'D';
  9512. $style = array();
  9513. switch ($head_style) {
  9514. case 0: {
  9515. // draw only arrowhead arms
  9516. $mode = 'D';
  9517. $style = array(1, 1, 0);
  9518. break;
  9519. }
  9520. case 1: {
  9521. // draw closed arrowhead, but no fill
  9522. $mode = 'D';
  9523. break;
  9524. }
  9525. case 2: {
  9526. // closed and filled arrowhead
  9527. $mode = 'DF';
  9528. break;
  9529. }
  9530. case 3: {
  9531. // filled arrowhead
  9532. $mode = 'F';
  9533. break;
  9534. }
  9535. }
  9536. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  9537. }
  9538. // END GRAPHIC FUNCTIONS SECTION -----------------------
  9539. // BIDIRECTIONAL TEXT SECTION --------------------------
  9540. /**
  9541. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  9542. * @param string $str string to manipulate.
  9543. * @param bool $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  9544. * @param bool $forcertl if true forces RTL text direction
  9545. * @return string
  9546. * @access protected
  9547. * @author Nicola Asuni
  9548. * @since 2.1.000 (2008-01-08)
  9549. */
  9550. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  9551. return $this->utf8StrArrRev($this->UTF8StringToArray($str), $str, $setbom, $forcertl);
  9552. }
  9553. /**
  9554. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  9555. * @param array $arr array of unicode values.
  9556. * @param string $str string to manipulate (or empty value).
  9557. * @param bool $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  9558. * @param bool $forcertl if true forces RTL text direction
  9559. * @return string
  9560. * @access protected
  9561. * @author Nicola Asuni
  9562. * @since 4.9.000 (2010-03-27)
  9563. */
  9564. protected function utf8StrArrRev($arr, $str='', $setbom=false, $forcertl=false) {
  9565. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($arr, $str, $forcertl), $setbom);
  9566. }
  9567. /**
  9568. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  9569. * @param array $ta array of characters composing the string.
  9570. * @param string $str string to process
  9571. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  9572. * @return array of unicode chars
  9573. * @author Nicola Asuni
  9574. * @access protected
  9575. * @since 2.4.000 (2008-03-06)
  9576. */
  9577. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  9578. global $unicode, $unicode_mirror, $unicode_arlet, $laa_array, $diacritics;
  9579. // paragraph embedding level
  9580. $pel = 0;
  9581. // max level
  9582. $maxlevel = 0;
  9583. if ($this->empty_string($str)) {
  9584. // create string from array
  9585. $str = $this->UTF8ArrSubString($ta);
  9586. }
  9587. // check if string contains arabic text
  9588. if (preg_match(K_RE_PATTERN_ARABIC, $str)) {
  9589. $arabic = true;
  9590. } else {
  9591. $arabic = false;
  9592. }
  9593. // check if string contains RTL text
  9594. if (!($forcertl OR $arabic OR preg_match(K_RE_PATTERN_RTL, $str))) {
  9595. return $ta;
  9596. }
  9597. // get number of chars
  9598. $numchars = count($ta);
  9599. if ($forcertl == 'R') {
  9600. $pel = 1;
  9601. } elseif ($forcertl == 'L') {
  9602. $pel = 0;
  9603. } else {
  9604. // P2. In each paragraph, find the first character of type L, AL, or R.
  9605. // 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.
  9606. for ($i=0; $i < $numchars; ++$i) {
  9607. $type = $unicode[$ta[$i]];
  9608. if ($type == 'L') {
  9609. $pel = 0;
  9610. break;
  9611. } elseif (($type == 'AL') OR ($type == 'R')) {
  9612. $pel = 1;
  9613. break;
  9614. }
  9615. }
  9616. }
  9617. // Current Embedding Level
  9618. $cel = $pel;
  9619. // directional override status
  9620. $dos = 'N';
  9621. $remember = array();
  9622. // start-of-level-run
  9623. $sor = $pel % 2 ? 'R' : 'L';
  9624. $eor = $sor;
  9625. // Array of characters data
  9626. $chardata = Array();
  9627. // 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.
  9628. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  9629. for ($i=0; $i < $numchars; ++$i) {
  9630. if ($ta[$i] == K_RLE) {
  9631. // X2. With each RLE, compute the least greater odd embedding level.
  9632. // 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.
  9633. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  9634. $next_level = $cel + ($cel % 2) + 1;
  9635. if ($next_level < 62) {
  9636. $remember[] = array('num' => K_RLE, 'cel' => $cel, 'dos' => $dos);
  9637. $cel = $next_level;
  9638. $dos = 'N';
  9639. $sor = $eor;
  9640. $eor = $cel % 2 ? 'R' : 'L';
  9641. }
  9642. } elseif ($ta[$i] == K_LRE) {
  9643. // X3. With each LRE, compute the least greater even embedding level.
  9644. // 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.
  9645. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  9646. $next_level = $cel + 2 - ($cel % 2);
  9647. if ( $next_level < 62 ) {
  9648. $remember[] = array('num' => K_LRE, 'cel' => $cel, 'dos' => $dos);
  9649. $cel = $next_level;
  9650. $dos = 'N';
  9651. $sor = $eor;
  9652. $eor = $cel % 2 ? 'R' : 'L';
  9653. }
  9654. } elseif ($ta[$i] == K_RLO) {
  9655. // X4. With each RLO, compute the least greater odd embedding level.
  9656. // 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.
  9657. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  9658. $next_level = $cel + ($cel % 2) + 1;
  9659. if ($next_level < 62) {
  9660. $remember[] = array('num' => K_RLO, 'cel' => $cel, 'dos' => $dos);
  9661. $cel = $next_level;
  9662. $dos = 'R';
  9663. $sor = $eor;
  9664. $eor = $cel % 2 ? 'R' : 'L';
  9665. }
  9666. } elseif ($ta[$i] == K_LRO) {
  9667. // X5. With each LRO, compute the least greater even embedding level.
  9668. // 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.
  9669. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  9670. $next_level = $cel + 2 - ($cel % 2);
  9671. if ( $next_level < 62 ) {
  9672. $remember[] = array('num' => K_LRO, 'cel' => $cel, 'dos' => $dos);
  9673. $cel = $next_level;
  9674. $dos = 'L';
  9675. $sor = $eor;
  9676. $eor = $cel % 2 ? 'R' : 'L';
  9677. }
  9678. } elseif ($ta[$i] == K_PDF) {
  9679. // 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.
  9680. if (count($remember)) {
  9681. $last = count($remember ) - 1;
  9682. if (($remember[$last]['num'] == K_RLE) OR
  9683. ($remember[$last]['num'] == K_LRE) OR
  9684. ($remember[$last]['num'] == K_RLO) OR
  9685. ($remember[$last]['num'] == K_LRO)) {
  9686. $match = array_pop($remember);
  9687. $cel = $match['cel'];
  9688. $dos = $match['dos'];
  9689. $sor = $eor;
  9690. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  9691. }
  9692. }
  9693. } elseif (($ta[$i] != K_RLE) AND
  9694. ($ta[$i] != K_LRE) AND
  9695. ($ta[$i] != K_RLO) AND
  9696. ($ta[$i] != K_LRO) AND
  9697. ($ta[$i] != K_PDF)) {
  9698. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  9699. // a. Set the level of the current character to the current embedding level.
  9700. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  9701. if ($dos != 'N') {
  9702. $chardir = $dos;
  9703. } else {
  9704. if (isset($unicode[$ta[$i]])) {
  9705. $chardir = $unicode[$ta[$i]];
  9706. } else {
  9707. $chardir = 'L';
  9708. }
  9709. }
  9710. // stores string characters and other information
  9711. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  9712. }
  9713. } // end for each char
  9714. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  9715. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  9716. // 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.
  9717. // 3.3.3 Resolving Weak Types
  9718. // 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.
  9719. // Nonspacing marks are now resolved based on the previous characters.
  9720. $numchars = count($chardata);
  9721. // 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.
  9722. $prevlevel = -1; // track level changes
  9723. $levcount = 0; // counts consecutive chars at the same level
  9724. for ($i=0; $i < $numchars; ++$i) {
  9725. if ($chardata[$i]['type'] == 'NSM') {
  9726. if ($levcount) {
  9727. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9728. } elseif ($i > 0) {
  9729. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  9730. }
  9731. }
  9732. if ($chardata[$i]['level'] != $prevlevel) {
  9733. $levcount = 0;
  9734. } else {
  9735. ++$levcount;
  9736. }
  9737. $prevlevel = $chardata[$i]['level'];
  9738. }
  9739. // 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.
  9740. $prevlevel = -1;
  9741. $levcount = 0;
  9742. for ($i=0; $i < $numchars; ++$i) {
  9743. if ($chardata[$i]['char'] == 'EN') {
  9744. for ($j=$levcount; $j >= 0; $j--) {
  9745. if ($chardata[$j]['type'] == 'AL') {
  9746. $chardata[$i]['type'] = 'AN';
  9747. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  9748. break;
  9749. }
  9750. }
  9751. }
  9752. if ($chardata[$i]['level'] != $prevlevel) {
  9753. $levcount = 0;
  9754. } else {
  9755. ++$levcount;
  9756. }
  9757. $prevlevel = $chardata[$i]['level'];
  9758. }
  9759. // W3. Change all ALs to R.
  9760. for ($i=0; $i < $numchars; ++$i) {
  9761. if ($chardata[$i]['type'] == 'AL') {
  9762. $chardata[$i]['type'] = 'R';
  9763. }
  9764. }
  9765. // 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.
  9766. $prevlevel = -1;
  9767. $levcount = 0;
  9768. for ($i=0; $i < $numchars; ++$i) {
  9769. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9770. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  9771. $chardata[$i]['type'] = 'EN';
  9772. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  9773. $chardata[$i]['type'] = 'EN';
  9774. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  9775. $chardata[$i]['type'] = 'AN';
  9776. }
  9777. }
  9778. if ($chardata[$i]['level'] != $prevlevel) {
  9779. $levcount = 0;
  9780. } else {
  9781. ++$levcount;
  9782. }
  9783. $prevlevel = $chardata[$i]['level'];
  9784. }
  9785. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  9786. $prevlevel = -1;
  9787. $levcount = 0;
  9788. for ($i=0; $i < $numchars; ++$i) {
  9789. if ($chardata[$i]['type'] == 'ET') {
  9790. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  9791. $chardata[$i]['type'] = 'EN';
  9792. } else {
  9793. $j = $i+1;
  9794. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  9795. if ($chardata[$j]['type'] == 'EN') {
  9796. $chardata[$i]['type'] = 'EN';
  9797. break;
  9798. } elseif ($chardata[$j]['type'] != 'ET') {
  9799. break;
  9800. }
  9801. ++$j;
  9802. }
  9803. }
  9804. }
  9805. if ($chardata[$i]['level'] != $prevlevel) {
  9806. $levcount = 0;
  9807. } else {
  9808. ++$levcount;
  9809. }
  9810. $prevlevel = $chardata[$i]['level'];
  9811. }
  9812. // W6. Otherwise, separators and terminators change to Other Neutral.
  9813. $prevlevel = -1;
  9814. $levcount = 0;
  9815. for ($i=0; $i < $numchars; ++$i) {
  9816. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  9817. $chardata[$i]['type'] = 'ON';
  9818. }
  9819. if ($chardata[$i]['level'] != $prevlevel) {
  9820. $levcount = 0;
  9821. } else {
  9822. ++$levcount;
  9823. }
  9824. $prevlevel = $chardata[$i]['level'];
  9825. }
  9826. //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.
  9827. $prevlevel = -1;
  9828. $levcount = 0;
  9829. for ($i=0; $i < $numchars; ++$i) {
  9830. if ($chardata[$i]['char'] == 'EN') {
  9831. for ($j=$levcount; $j >= 0; $j--) {
  9832. if ($chardata[$j]['type'] == 'L') {
  9833. $chardata[$i]['type'] = 'L';
  9834. } elseif ($chardata[$j]['type'] == 'R') {
  9835. break;
  9836. }
  9837. }
  9838. }
  9839. if ($chardata[$i]['level'] != $prevlevel) {
  9840. $levcount = 0;
  9841. } else {
  9842. ++$levcount;
  9843. }
  9844. $prevlevel = $chardata[$i]['level'];
  9845. }
  9846. // 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.
  9847. $prevlevel = -1;
  9848. $levcount = 0;
  9849. for ($i=0; $i < $numchars; ++$i) {
  9850. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9851. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  9852. $chardata[$i]['type'] = 'L';
  9853. } elseif (($chardata[$i]['type'] == 'N') AND
  9854. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  9855. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  9856. $chardata[$i]['type'] = 'R';
  9857. } elseif ($chardata[$i]['type'] == 'N') {
  9858. // N2. Any remaining neutrals take the embedding direction
  9859. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9860. }
  9861. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  9862. // first char
  9863. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  9864. $chardata[$i]['type'] = 'L';
  9865. } elseif (($chardata[$i]['type'] == 'N') AND
  9866. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  9867. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  9868. $chardata[$i]['type'] = 'R';
  9869. } elseif ($chardata[$i]['type'] == 'N') {
  9870. // N2. Any remaining neutrals take the embedding direction
  9871. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9872. }
  9873. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  9874. //last char
  9875. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  9876. $chardata[$i]['type'] = 'L';
  9877. } elseif (($chardata[$i]['type'] == 'N') AND
  9878. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  9879. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  9880. $chardata[$i]['type'] = 'R';
  9881. } elseif ($chardata[$i]['type'] == 'N') {
  9882. // N2. Any remaining neutrals take the embedding direction
  9883. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9884. }
  9885. } elseif ($chardata[$i]['type'] == 'N') {
  9886. // N2. Any remaining neutrals take the embedding direction
  9887. $chardata[$i]['type'] = $chardata[$i]['sor'];
  9888. }
  9889. if ($chardata[$i]['level'] != $prevlevel) {
  9890. $levcount = 0;
  9891. } else {
  9892. ++$levcount;
  9893. }
  9894. $prevlevel = $chardata[$i]['level'];
  9895. }
  9896. // 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.
  9897. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  9898. for ($i=0; $i < $numchars; ++$i) {
  9899. $odd = $chardata[$i]['level'] % 2;
  9900. if ($odd) {
  9901. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  9902. $chardata[$i]['level'] += 1;
  9903. }
  9904. } else {
  9905. if ($chardata[$i]['type'] == 'R') {
  9906. $chardata[$i]['level'] += 1;
  9907. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  9908. $chardata[$i]['level'] += 2;
  9909. }
  9910. }
  9911. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  9912. }
  9913. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  9914. // 1. Segment separators,
  9915. // 2. Paragraph separators,
  9916. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  9917. // 4. Any sequence of white space characters at the end of the line.
  9918. for ($i=0; $i < $numchars; ++$i) {
  9919. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  9920. $chardata[$i]['level'] = $pel;
  9921. } elseif ($chardata[$i]['type'] == 'WS') {
  9922. $j = $i+1;
  9923. while ($j < $numchars) {
  9924. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  9925. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  9926. $chardata[$i]['level'] = $pel;
  9927. break;
  9928. } elseif ($chardata[$j]['type'] != 'WS') {
  9929. break;
  9930. }
  9931. ++$j;
  9932. }
  9933. }
  9934. }
  9935. // Arabic Shaping
  9936. // 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.
  9937. if ($arabic) {
  9938. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  9939. $alfletter = array(1570,1571,1573,1575);
  9940. $chardata2 = $chardata;
  9941. $laaletter = false;
  9942. $charAL = array();
  9943. $x = 0;
  9944. for ($i=0; $i < $numchars; ++$i) {
  9945. if (($unicode[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  9946. $charAL[$x] = $chardata[$i];
  9947. $charAL[$x]['i'] = $i;
  9948. $chardata[$i]['x'] = $x;
  9949. ++$x;
  9950. }
  9951. }
  9952. $numAL = $x;
  9953. for ($i=0; $i < $numchars; ++$i) {
  9954. $thischar = $chardata[$i];
  9955. if ($i > 0) {
  9956. $prevchar = $chardata[($i-1)];
  9957. } else {
  9958. $prevchar = false;
  9959. }
  9960. if (($i+1) < $numchars) {
  9961. $nextchar = $chardata[($i+1)];
  9962. } else {
  9963. $nextchar = false;
  9964. }
  9965. if ($unicode[$thischar['char']] == 'AL') {
  9966. $x = $thischar['x'];
  9967. if ($x > 0) {
  9968. $prevchar = $charAL[($x-1)];
  9969. } else {
  9970. $prevchar = false;
  9971. }
  9972. if (($x+1) < $numAL) {
  9973. $nextchar = $charAL[($x+1)];
  9974. } else {
  9975. $nextchar = false;
  9976. }
  9977. // if laa letter
  9978. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  9979. $arabicarr = $laa_array;
  9980. $laaletter = true;
  9981. if ($x > 1) {
  9982. $prevchar = $charAL[($x-2)];
  9983. } else {
  9984. $prevchar = false;
  9985. }
  9986. } else {
  9987. $arabicarr = $unicode_arlet;
  9988. $laaletter = false;
  9989. }
  9990. if (($prevchar !== false) AND ($nextchar !== false) AND
  9991. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  9992. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  9993. ($prevchar['type'] == $thischar['type']) AND
  9994. ($nextchar['type'] == $thischar['type']) AND
  9995. ($nextchar['char'] != 1567)) {
  9996. if (in_array($prevchar['char'], $endedletter)) {
  9997. if (isset($arabicarr[$thischar['char']][2])) {
  9998. // initial
  9999. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  10000. }
  10001. } else {
  10002. if (isset($arabicarr[$thischar['char']][3])) {
  10003. // medial
  10004. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  10005. }
  10006. }
  10007. } elseif (($nextchar !== false) AND
  10008. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  10009. ($nextchar['type'] == $thischar['type']) AND
  10010. ($nextchar['char'] != 1567)) {
  10011. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  10012. // initial
  10013. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  10014. }
  10015. } elseif ((($prevchar !== false) AND
  10016. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  10017. ($prevchar['type'] == $thischar['type'])) OR
  10018. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  10019. // final
  10020. if (($i > 1) AND ($thischar['char'] == 1607) AND
  10021. ($chardata[$i-1]['char'] == 1604) AND
  10022. ($chardata[$i-2]['char'] == 1604)) {
  10023. //Allah Word
  10024. // mark characters to delete with false
  10025. $chardata2[$i-2]['char'] = false;
  10026. $chardata2[$i-1]['char'] = false;
  10027. $chardata2[$i]['char'] = 65010;
  10028. } else {
  10029. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  10030. if (isset($arabicarr[$thischar['char']][0])) {
  10031. // isolated
  10032. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  10033. }
  10034. } else {
  10035. if (isset($arabicarr[$thischar['char']][1])) {
  10036. // final
  10037. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  10038. }
  10039. }
  10040. }
  10041. } elseif (isset($arabicarr[$thischar['char']][0])) {
  10042. // isolated
  10043. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  10044. }
  10045. // if laa letter
  10046. if ($laaletter) {
  10047. // mark characters to delete with false
  10048. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  10049. }
  10050. } // end if AL (Arabic Letter)
  10051. } // end for each char
  10052. /*
  10053. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  10054. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  10055. */
  10056. $cw = &$this->CurrentFont['cw'];
  10057. for ($i = 0; $i < ($numchars-1); ++$i) {
  10058. if (($chardata2[$i]['char'] == 1617) AND (isset($diacritics[($chardata2[$i+1]['char'])]))) {
  10059. // check if the subtitution font is defined on current font
  10060. if (isset($cw[($diacritics[($chardata2[$i+1]['char'])])])) {
  10061. $chardata2[$i]['char'] = false;
  10062. $chardata2[$i+1]['char'] = $diacritics[($chardata2[$i+1]['char'])];
  10063. }
  10064. }
  10065. }
  10066. // remove marked characters
  10067. foreach ($chardata2 as $key => $value) {
  10068. if ($value['char'] === false) {
  10069. unset($chardata2[$key]);
  10070. }
  10071. }
  10072. $chardata = array_values($chardata2);
  10073. $numchars = count($chardata);
  10074. unset($chardata2);
  10075. unset($arabicarr);
  10076. unset($laaletter);
  10077. unset($charAL);
  10078. }
  10079. // 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.
  10080. for ($j=$maxlevel; $j > 0; $j--) {
  10081. $ordarray = Array();
  10082. $revarr = Array();
  10083. $onlevel = false;
  10084. for ($i=0; $i < $numchars; ++$i) {
  10085. if ($chardata[$i]['level'] >= $j) {
  10086. $onlevel = true;
  10087. if (isset($unicode_mirror[$chardata[$i]['char']])) {
  10088. // 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.
  10089. $chardata[$i]['char'] = $unicode_mirror[$chardata[$i]['char']];
  10090. }
  10091. $revarr[] = $chardata[$i];
  10092. } else {
  10093. if ($onlevel) {
  10094. $revarr = array_reverse($revarr);
  10095. $ordarray = array_merge($ordarray, $revarr);
  10096. $revarr = Array();
  10097. $onlevel = false;
  10098. }
  10099. $ordarray[] = $chardata[$i];
  10100. }
  10101. }
  10102. if ($onlevel) {
  10103. $revarr = array_reverse($revarr);
  10104. $ordarray = array_merge($ordarray, $revarr);
  10105. }
  10106. $chardata = $ordarray;
  10107. }
  10108. $ordarray = array();
  10109. for ($i=0; $i < $numchars; ++$i) {
  10110. $ordarray[] = $chardata[$i]['char'];
  10111. }
  10112. return $ordarray;
  10113. }
  10114. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  10115. /**
  10116. * Adds a bookmark.
  10117. * @param string $txt bookmark description.
  10118. * @param int $level bookmark level (minimum value is 0).
  10119. * @param float $y Ordinate of the boorkmark position (default = -1 = current position).
  10120. * @param int $page target page number (leave empty for current page).
  10121. * @access public
  10122. * @author Olivier Plathey, Nicola Asuni
  10123. * @since 2.1.002 (2008-02-12)
  10124. */
  10125. public function Bookmark($txt, $level=0, $y=-1, $page='') {
  10126. if ($level < 0) {
  10127. $level = 0;
  10128. }
  10129. if (isset($this->outlines[0])) {
  10130. $lastoutline = end($this->outlines);
  10131. $maxlevel = $lastoutline['l'] + 1;
  10132. } else {
  10133. $maxlevel = 0;
  10134. }
  10135. if ($level > $maxlevel) {
  10136. $level = $maxlevel;
  10137. }
  10138. if ($y == -1) {
  10139. $y = $this->GetY();
  10140. }
  10141. if (empty($page)) {
  10142. $page = $this->PageNo();
  10143. }
  10144. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page);
  10145. }
  10146. /**
  10147. * Create a bookmark PDF string.
  10148. * @access protected
  10149. * @author Olivier Plathey, Nicola Asuni
  10150. * @since 2.1.002 (2008-02-12)
  10151. */
  10152. protected function _putbookmarks() {
  10153. $nb = count($this->outlines);
  10154. if ($nb == 0) {
  10155. return;
  10156. }
  10157. // get sorting columns
  10158. $outline_p = array();
  10159. $outline_y = array();
  10160. foreach ($this->outlines as $key => $row) {
  10161. $outline_p[$key] = $row['p'];
  10162. $outline_k[$key] = $key;
  10163. }
  10164. // sort outlines by page and original position
  10165. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  10166. $lru = array();
  10167. $level = 0;
  10168. foreach ($this->outlines as $i => $o) {
  10169. if ($o['l'] > 0) {
  10170. $parent = $lru[($o['l'] - 1)];
  10171. //Set parent and last pointers
  10172. $this->outlines[$i]['parent'] = $parent;
  10173. $this->outlines[$parent]['last'] = $i;
  10174. if ($o['l'] > $level) {
  10175. //Level increasing: set first pointer
  10176. $this->outlines[$parent]['first'] = $i;
  10177. }
  10178. } else {
  10179. $this->outlines[$i]['parent'] = $nb;
  10180. }
  10181. if (($o['l'] <= $level) AND ($i > 0)) {
  10182. //Set prev and next pointers
  10183. $prev = $lru[$o['l']];
  10184. $this->outlines[$prev]['next'] = $i;
  10185. $this->outlines[$i]['prev'] = $prev;
  10186. }
  10187. $lru[$o['l']] = $i;
  10188. $level = $o['l'];
  10189. }
  10190. //Outline items
  10191. $n = $this->n + 1;
  10192. foreach ($this->outlines as $i => $o) {
  10193. $this->_newobj();
  10194. // covert HTML title to string
  10195. $nltags = '/<br[\s]?\/>|<\/(blockquote|dd|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|p|pre|ul|tcpdf|table|tr|td)>/si';
  10196. $title = preg_replace($nltags, "\n", $o['t']);
  10197. $title = preg_replace("/[\r]+/si", '', $title);
  10198. $title = preg_replace("/[\n]+/si", "\n", $title);
  10199. $title = strip_tags(trim($title));
  10200. $out = '<</Title '.$this->_textstring($title);
  10201. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  10202. if (isset($o['prev'])) {
  10203. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  10204. }
  10205. if (isset($o['next'])) {
  10206. $out .= ' /Next '.($n + $o['next']).' 0 R';
  10207. }
  10208. if (isset($o['first'])) {
  10209. $out .= ' /First '.($n + $o['first']).' 0 R';
  10210. }
  10211. if (isset($o['last'])) {
  10212. $out .= ' /Last '.($n + $o['last']).' 0 R';
  10213. }
  10214. $out .= ' '.sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $o['p'])), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  10215. $out .= ' /Count 0 >> endobj';
  10216. $this->_out($out);
  10217. }
  10218. //Outline root
  10219. $this->_newobj();
  10220. $this->OutlineRoot = $this->n;
  10221. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >> endobj');
  10222. }
  10223. // --- JAVASCRIPT ------------------------------------------------------
  10224. /**
  10225. * Adds a javascript
  10226. * @param string $script Javascript code
  10227. * @access public
  10228. * @author Johannes Güntert, Nicola Asuni
  10229. * @since 2.1.002 (2008-02-12)
  10230. */
  10231. public function IncludeJS($script) {
  10232. $this->javascript .= $script;
  10233. }
  10234. /**
  10235. * Adds a javascript object and return object ID
  10236. * @param string $script Javascript code
  10237. * @param boolean $onload if true executes this object when opening the document
  10238. * @return int internal object ID
  10239. * @access public
  10240. * @author Nicola Asuni
  10241. * @since 4.8.000 (2009-09-07)
  10242. */
  10243. public function addJavascriptObject($script, $onload=false) {
  10244. ++$this->js_obj_id;
  10245. $this->js_objects[$this->js_obj_id] = array('js' => $script, 'onload' => $onload);
  10246. return $this->js_obj_id;
  10247. }
  10248. /**
  10249. * Create a javascript PDF string.
  10250. * @access protected
  10251. * @author Johannes Güntert, Nicola Asuni
  10252. * @since 2.1.002 (2008-02-12)
  10253. */
  10254. protected function _putjavascript() {
  10255. if (empty($this->javascript) AND empty($this->js_objects)) {
  10256. return;
  10257. }
  10258. if (strpos($this->javascript, 'this.addField') > 0) {
  10259. if (!$this->ur) {
  10260. //$this->setUserRights();
  10261. }
  10262. // the following two lines are used to avoid form fields duplication after saving
  10263. // The addField method only works on Acrobat Writer, unless the document is signed with Adobe private key (UR3)
  10264. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  10265. $jsb = "getField('tcpdfdocsaved').value='saved';";
  10266. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  10267. }
  10268. $this->n_js = $this->_newobj();
  10269. $out = ' << /Names [';
  10270. if (!empty($this->javascript)) {
  10271. $out .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  10272. }
  10273. if (!empty($this->js_objects)) {
  10274. foreach ($this->js_objects as $key => $val) {
  10275. if ($val['onload']) {
  10276. $out .= ' (JS'.$key.') '.$key.' 0 R';
  10277. }
  10278. }
  10279. }
  10280. $out .= ' ] >> endobj';
  10281. $this->_out($out);
  10282. // default Javascript object
  10283. if (!empty($this->javascript)) {
  10284. $this->_newobj();
  10285. $out = '<< /S /JavaScript';
  10286. $out .= ' /JS '.$this->_textstring($this->javascript);
  10287. $out .= ' >> endobj';
  10288. $this->_out($out);
  10289. }
  10290. // additional Javascript objects
  10291. if (!empty($this->js_objects)) {
  10292. foreach ($this->js_objects as $key => $val) {
  10293. $this->offsets[$key] = $this->bufferlen;
  10294. $out = $key.' 0 obj'."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js']).' >> endobj';
  10295. $this->_out($out);
  10296. }
  10297. }
  10298. }
  10299. /**
  10300. * Convert color to javascript color.
  10301. * @param string $color color name or #RRGGBB
  10302. * @access protected
  10303. * @author Denis Van Nuffelen, Nicola Asuni
  10304. * @since 2.1.002 (2008-02-12)
  10305. */
  10306. protected function _JScolor($color) {
  10307. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  10308. if (substr($color,0,1) == '#') {
  10309. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  10310. }
  10311. if (!in_array($color,$aColors)) {
  10312. $this->Error('Invalid color: '.$color);
  10313. }
  10314. return 'color.'.$color;
  10315. }
  10316. /**
  10317. * Adds a javascript form field.
  10318. * @param string $type field type
  10319. * @param string $name field name
  10320. * @param int $x horizontal position
  10321. * @param int $y vertical position
  10322. * @param int $w width
  10323. * @param int $h height
  10324. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10325. * @access protected
  10326. * @author Denis Van Nuffelen, Nicola Asuni
  10327. * @since 2.1.002 (2008-02-12)
  10328. */
  10329. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  10330. if ($this->rtl) {
  10331. $x = $x - $w;
  10332. }
  10333. // the followind avoid fields duplication after saving the document
  10334. $this->javascript .= "if(getField('tcpdfdocsaved').value != 'saved') {";
  10335. $k = $this->k;
  10336. $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";
  10337. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  10338. while (list($key, $val) = each($prop)) {
  10339. if (strcmp(substr($key, -5), 'Color') == 0) {
  10340. $val = $this->_JScolor($val);
  10341. } else {
  10342. $val = "'".$val."'";
  10343. }
  10344. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  10345. }
  10346. if ($this->rtl) {
  10347. $this->x -= $w;
  10348. } else {
  10349. $this->x += $w;
  10350. }
  10351. $this->javascript .= '}';
  10352. }
  10353. // --- FORM FIELDS -----------------------------------------------------
  10354. /**
  10355. * Convert JavaScript form fields properties array to Annotation Properties array.
  10356. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10357. * @return array of annotation properties
  10358. * @access protected
  10359. * @author Nicola Asuni
  10360. * @since 4.8.000 (2009-09-06)
  10361. */
  10362. protected function getAnnotOptFromJSProp($prop) {
  10363. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  10364. // the annotation options area lready defined
  10365. return $prop['aopt'];
  10366. }
  10367. $opt = array(); // value to be returned
  10368. // alignment: Controls how the text is laid out within the text field.
  10369. if (isset($prop['alignment'])) {
  10370. switch ($prop['alignment']) {
  10371. case 'left': {
  10372. $opt['q'] = 0;
  10373. break;
  10374. }
  10375. case 'center': {
  10376. $opt['q'] = 1;
  10377. break;
  10378. }
  10379. case 'right': {
  10380. $opt['q'] = 2;
  10381. break;
  10382. }
  10383. default: {
  10384. $opt['q'] = ($this->rtl)?2:0;
  10385. break;
  10386. }
  10387. }
  10388. }
  10389. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  10390. if (isset($prop['lineWidth'])) {
  10391. $linewidth = intval($prop['lineWidth']);
  10392. } else {
  10393. $linewidth = 1;
  10394. }
  10395. // borderStyle: The border style for a field.
  10396. if (isset($prop['borderStyle'])) {
  10397. switch ($prop['borderStyle']) {
  10398. case 'border.d':
  10399. case 'dashed': {
  10400. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  10401. $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
  10402. break;
  10403. }
  10404. case 'border.b':
  10405. case 'beveled': {
  10406. $opt['border'] = array(0, 0, $linewidth);
  10407. $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
  10408. break;
  10409. }
  10410. case 'border.i':
  10411. case 'inset': {
  10412. $opt['border'] = array(0, 0, $linewidth);
  10413. $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
  10414. break;
  10415. }
  10416. case 'border.u':
  10417. case 'underline': {
  10418. $opt['border'] = array(0, 0, $linewidth);
  10419. $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
  10420. break;
  10421. }
  10422. default:
  10423. case 'border.s':
  10424. case 'solid': {
  10425. $opt['border'] = array(0, 0, $linewidth);
  10426. $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
  10427. break;
  10428. }
  10429. }
  10430. }
  10431. if (isset($prop['border']) AND is_array($prop['border'])) {
  10432. $opt['border'] = $prop['border'];
  10433. }
  10434. if (!isset($opt['mk'])) {
  10435. $opt['mk'] = array();
  10436. }
  10437. if (!isset($opt['mk']['if'])) {
  10438. $opt['mk']['if'] = array();
  10439. }
  10440. $opt['mk']['if']['a'] = array(0.5, 0.5);
  10441. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  10442. if (isset($prop['buttonAlignX'])) {
  10443. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  10444. }
  10445. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  10446. if (isset($prop['buttonAlignY'])) {
  10447. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  10448. }
  10449. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  10450. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  10451. $opt['mk']['if']['fb'] = true;
  10452. }
  10453. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  10454. if (isset($prop['buttonScaleHow'])) {
  10455. switch ($prop['buttonScaleHow']) {
  10456. case 'scaleHow.proportional': {
  10457. $opt['mk']['if']['s'] = 'P';
  10458. break;
  10459. }
  10460. case 'scaleHow.anamorphic': {
  10461. $opt['mk']['if']['s'] = 'A';
  10462. break;
  10463. }
  10464. }
  10465. }
  10466. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  10467. if (isset($prop['buttonScaleWhen'])) {
  10468. switch ($prop['buttonScaleWhen']) {
  10469. case 'scaleWhen.always': {
  10470. $opt['mk']['if']['sw'] = 'A';
  10471. break;
  10472. }
  10473. case 'scaleWhen.never': {
  10474. $opt['mk']['if']['sw'] = 'N';
  10475. break;
  10476. }
  10477. case 'scaleWhen.tooBig': {
  10478. $opt['mk']['if']['sw'] = 'B';
  10479. break;
  10480. }
  10481. case 'scaleWhen.tooSmall': {
  10482. $opt['mk']['if']['sw'] = 'S';
  10483. break;
  10484. }
  10485. }
  10486. }
  10487. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  10488. if (isset($prop['buttonPosition'])) {
  10489. switch ($prop['buttonPosition']) {
  10490. case 0:
  10491. case 'position.textOnly': {
  10492. $opt['mk']['tp'] = 0;
  10493. break;
  10494. }
  10495. case 1:
  10496. case 'position.iconOnly': {
  10497. $opt['mk']['tp'] = 1;
  10498. break;
  10499. }
  10500. case 2:
  10501. case 'position.iconTextV': {
  10502. $opt['mk']['tp'] = 2;
  10503. break;
  10504. }
  10505. case 3:
  10506. case 'position.textIconV': {
  10507. $opt['mk']['tp'] = 3;
  10508. break;
  10509. }
  10510. case 4:
  10511. case 'position.iconTextH': {
  10512. $opt['mk']['tp'] = 4;
  10513. break;
  10514. }
  10515. case 5:
  10516. case 'position.textIconH': {
  10517. $opt['mk']['tp'] = 5;
  10518. break;
  10519. }
  10520. case 6:
  10521. case 'position.overlay': {
  10522. $opt['mk']['tp'] = 6;
  10523. break;
  10524. }
  10525. }
  10526. }
  10527. // fillColor: Specifies the background color for a field.
  10528. if (isset($prop['fillColor'])) {
  10529. if (is_array($prop['fillColor'])) {
  10530. $opt['mk']['bg'] = $prop['fillColor'];
  10531. } else {
  10532. $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
  10533. }
  10534. }
  10535. // 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.
  10536. if (isset($prop['strokeColor'])) {
  10537. if (is_array($prop['strokeColor'])) {
  10538. $opt['mk']['bc'] = $prop['strokeColor'];
  10539. } else {
  10540. $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
  10541. }
  10542. }
  10543. // rotation: The rotation of a widget in counterclockwise increments.
  10544. if (isset($prop['rotation'])) {
  10545. $opt['mk']['r'] = $prop['rotation'];
  10546. }
  10547. // charLimit: Limits the number of characters that a user can type into a text field.
  10548. if (isset($prop['charLimit'])) {
  10549. $opt['maxlen'] = intval($prop['charLimit']);
  10550. }
  10551. if (!isset($ff)) {
  10552. $ff = 0;
  10553. }
  10554. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  10555. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  10556. $ff += 1 << 0;
  10557. }
  10558. // required: Specifies whether a field requires a value.
  10559. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  10560. $ff += 1 << 1;
  10561. }
  10562. // multiline: Controls how text is wrapped within the field.
  10563. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  10564. $ff += 1 << 12;
  10565. }
  10566. // password: Specifies whether the field should display asterisks when data is entered in the field.
  10567. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  10568. $ff += 1 << 13;
  10569. }
  10570. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  10571. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  10572. $ff += 1 << 14;
  10573. }
  10574. // Radio: If set, the field is a set of radio buttons.
  10575. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  10576. $ff += 1 << 15;
  10577. }
  10578. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  10579. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  10580. $ff += 1 << 16;
  10581. }
  10582. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  10583. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  10584. $ff += 1 << 17;
  10585. }
  10586. // editable: Controls whether a combo box is editable.
  10587. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  10588. $ff += 1 << 18;
  10589. }
  10590. // Sort: If set, the field's option items shall be sorted alphabetically.
  10591. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  10592. $ff += 1 << 19;
  10593. }
  10594. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  10595. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  10596. $ff += 1 << 20;
  10597. }
  10598. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  10599. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  10600. $ff += 1 << 21;
  10601. }
  10602. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  10603. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  10604. $ff += 1 << 22;
  10605. }
  10606. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  10607. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  10608. $ff += 1 << 23;
  10609. }
  10610. // 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.
  10611. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  10612. $ff += 1 << 24;
  10613. }
  10614. // 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.
  10615. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  10616. $ff += 1 << 25;
  10617. }
  10618. // richText: If true, the field allows rich text formatting.
  10619. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  10620. $ff += 1 << 25;
  10621. }
  10622. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  10623. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  10624. $ff += 1 << 26;
  10625. }
  10626. $opt['ff'] = $ff;
  10627. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  10628. if (isset($prop['defaultValue'])) {
  10629. $opt['dv'] = $prop['defaultValue'];
  10630. }
  10631. $f = 4; // default value for annotation flags
  10632. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  10633. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  10634. $f += 1 << 6;
  10635. }
  10636. // display: Controls whether the field is hidden or visible on screen and in print.
  10637. if (isset($prop['display'])) {
  10638. if ($prop['display'] == 'display.visible') {
  10639. //
  10640. } elseif ($prop['display'] == 'display.hidden') {
  10641. $f += 1 << 1;
  10642. } elseif ($prop['display'] == 'display.noPrint') {
  10643. $f -= 1 << 2;
  10644. } elseif ($prop['display'] == 'display.noView') {
  10645. $f += 1 << 5;
  10646. }
  10647. }
  10648. $opt['f'] = $f;
  10649. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  10650. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  10651. $opt['i'] = $prop['currentValueIndices'];
  10652. }
  10653. // value: The value of the field data that the user has entered.
  10654. if (isset($prop['value'])) {
  10655. if (is_array($prop['value'])) {
  10656. $opt['opt'] = array();
  10657. foreach ($prop['value'] AS $key => $optval) {
  10658. // exportValues: An array of strings representing the export values for the field.
  10659. if (isset($prop['exportValues'][$key])) {
  10660. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  10661. } else {
  10662. $opt['opt'][$key] = $prop['value'][$key];
  10663. }
  10664. }
  10665. } else {
  10666. $opt['v'] = $prop['value'];
  10667. }
  10668. }
  10669. // richValue: This property specifies the text contents and formatting of a rich text field.
  10670. if (isset($prop['richValue'])) {
  10671. $opt['rv'] = $prop['richValue'];
  10672. }
  10673. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  10674. if (isset($prop['submitName'])) {
  10675. $opt['tm'] = $prop['submitName'];
  10676. }
  10677. // name: Fully qualified field name.
  10678. if (isset($prop['name'])) {
  10679. $opt['t'] = $prop['name'];
  10680. }
  10681. // userName: The user name (short description string) of the field.
  10682. if (isset($prop['userName'])) {
  10683. $opt['tu'] = $prop['userName'];
  10684. }
  10685. // highlight: Defines how a button reacts when a user clicks it.
  10686. if (isset($prop['highlight'])) {
  10687. switch ($prop['highlight']) {
  10688. case 'none':
  10689. case 'highlight.n': {
  10690. $opt['h'] = 'N';
  10691. break;
  10692. }
  10693. case 'invert':
  10694. case 'highlight.i': {
  10695. $opt['h'] = 'i';
  10696. break;
  10697. }
  10698. case 'push':
  10699. case 'highlight.p': {
  10700. $opt['h'] = 'P';
  10701. break;
  10702. }
  10703. case 'outline':
  10704. case 'highlight.o': {
  10705. $opt['h'] = 'O';
  10706. break;
  10707. }
  10708. }
  10709. }
  10710. // Unsupported options:
  10711. // - calcOrderIndex: Changes the calculation order of fields in the document.
  10712. // - delay: Delays the redrawing of a field's appearance.
  10713. // - defaultStyle: This property defines the default style attributes for the form field.
  10714. // - style: Allows the user to set the glyph style of a check box or radio button.
  10715. // - textColor, textFont, textSize
  10716. return $opt;
  10717. }
  10718. /**
  10719. * Set default properties for form fields.
  10720. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10721. * @access public
  10722. * @author Nicola Asuni
  10723. * @since 4.8.000 (2009-09-06)
  10724. */
  10725. public function setFormDefaultProp($prop=array()) {
  10726. $this->default_form_prop = $prop;
  10727. }
  10728. /**
  10729. * Return the default properties for form fields.
  10730. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10731. * @access public
  10732. * @author Nicola Asuni
  10733. * @since 4.8.000 (2009-09-06)
  10734. */
  10735. public function getFormDefaultProp() {
  10736. return $this->default_form_prop;
  10737. }
  10738. /**
  10739. * Creates a text field
  10740. * @param string $name field name
  10741. * @param float $w Width of the rectangle
  10742. * @param float $h Height of the rectangle
  10743. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10744. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10745. * @param float $x Abscissa of the upper-left corner of the rectangle
  10746. * @param float $y Ordinate of the upper-left corner of the rectangle
  10747. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10748. * @access public
  10749. * @author Nicola Asuni
  10750. * @since 4.8.000 (2009-09-07)
  10751. */
  10752. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10753. if ($x === '') {
  10754. $x = $this->x;
  10755. }
  10756. if ($y === '') {
  10757. $y = $this->y;
  10758. }
  10759. if ($js) {
  10760. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  10761. return;
  10762. }
  10763. // get default style
  10764. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10765. // get annotation data
  10766. $popt = $this->getAnnotOptFromJSProp($prop);
  10767. // set default appearance stream
  10768. $font = $this->FontFamily;
  10769. $fontkey = array_search($font, $this->fontkeys);
  10770. if (!in_array($fontkey, $this->annotation_fonts)) {
  10771. $this->annotation_fonts[$font] = $fontkey;
  10772. }
  10773. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10774. $popt['da'] = $fontstyle;
  10775. $popt['ap'] = array();
  10776. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10777. // merge options
  10778. $opt = array_merge($popt, $opt);
  10779. // remove some conflicting options
  10780. unset($opt['bs']);
  10781. // set remaining annotation data
  10782. $opt['Subtype'] = 'Widget';
  10783. $opt['ft'] = 'Tx';
  10784. $opt['t'] = $name;
  10785. /*
  10786. Additional annotation's parameters (check _putannotsobj() method):
  10787. //$opt['f']
  10788. //$opt['ap']
  10789. //$opt['as']
  10790. //$opt['bs']
  10791. //$opt['be']
  10792. //$opt['c']
  10793. //$opt['border']
  10794. //$opt['h']
  10795. //$opt['mk']
  10796. //$opt['mk']['r']
  10797. //$opt['mk']['bc']
  10798. //$opt['mk']['bg']
  10799. //$opt['mk']['ca']
  10800. //$opt['mk']['rc']
  10801. //$opt['mk']['ac']
  10802. //$opt['mk']['i']
  10803. //$opt['mk']['ri']
  10804. //$opt['mk']['ix']
  10805. //$opt['mk']['if']
  10806. //$opt['mk']['if']['sw']
  10807. //$opt['mk']['if']['s']
  10808. //$opt['mk']['if']['a']
  10809. //$opt['mk']['if']['fb']
  10810. //$opt['mk']['tp']
  10811. //$opt['tu']
  10812. //$opt['tm']
  10813. //$opt['ff']
  10814. //$opt['v']
  10815. //$opt['dv']
  10816. //$opt['a']
  10817. //$opt['aa']
  10818. //$opt['q']
  10819. */
  10820. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10821. if ($this->rtl) {
  10822. $this->x -= $w;
  10823. } else {
  10824. $this->x += $w;
  10825. }
  10826. }
  10827. /**
  10828. * Creates a RadioButton field
  10829. * @param string $name field name
  10830. * @param int $w width
  10831. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10832. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10833. * @param string $onvalue value to be returned if selected.
  10834. * @param boolean $checked define the initial state.
  10835. * @param float $x Abscissa of the upper-left corner of the rectangle
  10836. * @param float $y Ordinate of the upper-left corner of the rectangle
  10837. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10838. * @access public
  10839. * @author Nicola Asuni
  10840. * @since 4.8.000 (2009-09-07)
  10841. */
  10842. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  10843. if ($x === '') {
  10844. $x = $this->x;
  10845. }
  10846. if ($y === '') {
  10847. $y = $this->y;
  10848. }
  10849. if ($js) {
  10850. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  10851. return;
  10852. }
  10853. if ($this->empty_string($onvalue)) {
  10854. $onvalue = 'On';
  10855. }
  10856. if ($checked) {
  10857. $defval = $onvalue;
  10858. } else {
  10859. $defval = 'Off';
  10860. }
  10861. // set data for parent group
  10862. if (!isset($this->radiobutton_groups[$this->page])) {
  10863. $this->radiobutton_groups[$this->page] = array();
  10864. }
  10865. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  10866. $this->radiobutton_groups[$this->page][$name] = array();
  10867. ++$this->annot_obj_id;
  10868. $this->radio_groups[] = $this->annot_obj_id;
  10869. }
  10870. // save object ID to be added on Kids entry on parent object
  10871. $this->radiobutton_groups[$this->page][$name][] = array('kid' => ($this->annot_obj_id + 1), 'def' => $defval);
  10872. // get default style
  10873. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10874. $prop['NoToggleToOff'] = 'true';
  10875. $prop['Radio'] = 'true';
  10876. $prop['borderStyle'] = 'inset';
  10877. // get annotation data
  10878. $popt = $this->getAnnotOptFromJSProp($prop);
  10879. // set additional default values
  10880. $font = 'zapfdingbats';
  10881. $this->AddFont($font);
  10882. $fontkey = array_search($font, $this->fontkeys);
  10883. if (!in_array($fontkey, $this->annotation_fonts)) {
  10884. $this->annotation_fonts[$font] = $fontkey;
  10885. }
  10886. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10887. $popt['da'] = $fontstyle;
  10888. $popt['ap'] = array();
  10889. $popt['ap']['n'] = array();
  10890. $popt['ap']['n'][$onvalue] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10891. $popt['ap']['n']['Off'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  10892. if (!isset($popt['mk'])) {
  10893. $popt['mk'] = array();
  10894. }
  10895. $popt['mk']['ca'] = '(l)';
  10896. // merge options
  10897. $opt = array_merge($popt, $opt);
  10898. // set remaining annotation data
  10899. $opt['Subtype'] = 'Widget';
  10900. $opt['ft'] = 'Btn';
  10901. if ($checked) {
  10902. $opt['v'] = array('/'.$onvalue);
  10903. $opt['as'] = $onvalue;
  10904. } else {
  10905. $opt['as'] = 'Off';
  10906. }
  10907. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  10908. if ($this->rtl) {
  10909. $this->x -= $w;
  10910. } else {
  10911. $this->x += $w;
  10912. }
  10913. }
  10914. /**
  10915. * Creates a List-box field
  10916. * @param string $name field name
  10917. * @param int $w width
  10918. * @param int $h height
  10919. * @param array $values array containing the list of values.
  10920. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10921. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10922. * @param float $x Abscissa of the upper-left corner of the rectangle
  10923. * @param float $y Ordinate of the upper-left corner of the rectangle
  10924. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10925. * @access public
  10926. * @author Nicola Asuni
  10927. * @since 4.8.000 (2009-09-07)
  10928. */
  10929. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10930. if ($x === '') {
  10931. $x = $this->x;
  10932. }
  10933. if ($y === '') {
  10934. $y = $this->y;
  10935. }
  10936. if ($js) {
  10937. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  10938. $s = '';
  10939. foreach ($values as $value) {
  10940. $s .= "'".addslashes($value)."',";
  10941. }
  10942. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  10943. return;
  10944. }
  10945. // get default style
  10946. $prop = array_merge($this->getFormDefaultProp(), $prop);
  10947. // get annotation data
  10948. $popt = $this->getAnnotOptFromJSProp($prop);
  10949. // set additional default values
  10950. $font = $this->FontFamily;
  10951. $fontkey = array_search($font, $this->fontkeys);
  10952. if (!in_array($fontkey, $this->annotation_fonts)) {
  10953. $this->annotation_fonts[$font] = $fontkey;
  10954. }
  10955. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  10956. $popt['da'] = $fontstyle;
  10957. $popt['ap'] = array();
  10958. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  10959. // merge options
  10960. $opt = array_merge($popt, $opt);
  10961. // set remaining annotation data
  10962. $opt['Subtype'] = 'Widget';
  10963. $opt['ft'] = 'Ch';
  10964. $opt['t'] = $name;
  10965. $opt['opt'] = $values;
  10966. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  10967. if ($this->rtl) {
  10968. $this->x -= $w;
  10969. } else {
  10970. $this->x += $w;
  10971. }
  10972. }
  10973. /**
  10974. * Creates a Combo-box field
  10975. * @param string $name field name
  10976. * @param int $w width
  10977. * @param int $h height
  10978. * @param array $values array containing the list of values.
  10979. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  10980. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  10981. * @param float $x Abscissa of the upper-left corner of the rectangle
  10982. * @param float $y Ordinate of the upper-left corner of the rectangle
  10983. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  10984. * @access public
  10985. * @author Nicola Asuni
  10986. * @since 4.8.000 (2009-09-07)
  10987. */
  10988. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  10989. if ($x === '') {
  10990. $x = $this->x;
  10991. }
  10992. if ($y === '') {
  10993. $y = $this->y;
  10994. }
  10995. if ($js) {
  10996. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  10997. $s = '';
  10998. foreach ($values as $value) {
  10999. $s .= "'".addslashes($value)."',";
  11000. }
  11001. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  11002. return;
  11003. }
  11004. // get default style
  11005. $prop = array_merge($this->getFormDefaultProp(), $prop);
  11006. $prop['Combo'] = true;
  11007. // get annotation data
  11008. $popt = $this->getAnnotOptFromJSProp($prop);
  11009. // set additional default options
  11010. $font = $this->FontFamily;
  11011. $fontkey = array_search($font, $this->fontkeys);
  11012. if (!in_array($fontkey, $this->annotation_fonts)) {
  11013. $this->annotation_fonts[$font] = $fontkey;
  11014. }
  11015. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  11016. $popt['da'] = $fontstyle;
  11017. $popt['ap'] = array();
  11018. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  11019. // merge options
  11020. $opt = array_merge($popt, $opt);
  11021. // set remaining annotation data
  11022. $opt['Subtype'] = 'Widget';
  11023. $opt['ft'] = 'Ch';
  11024. $opt['t'] = $name;
  11025. $opt['opt'] = $values;
  11026. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  11027. if ($this->rtl) {
  11028. $this->x -= $w;
  11029. } else {
  11030. $this->x += $w;
  11031. }
  11032. }
  11033. /**
  11034. * Creates a CheckBox field
  11035. * @param string $name field name
  11036. * @param int $w width
  11037. * @param boolean $checked define the initial state.
  11038. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  11039. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  11040. * @param string $onvalue value to be returned if selected.
  11041. * @param float $x Abscissa of the upper-left corner of the rectangle
  11042. * @param float $y Ordinate of the upper-left corner of the rectangle
  11043. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  11044. * @access public
  11045. * @author Nicola Asuni
  11046. * @since 4.8.000 (2009-09-07)
  11047. */
  11048. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  11049. if ($x === '') {
  11050. $x = $this->x;
  11051. }
  11052. if ($y === '') {
  11053. $y = $this->y;
  11054. }
  11055. if ($js) {
  11056. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  11057. return;
  11058. }
  11059. if (!isset($prop['value'])) {
  11060. $prop['value'] = array('Yes');
  11061. }
  11062. // get default style
  11063. $prop = array_merge($this->getFormDefaultProp(), $prop);
  11064. $prop['borderStyle'] = 'inset';
  11065. // get annotation data
  11066. $popt = $this->getAnnotOptFromJSProp($prop);
  11067. // set additional default options
  11068. $font = 'zapfdingbats';
  11069. $this->AddFont($font);
  11070. $fontkey = array_search($font, $this->fontkeys);
  11071. if (!in_array($fontkey, $this->annotation_fonts)) {
  11072. $this->annotation_fonts[$font] = $fontkey;
  11073. }
  11074. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  11075. $popt['da'] = $fontstyle;
  11076. $popt['ap'] = array();
  11077. $popt['ap']['n'] = array();
  11078. $popt['ap']['n']['Yes'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  11079. $popt['ap']['n']['Off'] = 'q BT '.$fontstyle.' 0 0 Td (8) Tj ET Q';
  11080. // merge options
  11081. $opt = array_merge($popt, $opt);
  11082. // set remaining annotation data
  11083. $opt['Subtype'] = 'Widget';
  11084. $opt['ft'] = 'Btn';
  11085. $opt['t'] = $name;
  11086. $opt['opt'] = array($onvalue);
  11087. if ($checked) {
  11088. $opt['v'] = array('/0');
  11089. $opt['as'] = 'Yes';
  11090. } else {
  11091. $opt['v'] = array('/Off');
  11092. $opt['as'] = 'Off';
  11093. }
  11094. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  11095. if ($this->rtl) {
  11096. $this->x -= $w;
  11097. } else {
  11098. $this->x += $w;
  11099. }
  11100. }
  11101. /**
  11102. * Creates a button field
  11103. * @param string $name field name
  11104. * @param int $w width
  11105. * @param int $h height
  11106. * @param string $caption caption.
  11107. * @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.
  11108. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  11109. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  11110. * @param float $x Abscissa of the upper-left corner of the rectangle
  11111. * @param float $y Ordinate of the upper-left corner of the rectangle
  11112. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  11113. * @access public
  11114. * @author Nicola Asuni
  11115. * @since 4.8.000 (2009-09-07)
  11116. */
  11117. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  11118. if ($x === '') {
  11119. $x = $this->x;
  11120. }
  11121. if ($y === '') {
  11122. $y = $this->y;
  11123. }
  11124. if ($js) {
  11125. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  11126. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  11127. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  11128. $this->javascript .= 'f'.$name.".highlight='push';\n";
  11129. $this->javascript .= 'f'.$name.".print=false;\n";
  11130. return;
  11131. }
  11132. // get default style
  11133. $prop = array_merge($this->getFormDefaultProp(), $prop);
  11134. $prop['Pushbutton'] = 'true';
  11135. $prop['highlight'] = 'push';
  11136. $prop['display'] = 'display.noPrint';
  11137. // get annotation data
  11138. $popt = $this->getAnnotOptFromJSProp($prop);
  11139. // set additional default options
  11140. if (!isset($popt['mk'])) {
  11141. $popt['mk'] = array();
  11142. }
  11143. $popt['mk']['ca'] = $this->_textstring($caption);
  11144. $popt['mk']['rc'] = $this->_textstring($caption);
  11145. $popt['mk']['ac'] = $this->_textstring($caption);
  11146. $font = $this->FontFamily;
  11147. $fontkey = array_search($font, $this->fontkeys);
  11148. if (!in_array($fontkey, $this->annotation_fonts)) {
  11149. $this->annotation_fonts[$font] = $fontkey;
  11150. }
  11151. $fontstyle = sprintf('/F%d %.2F Tf %s', ($fontkey + 1), $this->FontSizePt, $this->TextColor);
  11152. $popt['da'] = $fontstyle;
  11153. $popt['ap'] = array();
  11154. $popt['ap']['n'] = 'q BT '.$fontstyle.' ET Q';
  11155. // merge options
  11156. $opt = array_merge($popt, $opt);
  11157. // set remaining annotation data
  11158. $opt['Subtype'] = 'Widget';
  11159. $opt['ft'] = 'Btn';
  11160. $opt['t'] = $caption;
  11161. $opt['v'] = $name;
  11162. if (!empty($action)) {
  11163. if (is_array($action)) {
  11164. // form action options as on section 12.7.5 of PDF32000_2008.
  11165. $opt['aa'] = '/D <<';
  11166. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  11167. foreach ($action AS $key => $val) {
  11168. if (($key == 'S') AND in_array($val, $bmode)) {
  11169. $opt['aa'] .= ' /S /'.$val;
  11170. } elseif (($key == 'F') AND (!empty($val))) {
  11171. $opt['aa'] .= ' /F '.$this->_datastring($val);
  11172. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  11173. $opt['aa'] .= ' /Fields [';
  11174. foreach ($val AS $field) {
  11175. $opt['aa'] .= ' '.$this->_textstring($field);
  11176. }
  11177. $opt['aa'] .= ']';
  11178. } elseif (($key == 'Flags')) {
  11179. $ff = 0;
  11180. if (is_array($val)) {
  11181. foreach ($val AS $flag) {
  11182. switch ($flag) {
  11183. case 'Include/Exclude': {
  11184. $ff += 1 << 0;
  11185. break;
  11186. }
  11187. case 'IncludeNoValueFields': {
  11188. $ff += 1 << 1;
  11189. break;
  11190. }
  11191. case 'ExportFormat': {
  11192. $ff += 1 << 2;
  11193. break;
  11194. }
  11195. case 'GetMethod': {
  11196. $ff += 1 << 3;
  11197. break;
  11198. }
  11199. case 'SubmitCoordinates': {
  11200. $ff += 1 << 4;
  11201. break;
  11202. }
  11203. case 'XFDF': {
  11204. $ff += 1 << 5;
  11205. break;
  11206. }
  11207. case 'IncludeAppendSaves': {
  11208. $ff += 1 << 6;
  11209. break;
  11210. }
  11211. case 'IncludeAnnotations': {
  11212. $ff += 1 << 7;
  11213. break;
  11214. }
  11215. case 'SubmitPDF': {
  11216. $ff += 1 << 8;
  11217. break;
  11218. }
  11219. case 'CanonicalFormat': {
  11220. $ff += 1 << 9;
  11221. break;
  11222. }
  11223. case 'ExclNonUserAnnots': {
  11224. $ff += 1 << 10;
  11225. break;
  11226. }
  11227. case 'ExclFKey': {
  11228. $ff += 1 << 11;
  11229. break;
  11230. }
  11231. case 'EmbedForm': {
  11232. $ff += 1 << 13;
  11233. break;
  11234. }
  11235. }
  11236. }
  11237. } else {
  11238. $ff = intval($val);
  11239. }
  11240. $opt['aa'] .= ' /Flags '.$ff;
  11241. }
  11242. }
  11243. $opt['aa'] .= ' >>';
  11244. } else {
  11245. // Javascript action or raw action command
  11246. $js_obj_id = $this->addJavascriptObject($action);
  11247. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  11248. }
  11249. }
  11250. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  11251. if ($this->rtl) {
  11252. $this->x -= $w;
  11253. } else {
  11254. $this->x += $w;
  11255. }
  11256. }
  11257. // --- END FORMS FIELDS ------------------------------------------------
  11258. /**
  11259. * Add certification signature (DocMDP or UR3)
  11260. * You can set only one signature type
  11261. * @access protected
  11262. * @author Nicola Asuni
  11263. * @since 4.6.008 (2009-05-07)
  11264. */
  11265. protected function _putsignature() {
  11266. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  11267. return;
  11268. }
  11269. $this->_newobj();
  11270. $out = ' << /Type /Sig';
  11271. $out .= ' /Filter /Adobe.PPKLite';
  11272. $out .= ' /SubFilter /adbe.pkcs7.detached';
  11273. $out .= ' '.$this->byterange_string;
  11274. $out .= ' /Contents<>'.str_repeat(' ', $this->signature_max_length);
  11275. $out .= ' /Reference';
  11276. $out .= ' [';
  11277. $out .= ' << /Type /SigRef';
  11278. if ($this->signature_data['cert_type'] > 0) {
  11279. $out .= ' /TransformMethod /DocMDP';
  11280. $out .= ' /TransformParams';
  11281. $out .= ' <<';
  11282. $out .= ' /Type /TransformParams';
  11283. $out .= ' /V /1.2';
  11284. $out .= ' /P '.$this->signature_data['cert_type'];
  11285. } else {
  11286. $out .= ' /TransformMethod /UR3';
  11287. $out .= ' /TransformParams';
  11288. $out .= ' << /Type /TransformParams';
  11289. $out .= ' /V /2.2';
  11290. if (!$this->empty_string($this->ur_document)) {
  11291. $out .= ' /Document['.$this->ur_document.']';
  11292. }
  11293. if (!$this->empty_string($this->ur_annots)) {
  11294. $out .= ' /Annots['.$this->ur_annots.']';
  11295. }
  11296. if (!$this->empty_string($this->ur_form)) {
  11297. $out .= ' /Form['.$this->ur_form.']';
  11298. }
  11299. if (!$this->empty_string($this->ur_signature)) {
  11300. $out .= ' /Signature['.$this->ur_signature.']';
  11301. }
  11302. }
  11303. $out .= ' >> >> ]';
  11304. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  11305. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name']);
  11306. }
  11307. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  11308. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location']);
  11309. }
  11310. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  11311. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason']);
  11312. }
  11313. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  11314. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo']);
  11315. }
  11316. $out .= ' /M '.$this->_datestring();
  11317. $out .= ' >> endobj';
  11318. $this->_out($out);
  11319. }
  11320. /**
  11321. * Set User's Rights for PDF Reader
  11322. * WARNING: This works only using the Adobe private key with the setSignature() method!.
  11323. * Check the PDF Reference 8.7.1 Transform Methods,
  11324. * Table 8.105 Entries in the UR transform parameters dictionary
  11325. * @param boolean $enable if true enable user's rights on PDF reader
  11326. * @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.
  11327. * @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.
  11328. * @param string $form Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  11329. * @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.
  11330. * @access public
  11331. * @author Nicola Asuni
  11332. * @since 2.9.000 (2008-03-26)
  11333. */
  11334. public function setUserRights(
  11335. $enable=true,
  11336. $document='/FullSave',
  11337. $annots='/Create/Delete/Modify/Copy/Import/Export',
  11338. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  11339. $signature='/Modify') {
  11340. $this->ur = $enable;
  11341. $this->ur_document = $document;
  11342. $this->ur_annots = $annots;
  11343. $this->ur_form = $form;
  11344. $this->ur_signature = $signature;
  11345. if (!$this->sign) {
  11346. // This signature only works using the Adobe Private key that is unavailable!
  11347. $this->setSignature('', '', '', '', 0, array());
  11348. }
  11349. }
  11350. /**
  11351. * Enable document signature (requires the OpenSSL Library).
  11352. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  11353. * @param mixed $signing_cert signing certificate (string or filename prefixed with 'file://')
  11354. * @param mixed $private_key private key (string or filename prefixed with 'file://')
  11355. * @param string $private_key_password password
  11356. * @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.
  11357. * @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.
  11358. * @param array $info array of option information: Name, Location, Reason, ContactInfo.
  11359. * @access public
  11360. * @author Nicola Asuni
  11361. * @since 4.6.005 (2009-04-24)
  11362. */
  11363. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  11364. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  11365. // to convert pfx certificate to pem: openssl
  11366. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  11367. $this->sign = true;
  11368. $this->signature_data = array();
  11369. if (strlen($signing_cert) == 0) {
  11370. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
  11371. $private_key_password = 'tcpdfdemo';
  11372. }
  11373. if (strlen($private_key) == 0) {
  11374. $private_key = $signing_cert;
  11375. }
  11376. $this->signature_data['signcert'] = $signing_cert;
  11377. $this->signature_data['privkey'] = $private_key;
  11378. $this->signature_data['password'] = $private_key_password;
  11379. $this->signature_data['extracerts'] = $extracerts;
  11380. $this->signature_data['cert_type'] = $cert_type;
  11381. $this->signature_data['info'] = $info;
  11382. }
  11383. /**
  11384. * Create a new page group.
  11385. * NOTE: call this function before calling AddPage()
  11386. * @param int $page starting group page (leave empty for next page).
  11387. * @access public
  11388. * @since 3.0.000 (2008-03-27)
  11389. */
  11390. public function startPageGroup($page='') {
  11391. if (empty($page)) {
  11392. $page = $this->page + 1;
  11393. }
  11394. $this->newpagegroup[$page] = true;
  11395. }
  11396. /**
  11397. * Defines an alias for the total number of pages.
  11398. * It will be substituted as the document is closed.
  11399. * @param string $alias The alias.
  11400. * @access public
  11401. * @since 1.4
  11402. * @see getAliasNbPages(), PageNo(), Footer()
  11403. */
  11404. public function AliasNbPages($alias='{nb}') {
  11405. $this->AliasNbPages = $alias;
  11406. }
  11407. /**
  11408. * Returns the string alias used for the total number of pages.
  11409. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  11410. * @return string
  11411. * @access public
  11412. * @since 4.0.018 (2008-08-08)
  11413. * @see AliasNbPages(), PageNo(), Footer()
  11414. */
  11415. public function getAliasNbPages() {
  11416. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11417. return '{'.$this->AliasNbPages.'}';
  11418. }
  11419. return $this->AliasNbPages;
  11420. }
  11421. /**
  11422. * Defines an alias for the page number.
  11423. * It will be substituted as the document is closed.
  11424. * @param string $alias The alias.
  11425. * @access public
  11426. * @since 4.5.000 (2009-01-02)
  11427. * @see getAliasNbPages(), PageNo(), Footer()
  11428. */
  11429. public function AliasNumPage($alias='{pnb}') {
  11430. //Define an alias for total number of pages
  11431. $this->AliasNumPage = $alias;
  11432. }
  11433. /**
  11434. * Returns the string alias used for the page number.
  11435. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  11436. * @return string
  11437. * @access public
  11438. * @since 4.5.000 (2009-01-02)
  11439. * @see AliasNbPages(), PageNo(), Footer()
  11440. */
  11441. public function getAliasNumPage() {
  11442. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11443. return '{'.$this->AliasNumPage.'}';
  11444. }
  11445. return $this->AliasNumPage;
  11446. }
  11447. /**
  11448. * Return the current page in the group.
  11449. * @return current page in the group
  11450. * @access public
  11451. * @since 3.0.000 (2008-03-27)
  11452. */
  11453. public function getGroupPageNo() {
  11454. return $this->pagegroups[$this->currpagegroup];
  11455. }
  11456. /**
  11457. * Returns the current group page number formatted as a string.
  11458. * @access public
  11459. * @since 4.3.003 (2008-11-18)
  11460. * @see PaneNo(), formatPageNumber()
  11461. */
  11462. public function getGroupPageNoFormatted() {
  11463. return $this->formatPageNumber($this->getGroupPageNo());
  11464. }
  11465. /**
  11466. * Return the alias of the current page group
  11467. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  11468. * (will be replaced by the total number of pages in this group).
  11469. * @return alias of the current page group
  11470. * @access public
  11471. * @since 3.0.000 (2008-03-27)
  11472. */
  11473. public function getPageGroupAlias() {
  11474. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11475. return '{'.$this->currpagegroup.'}';
  11476. }
  11477. return $this->currpagegroup;
  11478. }
  11479. /**
  11480. * Return the alias for the page number on the current page group
  11481. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  11482. * (will be replaced by the total number of pages in this group).
  11483. * @return alias of the current page group
  11484. * @access public
  11485. * @since 4.5.000 (2009-01-02)
  11486. */
  11487. public function getPageNumGroupAlias() {
  11488. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11489. return '{'.str_replace('{nb', '{pnb', $this->currpagegroup).'}';
  11490. }
  11491. return str_replace('{nb', '{pnb', $this->currpagegroup);
  11492. }
  11493. /**
  11494. * Format the page numbers.
  11495. * This method can be overriden for custom formats.
  11496. * @param int $num page number
  11497. * @access protected
  11498. * @since 4.2.005 (2008-11-06)
  11499. */
  11500. protected function formatPageNumber($num) {
  11501. return number_format((float)$num, 0, '', '.');
  11502. }
  11503. /**
  11504. * Format the page numbers on the Table Of Content.
  11505. * This method can be overriden for custom formats.
  11506. * @param int $num page number
  11507. * @access protected
  11508. * @since 4.5.001 (2009-01-04)
  11509. * @see addTOC(), addHTMLTOC()
  11510. */
  11511. protected function formatTOCPageNumber($num) {
  11512. return number_format((float)$num, 0, '', '.');
  11513. }
  11514. /**
  11515. * Returns the current page number formatted as a string.
  11516. * @access public
  11517. * @since 4.2.005 (2008-11-06)
  11518. * @see PaneNo(), formatPageNumber()
  11519. */
  11520. public function PageNoFormatted() {
  11521. return $this->formatPageNumber($this->PageNo());
  11522. }
  11523. /**
  11524. * Put visibility settings.
  11525. * @access protected
  11526. * @since 3.0.000 (2008-03-27)
  11527. */
  11528. protected function _putocg() {
  11529. $this->_newobj();
  11530. $this->n_ocg_print = $this->n;
  11531. $this->_out('<< /Type /OCG /Name '.$this->_textstring('print').' /Usage << /Print <</PrintState /ON>> /View <</ViewState /OFF>> >> >> endobj');
  11532. $this->_newobj();
  11533. $this->n_ocg_view = $this->n;
  11534. $this->_out('<< /Type /OCG /Name '.$this->_textstring('view').' /Usage << /Print <</PrintState /OFF>> /View <</ViewState /ON>> >> >> endobj');
  11535. }
  11536. /**
  11537. * Set the visibility of the successive elements.
  11538. * This can be useful, for instance, to put a background
  11539. * image or color that will show on screen but won't print.
  11540. * @param string $v visibility mode. Legal values are: all, print, screen.
  11541. * @access public
  11542. * @since 3.0.000 (2008-03-27)
  11543. */
  11544. public function setVisibility($v) {
  11545. if ($this->openMarkedContent) {
  11546. // close existing open marked-content
  11547. $this->_out('EMC');
  11548. $this->openMarkedContent = false;
  11549. }
  11550. switch($v) {
  11551. case 'print': {
  11552. $this->_out('/OC /OC1 BDC');
  11553. $this->openMarkedContent = true;
  11554. break;
  11555. }
  11556. case 'screen': {
  11557. $this->_out('/OC /OC2 BDC');
  11558. $this->openMarkedContent = true;
  11559. break;
  11560. }
  11561. case 'all': {
  11562. $this->_out('');
  11563. break;
  11564. }
  11565. default: {
  11566. $this->Error('Incorrect visibility: '.$v);
  11567. break;
  11568. }
  11569. }
  11570. $this->visibility = $v;
  11571. }
  11572. /**
  11573. * Add transparency parameters to the current extgstate
  11574. * @param array $params parameters
  11575. * @return the number of extgstates
  11576. * @access protected
  11577. * @since 3.0.000 (2008-03-27)
  11578. */
  11579. protected function addExtGState($parms) {
  11580. $n = count($this->extgstates) + 1;
  11581. // check if this ExtGState already exist
  11582. for ($i = 1; $i < $n; ++$i) {
  11583. if ($this->extgstates[$i]['parms'] == $parms) {
  11584. // return reference to existing ExtGState
  11585. return $i;
  11586. }
  11587. }
  11588. $this->extgstates[$n]['parms'] = $parms;
  11589. return $n;
  11590. }
  11591. /**
  11592. * Add an extgstate
  11593. * @param array $gs extgstate
  11594. * @access protected
  11595. * @since 3.0.000 (2008-03-27)
  11596. */
  11597. protected function setExtGState($gs) {
  11598. $this->_out(sprintf('/GS%d gs', $gs));
  11599. }
  11600. /**
  11601. * Put extgstates for object transparency
  11602. * @param array $gs extgstate
  11603. * @access protected
  11604. * @since 3.0.000 (2008-03-27)
  11605. */
  11606. protected function _putextgstates() {
  11607. $ne = count($this->extgstates);
  11608. for ($i = 1; $i <= $ne; ++$i) {
  11609. $this->_newobj();
  11610. $this->extgstates[$i]['n'] = $this->n;
  11611. $out = '<< /Type /ExtGState';
  11612. foreach ($this->extgstates[$i]['parms'] as $k => $v) {
  11613. if (is_float($v)) {
  11614. $v = sprintf('%.2F', $v);
  11615. }
  11616. $out .= ' /'.$k.' '.$v;
  11617. }
  11618. $out .= ' >> endobj';
  11619. $this->_out($out);
  11620. }
  11621. }
  11622. /**
  11623. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  11624. * @param float $alpha real value from 0 (transparent) to 1 (opaque)
  11625. * @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
  11626. * @access public
  11627. * @since 3.0.000 (2008-03-27)
  11628. */
  11629. public function setAlpha($alpha, $bm='Normal') {
  11630. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm, 'AIS' => 'false'));
  11631. $this->setExtGState($gs);
  11632. }
  11633. /**
  11634. * Set the default JPEG compression quality (1-100)
  11635. * @param int $quality JPEG quality, integer between 1 and 100
  11636. * @access public
  11637. * @since 3.0.000 (2008-03-27)
  11638. */
  11639. public function setJPEGQuality($quality) {
  11640. if (($quality < 1) OR ($quality > 100)) {
  11641. $quality = 75;
  11642. }
  11643. $this->jpeg_quality = intval($quality);
  11644. }
  11645. /**
  11646. * Set the default number of columns in a row for HTML tables.
  11647. * @param int $cols number of columns
  11648. * @access public
  11649. * @since 3.0.014 (2008-06-04)
  11650. */
  11651. public function setDefaultTableColumns($cols=4) {
  11652. $this->default_table_columns = intval($cols);
  11653. }
  11654. /**
  11655. * Set the height of the cell (line height) respect the font height.
  11656. * @param int $h cell proportion respect font height (typical value = 1.25).
  11657. * @access public
  11658. * @since 3.0.014 (2008-06-04)
  11659. */
  11660. public function setCellHeightRatio($h) {
  11661. $this->cell_height_ratio = $h;
  11662. }
  11663. /**
  11664. * return the height of cell repect font height.
  11665. * @access public
  11666. * @since 4.0.012 (2008-07-24)
  11667. */
  11668. public function getCellHeightRatio() {
  11669. return $this->cell_height_ratio;
  11670. }
  11671. /**
  11672. * Set the PDF version (check PDF reference for valid values).
  11673. * Default value is 1.t
  11674. * @access public
  11675. * @since 3.1.000 (2008-06-09)
  11676. */
  11677. public function setPDFVersion($version='1.7') {
  11678. $this->PDFVersion = $version;
  11679. }
  11680. /**
  11681. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  11682. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  11683. * <ul><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><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><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><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><li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li><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><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><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><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><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><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><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><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><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><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><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></ul>
  11684. * @param array $preferences array of options.
  11685. * @author Nicola Asuni
  11686. * @access public
  11687. * @since 3.1.000 (2008-06-09)
  11688. */
  11689. public function setViewerPreferences($preferences) {
  11690. $this->viewer_preferences = $preferences;
  11691. }
  11692. /**
  11693. * Paints color transition registration bars
  11694. * @param float $x abscissa of the top left corner of the rectangle.
  11695. * @param float $y ordinate of the top left corner of the rectangle.
  11696. * @param float $w width of the rectangle.
  11697. * @param float $h height of the rectangle.
  11698. * @param boolean $transition if true prints tcolor transitions to white.
  11699. * @param boolean $vertical if true prints bar vertically.
  11700. * @param string $colors colors to print, one letter per color separated by comma (for example 'A,W,R,G,B,C,M,Y,K'): A=black, W=white, R=red, G=green, B=blue, C=cyan, M=magenta, Y=yellow, K=black.
  11701. * @author Nicola Asuni
  11702. * @since 4.9.000 (2010-03-26)
  11703. * @access public
  11704. */
  11705. public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
  11706. $bars = explode(',', $colors);
  11707. $numbars = count($bars); // number of bars to print
  11708. // set bar measures
  11709. if ($vertical) {
  11710. $coords = array(0, 0, 0, 1);
  11711. $wb = $w / $numbars; // bar width
  11712. $hb = $h; // bar height
  11713. $xd = $wb; // delta x
  11714. $yd = 0; // delta y
  11715. } else {
  11716. $coords = array(1, 0, 0, 0);
  11717. $wb = $w; // bar width
  11718. $hb = $h / $numbars; // bar height
  11719. $xd = 0; // delta x
  11720. $yd = $hb; // delta y
  11721. }
  11722. $xb = $x;
  11723. $yb = $y;
  11724. foreach ($bars as $col) {
  11725. switch ($col) {
  11726. // set transition colors
  11727. case 'A': { // BLACK
  11728. $col_a = array(255);
  11729. $col_b = array(0);
  11730. break;
  11731. }
  11732. case 'W': { // WHITE
  11733. $col_a = array(0);
  11734. $col_b = array(255);
  11735. break;
  11736. }
  11737. case 'R': { // R
  11738. $col_a = array(255,255,255);
  11739. $col_b = array(255,0,0);
  11740. break;
  11741. }
  11742. case 'G': { // G
  11743. $col_a = array(255,255,255);
  11744. $col_b = array(0,255,0);
  11745. break;
  11746. }
  11747. case 'B': { // B
  11748. $col_a = array(255,255,255);
  11749. $col_b = array(0,0,255);
  11750. break;
  11751. }
  11752. case 'C': { // C
  11753. $col_a = array(0,0,0,0);
  11754. $col_b = array(100,0,0,0);
  11755. break;
  11756. }
  11757. case 'M': { // M
  11758. $col_a = array(0,0,0,0);
  11759. $col_b = array(0,100,0,0);
  11760. break;
  11761. }
  11762. case 'Y': { // Y
  11763. $col_a = array(0,0,0,0);
  11764. $col_b = array(0,0,100,0);
  11765. break;
  11766. }
  11767. case 'K': { // K
  11768. $col_a = array(0,0,0,0);
  11769. $col_b = array(0,0,0,100);
  11770. break;
  11771. }
  11772. default: { // GRAY
  11773. $col_a = array(255);
  11774. $col_b = array(0);
  11775. break;
  11776. }
  11777. }
  11778. if ($transition) {
  11779. // color gradient
  11780. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  11781. } else {
  11782. // color rectangle
  11783. $this->SetFillColorArray($col_b);
  11784. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  11785. }
  11786. $xb += $xd;
  11787. $yb += $yd;
  11788. }
  11789. }
  11790. /**
  11791. * Paints crop mark
  11792. * @param float $x abscissa of the crop mark center.
  11793. * @param float $y ordinate of the crop mark center.
  11794. * @param float $w width of the crop mark.
  11795. * @param float $h height of the crop mark.
  11796. * @param string $type type of crop mark, one sybol per type separated by comma: A = top left, B = top right, C = bottom left, D = bottom right.
  11797. * @param array $color crop mark color (default black).
  11798. * @author Nicola Asuni
  11799. * @since 4.9.000 (2010-03-26)
  11800. * @access public
  11801. */
  11802. public function cropMark($x, $y, $w, $h, $type='A,B,C,D', $color=array(0,0,0)) {
  11803. $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  11804. $crops = explode(',', $type);
  11805. $numcrops = count($crops); // number of crop marks to print
  11806. $dw = $w / 4; // horizontal space to leave before the intersection point
  11807. $dh = $h / 4; // vertical space to leave before the intersection point
  11808. foreach ($crops as $crop) {
  11809. switch ($crop) {
  11810. case 'A': {
  11811. $x1 = $x;
  11812. $y1 = $y - $h;
  11813. $x2 = $x;
  11814. $y2 = $y - $dh;
  11815. $x3 = $x - $w;
  11816. $y3 = $y;
  11817. $x4 = $x - $dw;
  11818. $y4 = $y;
  11819. break;
  11820. }
  11821. case 'B': {
  11822. $x1 = $x;
  11823. $y1 = $y - $h;
  11824. $x2 = $x;
  11825. $y2 = $y - $dh;
  11826. $x3 = $x + $dw;
  11827. $y3 = $y;
  11828. $x4 = $x + $w;
  11829. $y4 = $y;
  11830. break;
  11831. }
  11832. case 'C': {
  11833. $x1 = $x - $w;
  11834. $y1 = $y;
  11835. $x2 = $x - $dw;
  11836. $y2 = $y;
  11837. $x3 = $x;
  11838. $y3 = $y + $dh;
  11839. $x4 = $x;
  11840. $y4 = $y + $h;
  11841. break;
  11842. }
  11843. case 'D': {
  11844. $x1 = $x + $dw;
  11845. $y1 = $y;
  11846. $x2 = $x + $w;
  11847. $y2 = $y;
  11848. $x3 = $x;
  11849. $y3 = $y + $dh;
  11850. $x4 = $x;
  11851. $y4 = $y + $h;
  11852. break;
  11853. }
  11854. }
  11855. $this->Line($x1, $y1, $x2, $y2);
  11856. $this->Line($x3, $y3, $x4, $y4);
  11857. }
  11858. }
  11859. /**
  11860. * Paints a registration mark
  11861. * @param float $x abscissa of the registration mark center.
  11862. * @param float $y ordinate of the registration mark center.
  11863. * @param float $r radius of the crop mark.
  11864. * @param boolean $double if true print two concentric crop marks.
  11865. * @param array $cola crop mark color (default black).
  11866. * @param array $colb second crop mark color.
  11867. * @author Nicola Asuni
  11868. * @since 4.9.000 (2010-03-26)
  11869. * @access public
  11870. */
  11871. public function registrationMark($x, $y, $r, $double=false, $cola=array(0,0,0), $colb=array(255,255,255)) {
  11872. $line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  11873. $this->SetFillColorArray($cola);
  11874. $this->PieSector($x, $y, $r, 90, 180, 'F');
  11875. $this->PieSector($x, $y, $r, 270, 360, 'F');
  11876. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  11877. if ($double) {
  11878. $r2 = $r * 0.5;
  11879. $this->SetFillColorArray($colb);
  11880. $this->PieSector($x, $y, $r2, 90, 180, 'F');
  11881. $this->PieSector($x, $y, $r2, 270, 360, 'F');
  11882. $this->SetFillColorArray($cola);
  11883. $this->PieSector($x, $y, $r2, 0, 90, 'F');
  11884. $this->PieSector($x, $y, $r2, 180, 270, 'F');
  11885. $this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
  11886. }
  11887. }
  11888. /**
  11889. * Paints a linear colour gradient.
  11890. * @param float $x abscissa of the top left corner of the rectangle.
  11891. * @param float $y ordinate of the top left corner of the rectangle.
  11892. * @param float $w width of the rectangle.
  11893. * @param float $h height of the rectangle.
  11894. * @param array $col1 first color (Grayscale, RGB or CMYK components).
  11895. * @param array $col2 second color (Grayscale, RGB or CMYK components).
  11896. * @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).
  11897. * @author Andreas Würmser, Nicola Asuni
  11898. * @since 3.1.000 (2008-06-09)
  11899. * @access public
  11900. */
  11901. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  11902. $this->Clip($x, $y, $w, $h);
  11903. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  11904. }
  11905. /**
  11906. * Paints a radial colour gradient.
  11907. * @param float $x abscissa of the top left corner of the rectangle.
  11908. * @param float $y ordinate of the top left corner of the rectangle.
  11909. * @param float $w width of the rectangle.
  11910. * @param float $h height of the rectangle.
  11911. * @param array $col1 first color (Grayscale, RGB or CMYK components).
  11912. * @param array $col2 second color (Grayscale, RGB or CMYK components).
  11913. * @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.
  11914. * @author Andreas Würmser, Nicola Asuni
  11915. * @since 3.1.000 (2008-06-09)
  11916. * @access public
  11917. */
  11918. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  11919. $this->Clip($x, $y, $w, $h);
  11920. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  11921. }
  11922. /**
  11923. * Paints a coons patch mesh.
  11924. * @param float $x abscissa of the top left corner of the rectangle.
  11925. * @param float $y ordinate of the top left corner of the rectangle.
  11926. * @param float $w width of the rectangle.
  11927. * @param float $h height of the rectangle.
  11928. * @param array $col1 first color (lower left corner) (RGB components).
  11929. * @param array $col2 second color (lower right corner) (RGB components).
  11930. * @param array $col3 third color (upper right corner) (RGB components).
  11931. * @param array $col4 fourth color (upper left corner) (RGB components).
  11932. * @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>
  11933. * @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
  11934. * @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
  11935. * @param boolean $antialias A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  11936. * @author Andreas Würmser, Nicola Asuni
  11937. * @since 3.1.000 (2008-06-09)
  11938. * @access public
  11939. */
  11940. 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, $antialias=false) {
  11941. $this->Clip($x, $y, $w, $h);
  11942. $n = count($this->gradients) + 1;
  11943. $this->gradients[$n] = array();
  11944. $this->gradients[$n]['type'] = 6; //coons patch mesh
  11945. $this->gradients[$n]['coords'] = array();
  11946. $this->gradients[$n]['antialias'] = $antialias;
  11947. $this->gradients[$n]['colors'] = array();
  11948. $this->gradients[$n]['transparency'] = false;
  11949. //check the coords array if it is the simple array or the multi patch array
  11950. if (!isset($coords[0]['f'])) {
  11951. //simple array -> convert to multi patch array
  11952. if (!isset($col1[1])) {
  11953. $col1[1] = $col1[2] = $col1[0];
  11954. }
  11955. if (!isset($col2[1])) {
  11956. $col2[1] = $col2[2] = $col2[0];
  11957. }
  11958. if (!isset($col3[1])) {
  11959. $col3[1] = $col3[2] = $col3[0];
  11960. }
  11961. if (!isset($col4[1])) {
  11962. $col4[1] = $col4[2] = $col4[0];
  11963. }
  11964. $patch_array[0]['f'] = 0;
  11965. $patch_array[0]['points'] = $coords;
  11966. $patch_array[0]['colors'][0]['r'] = $col1[0];
  11967. $patch_array[0]['colors'][0]['g'] = $col1[1];
  11968. $patch_array[0]['colors'][0]['b'] = $col1[2];
  11969. $patch_array[0]['colors'][1]['r'] = $col2[0];
  11970. $patch_array[0]['colors'][1]['g'] = $col2[1];
  11971. $patch_array[0]['colors'][1]['b'] = $col2[2];
  11972. $patch_array[0]['colors'][2]['r'] = $col3[0];
  11973. $patch_array[0]['colors'][2]['g'] = $col3[1];
  11974. $patch_array[0]['colors'][2]['b'] = $col3[2];
  11975. $patch_array[0]['colors'][3]['r'] = $col4[0];
  11976. $patch_array[0]['colors'][3]['g'] = $col4[1];
  11977. $patch_array[0]['colors'][3]['b'] = $col4[2];
  11978. } else {
  11979. //multi patch array
  11980. $patch_array = $coords;
  11981. }
  11982. $bpcd = 65535; //16 bits per coordinate
  11983. //build the data stream
  11984. $this->gradients[$n]['stream'] = '';
  11985. $count_patch = count($patch_array);
  11986. for ($i=0; $i < $count_patch; ++$i) {
  11987. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  11988. $count_points = count($patch_array[$i]['points']);
  11989. for ($j=0; $j < $count_points; ++$j) {
  11990. //each point as 16 bit
  11991. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  11992. if ($patch_array[$i]['points'][$j] < 0) {
  11993. $patch_array[$i]['points'][$j] = 0;
  11994. }
  11995. if ($patch_array[$i]['points'][$j] > $bpcd) {
  11996. $patch_array[$i]['points'][$j] = $bpcd;
  11997. }
  11998. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  11999. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  12000. }
  12001. $count_cols = count($patch_array[$i]['colors']);
  12002. for ($j=0; $j < $count_cols; ++$j) {
  12003. //each color component as 8 bit
  12004. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  12005. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  12006. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  12007. }
  12008. }
  12009. //paint the gradient
  12010. $this->_out('/Sh'.$n.' sh');
  12011. //restore previous Graphic State
  12012. $this->_out('Q');
  12013. }
  12014. /**
  12015. * Set a rectangular clipping area.
  12016. * @param float $x abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  12017. * @param float $y ordinate of the top left corner of the rectangle.
  12018. * @param float $w width of the rectangle.
  12019. * @param float $h height of the rectangle.
  12020. * @author Andreas Würmser, Nicola Asuni
  12021. * @since 3.1.000 (2008-06-09)
  12022. * @access protected
  12023. */
  12024. protected function Clip($x, $y, $w, $h) {
  12025. if ($this->rtl) {
  12026. $x = $this->w - $x - $w;
  12027. }
  12028. //save current Graphic State
  12029. $s = 'q';
  12030. //set clipping area
  12031. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  12032. //set up transformation matrix for gradient
  12033. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  12034. $this->_out($s);
  12035. }
  12036. /**
  12037. * Output gradient.
  12038. * @param int $type type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
  12039. * @param array $coords array of coordinates.
  12040. * @param array $stops array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
  12041. * @param array $background An array of colour components appropriate to the colour space, specifying a single background colour value.
  12042. * @param boolean $antialias A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  12043. * @author Nicola Asuni
  12044. * @since 3.1.000 (2008-06-09)
  12045. * @access public
  12046. */
  12047. public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
  12048. $n = count($this->gradients) + 1;
  12049. $this->gradients[$n] = array();
  12050. $this->gradients[$n]['type'] = $type;
  12051. $this->gradients[$n]['coords'] = $coords;
  12052. $this->gradients[$n]['antialias'] = $antialias;
  12053. $this->gradients[$n]['colors'] = array();
  12054. $this->gradients[$n]['transparency'] = false;
  12055. // color space
  12056. $numcolspace = count($stops[0]['color']);
  12057. $bcolor = array_values($background);
  12058. switch($numcolspace) {
  12059. case 4: { // CMYK
  12060. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  12061. if (!empty($background)) {
  12062. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F %.3F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
  12063. }
  12064. break;
  12065. }
  12066. case 3: { // RGB
  12067. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  12068. if (!empty($background)) {
  12069. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
  12070. }
  12071. break;
  12072. }
  12073. case 1: { // Gray scale
  12074. $this->gradients[$n]['colspace'] = 'DeviceGray';
  12075. if (!empty($background)) {
  12076. $this->gradients[$n]['background'] = sprintf('%.3F', $bcolor[0]/255);
  12077. }
  12078. break;
  12079. }
  12080. }
  12081. $num_stops = count($stops);
  12082. $last_stop_id = $num_stops - 1;
  12083. foreach ($stops as $key => $stop) {
  12084. $this->gradients[$n]['colors'][$key] = array();
  12085. // offset represents a location along the gradient vector
  12086. if (isset($stop['offset'])) {
  12087. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  12088. } else {
  12089. if ($key == 0) {
  12090. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  12091. } elseif ($key == $last_stop_id) {
  12092. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  12093. } else {
  12094. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  12095. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  12096. }
  12097. }
  12098. if (isset($stop['opacity'])) {
  12099. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  12100. if ($stop['opacity'] < 1) {
  12101. $this->gradients[$n]['transparency'] = true;
  12102. }
  12103. } else {
  12104. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  12105. }
  12106. // exponent for the exponential interpolation function
  12107. if (isset($stop['exponent'])) {
  12108. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  12109. } else {
  12110. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  12111. }
  12112. // set colors
  12113. $color = array_values($stop['color']);
  12114. switch($numcolspace) {
  12115. case 4: { // CMYK
  12116. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F %.3F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
  12117. break;
  12118. }
  12119. case 3: { // RGB
  12120. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  12121. break;
  12122. }
  12123. case 1: { // Gray scale
  12124. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F', $color[0]/255);
  12125. break;
  12126. }
  12127. }
  12128. }
  12129. if ($this->gradients[$n]['transparency']) {
  12130. // paint luminosity gradient
  12131. $this->_out('/TGS'.$n.' gs');
  12132. }
  12133. //paint the gradient
  12134. $this->_out('/Sh'.$n.' sh');
  12135. //restore previous Graphic State
  12136. $this->_out('Q');
  12137. }
  12138. /**
  12139. * Output gradient shaders.
  12140. * @author Nicola Asuni
  12141. * @since 3.1.000 (2008-06-09)
  12142. * @access protected
  12143. */
  12144. function _putshaders() {
  12145. $idt = count($this->gradients); //index for transparency gradients
  12146. foreach ($this->gradients as $id => $grad) {
  12147. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  12148. $this->_newobj();
  12149. $fc = $this->n;
  12150. $out = '<<';
  12151. $out .= ' /FunctionType 3';
  12152. $out .= ' /Domain [0 1]';
  12153. $functions = '';
  12154. $bounds = '';
  12155. $encode = '';
  12156. $i = 1;
  12157. $num_cols = count($grad['colors']);
  12158. $lastcols = $num_cols - 1;
  12159. for ($i = 1; $i < $num_cols; ++$i) {
  12160. $functions .= ($fc + $i).' 0 R ';
  12161. if ($i < $lastcols) {
  12162. $bounds .= sprintf('%.3F ', $grad['colors'][$i]['offset']);
  12163. }
  12164. $encode .= '0 1 ';
  12165. }
  12166. $out .= ' /Functions ['.trim($functions).']';
  12167. $out .= ' /Bounds ['.trim($bounds).']';
  12168. $out .= ' /Encode ['.trim($encode).']';
  12169. $out .= ' >>';
  12170. $out .= ' endobj';
  12171. $this->_out($out);
  12172. for ($i = 1; $i < $num_cols; ++$i) {
  12173. $this->_newobj();
  12174. $out = '<<';
  12175. $out .= ' /FunctionType 2';
  12176. $out .= ' /Domain [0 1]';
  12177. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
  12178. $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
  12179. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  12180. $out .= ' >>';
  12181. $out .= ' endobj';
  12182. $this->_out($out);
  12183. }
  12184. // set transparency fuctions
  12185. if ($grad['transparency']) {
  12186. $this->_newobj();
  12187. $ft = $this->n;
  12188. $out = '<<';
  12189. $out .= ' /FunctionType 3';
  12190. $out .= ' /Domain [0 1]';
  12191. $functions = '';
  12192. $i = 1;
  12193. $num_cols = count($grad['colors']);
  12194. for ($i = 1; $i < $num_cols; ++$i) {
  12195. $functions .= ($ft + $i).' 0 R ';
  12196. }
  12197. $out .= ' /Functions ['.trim($functions).']';
  12198. $out .= ' /Bounds ['.trim($bounds).']';
  12199. $out .= ' /Encode ['.trim($encode).']';
  12200. $out .= ' >>';
  12201. $out .= ' endobj';
  12202. $this->_out($out);
  12203. for ($i = 1; $i < $num_cols; ++$i) {
  12204. $this->_newobj();
  12205. $out = '<<';
  12206. $out .= ' /FunctionType 2';
  12207. $out .= ' /Domain [0 1]';
  12208. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
  12209. $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
  12210. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  12211. $out .= ' >>';
  12212. $out .= ' endobj';
  12213. $this->_out($out);
  12214. }
  12215. }
  12216. }
  12217. // set shading object
  12218. $this->_newobj();
  12219. $out = '<< /ShadingType '.$grad['type'];
  12220. if (isset($grad['colspace'])) {
  12221. $out .= ' /ColorSpace /'.$grad['colspace'];
  12222. } else {
  12223. $out .= ' /ColorSpace /DeviceRGB';
  12224. }
  12225. if (isset($grad['background']) AND !empty($grad['background'])) {
  12226. $out .= ' /Background ['.$grad['background'].']';
  12227. }
  12228. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  12229. $out .= ' /AntiAlias true';
  12230. }
  12231. if ($grad['type'] == 2) {
  12232. $out .= ' '.sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  12233. $out .= ' /Domain [0 1]';
  12234. $out .= ' /Function '.$fc.' 0 R';
  12235. $out .= ' /Extend [true true]';
  12236. $out .= ' >>';
  12237. } elseif ($grad['type'] == 3) {
  12238. //x0, y0, r0, x1, y1, r1
  12239. //at this this time radius of inner circle is 0
  12240. $out .= ' '.sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  12241. $out .= ' /Domain [0 1]';
  12242. $out .= ' /Function '.$fc.' 0 R';
  12243. $out .= ' /Extend [true true]';
  12244. $out .= ' >>';
  12245. } elseif ($grad['type'] == 6) {
  12246. $out .= ' /BitsPerCoordinate 16';
  12247. $out .= ' /BitsPerComponent 8';
  12248. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  12249. $out .= ' /BitsPerFlag 8';
  12250. $out .= ' /Length '.strlen($grad['stream']);
  12251. $out .= ' >>';
  12252. $out .= ' '.$this->_getstream($grad['stream']);
  12253. }
  12254. $out .= ' endobj';
  12255. $this->_out($out);
  12256. if ($grad['transparency']) {
  12257. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  12258. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
  12259. }
  12260. $this->gradients[$id]['id'] = $this->n;
  12261. // set pattern object
  12262. $this->_newobj();
  12263. $out = '<< /Type /Pattern /PatternType 2';
  12264. $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
  12265. $out .= ' >> endobj';
  12266. $this->_out($out);
  12267. $this->gradients[$id]['pattern'] = $this->n;
  12268. // set shading and pattern for transparency mask
  12269. if ($grad['transparency']) {
  12270. // luminosity pattern
  12271. $idgs = $id + $idt;
  12272. $this->_newobj();
  12273. $this->_out($shading_transparency);
  12274. $this->gradients[$idgs]['id'] = $this->n;
  12275. $this->_newobj();
  12276. $out = '<< /Type /Pattern /PatternType 2';
  12277. $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
  12278. $out .= ' >> endobj';
  12279. $this->_out($out);
  12280. $this->gradients[$idgs]['pattern'] = $this->n;
  12281. // luminosity XObject
  12282. $this->_newobj();
  12283. $filter = ($this->compress)?' /Filter /FlateDecode':'';
  12284. $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
  12285. $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
  12286. $out .= ' /Length '.strlen($stream);
  12287. $out .= ' /BBox [0 0 '.$this->wPt.' '.$this->hPt.']';
  12288. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  12289. $out .= ' /Resources <<';
  12290. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  12291. $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
  12292. $out .= ' >>';
  12293. $out .= ' >> ';
  12294. $out .= $this->_getstream($stream);
  12295. $out .= ' endobj';
  12296. $this->_out($out);
  12297. // SMask
  12298. $this->_newobj();
  12299. $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >> endobj';
  12300. $this->_out($out);
  12301. // ExtGState
  12302. $this->_newobj();
  12303. $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >> endobj';
  12304. $this->_out($out);
  12305. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
  12306. }
  12307. }
  12308. }
  12309. /**
  12310. * Draw the sector of a circle.
  12311. * It can be used for instance to render pie charts.
  12312. * @param float $xc abscissa of the center.
  12313. * @param float $yc ordinate of the center.
  12314. * @param float $r radius.
  12315. * @param float $a start angle (in degrees).
  12316. * @param float $b end angle (in degrees).
  12317. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  12318. * @param float $cw: indicates whether to go clockwise (default: true).
  12319. * @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.
  12320. * @author Maxime Delorme, Nicola Asuni
  12321. * @since 3.1.000 (2008-06-09)
  12322. * @access public
  12323. */
  12324. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  12325. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  12326. }
  12327. /**
  12328. * Draw the sector of an ellipse.
  12329. * It can be used for instance to render pie charts.
  12330. * @param float $xc abscissa of the center.
  12331. * @param float $yc ordinate of the center.
  12332. * @param float $rx the x-axis radius.
  12333. * @param float $ry the y-axis radius.
  12334. * @param float $a start angle (in degrees).
  12335. * @param float $b end angle (in degrees).
  12336. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  12337. * @param float $cw: indicates whether to go clockwise.
  12338. * @param float $o: origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  12339. * @param integer $nc Number of curves used to draw a 90 degrees portion of arc.
  12340. * @author Maxime Delorme, Nicola Asuni
  12341. * @since 3.1.000 (2008-06-09)
  12342. * @access public
  12343. */
  12344. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
  12345. if ($this->rtl) {
  12346. $xc = $this->w - $xc;
  12347. }
  12348. $op = $this->getPathPaintOperator($style);
  12349. if ($op == 'f') {
  12350. $line_style = array();
  12351. }
  12352. if ($cw) {
  12353. $d = $b;
  12354. $b = 360 - $a + $o;
  12355. $a = 360 - $d + $o;
  12356. } else {
  12357. $b += $o;
  12358. $a += $o;
  12359. }
  12360. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  12361. $this->_out($op);
  12362. }
  12363. /**
  12364. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  12365. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of SVG images using ImageMagick library.
  12366. * Only vector drawing is supported, not text or bitmap.
  12367. * 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).
  12368. * @param string $file Name of the file containing the image.
  12369. * @param float $x Abscissa of the upper-left corner.
  12370. * @param float $y Ordinate of the upper-left corner.
  12371. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  12372. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  12373. * @param mixed $link URL or identifier returned by AddLink().
  12374. * @param boolean useBoundingBox specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  12375. * @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>
  12376. * @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>
  12377. * @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>
  12378. * @param boolean $fitonpage if true the image is resized to not exceed page dimensions.
  12379. * @author Valentin Schmidt, Nicola Asuni
  12380. * @since 3.1.000 (2008-06-09)
  12381. * @access public
  12382. */
  12383. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false) {
  12384. if ($this->rasterize_vector_images) {
  12385. // convert SVG to raster image using GD or ImageMagick libraries
  12386. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  12387. }
  12388. if ($x === '') {
  12389. $x = $this->x;
  12390. }
  12391. if ($y === '') {
  12392. $y = $this->y;
  12393. }
  12394. $k = $this->k;
  12395. $data = file_get_contents($file);
  12396. if ($data === false) {
  12397. $this->Error('EPS file not found: '.$file);
  12398. }
  12399. $regs = array();
  12400. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  12401. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  12402. if (count($regs) > 1) {
  12403. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  12404. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  12405. $versexp = explode(' ', $version_str);
  12406. $version = (float)array_pop($versexp);
  12407. if ($version >= 9) {
  12408. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  12409. }
  12410. }
  12411. }
  12412. // strip binary bytes in front of PS-header
  12413. $start = strpos($data, '%!PS-Adobe');
  12414. if ($start > 0) {
  12415. $data = substr($data, $start);
  12416. }
  12417. // find BoundingBox params
  12418. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  12419. if (count($regs) > 1) {
  12420. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  12421. } else {
  12422. $this->Error('No BoundingBox found in EPS file: '.$file);
  12423. }
  12424. $start = strpos($data, '%%EndSetup');
  12425. if ($start === false) {
  12426. $start = strpos($data, '%%EndProlog');
  12427. }
  12428. if ($start === false) {
  12429. $start = strpos($data, '%%BoundingBox');
  12430. }
  12431. $data = substr($data, $start);
  12432. $end = strpos($data, '%%PageTrailer');
  12433. if ($end===false) {
  12434. $end = strpos($data, 'showpage');
  12435. }
  12436. if ($end) {
  12437. $data = substr($data, 0, $end);
  12438. }
  12439. // calculate image width and height on document
  12440. if (($w <= 0) AND ($h <= 0)) {
  12441. $w = ($x2 - $x1) / $k;
  12442. $h = ($y2 - $y1) / $k;
  12443. } elseif ($w <= 0) {
  12444. $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
  12445. } elseif ($h <= 0) {
  12446. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  12447. }
  12448. // Check whether we need a new page first as this does not fit
  12449. $prev_x = $this->x;
  12450. if ($this->checkPageBreak($h, $y)) {
  12451. $y = $this->y;
  12452. if ($this->rtl) {
  12453. $x += ($prev_x - $this->x);
  12454. } else {
  12455. $x += ($this->x - $prev_x);
  12456. }
  12457. }
  12458. // resize image to be contained on a single page
  12459. if ($fitonpage) {
  12460. $ratio_wh = $w / $h;
  12461. if (($y + $h) > $this->PageBreakTrigger) {
  12462. $h = $this->PageBreakTrigger - $y;
  12463. $w = $h * $ratio_wh;
  12464. }
  12465. if (($x + $w) > ($this->w - $this->rMargin)) {
  12466. $w = $this->w - $this->rMargin - $x;
  12467. $h = $w / $ratio_wh;
  12468. }
  12469. }
  12470. // set scaling factors
  12471. $scale_x = $w / (($x2 - $x1) / $k);
  12472. $scale_y = $h / (($y2 - $y1) / $k);
  12473. // set alignment
  12474. $this->img_rb_y = $y + $h;
  12475. // set alignment
  12476. if ($this->rtl) {
  12477. if ($palign == 'L') {
  12478. $ximg = $this->lMargin;
  12479. } elseif ($palign == 'C') {
  12480. $ximg = ($this->w - $w) / 2;
  12481. } elseif ($palign == 'R') {
  12482. $ximg = $this->w - $this->rMargin - $w;
  12483. } else {
  12484. $ximg = $this->w - $x - $w;
  12485. }
  12486. $this->img_rb_x = $ximg;
  12487. } else {
  12488. if ($palign == 'L') {
  12489. $ximg = $this->lMargin;
  12490. } elseif ($palign == 'C') {
  12491. $ximg = ($this->w - $w) / 2;
  12492. } elseif ($palign == 'R') {
  12493. $ximg = $this->w - $this->rMargin - $w;
  12494. } else {
  12495. $ximg = $x;
  12496. }
  12497. $this->img_rb_x = $ximg + $w;
  12498. }
  12499. if ($useBoundingBox) {
  12500. $dx = $ximg * $k - $x1;
  12501. $dy = $y * $k - $y1;
  12502. } else {
  12503. $dx = $ximg * $k;
  12504. $dy = $y * $k;
  12505. }
  12506. // save the current graphic state
  12507. $this->_out('q'.$this->epsmarker);
  12508. // translate
  12509. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  12510. // scale
  12511. if (isset($scale_x)) {
  12512. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  12513. }
  12514. // handle pc/unix/mac line endings
  12515. preg_match('/[\r\n]+/s', $data, $regs);
  12516. $lines = explode($regs[0], $data);
  12517. $u=0;
  12518. $cnt = count($lines);
  12519. for ($i=0; $i < $cnt; ++$i) {
  12520. $line = $lines[$i];
  12521. if (($line == '') OR ($line{0} == '%')) {
  12522. continue;
  12523. }
  12524. $len = strlen($line);
  12525. $chunks = explode(' ', $line);
  12526. $cmd = array_pop($chunks);
  12527. // RGB
  12528. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  12529. $b = array_pop($chunks);
  12530. $g = array_pop($chunks);
  12531. $r = array_pop($chunks);
  12532. $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!
  12533. continue;
  12534. }
  12535. switch ($cmd) {
  12536. case 'm':
  12537. case 'l':
  12538. case 'v':
  12539. case 'y':
  12540. case 'c':
  12541. case 'k':
  12542. case 'K':
  12543. case 'g':
  12544. case 'G':
  12545. case 's':
  12546. case 'S':
  12547. case 'J':
  12548. case 'j':
  12549. case 'w':
  12550. case 'M':
  12551. case 'd':
  12552. case 'n': {
  12553. $this->_out($line);
  12554. break;
  12555. }
  12556. case 'x': {// custom fill color
  12557. list($c,$m,$y,$k) = $chunks;
  12558. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' k');
  12559. break;
  12560. }
  12561. case 'X': { // custom stroke color
  12562. list($c,$m,$y,$k) = $chunks;
  12563. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' K');
  12564. break;
  12565. }
  12566. case 'Y':
  12567. case 'N':
  12568. case 'V':
  12569. case 'L':
  12570. case 'C': {
  12571. $line{$len-1} = strtolower($cmd);
  12572. $this->_out($line);
  12573. break;
  12574. }
  12575. case 'b':
  12576. case 'B': {
  12577. $this->_out($cmd . '*');
  12578. break;
  12579. }
  12580. case 'f':
  12581. case 'F': {
  12582. if ($u > 0) {
  12583. $isU = false;
  12584. $max = min($i+5, $cnt);
  12585. for ($j=$i+1; $j < $max; ++$j) {
  12586. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  12587. }
  12588. if ($isU) {
  12589. $this->_out('f*');
  12590. }
  12591. } else {
  12592. $this->_out('f*');
  12593. }
  12594. break;
  12595. }
  12596. case '*u': {
  12597. ++$u;
  12598. break;
  12599. }
  12600. case '*U': {
  12601. --$u;
  12602. break;
  12603. }
  12604. }
  12605. }
  12606. // restore previous graphic state
  12607. $this->_out($this->epsmarker.'Q');
  12608. if (!empty($border)) {
  12609. $bx = $x;
  12610. $by = $y;
  12611. $this->x = $ximg;
  12612. if ($this->rtl) {
  12613. $this->x += $w;
  12614. }
  12615. $this->y = $y;
  12616. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  12617. $this->x = $bx;
  12618. $this->y = $by;
  12619. }
  12620. if ($link) {
  12621. $this->Link($ximg, $y, $w, $h, $link, 0);
  12622. }
  12623. // set pointer to align the successive text/objects
  12624. switch($align) {
  12625. case 'T':{
  12626. $this->y = $y;
  12627. $this->x = $this->img_rb_x;
  12628. break;
  12629. }
  12630. case 'M':{
  12631. $this->y = $y + round($h/2);
  12632. $this->x = $this->img_rb_x;
  12633. break;
  12634. }
  12635. case 'B':{
  12636. $this->y = $this->img_rb_y;
  12637. $this->x = $this->img_rb_x;
  12638. break;
  12639. }
  12640. case 'N':{
  12641. $this->SetY($this->img_rb_y);
  12642. break;
  12643. }
  12644. default:{
  12645. break;
  12646. }
  12647. }
  12648. $this->endlinex = $this->img_rb_x;
  12649. }
  12650. /**
  12651. * Set document barcode.
  12652. * @param string $bc barcode
  12653. * @access public
  12654. */
  12655. public function setBarcode($bc='') {
  12656. $this->barcode = $bc;
  12657. }
  12658. /**
  12659. * Get current barcode.
  12660. * @return string
  12661. * @access public
  12662. * @since 4.0.012 (2008-07-24)
  12663. */
  12664. public function getBarcode() {
  12665. return $this->barcode;
  12666. }
  12667. /**
  12668. * Print a Linear Barcode.
  12669. * @param string $code code to print
  12670. * @param string $type type of barcode (see barcodes.php for supported formats).
  12671. * @param int $x x position in user units
  12672. * @param int $y y position in user units
  12673. * @param int $w width in user units
  12674. * @param int $h height in user units
  12675. * @param float $xres width of the smallest bar in user units
  12676. * @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 (set to 'auto' for automatic padding)</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>
  12677. * @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>
  12678. * @author Nicola Asuni
  12679. * @since 3.1.000 (2008-06-09)
  12680. * @access public
  12681. */
  12682. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='') {
  12683. if ($this->empty_string($code)) {
  12684. return;
  12685. }
  12686. require_once(dirname(__FILE__).'/barcodes.php');
  12687. // save current graphic settings
  12688. $gvars = $this->getGraphicVars();
  12689. // create new barcode object
  12690. $barcodeobj = new TCPDFBarcode($code, $type);
  12691. $arrcode = $barcodeobj->getBarcodeArray();
  12692. if ($arrcode === false) {
  12693. $this->Error('Error in 1D barcode string');
  12694. }
  12695. // set default values
  12696. if (!isset($style['position'])) {
  12697. if ($this->rtl) {
  12698. $style['position'] = 'R';
  12699. } else {
  12700. $style['position'] = 'L';
  12701. }
  12702. }
  12703. if (!isset($style['fgcolor'])) {
  12704. $style['fgcolor'] = array(0,0,0); // default black
  12705. }
  12706. if (!isset($style['bgcolor'])) {
  12707. $style['bgcolor'] = false; // default transparent
  12708. }
  12709. if (!isset($style['border'])) {
  12710. $style['border'] = false;
  12711. }
  12712. $fontsize = 0;
  12713. if (!isset($style['text'])) {
  12714. $style['text'] = false;
  12715. }
  12716. if ($style['text'] AND isset($style['font'])) {
  12717. if (isset($style['fontsize'])) {
  12718. $fontsize = $style['fontsize'];
  12719. }
  12720. $this->SetFont($style['font'], '', $fontsize);
  12721. }
  12722. if (!isset($style['stretchtext'])) {
  12723. $style['stretchtext'] = 4;
  12724. }
  12725. // set foreground color
  12726. $this->SetDrawColorArray($style['fgcolor']);
  12727. $this->SetTextColorArray($style['fgcolor']);
  12728. if ($this->empty_string($w) OR ($w <= 0)) {
  12729. if ($this->rtl) {
  12730. $w = $this->x - $this->lMargin;
  12731. } else {
  12732. $w = $this->w - $this->rMargin - $this->x;
  12733. }
  12734. }
  12735. if ($this->empty_string($x)) {
  12736. $x = $this->GetX();
  12737. }
  12738. if ($this->rtl) {
  12739. $x = $this->w - $x;
  12740. }
  12741. if ($this->empty_string($y)) {
  12742. $y = $this->GetY();
  12743. }
  12744. if ($this->empty_string($xres)) {
  12745. $xres = 0.4;
  12746. }
  12747. if ($this->empty_string($h) OR ($h <= 0)) {
  12748. $h = $w / 3;
  12749. }
  12750. // padding
  12751. if (!isset($style['padding'])) {
  12752. $style['padding'] = 0;
  12753. } elseif ($style['padding'] === 'auto') {
  12754. $style['padding'] = $h / 4;
  12755. }
  12756. $fbw = ($arrcode['maxw'] * $xres) + (2 * $style['padding']);
  12757. $extraspace = ($this->cell_height_ratio * $fontsize / $this->k) + (2 * $style['padding']);
  12758. $prev_x = $this->x;
  12759. // maximum bar height
  12760. $barh = $h;
  12761. $h += $extraspace;
  12762. if ($this->checkPageBreak($h, $y)) {
  12763. $y = $this->GetY() + $this->cMargin;
  12764. if ($this->rtl) {
  12765. $x += ($prev_x - $this->x);
  12766. } else {
  12767. $x += ($this->x - $prev_x);
  12768. }
  12769. }
  12770. switch ($style['position']) {
  12771. case 'L': { // left
  12772. if ($this->rtl) {
  12773. $xpos = $x - $w;
  12774. } else {
  12775. $xpos = $x;
  12776. }
  12777. break;
  12778. }
  12779. case 'C': { // center
  12780. $xdiff = (($w - $fbw) / 2);
  12781. if ($this->rtl) {
  12782. $xpos = $x - $w + $xdiff;
  12783. } else {
  12784. $xpos = $x + $xdiff;
  12785. }
  12786. break;
  12787. }
  12788. case 'R': { // right
  12789. if ($this->rtl) {
  12790. $xpos = $x - $fbw;
  12791. } else {
  12792. $xpos = $x + $w - $fbw;
  12793. }
  12794. break;
  12795. }
  12796. case 'S': { // stretch
  12797. $fbw = $w;
  12798. $xres = ($w - (2 * $style['padding'])) / $arrcode['maxw'];
  12799. if ($this->rtl) {
  12800. $xpos = $x - $w;
  12801. } else {
  12802. $xpos = $x;
  12803. }
  12804. break;
  12805. }
  12806. }
  12807. $xpos_rect = $xpos;
  12808. $xpos = $xpos_rect + $style['padding'];
  12809. $xpos_text = $xpos;
  12810. // barcode is always printed in LTR direction
  12811. $tempRTL = $this->rtl;
  12812. $this->rtl = false;
  12813. // print background color
  12814. if ($style['bgcolor']) {
  12815. $this->Rect($xpos_rect, $y, $fbw, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  12816. } elseif ($style['border']) {
  12817. $this->Rect($xpos_rect, $y, $fbw, $h, 'D');
  12818. }
  12819. // print bars
  12820. if ($arrcode !== false) {
  12821. foreach ($arrcode['bcode'] as $k => $v) {
  12822. $bw = ($v['w'] * $xres);
  12823. if ($v['t']) {
  12824. // draw a vertical bar
  12825. $ypos = $y + $style['padding'] + ($v['p'] * $barh / $arrcode['maxh']);
  12826. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  12827. }
  12828. $xpos += $bw;
  12829. }
  12830. }
  12831. // print text
  12832. if ($style['text']) {
  12833. // print text
  12834. $this->x = $xpos_text;
  12835. $this->y = $y + $style['padding'] + $barh;
  12836. $this->Cell(($arrcode['maxw'] * $xres), ($this->cell_height_ratio * $fontsize / $this->k), $code, 0, 0, 'C', 0, '', $style['stretchtext']);
  12837. }
  12838. // restore original direction
  12839. $this->rtl = $tempRTL;
  12840. // restore previous settings
  12841. $this->setGraphicVars($gvars);
  12842. // set bottomcoordinates
  12843. $this->img_rb_y = $y + $h;
  12844. if ($this->rtl) {
  12845. // set left side coordinate
  12846. $this->img_rb_x = ($this->w - $x - $w);
  12847. } else {
  12848. // set right side coordinate
  12849. $this->img_rb_x = $x + $w;
  12850. }
  12851. // set pointer to align the successive text/objects
  12852. switch($align) {
  12853. case 'T':{
  12854. $this->y = $y;
  12855. $this->x = $this->img_rb_x;
  12856. break;
  12857. }
  12858. case 'M':{
  12859. $this->y = $y + round($h/2);
  12860. $this->x = $this->img_rb_x;
  12861. break;
  12862. }
  12863. case 'B':{
  12864. $this->y = $this->img_rb_y;
  12865. $this->x = $this->img_rb_x;
  12866. break;
  12867. }
  12868. case 'N':{
  12869. $this->SetY($this->img_rb_y);
  12870. break;
  12871. }
  12872. default:{
  12873. break;
  12874. }
  12875. }
  12876. }
  12877. /**
  12878. * This function is DEPRECATED, please use the new write1DBarcode() function.
  12879. * @param int $x x position in user units
  12880. * @param int $y y position in user units
  12881. * @param int $w width in user units
  12882. * @param int $h height position in user units
  12883. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  12884. * @param string $style barcode style
  12885. * @param string $font font for text
  12886. * @param int $xres x resolution
  12887. * @param string $code code to print
  12888. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  12889. * @access public
  12890. * @see write1DBarcode()
  12891. */
  12892. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  12893. // convert old settings for the new write1DBarcode() function.
  12894. $xres = 1 / $xres;
  12895. $newstyle = array(
  12896. 'position' => 'L',
  12897. 'border' => false,
  12898. 'padding' => 0,
  12899. 'fgcolor' => array(0,0,0),
  12900. 'bgcolor' => false,
  12901. 'text' => true,
  12902. 'font' => $font,
  12903. 'fontsize' => 8,
  12904. 'stretchtext' => 4
  12905. );
  12906. if ($style & 1) {
  12907. $newstyle['border'] = true;
  12908. }
  12909. if ($style & 2) {
  12910. $newstyle['bgcolor'] = false;
  12911. }
  12912. if ($style & 4) {
  12913. $newstyle['position'] = 'C';
  12914. } elseif ($style & 8) {
  12915. $newstyle['position'] = 'L';
  12916. } elseif ($style & 16) {
  12917. $newstyle['position'] = 'R';
  12918. }
  12919. if ($style & 128) {
  12920. $newstyle['text'] = true;
  12921. }
  12922. if ($style & 256) {
  12923. $newstyle['stretchtext'] = 4;
  12924. }
  12925. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  12926. }
  12927. /**
  12928. * Print 2D Barcode.
  12929. * @param string $code code to print
  12930. * @param string $type type of barcode (see 2dbarcodes.php for supported formats).
  12931. * @param int $x x position in user units
  12932. * @param int $y y position in user units
  12933. * @param int $w width in user units
  12934. * @param int $h height in user units
  12935. * @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 (set to 'auto' for automatic padding)</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>
  12936. * @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>
  12937. * @author Nicola Asuni
  12938. * @since 4.5.037 (2009-04-07)
  12939. * @access public
  12940. */
  12941. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='') {
  12942. if ($this->empty_string($code)) {
  12943. return;
  12944. }
  12945. require_once(dirname(__FILE__).'/2dbarcodes.php');
  12946. // save current graphic settings
  12947. $gvars = $this->getGraphicVars();
  12948. // create new barcode object
  12949. $barcodeobj = new TCPDF2DBarcode($code, $type);
  12950. $arrcode = $barcodeobj->getBarcodeArray();
  12951. if ($arrcode === false) {
  12952. $this->Error('Error in 2D barcode string');
  12953. }
  12954. // set default values
  12955. if (!isset($style['fgcolor'])) {
  12956. $style['fgcolor'] = array(0,0,0); // default black
  12957. }
  12958. if (!isset($style['bgcolor'])) {
  12959. $style['bgcolor'] = false; // default transparent
  12960. }
  12961. if (!isset($style['border'])) {
  12962. $style['border'] = false;
  12963. }
  12964. // set foreground color
  12965. $this->SetDrawColorArray($style['fgcolor']);
  12966. if ($this->empty_string($x)) {
  12967. $x = $this->GetX();
  12968. }
  12969. if ($this->rtl) {
  12970. $x = $this->w - $x;
  12971. }
  12972. if ($this->empty_string($y)) {
  12973. $y = $this->GetY();
  12974. }
  12975. if ($this->empty_string($w) OR ($w <= 0)) {
  12976. if ($this->rtl) {
  12977. $w = $x - $this->lMargin;
  12978. } else {
  12979. $w = $this->w - $this->rMargin - $x;
  12980. }
  12981. }
  12982. if ($this->empty_string($h) OR ($h <= 0)) {
  12983. // 2d barcodes are square by default
  12984. $h = $w;
  12985. }
  12986. $prev_x = $this->x;
  12987. if ($this->checkPageBreak($h, $y)) {
  12988. $y = $this->GetY() + $this->cMargin;
  12989. if ($this->rtl) {
  12990. $x += ($prev_x - $this->x);
  12991. } else {
  12992. $x += ($this->x - $prev_x);
  12993. }
  12994. }
  12995. // padding
  12996. if (!isset($style['padding'])) {
  12997. $style['padding'] = 0;
  12998. } elseif ($style['padding'] === 'auto') {
  12999. $style['padding'] = 4 * $w / (8 + $arrcode['num_cols']);
  13000. }
  13001. // calculate barcode size (excluding padding)
  13002. $bw = $w - (2 * $style['padding']);
  13003. $bh = $h - (2 * $style['padding']);
  13004. // calculate starting coordinates
  13005. if ($this->rtl) {
  13006. $xpos = $x - $w;
  13007. } else {
  13008. $xpos = $x;
  13009. }
  13010. $xpos += $style['padding'];
  13011. $ypos = $y + $style['padding'];
  13012. // barcode is always printed in LTR direction
  13013. $tempRTL = $this->rtl;
  13014. $this->rtl = false;
  13015. // print background color
  13016. if ($style['bgcolor']) {
  13017. $this->Rect($x, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  13018. } elseif ($style['border']) {
  13019. $this->Rect($x, $y, $w, $h, 'D');
  13020. }
  13021. // print barcode cells
  13022. if ($arrcode !== false) {
  13023. $rows = $arrcode['num_rows'];
  13024. $cols = $arrcode['num_cols'];
  13025. // calculate dimension of single barcode cell
  13026. $cw = $bw / $cols;
  13027. $ch = $bh / $rows;
  13028. // for each row
  13029. for ($r = 0; $r < $rows; ++$r) {
  13030. $xr = $xpos;
  13031. // for each column
  13032. for ($c = 0; $c < $cols; ++$c) {
  13033. if ($arrcode['bcode'][$r][$c] == 1) {
  13034. // draw a single barcode cell
  13035. $this->Rect($xr, $ypos, $cw, $ch, 'F', array(), $style['fgcolor']);
  13036. }
  13037. $xr += $cw;
  13038. }
  13039. $ypos += $ch;
  13040. }
  13041. }
  13042. // restore original direction
  13043. $this->rtl = $tempRTL;
  13044. // restore previous settings
  13045. $this->setGraphicVars($gvars);
  13046. // set bottomcoordinates
  13047. $this->img_rb_y = $y + $h;
  13048. if ($this->rtl) {
  13049. // set left side coordinate
  13050. $this->img_rb_x = ($this->w - $x - $w);
  13051. } else {
  13052. // set right side coordinate
  13053. $this->img_rb_x = $x + $w;
  13054. }
  13055. // set pointer to align the successive text/objects
  13056. switch($align) {
  13057. case 'T':{
  13058. $this->y = $y;
  13059. $this->x = $this->img_rb_x;
  13060. break;
  13061. }
  13062. case 'M':{
  13063. $this->y = $y + round($h/2);
  13064. $this->x = $this->img_rb_x;
  13065. break;
  13066. }
  13067. case 'B':{
  13068. $this->y = $this->img_rb_y;
  13069. $this->x = $this->img_rb_x;
  13070. break;
  13071. }
  13072. case 'N':{
  13073. $this->SetY($this->img_rb_y);
  13074. break;
  13075. }
  13076. default:{
  13077. break;
  13078. }
  13079. }
  13080. }
  13081. /**
  13082. * Returns an array containing current margins:
  13083. * <ul>
  13084. <li>$ret['left'] = left margin</li>
  13085. <li>$ret['right'] = right margin</li>
  13086. <li>$ret['top'] = top margin</li>
  13087. <li>$ret['bottom'] = bottom margin</li>
  13088. <li>$ret['header'] = header margin</li>
  13089. <li>$ret['footer'] = footer margin</li>
  13090. <li>$ret['cell'] = cell margin</li>
  13091. * </ul>
  13092. * @return array containing all margins measures
  13093. * @access public
  13094. * @since 3.2.000 (2008-06-23)
  13095. */
  13096. public function getMargins() {
  13097. $ret = array(
  13098. 'left' => $this->lMargin,
  13099. 'right' => $this->rMargin,
  13100. 'top' => $this->tMargin,
  13101. 'bottom' => $this->bMargin,
  13102. 'header' => $this->header_margin,
  13103. 'footer' => $this->footer_margin,
  13104. 'cell' => $this->cMargin,
  13105. );
  13106. return $ret;
  13107. }
  13108. /**
  13109. * Returns an array containing original margins:
  13110. * <ul>
  13111. <li>$ret['left'] = left margin</li>
  13112. <li>$ret['right'] = right margin</li>
  13113. * </ul>
  13114. * @return array containing all margins measures
  13115. * @access public
  13116. * @since 4.0.012 (2008-07-24)
  13117. */
  13118. public function getOriginalMargins() {
  13119. $ret = array(
  13120. 'left' => $this->original_lMargin,
  13121. 'right' => $this->original_rMargin
  13122. );
  13123. return $ret;
  13124. }
  13125. /**
  13126. * Returns the current font size.
  13127. * @return current font size
  13128. * @access public
  13129. * @since 3.2.000 (2008-06-23)
  13130. */
  13131. public function getFontSize() {
  13132. return $this->FontSize;
  13133. }
  13134. /**
  13135. * Returns the current font size in points unit.
  13136. * @return current font size in points unit
  13137. * @access public
  13138. * @since 3.2.000 (2008-06-23)
  13139. */
  13140. public function getFontSizePt() {
  13141. return $this->FontSizePt;
  13142. }
  13143. /**
  13144. * Returns the current font family name.
  13145. * @return string current font family name
  13146. * @access public
  13147. * @since 4.3.008 (2008-12-05)
  13148. */
  13149. public function getFontFamily() {
  13150. return $this->FontFamily;
  13151. }
  13152. /**
  13153. * Returns the current font style.
  13154. * @return string current font style
  13155. * @access public
  13156. * @since 4.3.008 (2008-12-05)
  13157. */
  13158. public function getFontStyle() {
  13159. return $this->FontStyle;
  13160. }
  13161. /**
  13162. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  13163. * 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 />
  13164. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  13165. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  13166. * @param float $h Cell minimum height. The cell extends automatically if needed.
  13167. * @param float $x upper-left corner X coordinate
  13168. * @param float $y upper-left corner Y coordinate
  13169. * @param string $html html text to print. Default value: empty string.
  13170. * @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>
  13171. * @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>
  13172. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  13173. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  13174. * @param boolean $reseth if true reset the last cell height (default true).
  13175. * @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>
  13176. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  13177. * @access public
  13178. * @uses MultiCell()
  13179. * @see Multicell(), writeHTML()
  13180. */
  13181. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true) {
  13182. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0);
  13183. }
  13184. /**
  13185. * Returns the HTML DOM array.
  13186. * <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>
  13187. * @param string $html html code
  13188. * @return array
  13189. * @access protected
  13190. * @since 3.2.000 (2008-06-20)
  13191. */
  13192. protected function getHtmlDomArray($html) {
  13193. // define block tags
  13194. $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
  13195. // remove all unsupported tags (the line below lists all supported tags)
  13196. $html = strip_tags($html, '<marker/><a><b><blockquote><body><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>');
  13197. //replace some blank characters
  13198. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  13199. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dl|dt|form|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  13200. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  13201. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  13202. $html = strtr($html, $repTable);
  13203. $offset = 0;
  13204. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  13205. $html_a = substr($html, 0, $offset);
  13206. $html_b = substr($html, $offset, ($pos - $offset + 6));
  13207. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  13208. // preserve newlines on <pre> tag
  13209. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  13210. }
  13211. $html = $html_a.$html_b.substr($html, $pos + 6);
  13212. $offset = strlen($html_a.$html_b);
  13213. }
  13214. $offset = 0;
  13215. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  13216. $html_a = substr($html, 0, $offset);
  13217. $html_b = substr($html, $offset, ($pos - $offset + 11));
  13218. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  13219. // preserve newlines on <textarea> tag
  13220. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  13221. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  13222. }
  13223. $html = $html_a.$html_b.substr($html, $pos + 11);
  13224. $offset = strlen($html_a.$html_b);
  13225. }
  13226. $html = preg_replace("'([\s]*)<option'si", "<option", $html);
  13227. $html = preg_replace("'</option>([\s]*)'si", "</option>", $html);
  13228. $offset = 0;
  13229. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  13230. $html_a = substr($html, 0, $offset);
  13231. $html_b = substr($html, $offset, ($pos - $offset + 9));
  13232. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  13233. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2\t\\4\r", $html_b);
  13234. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2\r", $html_b);
  13235. }
  13236. $html = $html_a.$html_b.substr($html, $pos + 9);
  13237. $offset = strlen($html_a.$html_b);
  13238. }
  13239. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  13240. $html = preg_replace("'([\s]+)</select>'si", "\" />", $html);
  13241. $html = str_replace("\n", ' ', $html);
  13242. // restore textarea newlines
  13243. $html = str_replace('<TBR>', "\n", $html);
  13244. // remove extra spaces from code
  13245. $html = preg_replace('/[\s]+<\/(table|tr|td|th|ul|ol|li|dl|dt|dd)>/', '</\\1>', $html);
  13246. $html = preg_replace('/[\s]+<(tr|td|th|ul|ol|li|dl|dt|dd|br)/', '<\\1', $html);
  13247. $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|dt|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
  13248. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  13249. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  13250. $html = preg_replace('/[\s]*<img/', ' <img', $html);
  13251. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  13252. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  13253. $html = preg_replace('/<textarea([^\>]*)>/xi', '<textarea\\1 value="', $html);
  13254. $html = preg_replace('/<\/textarea>/', '" />', $html);
  13255. // trim string
  13256. $html = preg_replace('/^[\s]+/', '', $html);
  13257. $html = preg_replace('/[\s]+$/', '', $html);
  13258. // pattern for generic tag
  13259. $tagpattern = '/(<[^>]+>)/';
  13260. // explodes the string
  13261. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  13262. // count elements
  13263. $maxel = count($a);
  13264. $elkey = 0;
  13265. $key = 0;
  13266. // create an array of elements
  13267. $dom = array();
  13268. $dom[$key] = array();
  13269. // set first void element
  13270. $dom[$key]['tag'] = false;
  13271. $dom[$key]['block'] = false;
  13272. $dom[$key]['value'] = '';
  13273. $dom[$key]['parent'] = 0;
  13274. $dom[$key]['fontname'] = $this->FontFamily;
  13275. $dom[$key]['fontstyle'] = $this->FontStyle;
  13276. $dom[$key]['fontsize'] = $this->FontSizePt;
  13277. $dom[$key]['stroke'] = $this->textstrokewidth;
  13278. $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
  13279. $dom[$key]['clip'] = ($this->textrendermode > 3);
  13280. $dom[$key]['line-height'] = $this->cell_height_ratio;
  13281. $dom[$key]['bgcolor'] = false;
  13282. $dom[$key]['fgcolor'] = $this->fgcolor;
  13283. $dom[$key]['strokecolor'] = $this->strokecolor;
  13284. $dom[$key]['align'] = '';
  13285. $dom[$key]['listtype'] = '';
  13286. $dom[$key]['text-indent'] = 0;
  13287. $thead = false; // true when we are inside the THEAD tag
  13288. ++$key;
  13289. $level = array();
  13290. array_push($level, 0); // root
  13291. while ($elkey < $maxel) {
  13292. $dom[$key] = array();
  13293. $element = $a[$elkey];
  13294. $dom[$key]['elkey'] = $elkey;
  13295. if (preg_match($tagpattern, $element)) {
  13296. // html tag
  13297. $element = substr($element, 1, -1);
  13298. // get tag name
  13299. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  13300. $tagname = strtolower($tag[1]);
  13301. // check if we are inside a table header
  13302. if ($tagname == 'thead') {
  13303. if ($element{0} == '/') {
  13304. $thead = false;
  13305. } else {
  13306. $thead = true;
  13307. }
  13308. ++$elkey;
  13309. continue;
  13310. }
  13311. $dom[$key]['tag'] = true;
  13312. $dom[$key]['value'] = $tagname;
  13313. if (in_array($dom[$key]['value'], $blocktags)) {
  13314. $dom[$key]['block'] = true;
  13315. } else {
  13316. $dom[$key]['block'] = false;
  13317. }
  13318. if ($element{0} == '/') {
  13319. // *** closing html tag
  13320. $dom[$key]['opening'] = false;
  13321. $dom[$key]['parent'] = end($level);
  13322. array_pop($level);
  13323. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  13324. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  13325. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  13326. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  13327. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  13328. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  13329. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  13330. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  13331. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  13332. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  13333. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  13334. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  13335. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  13336. }
  13337. // set the number of columns in table tag
  13338. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  13339. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  13340. }
  13341. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  13342. $dom[($dom[$key]['parent'])]['content'] = '';
  13343. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  13344. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  13345. }
  13346. $key = $i;
  13347. // mark nested tables
  13348. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  13349. // remove thead sections from nested tables
  13350. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  13351. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  13352. }
  13353. // store header rows on a new table
  13354. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  13355. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  13356. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  13357. }
  13358. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  13359. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  13360. }
  13361. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  13362. $dom[($dom[$key]['parent'])]['attribute'] = array();
  13363. }
  13364. // header elements must be always contained in a single page
  13365. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  13366. }
  13367. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  13368. // remove the nobr attributes from the table header
  13369. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  13370. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  13371. }
  13372. } else {
  13373. // *** opening html tag
  13374. $dom[$key]['opening'] = true;
  13375. $dom[$key]['parent'] = end($level);
  13376. if (substr($element, -1, 1) != '/') {
  13377. // not self-closing tag
  13378. array_push($level, $key);
  13379. $dom[$key]['self'] = false;
  13380. } else {
  13381. $dom[$key]['self'] = true;
  13382. }
  13383. // copy some values from parent
  13384. $parentkey = 0;
  13385. if ($key > 0) {
  13386. $parentkey = $dom[$key]['parent'];
  13387. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  13388. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  13389. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  13390. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  13391. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  13392. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  13393. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  13394. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  13395. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  13396. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  13397. $dom[$key]['align'] = $dom[$parentkey]['align'];
  13398. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  13399. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  13400. }
  13401. // get attributes
  13402. preg_match_all('/([^=\s]*)=["]?([^"]*)["]?/', $element, $attr_array, PREG_PATTERN_ORDER);
  13403. $dom[$key]['attribute'] = array(); // reset attribute array
  13404. while (list($id, $name) = each($attr_array[1])) {
  13405. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  13406. }
  13407. // split style attributes
  13408. if (isset($dom[$key]['attribute']['style'])) {
  13409. // get style attributes
  13410. pre