PageRenderTime 142ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 2ms

/libs/tcpdf/tcpdf.php

https://github.com/CodeYellowBV/piwik
PHP | 13926 lines | 8428 code | 561 blank | 4937 comment | 1727 complexity | 5c02ba6d7a3dba39c140ca7aa25d22f0 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 6.0.044
  5. // Begin : 2002-08-03
  6. // Last Update : 2013-10-29
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2013 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can ioredistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the License
  25. // along with TCPDF. If not, see
  26. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  27. //
  28. // See LICENSE.TXT file for more information.
  29. // -------------------------------------------------------------------
  30. //
  31. // Description :
  32. // This is a PHP class for generating PDF documents without requiring external extensions.
  33. //
  34. // NOTE:
  35. // This class was originally derived in 2002 from the Public
  36. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  37. // but now is almost entirely rewritten and contains thousands of
  38. // new lines of code and hundreds new features.
  39. //
  40. // Main features:
  41. // * no external libraries are required for the basic functions;
  42. // * all standard page formats, custom page formats, custom margins and units of measure;
  43. // * UTF-8 Unicode and Right-To-Left languages;
  44. // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
  45. // * font subsetting;
  46. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  47. // * images, graphic (geometric figures) and transformation methods;
  48. // * 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)
  49. // * 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, Datamatrix, QR-Code, PDF417;
  50. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  51. // * automatic page header and footer management;
  52. // * document encryption up to 256 bit and digital signature certifications;
  53. // * transactions to UNDO commands;
  54. // * PDF annotations, including links, text and file attachments;
  55. // * text rendering modes (fill, stroke and clipping);
  56. // * multiple columns mode;
  57. // * no-write page regions;
  58. // * bookmarks, named destinations and table of content;
  59. // * text hyphenation;
  60. // * text stretching and spacing (tracking);
  61. // * automatic page break, line break and text alignments including justification;
  62. // * automatic page numbering and page groups;
  63. // * move and delete pages;
  64. // * page compression (requires php-zlib extension);
  65. // * XOBject Templates;
  66. // * Layers and object visibility.
  67. // * PDF/A-1b support
  68. //============================================================+
  69. /**
  70. * @file
  71. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  72. * 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>
  73. * <h3>TCPDF main features are:</h3>
  74. * <ul>
  75. * <li>no external libraries are required for the basic functions;</li>
  76. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  77. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  78. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  79. * <li>font subsetting;</li>
  80. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  81. * <li>images, graphic (geometric figures) and transformation methods;
  82. * <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>
  83. * <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, Datamatrix, QR-Code, PDF417;</li>
  84. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  85. * <li>automatic page header and footer management;</li>
  86. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  87. * <li>transactions to UNDO commands;</li>
  88. * <li>PDF annotations, including links, text and file attachments;</li>
  89. * <li>text rendering modes (fill, stroke and clipping);</li>
  90. * <li>multiple columns mode;</li>
  91. * <li>no-write page regions;</li>
  92. * <li>bookmarks, named destinations and table of content;</li>
  93. * <li>text hyphenation;</li>
  94. * <li>text stretching and spacing (tracking);</li>
  95. * <li>automatic page break, line break and text alignments including justification;</li>
  96. * <li>automatic page numbering and page groups;</li>
  97. * <li>move and delete pages;</li>
  98. * <li>page compression (requires php-zlib extension);</li>
  99. * <li>XOBject Templates;</li>
  100. * <li>Layers and object visibility;</li>
  101. * <li>PDF/A-1b support.</li>
  102. * </ul>
  103. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  104. * @package com.tecnick.tcpdf
  105. * @author Nicola Asuni
  106. * @version 6.0.044
  107. */
  108. // TCPDF configuration
  109. require_once(dirname(__FILE__).'/tcpdf_autoconfig.php');
  110. // TCPDF static font methods and data
  111. require_once(dirname(__FILE__).'/include/tcpdf_font_data.php');
  112. // TCPDF static font methods and data
  113. require_once(dirname(__FILE__).'/include/tcpdf_fonts.php');
  114. // TCPDF static color methods and data
  115. require_once(dirname(__FILE__).'/include/tcpdf_colors.php');
  116. // TCPDF static image methods and data
  117. require_once(dirname(__FILE__).'/include/tcpdf_images.php');
  118. // TCPDF static methods and data
  119. require_once(dirname(__FILE__).'/include/tcpdf_static.php');
  120. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  121. /**
  122. * @class TCPDF
  123. * PHP class for generating PDF documents without requiring external extensions.
  124. * 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>
  125. * @package com.tecnick.tcpdf
  126. * @brief PHP class for generating PDF documents without requiring external extensions.
  127. * @version 6.0.044
  128. * @author Nicola Asuni - info@tecnick.com
  129. */
  130. class TCPDF {
  131. // Protected properties
  132. /**
  133. * Current page number.
  134. * @protected
  135. */
  136. protected $page;
  137. /**
  138. * Current object number.
  139. * @protected
  140. */
  141. protected $n;
  142. /**
  143. * Array of object offsets.
  144. * @protected
  145. */
  146. protected $offsets = array();
  147. /**
  148. * Array of object IDs for each page.
  149. * @protected
  150. */
  151. protected $pageobjects = array();
  152. /**
  153. * Buffer holding in-memory PDF.
  154. * @protected
  155. */
  156. protected $buffer;
  157. /**
  158. * Array containing pages.
  159. * @protected
  160. */
  161. protected $pages = array();
  162. /**
  163. * Current document state.
  164. * @protected
  165. */
  166. protected $state;
  167. /**
  168. * Compression flag.
  169. * @protected
  170. */
  171. protected $compress;
  172. /**
  173. * Current page orientation (P = Portrait, L = Landscape).
  174. * @protected
  175. */
  176. protected $CurOrientation;
  177. /**
  178. * Page dimensions.
  179. * @protected
  180. */
  181. protected $pagedim = array();
  182. /**
  183. * Scale factor (number of points in user unit).
  184. * @protected
  185. */
  186. protected $k;
  187. /**
  188. * Width of page format in points.
  189. * @protected
  190. */
  191. protected $fwPt;
  192. /**
  193. * Height of page format in points.
  194. * @protected
  195. */
  196. protected $fhPt;
  197. /**
  198. * Current width of page in points.
  199. * @protected
  200. */
  201. protected $wPt;
  202. /**
  203. * Current height of page in points.
  204. * @protected
  205. */
  206. protected $hPt;
  207. /**
  208. * Current width of page in user unit.
  209. * @protected
  210. */
  211. protected $w;
  212. /**
  213. * Current height of page in user unit.
  214. * @protected
  215. */
  216. protected $h;
  217. /**
  218. * Left margin.
  219. * @protected
  220. */
  221. protected $lMargin;
  222. /**
  223. * Right margin.
  224. * @protected
  225. */
  226. protected $rMargin;
  227. /**
  228. * Cell left margin (used by regions).
  229. * @protected
  230. */
  231. protected $clMargin;
  232. /**
  233. * Cell right margin (used by regions).
  234. * @protected
  235. */
  236. protected $crMargin;
  237. /**
  238. * Top margin.
  239. * @protected
  240. */
  241. protected $tMargin;
  242. /**
  243. * Page break margin.
  244. * @protected
  245. */
  246. protected $bMargin;
  247. /**
  248. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  249. * @since 5.9.000 (2010-10-03)
  250. * @protected
  251. */
  252. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  253. /**
  254. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  255. * @since 5.9.000 (2010-10-04)
  256. * @protected
  257. */
  258. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  259. /**
  260. * Current horizontal position in user unit for cell positioning.
  261. * @protected
  262. */
  263. protected $x;
  264. /**
  265. * Current vertical position in user unit for cell positioning.
  266. * @protected
  267. */
  268. protected $y;
  269. /**
  270. * Height of last cell printed.
  271. * @protected
  272. */
  273. protected $lasth;
  274. /**
  275. * Line width in user unit.
  276. * @protected
  277. */
  278. protected $LineWidth;
  279. /**
  280. * Array of standard font names.
  281. * @protected
  282. */
  283. protected $CoreFonts;
  284. /**
  285. * Array of used fonts.
  286. * @protected
  287. */
  288. protected $fonts = array();
  289. /**
  290. * Array of font files.
  291. * @protected
  292. */
  293. protected $FontFiles = array();
  294. /**
  295. * Array of encoding differences.
  296. * @protected
  297. */
  298. protected $diffs = array();
  299. /**
  300. * Array of used images.
  301. * @protected
  302. */
  303. protected $images = array();
  304. /**
  305. * Array of cached files.
  306. * @protected
  307. */
  308. protected $cached_files = array();
  309. /**
  310. * Array of Annotations in pages.
  311. * @protected
  312. */
  313. protected $PageAnnots = array();
  314. /**
  315. * Array of internal links.
  316. * @protected
  317. */
  318. protected $links = array();
  319. /**
  320. * Current font family.
  321. * @protected
  322. */
  323. protected $FontFamily;
  324. /**
  325. * Current font style.
  326. * @protected
  327. */
  328. protected $FontStyle;
  329. /**
  330. * Current font ascent (distance between font top and baseline).
  331. * @protected
  332. * @since 2.8.000 (2007-03-29)
  333. */
  334. protected $FontAscent;
  335. /**
  336. * Current font descent (distance between font bottom and baseline).
  337. * @protected
  338. * @since 2.8.000 (2007-03-29)
  339. */
  340. protected $FontDescent;
  341. /**
  342. * Underlining flag.
  343. * @protected
  344. */
  345. protected $underline;
  346. /**
  347. * Overlining flag.
  348. * @protected
  349. */
  350. protected $overline;
  351. /**
  352. * Current font info.
  353. * @protected
  354. */
  355. protected $CurrentFont;
  356. /**
  357. * Current font size in points.
  358. * @protected
  359. */
  360. protected $FontSizePt;
  361. /**
  362. * Current font size in user unit.
  363. * @protected
  364. */
  365. protected $FontSize;
  366. /**
  367. * Commands for drawing color.
  368. * @protected
  369. */
  370. protected $DrawColor;
  371. /**
  372. * Commands for filling color.
  373. * @protected
  374. */
  375. protected $FillColor;
  376. /**
  377. * Commands for text color.
  378. * @protected
  379. */
  380. protected $TextColor;
  381. /**
  382. * Indicates whether fill and text colors are different.
  383. * @protected
  384. */
  385. protected $ColorFlag;
  386. /**
  387. * Automatic page breaking.
  388. * @protected
  389. */
  390. protected $AutoPageBreak;
  391. /**
  392. * Threshold used to trigger page breaks.
  393. * @protected
  394. */
  395. protected $PageBreakTrigger;
  396. /**
  397. * Flag set when processing page header.
  398. * @protected
  399. */
  400. protected $InHeader = false;
  401. /**
  402. * Flag set when processing page footer.
  403. * @protected
  404. */
  405. protected $InFooter = false;
  406. /**
  407. * Zoom display mode.
  408. * @protected
  409. */
  410. protected $ZoomMode;
  411. /**
  412. * Layout display mode.
  413. * @protected
  414. */
  415. protected $LayoutMode;
  416. /**
  417. * If true set the document information dictionary in Unicode.
  418. * @protected
  419. */
  420. protected $docinfounicode = true;
  421. /**
  422. * Document title.
  423. * @protected
  424. */
  425. protected $title = '';
  426. /**
  427. * Document subject.
  428. * @protected
  429. */
  430. protected $subject = '';
  431. /**
  432. * Document author.
  433. * @protected
  434. */
  435. protected $author = '';
  436. /**
  437. * Document keywords.
  438. * @protected
  439. */
  440. protected $keywords = '';
  441. /**
  442. * Document creator.
  443. * @protected
  444. */
  445. protected $creator = '';
  446. /**
  447. * Starting page number.
  448. * @protected
  449. */
  450. protected $starting_page_number = 1;
  451. /**
  452. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  453. * @since 2002-07-31
  454. * @author Nicola Asuni
  455. * @protected
  456. */
  457. protected $img_rb_x;
  458. /**
  459. * The right-bottom corner Y coordinate of last inserted image.
  460. * @since 2002-07-31
  461. * @author Nicola Asuni
  462. * @protected
  463. */
  464. protected $img_rb_y;
  465. /**
  466. * Adjusting factor to convert pixels to user units.
  467. * @since 2004-06-14
  468. * @author Nicola Asuni
  469. * @protected
  470. */
  471. protected $imgscale = 1;
  472. /**
  473. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  474. * @since 2005-01-02
  475. * @author Nicola Asuni
  476. * @protected
  477. */
  478. protected $isunicode = false;
  479. /**
  480. * PDF version.
  481. * @since 1.5.3
  482. * @protected
  483. */
  484. protected $PDFVersion = '1.7';
  485. /**
  486. * ID of the stored default header template (-1 = not set).
  487. * @protected
  488. */
  489. protected $header_xobjid = -1;
  490. /**
  491. * If true reset the Header Xobject template at each page
  492. * @protected
  493. */
  494. protected $header_xobj_autoreset = false;
  495. /**
  496. * Minimum distance between header and top page margin.
  497. * @protected
  498. */
  499. protected $header_margin;
  500. /**
  501. * Minimum distance between footer and bottom page margin.
  502. * @protected
  503. */
  504. protected $footer_margin;
  505. /**
  506. * Original left margin value.
  507. * @protected
  508. * @since 1.53.0.TC013
  509. */
  510. protected $original_lMargin;
  511. /**
  512. * Original right margin value.
  513. * @protected
  514. * @since 1.53.0.TC013
  515. */
  516. protected $original_rMargin;
  517. /**
  518. * Default font used on page header.
  519. * @protected
  520. */
  521. protected $header_font;
  522. /**
  523. * Default font used on page footer.
  524. * @protected
  525. */
  526. protected $footer_font;
  527. /**
  528. * Language templates.
  529. * @protected
  530. */
  531. protected $l;
  532. /**
  533. * Barcode to print on page footer (only if set).
  534. * @protected
  535. */
  536. protected $barcode = false;
  537. /**
  538. * Boolean flag to print/hide page header.
  539. * @protected
  540. */
  541. protected $print_header = true;
  542. /**
  543. * Boolean flag to print/hide page footer.
  544. * @protected
  545. */
  546. protected $print_footer = true;
  547. /**
  548. * Header image logo.
  549. * @protected
  550. */
  551. protected $header_logo = '';
  552. /**
  553. * Width of header image logo in user units.
  554. * @protected
  555. */
  556. protected $header_logo_width = 30;
  557. /**
  558. * Title to be printed on default page header.
  559. * @protected
  560. */
  561. protected $header_title = '';
  562. /**
  563. * String to pring on page header after title.
  564. * @protected
  565. */
  566. protected $header_string = '';
  567. /**
  568. * Color for header text (RGB array).
  569. * @since 5.9.174 (2012-07-25)
  570. * @protected
  571. */
  572. protected $header_text_color = array(0,0,0);
  573. /**
  574. * Color for header line (RGB array).
  575. * @since 5.9.174 (2012-07-25)
  576. * @protected
  577. */
  578. protected $header_line_color = array(0,0,0);
  579. /**
  580. * Color for footer text (RGB array).
  581. * @since 5.9.174 (2012-07-25)
  582. * @protected
  583. */
  584. protected $footer_text_color = array(0,0,0);
  585. /**
  586. * Color for footer line (RGB array).
  587. * @since 5.9.174 (2012-07-25)
  588. * @protected
  589. */
  590. protected $footer_line_color = array(0,0,0);
  591. /**
  592. * Text shadow data array.
  593. * @since 5.9.174 (2012-07-25)
  594. * @protected
  595. */
  596. protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
  597. /**
  598. * Default number of columns for html table.
  599. * @protected
  600. */
  601. protected $default_table_columns = 4;
  602. // variables for html parser
  603. /**
  604. * HTML PARSER: array to store current link and rendering styles.
  605. * @protected
  606. */
  607. protected $HREF = array();
  608. /**
  609. * List of available fonts on filesystem.
  610. * @protected
  611. */
  612. protected $fontlist = array();
  613. /**
  614. * Current foreground color.
  615. * @protected
  616. */
  617. protected $fgcolor;
  618. /**
  619. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  620. * @protected
  621. */
  622. protected $listordered = array();
  623. /**
  624. * HTML PARSER: array count list items on nested lists.
  625. * @protected
  626. */
  627. protected $listcount = array();
  628. /**
  629. * HTML PARSER: current list nesting level.
  630. * @protected
  631. */
  632. protected $listnum = 0;
  633. /**
  634. * HTML PARSER: indent amount for lists.
  635. * @protected
  636. */
  637. protected $listindent = 0;
  638. /**
  639. * HTML PARSER: current list indententation level.
  640. * @protected
  641. */
  642. protected $listindentlevel = 0;
  643. /**
  644. * Current background color.
  645. * @protected
  646. */
  647. protected $bgcolor;
  648. /**
  649. * Temporary font size in points.
  650. * @protected
  651. */
  652. protected $tempfontsize = 10;
  653. /**
  654. * Spacer string for LI tags.
  655. * @protected
  656. */
  657. protected $lispacer = '';
  658. /**
  659. * Default encoding.
  660. * @protected
  661. * @since 1.53.0.TC010
  662. */
  663. protected $encoding = 'UTF-8';
  664. /**
  665. * PHP internal encoding.
  666. * @protected
  667. * @since 1.53.0.TC016
  668. */
  669. protected $internal_encoding;
  670. /**
  671. * Boolean flag to indicate if the document language is Right-To-Left.
  672. * @protected
  673. * @since 2.0.000
  674. */
  675. protected $rtl = false;
  676. /**
  677. * Boolean flag used to force RTL or LTR string direction.
  678. * @protected
  679. * @since 2.0.000
  680. */
  681. protected $tmprtl = false;
  682. // --- Variables used for document encryption:
  683. /**
  684. * IBoolean flag indicating whether document is protected.
  685. * @protected
  686. * @since 2.0.000 (2008-01-02)
  687. */
  688. protected $encrypted;
  689. /**
  690. * Array containing encryption settings.
  691. * @protected
  692. * @since 5.0.005 (2010-05-11)
  693. */
  694. protected $encryptdata = array();
  695. /**
  696. * Last RC4 key encrypted (cached for optimisation).
  697. * @protected
  698. * @since 2.0.000 (2008-01-02)
  699. */
  700. protected $last_enc_key;
  701. /**
  702. * Last RC4 computed key.
  703. * @protected
  704. * @since 2.0.000 (2008-01-02)
  705. */
  706. protected $last_enc_key_c;
  707. /**
  708. * File ID (used on document trailer).
  709. * @protected
  710. * @since 5.0.005 (2010-05-12)
  711. */
  712. protected $file_id;
  713. // --- bookmark ---
  714. /**
  715. * Outlines for bookmark.
  716. * @protected
  717. * @since 2.1.002 (2008-02-12)
  718. */
  719. protected $outlines = array();
  720. /**
  721. * Outline root for bookmark.
  722. * @protected
  723. * @since 2.1.002 (2008-02-12)
  724. */
  725. protected $OutlineRoot;
  726. // --- javascript and form ---
  727. /**
  728. * Javascript code.
  729. * @protected
  730. * @since 2.1.002 (2008-02-12)
  731. */
  732. protected $javascript = '';
  733. /**
  734. * Javascript counter.
  735. * @protected
  736. * @since 2.1.002 (2008-02-12)
  737. */
  738. protected $n_js;
  739. /**
  740. * line through state
  741. * @protected
  742. * @since 2.8.000 (2008-03-19)
  743. */
  744. protected $linethrough;
  745. /**
  746. * Array with additional document-wide usage rights for the document.
  747. * @protected
  748. * @since 5.8.014 (2010-08-23)
  749. */
  750. protected $ur = array();
  751. /**
  752. * DPI (Dot Per Inch) Document Resolution (do not change).
  753. * @protected
  754. * @since 3.0.000 (2008-03-27)
  755. */
  756. protected $dpi = 72;
  757. /**
  758. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  759. * @protected
  760. * @since 3.0.000 (2008-03-27)
  761. */
  762. protected $newpagegroup = array();
  763. /**
  764. * Array that contains the number of pages in each page group.
  765. * @protected
  766. * @since 3.0.000 (2008-03-27)
  767. */
  768. protected $pagegroups = array();
  769. /**
  770. * Current page group number.
  771. * @protected
  772. * @since 3.0.000 (2008-03-27)
  773. */
  774. protected $currpagegroup = 0;
  775. /**
  776. * Array of transparency objects and parameters.
  777. * @protected
  778. * @since 3.0.000 (2008-03-27)
  779. */
  780. protected $extgstates;
  781. /**
  782. * Set the default JPEG compression quality (1-100).
  783. * @protected
  784. * @since 3.0.000 (2008-03-27)
  785. */
  786. protected $jpeg_quality;
  787. /**
  788. * Default cell height ratio.
  789. * @protected
  790. * @since 3.0.014 (2008-05-23)
  791. */
  792. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  793. /**
  794. * PDF viewer preferences.
  795. * @protected
  796. * @since 3.1.000 (2008-06-09)
  797. */
  798. protected $viewer_preferences;
  799. /**
  800. * A name object specifying how the document should be displayed when opened.
  801. * @protected
  802. * @since 3.1.000 (2008-06-09)
  803. */
  804. protected $PageMode;
  805. /**
  806. * Array for storing gradient information.
  807. * @protected
  808. * @since 3.1.000 (2008-06-09)
  809. */
  810. protected $gradients = array();
  811. /**
  812. * Array used to store positions inside the pages buffer (keys are the page numbers).
  813. * @protected
  814. * @since 3.2.000 (2008-06-26)
  815. */
  816. protected $intmrk = array();
  817. /**
  818. * Array used to store positions inside the pages buffer (keys are the page numbers).
  819. * @protected
  820. * @since 5.7.000 (2010-08-03)
  821. */
  822. protected $bordermrk = array();
  823. /**
  824. * Array used to store page positions to track empty pages (keys are the page numbers).
  825. * @protected
  826. * @since 5.8.007 (2010-08-18)
  827. */
  828. protected $emptypagemrk = array();
  829. /**
  830. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  831. * @protected
  832. * @since 4.6.021 (2009-07-20)
  833. */
  834. protected $cntmrk = array();
  835. /**
  836. * Array used to store footer positions of each page.
  837. * @protected
  838. * @since 3.2.000 (2008-07-01)
  839. */
  840. protected $footerpos = array();
  841. /**
  842. * Array used to store footer length of each page.
  843. * @protected
  844. * @since 4.0.014 (2008-07-29)
  845. */
  846. protected $footerlen = array();
  847. /**
  848. * Boolean flag to indicate if a new line is created.
  849. * @protected
  850. * @since 3.2.000 (2008-07-01)
  851. */
  852. protected $newline = true;
  853. /**
  854. * End position of the latest inserted line.
  855. * @protected
  856. * @since 3.2.000 (2008-07-01)
  857. */
  858. protected $endlinex = 0;
  859. /**
  860. * PDF string for width value of the last line.
  861. * @protected
  862. * @since 4.0.006 (2008-07-16)
  863. */
  864. protected $linestyleWidth = '';
  865. /**
  866. * PDF string for CAP value of the last line.
  867. * @protected
  868. * @since 4.0.006 (2008-07-16)
  869. */
  870. protected $linestyleCap = '0 J';
  871. /**
  872. * PDF string for join value of the last line.
  873. * @protected
  874. * @since 4.0.006 (2008-07-16)
  875. */
  876. protected $linestyleJoin = '0 j';
  877. /**
  878. * PDF string for dash value of the last line.
  879. * @protected
  880. * @since 4.0.006 (2008-07-16)
  881. */
  882. protected $linestyleDash = '[] 0 d';
  883. /**
  884. * Boolean flag to indicate if marked-content sequence is open.
  885. * @protected
  886. * @since 4.0.013 (2008-07-28)
  887. */
  888. protected $openMarkedContent = false;
  889. /**
  890. * Count the latest inserted vertical spaces on HTML.
  891. * @protected
  892. * @since 4.0.021 (2008-08-24)
  893. */
  894. protected $htmlvspace = 0;
  895. /**
  896. * Array of Spot colors.
  897. * @protected
  898. * @since 4.0.024 (2008-09-12)
  899. */
  900. protected $spot_colors = array();
  901. /**
  902. * Symbol used for HTML unordered list items.
  903. * @protected
  904. * @since 4.0.028 (2008-09-26)
  905. */
  906. protected $lisymbol = '';
  907. /**
  908. * String used to mark the beginning and end of EPS image blocks.
  909. * @protected
  910. * @since 4.1.000 (2008-10-18)
  911. */
  912. protected $epsmarker = 'x#!#EPS#!#x';
  913. /**
  914. * Array of transformation matrix.
  915. * @protected
  916. * @since 4.2.000 (2008-10-29)
  917. */
  918. protected $transfmatrix = array();
  919. /**
  920. * Current key for transformation matrix.
  921. * @protected
  922. * @since 4.8.005 (2009-09-17)
  923. */
  924. protected $transfmatrix_key = 0;
  925. /**
  926. * Booklet mode for double-sided pages.
  927. * @protected
  928. * @since 4.2.000 (2008-10-29)
  929. */
  930. protected $booklet = false;
  931. /**
  932. * Epsilon value used for float calculations.
  933. * @protected
  934. * @since 4.2.000 (2008-10-29)
  935. */
  936. protected $feps = 0.005;
  937. /**
  938. * Array used for custom vertical spaces for HTML tags.
  939. * @protected
  940. * @since 4.2.001 (2008-10-30)
  941. */
  942. protected $tagvspaces = array();
  943. /**
  944. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  945. * @protected
  946. * @since 4.2.007 (2008-11-12)
  947. */
  948. protected $customlistindent = -1;
  949. /**
  950. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  951. * @protected
  952. * @since 4.2.010 (2008-11-14)
  953. */
  954. protected $opencell = true;
  955. /**
  956. * Array of files to embedd.
  957. * @protected
  958. * @since 4.4.000 (2008-12-07)
  959. */
  960. protected $embeddedfiles = array();
  961. /**
  962. * Boolean flag to indicate if we are inside a PRE tag.
  963. * @protected
  964. * @since 4.4.001 (2008-12-08)
  965. */
  966. protected $premode = false;
  967. /**
  968. * Array used to store positions of graphics transformation blocks inside the page buffer.
  969. * keys are the page numbers
  970. * @protected
  971. * @since 4.4.002 (2008-12-09)
  972. */
  973. protected $transfmrk = array();
  974. /**
  975. * Default color for html links.
  976. * @protected
  977. * @since 4.4.003 (2008-12-09)
  978. */
  979. protected $htmlLinkColorArray = array(0, 0, 255);
  980. /**
  981. * Default font style to add to html links.
  982. * @protected
  983. * @since 4.4.003 (2008-12-09)
  984. */
  985. protected $htmlLinkFontStyle = 'U';
  986. /**
  987. * Counts the number of pages.
  988. * @protected
  989. * @since 4.5.000 (2008-12-31)
  990. */
  991. protected $numpages = 0;
  992. /**
  993. * Array containing page lengths in bytes.
  994. * @protected
  995. * @since 4.5.000 (2008-12-31)
  996. */
  997. protected $pagelen = array();
  998. /**
  999. * Counts the number of pages.
  1000. * @protected
  1001. * @since 4.5.000 (2008-12-31)
  1002. */
  1003. protected $numimages = 0;
  1004. /**
  1005. * Store the image keys.
  1006. * @protected
  1007. * @since 4.5.000 (2008-12-31)
  1008. */
  1009. protected $imagekeys = array();
  1010. /**
  1011. * Length of the buffer in bytes.
  1012. * @protected
  1013. * @since 4.5.000 (2008-12-31)
  1014. */
  1015. protected $bufferlen = 0;
  1016. /**
  1017. * If true enables disk caching.
  1018. * @protected
  1019. * @since 4.5.000 (2008-12-31)
  1020. */
  1021. protected $diskcache = false;
  1022. /**
  1023. * Counts the number of fonts.
  1024. * @protected
  1025. * @since 4.5.000 (2009-01-02)
  1026. */
  1027. protected $numfonts = 0;
  1028. /**
  1029. * Store the font keys.
  1030. * @protected
  1031. * @since 4.5.000 (2009-01-02)
  1032. */
  1033. protected $fontkeys = array();
  1034. /**
  1035. * Store the font object IDs.
  1036. * @protected
  1037. * @since 4.8.001 (2009-09-09)
  1038. */
  1039. protected $font_obj_ids = array();
  1040. /**
  1041. * Store the fage status (true when opened, false when closed).
  1042. * @protected
  1043. * @since 4.5.000 (2009-01-02)
  1044. */
  1045. protected $pageopen = array();
  1046. /**
  1047. * Default monospace font.
  1048. * @protected
  1049. * @since 4.5.025 (2009-03-10)
  1050. */
  1051. protected $default_monospaced_font = 'courier';
  1052. /**
  1053. * Cloned copy of the current class object.
  1054. * @protected
  1055. * @since 4.5.029 (2009-03-19)
  1056. */
  1057. protected $objcopy;
  1058. /**
  1059. * Array used to store the lengths of cache files.
  1060. * @protected
  1061. * @since 4.5.029 (2009-03-19)
  1062. */
  1063. protected $cache_file_length = array();
  1064. /**
  1065. * Table header content to be repeated on each new page.
  1066. * @protected
  1067. * @since 4.5.030 (2009-03-20)
  1068. */
  1069. protected $thead = '';
  1070. /**
  1071. * Margins used for table header.
  1072. * @protected
  1073. * @since 4.5.030 (2009-03-20)
  1074. */
  1075. protected $theadMargins = array();
  1076. /**
  1077. * Boolean flag to enable document digital signature.
  1078. * @protected
  1079. * @since 4.6.005 (2009-04-24)
  1080. */
  1081. protected $sign = false;
  1082. /**
  1083. * Digital signature data.
  1084. * @protected
  1085. * @since 4.6.005 (2009-04-24)
  1086. */
  1087. protected $signature_data = array();
  1088. /**
  1089. * Digital signature max length.
  1090. * @protected
  1091. * @since 4.6.005 (2009-04-24)
  1092. */
  1093. protected $signature_max_length = 11742;
  1094. /**
  1095. * Data for digital signature appearance.
  1096. * @protected
  1097. * @since 5.3.011 (2010-06-16)
  1098. */
  1099. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1100. /**
  1101. * Array of empty digital signature appearances.
  1102. * @protected
  1103. * @since 5.9.101 (2011-07-06)
  1104. */
  1105. protected $empty_signature_appearance = array();
  1106. /**
  1107. * Regular expression used to find blank characters (required for word-wrapping).
  1108. * @protected
  1109. * @since 4.6.006 (2009-04-28)
  1110. */
  1111. protected $re_spaces = '/[^\S\xa0]/';
  1112. /**
  1113. * Array of $re_spaces parts.
  1114. * @protected
  1115. * @since 5.5.011 (2010-07-09)
  1116. */
  1117. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1118. /**
  1119. * Digital signature object ID.
  1120. * @protected
  1121. * @since 4.6.022 (2009-06-23)
  1122. */
  1123. protected $sig_obj_id = 0;
  1124. /**
  1125. * ID of page objects.
  1126. * @protected
  1127. * @since 4.7.000 (2009-08-29)
  1128. */
  1129. protected $page_obj_id = array();
  1130. /**
  1131. * List of form annotations IDs.
  1132. * @protected
  1133. * @since 4.8.000 (2009-09-07)
  1134. */
  1135. protected $form_obj_id = array();
  1136. /**
  1137. * 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.
  1138. * @protected
  1139. * @since 4.8.000 (2009-09-07)
  1140. */
  1141. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1142. /**
  1143. * Javascript objects array.
  1144. * @protected
  1145. * @since 4.8.000 (2009-09-07)
  1146. */
  1147. protected $js_objects = array();
  1148. /**
  1149. * Current form action (used during XHTML rendering).
  1150. * @protected
  1151. * @since 4.8.000 (2009-09-07)
  1152. */
  1153. protected $form_action = '';
  1154. /**
  1155. * Current form encryption type (used during XHTML rendering).
  1156. * @protected
  1157. * @since 4.8.000 (2009-09-07)
  1158. */
  1159. protected $form_enctype = 'application/x-www-form-urlencoded';
  1160. /**
  1161. * Current method to submit forms.
  1162. * @protected
  1163. * @since 4.8.000 (2009-09-07)
  1164. */
  1165. protected $form_mode = 'post';
  1166. /**
  1167. * List of fonts used on form fields (fontname => fontkey).
  1168. * @protected
  1169. * @since 4.8.001 (2009-09-09)
  1170. */
  1171. protected $annotation_fonts = array();
  1172. /**
  1173. * List of radio buttons parent objects.
  1174. * @protected
  1175. * @since 4.8.001 (2009-09-09)
  1176. */
  1177. protected $radiobutton_groups = array();
  1178. /**
  1179. * List of radio group objects IDs.
  1180. * @protected
  1181. * @since 4.8.001 (2009-09-09)
  1182. */
  1183. protected $radio_groups = array();
  1184. /**
  1185. * Text indentation value (used for text-indent CSS attribute).
  1186. * @protected
  1187. * @since 4.8.006 (2009-09-23)
  1188. */
  1189. protected $textindent = 0;
  1190. /**
  1191. * Store page number when startTransaction() is called.
  1192. * @protected
  1193. * @since 4.8.006 (2009-09-23)
  1194. */
  1195. protected $start_transaction_page = 0;
  1196. /**
  1197. * Store Y position when startTransaction() is called.
  1198. * @protected
  1199. * @since 4.9.001 (2010-03-28)
  1200. */
  1201. protected $start_transaction_y = 0;
  1202. /**
  1203. * True when we are printing the thead section on a new page.
  1204. * @protected
  1205. * @since 4.8.027 (2010-01-25)
  1206. */
  1207. protected $inthead = false;
  1208. /**
  1209. * Array of column measures (width, space, starting Y position).
  1210. * @protected
  1211. * @since 4.9.001 (2010-03-28)
  1212. */
  1213. protected $columns = array();
  1214. /**
  1215. * Number of colums.
  1216. * @protected
  1217. * @since 4.9.001 (2010-03-28)
  1218. */
  1219. protected $num_columns = 1;
  1220. /**
  1221. * Current column number.
  1222. * @protected
  1223. * @since 4.9.001 (2010-03-28)
  1224. */
  1225. protected $current_column = 0;
  1226. /**
  1227. * Starting page for columns.
  1228. * @protected
  1229. * @since 4.9.001 (2010-03-28)
  1230. */
  1231. protected $column_start_page = 0;
  1232. /**
  1233. * Maximum page and column selected.
  1234. * @protected
  1235. * @since 5.8.000 (2010-08-11)
  1236. */
  1237. protected $maxselcol = array('page' => 0, 'column' => 0);
  1238. /**
  1239. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1240. * @protected
  1241. * @since 5.8.000 (2010-08-11)
  1242. */
  1243. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1244. /**
  1245. * 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.
  1246. * @protected
  1247. * @since 4.9.008 (2010-04-03)
  1248. */
  1249. protected $textrendermode = 0;
  1250. /**
  1251. * Text stroke width in doc units.
  1252. * @protected
  1253. * @since 4.9.008 (2010-04-03)
  1254. */
  1255. protected $textstrokewidth = 0;
  1256. /**
  1257. * Current stroke color.
  1258. * @protected
  1259. * @since 4.9.008 (2010-04-03)
  1260. */
  1261. protected $strokecolor;
  1262. /**
  1263. * Default unit of measure for document.
  1264. * @protected
  1265. * @since 5.0.000 (2010-04-22)
  1266. */
  1267. protected $pdfunit = 'mm';
  1268. /**
  1269. * Boolean flag true when we are on TOC (Table Of Content) page.
  1270. * @protected
  1271. */
  1272. protected $tocpage = false;
  1273. /**
  1274. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1275. * @protected
  1276. * @since 5.0.000 (2010-04-26)
  1277. */
  1278. protected $rasterize_vector_images = false;
  1279. /**
  1280. * Boolean flag: if true enables font subsetting by default.
  1281. * @protected
  1282. * @since 5.3.002 (2010-06-07)
  1283. */
  1284. protected $font_subsetting = true;
  1285. /**
  1286. * Array of default graphic settings.
  1287. * @protected
  1288. * @since 5.5.008 (2010-07-02)
  1289. */
  1290. protected $default_graphic_vars = array();
  1291. /**
  1292. * Array of XObjects.
  1293. * @protected
  1294. * @since 5.8.014 (2010-08-23)
  1295. */
  1296. protected $xobjects = array();
  1297. /**
  1298. * Boolean value true when we are inside an XObject.
  1299. * @protected
  1300. * @since 5.8.017 (2010-08-24)
  1301. */
  1302. protected $inxobj = false;
  1303. /**
  1304. * Current XObject ID.
  1305. * @protected
  1306. * @since 5.8.017 (2010-08-24)
  1307. */
  1308. protected $xobjid = '';
  1309. /**
  1310. * Percentage of character stretching.
  1311. * @protected
  1312. * @since 5.9.000 (2010-09-29)
  1313. */
  1314. protected $font_stretching = 100;
  1315. /**
  1316. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1317. * @protected
  1318. * @since 5.9.000 (2010-09-29)
  1319. */
  1320. protected $font_spacing = 0;
  1321. /**
  1322. * Array of no-write regions.
  1323. * ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
  1324. * @protected
  1325. * @since 5.9.003 (2010-10-14)
  1326. */
  1327. protected $page_regions = array();
  1328. /**
  1329. * Boolean value true when page region check is active.
  1330. * @protected
  1331. */
  1332. protected $check_page_regions = true;
  1333. /**
  1334. * Array of PDF layers data.
  1335. * @protected
  1336. * @since 5.9.102 (2011-07-13)
  1337. */
  1338. protected $pdflayers = array();
  1339. /**
  1340. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1341. * @protected
  1342. * @since 5.9.097 (2011-06-23)
  1343. */
  1344. protected $dests = array();
  1345. /**
  1346. * Object ID for Named Destinations
  1347. * @protected
  1348. * @since 5.9.097 (2011-06-23)
  1349. */
  1350. protected $n_dests;
  1351. /**
  1352. * Embedded Files Names
  1353. * @protected
  1354. * @since 5.9.204 (2013-01-23)
  1355. */
  1356. protected $efnames = array();
  1357. /**
  1358. * Directory used for the last SVG image.
  1359. * @protected
  1360. * @since 5.0.000 (2010-05-05)
  1361. */
  1362. protected $svgdir = '';
  1363. /**
  1364. * Deafult unit of measure for SVG.
  1365. * @protected
  1366. * @since 5.0.000 (2010-05-02)
  1367. */
  1368. protected $svgunit = 'px';
  1369. /**
  1370. * Array of SVG gradients.
  1371. * @protected
  1372. * @since 5.0.000 (2010-05-02)
  1373. */
  1374. protected $svggradients = array();
  1375. /**
  1376. * ID of last SVG gradient.
  1377. * @protected
  1378. * @since 5.0.000 (2010-05-02)
  1379. */
  1380. protected $svggradientid = 0;
  1381. /**
  1382. * Boolean value true when in SVG defs group.
  1383. * @protected
  1384. * @since 5.0.000 (2010-05-02)
  1385. */
  1386. protected $svgdefsmode = false;
  1387. /**
  1388. * Array of SVG defs.
  1389. * @protected
  1390. * @since 5.0.000 (2010-05-02)
  1391. */
  1392. protected $svgdefs = array();
  1393. /**
  1394. * Boolean value true when in SVG clipPath tag.
  1395. * @protected
  1396. * @since 5.0.000 (2010-04-26)
  1397. */
  1398. protected $svgclipmode = false;
  1399. /**
  1400. * Array of SVG clipPath commands.
  1401. * @protected
  1402. * @since 5.0.000 (2010-05-02)
  1403. */
  1404. protected $svgclippaths = array();
  1405. /**
  1406. * Array of SVG clipPath tranformation matrix.
  1407. * @protected
  1408. * @since 5.8.022 (2010-08-31)
  1409. */
  1410. protected $svgcliptm = array();
  1411. /**
  1412. * ID of last SVG clipPath.
  1413. * @protected
  1414. * @since 5.0.000 (2010-05-02)
  1415. */
  1416. protected $svgclipid = 0;
  1417. /**
  1418. * SVG text.
  1419. * @protected
  1420. * @since 5.0.000 (2010-05-02)
  1421. */
  1422. protected $svgtext = '';
  1423. /**
  1424. * SVG text properties.
  1425. * @protected
  1426. * @since 5.8.013 (2010-08-23)
  1427. */
  1428. protected $svgtextmode = array();
  1429. /**
  1430. * Array of SVG properties.
  1431. * @protected
  1432. * @since 5.0.000 (2010-05-02)
  1433. */
  1434. protected $svgstyles = array(array(
  1435. 'alignment-baseline' => 'auto',
  1436. 'baseline-shift' => 'baseline',
  1437. 'clip' => 'auto',
  1438. 'clip-path' => 'none',
  1439. 'clip-rule' => 'nonzero',
  1440. 'color' => 'black',
  1441. 'color-interpolation' => 'sRGB',
  1442. 'color-interpolation-filters' => 'linearRGB',
  1443. 'color-profile' => 'auto',
  1444. 'color-rendering' => 'auto',
  1445. 'cursor' => 'auto',
  1446. 'direction' => 'ltr',
  1447. 'display' => 'inline',
  1448. 'dominant-baseline' => 'auto',
  1449. 'enable-background' => 'accumulate',
  1450. 'fill' => 'black',
  1451. 'fill-opacity' => 1,
  1452. 'fill-rule' => 'nonzero',
  1453. 'filter' => 'none',
  1454. 'flood-color' => 'black',
  1455. 'flood-opacity' => 1,
  1456. 'font' => '',
  1457. 'font-family' => 'helvetica',
  1458. 'font-size' => 'medium',
  1459. 'font-size-adjust' => 'none',
  1460. 'font-stretch' => 'normal',
  1461. 'font-style' => 'normal',
  1462. 'font-variant' => 'normal',
  1463. 'font-weight' => 'normal',
  1464. 'glyph-orientation-horizontal' => '0deg',
  1465. 'glyph-orientation-vertical' => 'auto',
  1466. 'image-rendering' => 'auto',
  1467. 'kerning' => 'auto',
  1468. 'letter-spacing' => 'normal',
  1469. 'lighting-color' => 'white',
  1470. 'marker' => '',
  1471. 'marker-end' => 'none',
  1472. 'marker-mid' => 'none',
  1473. 'marker-start' => 'none',
  1474. 'mask' => 'none',
  1475. 'opacity' => 1,
  1476. 'overflow' => 'auto',
  1477. 'pointer-events' => 'visiblePainted',
  1478. 'shape-rendering' => 'auto',
  1479. 'stop-color' => 'black',
  1480. 'stop-opacity' => 1,
  1481. 'stroke' => 'none',
  1482. 'stroke-dasharray' => 'none',
  1483. 'stroke-dashoffset' => 0,
  1484. 'stroke-linecap' => 'butt',
  1485. 'stroke-linejoin' => 'miter',
  1486. 'stroke-miterlimit' => 4,
  1487. 'stroke-opacity' => 1,
  1488. 'stroke-width' => 1,
  1489. 'text-anchor' => 'start',
  1490. 'text-decoration' => 'none',
  1491. 'text-rendering' => 'auto',
  1492. 'unicode-bidi' => 'normal',
  1493. 'visibility' => 'visible',
  1494. 'word-spacing' => 'normal',
  1495. 'writing-mode' => 'lr-tb',
  1496. 'text-color' => 'black',
  1497. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1498. ));
  1499. /**
  1500. * If true force sRGB color profile for all document.
  1501. * @protected
  1502. * @since 5.9.121 (2011-09-28)
  1503. */
  1504. protected $force_srgb = false;
  1505. /**
  1506. * If true set the document to PDF/A mode.
  1507. * @protected
  1508. * @since 5.9.121 (2011-09-27)
  1509. */
  1510. protected $pdfa_mode = false;
  1511. /**
  1512. * Document creation date-time
  1513. * @protected
  1514. * @since 5.9.152 (2012-03-22)
  1515. */
  1516. protected $doc_creation_timestamp;
  1517. /**
  1518. * Document modification date-time
  1519. * @protected
  1520. * @since 5.9.152 (2012-03-22)
  1521. */
  1522. protected $doc_modification_timestamp;
  1523. /**
  1524. * Custom XMP data.
  1525. * @protected
  1526. * @since 5.9.128 (2011-10-06)
  1527. */
  1528. protected $custom_xmp = '';
  1529. /**
  1530. * Overprint mode array.
  1531. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1532. * @protected
  1533. * @since 5.9.152 (2012-03-23)
  1534. */
  1535. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1536. /**
  1537. * Alpha mode array.
  1538. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1539. * @protected
  1540. * @since 5.9.152 (2012-03-23)
  1541. */
  1542. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1543. /**
  1544. * Define the page boundaries boxes to be set on document.
  1545. * @protected
  1546. * @since 5.9.152 (2012-03-23)
  1547. */
  1548. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1549. /**
  1550. * If true print TCPDF meta link.
  1551. * @protected
  1552. * @since 5.9.152 (2012-03-23)
  1553. */
  1554. protected $tcpdflink = true;
  1555. /**
  1556. * Cache array for computed GD gamma values.
  1557. * @protected
  1558. * @since 5.9.1632 (2012-06-05)
  1559. */
  1560. protected $gdgammacache = array();
  1561. //------------------------------------------------------------
  1562. // METHODS
  1563. //------------------------------------------------------------
  1564. /**
  1565. * This is the class constructor.
  1566. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1567. *
  1568. * IMPORTANT: Please note that this method sets the mb_internal_encoding to ASCII, so if you are using the mbstring module functions with TCPDF you need to correctly set/unset the mb_internal_encoding when needed.
  1569. *
  1570. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1571. * @param $unit (string) 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.
  1572. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  1573. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1574. * @param $encoding (string) Charset encoding (used only when converting back html entities); default is UTF-8.
  1575. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1576. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1577. * @public
  1578. * @see getPageSizeFromFormat(), setPageFormat()
  1579. */
  1580. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1581. /* Set internal character encoding to ASCII */
  1582. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1583. $this->internal_encoding = mb_internal_encoding();
  1584. mb_internal_encoding('ASCII');
  1585. }
  1586. $this->font_obj_ids = array();
  1587. $this->page_obj_id = array();
  1588. $this->form_obj_id = array();
  1589. // set pdf/a mode
  1590. $this->pdfa_mode = $pdfa;
  1591. $this->force_srgb = false;
  1592. // set disk caching
  1593. $this->diskcache = $diskcache ? true : false;
  1594. // set language direction
  1595. $this->rtl = false;
  1596. $this->tmprtl = false;
  1597. // some checks
  1598. $this->_dochecks();
  1599. // initialization of properties
  1600. $this->isunicode = $unicode;
  1601. $this->page = 0;
  1602. $this->transfmrk[0] = array();
  1603. $this->pagedim = array();
  1604. $this->n = 2;
  1605. $this->buffer = '';
  1606. $this->pages = array();
  1607. $this->state = 0;
  1608. $this->fonts = array();
  1609. $this->FontFiles = array();
  1610. $this->diffs = array();
  1611. $this->images = array();
  1612. $this->links = array();
  1613. $this->gradients = array();
  1614. $this->InFooter = false;
  1615. $this->lasth = 0;
  1616. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1617. $this->FontStyle = '';
  1618. $this->FontSizePt = 12;
  1619. $this->underline = false;
  1620. $this->overline = false;
  1621. $this->linethrough = false;
  1622. $this->DrawColor = '0 G';
  1623. $this->FillColor = '0 g';
  1624. $this->TextColor = '0 g';
  1625. $this->ColorFlag = false;
  1626. $this->pdflayers = array();
  1627. // encryption values
  1628. $this->encrypted = false;
  1629. $this->last_enc_key = '';
  1630. // standard Unicode fonts
  1631. $this->CoreFonts = array(
  1632. 'courier'=>'Courier',
  1633. 'courierB'=>'Courier-Bold',
  1634. 'courierI'=>'Courier-Oblique',
  1635. 'courierBI'=>'Courier-BoldOblique',
  1636. 'helvetica'=>'Helvetica',
  1637. 'helveticaB'=>'Helvetica-Bold',
  1638. 'helveticaI'=>'Helvetica-Oblique',
  1639. 'helveticaBI'=>'Helvetica-BoldOblique',
  1640. 'times'=>'Times-Roman',
  1641. 'timesB'=>'Times-Bold',
  1642. 'timesI'=>'Times-Italic',
  1643. 'timesBI'=>'Times-BoldItalic',
  1644. 'symbol'=>'Symbol',
  1645. 'zapfdingbats'=>'ZapfDingbats'
  1646. );
  1647. // set scale factor
  1648. $this->setPageUnit($unit);
  1649. // set page format and orientation
  1650. $this->setPageFormat($format, $orientation);
  1651. // page margins (1 cm)
  1652. $margin = 28.35 / $this->k;
  1653. $this->SetMargins($margin, $margin);
  1654. $this->clMargin = $this->lMargin;
  1655. $this->crMargin = $this->rMargin;
  1656. // internal cell padding
  1657. $cpadding = $margin / 10;
  1658. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1659. // cell margins
  1660. $this->setCellMargins(0, 0, 0, 0);
  1661. // line width (0.2 mm)
  1662. $this->LineWidth = 0.57 / $this->k;
  1663. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1664. $this->linestyleCap = '0 J';
  1665. $this->linestyleJoin = '0 j';
  1666. $this->linestyleDash = '[] 0 d';
  1667. // automatic page break
  1668. $this->SetAutoPageBreak(true, (2 * $margin));
  1669. // full width display mode
  1670. $this->SetDisplayMode('fullwidth');
  1671. // compression
  1672. $this->SetCompression();
  1673. // set default PDF version number
  1674. $this->setPDFVersion();
  1675. $this->tcpdflink = true;
  1676. $this->encoding = $encoding;
  1677. $this->HREF = array();
  1678. $this->getFontsList();
  1679. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1680. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1681. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1682. $this->extgstates = array();
  1683. $this->setTextShadow();
  1684. // user's rights
  1685. $this->sign = false;
  1686. $this->ur['enabled'] = false;
  1687. $this->ur['document'] = '/FullSave';
  1688. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1689. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1690. $this->ur['signature'] = '/Modify';
  1691. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1692. $this->ur['formex'] = '';
  1693. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0', 'name' => 'Signature');
  1694. $this->empty_signature_appearance = array();
  1695. // set default JPEG quality
  1696. $this->jpeg_quality = 75;
  1697. // initialize some settings
  1698. TCPDF_FONTS::utf8Bidi(array(''), '', false, $this->isunicode, $this->CurrentFont);
  1699. // set default font
  1700. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1701. // check if PCRE Unicode support is enabled
  1702. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1703. // PCRE unicode support is turned ON
  1704. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1705. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1706. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1707. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1708. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1709. } else {
  1710. // PCRE unicode support is turned OFF
  1711. $this->setSpacesRE('/[^\S\xa0]/');
  1712. }
  1713. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1714. // set file ID for trailer
  1715. $serformat = (is_array($format) ? serialize($format) : $format);
  1716. $this->file_id = md5(TCPDF_STATIC::getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
  1717. // set document creation and modification timestamp
  1718. $this->doc_creation_timestamp = time();
  1719. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1720. // get default graphic vars
  1721. $this->default_graphic_vars = $this->getGraphicVars();
  1722. $this->header_xobj_autoreset = false;
  1723. $this->custom_xmp = '';
  1724. }
  1725. /**
  1726. * Default destructor.
  1727. * @public
  1728. * @since 1.53.0.TC016
  1729. */
  1730. public function __destruct() {
  1731. // restore internal encoding
  1732. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1733. mb_internal_encoding($this->internal_encoding);
  1734. }
  1735. // unset all class variables
  1736. $this->_destroy(true);
  1737. }
  1738. /**
  1739. * Set the units of measure for the document.
  1740. * @param $unit (string) 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.
  1741. * @public
  1742. * @since 3.0.015 (2008-06-06)
  1743. */
  1744. public function setPageUnit($unit) {
  1745. $unit = strtolower($unit);
  1746. //Set scale factor
  1747. switch ($unit) {
  1748. // points
  1749. case 'px':
  1750. case 'pt': {
  1751. $this->k = 1;
  1752. break;
  1753. }
  1754. // millimeters
  1755. case 'mm': {
  1756. $this->k = $this->dpi / 25.4;
  1757. break;
  1758. }
  1759. // centimeters
  1760. case 'cm': {
  1761. $this->k = $this->dpi / 2.54;
  1762. break;
  1763. }
  1764. // inches
  1765. case 'in': {
  1766. $this->k = $this->dpi;
  1767. break;
  1768. }
  1769. // unsupported unit
  1770. default : {
  1771. $this->Error('Incorrect unit: '.$unit);
  1772. break;
  1773. }
  1774. }
  1775. $this->pdfunit = $unit;
  1776. if (isset($this->CurOrientation)) {
  1777. $this->setPageOrientation($this->CurOrientation);
  1778. }
  1779. }
  1780. /**
  1781. * Change the format of the current page
  1782. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numners (width, height) or an array containing the following measures and options:<ul>
  1783. * <li>['format'] = page format name (one of the above);</li>
  1784. * <li>['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li>
  1785. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  1786. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  1787. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  1788. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  1789. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  1790. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  1791. * <li>['CropBox'] : the visible region of default user space:</li>
  1792. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  1793. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  1794. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  1795. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  1796. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  1797. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  1798. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  1799. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  1800. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  1801. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  1802. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  1803. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  1804. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  1805. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  1806. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  1807. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  1808. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  1809. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  1810. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  1811. * <li>['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:</li>
  1812. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  1813. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  1814. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  1815. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  1816. * <li>['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation</li>
  1817. * <li>['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li>
  1818. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  1819. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  1820. * <li>['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li>
  1821. * <li>['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li>
  1822. * <li>['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li>
  1823. * <li>['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.</li>
  1824. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  1825. * </ul>
  1826. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  1827. * <li>P or Portrait (default)</li>
  1828. * <li>L or Landscape</li>
  1829. * <li>'' (empty string) for automatic orientation</li>
  1830. * </ul>
  1831. * @protected
  1832. * @since 3.0.015 (2008-06-06)
  1833. * @see getPageSizeFromFormat()
  1834. */
  1835. protected function setPageFormat($format, $orientation='P') {
  1836. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  1837. // remove inherited values
  1838. unset($this->pagedim[$this->page]);
  1839. }
  1840. if (is_string($format)) {
  1841. // get page measures from format name
  1842. $pf = TCPDF_STATIC::getPageSizeFromFormat($format);
  1843. $this->fwPt = $pf[0];
  1844. $this->fhPt = $pf[1];
  1845. } else {
  1846. // the boundaries of the physical medium on which the page shall be displayed or printed
  1847. if (isset($format['MediaBox'])) {
  1848. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false, $this->k, $this->pagedim);
  1849. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  1850. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  1851. } else {
  1852. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  1853. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  1854. } else {
  1855. if (!isset($format['format'])) {
  1856. // default value
  1857. $format['format'] = 'A4';
  1858. }
  1859. $pf = TCPDF_STATIC::getPageSizeFromFormat($format['format']);
  1860. }
  1861. $this->fwPt = $pf[0];
  1862. $this->fhPt = $pf[1];
  1863. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1864. }
  1865. // the visible region of default user space
  1866. if (isset($format['CropBox'])) {
  1867. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false, $this->k, $this->pagedim);
  1868. }
  1869. // the region to which the contents of the page shall be clipped when output in a production environment
  1870. if (isset($format['BleedBox'])) {
  1871. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false, $this->k, $this->pagedim);
  1872. }
  1873. // the intended dimensions of the finished page after trimming
  1874. if (isset($format['TrimBox'])) {
  1875. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false, $this->k, $this->pagedim);
  1876. }
  1877. // the page's meaningful content (including potential white space)
  1878. if (isset($format['ArtBox'])) {
  1879. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false, $this->k, $this->pagedim);
  1880. }
  1881. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  1882. if (isset($format['BoxColorInfo'])) {
  1883. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  1884. }
  1885. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  1886. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1887. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  1888. }
  1889. if (isset($format['PZ'])) {
  1890. // The page's preferred zoom (magnification) factor
  1891. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  1892. }
  1893. if (isset($format['trans'])) {
  1894. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  1895. if (isset($format['trans']['Dur'])) {
  1896. // The page's display duration
  1897. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  1898. }
  1899. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  1900. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  1901. // The transition style that shall be used when moving to this page from another during a presentation
  1902. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  1903. $valid_effect = array('Split', 'Blinds');
  1904. $valid_vals = array('H', 'V');
  1905. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  1906. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  1907. }
  1908. $valid_effect = array('Split', 'Box', 'Fly');
  1909. $valid_vals = array('I', 'O');
  1910. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  1911. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  1912. }
  1913. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  1914. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  1915. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  1916. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  1917. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  1918. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  1919. }
  1920. }
  1921. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  1922. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  1923. }
  1924. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  1925. $this->pagedim[$this->page]['trans']['B'] = 'true';
  1926. }
  1927. } else {
  1928. $this->pagedim[$this->page]['trans']['S'] = 'R';
  1929. }
  1930. if (isset($format['trans']['D'])) {
  1931. // The duration of the transition effect, in seconds
  1932. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  1933. } else {
  1934. $this->pagedim[$this->page]['trans']['D'] = 1;
  1935. }
  1936. }
  1937. }
  1938. $this->setPageOrientation($orientation);
  1939. }
  1940. /**
  1941. * Set page orientation.
  1942. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1943. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  1944. * @param $bottommargin (float) bottom margin of the page.
  1945. * @public
  1946. * @since 3.0.015 (2008-06-06)
  1947. */
  1948. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1949. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  1950. // the boundaries of the physical medium on which the page shall be displayed or printed
  1951. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1952. }
  1953. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  1954. // the visible region of default user space
  1955. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true, $this->k, $this->pagedim);
  1956. }
  1957. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  1958. // the region to which the contents of the page shall be clipped when output in a production environment
  1959. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  1960. }
  1961. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  1962. // the intended dimensions of the finished page after trimming
  1963. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  1964. }
  1965. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  1966. // the page's meaningful content (including potential white space)
  1967. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  1968. }
  1969. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  1970. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1971. $this->pagedim[$this->page]['Rotate'] = 0;
  1972. }
  1973. if (!isset($this->pagedim[$this->page]['PZ'])) {
  1974. // The page's preferred zoom (magnification) factor
  1975. $this->pagedim[$this->page]['PZ'] = 1;
  1976. }
  1977. if ($this->fwPt > $this->fhPt) {
  1978. // landscape
  1979. $default_orientation = 'L';
  1980. } else {
  1981. // portrait
  1982. $default_orientation = 'P';
  1983. }
  1984. $valid_orientations = array('P', 'L');
  1985. if (empty($orientation)) {
  1986. $orientation = $default_orientation;
  1987. } else {
  1988. $orientation = strtoupper($orientation{0});
  1989. }
  1990. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  1991. $this->CurOrientation = $orientation;
  1992. $this->wPt = $this->fhPt;
  1993. $this->hPt = $this->fwPt;
  1994. } else {
  1995. $this->CurOrientation = $default_orientation;
  1996. $this->wPt = $this->fwPt;
  1997. $this->hPt = $this->fhPt;
  1998. }
  1999. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2000. // swap X and Y coordinates (change page orientation)
  2001. $this->pagedim = TCPDF_STATIC::swapPageBoxCoordinates($this->page, $this->pagedim);
  2002. }
  2003. $this->w = ($this->wPt / $this->k);
  2004. $this->h = ($this->hPt / $this->k);
  2005. if (TCPDF_STATIC::empty_string($autopagebreak)) {
  2006. if (isset($this->AutoPageBreak)) {
  2007. $autopagebreak = $this->AutoPageBreak;
  2008. } else {
  2009. $autopagebreak = true;
  2010. }
  2011. }
  2012. if (TCPDF_STATIC::empty_string($bottommargin)) {
  2013. if (isset($this->bMargin)) {
  2014. $bottommargin = $this->bMargin;
  2015. } else {
  2016. // default value = 2 cm
  2017. $bottommargin = 2 * 28.35 / $this->k;
  2018. }
  2019. }
  2020. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2021. // store page dimensions
  2022. $this->pagedim[$this->page]['w'] = $this->wPt;
  2023. $this->pagedim[$this->page]['h'] = $this->hPt;
  2024. $this->pagedim[$this->page]['wk'] = $this->w;
  2025. $this->pagedim[$this->page]['hk'] = $this->h;
  2026. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2027. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2028. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2029. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2030. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2031. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2032. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2033. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2034. }
  2035. /**
  2036. * Set regular expression to detect withespaces or word separators.
  2037. * The pattern delimiter must be the forward-slash character "/".
  2038. * Some example patterns are:
  2039. * <pre>
  2040. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2041. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2042. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2043. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2044. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2045. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2046. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2047. * </pre>
  2048. * @param $re (string) regular expression (leave empty for default).
  2049. * @public
  2050. * @since 4.6.016 (2009-06-15)
  2051. */
  2052. public function setSpacesRE($re='/[^\S\xa0]/') {
  2053. $this->re_spaces = $re;
  2054. $re_parts = explode('/', $re);
  2055. // get pattern parts
  2056. $this->re_space = array();
  2057. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2058. $this->re_space['p'] = $re_parts[1];
  2059. } else {
  2060. $this->re_space['p'] = '[\s]';
  2061. }
  2062. // set pattern modifiers
  2063. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2064. $this->re_space['m'] = $re_parts[2];
  2065. } else {
  2066. $this->re_space['m'] = '';
  2067. }
  2068. }
  2069. /**
  2070. * Enable or disable Right-To-Left language mode
  2071. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2072. * @param $resetx (Boolean) if true reset the X position on direction change.
  2073. * @public
  2074. * @since 2.0.000 (2008-01-03)
  2075. */
  2076. public function setRTL($enable, $resetx=true) {
  2077. $enable = $enable ? true : false;
  2078. $resetx = ($resetx AND ($enable != $this->rtl));
  2079. $this->rtl = $enable;
  2080. $this->tmprtl = false;
  2081. if ($resetx) {
  2082. $this->Ln(0);
  2083. }
  2084. }
  2085. /**
  2086. * Return the RTL status
  2087. * @return boolean
  2088. * @public
  2089. * @since 4.0.012 (2008-07-24)
  2090. */
  2091. public function getRTL() {
  2092. return $this->rtl;
  2093. }
  2094. /**
  2095. * Force temporary RTL language direction
  2096. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2097. * @public
  2098. * @since 2.1.000 (2008-01-09)
  2099. */
  2100. public function setTempRTL($mode) {
  2101. $newmode = false;
  2102. switch (strtoupper($mode)) {
  2103. case 'LTR':
  2104. case 'L': {
  2105. if ($this->rtl) {
  2106. $newmode = 'L';
  2107. }
  2108. break;
  2109. }
  2110. case 'RTL':
  2111. case 'R': {
  2112. if (!$this->rtl) {
  2113. $newmode = 'R';
  2114. }
  2115. break;
  2116. }
  2117. case false:
  2118. default: {
  2119. $newmode = false;
  2120. break;
  2121. }
  2122. }
  2123. $this->tmprtl = $newmode;
  2124. }
  2125. /**
  2126. * Return the current temporary RTL status
  2127. * @return boolean
  2128. * @public
  2129. * @since 4.8.014 (2009-11-04)
  2130. */
  2131. public function isRTLTextDir() {
  2132. return ($this->rtl OR ($this->tmprtl == 'R'));
  2133. }
  2134. /**
  2135. * Set the last cell height.
  2136. * @param $h (float) cell height.
  2137. * @author Nicola Asuni
  2138. * @public
  2139. * @since 1.53.0.TC034
  2140. */
  2141. public function setLastH($h) {
  2142. $this->lasth = $h;
  2143. }
  2144. /**
  2145. * Reset the last cell height.
  2146. * @public
  2147. * @since 5.9.000 (2010-10-03)
  2148. */
  2149. public function resetLastH() {
  2150. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2151. }
  2152. /**
  2153. * Get the last cell height.
  2154. * @return last cell height
  2155. * @public
  2156. * @since 4.0.017 (2008-08-05)
  2157. */
  2158. public function getLastH() {
  2159. return $this->lasth;
  2160. }
  2161. /**
  2162. * Set the adjusting factor to convert pixels to user units.
  2163. * @param $scale (float) adjusting factor to convert pixels to user units.
  2164. * @author Nicola Asuni
  2165. * @public
  2166. * @since 1.5.2
  2167. */
  2168. public function setImageScale($scale) {
  2169. $this->imgscale = $scale;
  2170. }
  2171. /**
  2172. * Returns the adjusting factor to convert pixels to user units.
  2173. * @return float adjusting factor to convert pixels to user units.
  2174. * @author Nicola Asuni
  2175. * @public
  2176. * @since 1.5.2
  2177. */
  2178. public function getImageScale() {
  2179. return $this->imgscale;
  2180. }
  2181. /**
  2182. * Returns an array of page dimensions:
  2183. * <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 user units</li><li>$this->pagedim[$this->page]['hk'] = page height in user units</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><li>$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li><li>$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.</li><li>$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation<ul><li>$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li><li>$this->pagedim[$this->page]['trans']['S'] = transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li><li>$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.</li><li>$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li><li>$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li><li>$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li><li>$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0. </li><li>$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li></ul></li><li>$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed<ul><li>$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['CropBox'] : the visible region of default user space<ul><li>$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment<ul><li>$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming<ul><li>$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content<ul><li>$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points</li></ul></li></ul>
  2184. * @param $pagenum (int) page number (empty = current page)
  2185. * @return array of page dimensions.
  2186. * @author Nicola Asuni
  2187. * @public
  2188. * @since 4.5.027 (2009-03-16)
  2189. */
  2190. public function getPageDimensions($pagenum='') {
  2191. if (empty($pagenum)) {
  2192. $pagenum = $this->page;
  2193. }
  2194. return $this->pagedim[$pagenum];
  2195. }
  2196. /**
  2197. * Returns the page width in units.
  2198. * @param $pagenum (int) page number (empty = current page)
  2199. * @return int page width.
  2200. * @author Nicola Asuni
  2201. * @public
  2202. * @since 1.5.2
  2203. * @see getPageDimensions()
  2204. */
  2205. public function getPageWidth($pagenum='') {
  2206. if (empty($pagenum)) {
  2207. return $this->w;
  2208. }
  2209. return $this->pagedim[$pagenum]['w'];
  2210. }
  2211. /**
  2212. * Returns the page height in units.
  2213. * @param $pagenum (int) page number (empty = current page)
  2214. * @return int page height.
  2215. * @author Nicola Asuni
  2216. * @public
  2217. * @since 1.5.2
  2218. * @see getPageDimensions()
  2219. */
  2220. public function getPageHeight($pagenum='') {
  2221. if (empty($pagenum)) {
  2222. return $this->h;
  2223. }
  2224. return $this->pagedim[$pagenum]['h'];
  2225. }
  2226. /**
  2227. * Returns the page break margin.
  2228. * @param $pagenum (int) page number (empty = current page)
  2229. * @return int page break margin.
  2230. * @author Nicola Asuni
  2231. * @public
  2232. * @since 1.5.2
  2233. * @see getPageDimensions()
  2234. */
  2235. public function getBreakMargin($pagenum='') {
  2236. if (empty($pagenum)) {
  2237. return $this->bMargin;
  2238. }
  2239. return $this->pagedim[$pagenum]['bm'];
  2240. }
  2241. /**
  2242. * Returns the scale factor (number of points in user unit).
  2243. * @return int scale factor.
  2244. * @author Nicola Asuni
  2245. * @public
  2246. * @since 1.5.2
  2247. */
  2248. public function getScaleFactor() {
  2249. return $this->k;
  2250. }
  2251. /**
  2252. * Defines the left, top and right margins.
  2253. * @param $left (float) Left margin.
  2254. * @param $top (float) Top margin.
  2255. * @param $right (float) Right margin. Default value is the left one.
  2256. * @param $keepmargins (boolean) if true overwrites the default page margins
  2257. * @public
  2258. * @since 1.0
  2259. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  2260. */
  2261. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  2262. //Set left, top and right margins
  2263. $this->lMargin = $left;
  2264. $this->tMargin = $top;
  2265. if ($right == -1) {
  2266. $right = $left;
  2267. }
  2268. $this->rMargin = $right;
  2269. if ($keepmargins) {
  2270. // overwrite original values
  2271. $this->original_lMargin = $this->lMargin;
  2272. $this->original_rMargin = $this->rMargin;
  2273. }
  2274. }
  2275. /**
  2276. * 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.
  2277. * @param $margin (float) The margin.
  2278. * @public
  2279. * @since 1.4
  2280. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2281. */
  2282. public function SetLeftMargin($margin) {
  2283. //Set left margin
  2284. $this->lMargin = $margin;
  2285. if (($this->page > 0) AND ($this->x < $margin)) {
  2286. $this->x = $margin;
  2287. }
  2288. }
  2289. /**
  2290. * Defines the top margin. The method can be called before creating the first page.
  2291. * @param $margin (float) The margin.
  2292. * @public
  2293. * @since 1.5
  2294. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2295. */
  2296. public function SetTopMargin($margin) {
  2297. //Set top margin
  2298. $this->tMargin = $margin;
  2299. if (($this->page > 0) AND ($this->y < $margin)) {
  2300. $this->y = $margin;
  2301. }
  2302. }
  2303. /**
  2304. * Defines the right margin. The method can be called before creating the first page.
  2305. * @param $margin (float) The margin.
  2306. * @public
  2307. * @since 1.5
  2308. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2309. */
  2310. public function SetRightMargin($margin) {
  2311. $this->rMargin = $margin;
  2312. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  2313. $this->x = $this->w - $margin;
  2314. }
  2315. }
  2316. /**
  2317. * Set the same internal Cell padding for top, right, bottom, left-
  2318. * @param $pad (float) internal padding.
  2319. * @public
  2320. * @since 2.1.000 (2008-01-09)
  2321. * @see getCellPaddings(), setCellPaddings()
  2322. */
  2323. public function SetCellPadding($pad) {
  2324. if ($pad >= 0) {
  2325. $this->cell_padding['L'] = $pad;
  2326. $this->cell_padding['T'] = $pad;
  2327. $this->cell_padding['R'] = $pad;
  2328. $this->cell_padding['B'] = $pad;
  2329. }
  2330. }
  2331. /**
  2332. * Set the internal Cell paddings.
  2333. * @param $left (float) left padding
  2334. * @param $top (float) top padding
  2335. * @param $right (float) right padding
  2336. * @param $bottom (float) bottom padding
  2337. * @public
  2338. * @since 5.9.000 (2010-10-03)
  2339. * @see getCellPaddings(), SetCellPadding()
  2340. */
  2341. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  2342. if (($left !== '') AND ($left >= 0)) {
  2343. $this->cell_padding['L'] = $left;
  2344. }
  2345. if (($top !== '') AND ($top >= 0)) {
  2346. $this->cell_padding['T'] = $top;
  2347. }
  2348. if (($right !== '') AND ($right >= 0)) {
  2349. $this->cell_padding['R'] = $right;
  2350. }
  2351. if (($bottom !== '') AND ($bottom >= 0)) {
  2352. $this->cell_padding['B'] = $bottom;
  2353. }
  2354. }
  2355. /**
  2356. * Get the internal Cell padding array.
  2357. * @return array of padding values
  2358. * @public
  2359. * @since 5.9.000 (2010-10-03)
  2360. * @see setCellPaddings(), SetCellPadding()
  2361. */
  2362. public function getCellPaddings() {
  2363. return $this->cell_padding;
  2364. }
  2365. /**
  2366. * Set the internal Cell margins.
  2367. * @param $left (float) left margin
  2368. * @param $top (float) top margin
  2369. * @param $right (float) right margin
  2370. * @param $bottom (float) bottom margin
  2371. * @public
  2372. * @since 5.9.000 (2010-10-03)
  2373. * @see getCellMargins()
  2374. */
  2375. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  2376. if (($left !== '') AND ($left >= 0)) {
  2377. $this->cell_margin['L'] = $left;
  2378. }
  2379. if (($top !== '') AND ($top >= 0)) {
  2380. $this->cell_margin['T'] = $top;
  2381. }
  2382. if (($right !== '') AND ($right >= 0)) {
  2383. $this->cell_margin['R'] = $right;
  2384. }
  2385. if (($bottom !== '') AND ($bottom >= 0)) {
  2386. $this->cell_margin['B'] = $bottom;
  2387. }
  2388. }
  2389. /**
  2390. * Get the internal Cell margin array.
  2391. * @return array of margin values
  2392. * @public
  2393. * @since 5.9.000 (2010-10-03)
  2394. * @see setCellMargins()
  2395. */
  2396. public function getCellMargins() {
  2397. return $this->cell_margin;
  2398. }
  2399. /**
  2400. * Adjust the internal Cell padding array to take account of the line width.
  2401. * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  2402. * @return array of adjustments
  2403. * @public
  2404. * @since 5.9.000 (2010-10-03)
  2405. */
  2406. protected function adjustCellPadding($brd=0) {
  2407. if (empty($brd)) {
  2408. return;
  2409. }
  2410. if (is_string($brd)) {
  2411. // convert string to array
  2412. $slen = strlen($brd);
  2413. $newbrd = array();
  2414. for ($i = 0; $i < $slen; ++$i) {
  2415. $newbrd[$brd[$i]] = true;
  2416. }
  2417. $brd = $newbrd;
  2418. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  2419. $brd = array('LRTB' => true);
  2420. }
  2421. if (!is_array($brd)) {
  2422. return;
  2423. }
  2424. // store current cell padding
  2425. $cp = $this->cell_padding;
  2426. // select border mode
  2427. if (isset($brd['mode'])) {
  2428. $mode = $brd['mode'];
  2429. unset($brd['mode']);
  2430. } else {
  2431. $mode = 'normal';
  2432. }
  2433. // process borders
  2434. foreach ($brd as $border => $style) {
  2435. $line_width = $this->LineWidth;
  2436. if (is_array($style) AND isset($style['width'])) {
  2437. // get border width
  2438. $line_width = $style['width'];
  2439. }
  2440. $adj = 0; // line width inside the cell
  2441. switch ($mode) {
  2442. case 'ext': {
  2443. $adj = 0;
  2444. break;
  2445. }
  2446. case 'int': {
  2447. $adj = $line_width;
  2448. break;
  2449. }
  2450. case 'normal':
  2451. default: {
  2452. $adj = ($line_width / 2);
  2453. break;
  2454. }
  2455. }
  2456. // correct internal cell padding if required to avoid overlap between text and lines
  2457. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  2458. $this->cell_padding['T'] = $adj;
  2459. }
  2460. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  2461. $this->cell_padding['R'] = $adj;
  2462. }
  2463. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  2464. $this->cell_padding['B'] = $adj;
  2465. }
  2466. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  2467. $this->cell_padding['L'] = $adj;
  2468. }
  2469. }
  2470. return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
  2471. }
  2472. /**
  2473. * 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.
  2474. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  2475. * @param $margin (float) Distance from the bottom of the page.
  2476. * @public
  2477. * @since 1.0
  2478. * @see Cell(), MultiCell(), AcceptPageBreak()
  2479. */
  2480. public function SetAutoPageBreak($auto, $margin=0) {
  2481. $this->AutoPageBreak = $auto ? true : false;
  2482. $this->bMargin = $margin;
  2483. $this->PageBreakTrigger = $this->h - $margin;
  2484. }
  2485. /**
  2486. * Return the auto-page-break mode (true or false).
  2487. * @return boolean auto-page-break mode
  2488. * @public
  2489. * @since 5.9.088
  2490. */
  2491. public function getAutoPageBreak() {
  2492. return $this->AutoPageBreak;
  2493. }
  2494. /**
  2495. * Defines the way the document is to be displayed by the viewer.
  2496. * @param $zoom (mixed) 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>
  2497. * @param $layout (string) 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>
  2498. * @param $mode (string) 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>
  2499. * @public
  2500. * @since 1.2
  2501. */
  2502. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  2503. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  2504. $this->ZoomMode = $zoom;
  2505. } else {
  2506. $this->Error('Incorrect zoom display mode: '.$zoom);
  2507. }
  2508. $this->LayoutMode = TCPDF_STATIC::getPageLayoutMode($layout);
  2509. $this->PageMode = TCPDF_STATIC::getPageMode($mode);
  2510. }
  2511. /**
  2512. * 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.
  2513. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  2514. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  2515. * @public
  2516. * @since 1.4
  2517. */
  2518. public function SetCompression($compress=true) {
  2519. if (function_exists('gzcompress')) {
  2520. $this->compress = $compress ? true : false;
  2521. } else {
  2522. $this->compress = false;
  2523. }
  2524. }
  2525. /**
  2526. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  2527. * @param $mode (boolean) If true force sRGB output intent.
  2528. * @public
  2529. * @since 5.9.121 (2011-09-28)
  2530. */
  2531. public function setSRGBmode($mode=false) {
  2532. $this->force_srgb = $mode ? true : false;
  2533. }
  2534. /**
  2535. * Turn on/off Unicode mode for document information dictionary (meta tags).
  2536. * This has effect only when unicode mode is set to false.
  2537. * @param $unicode (boolean) if true set the meta information in Unicode
  2538. * @since 5.9.027 (2010-12-01)
  2539. * @public
  2540. */
  2541. public function SetDocInfoUnicode($unicode=true) {
  2542. $this->docinfounicode = $unicode ? true : false;
  2543. }
  2544. /**
  2545. * Defines the title of the document.
  2546. * @param $title (string) The title.
  2547. * @public
  2548. * @since 1.2
  2549. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  2550. */
  2551. public function SetTitle($title) {
  2552. $this->title = $title;
  2553. }
  2554. /**
  2555. * Defines the subject of the document.
  2556. * @param $subject (string) The subject.
  2557. * @public
  2558. * @since 1.2
  2559. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  2560. */
  2561. public function SetSubject($subject) {
  2562. $this->subject = $subject;
  2563. }
  2564. /**
  2565. * Defines the author of the document.
  2566. * @param $author (string) The name of the author.
  2567. * @public
  2568. * @since 1.2
  2569. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  2570. */
  2571. public function SetAuthor($author) {
  2572. $this->author = $author;
  2573. }
  2574. /**
  2575. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  2576. * @param $keywords (string) The list of keywords.
  2577. * @public
  2578. * @since 1.2
  2579. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  2580. */
  2581. public function SetKeywords($keywords) {
  2582. $this->keywords = $keywords;
  2583. }
  2584. /**
  2585. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  2586. * @param $creator (string) The name of the creator.
  2587. * @public
  2588. * @since 1.2
  2589. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  2590. */
  2591. public function SetCreator($creator) {
  2592. $this->creator = $creator;
  2593. }
  2594. /**
  2595. * Throw an exception or print an error message and die if the K_TCPDF_PARSER_THROW_EXCEPTION_ERROR constant is set to true.
  2596. * @param $msg (string) The error message
  2597. * @public
  2598. * @since 1.0
  2599. */
  2600. public function Error($msg) {
  2601. // unset all class variables
  2602. $this->_destroy(true);
  2603. if (defined('K_TCPDF_THROW_EXCEPTION_ERROR') AND !K_TCPDF_THROW_EXCEPTION_ERROR) {
  2604. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2605. } else {
  2606. throw new Exception('TCPDF ERROR: '.$msg);
  2607. }
  2608. }
  2609. /**
  2610. * This method begins the generation of the PDF document.
  2611. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2612. * Note: no page is created by this method
  2613. * @public
  2614. * @since 1.0
  2615. * @see AddPage(), Close()
  2616. */
  2617. public function Open() {
  2618. $this->state = 1;
  2619. }
  2620. /**
  2621. * Terminates the PDF document.
  2622. * It is not necessary to call this method explicitly because Output() does it automatically.
  2623. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2624. * @public
  2625. * @since 1.0
  2626. * @see Open(), Output()
  2627. */
  2628. public function Close() {
  2629. if ($this->state == 3) {
  2630. return;
  2631. }
  2632. if ($this->page == 0) {
  2633. $this->AddPage();
  2634. }
  2635. $this->endLayer();
  2636. if ($this->tcpdflink) {
  2637. // save current graphic settings
  2638. $gvars = $this->getGraphicVars();
  2639. $this->setEqualColumns();
  2640. $this->lastpage(true);
  2641. $this->SetAutoPageBreak(false);
  2642. $this->x = 0;
  2643. $this->y = $this->h - (1 / $this->k);
  2644. $this->lMargin = 0;
  2645. $this->_out('q');
  2646. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  2647. $this->SetFont($font, '', 1);
  2648. $this->setTextRenderingMode(0, false, false);
  2649. $msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
  2650. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  2651. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  2652. $this->_out('Q');
  2653. // restore graphic settings
  2654. $this->setGraphicVars($gvars);
  2655. }
  2656. // close page
  2657. $this->endPage();
  2658. // close document
  2659. $this->_enddoc();
  2660. // unset all class variables (except critical ones)
  2661. $this->_destroy(false);
  2662. }
  2663. /**
  2664. * Move pointer at the specified document page and update page dimensions.
  2665. * @param $pnum (int) page number (1 ... numpages)
  2666. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2667. * @public
  2668. * @since 2.1.000 (2008-01-07)
  2669. * @see getPage(), lastpage(), getNumPages()
  2670. */
  2671. public function setPage($pnum, $resetmargins=false) {
  2672. if (($pnum == $this->page) AND ($this->state == 2)) {
  2673. return;
  2674. }
  2675. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  2676. $this->state = 2;
  2677. // save current graphic settings
  2678. //$gvars = $this->getGraphicVars();
  2679. $oldpage = $this->page;
  2680. $this->page = $pnum;
  2681. $this->wPt = $this->pagedim[$this->page]['w'];
  2682. $this->hPt = $this->pagedim[$this->page]['h'];
  2683. $this->w = $this->pagedim[$this->page]['wk'];
  2684. $this->h = $this->pagedim[$this->page]['hk'];
  2685. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2686. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2687. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2688. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2689. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2690. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2691. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2692. // restore graphic settings
  2693. //$this->setGraphicVars($gvars);
  2694. if ($resetmargins) {
  2695. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2696. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2697. $this->SetY($this->tMargin);
  2698. } else {
  2699. // account for booklet mode
  2700. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2701. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2702. $this->lMargin += $deltam;
  2703. $this->rMargin -= $deltam;
  2704. }
  2705. }
  2706. } else {
  2707. $this->Error('Wrong page number on setPage() function: '.$pnum);
  2708. }
  2709. }
  2710. /**
  2711. * Reset pointer to the last document page.
  2712. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2713. * @public
  2714. * @since 2.0.000 (2008-01-04)
  2715. * @see setPage(), getPage(), getNumPages()
  2716. */
  2717. public function lastPage($resetmargins=false) {
  2718. $this->setPage($this->getNumPages(), $resetmargins);
  2719. }
  2720. /**
  2721. * Get current document page number.
  2722. * @return int page number
  2723. * @public
  2724. * @since 2.1.000 (2008-01-07)
  2725. * @see setPage(), lastpage(), getNumPages()
  2726. */
  2727. public function getPage() {
  2728. return $this->page;
  2729. }
  2730. /**
  2731. * Get the total number of insered pages.
  2732. * @return int number of pages
  2733. * @public
  2734. * @since 2.1.000 (2008-01-07)
  2735. * @see setPage(), getPage(), lastpage()
  2736. */
  2737. public function getNumPages() {
  2738. return $this->numpages;
  2739. }
  2740. /**
  2741. * Adds a new TOC (Table Of Content) page to the document.
  2742. * @param $orientation (string) page orientation.
  2743. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2744. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2745. * @public
  2746. * @since 5.0.001 (2010-05-06)
  2747. * @see AddPage(), startPage(), endPage(), endTOCPage()
  2748. */
  2749. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  2750. $this->AddPage($orientation, $format, $keepmargins, true);
  2751. }
  2752. /**
  2753. * Terminate the current TOC (Table Of Content) page
  2754. * @public
  2755. * @since 5.0.001 (2010-05-06)
  2756. * @see AddPage(), startPage(), endPage(), addTOCPage()
  2757. */
  2758. public function endTOCPage() {
  2759. $this->endPage(true);
  2760. }
  2761. /**
  2762. * 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).
  2763. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2764. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2765. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2766. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2767. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  2768. * @public
  2769. * @since 1.0
  2770. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2771. */
  2772. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  2773. if ($this->inxobj) {
  2774. // we are inside an XObject template
  2775. return;
  2776. }
  2777. if (!isset($this->original_lMargin) OR $keepmargins) {
  2778. $this->original_lMargin = $this->lMargin;
  2779. }
  2780. if (!isset($this->original_rMargin) OR $keepmargins) {
  2781. $this->original_rMargin = $this->rMargin;
  2782. }
  2783. // terminate previous page
  2784. $this->endPage();
  2785. // start new page
  2786. $this->startPage($orientation, $format, $tocpage);
  2787. }
  2788. /**
  2789. * Terminate the current page
  2790. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  2791. * @public
  2792. * @since 4.2.010 (2008-11-14)
  2793. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  2794. */
  2795. public function endPage($tocpage=false) {
  2796. // check if page is already closed
  2797. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2798. return;
  2799. }
  2800. // print page footer
  2801. $this->setFooter();
  2802. // close page
  2803. $this->_endpage();
  2804. // mark page as closed
  2805. $this->pageopen[$this->page] = false;
  2806. if ($tocpage) {
  2807. $this->tocpage = false;
  2808. }
  2809. }
  2810. /**
  2811. * Starts a new page to the document. The page must be closed using the endPage() function.
  2812. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2813. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2814. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2815. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  2816. * @since 4.2.010 (2008-11-14)
  2817. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2818. * @public
  2819. */
  2820. public function startPage($orientation='', $format='', $tocpage=false) {
  2821. if ($tocpage) {
  2822. $this->tocpage = true;
  2823. }
  2824. // move page numbers of documents to be attached
  2825. if ($this->tocpage) {
  2826. // move reference to unexistent pages (used for page attachments)
  2827. // adjust outlines
  2828. $tmpoutlines = $this->outlines;
  2829. foreach ($tmpoutlines as $key => $outline) {
  2830. if ($outline['p'] > $this->numpages) {
  2831. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  2832. }
  2833. }
  2834. // adjust dests
  2835. $tmpdests = $this->dests;
  2836. foreach ($tmpdests as $key => $dest) {
  2837. if ($dest['p'] > $this->numpages) {
  2838. $this->dests[$key]['p'] = ($dest['p'] + 1);
  2839. }
  2840. }
  2841. // adjust links
  2842. $tmplinks = $this->links;
  2843. foreach ($tmplinks as $key => $link) {
  2844. if ($link[0] > $this->numpages) {
  2845. $this->links[$key][0] = ($link[0] + 1);
  2846. }
  2847. }
  2848. }
  2849. if ($this->numpages > $this->page) {
  2850. // this page has been already added
  2851. $this->setPage($this->page + 1);
  2852. $this->SetY($this->tMargin);
  2853. return;
  2854. }
  2855. // start a new page
  2856. if ($this->state == 0) {
  2857. $this->Open();
  2858. }
  2859. ++$this->numpages;
  2860. $this->swapMargins($this->booklet);
  2861. // save current graphic settings
  2862. $gvars = $this->getGraphicVars();
  2863. // start new page
  2864. $this->_beginpage($orientation, $format);
  2865. // mark page as open
  2866. $this->pageopen[$this->page] = true;
  2867. // restore graphic settings
  2868. $this->setGraphicVars($gvars);
  2869. // mark this point
  2870. $this->setPageMark();
  2871. // print page header
  2872. $this->setHeader();
  2873. // restore graphic settings
  2874. $this->setGraphicVars($gvars);
  2875. // mark this point
  2876. $this->setPageMark();
  2877. // print table header (if any)
  2878. $this->setTableHeader();
  2879. // set mark for empty page check
  2880. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  2881. }
  2882. /**
  2883. * Set start-writing mark on current page stream used to put borders and fills.
  2884. * Borders and fills are always created after content and inserted on the position marked by this method.
  2885. * This function must be called after calling Image() function for a background image.
  2886. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2887. * @public
  2888. * @since 4.0.016 (2008-07-30)
  2889. */
  2890. public function setPageMark() {
  2891. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2892. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  2893. $this->setContentMark();
  2894. }
  2895. /**
  2896. * Set start-writing mark on selected page.
  2897. * Borders and fills are always created after content and inserted on the position marked by this method.
  2898. * @param $page (int) page number (default is the current page)
  2899. * @protected
  2900. * @since 4.6.021 (2009-07-20)
  2901. */
  2902. protected function setContentMark($page=0) {
  2903. if ($page <= 0) {
  2904. $page = $this->page;
  2905. }
  2906. if (isset($this->footerlen[$page])) {
  2907. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2908. } else {
  2909. $this->cntmrk[$page] = $this->pagelen[$page];
  2910. }
  2911. }
  2912. /**
  2913. * Set header data.
  2914. * @param $ln (string) header image logo
  2915. * @param $lw (string) header image logo width in mm
  2916. * @param $ht (string) string to print as title on document header
  2917. * @param $hs (string) string to print on document header
  2918. * @param $tc (array) RGB array color for text.
  2919. * @param $lc (array) RGB array color for line.
  2920. * @public
  2921. */
  2922. public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
  2923. $this->header_logo = $ln;
  2924. $this->header_logo_width = $lw;
  2925. $this->header_title = $ht;
  2926. $this->header_string = $hs;
  2927. $this->header_text_color = $tc;
  2928. $this->header_line_color = $lc;
  2929. }
  2930. /**
  2931. * Set footer data.
  2932. * @param $tc (array) RGB array color for text.
  2933. * @param $lc (array) RGB array color for line.
  2934. * @public
  2935. */
  2936. public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
  2937. $this->footer_text_color = $tc;
  2938. $this->footer_line_color = $lc;
  2939. }
  2940. /**
  2941. * Returns header data:
  2942. * <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>
  2943. * @return array()
  2944. * @public
  2945. * @since 4.0.012 (2008-07-24)
  2946. */
  2947. public function getHeaderData() {
  2948. $ret = array();
  2949. $ret['logo'] = $this->header_logo;
  2950. $ret['logo_width'] = $this->header_logo_width;
  2951. $ret['title'] = $this->header_title;
  2952. $ret['string'] = $this->header_string;
  2953. $ret['text_color'] = $this->header_text_color;
  2954. $ret['line_color'] = $this->header_line_color;
  2955. return $ret;
  2956. }
  2957. /**
  2958. * Set header margin.
  2959. * (minimum distance between header and top page margin)
  2960. * @param $hm (int) distance in user units
  2961. * @public
  2962. */
  2963. public function setHeaderMargin($hm=10) {
  2964. $this->header_margin = $hm;
  2965. }
  2966. /**
  2967. * Returns header margin in user units.
  2968. * @return float
  2969. * @since 4.0.012 (2008-07-24)
  2970. * @public
  2971. */
  2972. public function getHeaderMargin() {
  2973. return $this->header_margin;
  2974. }
  2975. /**
  2976. * Set footer margin.
  2977. * (minimum distance between footer and bottom page margin)
  2978. * @param $fm (int) distance in user units
  2979. * @public
  2980. */
  2981. public function setFooterMargin($fm=10) {
  2982. $this->footer_margin = $fm;
  2983. }
  2984. /**
  2985. * Returns footer margin in user units.
  2986. * @return float
  2987. * @since 4.0.012 (2008-07-24)
  2988. * @public
  2989. */
  2990. public function getFooterMargin() {
  2991. return $this->footer_margin;
  2992. }
  2993. /**
  2994. * Set a flag to print page header.
  2995. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  2996. * @public
  2997. */
  2998. public function setPrintHeader($val=true) {
  2999. $this->print_header = $val ? true : false;
  3000. }
  3001. /**
  3002. * Set a flag to print page footer.
  3003. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3004. * @public
  3005. */
  3006. public function setPrintFooter($val=true) {
  3007. $this->print_footer = $val ? true : false;
  3008. }
  3009. /**
  3010. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3011. * @return float
  3012. * @public
  3013. */
  3014. public function getImageRBX() {
  3015. return $this->img_rb_x;
  3016. }
  3017. /**
  3018. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3019. * @return float
  3020. * @public
  3021. */
  3022. public function getImageRBY() {
  3023. return $this->img_rb_y;
  3024. }
  3025. /**
  3026. * Reset the xobject template used by Header() method.
  3027. * @public
  3028. */
  3029. public function resetHeaderTemplate() {
  3030. $this->header_xobjid = -1;
  3031. }
  3032. /**
  3033. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3034. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3035. * @public
  3036. */
  3037. public function setHeaderTemplateAutoreset($val=true) {
  3038. $this->header_xobj_autoreset = $val ? true : false;
  3039. }
  3040. /**
  3041. * This method is used to render the page header.
  3042. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3043. * @public
  3044. */
  3045. public function Header() {
  3046. if ($this->header_xobjid < 0) {
  3047. // start a new XObject Template
  3048. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3049. $headerfont = $this->getHeaderFont();
  3050. $headerdata = $this->getHeaderData();
  3051. $this->y = $this->header_margin;
  3052. if ($this->rtl) {
  3053. $this->x = $this->w - $this->original_rMargin;
  3054. } else {
  3055. $this->x = $this->original_lMargin;
  3056. }
  3057. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3058. $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3059. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3060. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3061. } elseif ($imgtype == 'svg') {
  3062. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3063. } else {
  3064. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3065. }
  3066. $imgy = $this->getImageRBY();
  3067. } else {
  3068. $imgy = $this->y;
  3069. }
  3070. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3071. // set starting margin for text data cell
  3072. if ($this->getRTL()) {
  3073. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3074. } else {
  3075. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3076. }
  3077. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3078. $this->SetTextColorArray($this->header_text_color);
  3079. // header title
  3080. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3081. $this->SetX($header_x);
  3082. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3083. // header string
  3084. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3085. $this->SetX($header_x);
  3086. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3087. // print an ending header line
  3088. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  3089. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3090. if ($this->rtl) {
  3091. $this->SetX($this->original_rMargin);
  3092. } else {
  3093. $this->SetX($this->original_lMargin);
  3094. }
  3095. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3096. $this->endTemplate();
  3097. }
  3098. // print header template
  3099. $x = 0;
  3100. $dx = 0;
  3101. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3102. // adjust margins for booklet mode
  3103. $dx = ($this->original_lMargin - $this->original_rMargin);
  3104. }
  3105. if ($this->rtl) {
  3106. $x = $this->w + $dx;
  3107. } else {
  3108. $x = 0 + $dx;
  3109. }
  3110. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3111. if ($this->header_xobj_autoreset) {
  3112. // reset header xobject template at each page
  3113. $this->header_xobjid = -1;
  3114. }
  3115. }
  3116. /**
  3117. * This method is used to render the page footer.
  3118. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3119. * @public
  3120. */
  3121. public function Footer() {
  3122. $cur_y = $this->y;
  3123. $this->SetTextColorArray($this->footer_text_color);
  3124. //set style for cell border
  3125. $line_width = (0.85 / $this->k);
  3126. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  3127. //print document barcode
  3128. $barcode = $this->getBarcode();
  3129. if (!empty($barcode)) {
  3130. $this->Ln($line_width);
  3131. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3132. $style = array(
  3133. 'position' => $this->rtl?'R':'L',
  3134. 'align' => $this->rtl?'R':'L',
  3135. 'stretch' => false,
  3136. 'fitwidth' => true,
  3137. 'cellfitalign' => '',
  3138. 'border' => false,
  3139. 'padding' => 0,
  3140. 'fgcolor' => array(0,0,0),
  3141. 'bgcolor' => false,
  3142. 'text' => false
  3143. );
  3144. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3145. }
  3146. $w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
  3147. if (empty($this->pagegroups)) {
  3148. $pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3149. } else {
  3150. $pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3151. }
  3152. $this->SetY($cur_y);
  3153. //Print page number
  3154. if ($this->getRTL()) {
  3155. $this->SetX($this->original_rMargin);
  3156. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3157. } else {
  3158. $this->SetX($this->original_lMargin);
  3159. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3160. }
  3161. }
  3162. /**
  3163. * This method is used to render the page header.
  3164. * @protected
  3165. * @since 4.0.012 (2008-07-24)
  3166. */
  3167. protected function setHeader() {
  3168. if (!$this->print_header OR ($this->state != 2)) {
  3169. return;
  3170. }
  3171. $this->InHeader = true;
  3172. $this->setGraphicVars($this->default_graphic_vars);
  3173. $temp_thead = $this->thead;
  3174. $temp_theadMargins = $this->theadMargins;
  3175. $lasth = $this->lasth;
  3176. $this->_out('q');
  3177. $this->rMargin = $this->original_rMargin;
  3178. $this->lMargin = $this->original_lMargin;
  3179. $this->SetCellPadding(0);
  3180. //set current position
  3181. if ($this->rtl) {
  3182. $this->SetXY($this->original_rMargin, $this->header_margin);
  3183. } else {
  3184. $this->SetXY($this->original_lMargin, $this->header_margin);
  3185. }
  3186. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3187. $this->Header();
  3188. //restore position
  3189. if ($this->rtl) {
  3190. $this->SetXY($this->original_rMargin, $this->tMargin);
  3191. } else {
  3192. $this->SetXY($this->original_lMargin, $this->tMargin);
  3193. }
  3194. $this->_out('Q');
  3195. $this->lasth = $lasth;
  3196. $this->thead = $temp_thead;
  3197. $this->theadMargins = $temp_theadMargins;
  3198. $this->newline = false;
  3199. $this->InHeader = false;
  3200. }
  3201. /**
  3202. * This method is used to render the page footer.
  3203. * @protected
  3204. * @since 4.0.012 (2008-07-24)
  3205. */
  3206. protected function setFooter() {
  3207. if ($this->state != 2) {
  3208. return;
  3209. }
  3210. $this->InFooter = true;
  3211. // save current graphic settings
  3212. $gvars = $this->getGraphicVars();
  3213. // mark this point
  3214. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  3215. $this->_out("\n");
  3216. if ($this->print_footer) {
  3217. $this->setGraphicVars($this->default_graphic_vars);
  3218. $this->current_column = 0;
  3219. $this->num_columns = 1;
  3220. $temp_thead = $this->thead;
  3221. $temp_theadMargins = $this->theadMargins;
  3222. $lasth = $this->lasth;
  3223. $this->_out('q');
  3224. $this->rMargin = $this->original_rMargin;
  3225. $this->lMargin = $this->original_lMargin;
  3226. $this->SetCellPadding(0);
  3227. //set current position
  3228. $footer_y = $this->h - $this->footer_margin;
  3229. if ($this->rtl) {
  3230. $this->SetXY($this->original_rMargin, $footer_y);
  3231. } else {
  3232. $this->SetXY($this->original_lMargin, $footer_y);
  3233. }
  3234. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  3235. $this->Footer();
  3236. //restore position
  3237. if ($this->rtl) {
  3238. $this->SetXY($this->original_rMargin, $this->tMargin);
  3239. } else {
  3240. $this->SetXY($this->original_lMargin, $this->tMargin);
  3241. }
  3242. $this->_out('Q');
  3243. $this->lasth = $lasth;
  3244. $this->thead = $temp_thead;
  3245. $this->theadMargins = $temp_theadMargins;
  3246. }
  3247. // restore graphic settings
  3248. $this->setGraphicVars($gvars);
  3249. $this->current_column = $gvars['current_column'];
  3250. $this->num_columns = $gvars['num_columns'];
  3251. // calculate footer length
  3252. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  3253. $this->InFooter = false;
  3254. }
  3255. /**
  3256. * Check if we are on the page body (excluding page header and footer).
  3257. * @return true if we are not in page header nor in page footer, false otherwise.
  3258. * @protected
  3259. * @since 5.9.091 (2011-06-15)
  3260. */
  3261. protected function inPageBody() {
  3262. return (($this->InHeader === false) AND ($this->InFooter === false));
  3263. }
  3264. /**
  3265. * This method is used to render the table header on new page (if any).
  3266. * @protected
  3267. * @since 4.5.030 (2009-03-25)
  3268. */
  3269. protected function setTableHeader() {
  3270. if ($this->num_columns > 1) {
  3271. // multi column mode
  3272. return;
  3273. }
  3274. if (isset($this->theadMargins['top'])) {
  3275. // restore the original top-margin
  3276. $this->tMargin = $this->theadMargins['top'];
  3277. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3278. $this->y = $this->tMargin;
  3279. }
  3280. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  3281. // set margins
  3282. $prev_lMargin = $this->lMargin;
  3283. $prev_rMargin = $this->rMargin;
  3284. $prev_cell_padding = $this->cell_padding;
  3285. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  3286. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  3287. $this->cell_padding = $this->theadMargins['cell_padding'];
  3288. if ($this->rtl) {
  3289. $this->x = $this->w - $this->rMargin;
  3290. } else {
  3291. $this->x = $this->lMargin;
  3292. }
  3293. // account for special "cell" mode
  3294. if ($this->theadMargins['cell']) {
  3295. if ($this->rtl) {
  3296. $this->x -= $this->cell_padding['R'];
  3297. } else {
  3298. $this->x += $this->cell_padding['L'];
  3299. }
  3300. }
  3301. // print table header
  3302. $this->writeHTML($this->thead, false, false, false, false, '');
  3303. // set new top margin to skip the table headers
  3304. if (!isset($this->theadMargins['top'])) {
  3305. $this->theadMargins['top'] = $this->tMargin;
  3306. }
  3307. // store end of header position
  3308. if (!isset($this->columns[0]['th'])) {
  3309. $this->columns[0]['th'] = array();
  3310. }
  3311. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  3312. $this->tMargin = $this->y;
  3313. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3314. $this->lasth = 0;
  3315. $this->lMargin = $prev_lMargin;
  3316. $this->rMargin = $prev_rMargin;
  3317. $this->cell_padding = $prev_cell_padding;
  3318. }
  3319. }
  3320. /**
  3321. * Returns the current page number.
  3322. * @return int page number
  3323. * @public
  3324. * @since 1.0
  3325. * @see getAliasNbPages()
  3326. */
  3327. public function PageNo() {
  3328. return $this->page;
  3329. }
  3330. /**
  3331. * Returns the array of spot colors.
  3332. * @return (array) Spot colors array.
  3333. * @public
  3334. * @since 6.0.038 (2013-09-30)
  3335. */
  3336. public function getAllSpotColors() {
  3337. return $this->spot_colors;
  3338. }
  3339. /**
  3340. * Defines a new spot color.
  3341. * It can be expressed in RGB components or gray scale.
  3342. * The method can be called before the first page is created and the value is retained from page to page.
  3343. * @param $name (string) Full name of the spot color.
  3344. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  3345. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  3346. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  3347. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  3348. * @public
  3349. * @since 4.0.024 (2008-09-12)
  3350. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3351. */
  3352. public function AddSpotColor($name, $c, $m, $y, $k) {
  3353. if (!isset($this->spot_colors[$name])) {
  3354. $i = (1 + count($this->spot_colors));
  3355. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  3356. }
  3357. }
  3358. /**
  3359. * Set the spot color for the specified type ('draw', 'fill', 'text').
  3360. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3361. * @param $name (string) Name of the spot color.
  3362. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3363. * @return (string) PDF color command.
  3364. * @public
  3365. * @since 5.9.125 (2011-10-03)
  3366. */
  3367. public function setSpotColor($type, $name, $tint=100) {
  3368. $spotcolor = TCPDF_COLORS::getSpotColor($name, $this->spot_colors);
  3369. if ($spotcolor === false) {
  3370. $this->Error('Undefined spot color: '.$name.', you must add it using the AddSpotColor() method.');
  3371. }
  3372. $tint = (max(0, min(100, $tint)) / 100);
  3373. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  3374. switch ($type) {
  3375. case 'draw': {
  3376. $pdfcolor .= sprintf('CS %F SCN', $tint);
  3377. $this->DrawColor = $pdfcolor;
  3378. $this->strokecolor = $spotcolor;
  3379. break;
  3380. }
  3381. case 'fill': {
  3382. $pdfcolor .= sprintf('cs %F scn', $tint);
  3383. $this->FillColor = $pdfcolor;
  3384. $this->bgcolor = $spotcolor;
  3385. break;
  3386. }
  3387. case 'text': {
  3388. $pdfcolor .= sprintf('cs %F scn', $tint);
  3389. $this->TextColor = $pdfcolor;
  3390. $this->fgcolor = $spotcolor;
  3391. break;
  3392. }
  3393. }
  3394. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3395. if ($this->state == 2) {
  3396. $this->_out($pdfcolor);
  3397. }
  3398. if ($this->inxobj) {
  3399. // we are inside an XObject template
  3400. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  3401. }
  3402. return $pdfcolor;
  3403. }
  3404. /**
  3405. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  3406. * @param $name (string) Name of the spot color.
  3407. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3408. * @public
  3409. * @since 4.0.024 (2008-09-12)
  3410. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3411. */
  3412. public function SetDrawSpotColor($name, $tint=100) {
  3413. $this->setSpotColor('draw', $name, $tint);
  3414. }
  3415. /**
  3416. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  3417. * @param $name (string) Name of the spot color.
  3418. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3419. * @public
  3420. * @since 4.0.024 (2008-09-12)
  3421. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  3422. */
  3423. public function SetFillSpotColor($name, $tint=100) {
  3424. $this->setSpotColor('fill', $name, $tint);
  3425. }
  3426. /**
  3427. * Defines the spot color used for text.
  3428. * @param $name (string) Name of the spot color.
  3429. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3430. * @public
  3431. * @since 4.0.024 (2008-09-12)
  3432. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  3433. */
  3434. public function SetTextSpotColor($name, $tint=100) {
  3435. $this->setSpotColor('text', $name, $tint);
  3436. }
  3437. /**
  3438. * Set the color array for the specified type ('draw', 'fill', 'text').
  3439. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3440. * The method can be called before the first page is created and the value is retained from page to page.
  3441. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3442. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  3443. * @param $ret (boolean) If true do not send the PDF command.
  3444. * @return (string) The PDF command or empty string.
  3445. * @public
  3446. * @since 3.1.000 (2008-06-11)
  3447. */
  3448. public function setColorArray($type, $color, $ret=false) {
  3449. if (is_array($color)) {
  3450. $color = array_values($color);
  3451. // component: grey, RGB red or CMYK cyan
  3452. $c = isset($color[0]) ? $color[0] : -1;
  3453. // component: RGB green or CMYK magenta
  3454. $m = isset($color[1]) ? $color[1] : -1;
  3455. // component: RGB blue or CMYK yellow
  3456. $y = isset($color[2]) ? $color[2] : -1;
  3457. // component: CMYK black
  3458. $k = isset($color[3]) ? $color[3] : -1;
  3459. // color name
  3460. $name = isset($color[4]) ? $color[4] : '';
  3461. if ($c >= 0) {
  3462. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  3463. }
  3464. }
  3465. return '';
  3466. }
  3467. /**
  3468. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  3469. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3470. * The method can be called before the first page is created and the value is retained from page to page.
  3471. * @param $color (array) Array of colors (1, 3 or 4 values).
  3472. * @param $ret (boolean) If true do not send the PDF command.
  3473. * @return string the PDF command
  3474. * @public
  3475. * @since 3.1.000 (2008-06-11)
  3476. * @see SetDrawColor()
  3477. */
  3478. public function SetDrawColorArray($color, $ret=false) {
  3479. return $this->setColorArray('draw', $color, $ret);
  3480. }
  3481. /**
  3482. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  3483. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3484. * The method can be called before the first page is created and the value is retained from page to page.
  3485. * @param $color (array) Array of colors (1, 3 or 4 values).
  3486. * @param $ret (boolean) If true do not send the PDF command.
  3487. * @public
  3488. * @since 3.1.000 (2008-6-11)
  3489. * @see SetFillColor()
  3490. */
  3491. public function SetFillColorArray($color, $ret=false) {
  3492. return $this->setColorArray('fill', $color, $ret);
  3493. }
  3494. /**
  3495. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  3496. * The method can be called before the first page is created and the value is retained from page to page.
  3497. * @param $color (array) Array of colors (1, 3 or 4 values).
  3498. * @param $ret (boolean) If true do not send the PDF command.
  3499. * @public
  3500. * @since 3.1.000 (2008-6-11)
  3501. * @see SetFillColor()
  3502. */
  3503. public function SetTextColorArray($color, $ret=false) {
  3504. return $this->setColorArray('text', $color, $ret);
  3505. }
  3506. /**
  3507. * Defines the color used by the specified type ('draw', 'fill', 'text').
  3508. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3509. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3510. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3511. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3512. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3513. * @param $ret (boolean) If true do not send the command.
  3514. * @param $name (string) spot color name (if any)
  3515. * @return (string) The PDF command or empty string.
  3516. * @public
  3517. * @since 5.9.125 (2011-10-03)
  3518. */
  3519. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3520. // set default values
  3521. if (!is_numeric($col1)) {
  3522. $col1 = 0;
  3523. }
  3524. if (!is_numeric($col2)) {
  3525. $col2 = -1;
  3526. }
  3527. if (!is_numeric($col3)) {
  3528. $col3 = -1;
  3529. }
  3530. if (!is_numeric($col4)) {
  3531. $col4 = -1;
  3532. }
  3533. // set color by case
  3534. $suffix = '';
  3535. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  3536. // Grey scale
  3537. $col1 = max(0, min(255, $col1));
  3538. $intcolor = array('G' => $col1);
  3539. $pdfcolor = sprintf('%F ', ($col1 / 255));
  3540. $suffix = 'g';
  3541. } elseif ($col4 == -1) {
  3542. // RGB
  3543. $col1 = max(0, min(255, $col1));
  3544. $col2 = max(0, min(255, $col2));
  3545. $col3 = max(0, min(255, $col3));
  3546. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  3547. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  3548. $suffix = 'rg';
  3549. } else {
  3550. $col1 = max(0, min(100, $col1));
  3551. $col2 = max(0, min(100, $col2));
  3552. $col3 = max(0, min(100, $col3));
  3553. $col4 = max(0, min(100, $col4));
  3554. if (empty($name)) {
  3555. // CMYK
  3556. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  3557. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  3558. $suffix = 'k';
  3559. } else {
  3560. // SPOT COLOR
  3561. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  3562. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  3563. $pdfcolor = $this->setSpotColor($type, $name, 100);
  3564. }
  3565. }
  3566. switch ($type) {
  3567. case 'draw': {
  3568. $pdfcolor .= strtoupper($suffix);
  3569. $this->DrawColor = $pdfcolor;
  3570. $this->strokecolor = $intcolor;
  3571. break;
  3572. }
  3573. case 'fill': {
  3574. $pdfcolor .= $suffix;
  3575. $this->FillColor = $pdfcolor;
  3576. $this->bgcolor = $intcolor;
  3577. break;
  3578. }
  3579. case 'text': {
  3580. $pdfcolor .= $suffix;
  3581. $this->TextColor = $pdfcolor;
  3582. $this->fgcolor = $intcolor;
  3583. break;
  3584. }
  3585. }
  3586. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3587. if (($type != 'text') AND ($this->state == 2)) {
  3588. if (!$ret) {
  3589. $this->_out($pdfcolor);
  3590. }
  3591. return $pdfcolor;
  3592. }
  3593. return '';
  3594. }
  3595. /**
  3596. * 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.
  3597. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3598. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3599. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3600. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3601. * @param $ret (boolean) If true do not send the command.
  3602. * @param $name (string) spot color name (if any)
  3603. * @return string the PDF command
  3604. * @public
  3605. * @since 1.3
  3606. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  3607. */
  3608. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3609. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  3610. }
  3611. /**
  3612. * 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.
  3613. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3614. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3615. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3616. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3617. * @param $ret (boolean) If true do not send the command.
  3618. * @param $name (string) Spot color name (if any).
  3619. * @return (string) The PDF command.
  3620. * @public
  3621. * @since 1.3
  3622. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  3623. */
  3624. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3625. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  3626. }
  3627. /**
  3628. * 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.
  3629. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3630. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3631. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3632. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3633. * @param $ret (boolean) If true do not send the command.
  3634. * @param $name (string) Spot color name (if any).
  3635. * @return (string) Empty string.
  3636. * @public
  3637. * @since 1.3
  3638. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  3639. */
  3640. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3641. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  3642. }
  3643. /**
  3644. * Returns the length of a string in user unit. A font must be selected.<br>
  3645. * @param $s (string) The string whose length is to be computed
  3646. * @param $fontname (string) 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.
  3647. * @param $fontstyle (string) 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-through</li><li>O: overline</li></ul> or any combination. The default value is regular.
  3648. * @param $fontsize (float) Font size in points. The default value is the current size.
  3649. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3650. * @return mixed int total string length or array of characted widths
  3651. * @author Nicola Asuni
  3652. * @public
  3653. * @since 1.2
  3654. */
  3655. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3656. return $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont), $s, $this->tmprtl, $this->isunicode, $this->CurrentFont), $fontname, $fontstyle, $fontsize, $getarray);
  3657. }
  3658. /**
  3659. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  3660. * @param $sa (string) The array of chars whose total length is to be computed
  3661. * @param $fontname (string) 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.
  3662. * @param $fontstyle (string) 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 through</li><li>O: overline</li></ul> or any combination. The default value is regular.
  3663. * @param $fontsize (float) Font size in points. The default value is the current size.
  3664. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3665. * @return mixed int total string length or array of characted widths
  3666. * @author Nicola Asuni
  3667. * @public
  3668. * @since 2.4.000 (2008-03-06)
  3669. */
  3670. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3671. // store current values
  3672. if (!TCPDF_STATIC::empty_string($fontname)) {
  3673. $prev_FontFamily = $this->FontFamily;
  3674. $prev_FontStyle = $this->FontStyle;
  3675. $prev_FontSizePt = $this->FontSizePt;
  3676. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  3677. }
  3678. // convert UTF-8 array to Latin1 if required
  3679. if ($this->isunicode AND (!$this->isUnicodeFont())) {
  3680. $sa = TCPDF_FONTS::UTF8ArrToLatin1Arr($sa);
  3681. }
  3682. $w = 0; // total width
  3683. $wa = array(); // array of characters widths
  3684. foreach ($sa as $ck => $char) {
  3685. // character width
  3686. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  3687. $wa[] = $cw;
  3688. $w += $cw;
  3689. }
  3690. // restore previous values
  3691. if (!TCPDF_STATIC::empty_string($fontname)) {
  3692. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  3693. }
  3694. if ($getarray) {
  3695. return $wa;
  3696. }
  3697. return $w;
  3698. }
  3699. /**
  3700. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  3701. * @param $char (int) The char code whose length is to be returned
  3702. * @param $notlast (boolean) If false ignore the font-spacing.
  3703. * @return float char width
  3704. * @author Nicola Asuni
  3705. * @public
  3706. * @since 2.4.000 (2008-03-06)
  3707. */
  3708. public function GetCharWidth($char, $notlast=true) {
  3709. // get raw width
  3710. $chw = $this->getRawCharWidth($char);
  3711. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  3712. // increase/decrease font spacing
  3713. $chw += $this->font_spacing;
  3714. }
  3715. if ($this->font_stretching != 100) {
  3716. // fixed stretching mode
  3717. $chw *= ($this->font_stretching / 100);
  3718. }
  3719. return $chw;
  3720. }
  3721. /**
  3722. * Returns the length of the char in user unit for the current font.
  3723. * @param $char (int) The char code whose length is to be returned
  3724. * @return float char width
  3725. * @author Nicola Asuni
  3726. * @public
  3727. * @since 5.9.000 (2010-09-28)
  3728. */
  3729. public function getRawCharWidth($char) {
  3730. if ($char == 173) {
  3731. // SHY character will not be printed
  3732. return (0);
  3733. }
  3734. if (isset($this->CurrentFont['cw'][$char])) {
  3735. $w = $this->CurrentFont['cw'][$char];
  3736. } elseif (isset($this->CurrentFont['dw'])) {
  3737. // default width
  3738. $w = $this->CurrentFont['dw'];
  3739. } elseif (isset($this->CurrentFont['cw'][32])) {
  3740. // default width
  3741. $w = $this->CurrentFont['cw'][32];
  3742. } else {
  3743. $w = 600;
  3744. }
  3745. return $this->getAbsFontMeasure($w);
  3746. }
  3747. /**
  3748. * Returns the numbero of characters in a string.
  3749. * @param $s (string) The input string.
  3750. * @return int number of characters
  3751. * @public
  3752. * @since 2.0.0001 (2008-01-07)
  3753. */
  3754. public function GetNumChars($s) {
  3755. if ($this->isUnicodeFont()) {
  3756. return count(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont));
  3757. }
  3758. return strlen($s);
  3759. }
  3760. /**
  3761. * Fill the list of available fonts ($this->fontlist).
  3762. * @protected
  3763. * @since 4.0.013 (2008-07-28)
  3764. */
  3765. protected function getFontsList() {
  3766. if (($fontsdir = opendir(TCPDF_FONTS::_getfontpath())) !== false) {
  3767. while (($file = readdir($fontsdir)) !== false) {
  3768. if (substr($file, -4) == '.php') {
  3769. array_push($this->fontlist, strtolower(basename($file, '.php')));
  3770. }
  3771. }
  3772. closedir($fontsdir);
  3773. }
  3774. }
  3775. /**
  3776. * Returns the unicode caracter specified by the value
  3777. * @param $c (int) UTF-8 value
  3778. * @return Returns the specified character.
  3779. * @since 2.3.000 (2008-03-05)
  3780. * @public
  3781. * @deprecated
  3782. */
  3783. public function unichr($c) {
  3784. return TCPDF_FONTS::unichr($c, $this->isunicode);
  3785. }
  3786. /**
  3787. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  3788. * @param $fontfile (string) Font file (full path).
  3789. * @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
  3790. * @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
  3791. * @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
  3792. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  3793. * @param $platid (int) Platform ID for CMAP table to extract (when building a Unicode font for Windows this value should be 3, for Macintosh should be 1).
  3794. * @param $encid (int) Encoding ID for CMAP table to extract (when building a Unicode font for Windows this value should be 1, for Macintosh should be 0). When Platform ID is 3, legal values for Encoding ID are: 0=Symbol, 1=Unicode, 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab, 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
  3795. * @param $addcbbox (boolean) If true includes the character bounding box information on the php font file.
  3796. * @return (string) TCPDF font name.
  3797. * @author Nicola Asuni
  3798. * @since 5.9.123 (2010-09-30)
  3799. * @public
  3800. * @deprecated
  3801. */
  3802. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1, $addcbbox=false) {
  3803. return TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
  3804. }
  3805. /**
  3806. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  3807. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  3808. * 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.
  3809. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  3810. * @param $style (string) 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>
  3811. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3812. * @return array containing the font data, or false in case of error.
  3813. * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  3814. * @public
  3815. * @since 1.5
  3816. * @see SetFont(), setFontSubsetting()
  3817. */
  3818. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  3819. if ($subset === 'default') {
  3820. $subset = $this->font_subsetting;
  3821. }
  3822. if ($this->pdfa_mode) {
  3823. $subset = false;
  3824. }
  3825. if (TCPDF_STATIC::empty_string($family)) {
  3826. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  3827. $family = $this->FontFamily;
  3828. } else {
  3829. $this->Error('Empty font family');
  3830. }
  3831. }
  3832. // move embedded styles on $style
  3833. if (substr($family, -1) == 'I') {
  3834. $style .= 'I';
  3835. $family = substr($family, 0, -1);
  3836. }
  3837. if (substr($family, -1) == 'B') {
  3838. $style .= 'B';
  3839. $family = substr($family, 0, -1);
  3840. }
  3841. // normalize family name
  3842. $family = strtolower($family);
  3843. if ((!$this->isunicode) AND ($family == 'arial')) {
  3844. $family = 'helvetica';
  3845. }
  3846. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  3847. $style = '';
  3848. }
  3849. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  3850. // all fonts must be embedded
  3851. $family = 'pdfa'.$family;
  3852. }
  3853. $tempstyle = strtoupper($style);
  3854. $style = '';
  3855. // underline
  3856. if (strpos($tempstyle, 'U') !== false) {
  3857. $this->underline = true;
  3858. } else {
  3859. $this->underline = false;
  3860. }
  3861. // line-through (deleted)
  3862. if (strpos($tempstyle, 'D') !== false) {
  3863. $this->linethrough = true;
  3864. } else {
  3865. $this->linethrough = false;
  3866. }
  3867. // overline
  3868. if (strpos($tempstyle, 'O') !== false) {
  3869. $this->overline = true;
  3870. } else {
  3871. $this->overline = false;
  3872. }
  3873. // bold
  3874. if (strpos($tempstyle, 'B') !== false) {
  3875. $style .= 'B';
  3876. }
  3877. // oblique
  3878. if (strpos($tempstyle, 'I') !== false) {
  3879. $style .= 'I';
  3880. }
  3881. $bistyle = $style;
  3882. $fontkey = $family.$style;
  3883. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  3884. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  3885. // check if the font has been already added
  3886. $fb = $this->getFontBuffer($fontkey);
  3887. if ($fb !== false) {
  3888. if ($this->inxobj) {
  3889. // we are inside an XObject template
  3890. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  3891. }
  3892. return $fontdata;
  3893. }
  3894. // get specified font directory (if any)
  3895. $fontdir = false;
  3896. if (!TCPDF_STATIC::empty_string($fontfile)) {
  3897. $fontdir = dirname($fontfile);
  3898. if (TCPDF_STATIC::empty_string($fontdir) OR ($fontdir == '.')) {
  3899. $fontdir = '';
  3900. } else {
  3901. $fontdir .= '/';
  3902. }
  3903. }
  3904. // true when the font style variation is missing
  3905. $missing_style = false;
  3906. // search and include font file
  3907. if (TCPDF_STATIC::empty_string($fontfile) OR (!@file_exists($fontfile))) {
  3908. // build a standard filenames for specified font
  3909. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  3910. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3911. if (TCPDF_STATIC::empty_string($fontfile)) {
  3912. $missing_style = true;
  3913. // try to remove the style part
  3914. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  3915. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3916. }
  3917. }
  3918. // include font file
  3919. if (!TCPDF_STATIC::empty_string($fontfile) AND (@file_exists($fontfile))) {
  3920. include($fontfile);
  3921. } else {
  3922. $this->Error('Could not include font definition file: '.$family.'');
  3923. }
  3924. // check font parameters
  3925. if ((!isset($type)) OR (!isset($cw))) {
  3926. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  3927. }
  3928. // SET default parameters
  3929. if (!isset($file) OR TCPDF_STATIC::empty_string($file)) {
  3930. $file = '';
  3931. }
  3932. if (!isset($enc) OR TCPDF_STATIC::empty_string($enc)) {
  3933. $enc = '';
  3934. }
  3935. if (!isset($cidinfo) OR TCPDF_STATIC::empty_string($cidinfo)) {
  3936. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  3937. $cidinfo['uni2cid'] = array();
  3938. }
  3939. if (!isset($ctg) OR TCPDF_STATIC::empty_string($ctg)) {
  3940. $ctg = '';
  3941. }
  3942. if (!isset($desc) OR TCPDF_STATIC::empty_string($desc)) {
  3943. $desc = array();
  3944. }
  3945. if (!isset($up) OR TCPDF_STATIC::empty_string($up)) {
  3946. $up = -100;
  3947. }
  3948. if (!isset($ut) OR TCPDF_STATIC::empty_string($ut)) {
  3949. $ut = 50;
  3950. }
  3951. if (!isset($cw) OR TCPDF_STATIC::empty_string($cw)) {
  3952. $cw = array();
  3953. }
  3954. if (!isset($dw) OR TCPDF_STATIC::empty_string($dw)) {
  3955. // set default width
  3956. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  3957. $dw = $desc['MissingWidth'];
  3958. } elseif (isset($cw[32])) {
  3959. $dw = $cw[32];
  3960. } else {
  3961. $dw = 600;
  3962. }
  3963. }
  3964. ++$this->numfonts;
  3965. if ($type == 'core') {
  3966. $name = $this->CoreFonts[$fontkey];
  3967. $subset = false;
  3968. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  3969. $subset = false;
  3970. } elseif ($type == 'TrueTypeUnicode') {
  3971. $enc = 'Identity-H';
  3972. } elseif ($type == 'cidfont0') {
  3973. if ($this->pdfa_mode) {
  3974. $this->Error('All fonts must be embedded in PDF/A mode!');
  3975. }
  3976. } else {
  3977. $this->Error('Unknow font type: '.$type.'');
  3978. }
  3979. // set name if unset
  3980. if (!isset($name) OR empty($name)) {
  3981. $name = $fontkey;
  3982. }
  3983. // create artificial font style variations if missing (only works with non-embedded fonts)
  3984. if (($type != 'core') AND $missing_style) {
  3985. // style variations
  3986. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  3987. $name .= $styles[$bistyle];
  3988. // artificial bold
  3989. if (strpos($bistyle, 'B') !== false) {
  3990. if (isset($desc['StemV'])) {
  3991. // from normal to bold
  3992. $desc['StemV'] = round($desc['StemV'] * 1.75);
  3993. } else {
  3994. // bold
  3995. $desc['StemV'] = 123;
  3996. }
  3997. }
  3998. // artificial italic
  3999. if (strpos($bistyle, 'I') !== false) {
  4000. if (isset($desc['ItalicAngle'])) {
  4001. $desc['ItalicAngle'] -= 11;
  4002. } else {
  4003. $desc['ItalicAngle'] = -11;
  4004. }
  4005. if (isset($desc['Flags'])) {
  4006. $desc['Flags'] |= 64; //bit 7
  4007. } else {
  4008. $desc['Flags'] = 64;
  4009. }
  4010. }
  4011. }
  4012. // check if the array of characters bounding boxes is defined
  4013. if (!isset($cbbox)) {
  4014. $cbbox = array();
  4015. }
  4016. // initialize subsetchars
  4017. $subsetchars = array_fill(0, 255, true);
  4018. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'cbbox' => $cbbox, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  4019. if ($this->inxobj) {
  4020. // we are inside an XObject template
  4021. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4022. }
  4023. if (isset($diff) AND (!empty($diff))) {
  4024. //Search existing encodings
  4025. $d = 0;
  4026. $nb = count($this->diffs);
  4027. for ($i=1; $i <= $nb; ++$i) {
  4028. if ($this->diffs[$i] == $diff) {
  4029. $d = $i;
  4030. break;
  4031. }
  4032. }
  4033. if ($d == 0) {
  4034. $d = $nb + 1;
  4035. $this->diffs[$d] = $diff;
  4036. }
  4037. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4038. }
  4039. if (!TCPDF_STATIC::empty_string($file)) {
  4040. if (!isset($this->FontFiles[$file])) {
  4041. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4042. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4043. } elseif ($type != 'core') {
  4044. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4045. }
  4046. } else {
  4047. // update fontkeys that are sharing this font file
  4048. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4049. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4050. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4051. }
  4052. }
  4053. }
  4054. return $fontdata;
  4055. }
  4056. /**
  4057. * Sets the font used to print character strings.
  4058. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4059. * The method can be called before the first page is created and the font is retained from page to page.
  4060. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4061. * 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 />
  4062. * @param $family (string) 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.
  4063. * @param $style (string) 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 through</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.
  4064. * @param $size (float) 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
  4065. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4066. * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  4067. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4068. * @author Nicola Asuni
  4069. * @public
  4070. * @since 1.0
  4071. * @see AddFont(), SetFontSize()
  4072. */
  4073. public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4074. //Select a font; size given in points
  4075. if ($size === null) {
  4076. $size = $this->FontSizePt;
  4077. }
  4078. if ($size < 0) {
  4079. $size = 0;
  4080. }
  4081. // try to add font (if not already added)
  4082. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4083. $this->FontFamily = $fontdata['family'];
  4084. $this->FontStyle = $fontdata['style'];
  4085. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  4086. // save subset chars of the previous font
  4087. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4088. }
  4089. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4090. $this->SetFontSize($size, $out);
  4091. }
  4092. /**
  4093. * Defines the size of the current font.
  4094. * @param $size (float) The font size in points.
  4095. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4096. * @public
  4097. * @since 1.0
  4098. * @see SetFont()
  4099. */
  4100. public function SetFontSize($size, $out=true) {
  4101. // font size in points
  4102. $this->FontSizePt = $size;
  4103. // font size in user units
  4104. $this->FontSize = $size / $this->k;
  4105. // calculate some font metrics
  4106. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4107. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4108. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4109. } else {
  4110. $font_height = $size * 1.219;
  4111. }
  4112. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4113. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4114. }
  4115. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4116. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4117. }
  4118. if (!isset($font_ascent) AND !isset($font_descent)) {
  4119. // core font
  4120. $font_ascent = 0.76 * $font_height;
  4121. $font_descent = $font_height - $font_ascent;
  4122. } elseif (!isset($font_descent)) {
  4123. $font_descent = $font_height - $font_ascent;
  4124. } elseif (!isset($font_ascent)) {
  4125. $font_ascent = $font_height - $font_descent;
  4126. }
  4127. $this->FontAscent = ($font_ascent / $this->k);
  4128. $this->FontDescent = ($font_descent / $this->k);
  4129. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
  4130. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4131. }
  4132. }
  4133. /**
  4134. * Returns the bounding box of the current font in user units.
  4135. * @return array
  4136. * @public
  4137. * @since 5.9.152 (2012-03-23)
  4138. */
  4139. public function getFontBBox() {
  4140. $fbbox = array();
  4141. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4142. $tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4143. $fbbox = array_map(array($this,'getAbsFontMeasure'), $tmpbbox);
  4144. } else {
  4145. // Find max width
  4146. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  4147. $maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
  4148. } else {
  4149. $maxw = 0;
  4150. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  4151. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  4152. }
  4153. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  4154. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  4155. }
  4156. if (isset($this->CurrentFont['dw'])) {
  4157. $maxw = max($maxw, $this->CurrentFont['dw']);
  4158. }
  4159. foreach ($this->CurrentFont['cw'] as $char => $w) {
  4160. $maxw = max($maxw, $w);
  4161. }
  4162. if ($maxw == 0) {
  4163. $maxw = 600;
  4164. }
  4165. $maxw = $this->getAbsFontMeasure($maxw);
  4166. }
  4167. $fbbox = array(0, (0 - $this->FontDescent), $maxw, $this->FontAscent);
  4168. }
  4169. return $fbbox;
  4170. }
  4171. /**
  4172. * Convert a relative font measure into absolute value.
  4173. * @param $s (int) Font measure.
  4174. * @return float Absolute measure.
  4175. * @since 5.9.186 (2012-09-13)
  4176. */
  4177. public function getAbsFontMeasure($s) {
  4178. return ($s * $this->FontSize / 1000);
  4179. }
  4180. /**
  4181. * Returns the glyph bounding box of the specified character in the current font in user units.
  4182. * @param $char (int) Input character code.
  4183. * @return mixed array(xMin, yMin, xMax, yMax) or FALSE if not defined.
  4184. * @since 5.9.186 (2012-09-13)
  4185. */
  4186. public function getCharBBox($char) {
  4187. if (isset($this->CurrentFont['cbbox'][$char])) {
  4188. return array_map(array($this,'getAbsFontMeasure'), $this->CurrentFont['cbbox'][intval($char)]);
  4189. }
  4190. return false;
  4191. }
  4192. /**
  4193. * Return the font descent value
  4194. * @param $font (string) font name
  4195. * @param $style (string) font style
  4196. * @param $size (float) The size (in points)
  4197. * @return int font descent
  4198. * @public
  4199. * @author Nicola Asuni
  4200. * @since 4.9.003 (2010-03-30)
  4201. */
  4202. public function getFontDescent($font, $style='', $size=0) {
  4203. $fontdata = $this->AddFont($font, $style);
  4204. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4205. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4206. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4207. } else {
  4208. $descent = (1.219 * 0.24 * $size);
  4209. }
  4210. return ($descent / $this->k);
  4211. }
  4212. /**
  4213. * Return the font ascent value.
  4214. * @param $font (string) font name
  4215. * @param $style (string) font style
  4216. * @param $size (float) The size (in points)
  4217. * @return int font ascent
  4218. * @public
  4219. * @author Nicola Asuni
  4220. * @since 4.9.003 (2010-03-30)
  4221. */
  4222. public function getFontAscent($font, $style='', $size=0) {
  4223. $fontdata = $this->AddFont($font, $style);
  4224. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4225. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4226. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4227. } else {
  4228. $ascent = 1.219 * 0.76 * $size;
  4229. }
  4230. return ($ascent / $this->k);
  4231. }
  4232. /**
  4233. * Return true in the character is present in the specified font.
  4234. * @param $char (mixed) Character to check (integer value or string)
  4235. * @param $font (string) Font name (family name).
  4236. * @param $style (string) Font style.
  4237. * @return (boolean) true if the char is defined, false otherwise.
  4238. * @public
  4239. * @since 5.9.153 (2012-03-28)
  4240. */
  4241. public function isCharDefined($char, $font='', $style='') {
  4242. if (is_string($char)) {
  4243. // get character code
  4244. $char = TCPDF_FONTS::UTF8StringToArray($char, $this->isunicode, $this->CurrentFont);
  4245. $char = $char[0];
  4246. }
  4247. if (TCPDF_STATIC::empty_string($font)) {
  4248. if (TCPDF_STATIC::empty_string($style)) {
  4249. return (isset($this->CurrentFont['cw'][intval($char)]));
  4250. }
  4251. $font = $this->FontFamily;
  4252. }
  4253. $fontdata = $this->AddFont($font, $style);
  4254. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4255. return (isset($fontinfo['cw'][intval($char)]));
  4256. }
  4257. /**
  4258. * Replace missing font characters on selected font with specified substitutions.
  4259. * @param $text (string) Text to process.
  4260. * @param $font (string) Font name (family name).
  4261. * @param $style (string) Font style.
  4262. * @param $subs (array) Array of possible character substitutions. The key is the character to check (integer value) and the value is a single intege value or an array of possible substitutes.
  4263. * @return (string) Processed text.
  4264. * @public
  4265. * @since 5.9.153 (2012-03-28)
  4266. */
  4267. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  4268. if (empty($subs)) {
  4269. return $text;
  4270. }
  4271. if (TCPDF_STATIC::empty_string($font)) {
  4272. $font = $this->FontFamily;
  4273. }
  4274. $fontdata = $this->AddFont($font, $style);
  4275. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4276. $uniarr = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  4277. foreach ($uniarr as $k => $chr) {
  4278. if (!isset($fontinfo['cw'][$chr])) {
  4279. // this character is missing on the selected font
  4280. if (isset($subs[$chr])) {
  4281. // we have available substitutions
  4282. if (is_array($subs[$chr])) {
  4283. foreach($subs[$chr] as $s) {
  4284. if (isset($fontinfo['cw'][$s])) {
  4285. $uniarr[$k] = $s;
  4286. break;
  4287. }
  4288. }
  4289. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  4290. $uniarr[$k] = $subs[$chr];
  4291. }
  4292. }
  4293. }
  4294. }
  4295. return TCPDF_FONTS::UniArrSubString(TCPDF_FONTS::UTF8ArrayToUniArray($uniarr, $this->isunicode));
  4296. }
  4297. /**
  4298. * Defines the default monospaced font.
  4299. * @param $font (string) Font name.
  4300. * @public
  4301. * @since 4.5.025
  4302. */
  4303. public function SetDefaultMonospacedFont($font) {
  4304. $this->default_monospaced_font = $font;
  4305. }
  4306. /**
  4307. * 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 />
  4308. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4309. * @public
  4310. * @since 1.5
  4311. * @see Cell(), Write(), Image(), Link(), SetLink()
  4312. */
  4313. public function AddLink() {
  4314. //Create a new internal link
  4315. $n = count($this->links) + 1;
  4316. $this->links[$n] = array(0, 0);
  4317. return $n;
  4318. }
  4319. /**
  4320. * Defines the page and position a link points to.
  4321. * @param $link (int) The link identifier returned by AddLink()
  4322. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4323. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  4324. * @public
  4325. * @since 1.5
  4326. * @see AddLink()
  4327. */
  4328. public function SetLink($link, $y=0, $page=-1) {
  4329. if ($y == -1) {
  4330. $y = $this->y;
  4331. }
  4332. if ($page == -1) {
  4333. $page = $this->page;
  4334. }
  4335. $this->links[$link] = array($page, $y);
  4336. }
  4337. /**
  4338. * Puts a link on a rectangular area of the page.
  4339. * 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.
  4340. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4341. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4342. * @param $w (float) Width of the rectangle
  4343. * @param $h (float) Height of the rectangle
  4344. * @param $link (mixed) URL or identifier returned by AddLink()
  4345. * @param $spaces (int) number of spaces on the text to link
  4346. * @public
  4347. * @since 1.5
  4348. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4349. */
  4350. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  4351. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  4352. }
  4353. /**
  4354. * Puts a markup annotation on a rectangular area of the page.
  4355. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  4356. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4357. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4358. * @param $w (float) Width of the rectangle
  4359. * @param $h (float) Height of the rectangle
  4360. * @param $text (string) annotation text or alternate content
  4361. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  4362. * @param $spaces (int) number of spaces on the text to link
  4363. * @public
  4364. * @since 4.0.018 (2008-08-06)
  4365. */
  4366. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  4367. if ($this->inxobj) {
  4368. // store parameters for later use on template
  4369. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  4370. return;
  4371. }
  4372. if ($x === '') {
  4373. $x = $this->x;
  4374. }
  4375. if ($y === '') {
  4376. $y = $this->y;
  4377. }
  4378. // check page for no-write regions and adapt page margins if necessary
  4379. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  4380. // recalculate coordinates to account for graphic transformations
  4381. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  4382. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  4383. $maxid = count($this->transfmatrix[$i]) - 1;
  4384. for ($j=$maxid; $j >= 0; --$j) {
  4385. $ctm = $this->transfmatrix[$i][$j];
  4386. if (isset($ctm['a'])) {
  4387. $x = $x * $this->k;
  4388. $y = ($this->h - $y) * $this->k;
  4389. $w = $w * $this->k;
  4390. $h = $h * $this->k;
  4391. // top left
  4392. $xt = $x;
  4393. $yt = $y;
  4394. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4395. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4396. // top right
  4397. $xt = $x + $w;
  4398. $yt = $y;
  4399. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4400. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4401. // bottom left
  4402. $xt = $x;
  4403. $yt = $y - $h;
  4404. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4405. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4406. // bottom right
  4407. $xt = $x + $w;
  4408. $yt = $y - $h;
  4409. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4410. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4411. // new coordinates (rectangle area)
  4412. $x = min($x1, $x2, $x3, $x4);
  4413. $y = max($y1, $y2, $y3, $y4);
  4414. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  4415. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  4416. $x = $x / $this->k;
  4417. $y = $this->h - ($y / $this->k);
  4418. }
  4419. }
  4420. }
  4421. }
  4422. if ($this->page <= 0) {
  4423. $page = 1;
  4424. } else {
  4425. $page = $this->page;
  4426. }
  4427. if (!isset($this->PageAnnots[$page])) {
  4428. $this->PageAnnots[$page] = array();
  4429. }
  4430. $this->PageAnnots[$page][] = array('n' => ++$this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  4431. if (!$this->pdfa_mode) {
  4432. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!TCPDF_STATIC::empty_string($opt['FS']))
  4433. AND (@file_exists($opt['FS']) OR TCPDF_STATIC::isValidURL($opt['FS']))
  4434. AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  4435. $this->embeddedfiles[basename($opt['FS'])] = array('f' => ++$this->n, 'n' => ++$this->n, 'file' => $opt['FS']);
  4436. }
  4437. }
  4438. // Add widgets annotation's icons
  4439. if (isset($opt['mk']['i']) AND @file_exists($opt['mk']['i'])) {
  4440. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  4441. }
  4442. if (isset($opt['mk']['ri']) AND @file_exists($opt['mk']['ri'])) {
  4443. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4444. }
  4445. if (isset($opt['mk']['ix']) AND @file_exists($opt['mk']['ix'])) {
  4446. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4447. }
  4448. }
  4449. /**
  4450. * Embedd the attached files.
  4451. * @since 4.4.000 (2008-12-07)
  4452. * @protected
  4453. * @see Annotation()
  4454. */
  4455. protected function _putEmbeddedFiles() {
  4456. if ($this->pdfa_mode) {
  4457. // embedded files are not allowed in PDF/A mode
  4458. return;
  4459. }
  4460. reset($this->embeddedfiles);
  4461. foreach ($this->embeddedfiles as $filename => $filedata) {
  4462. $data = TCPDF_STATIC::fileGetContents($filedata['file']);
  4463. if ($data !== FALSE) {
  4464. $rawsize = strlen($data);
  4465. if ($rawsize > 0) {
  4466. // update name tree
  4467. $this->efnames[$filename] = $filedata['f'].' 0 R';
  4468. // embedded file specification object
  4469. $out = $this->_getobj($filedata['f'])."\n";
  4470. $out .= '<</Type /Filespec /F '.$this->_datastring($filename, $filedata['f']).' /EF <</F '.$filedata['n'].' 0 R>> >>';
  4471. $out .= "\n".'endobj';
  4472. $this->_out($out);
  4473. // embedded file object
  4474. $filter = '';
  4475. if ($this->compress) {
  4476. $data = gzcompress($data);
  4477. $filter = ' /Filter /FlateDecode';
  4478. }
  4479. $stream = $this->_getrawstream($data, $filedata['n']);
  4480. $out = $this->_getobj($filedata['n'])."\n";
  4481. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' /Params <</Size '.$rawsize.'>> >>';
  4482. $out .= ' stream'."\n".$stream."\n".'endstream';
  4483. $out .= "\n".'endobj';
  4484. $this->_out($out);
  4485. }
  4486. }
  4487. }
  4488. }
  4489. /**
  4490. * Prints a text cell at the specified position.
  4491. * This method allows to place a string precisely on the page.
  4492. * @param $x (float) Abscissa of the cell origin
  4493. * @param $y (float) Ordinate of the cell origin
  4494. * @param $txt (string) String to print
  4495. * @param $fstroke (int) outline size in user units (false = disable)
  4496. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  4497. * @param $ffill (boolean) if true fills the text
  4498. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4499. * @param $ln (int) 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.
  4500. * @param $align (string) 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>
  4501. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4502. * @param $link (mixed) URL or identifier returned by AddLink().
  4503. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4504. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4505. * @param $calign (string) 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>
  4506. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4507. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  4508. * @public
  4509. * @since 1.0
  4510. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  4511. */
  4512. public function Text($x, $y, $txt, $fstroke=false, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
  4513. $textrendermode = $this->textrendermode;
  4514. $textstrokewidth = $this->textstrokewidth;
  4515. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  4516. $this->SetXY($x, $y, $rtloff);
  4517. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  4518. // restore previous rendering mode
  4519. $this->textrendermode = $textrendermode;
  4520. $this->textstrokewidth = $textstrokewidth;
  4521. }
  4522. /**
  4523. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  4524. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  4525. * This method is called automatically and should not be called directly by the application.
  4526. * @return boolean
  4527. * @public
  4528. * @since 1.4
  4529. * @see SetAutoPageBreak()
  4530. */
  4531. public function AcceptPageBreak() {
  4532. if ($this->num_columns > 1) {
  4533. // multi column mode
  4534. if ($this->current_column < ($this->num_columns - 1)) {
  4535. // go to next column
  4536. $this->selectColumn($this->current_column + 1);
  4537. } elseif ($this->AutoPageBreak) {
  4538. // add a new page
  4539. $this->AddPage();
  4540. // set first column
  4541. $this->selectColumn(0);
  4542. }
  4543. // avoid page breaking from checkPageBreak()
  4544. return false;
  4545. }
  4546. return $this->AutoPageBreak;
  4547. }
  4548. /**
  4549. * Add page if needed.
  4550. * @param $h (float) Cell height. Default value: 0.
  4551. * @param $y (mixed) starting y position, leave empty for current position.
  4552. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  4553. * @return boolean true in case of page break, false otherwise.
  4554. * @since 3.2.000 (2008-07-01)
  4555. * @protected
  4556. */
  4557. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  4558. if (TCPDF_STATIC::empty_string($y)) {
  4559. $y = $this->y;
  4560. }
  4561. $current_page = $this->page;
  4562. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  4563. if ($addpage) {
  4564. //Automatic page break
  4565. $x = $this->x;
  4566. $this->AddPage($this->CurOrientation);
  4567. $this->y = $this->tMargin;
  4568. $oldpage = $this->page - 1;
  4569. if ($this->rtl) {
  4570. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  4571. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  4572. } else {
  4573. $this->x = $x;
  4574. }
  4575. } else {
  4576. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  4577. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  4578. } else {
  4579. $this->x = $x;
  4580. }
  4581. }
  4582. }
  4583. return true;
  4584. }
  4585. if ($current_page != $this->page) {
  4586. // account for columns mode
  4587. return true;
  4588. }
  4589. return false;
  4590. }
  4591. /**
  4592. * 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 />
  4593. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4594. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4595. * @param $h (float) Cell height. Default value: 0.
  4596. * @param $txt (string) String to print. Default value: empty string.
  4597. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4598. * @param $ln (int) 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.
  4599. * @param $align (string) 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>
  4600. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4601. * @param $link (mixed) URL or identifier returned by AddLink().
  4602. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4603. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4604. * @param $calign (string) 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>
  4605. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4606. * @public
  4607. * @since 1.0
  4608. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  4609. */
  4610. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  4611. $prev_cell_margin = $this->cell_margin;
  4612. $prev_cell_padding = $this->cell_padding;
  4613. $this->adjustCellPadding($border);
  4614. if (!$ignore_min_height) {
  4615. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  4616. if ($h < $min_cell_height) {
  4617. $h = $min_cell_height;
  4618. }
  4619. }
  4620. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  4621. // apply text shadow if enabled
  4622. if ($this->txtshadow['enabled']) {
  4623. // save data
  4624. $x = $this->x;
  4625. $y = $this->y;
  4626. $bc = $this->bgcolor;
  4627. $fc = $this->fgcolor;
  4628. $sc = $this->strokecolor;
  4629. $alpha = $this->alpha;
  4630. // print shadow
  4631. $this->x += $this->txtshadow['depth_w'];
  4632. $this->y += $this->txtshadow['depth_h'];
  4633. $this->SetFillColorArray($this->txtshadow['color']);
  4634. $this->SetTextColorArray($this->txtshadow['color']);
  4635. $this->SetDrawColorArray($this->txtshadow['color']);
  4636. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4637. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  4638. }
  4639. if ($this->state == 2) {
  4640. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4641. }
  4642. //restore data
  4643. $this->x = $x;
  4644. $this->y = $y;
  4645. $this->SetFillColorArray($bc);
  4646. $this->SetTextColorArray($fc);
  4647. $this->SetDrawColorArray($sc);
  4648. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4649. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  4650. }
  4651. }
  4652. if ($this->state == 2) {
  4653. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4654. }
  4655. $this->cell_padding = $prev_cell_padding;
  4656. $this->cell_margin = $prev_cell_margin;
  4657. }
  4658. /**
  4659. * 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 />
  4660. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4661. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4662. * @param $h (float) Cell height. Default value: 0.
  4663. * @param $txt (string) String to print. Default value: empty string.
  4664. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4665. * @param $ln (int) 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.
  4666. * @param $align (string) 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>
  4667. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4668. * @param $link (mixed) URL or identifier returned by AddLink().
  4669. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4670. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4671. * @param $calign (string) 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>
  4672. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul>
  4673. * @return string containing cell code
  4674. * @protected
  4675. * @since 1.0
  4676. * @see Cell()
  4677. */
  4678. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  4679. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  4680. $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
  4681. $prev_cell_margin = $this->cell_margin;
  4682. $prev_cell_padding = $this->cell_padding;
  4683. $txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode);
  4684. $rs = ''; //string to be returned
  4685. $this->adjustCellPadding($border);
  4686. if (!$ignore_min_height) {
  4687. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  4688. if ($h < $min_cell_height) {
  4689. $h = $min_cell_height;
  4690. }
  4691. }
  4692. $k = $this->k;
  4693. // check page for no-write regions and adapt page margins if necessary
  4694. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  4695. if ($this->rtl) {
  4696. $x = $this->x - $this->cell_margin['R'];
  4697. } else {
  4698. $x = $this->x + $this->cell_margin['L'];
  4699. }
  4700. $y = $this->y + $this->cell_margin['T'];
  4701. $prev_font_stretching = $this->font_stretching;
  4702. $prev_font_spacing = $this->font_spacing;
  4703. // cell vertical alignment
  4704. switch ($calign) {
  4705. case 'A': {
  4706. // font top
  4707. switch ($valign) {
  4708. case 'T': {
  4709. // top
  4710. $y -= $this->cell_padding['T'];
  4711. break;
  4712. }
  4713. case 'B': {
  4714. // bottom
  4715. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  4716. break;
  4717. }
  4718. default:
  4719. case 'C':
  4720. case 'M': {
  4721. // center
  4722. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  4723. break;
  4724. }
  4725. }
  4726. break;
  4727. }
  4728. case 'L': {
  4729. // font baseline
  4730. switch ($valign) {
  4731. case 'T': {
  4732. // top
  4733. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  4734. break;
  4735. }
  4736. case 'B': {
  4737. // bottom
  4738. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  4739. break;
  4740. }
  4741. default:
  4742. case 'C':
  4743. case 'M': {
  4744. // center
  4745. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  4746. break;
  4747. }
  4748. }
  4749. break;
  4750. }
  4751. case 'D': {
  4752. // font bottom
  4753. switch ($valign) {
  4754. case 'T': {
  4755. // top
  4756. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  4757. break;
  4758. }
  4759. case 'B': {
  4760. // bottom
  4761. $y -= ($h - $this->cell_padding['B']);
  4762. break;
  4763. }
  4764. default:
  4765. case 'C':
  4766. case 'M': {
  4767. // center
  4768. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  4769. break;
  4770. }
  4771. }
  4772. break;
  4773. }
  4774. case 'B': {
  4775. // cell bottom
  4776. $y -= $h;
  4777. break;
  4778. }
  4779. case 'C':
  4780. case 'M': {
  4781. // cell center
  4782. $y -= ($h / 2);
  4783. break;
  4784. }
  4785. default:
  4786. case 'T': {
  4787. // cell top
  4788. break;
  4789. }
  4790. }
  4791. // text vertical alignment
  4792. switch ($valign) {
  4793. case 'T': {
  4794. // top
  4795. $yt = $y + $this->cell_padding['T'];
  4796. break;
  4797. }
  4798. case 'B': {
  4799. // bottom
  4800. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  4801. break;
  4802. }
  4803. default:
  4804. case 'C':
  4805. case 'M': {
  4806. // center
  4807. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  4808. break;
  4809. }
  4810. }
  4811. $basefonty = $yt + $this->FontAscent;
  4812. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  4813. if ($this->rtl) {
  4814. $w = $x - $this->lMargin;
  4815. } else {
  4816. $w = $this->w - $this->rMargin - $x;
  4817. }
  4818. }
  4819. $s = '';
  4820. // fill and borders
  4821. if (is_string($border) AND (strlen($border) == 4)) {
  4822. // full border
  4823. $border = 1;
  4824. }
  4825. if ($fill OR ($border == 1)) {
  4826. if ($fill) {
  4827. $op = ($border == 1) ? 'B' : 'f';
  4828. } else {
  4829. $op = 'S';
  4830. }
  4831. if ($this->rtl) {
  4832. $xk = (($x - $w) * $k);
  4833. } else {
  4834. $xk = ($x * $k);
  4835. }
  4836. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  4837. }
  4838. // draw borders
  4839. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  4840. if ($txt != '') {
  4841. $txt2 = $txt;
  4842. if ($this->isunicode) {
  4843. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  4844. $txt2 = TCPDF_FONTS::UTF8ToLatin1($txt2, $this->isunicode, $this->CurrentFont);
  4845. } else {
  4846. $unicode = TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont); // array of UTF-8 unicode values
  4847. $unicode = TCPDF_FONTS::utf8Bidi($unicode, '', $this->tmprtl, $this->isunicode, $this->CurrentFont);
  4848. // replace thai chars (if any)
  4849. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  4850. // number of chars
  4851. $numchars = count($unicode);
  4852. // po pla, for far, for fan
  4853. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  4854. // do chada, to patak
  4855. $lowtail = array(0x0e0e, 0x0e0f);
  4856. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  4857. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  4858. // mai ek, mai tho, mai tri, mai chattawa, karan
  4859. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  4860. // sara u, sara uu, pinthu
  4861. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  4862. $output = array();
  4863. for ($i = 0; $i < $numchars; $i++) {
  4864. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  4865. $ch0 = $unicode[$i];
  4866. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  4867. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  4868. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  4869. if (in_array($ch0, $tonemark)) {
  4870. if ($chn == 0x0e33) {
  4871. // sara um
  4872. if (in_array($ch1, $longtail)) {
  4873. // tonemark at upper left
  4874. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4875. } else {
  4876. // tonemark at upper right (normal position)
  4877. $output[] = $ch0;
  4878. }
  4879. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  4880. // tonemark at lower left
  4881. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  4882. } elseif (in_array($ch1, $upvowel)) {
  4883. if (in_array($ch2, $longtail)) {
  4884. // tonemark at upper left
  4885. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4886. } else {
  4887. // tonemark at upper right (normal position)
  4888. $output[] = $ch0;
  4889. }
  4890. } else {
  4891. // tonemark at lower right
  4892. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  4893. }
  4894. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  4895. // add lower left nikhahit and sara aa
  4896. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  4897. $output[] = 0xf711;
  4898. $this->CurrentFont['subsetchars'][0xf711] = true;
  4899. $output[] = 0x0e32;
  4900. $this->CurrentFont['subsetchars'][0x0e32] = true;
  4901. } else {
  4902. $output[] = $ch0;
  4903. }
  4904. } elseif (in_array($ch1, $longtail)) {
  4905. if ($ch0 == 0x0e31) {
  4906. // lower left mai hun arkad
  4907. $output[] = $this->replaceChar($ch0, 0xf710);
  4908. } elseif (in_array($ch0, $upvowel)) {
  4909. // lower left
  4910. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  4911. } elseif ($ch0 == 0x0e47) {
  4912. // lower left mai tai koo
  4913. $output[] = $this->replaceChar($ch0, 0xf712);
  4914. } else {
  4915. // normal character
  4916. $output[] = $ch0;
  4917. }
  4918. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  4919. // lower vowel
  4920. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  4921. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  4922. // yo ying without lower part
  4923. $output[] = $this->replaceChar($ch0, 0xf70f);
  4924. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  4925. // tho santan without lower part
  4926. $output[] = $this->replaceChar($ch0, 0xf700);
  4927. } else {
  4928. $output[] = $ch0;
  4929. }
  4930. } else {
  4931. // non-thai character
  4932. $output[] = $unicode[$i];
  4933. }
  4934. }
  4935. $unicode = $output;
  4936. // update font subsetchars
  4937. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4938. } // end of K_THAI_TOPCHARS
  4939. $txt2 = TCPDF_FONTS::arrUTF8ToUTF16BE($unicode, false);
  4940. }
  4941. }
  4942. $txt2 = TCPDF_STATIC::_escape($txt2);
  4943. // get current text width (considering general font stretching and spacing)
  4944. $txwidth = $this->GetStringWidth($txt);
  4945. $width = $txwidth;
  4946. // check for stretch mode
  4947. if ($stretch > 0) {
  4948. // calculate ratio between cell width and text width
  4949. if ($width <= 0) {
  4950. $ratio = 1;
  4951. } else {
  4952. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  4953. }
  4954. // check if stretching is required
  4955. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  4956. // the text will be stretched to fit cell width
  4957. if ($stretch > 2) {
  4958. // set new character spacing
  4959. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  4960. } else {
  4961. // set new horizontal stretching
  4962. $this->font_stretching *= $ratio;
  4963. }
  4964. // recalculate text width (the text fills the entire cell)
  4965. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  4966. // reset alignment
  4967. $align = '';
  4968. }
  4969. }
  4970. if ($this->font_stretching != 100) {
  4971. // apply font stretching
  4972. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  4973. }
  4974. if ($this->font_spacing != 0) {
  4975. // increase/decrease font spacing
  4976. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  4977. }
  4978. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  4979. $s .= 'q '.$this->TextColor.' ';
  4980. }
  4981. // rendering mode
  4982. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  4983. // count number of spaces
  4984. $ns = substr_count($txt, chr(32));
  4985. // Justification
  4986. $spacewidth = 0;
  4987. if (($align == 'J') AND ($ns > 0)) {
  4988. if ($this->isUnicodeFont()) {
  4989. // get string width without spaces
  4990. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  4991. // calculate average space width
  4992. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / ($this->FontSize?$this->FontSize:1);
  4993. if ($this->font_stretching != 100) {
  4994. // word spacing is affected by stretching
  4995. $spacewidth /= ($this->font_stretching / 100);
  4996. }
  4997. // set word position to be used with TJ operator
  4998. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  4999. $unicode_justification = true;
  5000. } else {
  5001. // get string width
  5002. $width = $txwidth;
  5003. // new space width
  5004. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5005. if ($this->font_stretching != 100) {
  5006. // word spacing (Tw) is affected by stretching
  5007. $spacewidth /= ($this->font_stretching / 100);
  5008. }
  5009. // set word spacing
  5010. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5011. }
  5012. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5013. }
  5014. // replace carriage return characters
  5015. $txt2 = str_replace("\r", ' ', $txt2);
  5016. switch ($align) {
  5017. case 'C': {
  5018. $dx = ($w - $width) / 2;
  5019. break;
  5020. }
  5021. case 'R': {
  5022. if ($this->rtl) {
  5023. $dx = $this->cell_padding['R'];
  5024. } else {
  5025. $dx = $w - $width - $this->cell_padding['R'];
  5026. }
  5027. break;
  5028. }
  5029. case 'L': {
  5030. if ($this->rtl) {
  5031. $dx = $w - $width - $this->cell_padding['L'];
  5032. } else {
  5033. $dx = $this->cell_padding['L'];
  5034. }
  5035. break;
  5036. }
  5037. case 'J':
  5038. default: {
  5039. if ($this->rtl) {
  5040. $dx = $this->cell_padding['R'];
  5041. } else {
  5042. $dx = $this->cell_padding['L'];
  5043. }
  5044. break;
  5045. }
  5046. }
  5047. if ($this->rtl) {
  5048. $xdx = $x - $dx - $width;
  5049. } else {
  5050. $xdx = $x + $dx;
  5051. }
  5052. $xdk = $xdx * $k;
  5053. // print text
  5054. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5055. if (isset($uniblock)) {
  5056. // print overlapping characters as separate string
  5057. $xshift = 0; // horizontal shift
  5058. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5059. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5060. foreach ($uniblock as $uk => $uniarr) {
  5061. if (($uk % 2) == 0) {
  5062. // x space to skip
  5063. if ($spacewidth != 0) {
  5064. // justification shift
  5065. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5066. }
  5067. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5068. } else {
  5069. // character to print
  5070. $topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false);
  5071. $topchr = TCPDF_STATIC::_escape($topchr);
  5072. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5073. }
  5074. }
  5075. }
  5076. if ($this->underline) {
  5077. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5078. }
  5079. if ($this->linethrough) {
  5080. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5081. }
  5082. if ($this->overline) {
  5083. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5084. }
  5085. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5086. $s .= ' Q';
  5087. }
  5088. if ($link) {
  5089. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5090. }
  5091. }
  5092. // output cell
  5093. if ($s) {
  5094. // output cell
  5095. $rs .= $s;
  5096. if ($this->font_spacing != 0) {
  5097. // reset font spacing mode
  5098. $rs .= ' BT 0 Tc ET';
  5099. }
  5100. if ($this->font_stretching != 100) {
  5101. // reset font stretching mode
  5102. $rs .= ' BT 100 Tz ET';
  5103. }
  5104. }
  5105. // reset word spacing
  5106. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5107. $rs .= ' BT 0 Tw ET';
  5108. }
  5109. // reset stretching and spacing
  5110. $this->font_stretching = $prev_font_stretching;
  5111. $this->font_spacing = $prev_font_spacing;
  5112. $this->lasth = $h;
  5113. if ($ln > 0) {
  5114. //Go to the beginning of the next line
  5115. $this->y = $y + $h + $this->cell_margin['B'];
  5116. if ($ln == 1) {
  5117. if ($this->rtl) {
  5118. $this->x = $this->w - $this->rMargin;
  5119. } else {
  5120. $this->x = $this->lMargin;
  5121. }
  5122. }
  5123. } else {
  5124. // go left or right by case
  5125. if ($this->rtl) {
  5126. $this->x = $x - $w - $this->cell_margin['L'];
  5127. } else {
  5128. $this->x = $x + $w + $this->cell_margin['R'];
  5129. }
  5130. }
  5131. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5132. $rs = $gstyles.$rs;
  5133. $this->cell_padding = $prev_cell_padding;
  5134. $this->cell_margin = $prev_cell_margin;
  5135. return $rs;
  5136. }
  5137. /**
  5138. * Replace a char if is defined on the current font.
  5139. * @param $oldchar (int) Integer code (unicode) of the character to replace.
  5140. * @param $newchar (int) Integer code (unicode) of the new character.
  5141. * @return int the replaced char or the old char in case the new char i not defined
  5142. * @protected
  5143. * @since 5.9.167 (2012-06-22)
  5144. */
  5145. protected function replaceChar($oldchar, $newchar) {
  5146. if ($this->isCharDefined($newchar)) {
  5147. // add the new char on the subset list
  5148. $this->CurrentFont['subsetchars'][$newchar] = true;
  5149. // return the new character
  5150. return $newchar;
  5151. }
  5152. // return the old char
  5153. return $oldchar;
  5154. }
  5155. /**
  5156. * Returns the code to draw the cell border
  5157. * @param $x (float) X coordinate.
  5158. * @param $y (float) Y coordinate.
  5159. * @param $w (float) Cell width.
  5160. * @param $h (float) Cell height.
  5161. * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5162. * @return string containing cell border code
  5163. * @protected
  5164. * @see SetLineStyle()
  5165. * @since 5.7.000 (2010-08-02)
  5166. */
  5167. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5168. $s = ''; // string to be returned
  5169. if (empty($brd)) {
  5170. return $s;
  5171. }
  5172. if ($brd == 1) {
  5173. $brd = array('LRTB' => true);
  5174. }
  5175. // calculate coordinates for border
  5176. $k = $this->k;
  5177. if ($this->rtl) {
  5178. $xeL = ($x - $w) * $k;
  5179. $xeR = $x * $k;
  5180. } else {
  5181. $xeL = $x * $k;
  5182. $xeR = ($x + $w) * $k;
  5183. }
  5184. $yeL = (($this->h - ($y + $h)) * $k);
  5185. $yeT = (($this->h - $y) * $k);
  5186. $xeT = $xeL;
  5187. $xeB = $xeR;
  5188. $yeR = $yeT;
  5189. $yeB = $yeL;
  5190. if (is_string($brd)) {
  5191. // convert string to array
  5192. $slen = strlen($brd);
  5193. $newbrd = array();
  5194. for ($i = 0; $i < $slen; ++$i) {
  5195. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5196. }
  5197. $brd = $newbrd;
  5198. }
  5199. if (isset($brd['mode'])) {
  5200. $mode = $brd['mode'];
  5201. unset($brd['mode']);
  5202. } else {
  5203. $mode = 'normal';
  5204. }
  5205. foreach ($brd as $border => $style) {
  5206. if (is_array($style) AND !empty($style)) {
  5207. // apply border style
  5208. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5209. $s .= $this->SetLineStyle($style, true)."\n";
  5210. }
  5211. switch ($mode) {
  5212. case 'ext': {
  5213. $off = (($this->LineWidth / 2) * $k);
  5214. $xL = $xeL - $off;
  5215. $xR = $xeR + $off;
  5216. $yT = $yeT + $off;
  5217. $yL = $yeL - $off;
  5218. $xT = $xL;
  5219. $xB = $xR;
  5220. $yR = $yT;
  5221. $yB = $yL;
  5222. $w += $this->LineWidth;
  5223. $h += $this->LineWidth;
  5224. break;
  5225. }
  5226. case 'int': {
  5227. $off = ($this->LineWidth / 2) * $k;
  5228. $xL = $xeL + $off;
  5229. $xR = $xeR - $off;
  5230. $yT = $yeT - $off;
  5231. $yL = $yeL + $off;
  5232. $xT = $xL;
  5233. $xB = $xR;
  5234. $yR = $yT;
  5235. $yB = $yL;
  5236. $w -= $this->LineWidth;
  5237. $h -= $this->LineWidth;
  5238. break;
  5239. }
  5240. case 'normal':
  5241. default: {
  5242. $xL = $xeL;
  5243. $xT = $xeT;
  5244. $xB = $xeB;
  5245. $xR = $xeR;
  5246. $yL = $yeL;
  5247. $yT = $yeT;
  5248. $yB = $yeB;
  5249. $yR = $yeR;
  5250. break;
  5251. }
  5252. }
  5253. // draw borders by case
  5254. if (strlen($border) == 4) {
  5255. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5256. } elseif (strlen($border) == 3) {
  5257. if (strpos($border,'B') === false) { // LTR
  5258. $s .= sprintf('%F %F m ', $xL, $yL);
  5259. $s .= sprintf('%F %F l ', $xT, $yT);
  5260. $s .= sprintf('%F %F l ', $xR, $yR);
  5261. $s .= sprintf('%F %F l ', $xB, $yB);
  5262. $s .= 'S ';
  5263. } elseif (strpos($border,'L') === false) { // TRB
  5264. $s .= sprintf('%F %F m ', $xT, $yT);
  5265. $s .= sprintf('%F %F l ', $xR, $yR);
  5266. $s .= sprintf('%F %F l ', $xB, $yB);
  5267. $s .= sprintf('%F %F l ', $xL, $yL);
  5268. $s .= 'S ';
  5269. } elseif (strpos($border,'T') === false) { // RBL
  5270. $s .= sprintf('%F %F m ', $xR, $yR);
  5271. $s .= sprintf('%F %F l ', $xB, $yB);
  5272. $s .= sprintf('%F %F l ', $xL, $yL);
  5273. $s .= sprintf('%F %F l ', $xT, $yT);
  5274. $s .= 'S ';
  5275. } elseif (strpos($border,'R') === false) { // BLT
  5276. $s .= sprintf('%F %F m ', $xB, $yB);
  5277. $s .= sprintf('%F %F l ', $xL, $yL);
  5278. $s .= sprintf('%F %F l ', $xT, $yT);
  5279. $s .= sprintf('%F %F l ', $xR, $yR);
  5280. $s .= 'S ';
  5281. }
  5282. } elseif (strlen($border) == 2) {
  5283. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5284. $s .= sprintf('%F %F m ', $xL, $yL);
  5285. $s .= sprintf('%F %F l ', $xT, $yT);
  5286. $s .= sprintf('%F %F l ', $xR, $yR);
  5287. $s .= 'S ';
  5288. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5289. $s .= sprintf('%F %F m ', $xT, $yT);
  5290. $s .= sprintf('%F %F l ', $xR, $yR);
  5291. $s .= sprintf('%F %F l ', $xB, $yB);
  5292. $s .= 'S ';
  5293. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5294. $s .= sprintf('%F %F m ', $xR, $yR);
  5295. $s .= sprintf('%F %F l ', $xB, $yB);
  5296. $s .= sprintf('%F %F l ', $xL, $yL);
  5297. $s .= 'S ';
  5298. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5299. $s .= sprintf('%F %F m ', $xB, $yB);
  5300. $s .= sprintf('%F %F l ', $xL, $yL);
  5301. $s .= sprintf('%F %F l ', $xT, $yT);
  5302. $s .= 'S ';
  5303. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5304. $s .= sprintf('%F %F m ', $xL, $yL);
  5305. $s .= sprintf('%F %F l ', $xT, $yT);
  5306. $s .= 'S ';
  5307. $s .= sprintf('%F %F m ', $xR, $yR);
  5308. $s .= sprintf('%F %F l ', $xB, $yB);
  5309. $s .= 'S ';
  5310. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5311. $s .= sprintf('%F %F m ', $xT, $yT);
  5312. $s .= sprintf('%F %F l ', $xR, $yR);
  5313. $s .= 'S ';
  5314. $s .= sprintf('%F %F m ', $xB, $yB);
  5315. $s .= sprintf('%F %F l ', $xL, $yL);
  5316. $s .= 'S ';
  5317. }
  5318. } else { // strlen($border) == 1
  5319. if (strpos($border,'L') !== false) { // L
  5320. $s .= sprintf('%F %F m ', $xL, $yL);
  5321. $s .= sprintf('%F %F l ', $xT, $yT);
  5322. $s .= 'S ';
  5323. } elseif (strpos($border,'T') !== false) { // T
  5324. $s .= sprintf('%F %F m ', $xT, $yT);
  5325. $s .= sprintf('%F %F l ', $xR, $yR);
  5326. $s .= 'S ';
  5327. } elseif (strpos($border,'R') !== false) { // R
  5328. $s .= sprintf('%F %F m ', $xR, $yR);
  5329. $s .= sprintf('%F %F l ', $xB, $yB);
  5330. $s .= 'S ';
  5331. } elseif (strpos($border,'B') !== false) { // B
  5332. $s .= sprintf('%F %F m ', $xB, $yB);
  5333. $s .= sprintf('%F %F l ', $xL, $yL);
  5334. $s .= 'S ';
  5335. }
  5336. }
  5337. if (is_array($style) AND !empty($style)) {
  5338. // reset border style to previous value
  5339. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5340. }
  5341. }
  5342. return $s;
  5343. }
  5344. /**
  5345. * This method allows printing text with line breaks.
  5346. * 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 />
  5347. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5348. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5349. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5350. * @param $txt (string) String to print
  5351. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5352. * @param $align (string) 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>
  5353. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5354. * @param $ln (int) 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>
  5355. * @param $x (float) x position in user units
  5356. * @param $y (float) y position in user units
  5357. * @param $reseth (boolean) if true reset the last cell height (default true).
  5358. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  5359. * @param $ishtml (boolean) INTERNAL USE ONLY -- set to true if $txt is HTML content (default = false). Never set this parameter to true, use instead writeHTMLCell() or writeHTML() methods.
  5360. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5361. * @param $maxh (float) 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.
  5362. * @param $valign (string) Vertical alignment of text (requires $maxh = $h > 0). Possible values are:<ul><li>T: TOP</li><li>M: middle</li><li>B: bottom</li></ul>. This feature works only when $ishtml=false and the cell must fit in a single page.
  5363. * @param $fitcell (boolean) if true attempt to fit all the text within the cell by reducing the font size (do not work in HTML mode).
  5364. * @return int Return the number of cells or 1 for html mode.
  5365. * @public
  5366. * @since 1.3
  5367. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5368. */
  5369. public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) {
  5370. $prev_cell_margin = $this->cell_margin;
  5371. $prev_cell_padding = $this->cell_padding;
  5372. // adjust internal padding
  5373. $this->adjustCellPadding($border);
  5374. $mc_padding = $this->cell_padding;
  5375. $mc_margin = $this->cell_margin;
  5376. $this->cell_padding['T'] = 0;
  5377. $this->cell_padding['B'] = 0;
  5378. $this->setCellMargins(0, 0, 0, 0);
  5379. if (TCPDF_STATIC::empty_string($this->lasth) OR $reseth) {
  5380. // reset row height
  5381. $this->resetLastH();
  5382. }
  5383. if (!TCPDF_STATIC::empty_string($y)) {
  5384. $this->SetY($y);
  5385. } else {
  5386. $y = $this->GetY();
  5387. }
  5388. $resth = 0;
  5389. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5390. // spit cell in more pages/columns
  5391. $newh = ($this->PageBreakTrigger - $y);
  5392. $resth = ($h - $newh); // cell to be printed on the next page/column
  5393. $h = $newh;
  5394. }
  5395. // get current page number
  5396. $startpage = $this->page;
  5397. // get current column
  5398. $startcolumn = $this->current_column;
  5399. if (!TCPDF_STATIC::empty_string($x)) {
  5400. $this->SetX($x);
  5401. } else {
  5402. $x = $this->GetX();
  5403. }
  5404. // check page for no-write regions and adapt page margins if necessary
  5405. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5406. // apply margins
  5407. $oy = $y + $mc_margin['T'];
  5408. if ($this->rtl) {
  5409. $ox = ($this->w - $x - $mc_margin['R']);
  5410. } else {
  5411. $ox = ($x + $mc_margin['L']);
  5412. }
  5413. $this->x = $ox;
  5414. $this->y = $oy;
  5415. // set width
  5416. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5417. if ($this->rtl) {
  5418. $w = ($this->x - $this->lMargin - $mc_margin['L']);
  5419. } else {
  5420. $w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
  5421. }
  5422. }
  5423. // store original margin values
  5424. $lMargin = $this->lMargin;
  5425. $rMargin = $this->rMargin;
  5426. if ($this->rtl) {
  5427. $this->rMargin = ($this->w - $this->x);
  5428. $this->lMargin = ($this->x - $w);
  5429. } else {
  5430. $this->lMargin = ($this->x);
  5431. $this->rMargin = ($this->w - $this->x - $w);
  5432. }
  5433. $this->clMargin = $this->lMargin;
  5434. $this->crMargin = $this->rMargin;
  5435. if ($autopadding) {
  5436. // add top padding
  5437. $this->y += $mc_padding['T'];
  5438. }
  5439. if ($ishtml) { // ******* Write HTML text
  5440. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5441. $nl = 1;
  5442. } else { // ******* Write simple text
  5443. $prev_FontSizePt = $this->FontSizePt;
  5444. // vertical alignment
  5445. if ($maxh > 0) {
  5446. // get text height
  5447. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5448. if ($fitcell) {
  5449. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5450. $fmin = 1;
  5451. $fmax = $this->FontSizePt;
  5452. $prev_text_height = $text_height;
  5453. $maxit = 100; // max number of iterations
  5454. while ($maxit > 0) {
  5455. $fmid = (($fmax + $fmin) / 2);
  5456. $this->SetFontSize($fmid, false);
  5457. $this->resetLastH();
  5458. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5459. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  5460. break;
  5461. } elseif ($text_height < $maxh) {
  5462. $fmin = $fmid;
  5463. } else {
  5464. $fmax = $fmid;
  5465. }
  5466. --$maxit;
  5467. }
  5468. $this->SetFontSize($this->FontSizePt);
  5469. }
  5470. if ($text_height < $maxh) {
  5471. if ($valign == 'M') {
  5472. // text vertically centered
  5473. $this->y += (($maxh - $text_height) / 2);
  5474. } elseif ($valign == 'B') {
  5475. // text vertically aligned on bottom
  5476. $this->y += ($maxh - $text_height);
  5477. }
  5478. }
  5479. }
  5480. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  5481. if ($fitcell) {
  5482. // restore font size
  5483. $this->SetFontSize($prev_FontSizePt);
  5484. }
  5485. }
  5486. if ($autopadding) {
  5487. // add bottom padding
  5488. $this->y += $mc_padding['B'];
  5489. }
  5490. // Get end-of-text Y position
  5491. $currentY = $this->y;
  5492. // get latest page number
  5493. $endpage = $this->page;
  5494. if ($resth > 0) {
  5495. $skip = ($endpage - $startpage);
  5496. $tmpresth = $resth;
  5497. while ($tmpresth > 0) {
  5498. if ($skip <= 0) {
  5499. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  5500. $this->checkPageBreak($this->PageBreakTrigger + 1);
  5501. }
  5502. if ($this->num_columns > 1) {
  5503. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  5504. } else {
  5505. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  5506. }
  5507. --$skip;
  5508. }
  5509. $currentY = $this->y;
  5510. $endpage = $this->page;
  5511. }
  5512. // get latest column
  5513. $endcolumn = $this->current_column;
  5514. if ($this->num_columns == 0) {
  5515. $this->num_columns = 1;
  5516. }
  5517. // disable page regions check
  5518. $check_page_regions = $this->check_page_regions;
  5519. $this->check_page_regions = false;
  5520. // get border modes
  5521. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  5522. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  5523. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  5524. // design borders around HTML cells.
  5525. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  5526. $ccode = '';
  5527. $this->setPage($page);
  5528. if ($this->num_columns < 2) {
  5529. // single-column mode
  5530. $this->SetX($x);
  5531. $this->y = $this->tMargin;
  5532. }
  5533. // account for margin changes
  5534. if ($page > $startpage) {
  5535. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  5536. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  5537. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  5538. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  5539. }
  5540. }
  5541. if ($startpage == $endpage) {
  5542. // single page
  5543. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  5544. $this->selectColumn($column);
  5545. if ($this->rtl) {
  5546. $this->x -= $mc_margin['R'];
  5547. } else {
  5548. $this->x += $mc_margin['L'];
  5549. }
  5550. if ($startcolumn == $endcolumn) { // single column
  5551. $cborder = $border;
  5552. $h = max($h, ($currentY - $oy));
  5553. $this->y = $oy;
  5554. } elseif ($column == $startcolumn) { // first column
  5555. $cborder = $border_start;
  5556. $this->y = $oy;
  5557. $h = $this->h - $this->y - $this->bMargin;
  5558. } elseif ($column == $endcolumn) { // end column
  5559. $cborder = $border_end;
  5560. $h = $currentY - $this->y;
  5561. if ($resth > $h) {
  5562. $h = $resth;
  5563. }
  5564. } else { // middle column
  5565. $cborder = $border_middle;
  5566. $h = $this->h - $this->y - $this->bMargin;
  5567. $resth -= $h;
  5568. }
  5569. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5570. } // end for each column
  5571. } elseif ($page == $startpage) { // first page
  5572. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  5573. $this->selectColumn($column);
  5574. if ($this->rtl) {
  5575. $this->x -= $mc_margin['R'];
  5576. } else {
  5577. $this->x += $mc_margin['L'];
  5578. }
  5579. if ($column == $startcolumn) { // first column
  5580. $cborder = $border_start;
  5581. $this->y = $oy;
  5582. $h = $this->h - $this->y - $this->bMargin;
  5583. } else { // middle column
  5584. $cborder = $border_middle;
  5585. $h = $this->h - $this->y - $this->bMargin;
  5586. $resth -= $h;
  5587. }
  5588. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5589. } // end for each column
  5590. } elseif ($page == $endpage) { // last page
  5591. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  5592. $this->selectColumn($column);
  5593. if ($this->rtl) {
  5594. $this->x -= $mc_margin['R'];
  5595. } else {
  5596. $this->x += $mc_margin['L'];
  5597. }
  5598. if ($column == $endcolumn) {
  5599. // end column
  5600. $cborder = $border_end;
  5601. $h = $currentY - $this->y;
  5602. if ($resth > $h) {
  5603. $h = $resth;
  5604. }
  5605. } else {
  5606. // middle column
  5607. $cborder = $border_middle;
  5608. $h = $this->h - $this->y - $this->bMargin;
  5609. $resth -= $h;
  5610. }
  5611. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5612. } // end for each column
  5613. } else { // middle page
  5614. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  5615. $this->selectColumn($column);
  5616. if ($this->rtl) {
  5617. $this->x -= $mc_margin['R'];
  5618. } else {
  5619. $this->x += $mc_margin['L'];
  5620. }
  5621. $cborder = $border_middle;
  5622. $h = $this->h - $this->y - $this->bMargin;
  5623. $resth -= $h;
  5624. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5625. } // end for each column
  5626. }
  5627. if ($cborder OR $fill) {
  5628. $offsetlen = strlen($ccode);
  5629. // draw border and fill
  5630. if ($this->inxobj) {
  5631. // we are inside an XObject template
  5632. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  5633. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  5634. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  5635. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  5636. } else {
  5637. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  5638. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  5639. }
  5640. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  5641. $pstart = substr($pagebuff, 0, $pagemark);
  5642. $pend = substr($pagebuff, $pagemark);
  5643. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  5644. } else {
  5645. if (end($this->transfmrk[$this->page]) !== false) {
  5646. $pagemarkkey = key($this->transfmrk[$this->page]);
  5647. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  5648. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  5649. } elseif ($this->InFooter) {
  5650. $pagemark = $this->footerpos[$this->page];
  5651. $this->footerpos[$this->page] += $offsetlen;
  5652. } else {
  5653. $pagemark = $this->intmrk[$this->page];
  5654. $this->intmrk[$this->page] += $offsetlen;
  5655. }
  5656. $pagebuff = $this->getPageBuffer($this->page);
  5657. $pstart = substr($pagebuff, 0, $pagemark);
  5658. $pend = substr($pagebuff, $pagemark);
  5659. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  5660. }
  5661. }
  5662. } // end for each page
  5663. // restore page regions check
  5664. $this->check_page_regions = $check_page_regions;
  5665. // Get end-of-cell Y position
  5666. $currentY = $this->GetY();
  5667. // restore previous values
  5668. if ($this->num_columns > 1) {
  5669. $this->selectColumn();
  5670. } else {
  5671. // restore original margins
  5672. $this->lMargin = $lMargin;
  5673. $this->rMargin = $rMargin;
  5674. if ($this->page > $startpage) {
  5675. // check for margin variations between pages (i.e. booklet mode)
  5676. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  5677. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  5678. if (($dl != 0) OR ($dr != 0)) {
  5679. $this->lMargin += $dl;
  5680. $this->rMargin += $dr;
  5681. }
  5682. }
  5683. }
  5684. if ($ln > 0) {
  5685. //Go to the beginning of the next line
  5686. $this->SetY($currentY + $mc_margin['B']);
  5687. if ($ln == 2) {
  5688. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5689. }
  5690. } else {
  5691. // go left or right by case
  5692. $this->setPage($startpage);
  5693. $this->y = $y;
  5694. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5695. }
  5696. $this->setContentMark();
  5697. $this->cell_padding = $prev_cell_padding;
  5698. $this->cell_margin = $prev_cell_margin;
  5699. $this->clMargin = $this->lMargin;
  5700. $this->crMargin = $this->rMargin;
  5701. return $nl;
  5702. }
  5703. /**
  5704. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  5705. * @param $txt (string) String for calculating his height
  5706. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5707. * @param $reseth (boolean) if true reset the last cell height (default false).
  5708. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5709. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5710. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5711. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5712. * @author Alexander Escalona Fern\E1ndez, Nicola Asuni
  5713. * @public
  5714. * @since 4.5.011
  5715. */
  5716. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5717. if ($txt === '') {
  5718. // empty string
  5719. return 1;
  5720. }
  5721. // adjust internal padding
  5722. $prev_cell_padding = $this->cell_padding;
  5723. $prev_lasth = $this->lasth;
  5724. if (is_array($cellpadding)) {
  5725. $this->cell_padding = $cellpadding;
  5726. }
  5727. $this->adjustCellPadding($border);
  5728. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5729. if ($this->rtl) {
  5730. $w = $this->x - $this->lMargin;
  5731. } else {
  5732. $w = $this->w - $this->rMargin - $this->x;
  5733. }
  5734. }
  5735. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5736. if ($reseth) {
  5737. // reset row height
  5738. $this->resetLastH();
  5739. }
  5740. $lines = 1;
  5741. $sum = 0;
  5742. $chars = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont), $txt, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5743. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  5744. $length = count($chars);
  5745. $lastSeparator = -1;
  5746. for ($i = 0; $i < $length; ++$i) {
  5747. $charWidth = $charsWidth[$i];
  5748. if (preg_match($this->re_spaces, TCPDF_FONTS::unichr($chars[$i], $this->isunicode))) {
  5749. $lastSeparator = $i;
  5750. }
  5751. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  5752. ++$lines;
  5753. if ($chars[$i] == 10) {
  5754. $lastSeparator = -1;
  5755. $sum = 0;
  5756. } elseif ($lastSeparator != -1) {
  5757. $i = $lastSeparator;
  5758. $lastSeparator = -1;
  5759. $sum = 0;
  5760. } else {
  5761. $sum = $charWidth;
  5762. }
  5763. } else {
  5764. $sum += $charWidth;
  5765. }
  5766. }
  5767. if ($chars[($length - 1)] == 10) {
  5768. --$lines;
  5769. }
  5770. $this->cell_padding = $prev_cell_padding;
  5771. $this->lasth = $prev_lasth;
  5772. return $lines;
  5773. }
  5774. /**
  5775. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  5776. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  5777. * @pre
  5778. * // store current object
  5779. * $pdf->startTransaction();
  5780. * // store starting values
  5781. * $start_y = $pdf->GetY();
  5782. * $start_page = $pdf->getPage();
  5783. * // call your printing functions with your parameters
  5784. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5785. * $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
  5786. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5787. * // get the new Y
  5788. * $end_y = $pdf->GetY();
  5789. * $end_page = $pdf->getPage();
  5790. * // calculate height
  5791. * $height = 0;
  5792. * if ($end_page == $start_page) {
  5793. * $height = $end_y - $start_y;
  5794. * } else {
  5795. * for ($page=$start_page; $page <= $end_page; ++$page) {
  5796. * $this->setPage($page);
  5797. * if ($page == $start_page) {
  5798. * // first page
  5799. * $height = $this->h - $start_y - $this->bMargin;
  5800. * } elseif ($page == $end_page) {
  5801. * // last page
  5802. * $height = $end_y - $this->tMargin;
  5803. * } else {
  5804. * $height = $this->h - $this->tMargin - $this->bMargin;
  5805. * }
  5806. * }
  5807. * }
  5808. * // restore previous object
  5809. * $pdf = $pdf->rollbackTransaction();
  5810. *
  5811. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5812. * @param $txt (string) String for calculating his height
  5813. * @param $reseth (boolean) if true reset the last cell height (default false).
  5814. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5815. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5816. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5817. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5818. * @author Nicola Asuni, Alexander Escalona Fern\E1ndez
  5819. * @public
  5820. */
  5821. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5822. // adjust internal padding
  5823. $prev_cell_padding = $this->cell_padding;
  5824. $prev_lasth = $this->lasth;
  5825. if (is_array($cellpadding)) {
  5826. $this->cell_padding = $cellpadding;
  5827. }
  5828. $this->adjustCellPadding($border);
  5829. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  5830. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  5831. if ($autopadding) {
  5832. // add top and bottom padding
  5833. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  5834. }
  5835. $this->cell_padding = $prev_cell_padding;
  5836. $this->lasth = $prev_lasth;
  5837. return $height;
  5838. }
  5839. /**
  5840. * This method prints text from the current position.<br />
  5841. * @param $h (float) Line height
  5842. * @param $txt (string) String to print
  5843. * @param $link (mixed) URL or identifier returned by AddLink()
  5844. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5845. * @param $align (string) 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>
  5846. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  5847. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  5848. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  5849. * @param $firstblock (boolean) if true the string is the starting of a line.
  5850. * @param $maxh (float) maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  5851. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  5852. * @param $margin (array) margin array of the parent container
  5853. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  5854. * @public
  5855. * @since 1.5
  5856. */
  5857. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  5858. // check page for no-write regions and adapt page margins if necessary
  5859. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5860. if (strlen($txt) == 0) {
  5861. // fix empty text
  5862. $txt = ' ';
  5863. }
  5864. if ($margin === '') {
  5865. // set default margins
  5866. $margin = $this->cell_margin;
  5867. }
  5868. // remove carriage returns
  5869. $s = str_replace("\r", '', $txt);
  5870. // check if string contains arabic text
  5871. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $s)) {
  5872. $arabic = true;
  5873. } else {
  5874. $arabic = false;
  5875. }
  5876. // check if string contains RTL text
  5877. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $s)) {
  5878. $rtlmode = true;
  5879. } else {
  5880. $rtlmode = false;
  5881. }
  5882. // get a char width
  5883. $chrwidth = $this->GetCharWidth(46); // dot character
  5884. // get array of unicode values
  5885. $chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);
  5886. // calculate maximum width for a single character on string
  5887. $chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
  5888. array_walk($chrw, array($this, 'getRawCharWidth'));
  5889. $maxchwidth = max($chrw);
  5890. // get array of chars
  5891. $uchars = TCPDF_FONTS::UTF8ArrayToUniArray($chars, $this->isunicode);
  5892. // get the number of characters
  5893. $nb = count($chars);
  5894. // replacement for SHY character (minus symbol)
  5895. $shy_replacement = 45;
  5896. $shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
  5897. // widht for SHY replacement
  5898. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  5899. // max Y
  5900. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  5901. // page width
  5902. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  5903. // calculate remaining line width ($w)
  5904. if ($this->rtl) {
  5905. $w = $this->x - $this->lMargin;
  5906. } else {
  5907. $w = $this->w - $this->rMargin - $this->x;
  5908. }
  5909. // max column width
  5910. $wmax = ($w - $wadj);
  5911. if (!$firstline) {
  5912. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  5913. }
  5914. if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
  5915. // the maximum width character do not fit on column
  5916. return '';
  5917. }
  5918. // minimum row height
  5919. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  5920. $start_page = $this->page;
  5921. $i = 0; // character position
  5922. $j = 0; // current starting position
  5923. $sep = -1; // position of the last blank space
  5924. $shy = false; // true if the last blank is a soft hypen (SHY)
  5925. $l = 0; // current string length
  5926. $nl = 0; //number of lines
  5927. $linebreak = false;
  5928. $pc = 0; // previous character
  5929. // for each character
  5930. while ($i < $nb) {
  5931. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  5932. break;
  5933. }
  5934. //Get the current character
  5935. $c = $chars[$i];
  5936. if ($c == 10) { // 10 = "\n" = new line
  5937. //Explicit line break
  5938. if ($align == 'J') {
  5939. if ($this->rtl) {
  5940. $talign = 'R';
  5941. } else {
  5942. $talign = 'L';
  5943. }
  5944. } else {
  5945. $talign = $align;
  5946. }
  5947. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  5948. if ($firstline) {
  5949. $startx = $this->x;
  5950. $tmparr = array_slice($chars, $j, ($i - $j));
  5951. if ($rtlmode) {
  5952. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5953. }
  5954. $linew = $this->GetArrStringWidth($tmparr);
  5955. unset($tmparr);
  5956. if ($this->rtl) {
  5957. $this->endlinex = $startx - $linew;
  5958. } else {
  5959. $this->endlinex = $startx + $linew;
  5960. }
  5961. $w = $linew;
  5962. $tmpcellpadding = $this->cell_padding;
  5963. if ($maxh == 0) {
  5964. $this->SetCellPadding(0);
  5965. }
  5966. }
  5967. if ($firstblock AND $this->isRTLTextDir()) {
  5968. $tmpstr = $this->stringRightTrim($tmpstr);
  5969. }
  5970. // Skip newlines at the begining of a page or column
  5971. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  5972. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  5973. }
  5974. unset($tmpstr);
  5975. if ($firstline) {
  5976. $this->cell_padding = $tmpcellpadding;
  5977. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  5978. }
  5979. ++$nl;
  5980. $j = $i + 1;
  5981. $l = 0;
  5982. $sep = -1;
  5983. $shy = false;
  5984. // account for margin changes
  5985. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  5986. $this->AcceptPageBreak();
  5987. if ($this->rtl) {
  5988. $this->x -= $margin['R'];
  5989. } else {
  5990. $this->x += $margin['L'];
  5991. }
  5992. $this->lMargin += $margin['L'];
  5993. $this->rMargin += $margin['R'];
  5994. }
  5995. $w = $this->getRemainingWidth();
  5996. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  5997. } else {
  5998. // 160 is the non-breaking space.
  5999. // 173 is SHY (Soft Hypen).
  6000. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6001. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6002. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6003. if (($c != 160)
  6004. AND (($c == 173)
  6005. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  6006. OR (($c == 45)
  6007. AND ($i < ($nb - 1))
  6008. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($pc, $this->isunicode))
  6009. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  6010. )
  6011. )
  6012. ) {
  6013. // update last blank space position
  6014. $sep = $i;
  6015. // check if is a SHY
  6016. if (($c == 173) OR ($c == 45)) {
  6017. $shy = true;
  6018. if ($pc == 45) {
  6019. $tmp_shy_replacement_width = 0;
  6020. $tmp_shy_replacement_char = '';
  6021. } else {
  6022. $tmp_shy_replacement_width = $shy_replacement_width;
  6023. $tmp_shy_replacement_char = $shy_replacement_char;
  6024. }
  6025. } else {
  6026. $shy = false;
  6027. }
  6028. }
  6029. // update string length
  6030. if ($this->isUnicodeFont() AND ($arabic)) {
  6031. // with bidirectional algorithm some chars may be changed affecting the line length
  6032. // *** very slow ***
  6033. $l = $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl, $this->isunicode, $this->CurrentFont));
  6034. } else {
  6035. $l += $this->GetCharWidth($c);
  6036. }
  6037. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6038. // we have reached the end of column
  6039. if ($sep == -1) {
  6040. // check if the line was already started
  6041. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  6042. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  6043. // print a void cell and go to next line
  6044. $this->Cell($w, $h, '', 0, 1);
  6045. $linebreak = true;
  6046. if ($firstline) {
  6047. return (TCPDF_FONTS::UniArrSubString($uchars, $j));
  6048. }
  6049. } else {
  6050. // truncate the word because do not fit on column
  6051. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6052. if ($firstline) {
  6053. $startx = $this->x;
  6054. $tmparr = array_slice($chars, $j, ($i - $j));
  6055. if ($rtlmode) {
  6056. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6057. }
  6058. $linew = $this->GetArrStringWidth($tmparr);
  6059. unset($tmparr);
  6060. if ($this->rtl) {
  6061. $this->endlinex = $startx - $linew;
  6062. } else {
  6063. $this->endlinex = $startx + $linew;
  6064. }
  6065. $w = $linew;
  6066. $tmpcellpadding = $this->cell_padding;
  6067. if ($maxh == 0) {
  6068. $this->SetCellPadding(0);
  6069. }
  6070. }
  6071. if ($firstblock AND $this->isRTLTextDir()) {
  6072. $tmpstr = $this->stringRightTrim($tmpstr);
  6073. }
  6074. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6075. unset($tmpstr);
  6076. if ($firstline) {
  6077. $this->cell_padding = $tmpcellpadding;
  6078. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6079. }
  6080. $j = $i;
  6081. --$i;
  6082. }
  6083. } else {
  6084. // word wrapping
  6085. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6086. $endspace = 1;
  6087. } else {
  6088. $endspace = 0;
  6089. }
  6090. // check the length of the next string
  6091. $strrest = TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace));
  6092. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $this->stringTrim($strrest));
  6093. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6094. // truncate the word because do not fit on a full page width
  6095. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6096. if ($firstline) {
  6097. $startx = $this->x;
  6098. $tmparr = array_slice($chars, $j, ($i - $j));
  6099. if ($rtlmode) {
  6100. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6101. }
  6102. $linew = $this->GetArrStringWidth($tmparr);
  6103. unset($tmparr);
  6104. if ($this->rtl) {
  6105. $this->endlinex = ($startx - $linew);
  6106. } else {
  6107. $this->endlinex = ($startx + $linew);
  6108. }
  6109. $w = $linew;
  6110. $tmpcellpadding = $this->cell_padding;
  6111. if ($maxh == 0) {
  6112. $this->SetCellPadding(0);
  6113. }
  6114. }
  6115. if ($firstblock AND $this->isRTLTextDir()) {
  6116. $tmpstr = $this->stringRightTrim($tmpstr);
  6117. }
  6118. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6119. unset($tmpstr);
  6120. if ($firstline) {
  6121. $this->cell_padding = $tmpcellpadding;
  6122. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6123. }
  6124. $j = $i;
  6125. --$i;
  6126. } else {
  6127. // word wrapping
  6128. if ($shy) {
  6129. // add hypen (minus symbol) at the end of the line
  6130. $shy_width = $tmp_shy_replacement_width;
  6131. if ($this->rtl) {
  6132. $shy_char_left = $tmp_shy_replacement_char;
  6133. $shy_char_right = '';
  6134. } else {
  6135. $shy_char_left = '';
  6136. $shy_char_right = $tmp_shy_replacement_char;
  6137. }
  6138. } else {
  6139. $shy_width = 0;
  6140. $shy_char_left = '';
  6141. $shy_char_right = '';
  6142. }
  6143. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, ($sep + $endspace));
  6144. if ($firstline) {
  6145. $startx = $this->x;
  6146. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6147. if ($rtlmode) {
  6148. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6149. }
  6150. $linew = $this->GetArrStringWidth($tmparr);
  6151. unset($tmparr);
  6152. if ($this->rtl) {
  6153. $this->endlinex = $startx - $linew - $shy_width;
  6154. } else {
  6155. $this->endlinex = $startx + $linew + $shy_width;
  6156. }
  6157. $w = $linew;
  6158. $tmpcellpadding = $this->cell_padding;
  6159. if ($maxh == 0) {
  6160. $this->SetCellPadding(0);
  6161. }
  6162. }
  6163. // print the line
  6164. if ($firstblock AND $this->isRTLTextDir()) {
  6165. $tmpstr = $this->stringRightTrim($tmpstr);
  6166. }
  6167. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6168. unset($tmpstr);
  6169. if ($firstline) {
  6170. if ($chars[$sep] == 45) {
  6171. $endspace += 1;
  6172. }
  6173. // return the remaining text
  6174. $this->cell_padding = $tmpcellpadding;
  6175. return (TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace)));
  6176. }
  6177. $i = $sep;
  6178. $sep = -1;
  6179. $shy = false;
  6180. $j = ($i + 1);
  6181. }
  6182. }
  6183. // account for margin changes
  6184. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6185. $this->AcceptPageBreak();
  6186. if ($this->rtl) {
  6187. $this->x -= $margin['R'];
  6188. } else {
  6189. $this->x += $margin['L'];
  6190. }
  6191. $this->lMargin += $margin['L'];
  6192. $this->rMargin += $margin['R'];
  6193. }
  6194. $w = $this->getRemainingWidth();
  6195. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6196. if ($linebreak) {
  6197. $linebreak = false;
  6198. } else {
  6199. ++$nl;
  6200. $l = 0;
  6201. }
  6202. }
  6203. }
  6204. // save last character
  6205. $pc = $c;
  6206. ++$i;
  6207. } // end while i < nb
  6208. // print last substring (if any)
  6209. if ($l > 0) {
  6210. switch ($align) {
  6211. case 'J':
  6212. case 'C': {
  6213. $w = $w;
  6214. break;
  6215. }
  6216. case 'L': {
  6217. if ($this->rtl) {
  6218. $w = $w;
  6219. } else {
  6220. $w = $l;
  6221. }
  6222. break;
  6223. }
  6224. case 'R': {
  6225. if ($this->rtl) {
  6226. $w = $l;
  6227. } else {
  6228. $w = $w;
  6229. }
  6230. break;
  6231. }
  6232. default: {
  6233. $w = $l;
  6234. break;
  6235. }
  6236. }
  6237. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $nb);
  6238. if ($firstline) {
  6239. $startx = $this->x;
  6240. $tmparr = array_slice($chars, $j, ($nb - $j));
  6241. if ($rtlmode) {
  6242. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6243. }
  6244. $linew = $this->GetArrStringWidth($tmparr);
  6245. unset($tmparr);
  6246. if ($this->rtl) {
  6247. $this->endlinex = $startx - $linew;
  6248. } else {
  6249. $this->endlinex = $startx + $linew;
  6250. }
  6251. $w = $linew;
  6252. $tmpcellpadding = $this->cell_padding;
  6253. if ($maxh == 0) {
  6254. $this->SetCellPadding(0);
  6255. }
  6256. }
  6257. if ($firstblock AND $this->isRTLTextDir()) {
  6258. $tmpstr = $this->stringRightTrim($tmpstr);
  6259. }
  6260. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6261. unset($tmpstr);
  6262. if ($firstline) {
  6263. $this->cell_padding = $tmpcellpadding;
  6264. return (TCPDF_FONTS::UniArrSubString($uchars, $nb));
  6265. }
  6266. ++$nl;
  6267. }
  6268. if ($firstline) {
  6269. return '';
  6270. }
  6271. return $nl;
  6272. }
  6273. /**
  6274. * Returns the remaining width between the current position and margins.
  6275. * @return int Return the remaining width
  6276. * @protected
  6277. */
  6278. protected function getRemainingWidth() {
  6279. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6280. if ($this->rtl) {
  6281. return ($this->x - $this->lMargin);
  6282. } else {
  6283. return ($this->w - $this->rMargin - $this->x);
  6284. }
  6285. }
  6286. /**
  6287. * Set the block dimensions accounting for page breaks and page/column fitting
  6288. * @param $w (float) width
  6289. * @param $h (float) height
  6290. * @param $x (float) X coordinate
  6291. * @param $y (float) Y coodiante
  6292. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  6293. * @return array($w, $h, $x, $y)
  6294. * @protected
  6295. * @since 5.5.009 (2010-07-05)
  6296. */
  6297. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6298. if ($w <= 0) {
  6299. // set maximum width
  6300. $w = ($this->w - $this->lMargin - $this->rMargin);
  6301. if ($w <= 0) {
  6302. $w = 1;
  6303. }
  6304. }
  6305. if ($h <= 0) {
  6306. // set maximum height
  6307. $h = ($this->PageBreakTrigger - $this->tMargin);
  6308. if ($h <= 0) {
  6309. $h = 1;
  6310. }
  6311. }
  6312. // resize the block to be vertically contained on a single page or single column
  6313. if ($fitonpage OR $this->AutoPageBreak) {
  6314. $ratio_wh = ($w / $h);
  6315. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  6316. $h = $this->PageBreakTrigger - $this->tMargin;
  6317. $w = ($h * $ratio_wh);
  6318. }
  6319. // resize the block to be horizontally contained on a single page or single column
  6320. if ($fitonpage) {
  6321. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  6322. if ($w > $maxw) {
  6323. $w = $maxw;
  6324. $h = ($w / $ratio_wh);
  6325. }
  6326. }
  6327. }
  6328. // Check whether we need a new page or new column first as this does not fit
  6329. $prev_x = $this->x;
  6330. $prev_y = $this->y;
  6331. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  6332. $y = $this->y;
  6333. if ($this->rtl) {
  6334. $x += ($prev_x - $this->x);
  6335. } else {
  6336. $x += ($this->x - $prev_x);
  6337. }
  6338. $this->newline = true;
  6339. }
  6340. // resize the block to be contained on the remaining available page or column space
  6341. if ($fitonpage) {
  6342. $ratio_wh = ($w / $h);
  6343. if (($y + $h) > $this->PageBreakTrigger) {
  6344. $h = $this->PageBreakTrigger - $y;
  6345. $w = ($h * $ratio_wh);
  6346. }
  6347. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  6348. $w = $this->w - $this->rMargin - $x;
  6349. $h = ($w / $ratio_wh);
  6350. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  6351. $w = $x - $this->lMargin;
  6352. $h = ($w / $ratio_wh);
  6353. }
  6354. }
  6355. return array($w, $h, $x, $y);
  6356. }
  6357. /**
  6358. * Puts an image in the page.
  6359. * The upper-left corner must be given.
  6360. * The dimensions can be specified in different ways:<ul>
  6361. * <li>explicit width and height (expressed in user unit)</li>
  6362. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  6363. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  6364. * 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;
  6365. * The format can be specified explicitly or inferred from the file extension.<br />
  6366. * It is possible to put a link on the image.<br />
  6367. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  6368. * @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
  6369. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  6370. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  6371. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6372. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6373. * @param $type (string) 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.
  6374. * @param $link (mixed) URL or identifier returned by AddLink().
  6375. * @param $align (string) 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>
  6376. * @param $resize (mixed) 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).
  6377. * @param $dpi (int) dot-per-inch resolution used on resize
  6378. * @param $palign (string) 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>
  6379. * @param $ismask (boolean) true if this image is a mask, false otherwise
  6380. * @param $imgmask (mixed) image object returned by this function or false
  6381. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6382. * @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
  6383. * @param $hidden (boolean) If true do not display the image.
  6384. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  6385. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  6386. * @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
  6387. * @return image information
  6388. * @public
  6389. * @since 1.1
  6390. */
  6391. 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, $alt=false, $altimgs=array()) {
  6392. if ($this->state != 2) {
  6393. return;
  6394. }
  6395. if ($x === '') {
  6396. $x = $this->x;
  6397. }
  6398. if ($y === '') {
  6399. $y = $this->y;
  6400. }
  6401. // check page for no-write regions and adapt page margins if necessary
  6402. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6403. $exurl = ''; // external streams
  6404. $imsize = FALSE;
  6405. // check if we are passing an image as file or string
  6406. if ($file[0] === '@') {
  6407. // image from string
  6408. $imgdata = substr($file, 1);
  6409. } else { // image file
  6410. if ($file{0} === '*') {
  6411. // image as external stream
  6412. $file = substr($file, 1);
  6413. $exurl = $file;
  6414. }
  6415. // check if is local file
  6416. if (!@file_exists($file)) {
  6417. // encode spaces on filename (file is probably an URL)
  6418. $file = str_replace(' ', '%20', $file);
  6419. }
  6420. if (@file_exists($file)) {
  6421. // get image dimensions
  6422. $imsize = @getimagesize($file);
  6423. }
  6424. if ($imsize === FALSE) {
  6425. $imgdata = TCPDF_STATIC::fileGetContents($file);
  6426. }
  6427. }
  6428. if (isset($imgdata) AND ($imgdata !== FALSE) AND (strpos($file, '__tcpdf_img') === FALSE)) {
  6429. // copy image to cache
  6430. $original_file = $file;
  6431. $file = TCPDF_STATIC::getObjFilename('img');
  6432. $fp = fopen($file, 'w');
  6433. fwrite($fp, $imgdata);
  6434. fclose($fp);
  6435. unset($imgdata);
  6436. $imsize = @getimagesize($file);
  6437. if ($imsize === FALSE) {
  6438. unlink($file);
  6439. $file = $original_file;
  6440. } else {
  6441. $this->cached_files[] = $file;
  6442. }
  6443. }
  6444. if ($imsize === FALSE) {
  6445. if (($w > 0) AND ($h > 0)) {
  6446. // get measures from specified data
  6447. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6448. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6449. $imsize = array($pw, $ph);
  6450. } else {
  6451. $this->Error('[Image] Unable to get the size of the image: '.$file);
  6452. }
  6453. }
  6454. // file hash
  6455. $filehash = md5($this->file_id.$file);
  6456. // get original image width and height in pixels
  6457. list($pixw, $pixh) = $imsize;
  6458. // calculate image width and height on document
  6459. if (($w <= 0) AND ($h <= 0)) {
  6460. // convert image size to document unit
  6461. $w = $this->pixelsToUnits($pixw);
  6462. $h = $this->pixelsToUnits($pixh);
  6463. } elseif ($w <= 0) {
  6464. $w = $h * $pixw / $pixh;
  6465. } elseif ($h <= 0) {
  6466. $h = $w * $pixh / $pixw;
  6467. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  6468. if (strlen($fitbox) !== 2) {
  6469. // set default alignment
  6470. $fitbox = '--';
  6471. }
  6472. // scale image dimensions proportionally to fit within the ($w, $h) box
  6473. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  6474. // store current height
  6475. $oldh = $h;
  6476. // calculate new height
  6477. $h = $w * $pixh / $pixw;
  6478. // height difference
  6479. $hdiff = ($oldh - $h);
  6480. // vertical alignment
  6481. switch (strtoupper($fitbox{1})) {
  6482. case 'T': {
  6483. break;
  6484. }
  6485. case 'M': {
  6486. $y += ($hdiff / 2);
  6487. break;
  6488. }
  6489. case 'B': {
  6490. $y += $hdiff;
  6491. break;
  6492. }
  6493. }
  6494. } else {
  6495. // store current width
  6496. $oldw = $w;
  6497. // calculate new width
  6498. $w = $h * $pixw / $pixh;
  6499. // width difference
  6500. $wdiff = ($oldw - $w);
  6501. // horizontal alignment
  6502. switch (strtoupper($fitbox{0})) {
  6503. case 'L': {
  6504. if ($this->rtl) {
  6505. $x -= $wdiff;
  6506. }
  6507. break;
  6508. }
  6509. case 'C': {
  6510. if ($this->rtl) {
  6511. $x -= ($wdiff / 2);
  6512. } else {
  6513. $x += ($wdiff / 2);
  6514. }
  6515. break;
  6516. }
  6517. case 'R': {
  6518. if (!$this->rtl) {
  6519. $x += $wdiff;
  6520. }
  6521. break;
  6522. }
  6523. }
  6524. }
  6525. }
  6526. // fit the image on available space
  6527. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  6528. // calculate new minimum dimensions in pixels
  6529. $neww = round($w * $this->k * $dpi / $this->dpi);
  6530. $newh = round($h * $this->k * $dpi / $this->dpi);
  6531. // check if resize is necessary (resize is used only to reduce the image)
  6532. $newsize = ($neww * $newh);
  6533. $pixsize = ($pixw * $pixh);
  6534. if (intval($resize) == 2) {
  6535. $resize = true;
  6536. } elseif ($newsize >= $pixsize) {
  6537. $resize = false;
  6538. }
  6539. // check if image has been already added on document
  6540. $newimage = true;
  6541. if (in_array($file, $this->imagekeys)) {
  6542. $newimage = false;
  6543. // get existing image data
  6544. $info = $this->getImageBuffer($file);
  6545. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  6546. // check if the newer image is larger
  6547. $oldsize = ($info['w'] * $info['h']);
  6548. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6549. $newimage = true;
  6550. }
  6551. }
  6552. } elseif (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_imgmask_') === FALSE)) {
  6553. // create temp image file (without alpha channel)
  6554. $tempfile_plain = K_PATH_CACHE.'__tcpdf_imgmask_plain_'.$filehash;
  6555. // create temp alpha file
  6556. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_imgmask_alpha_'.$filehash;
  6557. // check for cached images
  6558. if (in_array($tempfile_plain, $this->imagekeys)) {
  6559. // get existing image data
  6560. $info = $this->getImageBuffer($tempfile_plain);
  6561. // check if the newer image is larger
  6562. $oldsize = ($info['w'] * $info['h']);
  6563. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6564. $newimage = true;
  6565. } else {
  6566. $newimage = false;
  6567. // embed mask image
  6568. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6569. // embed image, masked with previously embedded mask
  6570. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6571. }
  6572. }
  6573. }
  6574. if ($newimage) {
  6575. //First use of image, get info
  6576. $type = strtolower($type);
  6577. if ($type == '') {
  6578. $type = TCPDF_IMAGES::getImageFileType($file, $imsize);
  6579. } elseif ($type == 'jpg') {
  6580. $type = 'jpeg';
  6581. }
  6582. $mqr = TCPDF_STATIC::get_mqr();
  6583. TCPDF_STATIC::set_mqr(false);
  6584. // Specific image handlers (defined on TCPDF_IMAGES CLASS)
  6585. $mtd = '_parse'.$type;
  6586. // GD image handler function
  6587. $gdfunction = 'imagecreatefrom'.$type;
  6588. $info = false;
  6589. if ((method_exists('TCPDF_IMAGES', $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  6590. // TCPDF image functions
  6591. $info = TCPDF_IMAGES::$mtd($file);
  6592. if (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_imgmask_') === FALSE)
  6593. AND (($info === 'pngalpha') OR (isset($info['trns']) AND !empty($info['trns'])))) {
  6594. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  6595. }
  6596. }
  6597. if (($info === false) AND function_exists($gdfunction)) {
  6598. try {
  6599. // GD library
  6600. $img = @$gdfunction($file);
  6601. if ($img !== false) {
  6602. if ($resize) {
  6603. $imgr = imagecreatetruecolor($neww, $newh);
  6604. if (($type == 'gif') OR ($type == 'png')) {
  6605. $imgr = TCPDF_IMAGES::setGDImageTransparency($imgr, $img);
  6606. }
  6607. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  6608. if (($type == 'gif') OR ($type == 'png')) {
  6609. $info = TCPDF_IMAGES::_toPNG($imgr);
  6610. } else {
  6611. $info = TCPDF_IMAGES::_toJPEG($imgr, $this->jpeg_quality);
  6612. }
  6613. } else {
  6614. if (($type == 'gif') OR ($type == 'png')) {
  6615. $info = TCPDF_IMAGES::_toPNG($img);
  6616. } else {
  6617. $info = TCPDF_IMAGES::_toJPEG($img, $this->jpeg_quality);
  6618. }
  6619. }
  6620. }
  6621. } catch(Exception $e) {
  6622. $info = false;
  6623. }
  6624. }
  6625. if (($info === false) AND extension_loaded('imagick')) {
  6626. try {
  6627. // ImageMagick library
  6628. $img = new Imagick();
  6629. if ($type == 'SVG') {
  6630. // get SVG file content
  6631. $svgimg = TCPDF_STATIC::fileGetContents($file);
  6632. if ($svgimg !== FALSE) {
  6633. // get width and height
  6634. $regs = array();
  6635. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  6636. $svgtag = $regs[1];
  6637. $tmp = array();
  6638. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6639. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6640. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  6641. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  6642. } else {
  6643. $ow = $w;
  6644. }
  6645. $tmp = array();
  6646. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6647. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6648. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  6649. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  6650. } else {
  6651. $oh = $h;
  6652. }
  6653. $tmp = array();
  6654. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  6655. $vbw = ($ow * $this->imgscale * $this->k);
  6656. $vbh = ($oh * $this->imgscale * $this->k);
  6657. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  6658. $svgtag = $vbox.$svgtag;
  6659. }
  6660. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  6661. }
  6662. $img->readImageBlob($svgimg);
  6663. }
  6664. } else {
  6665. $img->readImage($file);
  6666. }
  6667. if ($resize) {
  6668. $img->resizeImage($neww, $newh, 10, 1, false);
  6669. }
  6670. $img->setCompressionQuality($this->jpeg_quality);
  6671. $img->setImageFormat('jpeg');
  6672. $tempname = TCPDF_STATIC::getObjFilename('img');
  6673. $img->writeImage($tempname);
  6674. $info = TCPDF_IMAGES::_parsejpeg($tempname);
  6675. unlink($tempname);
  6676. $img->destroy();
  6677. } catch(Exception $e) {
  6678. $info = false;
  6679. }
  6680. }
  6681. if ($info === false) {
  6682. // unable to process image
  6683. return;
  6684. }
  6685. TCPDF_STATIC::set_mqr($mqr);
  6686. if(is_array($info)) {
  6687. if ($ismask) {
  6688. // force grayscale
  6689. $info['cs'] = 'DeviceGray';
  6690. }
  6691. if ($imgmask !== false) {
  6692. $info['masked'] = $imgmask;
  6693. }
  6694. if (!empty($exurl)) {
  6695. $info['exurl'] = $exurl;
  6696. }
  6697. // array of alternative images
  6698. $info['altimgs'] = $altimgs;
  6699. // add image to document
  6700. $info['i'] = $this->setImageBuffer($file, $info);
  6701. }
  6702. }
  6703. // set alignment
  6704. $this->img_rb_y = $y + $h;
  6705. // set alignment
  6706. if ($this->rtl) {
  6707. if ($palign == 'L') {
  6708. $ximg = $this->lMargin;
  6709. } elseif ($palign == 'C') {
  6710. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6711. } elseif ($palign == 'R') {
  6712. $ximg = $this->w - $this->rMargin - $w;
  6713. } else {
  6714. $ximg = $x - $w;
  6715. }
  6716. $this->img_rb_x = $ximg;
  6717. } else {
  6718. if ($palign == 'L') {
  6719. $ximg = $this->lMargin;
  6720. } elseif ($palign == 'C') {
  6721. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6722. } elseif ($palign == 'R') {
  6723. $ximg = $this->w - $this->rMargin - $w;
  6724. } else {
  6725. $ximg = $x;
  6726. }
  6727. $this->img_rb_x = $ximg + $w;
  6728. }
  6729. if ($ismask OR $hidden) {
  6730. // image is not displayed
  6731. return $info['i'];
  6732. }
  6733. $xkimg = $ximg * $this->k;
  6734. if (!$alt) {
  6735. // only non-alternative immages will be set
  6736. $this->_out(sprintf('q %F 0 0 %F %F %F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), @$info['i']));
  6737. }
  6738. if (!empty($border)) {
  6739. $bx = $this->x;
  6740. $by = $this->y;
  6741. $this->x = $ximg;
  6742. if ($this->rtl) {
  6743. $this->x += $w;
  6744. }
  6745. $this->y = $y;
  6746. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  6747. $this->x = $bx;
  6748. $this->y = $by;
  6749. }
  6750. if ($link) {
  6751. $this->Link($ximg, $y, $w, $h, $link, 0);
  6752. }
  6753. // set pointer to align the next text/objects
  6754. switch($align) {
  6755. case 'T': {
  6756. $this->y = $y;
  6757. $this->x = $this->img_rb_x;
  6758. break;
  6759. }
  6760. case 'M': {
  6761. $this->y = $y + round($h/2);
  6762. $this->x = $this->img_rb_x;
  6763. break;
  6764. }
  6765. case 'B': {
  6766. $this->y = $this->img_rb_y;
  6767. $this->x = $this->img_rb_x;
  6768. break;
  6769. }
  6770. case 'N': {
  6771. $this->SetY($this->img_rb_y);
  6772. break;
  6773. }
  6774. default:{
  6775. break;
  6776. }
  6777. }
  6778. $this->endlinex = $this->img_rb_x;
  6779. if ($this->inxobj) {
  6780. // we are inside an XObject template
  6781. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  6782. }
  6783. return $info['i'];
  6784. }
  6785. /**
  6786. * Extract info from a PNG image with alpha channel using the Imagick or GD library.
  6787. * @param $file (string) Name of the file containing the image.
  6788. * @param $x (float) Abscissa of the upper-left corner.
  6789. * @param $y (float) Ordinate of the upper-left corner.
  6790. * @param $wpx (float) Original width of the image in pixels.
  6791. * @param $hpx (float) original height of the image in pixels.
  6792. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6793. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6794. * @param $type (string) 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.
  6795. * @param $link (mixed) URL or identifier returned by AddLink().
  6796. * @param $align (string) 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>
  6797. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  6798. * @param $dpi (int) dot-per-inch resolution used on resize
  6799. * @param $palign (string) 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>
  6800. * @param $filehash (string) File hash used to build unique file names.
  6801. * @author Nicola Asuni
  6802. * @protected
  6803. * @since 4.3.007 (2008-12-04)
  6804. * @see Image()
  6805. */
  6806. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  6807. // create temp images
  6808. if (empty($filehash)) {
  6809. $filehash = md5($this->file_id.$file);
  6810. }
  6811. // create temp image file (without alpha channel)
  6812. $tempfile_plain = K_PATH_CACHE.'__tcpdf_imgmask_plain_'.$filehash;
  6813. // create temp alpha file
  6814. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_imgmask_alpha_'.$filehash;
  6815. $parsed = false;
  6816. $parse_error = '';
  6817. // ImageMagick extension
  6818. if (($parsed === false) AND extension_loaded('imagick')) {
  6819. try {
  6820. // ImageMagick library
  6821. $img = new Imagick();
  6822. $img->readImage($file);
  6823. // clone image object
  6824. $imga = TCPDF_STATIC::objclone($img);
  6825. // extract alpha channel
  6826. if (method_exists($img, 'setImageAlphaChannel') AND defined('Imagick::ALPHACHANNEL_EXTRACT')) {
  6827. $img->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
  6828. } else {
  6829. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  6830. $img->negateImage(true);
  6831. }
  6832. $img->setImageFormat('png');
  6833. $img->writeImage($tempfile_alpha);
  6834. // remove alpha channel
  6835. if (method_exists($imga, 'setImageMatte')) {
  6836. $imga->setImageMatte(false);
  6837. } else {
  6838. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  6839. }
  6840. $imga->setImageFormat('png');
  6841. $imga->writeImage($tempfile_plain);
  6842. $parsed = true;
  6843. } catch (Exception $e) {
  6844. // Imagemagick fails, try with GD
  6845. $parse_error = 'Imagick library error: '.$e->getMessage();
  6846. }
  6847. }
  6848. // GD extension
  6849. if (($parsed === false) AND function_exists('imagecreatefrompng')) {
  6850. try {
  6851. // generate images
  6852. $img = imagecreatefrompng($file);
  6853. $imgalpha = imagecreate($wpx, $hpx);
  6854. // generate gray scale palette (0 -> 255)
  6855. for ($c = 0; $c < 256; ++$c) {
  6856. ImageColorAllocate($imgalpha, $c, $c, $c);
  6857. }
  6858. // extract alpha channel
  6859. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  6860. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  6861. $color = imagecolorat($img, $xpx, $ypx);
  6862. // get and correct gamma color
  6863. $alpha = $this->getGDgamma($img, $color);
  6864. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  6865. }
  6866. }
  6867. imagepng($imgalpha, $tempfile_alpha);
  6868. imagedestroy($imgalpha);
  6869. // extract image without alpha channel
  6870. $imgplain = imagecreatetruecolor($wpx, $hpx);
  6871. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  6872. imagepng($imgplain, $tempfile_plain);
  6873. imagedestroy($imgplain);
  6874. $parsed = true;
  6875. } catch (Exception $e) {
  6876. // GD fails
  6877. $parse_error = 'GD library error: '.$e->getMessage();
  6878. }
  6879. }
  6880. if ($parsed === false) {
  6881. if (empty($parse_error)) {
  6882. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  6883. } else {
  6884. $this->Error($parse_error);
  6885. }
  6886. }
  6887. // embed mask image
  6888. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6889. // embed image, masked with previously embedded mask
  6890. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6891. // remove temp files
  6892. unlink($tempfile_alpha);
  6893. unlink($tempfile_plain);
  6894. }
  6895. /**
  6896. * Get the GD-corrected PNG gamma value from alpha color
  6897. * @param $img (int) GD image Resource ID.
  6898. * @param $c (int) alpha color
  6899. * @protected
  6900. * @since 4.3.007 (2008-12-04)
  6901. */
  6902. protected function getGDgamma($img, $c) {
  6903. if (!isset($this->gdgammacache['#'.$c])) {
  6904. $colors = imagecolorsforindex($img, $c);
  6905. // GD alpha is only 7 bit (0 -> 127)
  6906. $this->gdgammacache['#'.$c] = (((127 - $colors['alpha']) / 127) * 255);
  6907. // correct gamma
  6908. $this->gdgammacache['#'.$c] = (pow(($this->gdgammacache['#'.$c] / 255), 2.2) * 255);
  6909. // store the latest values on cache to improve performances
  6910. if (count($this->gdgammacache) > 8) {
  6911. // remove one element from the cache array
  6912. array_shift($this->gdgammacache);
  6913. }
  6914. }
  6915. return $this->gdgammacache['#'.$c];
  6916. }
  6917. /**
  6918. * Performs a line break.
  6919. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  6920. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  6921. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  6922. * @public
  6923. * @since 1.0
  6924. * @see Cell()
  6925. */
  6926. public function Ln($h='', $cell=false) {
  6927. if (($this->num_columns > 1) 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'])) {
  6928. // revove vertical space from the top of the column
  6929. return;
  6930. }
  6931. if ($cell) {
  6932. if ($this->rtl) {
  6933. $cellpadding = $this->cell_padding['R'];
  6934. } else {
  6935. $cellpadding = $this->cell_padding['L'];
  6936. }
  6937. } else {
  6938. $cellpadding = 0;
  6939. }
  6940. if ($this->rtl) {
  6941. $this->x = $this->w - $this->rMargin - $cellpadding;
  6942. } else {
  6943. $this->x = $this->lMargin + $cellpadding;
  6944. }
  6945. if (is_string($h)) {
  6946. $this->y += $this->lasth;
  6947. } else {
  6948. $this->y += $h;
  6949. }
  6950. $this->newline = true;
  6951. }
  6952. /**
  6953. * Returns the relative X value of current position.
  6954. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  6955. * @return float
  6956. * @public
  6957. * @since 1.2
  6958. * @see SetX(), GetY(), SetY()
  6959. */
  6960. public function GetX() {
  6961. //Get x position
  6962. if ($this->rtl) {
  6963. return ($this->w - $this->x);
  6964. } else {
  6965. return $this->x;
  6966. }
  6967. }
  6968. /**
  6969. * Returns the absolute X value of current position.
  6970. * @return float
  6971. * @public
  6972. * @since 1.2
  6973. * @see SetX(), GetY(), SetY()
  6974. */
  6975. public function GetAbsX() {
  6976. return $this->x;
  6977. }
  6978. /**
  6979. * Returns the ordinate of the current position.
  6980. * @return float
  6981. * @public
  6982. * @since 1.0
  6983. * @see SetY(), GetX(), SetX()
  6984. */
  6985. public function GetY() {
  6986. return $this->y;
  6987. }
  6988. /**
  6989. * Defines the abscissa of the current position.
  6990. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  6991. * @param $x (float) The value of the abscissa in user units.
  6992. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  6993. * @public
  6994. * @since 1.2
  6995. * @see GetX(), GetY(), SetY(), SetXY()
  6996. */
  6997. public function SetX($x, $rtloff=false) {
  6998. $x = floatval($x);
  6999. if (!$rtloff AND $this->rtl) {
  7000. if ($x >= 0) {
  7001. $this->x = $this->w - $x;
  7002. } else {
  7003. $this->x = abs($x);
  7004. }
  7005. } else {
  7006. if ($x >= 0) {
  7007. $this->x = $x;
  7008. } else {
  7009. $this->x = $this->w + $x;
  7010. }
  7011. }
  7012. if ($this->x < 0) {
  7013. $this->x = 0;
  7014. }
  7015. if ($this->x > $this->w) {
  7016. $this->x = $this->w;
  7017. }
  7018. }
  7019. /**
  7020. * Moves the current abscissa back to the left margin and sets the ordinate.
  7021. * If the passed value is negative, it is relative to the bottom of the page.
  7022. * @param $y (float) The value of the ordinate in user units.
  7023. * @param $resetx (bool) if true (default) reset the X position.
  7024. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7025. * @public
  7026. * @since 1.0
  7027. * @see GetX(), GetY(), SetY(), SetXY()
  7028. */
  7029. public function SetY($y, $resetx=true, $rtloff=false) {
  7030. $y = floatval($y);
  7031. if ($resetx) {
  7032. //reset x
  7033. if (!$rtloff AND $this->rtl) {
  7034. $this->x = $this->w - $this->rMargin;
  7035. } else {
  7036. $this->x = $this->lMargin;
  7037. }
  7038. }
  7039. if ($y >= 0) {
  7040. $this->y = $y;
  7041. } else {
  7042. $this->y = $this->h + $y;
  7043. }
  7044. if ($this->y < 0) {
  7045. $this->y = 0;
  7046. }
  7047. if ($this->y > $this->h) {
  7048. $this->y = $this->h;
  7049. }
  7050. }
  7051. /**
  7052. * Defines the abscissa and ordinate of the current position.
  7053. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  7054. * @param $x (float) The value of the abscissa.
  7055. * @param $y (float) The value of the ordinate.
  7056. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7057. * @public
  7058. * @since 1.2
  7059. * @see SetX(), SetY()
  7060. */
  7061. public function SetXY($x, $y, $rtloff=false) {
  7062. $this->SetY($y, false, $rtloff);
  7063. $this->SetX($x, $rtloff);
  7064. }
  7065. /**
  7066. * Set the absolute X coordinate of the current pointer.
  7067. * @param $x (float) The value of the abscissa in user units.
  7068. * @public
  7069. * @since 5.9.186 (2012-09-13)
  7070. * @see setAbsX(), setAbsY(), SetAbsXY()
  7071. */
  7072. public function SetAbsX($x) {
  7073. $this->x = floatval($x);
  7074. }
  7075. /**
  7076. * Set the absolute Y coordinate of the current pointer.
  7077. * @param $y (float) (float) The value of the ordinate in user units.
  7078. * @public
  7079. * @since 5.9.186 (2012-09-13)
  7080. * @see setAbsX(), setAbsY(), SetAbsXY()
  7081. */
  7082. public function SetAbsY($y) {
  7083. $this->y = floatval($y);
  7084. }
  7085. /**
  7086. * Set the absolute X and Y coordinates of the current pointer.
  7087. * @param $x (float) The value of the abscissa in user units.
  7088. * @param $y (float) (float) The value of the ordinate in user units.
  7089. * @public
  7090. * @since 5.9.186 (2012-09-13)
  7091. * @see setAbsX(), setAbsY(), SetAbsXY()
  7092. */
  7093. public function SetAbsXY($x, $y) {
  7094. $this->SetAbsX($x);
  7095. $this->SetAbsY($y);
  7096. }
  7097. /**
  7098. * Send the document to a given destination: string, local file or browser.
  7099. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  7100. * The method first calls Close() if necessary to terminate the document.
  7101. * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
  7102. * @param $dest (string) 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><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
  7103. * @public
  7104. * @since 1.0
  7105. * @see Close()
  7106. */
  7107. public function Output($name='doc.pdf', $dest='I') {
  7108. //Output PDF to some destination
  7109. //Finish document if necessary
  7110. if ($this->state < 3) {
  7111. $this->Close();
  7112. }
  7113. //Normalize parameters
  7114. if (is_bool($dest)) {
  7115. $dest = $dest ? 'D' : 'F';
  7116. }
  7117. $dest = strtoupper($dest);
  7118. if ($dest{0} != 'F') {
  7119. $name = preg_replace('/[\s]+/', '_', $name);
  7120. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  7121. }
  7122. if ($this->sign) {
  7123. // *** apply digital signature to the document ***
  7124. // get the document content
  7125. $pdfdoc = $this->getBuffer();
  7126. // remove last newline
  7127. $pdfdoc = substr($pdfdoc, 0, -1);
  7128. // Remove the original buffer
  7129. if (isset($this->diskcache) AND $this->diskcache) {
  7130. // remove buffer file from cache
  7131. unlink($this->buffer);
  7132. }
  7133. unset($this->buffer);
  7134. // remove filler space
  7135. $byterange_string_len = strlen(TCPDF_STATIC::$byterange_string);
  7136. // define the ByteRange
  7137. $byte_range = array();
  7138. $byte_range[0] = 0;
  7139. $byte_range[1] = strpos($pdfdoc, TCPDF_STATIC::$byterange_string) + $byterange_string_len + 10;
  7140. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  7141. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  7142. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  7143. // replace the ByteRange
  7144. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  7145. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  7146. $pdfdoc = str_replace(TCPDF_STATIC::$byterange_string, $byterange, $pdfdoc);
  7147. // write the document to a temporary folder
  7148. $tempdoc = TCPDF_STATIC::getObjFilename('doc');
  7149. $f = fopen($tempdoc, 'wb');
  7150. if (!$f) {
  7151. $this->Error('Unable to create temporary file: '.$tempdoc);
  7152. }
  7153. $pdfdoc_length = strlen($pdfdoc);
  7154. fwrite($f, $pdfdoc, $pdfdoc_length);
  7155. fclose($f);
  7156. // get digital signature via openssl library
  7157. $tempsign = TCPDF_STATIC::getObjFilename('sig');
  7158. if (empty($this->signature_data['extracerts'])) {
  7159. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  7160. } else {
  7161. 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']);
  7162. }
  7163. unlink($tempdoc);
  7164. // read signature
  7165. $signature = file_get_contents($tempsign);
  7166. unlink($tempsign);
  7167. // extract signature
  7168. $signature = substr($signature, $pdfdoc_length);
  7169. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  7170. $tmparr = explode("\n\n", $signature);
  7171. $signature = $tmparr[1];
  7172. unset($tmparr);
  7173. // decode signature
  7174. $signature = base64_decode(trim($signature));
  7175. // convert signature to hex
  7176. $signature = current(unpack('H*', $signature));
  7177. $signature = str_pad($signature, $this->signature_max_length, '0');
  7178. // disable disk caching
  7179. $this->diskcache = false;
  7180. // Add signature to the document
  7181. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  7182. $this->bufferlen = strlen($this->buffer);
  7183. }
  7184. switch($dest) {
  7185. case 'I': {
  7186. // Send PDF to the standard output
  7187. if (ob_get_contents()) {
  7188. $this->Error('Some data has already been output, can\'t send PDF file');
  7189. }
  7190. if (php_sapi_name() != 'cli') {
  7191. // send output to a browser
  7192. header('Content-Type: application/pdf');
  7193. if (headers_sent()) {
  7194. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7195. }
  7196. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7197. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7198. header('Pragma: public');
  7199. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7200. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7201. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7202. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7203. } else {
  7204. echo $this->getBuffer();
  7205. }
  7206. break;
  7207. }
  7208. case 'D': {
  7209. // download PDF as file
  7210. if (ob_get_contents()) {
  7211. $this->Error('Some data has already been output, can\'t send PDF file');
  7212. }
  7213. header('Content-Description: File Transfer');
  7214. if (headers_sent()) {
  7215. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7216. }
  7217. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7218. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7219. header('Pragma: public');
  7220. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7221. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7222. // force download dialog
  7223. if (strpos(php_sapi_name(), 'cgi') === false) {
  7224. header('Content-Type: application/force-download');
  7225. header('Content-Type: application/octet-stream', false);
  7226. header('Content-Type: application/download', false);
  7227. header('Content-Type: application/pdf', false);
  7228. } else {
  7229. header('Content-Type: application/pdf');
  7230. }
  7231. // use the Content-Disposition header to supply a recommended filename
  7232. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7233. header('Content-Transfer-Encoding: binary');
  7234. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7235. break;
  7236. }
  7237. case 'F':
  7238. case 'FI':
  7239. case 'FD': {
  7240. // save PDF to a local file
  7241. if ($this->diskcache) {
  7242. copy($this->buffer, $name);
  7243. } else {
  7244. $f = fopen($name, 'wb');
  7245. if (!$f) {
  7246. $this->Error('Unable to create output file: '.$name);
  7247. }
  7248. fwrite($f, $this->getBuffer(), $this->bufferlen);
  7249. fclose($f);
  7250. }
  7251. if ($dest == 'FI') {
  7252. // send headers to browser
  7253. header('Content-Type: application/pdf');
  7254. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7255. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7256. header('Pragma: public');
  7257. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7258. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7259. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7260. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7261. } elseif ($dest == 'FD') {
  7262. // send headers to browser
  7263. if (ob_get_contents()) {
  7264. $this->Error('Some data has already been output, can\'t send PDF file');
  7265. }
  7266. header('Content-Description: File Transfer');
  7267. if (headers_sent()) {
  7268. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7269. }
  7270. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7271. header('Pragma: public');
  7272. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7273. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7274. // force download dialog
  7275. if (strpos(php_sapi_name(), 'cgi') === false) {
  7276. header('Content-Type: application/force-download');
  7277. header('Content-Type: application/octet-stream', false);
  7278. header('Content-Type: application/download', false);
  7279. header('Content-Type: application/pdf', false);
  7280. } else {
  7281. header('Content-Type: application/pdf');
  7282. }
  7283. // use the Content-Disposition header to supply a recommended filename
  7284. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7285. header('Content-Transfer-Encoding: binary');
  7286. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7287. }
  7288. break;
  7289. }
  7290. case 'E': {
  7291. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  7292. $retval = 'Content-Type: application/pdf;'."\r\n";
  7293. $retval .= ' name="'.$name.'"'."\r\n";
  7294. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  7295. $retval .= 'Content-Disposition: attachment;'."\r\n";
  7296. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  7297. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  7298. return $retval;
  7299. }
  7300. case 'S': {
  7301. // returns PDF as a string
  7302. return $this->getBuffer();
  7303. }
  7304. default: {
  7305. $this->Error('Incorrect output destination: '.$dest);
  7306. }
  7307. }
  7308. return '';
  7309. }
  7310. /**
  7311. * Unset all class variables except the following critical variables.
  7312. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  7313. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  7314. * @public
  7315. * @since 4.5.016 (2009-02-24)
  7316. */
  7317. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  7318. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!TCPDF_STATIC::empty_string($this->buffer))) {
  7319. // remove buffer file from cache
  7320. unlink($this->buffer);
  7321. }
  7322. if ($destroyall AND isset($this->cached_files) AND !empty($this->cached_files)) {
  7323. // remove cached files
  7324. foreach ($this->cached_files as $cachefile) {
  7325. if (is_file($cachefile)) {
  7326. unlink($cachefile);
  7327. }
  7328. }
  7329. unset($this->cached_files);
  7330. }
  7331. foreach (array_keys(get_object_vars($this)) as $val) {
  7332. if ($destroyall OR (
  7333. ($val != 'internal_encoding')
  7334. AND ($val != 'state')
  7335. AND ($val != 'bufferlen')
  7336. AND ($val != 'buffer')
  7337. AND ($val != 'diskcache')
  7338. AND ($val != 'cached_files')
  7339. AND ($val != 'sign')
  7340. AND ($val != 'signature_data')
  7341. AND ($val != 'signature_max_length')
  7342. AND ($val != 'byterange_string')
  7343. )) {
  7344. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  7345. unset($this->$val);
  7346. }
  7347. }
  7348. }
  7349. }
  7350. /**
  7351. * Check for locale-related bug
  7352. * @protected
  7353. */
  7354. protected function _dochecks() {
  7355. //Check for locale-related bug
  7356. if (1.1 == 1) {
  7357. $this->Error('Don\'t alter the locale before including class file');
  7358. }
  7359. //Check for decimal separator
  7360. if (sprintf('%.1F', 1.0) != '1.0') {
  7361. setlocale(LC_NUMERIC, 'C');
  7362. }
  7363. }
  7364. /**
  7365. * Return an array containing variations for the basic page number alias.
  7366. * @param $a (string) Base alias.
  7367. * @return array of page number aliases
  7368. * @protected
  7369. */
  7370. protected function getInternalPageNumberAliases($a= '') {
  7371. $alias = array();
  7372. // build array of Unicode + ASCII variants (the order is important)
  7373. $alias = array('u' => array(), 'a' => array());
  7374. $u = '{'.$a.'}';
  7375. $alias['u'][] = TCPDF_STATIC::_escape($u);
  7376. if ($this->isunicode) {
  7377. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($u, $this->isunicode, $this->CurrentFont));
  7378. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($u, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7379. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($a, $this->isunicode, $this->CurrentFont));
  7380. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($a, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7381. }
  7382. $alias['a'][] = TCPDF_STATIC::_escape($a);
  7383. return $alias;
  7384. }
  7385. /**
  7386. * Return an array containing all internal page aliases.
  7387. * @return array of page number aliases
  7388. * @protected
  7389. */
  7390. protected function getAllInternalPageNumberAliases() {
  7391. $basic_alias = array(TCPDF_STATIC::$alias_tot_pages, TCPDF_STATIC::$alias_num_page, TCPDF_STATIC::$alias_group_tot_pages, TCPDF_STATIC::$alias_group_num_page, TCPDF_STATIC::$alias_right_shift);
  7392. $pnalias = array();
  7393. foreach($basic_alias as $k => $a) {
  7394. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  7395. }
  7396. return $pnalias;
  7397. }
  7398. /**
  7399. * Replace right shift page number aliases with spaces to correct right alignment.
  7400. * This works perfectly only when using monospaced fonts.
  7401. * @param $page (string) Page content.
  7402. * @param $aliases (array) Array of page aliases.
  7403. * @param $diff (int) initial difference to add.
  7404. * @return replaced page content.
  7405. * @protected
  7406. */
  7407. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  7408. foreach ($aliases as $type => $alias) {
  7409. foreach ($alias as $a) {
  7410. // find position of compensation factor
  7411. $startnum = (strpos($a, ':') + 1);
  7412. $a = substr($a, 0, $startnum);
  7413. if (($pos = strpos($page, $a)) !== false) {
  7414. // end of alias
  7415. $endnum = strpos($page, '}', $pos);
  7416. // string to be replaced
  7417. $aa = substr($page, $pos, ($endnum - $pos + 1));
  7418. // get compensation factor
  7419. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  7420. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  7421. $ratio = floatval($ratio);
  7422. if ($type == 'u') {
  7423. $chrdiff = floor(($diff + 12) * $ratio);
  7424. $shift = str_repeat(' ', $chrdiff);
  7425. $shift = TCPDF_FONTS::UTF8ToUTF16BE($shift, false, $this->isunicode, $this->CurrentFont);
  7426. } else {
  7427. $chrdiff = floor(($diff + 11) * $ratio);
  7428. $shift = str_repeat(' ', $chrdiff);
  7429. }
  7430. $page = str_replace($aa, $shift, $page);
  7431. }
  7432. }
  7433. }
  7434. return $page;
  7435. }
  7436. /**
  7437. * Set page boxes to be included on page descriptions.
  7438. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  7439. * @protected
  7440. */
  7441. protected function setPageBoxTypes($boxes) {
  7442. $this->page_boxes = array();
  7443. foreach ($boxes as $box) {
  7444. if (in_array($box, TCPDF_STATIC::$pageboxes)) {
  7445. $this->page_boxes[] = $box;
  7446. }
  7447. }
  7448. }
  7449. /**
  7450. * Output pages (and replace page number aliases).
  7451. * @protected
  7452. */
  7453. protected function _putpages() {
  7454. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  7455. // get internal aliases for page numbers
  7456. $pnalias = $this->getAllInternalPageNumberAliases();
  7457. $num_pages = $this->numpages;
  7458. $ptpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $num_pages - 1));
  7459. $ptpu = TCPDF_FONTS::UTF8ToUTF16BE($ptpa, false, $this->isunicode, $this->CurrentFont);
  7460. $ptp_num_chars = $this->GetNumChars($ptpa);
  7461. $pagegroupnum = 0;
  7462. $groupnum = 0;
  7463. $ptgu = 1;
  7464. $ptga = 1;
  7465. $ptg_num_chars = 1;
  7466. for ($n = 1; $n <= $num_pages; ++$n) {
  7467. // get current page
  7468. $temppage = $this->getPageBuffer($n);
  7469. $pagelen = strlen($temppage);
  7470. // set replacements for total pages number
  7471. $pnpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $n - 1));
  7472. $pnpu = TCPDF_FONTS::UTF8ToUTF16BE($pnpa, false, $this->isunicode, $this->CurrentFont);
  7473. $pnp_num_chars = $this->GetNumChars($pnpa);
  7474. $pdiff = 0; // difference used for right shift alignment of page numbers
  7475. $gdiff = 0; // difference used for right shift alignment of page group numbers
  7476. if (!empty($this->pagegroups)) {
  7477. if (isset($this->newpagegroup[$n])) {
  7478. $pagegroupnum = 0;
  7479. ++$groupnum;
  7480. $ptga = TCPDF_STATIC::formatPageNumber($this->pagegroups[$groupnum]);
  7481. $ptgu = TCPDF_FONTS::UTF8ToUTF16BE($ptga, false, $this->isunicode, $this->CurrentFont);
  7482. $ptg_num_chars = $this->GetNumChars($ptga);
  7483. }
  7484. ++$pagegroupnum;
  7485. $pnga = TCPDF_STATIC::formatPageNumber($pagegroupnum);
  7486. $pngu = TCPDF_FONTS::UTF8ToUTF16BE($pnga, false, $this->isunicode, $this->CurrentFont);
  7487. $png_num_chars = $this->GetNumChars($pnga);
  7488. // replace page numbers
  7489. $replace = array();
  7490. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  7491. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  7492. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  7493. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  7494. list($temppage, $gdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $gdiff);
  7495. }
  7496. // replace page numbers
  7497. $replace = array();
  7498. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  7499. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  7500. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  7501. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  7502. list($temppage, $pdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $pdiff);
  7503. // replace right shift alias
  7504. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  7505. // replace EPS marker
  7506. $temppage = str_replace($this->epsmarker, '', $temppage);
  7507. //Page
  7508. $this->page_obj_id[$n] = $this->_newobj();
  7509. $out = '<<';
  7510. $out .= ' /Type /Page';
  7511. $out .= ' /Parent 1 0 R';
  7512. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  7513. $out .= ' /Resources 2 0 R';
  7514. foreach ($this->page_boxes as $box) {
  7515. $out .= ' /'.$box;
  7516. $out .= sprintf(' [%F %F %F %F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  7517. }
  7518. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  7519. $out .= ' /BoxColorInfo <<';
  7520. foreach ($this->page_boxes as $box) {
  7521. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  7522. $out .= ' /'.$box.' <<';
  7523. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  7524. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  7525. $out .= ' /C [';
  7526. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  7527. $out .= ' ]';
  7528. }
  7529. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  7530. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  7531. }
  7532. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  7533. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  7534. }
  7535. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  7536. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  7537. $out .= ' /D [';
  7538. foreach ($dashes as $dash) {
  7539. $out .= sprintf(' %F', ($dash * $this->k));
  7540. }
  7541. $out .= ' ]';
  7542. }
  7543. $out .= ' >>';
  7544. }
  7545. }
  7546. $out .= ' >>';
  7547. }
  7548. $out .= ' /Contents '.($this->n + 1).' 0 R';
  7549. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  7550. if (!$this->pdfa_mode) {
  7551. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  7552. }
  7553. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  7554. // page transitions
  7555. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  7556. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  7557. }
  7558. $out .= ' /Trans <<';
  7559. $out .= ' /Type /Trans';
  7560. if (isset($this->pagedim[$n]['trans']['S'])) {
  7561. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  7562. }
  7563. if (isset($this->pagedim[$n]['trans']['D'])) {
  7564. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  7565. }
  7566. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  7567. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  7568. }
  7569. if (isset($this->pagedim[$n]['trans']['M'])) {
  7570. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  7571. }
  7572. if (isset($this->pagedim[$n]['trans']['Di'])) {
  7573. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  7574. }
  7575. if (isset($this->pagedim[$n]['trans']['SS'])) {
  7576. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  7577. }
  7578. if (isset($this->pagedim[$n]['trans']['B'])) {
  7579. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  7580. }
  7581. $out .= ' >>';
  7582. }
  7583. $out .= $this->_getannotsrefs($n);
  7584. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  7585. $out .= ' >>';
  7586. $out .= "\n".'endobj';
  7587. $this->_out($out);
  7588. //Page content
  7589. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  7590. $this->_newobj();
  7591. $p = $this->_getrawstream($p);
  7592. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  7593. if ($this->diskcache) {
  7594. // remove temporary files
  7595. unlink($this->pages[$n]);
  7596. }
  7597. }
  7598. //Pages root
  7599. $out = $this->_getobj(1)."\n";
  7600. $out .= '<< /Type /Pages /Kids [';
  7601. foreach($this->page_obj_id as $page_obj) {
  7602. $out .= ' '.$page_obj.' 0 R';
  7603. }
  7604. $out .= ' ] /Count '.$num_pages.' >>';
  7605. $out .= "\n".'endobj';
  7606. $this->_out($out);
  7607. }
  7608. /**
  7609. * Output references to page annotations
  7610. * @param $n (int) page number
  7611. * @protected
  7612. * @author Nicola Asuni
  7613. * @since 4.7.000 (2008-08-29)
  7614. * @deprecated
  7615. */
  7616. protected function _putannotsrefs($n) {
  7617. $this->_out($this->_getannotsrefs($n));
  7618. }
  7619. /**
  7620. * Get references to page annotations.
  7621. * @param $n (int) page number
  7622. * @return string
  7623. * @protected
  7624. * @author Nicola Asuni
  7625. * @since 5.0.010 (2010-05-17)
  7626. */
  7627. protected function _getannotsrefs($n) {
  7628. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  7629. return '';
  7630. }
  7631. $out = ' /Annots [';
  7632. if (isset($this->PageAnnots[$n])) {
  7633. foreach ($this->PageAnnots[$n] as $key => $val) {
  7634. if (!in_array($val['n'], $this->radio_groups)) {
  7635. $out .= ' '.$val['n'].' 0 R';
  7636. }
  7637. }
  7638. // add radiobutton groups
  7639. if (isset($this->radiobutton_groups[$n])) {
  7640. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  7641. if (isset($data['n'])) {
  7642. $out .= ' '.$data['n'].' 0 R';
  7643. }
  7644. }
  7645. }
  7646. }
  7647. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  7648. // set reference for signature object
  7649. $out .= ' '.$this->sig_obj_id.' 0 R';
  7650. }
  7651. if (!empty($this->empty_signature_appearance)) {
  7652. foreach ($this->empty_signature_appearance as $esa) {
  7653. if ($esa['page'] == $n) {
  7654. // set reference for empty signature objects
  7655. $out .= ' '.$esa['objid'].' 0 R';
  7656. }
  7657. }
  7658. }
  7659. $out .= ' ]';
  7660. return $out;
  7661. }
  7662. /**
  7663. * Output annotations objects for all pages.
  7664. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  7665. * See section 12.5 of PDF 32000_2008 reference.
  7666. * @protected
  7667. * @author Nicola Asuni
  7668. * @since 4.0.018 (2008-08-06)
  7669. */
  7670. protected function _putannotsobjs() {
  7671. // reset object counter
  7672. for ($n=1; $n <= $this->numpages; ++$n) {
  7673. if (isset($this->PageAnnots[$n])) {
  7674. // set page annotations
  7675. foreach ($this->PageAnnots[$n] as $key => $pl) {
  7676. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  7677. // create annotation object for grouping radiobuttons
  7678. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  7679. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  7680. $annots = '<<';
  7681. $annots .= ' /Type /Annot';
  7682. $annots .= ' /Subtype /Widget';
  7683. $annots .= ' /Rect [0 0 0 0]';
  7684. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  7685. // read only
  7686. $annots .= ' /F 68';
  7687. $annots .= ' /Ff 49153';
  7688. } else {
  7689. $annots .= ' /F 4'; // default print for PDF/A
  7690. $annots .= ' /Ff 49152';
  7691. }
  7692. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  7693. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  7694. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $radio_button_obj_id);
  7695. }
  7696. $annots .= ' /FT /Btn';
  7697. $annots .= ' /Kids [';
  7698. $defval = '';
  7699. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  7700. if (isset($data['kid'])) {
  7701. $annots .= ' '.$data['kid'].' 0 R';
  7702. if ($data['def'] !== 'Off') {
  7703. $defval = $data['def'];
  7704. }
  7705. }
  7706. }
  7707. $annots .= ' ]';
  7708. if (!empty($defval)) {
  7709. $annots .= ' /V /'.$defval;
  7710. }
  7711. $annots .= ' >>';
  7712. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  7713. $this->form_obj_id[] = $radio_button_obj_id;
  7714. // store object id to be used on Parent entry of Kids
  7715. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  7716. }
  7717. $formfield = false;
  7718. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  7719. $a = $pl['x'] * $this->k;
  7720. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  7721. $c = $pl['w'] * $this->k;
  7722. $d = $pl['h'] * $this->k;
  7723. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  7724. // create new annotation object
  7725. $annots = '<</Type /Annot';
  7726. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  7727. $annots .= ' /Rect ['.$rect.']';
  7728. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  7729. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  7730. $annots .= ' /FT /'.$pl['opt']['ft'];
  7731. $formfield = true;
  7732. }
  7733. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  7734. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  7735. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  7736. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  7737. if (isset($pl['opt']['f'])) {
  7738. $fval = 0;
  7739. if (is_array($pl['opt']['f'])) {
  7740. foreach ($pl['opt']['f'] as $f) {
  7741. switch (strtolower($f)) {
  7742. case 'invisible': {
  7743. $fval += 1 << 0;
  7744. break;
  7745. }
  7746. case 'hidden': {
  7747. $fval += 1 << 1;
  7748. break;
  7749. }
  7750. case 'print': {
  7751. $fval += 1 << 2;
  7752. break;
  7753. }
  7754. case 'nozoom': {
  7755. $fval += 1 << 3;
  7756. break;
  7757. }
  7758. case 'norotate': {
  7759. $fval += 1 << 4;
  7760. break;
  7761. }
  7762. case 'noview': {
  7763. $fval += 1 << 5;
  7764. break;
  7765. }
  7766. case 'readonly': {
  7767. $fval += 1 << 6;
  7768. break;
  7769. }
  7770. case 'locked': {
  7771. $fval += 1 << 8;
  7772. break;
  7773. }
  7774. case 'togglenoview': {
  7775. $fval += 1 << 9;
  7776. break;
  7777. }
  7778. case 'lockedcontents': {
  7779. $fval += 1 << 10;
  7780. break;
  7781. }
  7782. default: {
  7783. break;
  7784. }
  7785. }
  7786. }
  7787. } else {
  7788. $fval = intval($pl['opt']['f']);
  7789. }
  7790. } else {
  7791. $fval = 4;
  7792. }
  7793. if ($this->pdfa_mode) {
  7794. // force print flag for PDF/A mode
  7795. $fval |= 4;
  7796. }
  7797. $annots .= ' /F '.intval($fval);
  7798. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  7799. $annots .= ' /AS /'.$pl['opt']['as'];
  7800. }
  7801. if (isset($pl['opt']['ap'])) {
  7802. // appearance stream
  7803. $annots .= ' /AP <<';
  7804. if (is_array($pl['opt']['ap'])) {
  7805. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  7806. // $apmode can be: n = normal; r = rollover; d = down;
  7807. $annots .= ' /'.strtoupper($apmode);
  7808. if (is_array($apdef)) {
  7809. $annots .= ' <<';
  7810. foreach ($apdef as $apstate => $stream) {
  7811. // reference to XObject that define the appearance for this mode-state
  7812. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  7813. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  7814. }
  7815. $annots .= ' >>';
  7816. } else {
  7817. // reference to XObject that define the appearance for this mode
  7818. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  7819. $annots .= ' '.$apsobjid.' 0 R';
  7820. }
  7821. }
  7822. } else {
  7823. $annots .= $pl['opt']['ap'];
  7824. }
  7825. $annots .= ' >>';
  7826. }
  7827. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  7828. $annots .= ' /BS <<';
  7829. $annots .= ' /Type /Border';
  7830. if (isset($pl['opt']['bs']['w'])) {
  7831. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  7832. }
  7833. $bstyles = array('S', 'D', 'B', 'I', 'U');
  7834. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  7835. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7836. }
  7837. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  7838. $annots .= ' /D [';
  7839. foreach ($pl['opt']['bs']['d'] as $cord) {
  7840. $annots .= ' '.intval($cord);
  7841. }
  7842. $annots .= ']';
  7843. }
  7844. $annots .= ' >>';
  7845. } else {
  7846. $annots .= ' /Border [';
  7847. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  7848. $annots .= intval($pl['opt']['border'][0]).' ';
  7849. $annots .= intval($pl['opt']['border'][1]).' ';
  7850. $annots .= intval($pl['opt']['border'][2]);
  7851. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  7852. $annots .= ' [';
  7853. foreach ($pl['opt']['border'][3] as $dash) {
  7854. $annots .= intval($dash).' ';
  7855. }
  7856. $annots .= ']';
  7857. }
  7858. } else {
  7859. $annots .= '0 0 0';
  7860. }
  7861. $annots .= ']';
  7862. }
  7863. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  7864. $annots .= ' /BE <<';
  7865. $bstyles = array('S', 'C');
  7866. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  7867. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7868. } else {
  7869. $annots .= ' /S /S';
  7870. }
  7871. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  7872. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  7873. }
  7874. $annots .= '>>';
  7875. }
  7876. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  7877. $annots .= ' /C '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['c']);
  7878. }
  7879. //$annots .= ' /StructParent ';
  7880. //$annots .= ' /OC ';
  7881. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  7882. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  7883. // this is a markup type
  7884. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  7885. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  7886. }
  7887. //$annots .= ' /Popup ';
  7888. if (isset($pl['opt']['ca'])) {
  7889. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  7890. }
  7891. if (isset($pl['opt']['rc'])) {
  7892. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  7893. }
  7894. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  7895. //$annots .= ' /IRT ';
  7896. if (isset($pl['opt']['subj'])) {
  7897. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  7898. }
  7899. //$annots .= ' /RT ';
  7900. //$annots .= ' /IT ';
  7901. //$annots .= ' /ExData ';
  7902. }
  7903. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  7904. // Annotation types
  7905. switch (strtolower($pl['opt']['subtype'])) {
  7906. case 'text': {
  7907. if (isset($pl['opt']['open'])) {
  7908. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  7909. }
  7910. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  7911. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  7912. $annots .= ' /Name /'.$pl['opt']['name'];
  7913. } else {
  7914. $annots .= ' /Name /Note';
  7915. }
  7916. $statemodels = array('Marked', 'Review');
  7917. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  7918. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7919. } else {
  7920. $pl['opt']['statemodel'] = 'Marked';
  7921. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7922. }
  7923. if ($pl['opt']['statemodel'] == 'Marked') {
  7924. $states = array('Accepted', 'Unmarked');
  7925. } else {
  7926. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  7927. }
  7928. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  7929. $annots .= ' /State /'.$pl['opt']['state'];
  7930. } else {
  7931. if ($pl['opt']['statemodel'] == 'Marked') {
  7932. $annots .= ' /State /Unmarked';
  7933. } else {
  7934. $annots .= ' /State /None';
  7935. }
  7936. }
  7937. break;
  7938. }
  7939. case 'link': {
  7940. if (is_string($pl['txt'])) {
  7941. if ($pl['txt'][0] == '#') {
  7942. // internal destination
  7943. $annots .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($pl['txt'], 1));
  7944. } elseif ($pl['txt'][0] == '%') {
  7945. // embedded PDF file
  7946. $filename = basename(substr($pl['txt'], 1));
  7947. $annots .= ' /A << /S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($n - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  7948. } elseif ($pl['txt'][0] == '*') {
  7949. // embedded generic file
  7950. $filename = basename(substr($pl['txt'], 1));
  7951. $jsa = 'var D=event.target.doc;var MyData=D.dataObjects;for (var i in MyData) if (MyData[i].path=="'.$filename.'") D.exportDataObject( { cName : MyData[i].name, nLaunch : 2});';
  7952. $annots .= ' /A << /S /JavaScript /JS '.$this->_textstring($jsa, $annot_obj_id).'>>';
  7953. } else {
  7954. // external URI link
  7955. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  7956. }
  7957. } elseif (isset($this->links[$pl['txt']])) {
  7958. // internal link ID
  7959. $l = $this->links[$pl['txt']];
  7960. if (isset($this->page_obj_id[($l[0])])) {
  7961. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  7962. }
  7963. }
  7964. $hmodes = array('N', 'I', 'O', 'P');
  7965. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  7966. $annots .= ' /H /'.$pl['opt']['h'];
  7967. } else {
  7968. $annots .= ' /H /I';
  7969. }
  7970. //$annots .= ' /PA ';
  7971. //$annots .= ' /Quadpoints ';
  7972. break;
  7973. }
  7974. case 'freetext': {
  7975. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  7976. $annots .= ' /DA ('.$pl['opt']['da'].')';
  7977. }
  7978. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  7979. $annots .= ' /Q '.intval($pl['opt']['q']);
  7980. }
  7981. if (isset($pl['opt']['rc'])) {
  7982. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  7983. }
  7984. if (isset($pl['opt']['ds'])) {
  7985. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  7986. }
  7987. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  7988. $annots .= ' /CL [';
  7989. foreach ($pl['opt']['cl'] as $cl) {
  7990. $annots .= sprintf('%F ', $cl * $this->k);
  7991. }
  7992. $annots .= ']';
  7993. }
  7994. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  7995. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  7996. $annots .= ' /IT /'.$pl['opt']['it'];
  7997. }
  7998. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  7999. $l = $pl['opt']['rd'][0] * $this->k;
  8000. $r = $pl['opt']['rd'][1] * $this->k;
  8001. $t = $pl['opt']['rd'][2] * $this->k;
  8002. $b = $pl['opt']['rd'][3] * $this->k;
  8003. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  8004. }
  8005. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8006. $annots .= ' /LE /'.$pl['opt']['le'];
  8007. }
  8008. break;
  8009. }
  8010. case 'line': {
  8011. break;
  8012. }
  8013. case 'square': {
  8014. break;
  8015. }
  8016. case 'circle': {
  8017. break;
  8018. }
  8019. case 'polygon': {
  8020. break;
  8021. }
  8022. case 'polyline': {
  8023. break;
  8024. }
  8025. case 'highlight': {
  8026. break;
  8027. }
  8028. case 'underline': {
  8029. break;
  8030. }
  8031. case 'squiggly': {
  8032. break;
  8033. }
  8034. case 'strikeout': {
  8035. break;
  8036. }
  8037. case 'stamp': {
  8038. break;
  8039. }
  8040. case 'caret': {
  8041. break;
  8042. }
  8043. case 'ink': {
  8044. break;
  8045. }
  8046. case 'popup': {
  8047. break;
  8048. }
  8049. case 'fileattachment': {
  8050. if ($this->pdfa_mode) {
  8051. // embedded files are not allowed in PDF/A mode
  8052. break;
  8053. }
  8054. if (!isset($pl['opt']['fs'])) {
  8055. break;
  8056. }
  8057. $filename = basename($pl['opt']['fs']);
  8058. if (isset($this->embeddedfiles[$filename]['f'])) {
  8059. $annots .= ' /FS '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8060. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  8061. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8062. $annots .= ' /Name /'.$pl['opt']['name'];
  8063. } else {
  8064. $annots .= ' /Name /PushPin';
  8065. }
  8066. // index (zero-based) of the annotation in the Annots array of this page
  8067. $this->embeddedfiles[$filename]['a'] = $key;
  8068. }
  8069. break;
  8070. }
  8071. case 'sound': {
  8072. if (!isset($pl['opt']['fs'])) {
  8073. break;
  8074. }
  8075. $filename = basename($pl['opt']['fs']);
  8076. if (isset($this->embeddedfiles[$filename]['f'])) {
  8077. // ... TO BE COMPLETED ...
  8078. // /R /C /B /E /CO /CP
  8079. $annots .= ' /Sound '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8080. $iconsapp = array('Speaker', 'Mic');
  8081. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8082. $annots .= ' /Name /'.$pl['opt']['name'];
  8083. } else {
  8084. $annots .= ' /Name /Speaker';
  8085. }
  8086. }
  8087. break;
  8088. }
  8089. case 'movie': {
  8090. break;
  8091. }
  8092. case 'widget': {
  8093. $hmode = array('N', 'I', 'O', 'P', 'T');
  8094. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  8095. $annots .= ' /H /'.$pl['opt']['h'];
  8096. }
  8097. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  8098. $annots .= ' /MK <<';
  8099. if (isset($pl['opt']['mk']['r'])) {
  8100. $annots .= ' /R '.$pl['opt']['mk']['r'];
  8101. }
  8102. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  8103. $annots .= ' /BC '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bc']);
  8104. }
  8105. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  8106. $annots .= ' /BG '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bg']);
  8107. }
  8108. if (isset($pl['opt']['mk']['ca'])) {
  8109. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  8110. }
  8111. if (isset($pl['opt']['mk']['rc'])) {
  8112. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  8113. }
  8114. if (isset($pl['opt']['mk']['ac'])) {
  8115. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  8116. }
  8117. if (isset($pl['opt']['mk']['i'])) {
  8118. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  8119. if ($info !== false) {
  8120. $annots .= ' /I '.$info['n'].' 0 R';
  8121. }
  8122. }
  8123. if (isset($pl['opt']['mk']['ri'])) {
  8124. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  8125. if ($info !== false) {
  8126. $annots .= ' /RI '.$info['n'].' 0 R';
  8127. }
  8128. }
  8129. if (isset($pl['opt']['mk']['ix'])) {
  8130. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  8131. if ($info !== false) {
  8132. $annots .= ' /IX '.$info['n'].' 0 R';
  8133. }
  8134. }
  8135. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  8136. $annots .= ' /IF <<';
  8137. $if_sw = array('A', 'B', 'S', 'N');
  8138. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  8139. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  8140. }
  8141. $if_s = array('A', 'P');
  8142. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  8143. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  8144. }
  8145. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  8146. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  8147. }
  8148. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  8149. $annots .= ' /FB true';
  8150. }
  8151. $annots .= '>>';
  8152. }
  8153. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  8154. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  8155. }
  8156. $annots .= '>>';
  8157. } // end MK
  8158. // --- Entries for field dictionaries ---
  8159. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8160. // set parent
  8161. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  8162. }
  8163. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8164. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  8165. }
  8166. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  8167. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  8168. }
  8169. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  8170. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  8171. }
  8172. if (isset($pl['opt']['ff'])) {
  8173. if (is_array($pl['opt']['ff'])) {
  8174. // array of bit settings
  8175. $flag = 0;
  8176. foreach($pl['opt']['ff'] as $val) {
  8177. $flag += 1 << ($val - 1);
  8178. }
  8179. } else {
  8180. $flag = intval($pl['opt']['ff']);
  8181. }
  8182. $annots .= ' /Ff '.$flag;
  8183. }
  8184. if (isset($pl['opt']['maxlen'])) {
  8185. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  8186. }
  8187. if (isset($pl['opt']['v'])) {
  8188. $annots .= ' /V';
  8189. if (is_array($pl['opt']['v'])) {
  8190. foreach ($pl['opt']['v'] AS $optval) {
  8191. if (is_float($optval)) {
  8192. $optval = sprintf('%F', $optval);
  8193. }
  8194. $annots .= ' '.$optval;
  8195. }
  8196. } else {
  8197. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  8198. }
  8199. }
  8200. if (isset($pl['opt']['dv'])) {
  8201. $annots .= ' /DV';
  8202. if (is_array($pl['opt']['dv'])) {
  8203. foreach ($pl['opt']['dv'] AS $optval) {
  8204. if (is_float($optval)) {
  8205. $optval = sprintf('%F', $optval);
  8206. }
  8207. $annots .= ' '.$optval;
  8208. }
  8209. } else {
  8210. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  8211. }
  8212. }
  8213. if (isset($pl['opt']['rv'])) {
  8214. $annots .= ' /RV';
  8215. if (is_array($pl['opt']['rv'])) {
  8216. foreach ($pl['opt']['rv'] AS $optval) {
  8217. if (is_float($optval)) {
  8218. $optval = sprintf('%F', $optval);
  8219. }
  8220. $annots .= ' '.$optval;
  8221. }
  8222. } else {
  8223. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  8224. }
  8225. }
  8226. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  8227. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  8228. }
  8229. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  8230. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  8231. }
  8232. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8233. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8234. }
  8235. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8236. $annots .= ' /Q '.intval($pl['opt']['q']);
  8237. }
  8238. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  8239. $annots .= ' /Opt [';
  8240. foreach($pl['opt']['opt'] AS $copt) {
  8241. if (is_array($copt)) {
  8242. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  8243. } else {
  8244. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  8245. }
  8246. }
  8247. $annots .= ']';
  8248. }
  8249. if (isset($pl['opt']['ti'])) {
  8250. $annots .= ' /TI '.intval($pl['opt']['ti']);
  8251. }
  8252. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  8253. $annots .= ' /I [';
  8254. foreach($pl['opt']['i'] AS $copt) {
  8255. $annots .= intval($copt).' ';
  8256. }
  8257. $annots .= ']';
  8258. }
  8259. break;
  8260. }
  8261. case 'screen': {
  8262. break;
  8263. }
  8264. case 'printermark': {
  8265. break;
  8266. }
  8267. case 'trapnet': {
  8268. break;
  8269. }
  8270. case 'watermark': {
  8271. break;
  8272. }
  8273. case '3d': {
  8274. break;
  8275. }
  8276. default: {
  8277. break;
  8278. }
  8279. }
  8280. $annots .= '>>';
  8281. // create new annotation object
  8282. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  8283. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8284. // store reference of form object
  8285. $this->form_obj_id[] = $annot_obj_id;
  8286. }
  8287. }
  8288. }
  8289. } // end for each page
  8290. }
  8291. /**
  8292. * Put appearance streams XObject used to define annotation's appearance states.
  8293. * @param $w (int) annotation width
  8294. * @param $h (int) annotation height
  8295. * @param $stream (string) appearance stream
  8296. * @return int object ID
  8297. * @protected
  8298. * @since 4.8.001 (2009-09-09)
  8299. */
  8300. protected function _putAPXObject($w=0, $h=0, $stream='') {
  8301. $stream = trim($stream);
  8302. $out = $this->_getobj()."\n";
  8303. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  8304. $out .= '<<';
  8305. $out .= ' /Type /XObject';
  8306. $out .= ' /Subtype /Form';
  8307. $out .= ' /FormType 1';
  8308. if ($this->compress) {
  8309. $stream = gzcompress($stream);
  8310. $out .= ' /Filter /FlateDecode';
  8311. }
  8312. $rect = sprintf('%F %F', $w, $h);
  8313. $out .= ' /BBox [0 0 '.$rect.']';
  8314. $out .= ' /Matrix [1 0 0 1 0 0]';
  8315. $out .= ' /Resources 2 0 R';
  8316. $stream = $this->_getrawstream($stream);
  8317. $out .= ' /Length '.strlen($stream);
  8318. $out .= ' >>';
  8319. $out .= ' stream'."\n".$stream."\n".'endstream';
  8320. $out .= "\n".'endobj';
  8321. $this->_out($out);
  8322. return $this->n;
  8323. }
  8324. /**
  8325. * Output fonts.
  8326. * @author Nicola Asuni
  8327. * @protected
  8328. */
  8329. protected function _putfonts() {
  8330. $nf = $this->n;
  8331. foreach ($this->diffs as $diff) {
  8332. //Encodings
  8333. $this->_newobj();
  8334. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  8335. }
  8336. $mqr = TCPDF_STATIC::get_mqr();
  8337. TCPDF_STATIC::set_mqr(false);
  8338. foreach ($this->FontFiles as $file => $info) {
  8339. // search and get font file to embedd
  8340. $fontfile = TCPDF_FONTS::getFontFullPath($file, $info['fontdir']);
  8341. if (!TCPDF_STATIC::empty_string($fontfile)) {
  8342. $font = file_get_contents($fontfile);
  8343. $compressed = (substr($file, -2) == '.z');
  8344. if ((!$compressed) AND (isset($info['length2']))) {
  8345. $header = (ord($font{0}) == 128);
  8346. if ($header) {
  8347. // strip first binary header
  8348. $font = substr($font, 6);
  8349. }
  8350. if ($header AND (ord($font[$info['length1']]) == 128)) {
  8351. // strip second binary header
  8352. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  8353. }
  8354. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  8355. if ($compressed) {
  8356. // uncompress font
  8357. $font = gzuncompress($font);
  8358. }
  8359. // merge subset characters
  8360. $subsetchars = array(); // used chars
  8361. foreach ($info['fontkeys'] as $fontkey) {
  8362. $fontinfo = $this->getFontBuffer($fontkey);
  8363. $subsetchars += $fontinfo['subsetchars'];
  8364. }
  8365. // rebuild a font subset
  8366. $font = TCPDF_FONTS::_getTrueTypeFontSubset($font, $subsetchars);
  8367. // calculate new font length
  8368. $info['length1'] = strlen($font);
  8369. if ($compressed) {
  8370. // recompress font
  8371. $font = gzcompress($font);
  8372. }
  8373. }
  8374. $this->_newobj();
  8375. $this->FontFiles[$file]['n'] = $this->n;
  8376. $stream = $this->_getrawstream($font);
  8377. $out = '<< /Length '.strlen($stream);
  8378. if ($compressed) {
  8379. $out .= ' /Filter /FlateDecode';
  8380. }
  8381. $out .= ' /Length1 '.$info['length1'];
  8382. if (isset($info['length2'])) {
  8383. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  8384. }
  8385. $out .= ' >>';
  8386. $out .= ' stream'."\n".$stream."\n".'endstream';
  8387. $out .= "\n".'endobj';
  8388. $this->_out($out);
  8389. }
  8390. }
  8391. TCPDF_STATIC::set_mqr($mqr);
  8392. foreach ($this->fontkeys as $k) {
  8393. //Font objects
  8394. $font = $this->getFontBuffer($k);
  8395. $type = $font['type'];
  8396. $name = $font['name'];
  8397. if ($type == 'core') {
  8398. // standard core font
  8399. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8400. $out .= '<</Type /Font';
  8401. $out .= ' /Subtype /Type1';
  8402. $out .= ' /BaseFont /'.$name;
  8403. $out .= ' /Name /F'.$font['i'];
  8404. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  8405. $out .= ' /Encoding /WinAnsiEncoding';
  8406. }
  8407. if ($k == 'helvetica') {
  8408. // add default font for annotations
  8409. $this->annotation_fonts[$k] = $font['i'];
  8410. }
  8411. $out .= ' >>';
  8412. $out .= "\n".'endobj';
  8413. $this->_out($out);
  8414. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  8415. // additional Type1 or TrueType font
  8416. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8417. $out .= '<</Type /Font';
  8418. $out .= ' /Subtype /'.$type;
  8419. $out .= ' /BaseFont /'.$name;
  8420. $out .= ' /Name /F'.$font['i'];
  8421. $out .= ' /FirstChar 32 /LastChar 255';
  8422. $out .= ' /Widths '.($this->n + 1).' 0 R';
  8423. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  8424. if ($font['enc']) {
  8425. if (isset($font['diff'])) {
  8426. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  8427. } else {
  8428. $out .= ' /Encoding /WinAnsiEncoding';
  8429. }
  8430. }
  8431. $out .= ' >>';
  8432. $out .= "\n".'endobj';
  8433. $this->_out($out);
  8434. // Widths
  8435. $this->_newobj();
  8436. $s = '[';
  8437. for ($i = 32; $i < 256; ++$i) {
  8438. if (isset($font['cw'][$i])) {
  8439. $s .= $font['cw'][$i].' ';
  8440. } else {
  8441. $s .= $font['dw'].' ';
  8442. }
  8443. }
  8444. $s .= ']';
  8445. $s .= "\n".'endobj';
  8446. $this->_out($s);
  8447. //Descriptor
  8448. $this->_newobj();
  8449. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8450. foreach ($font['desc'] as $fdk => $fdv) {
  8451. if (is_float($fdv)) {
  8452. $fdv = sprintf('%F', $fdv);
  8453. }
  8454. $s .= ' /'.$fdk.' '.$fdv.'';
  8455. }
  8456. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8457. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8458. }
  8459. $s .= '>>';
  8460. $s .= "\n".'endobj';
  8461. $this->_out($s);
  8462. } else {
  8463. // additional types
  8464. $mtd = '_put'.strtolower($type);
  8465. if (!method_exists($this, $mtd)) {
  8466. $this->Error('Unsupported font type: '.$type);
  8467. }
  8468. $this->$mtd($font);
  8469. }
  8470. }
  8471. }
  8472. /**
  8473. * Adds unicode fonts.<br>
  8474. * Based on PDF Reference 1.3 (section 5)
  8475. * @param $font (array) font data
  8476. * @protected
  8477. * @author Nicola Asuni
  8478. * @since 1.52.0.TC005 (2005-01-05)
  8479. */
  8480. protected function _puttruetypeunicode($font) {
  8481. $fontname = '';
  8482. if ($font['subset']) {
  8483. // change name for font subsetting
  8484. $subtag = sprintf('%06u', $font['i']);
  8485. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  8486. $fontname .= $subtag.'+';
  8487. }
  8488. $fontname .= $font['name'];
  8489. // Type0 Font
  8490. // A composite font composed of other fonts, organized hierarchically
  8491. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8492. $out .= '<< /Type /Font';
  8493. $out .= ' /Subtype /Type0';
  8494. $out .= ' /BaseFont /'.$fontname;
  8495. $out .= ' /Name /F'.$font['i'];
  8496. $out .= ' /Encoding /'.$font['enc'];
  8497. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  8498. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  8499. $out .= ' >>';
  8500. $out .= "\n".'endobj';
  8501. $this->_out($out);
  8502. // ToUnicode map for Identity-H
  8503. $stream = TCPDF_FONT_DATA::$uni_identity_h;
  8504. // ToUnicode Object
  8505. $this->_newobj();
  8506. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  8507. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8508. $stream = $this->_getrawstream($stream);
  8509. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  8510. // CIDFontType2
  8511. // A CIDFont whose glyph descriptions are based on TrueType font technology
  8512. $oid = $this->_newobj();
  8513. $out = '<< /Type /Font';
  8514. $out .= ' /Subtype /CIDFontType2';
  8515. $out .= ' /BaseFont /'.$fontname;
  8516. // A dictionary containing entries that define the character collection of the CIDFont.
  8517. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8518. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8519. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8520. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  8521. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8522. $out .= ' /DW '.$font['dw']; // default width
  8523. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, 0);
  8524. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8525. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  8526. }
  8527. $out .= ' >>';
  8528. $out .= "\n".'endobj';
  8529. $this->_out($out);
  8530. // Font descriptor
  8531. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  8532. $this->_newobj();
  8533. $out = '<< /Type /FontDescriptor';
  8534. $out .= ' /FontName /'.$fontname;
  8535. foreach ($font['desc'] as $key => $value) {
  8536. if (is_float($value)) {
  8537. $value = sprintf('%F', $value);
  8538. }
  8539. $out .= ' /'.$key.' '.$value;
  8540. }
  8541. $fontdir = false;
  8542. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8543. // A stream containing a TrueType font
  8544. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8545. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  8546. }
  8547. $out .= ' >>';
  8548. $out .= "\n".'endobj';
  8549. $this->_out($out);
  8550. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8551. $this->_newobj();
  8552. // Embed CIDToGIDMap
  8553. // A specification of the mapping from CIDs to glyph indices
  8554. // search and get CTG font file to embedd
  8555. $ctgfile = strtolower($font['ctg']);
  8556. // search and get ctg font file to embedd
  8557. $fontfile = TCPDF_FONTS::getFontFullPath($ctgfile, $fontdir);
  8558. if (TCPDF_STATIC::empty_string($fontfile)) {
  8559. $this->Error('Font file not found: '.$ctgfile);
  8560. }
  8561. $stream = $this->_getrawstream(file_get_contents($fontfile));
  8562. $out = '<< /Length '.strlen($stream).'';
  8563. if (substr($fontfile, -2) == '.z') { // check file extension
  8564. // Decompresses data encoded using the public-domain
  8565. // zlib/deflate compression method, reproducing the
  8566. // original text or binary data
  8567. $out .= ' /Filter /FlateDecode';
  8568. }
  8569. $out .= ' >>';
  8570. $out .= ' stream'."\n".$stream."\n".'endstream';
  8571. $out .= "\n".'endobj';
  8572. $this->_out($out);
  8573. }
  8574. }
  8575. /**
  8576. * Output CID-0 fonts.
  8577. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  8578. * @param $font (array) font data
  8579. * @protected
  8580. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  8581. * @since 3.2.000 (2008-06-23)
  8582. */
  8583. protected function _putcidfont0($font) {
  8584. $cidoffset = 0;
  8585. if (!isset($font['cw'][1])) {
  8586. $cidoffset = 31;
  8587. }
  8588. if (isset($font['cidinfo']['uni2cid'])) {
  8589. // convert unicode to cid.
  8590. $uni2cid = $font['cidinfo']['uni2cid'];
  8591. $cw = array();
  8592. foreach ($font['cw'] as $uni => $width) {
  8593. if (isset($uni2cid[$uni])) {
  8594. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  8595. } elseif ($uni < 256) {
  8596. $cw[$uni] = $width;
  8597. } // else unknown character
  8598. }
  8599. $font = array_merge($font, array('cw' => $cw));
  8600. }
  8601. $name = $font['name'];
  8602. $enc = $font['enc'];
  8603. if ($enc) {
  8604. $longname = $name.'-'.$enc;
  8605. } else {
  8606. $longname = $name;
  8607. }
  8608. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8609. $out .= '<</Type /Font';
  8610. $out .= ' /Subtype /Type0';
  8611. $out .= ' /BaseFont /'.$longname;
  8612. $out .= ' /Name /F'.$font['i'];
  8613. if ($enc) {
  8614. $out .= ' /Encoding /'.$enc;
  8615. }
  8616. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  8617. $out .= ' >>';
  8618. $out .= "\n".'endobj';
  8619. $this->_out($out);
  8620. $oid = $this->_newobj();
  8621. $out = '<</Type /Font';
  8622. $out .= ' /Subtype /CIDFontType0';
  8623. $out .= ' /BaseFont /'.$name;
  8624. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8625. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8626. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8627. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  8628. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8629. $out .= ' /DW '.$font['dw'];
  8630. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, $cidoffset);
  8631. $out .= ' >>';
  8632. $out .= "\n".'endobj';
  8633. $this->_out($out);
  8634. $this->_newobj();
  8635. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8636. foreach ($font['desc'] as $k => $v) {
  8637. if ($k != 'Style') {
  8638. if (is_float($v)) {
  8639. $v = sprintf('%F', $v);
  8640. }
  8641. $s .= ' /'.$k.' '.$v.'';
  8642. }
  8643. }
  8644. $s .= '>>';
  8645. $s .= "\n".'endobj';
  8646. $this->_out($s);
  8647. }
  8648. /**
  8649. * Output images.
  8650. * @protected
  8651. */
  8652. protected function _putimages() {
  8653. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8654. foreach ($this->imagekeys as $file) {
  8655. $info = $this->getImageBuffer($file);
  8656. // set object for alternate images array
  8657. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  8658. $altoid = $this->_newobj();
  8659. $out = '[';
  8660. foreach ($info['altimgs'] as $altimage) {
  8661. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  8662. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  8663. $out .= ' /DefaultForPrinting';
  8664. if ($altimage[1] === true) {
  8665. $out .= ' true';
  8666. } else {
  8667. $out .= ' false';
  8668. }
  8669. $out .= ' >>';
  8670. }
  8671. }
  8672. $out .= ' ]';
  8673. $out .= "\n".'endobj';
  8674. $this->_out($out);
  8675. }
  8676. // set image object
  8677. $oid = $this->_newobj();
  8678. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  8679. $this->setImageSubBuffer($file, 'n', $this->n);
  8680. $out = '<</Type /XObject';
  8681. $out .= ' /Subtype /Image';
  8682. $out .= ' /Width '.$info['w'];
  8683. $out .= ' /Height '.$info['h'];
  8684. if (array_key_exists('masked', $info)) {
  8685. $out .= ' /SMask '.($this->n - 1).' 0 R';
  8686. }
  8687. // set color space
  8688. $icc = false;
  8689. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  8690. // ICC Colour Space
  8691. $icc = true;
  8692. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  8693. } elseif ($info['cs'] == 'Indexed') {
  8694. // Indexed Colour Space
  8695. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  8696. } else {
  8697. // Device Colour Space
  8698. $out .= ' /ColorSpace /'.$info['cs'];
  8699. }
  8700. if ($info['cs'] == 'DeviceCMYK') {
  8701. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  8702. }
  8703. $out .= ' /BitsPerComponent '.$info['bpc'];
  8704. if (isset($altoid) AND ($altoid > 0)) {
  8705. // reference to alternate images dictionary
  8706. $out .= ' /Alternates '.$altoid.' 0 R';
  8707. }
  8708. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  8709. // external stream
  8710. $out .= ' /Length 0';
  8711. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  8712. if (isset($info['f'])) {
  8713. $out .= ' /FFilter /'.$info['f'];
  8714. }
  8715. $out .= ' >>';
  8716. $out .= ' stream'."\n".'endstream';
  8717. } else {
  8718. if (isset($info['f'])) {
  8719. $out .= ' /Filter /'.$info['f'];
  8720. }
  8721. if (isset($info['parms'])) {
  8722. $out .= ' '.$info['parms'];
  8723. }
  8724. if (isset($info['trns']) AND is_array($info['trns'])) {
  8725. $trns = '';
  8726. $count_info = count($info['trns']);
  8727. for ($i=0; $i < $count_info; ++$i) {
  8728. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  8729. }
  8730. $out .= ' /Mask ['.$trns.']';
  8731. }
  8732. $stream = $this->_getrawstream($info['data']);
  8733. $out .= ' /Length '.strlen($stream).' >>';
  8734. $out .= ' stream'."\n".$stream."\n".'endstream';
  8735. }
  8736. $out .= "\n".'endobj';
  8737. $this->_out($out);
  8738. if ($icc) {
  8739. // ICC colour profile
  8740. $this->_newobj();
  8741. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  8742. $icc = $this->_getrawstream($icc);
  8743. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  8744. } elseif ($info['cs'] == 'Indexed') {
  8745. // colour palette
  8746. $this->_newobj();
  8747. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  8748. $pal = $this->_getrawstream($pal);
  8749. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  8750. }
  8751. }
  8752. }
  8753. /**
  8754. * Output Form XObjects Templates.
  8755. * @author Nicola Asuni
  8756. * @since 5.8.017 (2010-08-24)
  8757. * @protected
  8758. * @see startTemplate(), endTemplate(), printTemplate()
  8759. */
  8760. protected function _putxobjects() {
  8761. foreach ($this->xobjects as $key => $data) {
  8762. if (isset($data['outdata'])) {
  8763. $stream = str_replace($this->epsmarker, '', trim($data['outdata']));
  8764. $out = $this->_getobj($data['n'])."\n";
  8765. $out .= '<<';
  8766. $out .= ' /Type /XObject';
  8767. $out .= ' /Subtype /Form';
  8768. $out .= ' /FormType 1';
  8769. if ($this->compress) {
  8770. $stream = gzcompress($stream);
  8771. $out .= ' /Filter /FlateDecode';
  8772. }
  8773. $out .= sprintf(' /BBox [%F %F %F %F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  8774. $out .= ' /Matrix [1 0 0 1 0 0]';
  8775. $out .= ' /Resources <<';
  8776. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8777. if (!$this->pdfa_mode) {
  8778. // transparency
  8779. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  8780. $out .= ' /ExtGState <<';
  8781. foreach ($data['extgstates'] as $k => $extgstate) {
  8782. if (isset($this->extgstates[$k]['name'])) {
  8783. $out .= ' /'.$this->extgstates[$k]['name'];
  8784. } else {
  8785. $out .= ' /GS'.$k;
  8786. }
  8787. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  8788. }
  8789. $out .= ' >>';
  8790. }
  8791. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  8792. $gp = '';
  8793. $gs = '';
  8794. foreach ($data['gradients'] as $id => $grad) {
  8795. // gradient patterns
  8796. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  8797. // gradient shadings
  8798. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  8799. }
  8800. $out .= ' /Pattern <<'.$gp.' >>';
  8801. $out .= ' /Shading <<'.$gs.' >>';
  8802. }
  8803. }
  8804. // spot colors
  8805. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  8806. $out .= ' /ColorSpace <<';
  8807. foreach ($data['spot_colors'] as $name => $color) {
  8808. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  8809. }
  8810. $out .= ' >>';
  8811. }
  8812. // fonts
  8813. if (!empty($data['fonts'])) {
  8814. $out .= ' /Font <<';
  8815. foreach ($data['fonts'] as $fontkey => $fontid) {
  8816. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  8817. }
  8818. $out .= ' >>';
  8819. }
  8820. // images or nested xobjects
  8821. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  8822. $out .= ' /XObject <<';
  8823. foreach ($data['images'] as $imgid) {
  8824. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  8825. }
  8826. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  8827. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  8828. }
  8829. $out .= ' >>';
  8830. }
  8831. $out .= ' >>'; //end resources
  8832. if (isset($data['group']) AND ($data['group'] !== false)) {
  8833. // set transparency group
  8834. $out .= ' /Group << /Type /Group /S /Transparency';
  8835. if (is_array($data['group'])) {
  8836. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  8837. $out .= ' /CS /'.$data['group']['CS'];
  8838. }
  8839. if (isset($data['group']['I'])) {
  8840. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  8841. }
  8842. if (isset($data['group']['K'])) {
  8843. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  8844. }
  8845. }
  8846. $out .= ' >>';
  8847. }
  8848. $stream = $this->_getrawstream($stream, $data['n']);
  8849. $out .= ' /Length '.strlen($stream);
  8850. $out .= ' >>';
  8851. $out .= ' stream'."\n".$stream."\n".'endstream';
  8852. $out .= "\n".'endobj';
  8853. $this->_out($out);
  8854. }
  8855. }
  8856. }
  8857. /**
  8858. * Output Spot Colors Resources.
  8859. * @protected
  8860. * @since 4.0.024 (2008-09-12)
  8861. */
  8862. protected function _putspotcolors() {
  8863. foreach ($this->spot_colors as $name => $color) {
  8864. $this->_newobj();
  8865. $this->spot_colors[$name]['n'] = $this->n;
  8866. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  8867. $out .= ' /DeviceCMYK <<';
  8868. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  8869. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  8870. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  8871. $out .= "\n".'endobj';
  8872. $this->_out($out);
  8873. }
  8874. }
  8875. /**
  8876. * Return XObjects Dictionary.
  8877. * @return string XObjects dictionary
  8878. * @protected
  8879. * @since 5.8.014 (2010-08-23)
  8880. */
  8881. protected function _getxobjectdict() {
  8882. $out = '';
  8883. foreach ($this->xobjects as $id => $objid) {
  8884. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  8885. }
  8886. return $out;
  8887. }
  8888. /**
  8889. * Output Resources Dictionary.
  8890. * @protected
  8891. */
  8892. protected function _putresourcedict() {
  8893. $out = $this->_getobj(2)."\n";
  8894. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8895. $out .= ' /Font <<';
  8896. foreach ($this->fontkeys as $fontkey) {
  8897. $font = $this->getFontBuffer($fontkey);
  8898. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  8899. }
  8900. $out .= ' >>';
  8901. $out .= ' /XObject <<';
  8902. $out .= $this->_getxobjectdict();
  8903. $out .= ' >>';
  8904. // layers
  8905. if (!empty($this->pdflayers)) {
  8906. $out .= ' /Properties <<';
  8907. foreach ($this->pdflayers as $layer) {
  8908. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  8909. }
  8910. $out .= ' >>';
  8911. }
  8912. if (!$this->pdfa_mode) {
  8913. // transparency
  8914. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  8915. $out .= ' /ExtGState <<';
  8916. foreach ($this->extgstates as $k => $extgstate) {
  8917. if (isset($extgstate['name'])) {
  8918. $out .= ' /'.$extgstate['name'];
  8919. } else {
  8920. $out .= ' /GS'.$k;
  8921. }
  8922. $out .= ' '.$extgstate['n'].' 0 R';
  8923. }
  8924. $out .= ' >>';
  8925. }
  8926. if (isset($this->gradients) AND !empty($this->gradients)) {
  8927. $gp = '';
  8928. $gs = '';
  8929. foreach ($this->gradients as $id => $grad) {
  8930. // gradient patterns
  8931. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  8932. // gradient shadings
  8933. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  8934. }
  8935. $out .= ' /Pattern <<'.$gp.' >>';
  8936. $out .= ' /Shading <<'.$gs.' >>';
  8937. }
  8938. }
  8939. // spot colors
  8940. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  8941. $out .= ' /ColorSpace <<';
  8942. foreach ($this->spot_colors as $color) {
  8943. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  8944. }
  8945. $out .= ' >>';
  8946. }
  8947. $out .= ' >>';
  8948. $out .= "\n".'endobj';
  8949. $this->_out($out);
  8950. }
  8951. /**
  8952. * Output Resources.
  8953. * @protected
  8954. */
  8955. protected function _putresources() {
  8956. $this->_putextgstates();
  8957. $this->_putocg();
  8958. $this->_putfonts();
  8959. $this->_putimages();
  8960. $this->_putspotcolors();
  8961. $this->_putshaders();
  8962. $this->_putxobjects();
  8963. $this->_putresourcedict();
  8964. $this->_putdests();
  8965. $this->_putEmbeddedFiles();
  8966. $this->_putannotsobjs();
  8967. $this->_putjavascript();
  8968. $this->_putbookmarks();
  8969. $this->_putencryption();
  8970. }
  8971. /**
  8972. * Adds some Metadata information (Document Information Dictionary)
  8973. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  8974. * @return int object id
  8975. * @protected
  8976. */
  8977. protected function _putinfo() {
  8978. $oid = $this->_newobj();
  8979. $out = '<<';
  8980. // store current isunicode value
  8981. $prev_isunicode = $this->isunicode;
  8982. if ($this->docinfounicode) {
  8983. $this->isunicode = true;
  8984. }
  8985. if (!TCPDF_STATIC::empty_string($this->title)) {
  8986. // The document's title.
  8987. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  8988. }
  8989. if (!TCPDF_STATIC::empty_string($this->author)) {
  8990. // The name of the person who created the document.
  8991. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  8992. }
  8993. if (!TCPDF_STATIC::empty_string($this->subject)) {
  8994. // The subject of the document.
  8995. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  8996. }
  8997. if (!TCPDF_STATIC::empty_string($this->keywords)) {
  8998. // Keywords associated with the document.
  8999. $out .= ' /Keywords '.$this->_textstring($this->keywords, $oid);
  9000. }
  9001. if (!TCPDF_STATIC::empty_string($this->creator)) {
  9002. // 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.
  9003. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  9004. }
  9005. // restore previous isunicode value
  9006. $this->isunicode = $prev_isunicode;
  9007. // default producer
  9008. $out .= ' /Producer '.$this->_textstring(TCPDF_STATIC::getTCPDFProducer(), $oid);
  9009. // The date and time the document was created, in human-readable form
  9010. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  9011. // The date and time the document was most recently modified, in human-readable form
  9012. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  9013. // A name object indicating whether the document has been modified to include trapping information
  9014. $out .= ' /Trapped /False';
  9015. $out .= ' >>';
  9016. $out .= "\n".'endobj';
  9017. $this->_out($out);
  9018. return $oid;
  9019. }
  9020. /**
  9021. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  9022. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  9023. * @param $xmp (string) Custom XMP data.
  9024. * @since 5.9.128 (2011-10-06)
  9025. * @public
  9026. */
  9027. public function setExtraXMP($xmp) {
  9028. $this->custom_xmp = $xmp;
  9029. }
  9030. /**
  9031. * Put XMP data object and return ID.
  9032. * @return (int) The object ID.
  9033. * @since 5.9.121 (2011-09-28)
  9034. * @protected
  9035. */
  9036. protected function _putXMP() {
  9037. $oid = $this->_newobj();
  9038. // store current isunicode value
  9039. $prev_isunicode = $this->isunicode;
  9040. $this->isunicode = true;
  9041. $prev_encrypted = $this->encrypted;
  9042. $this->encrypted = false;
  9043. // set XMP data
  9044. $xmp = '<?xpacket begin="'.TCPDF_FONTS::unichr(0xfeff, $this->isunicode).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  9045. $xmp .= '<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04">'."\n";
  9046. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  9047. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  9048. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  9049. $xmp .= "\t\t\t".'<dc:title>'."\n";
  9050. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9051. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->title).'</rdf:li>'."\n";
  9052. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9053. $xmp .= "\t\t\t".'</dc:title>'."\n";
  9054. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  9055. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  9056. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->author).'</rdf:li>'."\n";
  9057. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  9058. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  9059. $xmp .= "\t\t\t".'<dc:description>'."\n";
  9060. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9061. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->subject).'</rdf:li>'."\n";
  9062. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9063. $xmp .= "\t\t\t".'</dc:description>'."\n";
  9064. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  9065. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9066. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->keywords).'</rdf:li>'."\n";
  9067. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9068. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  9069. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9070. // convert doc creation date format
  9071. $dcdate = TCPDF_STATIC::getFormattedDate($this->doc_creation_timestamp);
  9072. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  9073. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  9074. $doccreationdate .= '+'.substr($dcdate, 15, 2).':'.substr($dcdate, 18, 2);
  9075. $doccreationdate = TCPDF_STATIC::_escapeXML($doccreationdate);
  9076. // convert doc modification date format
  9077. $dmdate = TCPDF_STATIC::getFormattedDate($this->doc_modification_timestamp);
  9078. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  9079. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  9080. $docmoddate .= '+'.substr($dmdate, 15, 2).':'.substr($dmdate, 18, 2);
  9081. $docmoddate = TCPDF_STATIC::_escapeXML($docmoddate);
  9082. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  9083. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  9084. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  9085. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  9086. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  9087. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9088. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  9089. $xmp .= "\t\t\t".'<pdf:Keywords>'.TCPDF_STATIC::_escapeXML($this->keywords).'</pdf:Keywords>'."\n";
  9090. $xmp .= "\t\t\t".'<pdf:Producer>'.TCPDF_STATIC::_escapeXML(TCPDF_STATIC::getTCPDFProducer()).'</pdf:Producer>'."\n";
  9091. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9092. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  9093. $uuid = 'uuid:'.substr($this->file_id, 0, 8).'-'.substr($this->file_id, 8, 4).'-'.substr($this->file_id, 12, 4).'-'.substr($this->file_id, 16, 4).'-'.substr($this->file_id, 20, 12);
  9094. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  9095. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  9096. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9097. if ($this->pdfa_mode) {
  9098. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  9099. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  9100. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  9101. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9102. }
  9103. // XMP extension schemas
  9104. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">'."\n";
  9105. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  9106. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9107. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9108. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  9109. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  9110. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  9111. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9112. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9113. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  9114. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  9115. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  9116. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9117. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9118. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9119. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9120. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  9121. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  9122. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  9123. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9124. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9125. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9126. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9127. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9128. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  9129. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  9130. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  9131. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9132. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9133. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9134. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9135. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  9136. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  9137. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  9138. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9139. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9140. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9141. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  9142. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  9143. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9144. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9145. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9146. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9147. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  9148. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  9149. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9150. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9151. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9152. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9153. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9154. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9155. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  9156. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9157. $xmp .= "\t".'</rdf:RDF>'."\n";
  9158. $xmp .= $this->custom_xmp;
  9159. $xmp .= '</x:xmpmeta>'."\n";
  9160. $xmp .= '<?xpacket end="w"?>';
  9161. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  9162. // restore previous isunicode value
  9163. $this->isunicode = $prev_isunicode;
  9164. $this->encrypted = $prev_encrypted;
  9165. $this->_out($out);
  9166. return $oid;
  9167. }
  9168. /**
  9169. * Output Catalog.
  9170. * @return int object id
  9171. * @protected
  9172. */
  9173. protected function _putcatalog() {
  9174. // put XMP
  9175. $xmpobj = $this->_putXMP();
  9176. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  9177. if ($this->pdfa_mode OR $this->force_srgb) {
  9178. $iccobj = $this->_newobj();
  9179. $icc = file_get_contents(dirname(__FILE__).'/include/sRGB.icc');
  9180. $filter = '';
  9181. if ($this->compress) {
  9182. $filter = ' /Filter /FlateDecode';
  9183. $icc = gzcompress($icc);
  9184. }
  9185. $icc = $this->_getrawstream($icc);
  9186. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  9187. }
  9188. // start catalog
  9189. $oid = $this->_newobj();
  9190. $out = '<< /Type /Catalog';
  9191. $out .= ' /Version /'.$this->PDFVersion;
  9192. //$out .= ' /Extensions <<>>';
  9193. $out .= ' /Pages 1 0 R';
  9194. //$out .= ' /PageLabels ' //...;
  9195. $out .= ' /Names <<';
  9196. if ((!$this->pdfa_mode) AND !empty($this->n_js)) {
  9197. $out .= ' /JavaScript '.$this->n_js;
  9198. }
  9199. if (!empty($this->efnames)) {
  9200. $out .= ' /EmbeddedFiles <</Names [';
  9201. foreach ($this->efnames AS $fn => $fref) {
  9202. $out .= ' '.$this->_datastring($fn).' '.$fref;
  9203. }
  9204. $out .= ' ]>>';
  9205. }
  9206. $out .= ' >>';
  9207. if (!empty($this->dests)) {
  9208. $out .= ' /Dests '.($this->n_dests).' 0 R';
  9209. }
  9210. $out .= $this->_putviewerpreferences();
  9211. if (isset($this->LayoutMode) AND (!TCPDF_STATIC::empty_string($this->LayoutMode))) {
  9212. $out .= ' /PageLayout /'.$this->LayoutMode;
  9213. }
  9214. if (isset($this->PageMode) AND (!TCPDF_STATIC::empty_string($this->PageMode))) {
  9215. $out .= ' /PageMode /'.$this->PageMode;
  9216. }
  9217. if (count($this->outlines) > 0) {
  9218. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  9219. $out .= ' /PageMode /UseOutlines';
  9220. }
  9221. //$out .= ' /Threads []';
  9222. if ($this->ZoomMode == 'fullpage') {
  9223. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  9224. } elseif ($this->ZoomMode == 'fullwidth') {
  9225. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  9226. } elseif ($this->ZoomMode == 'real') {
  9227. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  9228. } elseif (!is_string($this->ZoomMode)) {
  9229. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  9230. }
  9231. //$out .= ' /AA <<>>';
  9232. //$out .= ' /URI <<>>';
  9233. $out .= ' /Metadata '.$xmpobj.' 0 R';
  9234. //$out .= ' /StructTreeRoot <<>>';
  9235. //$out .= ' /MarkInfo <<>>';
  9236. if (isset($this->l['a_meta_language'])) {
  9237. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  9238. }
  9239. //$out .= ' /SpiderInfo <<>>';
  9240. // set OutputIntent to sRGB IEC61966-2.1 if required
  9241. if ($this->pdfa_mode OR $this->force_srgb) {
  9242. $out .= ' /OutputIntents [<<';
  9243. $out .= ' /Type /OutputIntent';
  9244. $out .= ' /S /GTS_PDFA1';
  9245. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9246. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9247. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  9248. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9249. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  9250. $out .= ' >>]';
  9251. }
  9252. //$out .= ' /PieceInfo <<>>';
  9253. if (!empty($this->pdflayers)) {
  9254. $lyrobjs = '';
  9255. $lyrobjs_print = '';
  9256. $lyrobjs_view = '';
  9257. foreach ($this->pdflayers as $layer) {
  9258. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  9259. if ($layer['print']) {
  9260. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  9261. }
  9262. if ($layer['view']) {
  9263. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  9264. }
  9265. }
  9266. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  9267. $out .= ' /D <<';
  9268. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  9269. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  9270. $out .= ' /BaseState /ON';
  9271. $out .= ' /ON ['.$lyrobjs_print.']';
  9272. $out .= ' /OFF ['.$lyrobjs_view.']';
  9273. $out .= ' /Intent /View';
  9274. $out .= ' /AS [';
  9275. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  9276. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  9277. $out .= ' ]';
  9278. $out .= ' /Order ['.$lyrobjs.']';
  9279. $out .= ' /ListMode /AllPages';
  9280. //$out .= ' /RBGroups ['..']';
  9281. //$out .= ' /Locked ['..']';
  9282. $out .= ' >>';
  9283. $out .= ' >>';
  9284. }
  9285. // AcroForm
  9286. if (!empty($this->form_obj_id)
  9287. OR ($this->sign AND isset($this->signature_data['cert_type']))
  9288. OR !empty($this->empty_signature_appearance)) {
  9289. $out .= ' /AcroForm <<';
  9290. $objrefs = '';
  9291. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9292. // set reference for signature object
  9293. $objrefs .= $this->sig_obj_id.' 0 R';
  9294. }
  9295. if (!empty($this->empty_signature_appearance)) {
  9296. foreach ($this->empty_signature_appearance as $esa) {
  9297. // set reference for empty signature objects
  9298. $objrefs .= ' '.$esa['objid'].' 0 R';
  9299. }
  9300. }
  9301. if (!empty($this->form_obj_id)) {
  9302. foreach($this->form_obj_id as $objid) {
  9303. $objrefs .= ' '.$objid.' 0 R';
  9304. }
  9305. }
  9306. $out .= ' /Fields ['.$objrefs.']';
  9307. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  9308. $out .= ' /NeedAppearances false';
  9309. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9310. if ($this->signature_data['cert_type'] > 0) {
  9311. $out .= ' /SigFlags 3';
  9312. } else {
  9313. $out .= ' /SigFlags 1';
  9314. }
  9315. }
  9316. //$out .= ' /CO ';
  9317. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  9318. $out .= ' /DR <<';
  9319. $out .= ' /Font <<';
  9320. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  9321. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  9322. }
  9323. $out .= ' >> >>';
  9324. }
  9325. $font = $this->getFontBuffer('helvetica');
  9326. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  9327. $out .= ' /Q '.(($this->rtl)?'2':'0');
  9328. //$out .= ' /XFA ';
  9329. $out .= ' >>';
  9330. // signatures
  9331. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9332. if ($this->signature_data['cert_type'] > 0) {
  9333. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  9334. } else {
  9335. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  9336. }
  9337. }
  9338. }
  9339. //$out .= ' /Legal <<>>';
  9340. //$out .= ' /Requirements []';
  9341. //$out .= ' /Collection <<>>';
  9342. //$out .= ' /NeedsRendering true';
  9343. $out .= ' >>';
  9344. $out .= "\n".'endobj';
  9345. $this->_out($out);
  9346. return $oid;
  9347. }
  9348. /**
  9349. * Output viewer preferences.
  9350. * @return string for viewer preferences
  9351. * @author Nicola asuni
  9352. * @since 3.1.000 (2008-06-09)
  9353. * @protected
  9354. */
  9355. protected function _putviewerpreferences() {
  9356. $vp = $this->viewer_preferences;
  9357. $out = ' /ViewerPreferences <<';
  9358. if ($this->rtl) {
  9359. $out .= ' /Direction /R2L';
  9360. } else {
  9361. $out .= ' /Direction /L2R';
  9362. }
  9363. if (isset($vp['HideToolbar']) AND ($vp['HideToolbar'])) {
  9364. $out .= ' /HideToolbar true';
  9365. }
  9366. if (isset($vp['HideMenubar']) AND ($vp['HideMenubar'])) {
  9367. $out .= ' /HideMenubar true';
  9368. }
  9369. if (isset($vp['HideWindowUI']) AND ($vp['HideWindowUI'])) {
  9370. $out .= ' /HideWindowUI true';
  9371. }
  9372. if (isset($vp['FitWindow']) AND ($vp['FitWindow'])) {
  9373. $out .= ' /FitWindow true';
  9374. }
  9375. if (isset($vp['CenterWindow']) AND ($vp['CenterWindow'])) {
  9376. $out .= ' /CenterWindow true';
  9377. }
  9378. if (isset($vp['DisplayDocTitle']) AND ($vp['DisplayDocTitle'])) {
  9379. $out .= ' /DisplayDocTitle true';
  9380. }
  9381. if (isset($vp['NonFullScreenPageMode'])) {
  9382. $out .= ' /NonFullScreenPageMode /'.$vp['NonFullScreenPageMode'];
  9383. }
  9384. if (isset($vp['ViewArea'])) {
  9385. $out .= ' /ViewArea /'.$vp['ViewArea'];
  9386. }
  9387. if (isset($vp['ViewClip'])) {
  9388. $out .= ' /ViewClip /'.$vp['ViewClip'];
  9389. }
  9390. if (isset($vp['PrintArea'])) {
  9391. $out .= ' /PrintArea /'.$vp['PrintArea'];
  9392. }
  9393. if (isset($vp['PrintClip'])) {
  9394. $out .= ' /PrintClip /'.$vp['PrintClip'];
  9395. }
  9396. if (isset($vp['PrintScaling'])) {
  9397. $out .= ' /PrintScaling /'.$vp['PrintScaling'];
  9398. }
  9399. if (isset($vp['Duplex']) AND (!TCPDF_STATIC::empty_string($vp['Duplex']))) {
  9400. $out .= ' /Duplex /'.$vp['Duplex'];
  9401. }
  9402. if (isset($vp['PickTrayByPDFSize'])) {
  9403. if ($vp['PickTrayByPDFSize']) {
  9404. $out .= ' /PickTrayByPDFSize true';
  9405. } else {
  9406. $out .= ' /PickTrayByPDFSize false';
  9407. }
  9408. }
  9409. if (isset($vp['PrintPageRange'])) {
  9410. $PrintPageRangeNum = '';
  9411. foreach ($vp['PrintPageRange'] as $k => $v) {
  9412. $PrintPageRangeNum .= ' '.($v - 1).'';
  9413. }
  9414. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  9415. }
  9416. if (isset($vp['NumCopies'])) {
  9417. $out .= ' /NumCopies '.intval($vp['NumCopies']);
  9418. }
  9419. $out .= ' >>';
  9420. return $out;
  9421. }
  9422. /**
  9423. * Output PDF File Header (7.5.2).
  9424. * @protected
  9425. */
  9426. protected function _putheader() {
  9427. $this->_out('%PDF-'.$this->PDFVersion);
  9428. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  9429. }
  9430. /**
  9431. * Output end of document (EOF).
  9432. * @protected
  9433. */
  9434. protected function _enddoc() {
  9435. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  9436. // save subset chars of the previous font
  9437. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  9438. }
  9439. $this->state = 1;
  9440. $this->_putheader();
  9441. $this->_putpages();
  9442. $this->_putresources();
  9443. // empty signature fields
  9444. if (!empty($this->empty_signature_appearance)) {
  9445. foreach ($this->empty_signature_appearance as $key => $esa) {
  9446. // widget annotation for empty signature
  9447. $out = $this->_getobj($esa['objid'])."\n";
  9448. $out .= '<< /Type /Annot';
  9449. $out .= ' /Subtype /Widget';
  9450. $out .= ' /Rect ['.$esa['rect'].']';
  9451. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  9452. $out .= ' /F 4';
  9453. $out .= ' /FT /Sig';
  9454. $signame = $esa['name'].sprintf(' [%03d]', ($key + 1));
  9455. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  9456. $out .= ' /Ff 0';
  9457. $out .= ' >>';
  9458. $out .= "\n".'endobj';
  9459. $this->_out($out);
  9460. }
  9461. }
  9462. // Signature
  9463. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9464. // widget annotation for signature
  9465. $out = $this->_getobj($this->sig_obj_id)."\n";
  9466. $out .= '<< /Type /Annot';
  9467. $out .= ' /Subtype /Widget';
  9468. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  9469. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  9470. $out .= ' /F 4';
  9471. $out .= ' /FT /Sig';
  9472. $out .= ' /T '.$this->_textstring($this->signature_appearance['name'], $this->sig_obj_id);
  9473. $out .= ' /Ff 0';
  9474. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  9475. $out .= ' >>';
  9476. $out .= "\n".'endobj';
  9477. $this->_out($out);
  9478. // signature
  9479. $this->_putsignature();
  9480. }
  9481. // Info
  9482. $objid_info = $this->_putinfo();
  9483. // Catalog
  9484. $objid_catalog = $this->_putcatalog();
  9485. // Cross-ref
  9486. $o = $this->bufferlen;
  9487. // XREF section
  9488. $this->_out('xref');
  9489. $this->_out('0 '.($this->n + 1));
  9490. $this->_out('0000000000 65535 f ');
  9491. $freegen = ($this->n + 2);
  9492. for ($i=1; $i <= $this->n; ++$i) {
  9493. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  9494. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  9495. ++$freegen;
  9496. } else {
  9497. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  9498. }
  9499. }
  9500. // TRAILER
  9501. $out = 'trailer'."\n";
  9502. $out .= '<<';
  9503. $out .= ' /Size '.($this->n + 1);
  9504. $out .= ' /Root '.$objid_catalog.' 0 R';
  9505. $out .= ' /Info '.$objid_info.' 0 R';
  9506. if ($this->encrypted) {
  9507. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  9508. }
  9509. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  9510. $out .= ' >>';
  9511. $this->_out($out);
  9512. $this->_out('startxref');
  9513. $this->_out($o);
  9514. $this->_out('%%EOF');
  9515. $this->state = 3; // end-of-doc
  9516. if ($this->diskcache) {
  9517. // remove temporary files used for images
  9518. foreach ($this->imagekeys as $key) {
  9519. // remove temporary files
  9520. unlink($this->images[$key]);
  9521. }
  9522. foreach ($this->fontkeys as $key) {
  9523. // remove temporary files
  9524. unlink($this->fonts[$key]);
  9525. }
  9526. }
  9527. }
  9528. /**
  9529. * Initialize a new page.
  9530. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  9531. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  9532. * @protected
  9533. * @see getPageSizeFromFormat(), setPageFormat()
  9534. */
  9535. protected function _beginpage($orientation='', $format='') {
  9536. ++$this->page;
  9537. $this->pageobjects[$this->page] = array();
  9538. $this->setPageBuffer($this->page, '');
  9539. // initialize array for graphics tranformation positions inside a page buffer
  9540. $this->transfmrk[$this->page] = array();
  9541. $this->state = 2;
  9542. if (TCPDF_STATIC::empty_string($orientation)) {
  9543. if (isset($this->CurOrientation)) {
  9544. $orientation = $this->CurOrientation;
  9545. } elseif ($this->fwPt > $this->fhPt) {
  9546. // landscape
  9547. $orientation = 'L';
  9548. } else {
  9549. // portrait
  9550. $orientation = 'P';
  9551. }
  9552. }
  9553. if (TCPDF_STATIC::empty_string($format)) {
  9554. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  9555. $this->setPageOrientation($orientation);
  9556. } else {
  9557. $this->setPageFormat($format, $orientation);
  9558. }
  9559. if ($this->rtl) {
  9560. $this->x = $this->w - $this->rMargin;
  9561. } else {
  9562. $this->x = $this->lMargin;
  9563. }
  9564. $this->y = $this->tMargin;
  9565. if (isset($this->newpagegroup[$this->page])) {
  9566. // start a new group
  9567. $this->currpagegroup = $this->newpagegroup[$this->page];
  9568. $this->pagegroups[$this->currpagegroup] = 1;
  9569. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  9570. ++$this->pagegroups[$this->currpagegroup];
  9571. }
  9572. }
  9573. /**
  9574. * Mark end of page.
  9575. * @protected
  9576. */
  9577. protected function _endpage() {
  9578. $this->setVisibility('all');
  9579. $this->state = 1;
  9580. }
  9581. /**
  9582. * Begin a new object and return the object number.
  9583. * @return int object number
  9584. * @protected
  9585. */
  9586. protected function _newobj() {
  9587. $this->_out($this->_getobj());
  9588. return $this->n;
  9589. }
  9590. /**
  9591. * Return the starting object string for the selected object ID.
  9592. * @param $objid (int) Object ID (leave empty to get a new ID).
  9593. * @return string the starting object string
  9594. * @protected
  9595. * @since 5.8.009 (2010-08-20)
  9596. */
  9597. protected function _getobj($objid='') {
  9598. if ($objid === '') {
  9599. ++$this->n;
  9600. $objid = $this->n;
  9601. }
  9602. $this->offsets[$objid] = $this->bufferlen;
  9603. $this->pageobjects[$this->page][] = $objid;
  9604. return $objid.' 0 obj';
  9605. }
  9606. /**
  9607. * Underline text.
  9608. * @param $x (int) X coordinate
  9609. * @param $y (int) Y coordinate
  9610. * @param $txt (string) text to underline
  9611. * @protected
  9612. */
  9613. protected function _dounderline($x, $y, $txt) {
  9614. $w = $this->GetStringWidth($txt);
  9615. return $this->_dounderlinew($x, $y, $w);
  9616. }
  9617. /**
  9618. * Underline for rectangular text area.
  9619. * @param $x (int) X coordinate
  9620. * @param $y (int) Y coordinate
  9621. * @param $w (int) width to underline
  9622. * @protected
  9623. * @since 4.8.008 (2009-09-29)
  9624. */
  9625. protected function _dounderlinew($x, $y, $w) {
  9626. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9627. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  9628. }
  9629. /**
  9630. * Line through text.
  9631. * @param $x (int) X coordinate
  9632. * @param $y (int) Y coordinate
  9633. * @param $txt (string) text to linethrough
  9634. * @protected
  9635. */
  9636. protected function _dolinethrough($x, $y, $txt) {
  9637. $w = $this->GetStringWidth($txt);
  9638. return $this->_dolinethroughw($x, $y, $w);
  9639. }
  9640. /**
  9641. * Line through for rectangular text area.
  9642. * @param $x (int) X coordinate
  9643. * @param $y (int) Y coordinate
  9644. * @param $w (int) line length (width)
  9645. * @protected
  9646. * @since 4.9.008 (2009-09-29)
  9647. */
  9648. protected function _dolinethroughw($x, $y, $w) {
  9649. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9650. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  9651. }
  9652. /**
  9653. * Overline text.
  9654. * @param $x (int) X coordinate
  9655. * @param $y (int) Y coordinate
  9656. * @param $txt (string) text to overline
  9657. * @protected
  9658. * @since 4.9.015 (2010-04-19)
  9659. */
  9660. protected function _dooverline($x, $y, $txt) {
  9661. $w = $this->GetStringWidth($txt);
  9662. return $this->_dooverlinew($x, $y, $w);
  9663. }
  9664. /**
  9665. * Overline for rectangular text area.
  9666. * @param $x (int) X coordinate
  9667. * @param $y (int) Y coordinate
  9668. * @param $w (int) width to overline
  9669. * @protected
  9670. * @since 4.9.015 (2010-04-19)
  9671. */
  9672. protected function _dooverlinew($x, $y, $w) {
  9673. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9674. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  9675. }
  9676. /**
  9677. * Format a data string for meta information
  9678. * @param $s (string) data string to escape.
  9679. * @param $n (int) object ID
  9680. * @return string escaped string.
  9681. * @protected
  9682. */
  9683. protected function _datastring($s, $n=0) {
  9684. if ($n == 0) {
  9685. $n = $this->n;
  9686. }
  9687. $s = $this->_encrypt_data($n, $s);
  9688. return '('. TCPDF_STATIC::_escape($s).')';
  9689. }
  9690. /**
  9691. * Set the document creation timestamp
  9692. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  9693. * @public
  9694. * @since 5.9.152 (2012-03-23)
  9695. */
  9696. public function setDocCreationTimestamp($time) {
  9697. if (is_string($time)) {
  9698. $time = TCPDF_STATIC::getTimestamp($time);
  9699. }
  9700. $this->doc_creation_timestamp = intval($time);
  9701. }
  9702. /**
  9703. * Set the document modification timestamp
  9704. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  9705. * @public
  9706. * @since 5.9.152 (2012-03-23)
  9707. */
  9708. public function setDocModificationTimestamp($time) {
  9709. if (is_string($time)) {
  9710. $time = TCPDF_STATIC::getTimestamp($time);
  9711. }
  9712. $this->doc_modification_timestamp = intval($time);
  9713. }
  9714. /**
  9715. * Returns document creation timestamp in seconds.
  9716. * @return (int) Creation timestamp in seconds.
  9717. * @public
  9718. * @since 5.9.152 (2012-03-23)
  9719. */
  9720. public function getDocCreationTimestamp() {
  9721. return $this->doc_creation_timestamp;
  9722. }
  9723. /**
  9724. * Returns document modification timestamp in seconds.
  9725. * @return (int) Modfication timestamp in seconds.
  9726. * @public
  9727. * @since 5.9.152 (2012-03-23)
  9728. */
  9729. public function getDocModificationTimestamp() {
  9730. return $this->doc_modification_timestamp;
  9731. }
  9732. /**
  9733. * Returns a formatted date for meta information
  9734. * @param $n (int) Object ID.
  9735. * @param $timestamp (int) Timestamp to convert.
  9736. * @return string escaped date string.
  9737. * @protected
  9738. * @since 4.6.028 (2009-08-25)
  9739. */
  9740. protected function _datestring($n=0, $timestamp=0) {
  9741. if ((empty($timestamp)) OR ($timestamp < 0)) {
  9742. $timestamp = $this->doc_creation_timestamp;
  9743. }
  9744. return $this->_datastring('D:'.TCPDF_STATIC::getFormattedDate($timestamp), $n);
  9745. }
  9746. /**
  9747. * Format a text string for meta information
  9748. * @param $s (string) string to escape.
  9749. * @param $n (int) object ID
  9750. * @return string escaped string.
  9751. * @protected
  9752. */
  9753. protected function _textstring($s, $n=0) {
  9754. if ($this->isunicode) {
  9755. //Convert string to UTF-16BE
  9756. $s = TCPDF_FONTS::UTF8ToUTF16BE($s, true, $this->isunicode, $this->CurrentFont);
  9757. }
  9758. return $this->_datastring($s, $n);
  9759. }
  9760. /**
  9761. * THIS METHOD IS DEPRECATED
  9762. * Format a text string
  9763. * @param $s (string) string to escape.
  9764. * @return string escaped string.
  9765. * @protected
  9766. * @deprecated
  9767. */
  9768. protected function _escapetext($s) {
  9769. if ($this->isunicode) {
  9770. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  9771. $s = TCPDF_FONTS::UTF8ToLatin1($s, $this->isunicode, $this->CurrentFont);
  9772. } else {
  9773. //Convert string to UTF-16BE and reverse RTL language
  9774. $s = TCPDF_FONTS::utf8StrRev($s, false, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  9775. }
  9776. }
  9777. return TCPDF_STATIC::_escape($s);
  9778. }
  9779. /**
  9780. * get raw output stream.
  9781. * @param $s (string) string to output.
  9782. * @param $n (int) object reference for encryption mode
  9783. * @protected
  9784. * @author Nicola Asuni
  9785. * @since 5.5.000 (2010-06-22)
  9786. */
  9787. protected function _getrawstream($s, $n=0) {
  9788. if ($n <= 0) {
  9789. // default to current object
  9790. $n = $this->n;
  9791. }
  9792. return $this->_encrypt_data($n, $s);
  9793. }
  9794. /**
  9795. * Format output stream (DEPRECATED).
  9796. * @param $s (string) string to output.
  9797. * @param $n (int) object reference for encryption mode
  9798. * @protected
  9799. * @deprecated
  9800. */
  9801. protected function _getstream($s, $n=0) {
  9802. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  9803. }
  9804. /**
  9805. * Output a stream (DEPRECATED).
  9806. * @param $s (string) string to output.
  9807. * @param $n (int) object reference for encryption mode
  9808. * @protected
  9809. * @deprecated
  9810. */
  9811. protected function _putstream($s, $n=0) {
  9812. $this->_out($this->_getstream($s, $n));
  9813. }
  9814. /**
  9815. * Output a string to the document.
  9816. * @param $s (string) string to output.
  9817. * @protected
  9818. */
  9819. protected function _out($s) {
  9820. if ($this->state == 2) {
  9821. if ($this->inxobj) {
  9822. // we are inside an XObject template
  9823. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  9824. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  9825. // puts data before page footer
  9826. $pagebuff = $this->getPageBuffer($this->page);
  9827. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  9828. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  9829. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  9830. // update footer position
  9831. $this->footerpos[$this->page] += strlen($s."\n");
  9832. } else {
  9833. // set page data
  9834. $this->setPageBuffer($this->page, $s."\n", true);
  9835. }
  9836. } elseif ($this->state > 0) {
  9837. // set general data
  9838. $this->setBuffer($s."\n");
  9839. }
  9840. }
  9841. /**
  9842. * Set header font.
  9843. * @param $font (array) font
  9844. * @public
  9845. * @since 1.1
  9846. */
  9847. public function setHeaderFont($font) {
  9848. $this->header_font = $font;
  9849. }
  9850. /**
  9851. * Get header font.
  9852. * @return array()
  9853. * @public
  9854. * @since 4.0.012 (2008-07-24)
  9855. */
  9856. public function getHeaderFont() {
  9857. return $this->header_font;
  9858. }
  9859. /**
  9860. * Set footer font.
  9861. * @param $font (array) font
  9862. * @public
  9863. * @since 1.1
  9864. */
  9865. public function setFooterFont($font) {
  9866. $this->footer_font = $font;
  9867. }
  9868. /**
  9869. * Get Footer font.
  9870. * @return array()
  9871. * @public
  9872. * @since 4.0.012 (2008-07-24)
  9873. */
  9874. public function getFooterFont() {
  9875. return $this->footer_font;
  9876. }
  9877. /**
  9878. * Set language array.
  9879. * @param $language (array)
  9880. * @public
  9881. * @since 1.1
  9882. */
  9883. public function setLanguageArray($language) {
  9884. $this->l = $language;
  9885. if (isset($this->l['a_meta_dir'])) {
  9886. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  9887. } else {
  9888. $this->rtl = false;
  9889. }
  9890. }
  9891. /**
  9892. * Returns the PDF data.
  9893. * @public
  9894. */
  9895. public function getPDFData() {
  9896. if ($this->state < 3) {
  9897. $this->Close();
  9898. }
  9899. return $this->buffer;
  9900. }
  9901. /**
  9902. * Output anchor link.
  9903. * @param $url (string) link URL or internal link (i.e.: &lt;a href="#23,4.5"&gt;link to page 23 at 4.5 Y position&lt;/a&gt;)
  9904. * @param $name (string) link name
  9905. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  9906. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  9907. * @param $color (array) array of RGB text color
  9908. * @param $style (string) font style (U, D, B, I)
  9909. * @param $firstblock (boolean) if true the string is the starting of a line.
  9910. * @return the number of cells used or the remaining text if $firstline = true;
  9911. * @public
  9912. */
  9913. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  9914. if (isset($url[1]) AND ($url[0] == '#') AND is_numeric($url[1])) {
  9915. // convert url to internal link
  9916. $lnkdata = explode(',', $url);
  9917. if (isset($lnkdata[0])) {
  9918. $page = intval(substr($lnkdata[0], 1));
  9919. if (empty($page) OR ($page <= 0)) {
  9920. $page = $this->page;
  9921. }
  9922. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  9923. $lnky = floatval($lnkdata[1]);
  9924. } else {
  9925. $lnky = 0;
  9926. }
  9927. $url = $this->AddLink();
  9928. $this->SetLink($url, $lnky, $page);
  9929. }
  9930. }
  9931. // store current settings
  9932. $prevcolor = $this->fgcolor;
  9933. $prevstyle = $this->FontStyle;
  9934. if (empty($color)) {
  9935. $this->SetTextColorArray($this->htmlLinkColorArray);
  9936. } else {
  9937. $this->SetTextColorArray($color);
  9938. }
  9939. if ($style == -1) {
  9940. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  9941. } else {
  9942. $this->SetFont('', $this->FontStyle.$style);
  9943. }
  9944. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  9945. // restore settings
  9946. $this->SetFont('', $prevstyle);
  9947. $this->SetTextColorArray($prevcolor);
  9948. return $ret;
  9949. }
  9950. /**
  9951. * Converts pixels to User's Units.
  9952. * @param $px (int) pixels
  9953. * @return float value in user's unit
  9954. * @public
  9955. * @see setImageScale(), getImageScale()
  9956. */
  9957. public function pixelsToUnits($px) {
  9958. return ($px / ($this->imgscale * $this->k));
  9959. }
  9960. /**
  9961. * Reverse function for htmlentities.
  9962. * Convert entities in UTF-8.
  9963. * @param $text_to_convert (string) Text to convert.
  9964. * @return string converted text string
  9965. * @public
  9966. */
  9967. public function unhtmlentities($text_to_convert) {
  9968. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  9969. }
  9970. // ENCRYPTION METHODS ----------------------------------
  9971. /**
  9972. * Compute encryption key depending on object number where the encrypted data is stored.
  9973. * This is used for all strings and streams without crypt filter specifier.
  9974. * @param $n (int) object number
  9975. * @return int object key
  9976. * @protected
  9977. * @author Nicola Asuni
  9978. * @since 2.0.000 (2008-01-02)
  9979. */
  9980. protected function _objectkey($n) {
  9981. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  9982. if ($this->encryptdata['mode'] == 2) { // AES-128
  9983. // AES padding
  9984. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  9985. }
  9986. $objkey = substr(TCPDF_STATIC::_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  9987. $objkey = substr($objkey, 0, 16);
  9988. return $objkey;
  9989. }
  9990. /**
  9991. * Encrypt the input string.
  9992. * @param $n (int) object number
  9993. * @param $s (string) data string to encrypt
  9994. * @return encrypted string
  9995. * @protected
  9996. * @author Nicola Asuni
  9997. * @since 5.0.005 (2010-05-11)
  9998. */
  9999. protected function _encrypt_data($n, $s) {
  10000. if (!$this->encrypted) {
  10001. return $s;
  10002. }
  10003. switch ($this->encryptdata['mode']) {
  10004. case 0: // RC4-40
  10005. case 1: { // RC4-128
  10006. $s = TCPDF_STATIC::_RC4($this->_objectkey($n), $s, $this->last_enc_key, $this->last_enc_key_c);
  10007. break;
  10008. }
  10009. case 2: { // AES-128
  10010. $s = TCPDF_STATIC::_AES($this->_objectkey($n), $s);
  10011. break;
  10012. }
  10013. case 3: { // AES-256
  10014. $s = TCPDF_STATIC::_AES($this->encryptdata['key'], $s);
  10015. break;
  10016. }
  10017. }
  10018. return $s;
  10019. }
  10020. /**
  10021. * Put encryption on PDF document.
  10022. * @protected
  10023. * @author Nicola Asuni
  10024. * @since 2.0.000 (2008-01-02)
  10025. */
  10026. protected function _putencryption() {
  10027. if (!$this->encrypted) {
  10028. return;
  10029. }
  10030. $this->encryptdata['objid'] = $this->_newobj();
  10031. $out = '<<';
  10032. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  10033. $this->encryptdata['Filter'] = 'Standard';
  10034. }
  10035. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  10036. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  10037. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  10038. }
  10039. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  10040. $this->encryptdata['V'] = 1;
  10041. }
  10042. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  10043. $out .= ' /V '.$this->encryptdata['V'];
  10044. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  10045. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  10046. $out .= ' /Length '.$this->encryptdata['Length'];
  10047. } else {
  10048. $out .= ' /Length 40';
  10049. }
  10050. if ($this->encryptdata['V'] >= 4) {
  10051. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  10052. $this->encryptdata['StmF'] = 'Identity';
  10053. }
  10054. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  10055. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10056. $this->encryptdata['StrF'] = 'Identity';
  10057. }
  10058. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  10059. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  10060. $out .= ' /CF <<';
  10061. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  10062. $out .= ' /Type /CryptFilter';
  10063. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  10064. // The method used
  10065. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  10066. if ($this->encryptdata['pubkey']) {
  10067. $out .= ' /Recipients [';
  10068. foreach ($this->encryptdata['Recipients'] as $rec) {
  10069. $out .= ' <'.$rec.'>';
  10070. }
  10071. $out .= ' ]';
  10072. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  10073. $out .= ' /EncryptMetadata false';
  10074. } else {
  10075. $out .= ' /EncryptMetadata true';
  10076. }
  10077. }
  10078. } else {
  10079. $out .= ' /CFM /None';
  10080. }
  10081. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  10082. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  10083. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  10084. } else {
  10085. $out .= ' /AuthEvent /DocOpen';
  10086. }
  10087. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  10088. // The bit length of the encryption key.
  10089. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  10090. }
  10091. $out .= ' >> >>';
  10092. }
  10093. // The name of the crypt filter that shall be used by default when decrypting streams.
  10094. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  10095. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10096. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  10097. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  10098. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  10099. $out .= ' /EFF /'.$this->encryptdata[''];
  10100. }
  10101. }
  10102. // Additional encryption dictionary entries for the standard security handler
  10103. if ($this->encryptdata['pubkey']) {
  10104. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  10105. $out .= ' /Recipients [';
  10106. foreach ($this->encryptdata['Recipients'] as $rec) {
  10107. $out .= ' <'.$rec.'>';
  10108. }
  10109. $out .= ' ]';
  10110. }
  10111. } else {
  10112. $out .= ' /R';
  10113. if ($this->encryptdata['V'] == 5) { // AES-256
  10114. $out .= ' 5';
  10115. $out .= ' /OE ('.TCPDF_STATIC::_escape($this->encryptdata['OE']).')';
  10116. $out .= ' /UE ('.TCPDF_STATIC::_escape($this->encryptdata['UE']).')';
  10117. $out .= ' /Perms ('.TCPDF_STATIC::_escape($this->encryptdata['perms']).')';
  10118. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  10119. $out .= ' 4';
  10120. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  10121. $out .= ' 2';
  10122. } else { // RC-128
  10123. $out .= ' 3';
  10124. }
  10125. $out .= ' /O ('.TCPDF_STATIC::_escape($this->encryptdata['O']).')';
  10126. $out .= ' /U ('.TCPDF_STATIC::_escape($this->encryptdata['U']).')';
  10127. $out .= ' /P '.$this->encryptdata['P'];
  10128. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  10129. $out .= ' /EncryptMetadata false';
  10130. } else {
  10131. $out .= ' /EncryptMetadata true';
  10132. }
  10133. }
  10134. $out .= ' >>';
  10135. $out .= "\n".'endobj';
  10136. $this->_out($out);
  10137. }
  10138. /**
  10139. * Compute U value (used for encryption)
  10140. * @return string U value
  10141. * @protected
  10142. * @since 2.0.000 (2008-01-02)
  10143. * @author Nicola Asuni
  10144. */
  10145. protected function _Uvalue() {
  10146. if ($this->encryptdata['mode'] == 0) { // RC4-40
  10147. return TCPDF_STATIC::_RC4($this->encryptdata['key'], TCPDF_STATIC::$enc_padding, $this->last_enc_key, $this->last_enc_key_c);
  10148. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  10149. $tmp = TCPDF_STATIC::_md5_16(TCPDF_STATIC::$enc_padding.$this->encryptdata['fileid']);
  10150. $enc = TCPDF_STATIC::_RC4($this->encryptdata['key'], $tmp, $this->last_enc_key, $this->last_enc_key_c);
  10151. $len = strlen($tmp);
  10152. for ($i = 1; $i <= 19; ++$i) {
  10153. $ek = '';
  10154. for ($j = 0; $j < $len; ++$j) {
  10155. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  10156. }
  10157. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10158. }
  10159. $enc .= str_repeat("\x00", 16);
  10160. return substr($enc, 0, 32);
  10161. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10162. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10163. // User Validation Salt
  10164. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  10165. // User Key Salt
  10166. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  10167. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  10168. }
  10169. }
  10170. /**
  10171. * Compute UE value (used for encryption)
  10172. * @return string UE value
  10173. * @protected
  10174. * @since 5.9.006 (2010-10-19)
  10175. * @author Nicola Asuni
  10176. */
  10177. protected function _UEvalue() {
  10178. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  10179. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  10180. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  10181. }
  10182. /**
  10183. * Compute O value (used for encryption)
  10184. * @return string O value
  10185. * @protected
  10186. * @since 2.0.000 (2008-01-02)
  10187. * @author Nicola Asuni
  10188. */
  10189. protected function _Ovalue() {
  10190. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  10191. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['owner_password']);
  10192. if ($this->encryptdata['mode'] > 0) {
  10193. for ($i = 0; $i < 50; ++$i) {
  10194. $tmp = TCPDF_STATIC::_md5_16($tmp);
  10195. }
  10196. }
  10197. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  10198. $enc = TCPDF_STATIC::_RC4($owner_key, $this->encryptdata['user_password'], $this->last_enc_key, $this->last_enc_key_c);
  10199. if ($this->encryptdata['mode'] > 0) {
  10200. $len = strlen($owner_key);
  10201. for ($i = 1; $i <= 19; ++$i) {
  10202. $ek = '';
  10203. for ($j = 0; $j < $len; ++$j) {
  10204. $ek .= chr(ord($owner_key[$j]) ^ $i);
  10205. }
  10206. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10207. }
  10208. }
  10209. return $enc;
  10210. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10211. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10212. // Owner Validation Salt
  10213. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  10214. // Owner Key Salt
  10215. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  10216. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  10217. }
  10218. }
  10219. /**
  10220. * Compute OE value (used for encryption)
  10221. * @return string OE value
  10222. * @protected
  10223. * @since 5.9.006 (2010-10-19)
  10224. * @author Nicola Asuni
  10225. */
  10226. protected function _OEvalue() {
  10227. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  10228. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  10229. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  10230. }
  10231. /**
  10232. * Convert password for AES-256 encryption mode
  10233. * @param $password (string) password
  10234. * @return string password
  10235. * @protected
  10236. * @since 5.9.006 (2010-10-19)
  10237. * @author Nicola Asuni
  10238. */
  10239. protected function _fixAES256Password($password) {
  10240. $psw = ''; // password to be returned
  10241. $psw_array = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($password, $this->isunicode, $this->CurrentFont), $password, $this->rtl, $this->isunicode, $this->CurrentFont);
  10242. foreach ($psw_array as $c) {
  10243. $psw .= TCPDF_FONTS::unichr($c, $this->isunicode);
  10244. }
  10245. return substr($psw, 0, 127);
  10246. }
  10247. /**
  10248. * Compute encryption key
  10249. * @protected
  10250. * @since 2.0.000 (2008-01-02)
  10251. * @author Nicola Asuni
  10252. */
  10253. protected function _generateencryptionkey() {
  10254. $keybytelen = ($this->encryptdata['Length'] / 8);
  10255. if (!$this->encryptdata['pubkey']) { // standard mode
  10256. if ($this->encryptdata['mode'] == 3) { // AES-256
  10257. // generate 256 bit random key
  10258. $this->encryptdata['key'] = substr(hash('sha256', TCPDF_STATIC::getRandomSeed(), true), 0, $keybytelen);
  10259. // truncate passwords
  10260. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  10261. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  10262. // Compute U value
  10263. $this->encryptdata['U'] = $this->_Uvalue();
  10264. // Compute UE value
  10265. $this->encryptdata['UE'] = $this->_UEvalue();
  10266. // Compute O value
  10267. $this->encryptdata['O'] = $this->_Ovalue();
  10268. // Compute OE value
  10269. $this->encryptdata['OE'] = $this->_OEvalue();
  10270. // Compute P value
  10271. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10272. // Computing the encryption dictionary's Perms (permissions) value
  10273. $perms = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  10274. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  10275. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  10276. $perms .= 'F';
  10277. } else {
  10278. $perms .= 'T';
  10279. }
  10280. $perms .= 'adb'; // bytes 9-11
  10281. $perms .= 'nick'; // bytes 12-15
  10282. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  10283. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  10284. } else { // RC4-40, RC4-128, AES-128
  10285. // Pad passwords
  10286. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10287. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10288. // Compute O value
  10289. $this->encryptdata['O'] = $this->_Ovalue();
  10290. // get default permissions (reverse byte order)
  10291. $permissions = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']);
  10292. // Compute encryption key
  10293. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  10294. if ($this->encryptdata['mode'] > 0) {
  10295. for ($i = 0; $i < 50; ++$i) {
  10296. $tmp = TCPDF_STATIC::_md5_16(substr($tmp, 0, $keybytelen));
  10297. }
  10298. }
  10299. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  10300. // Compute U value
  10301. $this->encryptdata['U'] = $this->_Uvalue();
  10302. // Compute P value
  10303. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10304. }
  10305. } else { // Public-Key mode
  10306. // random 20-byte seed
  10307. $seed = sha1(TCPDF_STATIC::getRandomSeed(), true);
  10308. $recipient_bytes = '';
  10309. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  10310. // for each public certificate
  10311. if (isset($pubkey['p'])) {
  10312. $pkprotection = TCPDF_STATIC::getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  10313. } else {
  10314. $pkprotection = $this->encryptdata['protection'];
  10315. }
  10316. // get default permissions (reverse byte order)
  10317. $pkpermissions = TCPDF_STATIC::getEncPermissionsString($pkprotection);
  10318. // envelope data
  10319. $envelope = $seed.$pkpermissions;
  10320. // write the envelope data to a temporary file
  10321. $tempkeyfile = TCPDF_STATIC::getObjFilename('key');
  10322. $f = fopen($tempkeyfile, 'wb');
  10323. if (!$f) {
  10324. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  10325. }
  10326. $envelope_length = strlen($envelope);
  10327. fwrite($f, $envelope, $envelope_length);
  10328. fclose($f);
  10329. $tempencfile = TCPDF_STATIC::getObjFilename('enc');
  10330. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  10331. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  10332. }
  10333. unlink($tempkeyfile);
  10334. // read encryption signature
  10335. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  10336. unlink($tempencfile);
  10337. // extract signature
  10338. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  10339. $tmparr = explode("\n\n", $signature);
  10340. $signature = trim($tmparr[1]);
  10341. unset($tmparr);
  10342. // decode signature
  10343. $signature = base64_decode($signature);
  10344. // convert signature to hex
  10345. $hexsignature = current(unpack('H*', $signature));
  10346. // store signature on recipients array
  10347. $this->encryptdata['Recipients'][] = $hexsignature;
  10348. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  10349. $recipient_bytes .= $signature;
  10350. }
  10351. // calculate encryption key
  10352. if ($this->encryptdata['mode'] == 3) { // AES-256
  10353. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  10354. } else { // RC4-40, RC4-128, AES-128
  10355. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  10356. }
  10357. }
  10358. }
  10359. /**
  10360. * Set document protection
  10361. * Remark: the protection against modification is for people who have the full Acrobat product.
  10362. * 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.
  10363. * 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.
  10364. * @param $permissions (Array) the set of permissions (specify the ones you want to block):<ul><li>print : Print the document;</li><li>modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';</li><li>copy : Copy or otherwise extract text and graphics from the document;</li><li>annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);</li><li>fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;</li><li>extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);</li><li>assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;</li><li>print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.</li><li>owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.</li></ul>
  10365. * @param $user_pass (String) user password. Empty by default.
  10366. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  10367. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  10368. * @param $pubkeys (String) array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../examples/data/cert/tcpdf.crt', 'p' => array('print')))
  10369. * @public
  10370. * @since 2.0.000 (2008-01-02)
  10371. * @author Nicola Asuni
  10372. */
  10373. public function SetProtection($permissions=array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass='', $owner_pass=null, $mode=0, $pubkeys=null) {
  10374. if ($this->pdfa_mode) {
  10375. // encryption is not allowed in PDF/A mode
  10376. return;
  10377. }
  10378. $this->encryptdata['protection'] = TCPDF_STATIC::getUserPermissionCode($permissions, $mode);
  10379. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  10380. // public-key mode
  10381. $this->encryptdata['pubkeys'] = $pubkeys;
  10382. if ($mode == 0) {
  10383. // public-Key Security requires at least 128 bit
  10384. $mode = 1;
  10385. }
  10386. if (!function_exists('openssl_pkcs7_encrypt')) {
  10387. $this->Error('Public-Key Security requires openssl library.');
  10388. }
  10389. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  10390. $this->encryptdata['pubkey'] = true;
  10391. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  10392. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  10393. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  10394. } else {
  10395. // standard mode (password mode)
  10396. $this->encryptdata['pubkey'] = false;
  10397. $this->encryptdata['Filter'] = 'Standard';
  10398. $this->encryptdata['StmF'] = 'StdCF';
  10399. $this->encryptdata['StrF'] = 'StdCF';
  10400. }
  10401. if ($mode > 1) { // AES
  10402. if (!extension_loaded('mcrypt')) {
  10403. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  10404. }
  10405. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  10406. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  10407. }
  10408. if (($mode == 3) AND !function_exists('hash')) {
  10409. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  10410. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  10411. }
  10412. }
  10413. if ($owner_pass === null) {
  10414. $owner_pass = md5(TCPDF_STATIC::getRandomSeed());
  10415. }
  10416. $this->encryptdata['user_password'] = $user_pass;
  10417. $this->encryptdata['owner_password'] = $owner_pass;
  10418. $this->encryptdata['mode'] = $mode;
  10419. switch ($mode) {
  10420. case 0: { // RC4 40 bit
  10421. $this->encryptdata['V'] = 1;
  10422. $this->encryptdata['Length'] = 40;
  10423. $this->encryptdata['CF']['CFM'] = 'V2';
  10424. break;
  10425. }
  10426. case 1: { // RC4 128 bit
  10427. $this->encryptdata['V'] = 2;
  10428. $this->encryptdata['Length'] = 128;
  10429. $this->encryptdata['CF']['CFM'] = 'V2';
  10430. if ($this->encryptdata['pubkey']) {
  10431. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  10432. $this->encryptdata['Recipients'] = array();
  10433. }
  10434. break;
  10435. }
  10436. case 2: { // AES 128 bit
  10437. $this->encryptdata['V'] = 4;
  10438. $this->encryptdata['Length'] = 128;
  10439. $this->encryptdata['CF']['CFM'] = 'AESV2';
  10440. $this->encryptdata['CF']['Length'] = 128;
  10441. if ($this->encryptdata['pubkey']) {
  10442. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10443. $this->encryptdata['Recipients'] = array();
  10444. }
  10445. break;
  10446. }
  10447. case 3: { // AES 256 bit
  10448. $this->encryptdata['V'] = 5;
  10449. $this->encryptdata['Length'] = 256;
  10450. $this->encryptdata['CF']['CFM'] = 'AESV3';
  10451. $this->encryptdata['CF']['Length'] = 256;
  10452. if ($this->encryptdata['pubkey']) {
  10453. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10454. $this->encryptdata['Recipients'] = array();
  10455. }
  10456. break;
  10457. }
  10458. }
  10459. $this->encrypted = true;
  10460. $this->encryptdata['fileid'] = TCPDF_STATIC::convertHexStringToString($this->file_id);
  10461. $this->_generateencryptionkey();
  10462. }
  10463. // END OF ENCRYPTION FUNCTIONS -------------------------
  10464. // START TRANSFORMATIONS SECTION -----------------------
  10465. /**
  10466. * Starts a 2D tranformation saving current graphic state.
  10467. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  10468. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10469. * @public
  10470. * @since 2.1.000 (2008-01-07)
  10471. * @see StartTransform(), StopTransform()
  10472. */
  10473. public function StartTransform() {
  10474. if ($this->state != 2) {
  10475. return;
  10476. }
  10477. $this->_out('q');
  10478. if ($this->inxobj) {
  10479. // we are inside an XObject template
  10480. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10481. } else {
  10482. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  10483. }
  10484. ++$this->transfmatrix_key;
  10485. $this->transfmatrix[$this->transfmatrix_key] = array();
  10486. }
  10487. /**
  10488. * Stops a 2D tranformation restoring previous graphic state.
  10489. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  10490. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10491. * @public
  10492. * @since 2.1.000 (2008-01-07)
  10493. * @see StartTransform(), StopTransform()
  10494. */
  10495. public function StopTransform() {
  10496. if ($this->state != 2) {
  10497. return;
  10498. }
  10499. $this->_out('Q');
  10500. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  10501. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  10502. --$this->transfmatrix_key;
  10503. }
  10504. if ($this->inxobj) {
  10505. // we are inside an XObject template
  10506. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  10507. } else {
  10508. array_pop($this->transfmrk[$this->page]);
  10509. }
  10510. }
  10511. /**
  10512. * Horizontal Scaling.
  10513. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10514. * @param $x (int) abscissa of the scaling center. Default is current x position
  10515. * @param $y (int) ordinate of the scaling center. Default is current y position
  10516. * @public
  10517. * @since 2.1.000 (2008-01-07)
  10518. * @see StartTransform(), StopTransform()
  10519. */
  10520. public function ScaleX($s_x, $x='', $y='') {
  10521. $this->Scale($s_x, 100, $x, $y);
  10522. }
  10523. /**
  10524. * Vertical Scaling.
  10525. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  10526. * @param $x (int) abscissa of the scaling center. Default is current x position
  10527. * @param $y (int) ordinate of the scaling center. Default is current y position
  10528. * @public
  10529. * @since 2.1.000 (2008-01-07)
  10530. * @see StartTransform(), StopTransform()
  10531. */
  10532. public function ScaleY($s_y, $x='', $y='') {
  10533. $this->Scale(100, $s_y, $x, $y);
  10534. }
  10535. /**
  10536. * Vertical and horizontal proportional Scaling.
  10537. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  10538. * @param $x (int) abscissa of the scaling center. Default is current x position
  10539. * @param $y (int) ordinate of the scaling center. Default is current y position
  10540. * @public
  10541. * @since 2.1.000 (2008-01-07)
  10542. * @see StartTransform(), StopTransform()
  10543. */
  10544. public function ScaleXY($s, $x='', $y='') {
  10545. $this->Scale($s, $s, $x, $y);
  10546. }
  10547. /**
  10548. * Vertical and horizontal non-proportional Scaling.
  10549. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10550. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  10551. * @param $x (int) abscissa of the scaling center. Default is current x position
  10552. * @param $y (int) ordinate of the scaling center. Default is current y position
  10553. * @public
  10554. * @since 2.1.000 (2008-01-07)
  10555. * @see StartTransform(), StopTransform()
  10556. */
  10557. public function Scale($s_x, $s_y, $x='', $y='') {
  10558. if ($x === '') {
  10559. $x = $this->x;
  10560. }
  10561. if ($y === '') {
  10562. $y = $this->y;
  10563. }
  10564. if (($s_x == 0) OR ($s_y == 0)) {
  10565. $this->Error('Please do not use values equal to zero for scaling');
  10566. }
  10567. $y = ($this->h - $y) * $this->k;
  10568. $x *= $this->k;
  10569. //calculate elements of transformation matrix
  10570. $s_x /= 100;
  10571. $s_y /= 100;
  10572. $tm = array();
  10573. $tm[0] = $s_x;
  10574. $tm[1] = 0;
  10575. $tm[2] = 0;
  10576. $tm[3] = $s_y;
  10577. $tm[4] = $x * (1 - $s_x);
  10578. $tm[5] = $y * (1 - $s_y);
  10579. //scale the coordinate system
  10580. $this->Transform($tm);
  10581. }
  10582. /**
  10583. * Horizontal Mirroring.
  10584. * @param $x (int) abscissa of the point. Default is current x position
  10585. * @public
  10586. * @since 2.1.000 (2008-01-07)
  10587. * @see StartTransform(), StopTransform()
  10588. */
  10589. public function MirrorH($x='') {
  10590. $this->Scale(-100, 100, $x);
  10591. }
  10592. /**
  10593. * Verical Mirroring.
  10594. * @param $y (int) ordinate of the point. Default is current y position
  10595. * @public
  10596. * @since 2.1.000 (2008-01-07)
  10597. * @see StartTransform(), StopTransform()
  10598. */
  10599. public function MirrorV($y='') {
  10600. $this->Scale(100, -100, '', $y);
  10601. }
  10602. /**
  10603. * Point reflection mirroring.
  10604. * @param $x (int) abscissa of the point. Default is current x position
  10605. * @param $y (int) ordinate of the point. Default is current y position
  10606. * @public
  10607. * @since 2.1.000 (2008-01-07)
  10608. * @see StartTransform(), StopTransform()
  10609. */
  10610. public function MirrorP($x='',$y='') {
  10611. $this->Scale(-100, -100, $x, $y);
  10612. }
  10613. /**
  10614. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  10615. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  10616. * @param $x (int) abscissa of the point. Default is current x position
  10617. * @param $y (int) ordinate of the point. Default is current y position
  10618. * @public
  10619. * @since 2.1.000 (2008-01-07)
  10620. * @see StartTransform(), StopTransform()
  10621. */
  10622. public function MirrorL($angle=0, $x='',$y='') {
  10623. $this->Scale(-100, 100, $x, $y);
  10624. $this->Rotate(-2*($angle-90), $x, $y);
  10625. }
  10626. /**
  10627. * Translate graphic object horizontally.
  10628. * @param $t_x (int) movement to the right (or left for RTL)
  10629. * @public
  10630. * @since 2.1.000 (2008-01-07)
  10631. * @see StartTransform(), StopTransform()
  10632. */
  10633. public function TranslateX($t_x) {
  10634. $this->Translate($t_x, 0);
  10635. }
  10636. /**
  10637. * Translate graphic object vertically.
  10638. * @param $t_y (int) movement to the bottom
  10639. * @public
  10640. * @since 2.1.000 (2008-01-07)
  10641. * @see StartTransform(), StopTransform()
  10642. */
  10643. public function TranslateY($t_y) {
  10644. $this->Translate(0, $t_y);
  10645. }
  10646. /**
  10647. * Translate graphic object horizontally and vertically.
  10648. * @param $t_x (int) movement to the right
  10649. * @param $t_y (int) movement to the bottom
  10650. * @public
  10651. * @since 2.1.000 (2008-01-07)
  10652. * @see StartTransform(), StopTransform()
  10653. */
  10654. public function Translate($t_x, $t_y) {
  10655. //calculate elements of transformation matrix
  10656. $tm = array();
  10657. $tm[0] = 1;
  10658. $tm[1] = 0;
  10659. $tm[2] = 0;
  10660. $tm[3] = 1;
  10661. $tm[4] = $t_x * $this->k;
  10662. $tm[5] = -$t_y * $this->k;
  10663. //translate the coordinate system
  10664. $this->Transform($tm);
  10665. }
  10666. /**
  10667. * Rotate object.
  10668. * @param $angle (float) angle in degrees for counter-clockwise rotation
  10669. * @param $x (int) abscissa of the rotation center. Default is current x position
  10670. * @param $y (int) ordinate of the rotation center. Default is current y position
  10671. * @public
  10672. * @since 2.1.000 (2008-01-07)
  10673. * @see StartTransform(), StopTransform()
  10674. */
  10675. public function Rotate($angle, $x='', $y='') {
  10676. if ($x === '') {
  10677. $x = $this->x;
  10678. }
  10679. if ($y === '') {
  10680. $y = $this->y;
  10681. }
  10682. $y = ($this->h - $y) * $this->k;
  10683. $x *= $this->k;
  10684. //calculate elements of transformation matrix
  10685. $tm = array();
  10686. $tm[0] = cos(deg2rad($angle));
  10687. $tm[1] = sin(deg2rad($angle));
  10688. $tm[2] = -$tm[1];
  10689. $tm[3] = $tm[0];
  10690. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  10691. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  10692. //rotate the coordinate system around ($x,$y)
  10693. $this->Transform($tm);
  10694. }
  10695. /**
  10696. * Skew horizontally.
  10697. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10698. * @param $x (int) abscissa of the skewing center. default is current x position
  10699. * @param $y (int) ordinate of the skewing center. default is current y position
  10700. * @public
  10701. * @since 2.1.000 (2008-01-07)
  10702. * @see StartTransform(), StopTransform()
  10703. */
  10704. public function SkewX($angle_x, $x='', $y='') {
  10705. $this->Skew($angle_x, 0, $x, $y);
  10706. }
  10707. /**
  10708. * Skew vertically.
  10709. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10710. * @param $x (int) abscissa of the skewing center. default is current x position
  10711. * @param $y (int) ordinate of the skewing center. default is current y position
  10712. * @public
  10713. * @since 2.1.000 (2008-01-07)
  10714. * @see StartTransform(), StopTransform()
  10715. */
  10716. public function SkewY($angle_y, $x='', $y='') {
  10717. $this->Skew(0, $angle_y, $x, $y);
  10718. }
  10719. /**
  10720. * Skew.
  10721. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10722. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10723. * @param $x (int) abscissa of the skewing center. default is current x position
  10724. * @param $y (int) ordinate of the skewing center. default is current y position
  10725. * @public
  10726. * @since 2.1.000 (2008-01-07)
  10727. * @see StartTransform(), StopTransform()
  10728. */
  10729. public function Skew($angle_x, $angle_y, $x='', $y='') {
  10730. if ($x === '') {
  10731. $x = $this->x;
  10732. }
  10733. if ($y === '') {
  10734. $y = $this->y;
  10735. }
  10736. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  10737. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  10738. }
  10739. $x *= $this->k;
  10740. $y = ($this->h - $y) * $this->k;
  10741. //calculate elements of transformation matrix
  10742. $tm = array();
  10743. $tm[0] = 1;
  10744. $tm[1] = tan(deg2rad($angle_y));
  10745. $tm[2] = tan(deg2rad($angle_x));
  10746. $tm[3] = 1;
  10747. $tm[4] = -$tm[2] * $y;
  10748. $tm[5] = -$tm[1] * $x;
  10749. //skew the coordinate system
  10750. $this->Transform($tm);
  10751. }
  10752. /**
  10753. * Apply graphic transformations.
  10754. * @param $tm (array) transformation matrix
  10755. * @protected
  10756. * @since 2.1.000 (2008-01-07)
  10757. * @see StartTransform(), StopTransform()
  10758. */
  10759. protected function Transform($tm) {
  10760. if ($this->state != 2) {
  10761. return;
  10762. }
  10763. $this->_out(sprintf('%F %F %F %F %F %F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  10764. // add tranformation matrix
  10765. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  10766. // update transformation mark
  10767. if ($this->inxobj) {
  10768. // we are inside an XObject template
  10769. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  10770. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  10771. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10772. }
  10773. } elseif (end($this->transfmrk[$this->page]) !== false) {
  10774. $key = key($this->transfmrk[$this->page]);
  10775. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  10776. }
  10777. }
  10778. // END TRANSFORMATIONS SECTION -------------------------
  10779. // START GRAPHIC FUNCTIONS SECTION ---------------------
  10780. // The following section is based on the code provided by David Hernandez Sanz
  10781. /**
  10782. * 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.
  10783. * @param $width (float) The width.
  10784. * @public
  10785. * @since 1.0
  10786. * @see Line(), Rect(), Cell(), MultiCell()
  10787. */
  10788. public function SetLineWidth($width) {
  10789. //Set line width
  10790. $this->LineWidth = $width;
  10791. $this->linestyleWidth = sprintf('%F w', ($width * $this->k));
  10792. if ($this->state == 2) {
  10793. $this->_out($this->linestyleWidth);
  10794. }
  10795. }
  10796. /**
  10797. * Returns the current the line width.
  10798. * @return int Line width
  10799. * @public
  10800. * @since 2.1.000 (2008-01-07)
  10801. * @see Line(), SetLineWidth()
  10802. */
  10803. public function GetLineWidth() {
  10804. return $this->LineWidth;
  10805. }
  10806. /**
  10807. * Set line style.
  10808. * @param $style (array) Line style. Array with keys among the following:
  10809. * <ul>
  10810. * <li>width (float): Width of the line in user units.</li>
  10811. * <li>cap (string): Type of cap to put on the line. Possible values are:
  10812. * butt, round, square. The difference between "square" and "butt" is that
  10813. * "square" projects a flat end past the end of the line.</li>
  10814. * <li>join (string): Type of join. Possible values are: miter, round,
  10815. * bevel.</li>
  10816. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  10817. * series of length values, which are the lengths of the on and off dashes.
  10818. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  10819. * 1 off, 2 on, 1 off, ...</li>
  10820. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  10821. * the point at which the pattern starts.</li>
  10822. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName).</li>
  10823. * </ul>
  10824. * @param $ret (boolean) if true do not send the command.
  10825. * @return string the PDF command
  10826. * @public
  10827. * @since 2.1.000 (2008-01-08)
  10828. */
  10829. public function SetLineStyle($style, $ret=false) {
  10830. $s = ''; // string to be returned
  10831. if (!is_array($style)) {
  10832. return;
  10833. }
  10834. if (isset($style['width'])) {
  10835. $this->LineWidth = $style['width'];
  10836. $this->linestyleWidth = sprintf('%F w', ($style['width'] * $this->k));
  10837. $s .= $this->linestyleWidth.' ';
  10838. }
  10839. if (isset($style['cap'])) {
  10840. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  10841. if (isset($ca[$style['cap']])) {
  10842. $this->linestyleCap = $ca[$style['cap']].' J';
  10843. $s .= $this->linestyleCap.' ';
  10844. }
  10845. }
  10846. if (isset($style['join'])) {
  10847. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  10848. if (isset($ja[$style['join']])) {
  10849. $this->linestyleJoin = $ja[$style['join']].' j';
  10850. $s .= $this->linestyleJoin.' ';
  10851. }
  10852. }
  10853. if (isset($style['dash'])) {
  10854. $dash_string = '';
  10855. if ($style['dash']) {
  10856. if (preg_match('/^.+,/', $style['dash']) > 0) {
  10857. $tab = explode(',', $style['dash']);
  10858. } else {
  10859. $tab = array($style['dash']);
  10860. }
  10861. $dash_string = '';
  10862. foreach ($tab as $i => $v) {
  10863. if ($i) {
  10864. $dash_string .= ' ';
  10865. }
  10866. $dash_string .= sprintf('%F', $v);
  10867. }
  10868. }
  10869. if (!isset($style['phase']) OR !$style['dash']) {
  10870. $style['phase'] = 0;
  10871. }
  10872. $this->linestyleDash = sprintf('[%s] %F d', $dash_string, $style['phase']);
  10873. $s .= $this->linestyleDash.' ';
  10874. }
  10875. if (isset($style['color'])) {
  10876. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  10877. }
  10878. if (!$ret AND ($this->state == 2)) {
  10879. $this->_out($s);
  10880. }
  10881. return $s;
  10882. }
  10883. /**
  10884. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  10885. * @param $x (float) Abscissa of point.
  10886. * @param $y (float) Ordinate of point.
  10887. * @protected
  10888. * @since 2.1.000 (2008-01-08)
  10889. */
  10890. protected function _outPoint($x, $y) {
  10891. if ($this->state == 2) {
  10892. $this->_out(sprintf('%F %F m', ($x * $this->k), (($this->h - $y) * $this->k)));
  10893. }
  10894. }
  10895. /**
  10896. * Append a straight line segment from the current point to the point (x, y).
  10897. * The new current point shall be (x, y).
  10898. * @param $x (float) Abscissa of end point.
  10899. * @param $y (float) Ordinate of end point.
  10900. * @protected
  10901. * @since 2.1.000 (2008-01-08)
  10902. */
  10903. protected function _outLine($x, $y) {
  10904. if ($this->state == 2) {
  10905. $this->_out(sprintf('%F %F l', ($x * $this->k), (($this->h - $y) * $this->k)));
  10906. }
  10907. }
  10908. /**
  10909. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  10910. * @param $x (float) Abscissa of upper-left corner.
  10911. * @param $y (float) Ordinate of upper-left corner.
  10912. * @param $w (float) Width.
  10913. * @param $h (float) Height.
  10914. * @param $op (string) options
  10915. * @protected
  10916. * @since 2.1.000 (2008-01-08)
  10917. */
  10918. protected function _outRect($x, $y, $w, $h, $op) {
  10919. if ($this->state == 2) {
  10920. $this->_out(sprintf('%F %F %F %F re %s', ($x * $this->k), (($this->h - $y) * $this->k), ($w * $this->k), (-$h * $this->k), $op));
  10921. }
  10922. }
  10923. /**
  10924. * Append a cubic B\E9zier 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\E9zier control points.
  10925. * The new current point shall be (x3, y3).
  10926. * @param $x1 (float) Abscissa of control point 1.
  10927. * @param $y1 (float) Ordinate of control point 1.
  10928. * @param $x2 (float) Abscissa of control point 2.
  10929. * @param $y2 (float) Ordinate of control point 2.
  10930. * @param $x3 (float) Abscissa of end point.
  10931. * @param $y3 (float) Ordinate of end point.
  10932. * @protected
  10933. * @since 2.1.000 (2008-01-08)
  10934. */
  10935. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  10936. if ($this->state == 2) {
  10937. $this->_out(sprintf('%F %F %F %F %F %F 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)));
  10938. }
  10939. }
  10940. /**
  10941. * Append a cubic B\E9zier 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\E9zier control points.
  10942. * The new current point shall be (x3, y3).
  10943. * @param $x2 (float) Abscissa of control point 2.
  10944. * @param $y2 (float) Ordinate of control point 2.
  10945. * @param $x3 (float) Abscissa of end point.
  10946. * @param $y3 (float) Ordinate of end point.
  10947. * @protected
  10948. * @since 4.9.019 (2010-04-26)
  10949. */
  10950. protected function _outCurveV($x2, $y2, $x3, $y3) {
  10951. if ($this->state == 2) {
  10952. $this->_out(sprintf('%F %F %F %F v', ($x2 * $this->k), (($this->h - $y2) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10953. }
  10954. }
  10955. /**
  10956. * Append a cubic B\E9zier 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\E9zier control points.
  10957. * The new current point shall be (x3, y3).
  10958. * @param $x1 (float) Abscissa of control point 1.
  10959. * @param $y1 (float) Ordinate of control point 1.
  10960. * @param $x3 (float) Abscissa of end point.
  10961. * @param $y3 (float) Ordinate of end point.
  10962. * @protected
  10963. * @since 2.1.000 (2008-01-08)
  10964. */
  10965. protected function _outCurveY($x1, $y1, $x3, $y3) {
  10966. if ($this->state == 2) {
  10967. $this->_out(sprintf('%F %F %F %F y', ($x1 * $this->k), (($this->h - $y1) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10968. }
  10969. }
  10970. /**
  10971. * Draws a line between two points.
  10972. * @param $x1 (float) Abscissa of first point.
  10973. * @param $y1 (float) Ordinate of first point.
  10974. * @param $x2 (float) Abscissa of second point.
  10975. * @param $y2 (float) Ordinate of second point.
  10976. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  10977. * @public
  10978. * @since 1.0
  10979. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  10980. */
  10981. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  10982. if ($this->state != 2) {
  10983. return;
  10984. }
  10985. if (is_array($style)) {
  10986. $this->SetLineStyle($style);
  10987. }
  10988. $this->_outPoint($x1, $y1);
  10989. $this->_outLine($x2, $y2);
  10990. $this->_out('S');
  10991. }
  10992. /**
  10993. * Draws a rectangle.
  10994. * @param $x (float) Abscissa of upper-left corner.
  10995. * @param $y (float) Ordinate of upper-left corner.
  10996. * @param $w (float) Width.
  10997. * @param $h (float) Height.
  10998. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  10999. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  11000. * <ul>
  11001. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  11002. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  11003. * </ul>
  11004. * If a key is not present or is null, the correspondent border is not drawn. Default value: default line style (empty array).
  11005. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11006. * @public
  11007. * @since 1.0
  11008. * @see SetLineStyle()
  11009. */
  11010. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  11011. if ($this->state != 2) {
  11012. return;
  11013. }
  11014. if (empty($style)) {
  11015. $style = 'S';
  11016. }
  11017. if (!(strpos($style, 'F') === false) AND !empty($fill_color)) {
  11018. // set background color
  11019. $this->SetFillColorArray($fill_color);
  11020. }
  11021. if (!empty($border_style)) {
  11022. if (isset($border_style['all']) AND !empty($border_style['all'])) {
  11023. //set global style for border
  11024. $this->SetLineStyle($border_style['all']);
  11025. $border_style = array();
  11026. } else {
  11027. // remove stroke operator from style
  11028. $opnostroke = array('S' => '', 'D' => '', 's' => '', 'd' => '', 'B' => 'F', 'FD' => 'F', 'DF' => 'F', 'B*' => 'F*', 'F*D' => 'F*', 'DF*' => 'F*', 'b' => 'f', 'fd' => 'f', 'df' => 'f', 'b*' => 'f*', 'f*d' => 'f*', 'df*' => 'f*' );
  11029. if (isset($opnostroke[$style])) {
  11030. $style = $opnostroke[$style];
  11031. }
  11032. }
  11033. }
  11034. if (!empty($style)) {
  11035. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11036. $this->_outRect($x, $y, $w, $h, $op);
  11037. }
  11038. if (!empty($border_style)) {
  11039. $border_style2 = array();
  11040. foreach ($border_style as $line => $value) {
  11041. $length = strlen($line);
  11042. for ($i = 0; $i < $length; ++$i) {
  11043. $border_style2[$line[$i]] = $value;
  11044. }
  11045. }
  11046. $border_style = $border_style2;
  11047. if (isset($border_style['L']) AND $border_style['L']) {
  11048. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  11049. }
  11050. if (isset($border_style['T']) AND $border_style['T']) {
  11051. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  11052. }
  11053. if (isset($border_style['R']) AND $border_style['R']) {
  11054. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  11055. }
  11056. if (isset($border_style['B']) AND $border_style['B']) {
  11057. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  11058. }
  11059. }
  11060. }
  11061. /**
  11062. * Draws a Bezier curve.
  11063. * The Bezier curve is a tangent to the line between the control points at
  11064. * either end of the curve.
  11065. * @param $x0 (float) Abscissa of start point.
  11066. * @param $y0 (float) Ordinate of start point.
  11067. * @param $x1 (float) Abscissa of control point 1.
  11068. * @param $y1 (float) Ordinate of control point 1.
  11069. * @param $x2 (float) Abscissa of control point 2.
  11070. * @param $y2 (float) Ordinate of control point 2.
  11071. * @param $x3 (float) Abscissa of end point.
  11072. * @param $y3 (float) Ordinate of end point.
  11073. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11074. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11075. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11076. * @public
  11077. * @see SetLineStyle()
  11078. * @since 2.1.000 (2008-01-08)
  11079. */
  11080. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  11081. if ($this->state != 2) {
  11082. return;
  11083. }
  11084. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11085. $this->SetFillColorArray($fill_color);
  11086. }
  11087. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11088. if ($line_style) {
  11089. $this->SetLineStyle($line_style);
  11090. }
  11091. $this->_outPoint($x0, $y0);
  11092. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11093. $this->_out($op);
  11094. }
  11095. /**
  11096. * Draws a poly-Bezier curve.
  11097. * Each Bezier curve segment is a tangent to the line between the control points at
  11098. * either end of the curve.
  11099. * @param $x0 (float) Abscissa of start point.
  11100. * @param $y0 (float) Ordinate of start point.
  11101. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  11102. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11103. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11104. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11105. * @public
  11106. * @see SetLineStyle()
  11107. * @since 3.0008 (2008-05-12)
  11108. */
  11109. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  11110. if ($this->state != 2) {
  11111. return;
  11112. }
  11113. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11114. $this->SetFillColorArray($fill_color);
  11115. }
  11116. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11117. if ($op == 'f') {
  11118. $line_style = array();
  11119. }
  11120. if ($line_style) {
  11121. $this->SetLineStyle($line_style);
  11122. }
  11123. $this->_outPoint($x0, $y0);
  11124. foreach ($segments as $segment) {
  11125. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  11126. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11127. }
  11128. $this->_out($op);
  11129. }
  11130. /**
  11131. * Draws an ellipse.
  11132. * An ellipse is formed from n Bezier curves.
  11133. * @param $x0 (float) Abscissa of center point.
  11134. * @param $y0 (float) Ordinate of center point.
  11135. * @param $rx (float) Horizontal radius.
  11136. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11137. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11138. * @param $astart: (float) Angle start of draw line. Default value: 0.
  11139. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  11140. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11141. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  11142. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11143. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11144. * @author Nicola Asuni
  11145. * @public
  11146. * @since 2.1.000 (2008-01-08)
  11147. */
  11148. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11149. if ($this->state != 2) {
  11150. return;
  11151. }
  11152. if (TCPDF_STATIC::empty_string($ry) OR ($ry == 0)) {
  11153. $ry = $rx;
  11154. }
  11155. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11156. $this->SetFillColorArray($fill_color);
  11157. }
  11158. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11159. if ($op == 'f') {
  11160. $line_style = array();
  11161. }
  11162. if ($line_style) {
  11163. $this->SetLineStyle($line_style);
  11164. }
  11165. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  11166. $this->_out($op);
  11167. }
  11168. /**
  11169. * Append an elliptical arc to the current path.
  11170. * An ellipse is formed from n Bezier curves.
  11171. * @param $xc (float) Abscissa of center point.
  11172. * @param $yc (float) Ordinate of center point.
  11173. * @param $rx (float) Horizontal radius.
  11174. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11175. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  11176. * @param $angs: (float) Angle start of draw line. Default value: 0.
  11177. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  11178. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  11179. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11180. * @param $startpoint (boolean) if true output a starting point.
  11181. * @param $ccw (boolean) if true draws in counter-clockwise.
  11182. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  11183. * @return array bounding box coordinates (x min, y min, x max, y max)
  11184. * @author Nicola Asuni
  11185. * @protected
  11186. * @since 4.9.019 (2010-04-26)
  11187. */
  11188. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  11189. if (($rx <= 0) OR ($ry < 0)) {
  11190. return;
  11191. }
  11192. $k = $this->k;
  11193. if ($nc < 2) {
  11194. $nc = 2;
  11195. }
  11196. $xmin = 2147483647;
  11197. $ymin = 2147483647;
  11198. $xmax = 0;
  11199. $ymax = 0;
  11200. if ($pie) {
  11201. // center of the arc
  11202. $this->_outPoint($xc, $yc);
  11203. }
  11204. $xang = deg2rad((float) $xang);
  11205. $angs = deg2rad((float) $angs);
  11206. $angf = deg2rad((float) $angf);
  11207. if ($svg) {
  11208. $as = $angs;
  11209. $af = $angf;
  11210. } else {
  11211. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  11212. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  11213. }
  11214. if ($as < 0) {
  11215. $as += (2 * M_PI);
  11216. }
  11217. if ($af < 0) {
  11218. $af += (2 * M_PI);
  11219. }
  11220. if ($ccw AND ($as > $af)) {
  11221. // reverse rotation
  11222. $as -= (2 * M_PI);
  11223. } elseif (!$ccw AND ($as < $af)) {
  11224. // reverse rotation
  11225. $af -= (2 * M_PI);
  11226. }
  11227. $total_angle = ($af - $as);
  11228. if ($nc < 2) {
  11229. $nc = 2;
  11230. }
  11231. // total arcs to draw
  11232. $nc *= (2 * abs($total_angle) / M_PI);
  11233. $nc = round($nc) + 1;
  11234. // angle of each arc
  11235. $arcang = ($total_angle / $nc);
  11236. // center point in PDF coordinates
  11237. $x0 = $xc;
  11238. $y0 = ($this->h - $yc);
  11239. // starting angle
  11240. $ang = $as;
  11241. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  11242. $cos_xang = cos($xang);
  11243. $sin_xang = sin($xang);
  11244. $cos_ang = cos($ang);
  11245. $sin_ang = sin($ang);
  11246. // first arc point
  11247. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11248. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11249. // first Bezier control point
  11250. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11251. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11252. if ($pie) {
  11253. // line from center to arc starting point
  11254. $this->_outLine($px1, $this->h - $py1);
  11255. } elseif ($startpoint) {
  11256. // arc starting point
  11257. $this->_outPoint($px1, $this->h - $py1);
  11258. }
  11259. // draw arcs
  11260. for ($i = 1; $i <= $nc; ++$i) {
  11261. // starting angle
  11262. $ang = $as + ($i * $arcang);
  11263. if ($i == $nc) {
  11264. $ang = $af;
  11265. }
  11266. $cos_ang = cos($ang);
  11267. $sin_ang = sin($ang);
  11268. // second arc point
  11269. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11270. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11271. // second Bezier control point
  11272. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11273. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11274. // draw arc
  11275. $cx1 = ($px1 + $qx1);
  11276. $cy1 = ($this->h - ($py1 + $qy1));
  11277. $cx2 = ($px2 - $qx2);
  11278. $cy2 = ($this->h - ($py2 - $qy2));
  11279. $cx3 = $px2;
  11280. $cy3 = ($this->h - $py2);
  11281. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  11282. // get bounding box coordinates
  11283. $xmin = min($xmin, $cx1, $cx2, $cx3);
  11284. $ymin = min($ymin, $cy1, $cy2, $cy3);
  11285. $xmax = max($xmax, $cx1, $cx2, $cx3);
  11286. $ymax = max($ymax, $cy1, $cy2, $cy3);
  11287. // move to next point
  11288. $px1 = $px2;
  11289. $py1 = $py2;
  11290. $qx1 = $qx2;
  11291. $qy1 = $qy2;
  11292. }
  11293. if ($pie) {
  11294. $this->_outLine($xc, $yc);
  11295. // get bounding box coordinates
  11296. $xmin = min($xmin, $xc);
  11297. $ymin = min($ymin, $yc);
  11298. $xmax = max($xmax, $xc);
  11299. $ymax = max($ymax, $yc);
  11300. }
  11301. return array($xmin, $ymin, $xmax, $ymax);
  11302. }
  11303. /**
  11304. * Draws a circle.
  11305. * A circle is formed from n Bezier curves.
  11306. * @param $x0 (float) Abscissa of center point.
  11307. * @param $y0 (float) Ordinate of center point.
  11308. * @param $r (float) Radius.
  11309. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  11310. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  11311. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11312. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11313. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11314. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  11315. * @public
  11316. * @since 2.1.000 (2008-01-08)
  11317. */
  11318. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11319. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  11320. }
  11321. /**
  11322. * Draws a polygonal line
  11323. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11324. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11325. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11326. * <ul>
  11327. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11328. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11329. * </ul>
  11330. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11331. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11332. * @since 4.8.003 (2009-09-15)
  11333. * @public
  11334. */
  11335. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  11336. $this->Polygon($p, $style, $line_style, $fill_color, false);
  11337. }
  11338. /**
  11339. * Draws a polygon.
  11340. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11341. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11342. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11343. * <ul>
  11344. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11345. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11346. * </ul>
  11347. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11348. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11349. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  11350. * @public
  11351. * @since 2.1.000 (2008-01-08)
  11352. */
  11353. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  11354. if ($this->state != 2) {
  11355. return;
  11356. }
  11357. $nc = count($p); // number of coordinates
  11358. $np = $nc / 2; // number of points
  11359. if ($closed) {
  11360. // close polygon by adding the first 2 points at the end (one line)
  11361. for ($i = 0; $i < 4; ++$i) {
  11362. $p[$nc + $i] = $p[$i];
  11363. }
  11364. // copy style for the last added line
  11365. if (isset($line_style[0])) {
  11366. $line_style[$np] = $line_style[0];
  11367. }
  11368. $nc += 4;
  11369. }
  11370. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11371. $this->SetFillColorArray($fill_color);
  11372. }
  11373. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11374. if ($op == 'f') {
  11375. $line_style = array();
  11376. }
  11377. $draw = true;
  11378. if ($line_style) {
  11379. if (isset($line_style['all'])) {
  11380. $this->SetLineStyle($line_style['all']);
  11381. } else {
  11382. $draw = false;
  11383. if ($op == 'B') {
  11384. // draw fill
  11385. $op = 'f';
  11386. $this->_outPoint($p[0], $p[1]);
  11387. for ($i = 2; $i < $nc; $i = $i + 2) {
  11388. $this->_outLine($p[$i], $p[$i + 1]);
  11389. }
  11390. $this->_out($op);
  11391. }
  11392. // draw outline
  11393. $this->_outPoint($p[0], $p[1]);
  11394. for ($i = 2; $i < $nc; $i = $i + 2) {
  11395. $line_num = ($i / 2) - 1;
  11396. if (isset($line_style[$line_num])) {
  11397. if ($line_style[$line_num] != 0) {
  11398. if (is_array($line_style[$line_num])) {
  11399. $this->_out('S');
  11400. $this->SetLineStyle($line_style[$line_num]);
  11401. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  11402. $this->_outLine($p[$i], $p[$i + 1]);
  11403. $this->_out('S');
  11404. $this->_outPoint($p[$i], $p[$i + 1]);
  11405. } else {
  11406. $this->_outLine($p[$i], $p[$i + 1]);
  11407. }
  11408. }
  11409. } else {
  11410. $this->_outLine($p[$i], $p[$i + 1]);
  11411. }
  11412. }
  11413. $this->_out($op);
  11414. }
  11415. }
  11416. if ($draw) {
  11417. $this->_outPoint($p[0], $p[1]);
  11418. for ($i = 2; $i < $nc; $i = $i + 2) {
  11419. $this->_outLine($p[$i], $p[$i + 1]);
  11420. }
  11421. $this->_out($op);
  11422. }
  11423. }
  11424. /**
  11425. * Draws a regular polygon.
  11426. * @param $x0 (float) Abscissa of center point.
  11427. * @param $y0 (float) Ordinate of center point.
  11428. * @param $r: (float) Radius of inscribed circle.
  11429. * @param $ns (integer) Number of sides.
  11430. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  11431. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  11432. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11433. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11434. * <ul>
  11435. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  11436. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  11437. * </ul>
  11438. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11439. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11440. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11441. * <ul>
  11442. * <li>D or empty string: Draw (default).</li>
  11443. * <li>F: Fill.</li>
  11444. * <li>DF or FD: Draw and fill.</li>
  11445. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11446. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11447. * </ul>
  11448. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11449. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11450. * @public
  11451. * @since 2.1.000 (2008-01-08)
  11452. */
  11453. 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()) {
  11454. if (3 > $ns) {
  11455. $ns = 3;
  11456. }
  11457. if ($draw_circle) {
  11458. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11459. }
  11460. $p = array();
  11461. for ($i = 0; $i < $ns; ++$i) {
  11462. $a = $angle + ($i * 360 / $ns);
  11463. $a_rad = deg2rad((float) $a);
  11464. $p[] = $x0 + ($r * sin($a_rad));
  11465. $p[] = $y0 + ($r * cos($a_rad));
  11466. }
  11467. $this->Polygon($p, $style, $line_style, $fill_color);
  11468. }
  11469. /**
  11470. * Draws a star polygon
  11471. * @param $x0 (float) Abscissa of center point.
  11472. * @param $y0 (float) Ordinate of center point.
  11473. * @param $r (float) Radius of inscribed circle.
  11474. * @param $nv (integer) Number of vertices.
  11475. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  11476. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11477. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  11478. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11479. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11480. * <ul>
  11481. * <li>all: Line style of all sides. Array like for
  11482. * SetLineStyle().</li>
  11483. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  11484. * </ul>
  11485. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11486. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11487. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11488. * <ul>
  11489. * <li>D or empty string: Draw (default).</li>
  11490. * <li>F: Fill.</li>
  11491. * <li>DF or FD: Draw and fill.</li>
  11492. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11493. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11494. * </ul>
  11495. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11496. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11497. * @public
  11498. * @since 2.1.000 (2008-01-08)
  11499. */
  11500. 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()) {
  11501. if ($nv < 2) {
  11502. $nv = 2;
  11503. }
  11504. if ($draw_circle) {
  11505. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11506. }
  11507. $p2 = array();
  11508. $visited = array();
  11509. for ($i = 0; $i < $nv; ++$i) {
  11510. $a = $angle + ($i * 360 / $nv);
  11511. $a_rad = deg2rad((float) $a);
  11512. $p2[] = $x0 + ($r * sin($a_rad));
  11513. $p2[] = $y0 + ($r * cos($a_rad));
  11514. $visited[] = false;
  11515. }
  11516. $p = array();
  11517. $i = 0;
  11518. do {
  11519. $p[] = $p2[$i * 2];
  11520. $p[] = $p2[($i * 2) + 1];
  11521. $visited[$i] = true;
  11522. $i += $ng;
  11523. $i %= $nv;
  11524. } while (!$visited[$i]);
  11525. $this->Polygon($p, $style, $line_style, $fill_color);
  11526. }
  11527. /**
  11528. * Draws a rounded rectangle.
  11529. * @param $x (float) Abscissa of upper-left corner.
  11530. * @param $y (float) Ordinate of upper-left corner.
  11531. * @param $w (float) Width.
  11532. * @param $h (float) Height.
  11533. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  11534. * @param $round_corner (string) 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 right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  11535. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11536. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11537. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11538. * @public
  11539. * @since 2.1.000 (2008-01-08)
  11540. */
  11541. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11542. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  11543. }
  11544. /**
  11545. * Draws a rounded rectangle.
  11546. * @param $x (float) Abscissa of upper-left corner.
  11547. * @param $y (float) Ordinate of upper-left corner.
  11548. * @param $w (float) Width.
  11549. * @param $h (float) Height.
  11550. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  11551. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  11552. * @param $round_corner (string) 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 right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  11553. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11554. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11555. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11556. * @public
  11557. * @since 4.9.019 (2010-04-22)
  11558. */
  11559. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11560. if ($this->state != 2) {
  11561. return;
  11562. }
  11563. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  11564. // Not rounded
  11565. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  11566. return;
  11567. }
  11568. // Rounded
  11569. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11570. $this->SetFillColorArray($fill_color);
  11571. }
  11572. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11573. if ($op == 'f') {
  11574. $border_style = array();
  11575. }
  11576. if ($border_style) {
  11577. $this->SetLineStyle($border_style);
  11578. }
  11579. $MyArc = 4 / 3 * (sqrt(2) - 1);
  11580. $this->_outPoint($x + $rx, $y);
  11581. $xc = $x + $w - $rx;
  11582. $yc = $y + $ry;
  11583. $this->_outLine($xc, $y);
  11584. if ($round_corner[0]) {
  11585. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  11586. } else {
  11587. $this->_outLine($x + $w, $y);
  11588. }
  11589. $xc = $x + $w - $rx;
  11590. $yc = $y + $h - $ry;
  11591. $this->_outLine($x + $w, $yc);
  11592. if ($round_corner[1]) {
  11593. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  11594. } else {
  11595. $this->_outLine($x + $w, $y + $h);
  11596. }
  11597. $xc = $x + $rx;
  11598. $yc = $y + $h - $ry;
  11599. $this->_outLine($xc, $y + $h);
  11600. if ($round_corner[2]) {
  11601. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  11602. } else {
  11603. $this->_outLine($x, $y + $h);
  11604. }
  11605. $xc = $x + $rx;
  11606. $yc = $y + $ry;
  11607. $this->_outLine($x, $yc);
  11608. if ($round_corner[3]) {
  11609. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  11610. } else {
  11611. $this->_outLine($x, $y);
  11612. $this->_outLine($x + $rx, $y);
  11613. }
  11614. $this->_out($op);
  11615. }
  11616. /**
  11617. * Draws a grahic arrow.
  11618. * @param $x0 (float) Abscissa of first point.
  11619. * @param $y0 (float) Ordinate of first point.
  11620. * @param $x1 (float) Abscissa of second point.
  11621. * @param $y1 (float) Ordinate of second point.
  11622. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  11623. * @param $arm_size (float) length of arrowhead arms
  11624. * @param $arm_angle (int) angle between an arm and the shaft
  11625. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  11626. * @since 4.6.018 (2009-07-10)
  11627. */
  11628. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  11629. // getting arrow direction angle
  11630. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  11631. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  11632. if ($dir_angle < 0) {
  11633. $dir_angle += (2 * M_PI);
  11634. }
  11635. $arm_angle = deg2rad($arm_angle);
  11636. $sx1 = $x1;
  11637. $sy1 = $y1;
  11638. if ($head_style > 0) {
  11639. // calculate the stopping point for the arrow shaft
  11640. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  11641. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  11642. }
  11643. // main arrow line / shaft
  11644. $this->Line($x0, $y0, $sx1, $sy1);
  11645. // left arrowhead arm tip
  11646. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  11647. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  11648. // right arrowhead arm tip
  11649. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  11650. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  11651. $mode = 'D';
  11652. $style = array();
  11653. switch ($head_style) {
  11654. case 0: {
  11655. // draw only arrowhead arms
  11656. $mode = 'D';
  11657. $style = array(1, 1, 0);
  11658. break;
  11659. }
  11660. case 1: {
  11661. // draw closed arrowhead, but no fill
  11662. $mode = 'D';
  11663. break;
  11664. }
  11665. case 2: {
  11666. // closed and filled arrowhead
  11667. $mode = 'DF';
  11668. break;
  11669. }
  11670. case 3: {
  11671. // filled arrowhead
  11672. $mode = 'F';
  11673. break;
  11674. }
  11675. }
  11676. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  11677. }
  11678. // END GRAPHIC FUNCTIONS SECTION -----------------------
  11679. /**
  11680. * Add a Named Destination.
  11681. * NOTE: destination names are unique, so only last entry will be saved.
  11682. * @param $name (string) Destination name.
  11683. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  11684. * @param $page (int) Target page number (leave empty for current page).
  11685. * @param $x (float) X position in user units of the destiantion on the selected page (default = -1 = current position;).
  11686. * @return (string) Stripped named destination identifier or false in case of error.
  11687. * @public
  11688. * @author Christian Deligant, Nicola Asuni
  11689. * @since 5.9.097 (2011-06-23)
  11690. */
  11691. public function setDestination($name, $y=-1, $page='', $x=-1) {
  11692. // remove unsupported characters
  11693. $name = TCPDF_STATIC::encodeNameObject($name);
  11694. if (TCPDF_STATIC::empty_string($name)) {
  11695. return false;
  11696. }
  11697. if ($y == -1) {
  11698. $y = $this->GetY();
  11699. } elseif ($y < 0) {
  11700. $y = 0;
  11701. } elseif ($y > $this->h) {
  11702. $y = $this->h;
  11703. }
  11704. if ($x == -1) {
  11705. $x = $this->GetX();
  11706. } elseif ($x < 0) {
  11707. $x = 0;
  11708. } elseif ($x > $this->w) {
  11709. $x = $this->w;
  11710. }
  11711. if (empty($page)) {
  11712. $page = $this->PageNo();
  11713. if (empty($page)) {
  11714. return;
  11715. }
  11716. }
  11717. $this->dests[$name] = array('x' => $x, 'y' => $y, 'p' => $page);
  11718. return $name;
  11719. }
  11720. /**
  11721. * Return the Named Destination array.
  11722. * @return (array) Named Destination array.
  11723. * @public
  11724. * @author Nicola Asuni
  11725. * @since 5.9.097 (2011-06-23)
  11726. */
  11727. public function getDestination() {
  11728. return $this->dests;
  11729. }
  11730. /**
  11731. * Insert Named Destinations.
  11732. * @protected
  11733. * @author Johannes G\FCntert, Nicola Asuni
  11734. * @since 5.9.098 (2011-06-23)
  11735. */
  11736. protected function _putdests() {
  11737. if (empty($this->dests)) {
  11738. return;
  11739. }
  11740. $this->n_dests = $this->_newobj();
  11741. $out = ' <<';
  11742. foreach($this->dests as $name => $o) {
  11743. $out .= ' /'.$name.' '.sprintf('[%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  11744. }
  11745. $out .= ' >>';
  11746. $out .= "\n".'endobj';
  11747. $this->_out($out);
  11748. }
  11749. /**
  11750. * Adds a bookmark - alias for Bookmark().
  11751. * @param $txt (string) Bookmark description.
  11752. * @param $level (int) Bookmark level (minimum value is 0).
  11753. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11754. * @param $page (int) Target page number (leave empty for current page).
  11755. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11756. * @param $color (array) RGB color array (values from 0 to 255).
  11757. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11758. * @param $link (mixed) URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11759. * @public
  11760. */
  11761. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11762. $this->Bookmark($txt, $level, $y, $page, $style, $color, $x, $link);
  11763. }
  11764. /**
  11765. * Adds a bookmark.
  11766. * @param $txt (string) Bookmark description.
  11767. * @param $level (int) Bookmark level (minimum value is 0).
  11768. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11769. * @param $page (int) Target page number (leave empty for current page).
  11770. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11771. * @param $color (array) RGB color array (values from 0 to 255).
  11772. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11773. * @param $link (mixed) URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11774. * @public
  11775. * @since 2.1.002 (2008-02-12)
  11776. */
  11777. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11778. if ($level < 0) {
  11779. $level = 0;
  11780. }
  11781. if (isset($this->outlines[0])) {
  11782. $lastoutline = end($this->outlines);
  11783. $maxlevel = $lastoutline['l'] + 1;
  11784. } else {
  11785. $maxlevel = 0;
  11786. }
  11787. if ($level > $maxlevel) {
  11788. $level = $maxlevel;
  11789. }
  11790. if ($y == -1) {
  11791. $y = $this->GetY();
  11792. } elseif ($y < 0) {
  11793. $y = 0;
  11794. } elseif ($y > $this->h) {
  11795. $y = $this->h;
  11796. }
  11797. if ($x == -1) {
  11798. $x = $this->GetX();
  11799. } elseif ($x < 0) {
  11800. $x = 0;
  11801. } elseif ($x > $this->w) {
  11802. $x = $this->w;
  11803. }
  11804. if (empty($page)) {
  11805. $page = $this->PageNo();
  11806. if (empty($page)) {
  11807. return;
  11808. }
  11809. }
  11810. $this->outlines[] = array('t' => $txt, 'l' => $level, 'x' => $x, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color, 'u' => $link);
  11811. }
  11812. /**
  11813. * Sort bookmarks for page and key.
  11814. * @protected
  11815. * @since 5.9.119 (2011-09-19)
  11816. */
  11817. protected function sortBookmarks() {
  11818. // get sorting columns
  11819. $outline_p = array();
  11820. $outline_y = array();
  11821. foreach ($this->outlines as $key => $row) {
  11822. $outline_p[$key] = $row['p'];
  11823. $outline_k[$key] = $key;
  11824. }
  11825. // sort outlines by page and original position
  11826. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  11827. }
  11828. /**
  11829. * Create a bookmark PDF string.
  11830. * @protected
  11831. * @author Olivier Plathey, Nicola Asuni
  11832. * @since 2.1.002 (2008-02-12)
  11833. */
  11834. protected function _putbookmarks() {
  11835. $nb = count($this->outlines);
  11836. if ($nb == 0) {
  11837. return;
  11838. }
  11839. // sort bookmarks
  11840. $this->sortBookmarks();
  11841. $lru = array();
  11842. $level = 0;
  11843. foreach ($this->outlines as $i => $o) {
  11844. if ($o['l'] > 0) {
  11845. $parent = $lru[($o['l'] - 1)];
  11846. //Set parent and last pointers
  11847. $this->outlines[$i]['parent'] = $parent;
  11848. $this->outlines[$parent]['last'] = $i;
  11849. if ($o['l'] > $level) {
  11850. //Level increasing: set first pointer
  11851. $this->outlines[$parent]['first'] = $i;
  11852. }
  11853. } else {
  11854. $this->outlines[$i]['parent'] = $nb;
  11855. }
  11856. if (($o['l'] <= $level) AND ($i > 0)) {
  11857. //Set prev and next pointers
  11858. $prev = $lru[$o['l']];
  11859. $this->outlines[$prev]['next'] = $i;
  11860. $this->outlines[$i]['prev'] = $prev;
  11861. }
  11862. $lru[$o['l']] = $i;
  11863. $level = $o['l'];
  11864. }
  11865. //Outline items
  11866. $n = $this->n + 1;
  11867. $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';
  11868. foreach ($this->outlines as $i => $o) {
  11869. $oid = $this->_newobj();
  11870. // covert HTML title to string
  11871. $title = preg_replace($nltags, "\n", $o['t']);
  11872. $title = preg_replace("/[\r]+/si", '', $title);
  11873. $title = preg_replace("/[\n]+/si", "\n", $title);
  11874. $title = strip_tags($title);
  11875. $title = $this->stringTrim($title);
  11876. $out = '<</Title '.$this->_textstring($title, $oid);
  11877. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  11878. if (isset($o['prev'])) {
  11879. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  11880. }
  11881. if (isset($o['next'])) {
  11882. $out .= ' /Next '.($n + $o['next']).' 0 R';
  11883. }
  11884. if (isset($o['first'])) {
  11885. $out .= ' /First '.($n + $o['first']).' 0 R';
  11886. }
  11887. if (isset($o['last'])) {
  11888. $out .= ' /Last '.($n + $o['last']).' 0 R';
  11889. }
  11890. if (isset($o['u']) AND !empty($o['u'])) {
  11891. // link
  11892. if (is_string($o['u'])) {
  11893. if ($o['u'][0] == '#') {
  11894. // internal destination
  11895. $out .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($o['u'], 1));
  11896. } elseif ($o['u'][0] == '%') {
  11897. // embedded PDF file
  11898. $filename = basename(substr($o['u'], 1));
  11899. $out .= ' /A <</S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($o['p'] - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  11900. } elseif ($o['u'][0] == '*') {
  11901. // embedded generic file
  11902. $filename = basename(substr($o['u'], 1));
  11903. $jsa = 'var D=event.target.doc;var MyData=D.dataObjects;for (var i in MyData) if (MyData[i].path=="'.$filename.'") D.exportDataObject( { cName : MyData[i].name, nLaunch : 2});';
  11904. $out .= ' /A <</S /JavaScript /JS '.$this->_textstring($jsa, $oid).'>>';
  11905. } else {
  11906. // external URI link
  11907. $out .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($o['u']), $oid).'>>';
  11908. }
  11909. } elseif (isset($this->links[$o['u']])) {
  11910. // internal link ID
  11911. $l = $this->links[$o['u']];
  11912. if (isset($this->page_obj_id[($l[0])])) {
  11913. $out .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  11914. }
  11915. }
  11916. } elseif (isset($this->page_obj_id[($o['p'])])) {
  11917. // link to a page
  11918. $out .= ' '.sprintf('/Dest [%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  11919. }
  11920. // set font style
  11921. $style = 0;
  11922. if (!empty($o['s'])) {
  11923. // bold
  11924. if (strpos($o['s'], 'B') !== false) {
  11925. $style |= 2;
  11926. }
  11927. // oblique
  11928. if (strpos($o['s'], 'I') !== false) {
  11929. $style |= 1;
  11930. }
  11931. }
  11932. $out .= sprintf(' /F %d', $style);
  11933. // set bookmark color
  11934. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  11935. $color = array_values($o['c']);
  11936. $out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  11937. } else {
  11938. // black
  11939. $out .= ' /C [0.0 0.0 0.0]';
  11940. }
  11941. $out .= ' /Count 0'; // normally closed item
  11942. $out .= ' >>';
  11943. $out .= "\n".'endobj';
  11944. $this->_out($out);
  11945. }
  11946. //Outline root
  11947. $this->OutlineRoot = $this->_newobj();
  11948. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  11949. }
  11950. // --- JAVASCRIPT ------------------------------------------------------
  11951. /**
  11952. * Adds a javascript
  11953. * @param $script (string) Javascript code
  11954. * @public
  11955. * @author Johannes G\FCntert, Nicola Asuni
  11956. * @since 2.1.002 (2008-02-12)
  11957. */
  11958. public function IncludeJS($script) {
  11959. $this->javascript .= $script;
  11960. }
  11961. /**
  11962. * Adds a javascript object and return object ID
  11963. * @param $script (string) Javascript code
  11964. * @param $onload (boolean) if true executes this object when opening the document
  11965. * @return int internal object ID
  11966. * @public
  11967. * @author Nicola Asuni
  11968. * @since 4.8.000 (2009-09-07)
  11969. */
  11970. public function addJavascriptObject($script, $onload=false) {
  11971. if ($this->pdfa_mode) {
  11972. // javascript is not allowed in PDF/A mode
  11973. return false;
  11974. }
  11975. ++$this->n;
  11976. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  11977. return $this->n;
  11978. }
  11979. /**
  11980. * Create a javascript PDF string.
  11981. * @protected
  11982. * @author Johannes G\FCntert, Nicola Asuni
  11983. * @since 2.1.002 (2008-02-12)
  11984. */
  11985. protected function _putjavascript() {
  11986. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  11987. return;
  11988. }
  11989. if (strpos($this->javascript, 'this.addField') > 0) {
  11990. if (!$this->ur['enabled']) {
  11991. //$this->setUserRights();
  11992. }
  11993. // the following two lines are used to avoid form fields duplication after saving
  11994. // The addField method only works when releasing user rights (UR3)
  11995. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  11996. $jsb = "getField('tcpdfdocsaved').value='saved';";
  11997. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  11998. }
  11999. // name tree for javascript
  12000. $this->n_js = '<< /Names [';
  12001. if (!empty($this->javascript)) {
  12002. $this->n_js .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  12003. }
  12004. if (!empty($this->js_objects)) {
  12005. foreach ($this->js_objects as $key => $val) {
  12006. if ($val['onload']) {
  12007. $this->n_js .= ' (JS'.$key.') '.$key.' 0 R';
  12008. }
  12009. }
  12010. }
  12011. $this->n_js .= ' ] >>';
  12012. // default Javascript object
  12013. if (!empty($this->javascript)) {
  12014. $obj_id = $this->_newobj();
  12015. $out = '<< /S /JavaScript';
  12016. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  12017. $out .= ' >>';
  12018. $out .= "\n".'endobj';
  12019. $this->_out($out);
  12020. }
  12021. // additional Javascript objects
  12022. if (!empty($this->js_objects)) {
  12023. foreach ($this->js_objects as $key => $val) {
  12024. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  12025. $this->_out($out);
  12026. }
  12027. }
  12028. }
  12029. /**
  12030. * Adds a javascript form field.
  12031. * @param $type (string) field type
  12032. * @param $name (string) field name
  12033. * @param $x (int) horizontal position
  12034. * @param $y (int) vertical position
  12035. * @param $w (int) width
  12036. * @param $h (int) height
  12037. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12038. * @protected
  12039. * @author Denis Van Nuffelen, Nicola Asuni
  12040. * @since 2.1.002 (2008-02-12)
  12041. */
  12042. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  12043. if ($this->rtl) {
  12044. $x = $x - $w;
  12045. }
  12046. // the followind avoid fields duplication after saving the document
  12047. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  12048. $k = $this->k;
  12049. $this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%u,[%F,%F,%F,%F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
  12050. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  12051. while (list($key, $val) = each($prop)) {
  12052. if (strcmp(substr($key, -5), 'Color') == 0) {
  12053. $val = TCPDF_COLORS::_JScolor($val);
  12054. } else {
  12055. $val = "'".$val."'";
  12056. }
  12057. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  12058. }
  12059. if ($this->rtl) {
  12060. $this->x -= $w;
  12061. } else {
  12062. $this->x += $w;
  12063. }
  12064. $this->javascript .= '}';
  12065. }
  12066. // --- FORM FIELDS -----------------------------------------------------
  12067. /**
  12068. * Set default properties for form fields.
  12069. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12070. * @public
  12071. * @author Nicola Asuni
  12072. * @since 4.8.000 (2009-09-06)
  12073. */
  12074. public function setFormDefaultProp($prop=array()) {
  12075. $this->default_form_prop = $prop;
  12076. }
  12077. /**
  12078. * Return the default properties for form fields.
  12079. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12080. * @public
  12081. * @author Nicola Asuni
  12082. * @since 4.8.000 (2009-09-06)
  12083. */
  12084. public function getFormDefaultProp() {
  12085. return $this->default_form_prop;
  12086. }
  12087. /**
  12088. * Creates a text field
  12089. * @param $name (string) field name
  12090. * @param $w (float) Width of the rectangle
  12091. * @param $h (float) Height of the rectangle
  12092. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12093. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12094. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12095. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12096. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12097. * @public
  12098. * @author Nicola Asuni
  12099. * @since 4.8.000 (2009-09-07)
  12100. */
  12101. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12102. if ($x === '') {
  12103. $x = $this->x;
  12104. }
  12105. if ($y === '') {
  12106. $y = $this->y;
  12107. }
  12108. // check page for no-write regions and adapt page margins if necessary
  12109. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12110. if ($js) {
  12111. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  12112. return;
  12113. }
  12114. // get default style
  12115. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12116. // get annotation data
  12117. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12118. // set default appearance stream
  12119. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12120. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12121. $popt['da'] = $fontstyle;
  12122. // build appearance stream
  12123. $popt['ap'] = array();
  12124. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12125. $text = '';
  12126. if (isset($prop['value']) AND !empty($prop['value'])) {
  12127. $text = $prop['value'];
  12128. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  12129. $text = $opt['v'];
  12130. }
  12131. $tmpid = $this->startTemplate($w, $h, false);
  12132. $align = '';
  12133. if (isset($popt['q'])) {
  12134. switch ($popt['q']) {
  12135. case 0: {
  12136. $align = 'L';
  12137. break;
  12138. }
  12139. case 1: {
  12140. $align = 'C';
  12141. break;
  12142. }
  12143. case 2: {
  12144. $align = 'R';
  12145. break;
  12146. }
  12147. default: {
  12148. $align = '';
  12149. break;
  12150. }
  12151. }
  12152. }
  12153. $this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12154. $this->endTemplate();
  12155. --$this->n;
  12156. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12157. unset($this->xobjects[$tmpid]);
  12158. $popt['ap']['n'] .= 'Q EMC';
  12159. // merge options
  12160. $opt = array_merge($popt, $opt);
  12161. // remove some conflicting options
  12162. unset($opt['bs']);
  12163. // set remaining annotation data
  12164. $opt['Subtype'] = 'Widget';
  12165. $opt['ft'] = 'Tx';
  12166. $opt['t'] = $name;
  12167. // Additional annotation's parameters (check _putannotsobj() method):
  12168. //$opt['f']
  12169. //$opt['as']
  12170. //$opt['bs']
  12171. //$opt['be']
  12172. //$opt['c']
  12173. //$opt['border']
  12174. //$opt['h']
  12175. //$opt['mk'];
  12176. //$opt['mk']['r']
  12177. //$opt['mk']['bc'];
  12178. //$opt['mk']['bg'];
  12179. unset($opt['mk']['ca']);
  12180. unset($opt['mk']['rc']);
  12181. unset($opt['mk']['ac']);
  12182. unset($opt['mk']['i']);
  12183. unset($opt['mk']['ri']);
  12184. unset($opt['mk']['ix']);
  12185. unset($opt['mk']['if']);
  12186. //$opt['mk']['if']['sw'];
  12187. //$opt['mk']['if']['s'];
  12188. //$opt['mk']['if']['a'];
  12189. //$opt['mk']['if']['fb'];
  12190. unset($opt['mk']['tp']);
  12191. //$opt['tu']
  12192. //$opt['tm']
  12193. //$opt['ff']
  12194. //$opt['v']
  12195. //$opt['dv']
  12196. //$opt['a']
  12197. //$opt['aa']
  12198. //$opt['q']
  12199. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12200. if ($this->rtl) {
  12201. $this->x -= $w;
  12202. } else {
  12203. $this->x += $w;
  12204. }
  12205. }
  12206. /**
  12207. * Creates a RadioButton field.
  12208. * @param $name (string) Field name.
  12209. * @param $w (int) Width of the radio button.
  12210. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12211. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12212. * @param $onvalue (string) Value to be returned if selected.
  12213. * @param $checked (boolean) Define the initial state.
  12214. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12215. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12216. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12217. * @public
  12218. * @author Nicola Asuni
  12219. * @since 4.8.000 (2009-09-07)
  12220. */
  12221. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  12222. if ($x === '') {
  12223. $x = $this->x;
  12224. }
  12225. if ($y === '') {
  12226. $y = $this->y;
  12227. }
  12228. // check page for no-write regions and adapt page margins if necessary
  12229. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12230. if ($js) {
  12231. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  12232. return;
  12233. }
  12234. if (TCPDF_STATIC::empty_string($onvalue)) {
  12235. $onvalue = 'On';
  12236. }
  12237. if ($checked) {
  12238. $defval = $onvalue;
  12239. } else {
  12240. $defval = 'Off';
  12241. }
  12242. // set font
  12243. $font = 'zapfdingbats';
  12244. if ($this->pdfa_mode) {
  12245. // all fonts must be embedded
  12246. $font = 'pdfa'.$font;
  12247. }
  12248. $this->AddFont($font);
  12249. $tmpfont = $this->getFontBuffer($font);
  12250. // set data for parent group
  12251. if (!isset($this->radiobutton_groups[$this->page])) {
  12252. $this->radiobutton_groups[$this->page] = array();
  12253. }
  12254. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  12255. $this->radiobutton_groups[$this->page][$name] = array();
  12256. ++$this->n;
  12257. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  12258. $this->radio_groups[] = $this->n;
  12259. }
  12260. $kid = ($this->n + 1);
  12261. // save object ID to be added on Kids entry on parent object
  12262. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  12263. // get default style
  12264. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12265. $prop['NoToggleToOff'] = 'true';
  12266. $prop['Radio'] = 'true';
  12267. $prop['borderStyle'] = 'inset';
  12268. // get annotation data
  12269. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12270. // set additional default options
  12271. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12272. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12273. $popt['da'] = $fontstyle;
  12274. // build appearance stream
  12275. $popt['ap'] = array();
  12276. $popt['ap']['n'] = array();
  12277. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
  12278. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12279. $popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(108).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12280. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(109).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12281. if (!isset($popt['mk'])) {
  12282. $popt['mk'] = array();
  12283. }
  12284. $popt['mk']['ca'] = '(l)';
  12285. // merge options
  12286. $opt = array_merge($popt, $opt);
  12287. // set remaining annotation data
  12288. $opt['Subtype'] = 'Widget';
  12289. $opt['ft'] = 'Btn';
  12290. if ($checked) {
  12291. $opt['v'] = array('/'.$onvalue);
  12292. $opt['as'] = $onvalue;
  12293. } else {
  12294. $opt['as'] = 'Off';
  12295. }
  12296. // store readonly flag
  12297. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  12298. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  12299. }
  12300. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  12301. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12302. if ($this->rtl) {
  12303. $this->x -= $w;
  12304. } else {
  12305. $this->x += $w;
  12306. }
  12307. }
  12308. /**
  12309. * Creates a List-box field
  12310. * @param $name (string) field name
  12311. * @param $w (int) width
  12312. * @param $h (int) height
  12313. * @param $values (array) array containing the list of values.
  12314. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12315. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12316. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12317. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12318. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12319. * @public
  12320. * @author Nicola Asuni
  12321. * @since 4.8.000 (2009-09-07)
  12322. */
  12323. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12324. if ($x === '') {
  12325. $x = $this->x;
  12326. }
  12327. if ($y === '') {
  12328. $y = $this->y;
  12329. }
  12330. // check page for no-write regions and adapt page margins if necessary
  12331. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12332. if ($js) {
  12333. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  12334. $s = '';
  12335. foreach ($values as $value) {
  12336. if (is_array($value)) {
  12337. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12338. } else {
  12339. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12340. }
  12341. }
  12342. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12343. return;
  12344. }
  12345. // get default style
  12346. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12347. // get annotation data
  12348. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12349. // set additional default values
  12350. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12351. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12352. $popt['da'] = $fontstyle;
  12353. // build appearance stream
  12354. $popt['ap'] = array();
  12355. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12356. $text = '';
  12357. foreach($values as $item) {
  12358. if (is_array($item)) {
  12359. $text .= $item[1]."\n";
  12360. } else {
  12361. $text .= $item."\n";
  12362. }
  12363. }
  12364. $tmpid = $this->startTemplate($w, $h, false);
  12365. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12366. $this->endTemplate();
  12367. --$this->n;
  12368. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12369. unset($this->xobjects[$tmpid]);
  12370. $popt['ap']['n'] .= 'Q EMC';
  12371. // merge options
  12372. $opt = array_merge($popt, $opt);
  12373. // set remaining annotation data
  12374. $opt['Subtype'] = 'Widget';
  12375. $opt['ft'] = 'Ch';
  12376. $opt['t'] = $name;
  12377. $opt['opt'] = $values;
  12378. unset($opt['mk']['ca']);
  12379. unset($opt['mk']['rc']);
  12380. unset($opt['mk']['ac']);
  12381. unset($opt['mk']['i']);
  12382. unset($opt['mk']['ri']);
  12383. unset($opt['mk']['ix']);
  12384. unset($opt['mk']['if']);
  12385. unset($opt['mk']['tp']);
  12386. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12387. if ($this->rtl) {
  12388. $this->x -= $w;
  12389. } else {
  12390. $this->x += $w;
  12391. }
  12392. }
  12393. /**
  12394. * Creates a Combo-box field
  12395. * @param $name (string) field name
  12396. * @param $w (int) width
  12397. * @param $h (int) height
  12398. * @param $values (array) array containing the list of values.
  12399. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12400. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12401. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12402. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12403. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12404. * @public
  12405. * @author Nicola Asuni
  12406. * @since 4.8.000 (2009-09-07)
  12407. */
  12408. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12409. if ($x === '') {
  12410. $x = $this->x;
  12411. }
  12412. if ($y === '') {
  12413. $y = $this->y;
  12414. }
  12415. // check page for no-write regions and adapt page margins if necessary
  12416. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12417. if ($js) {
  12418. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  12419. $s = '';
  12420. foreach ($values as $value) {
  12421. if (is_array($value)) {
  12422. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12423. } else {
  12424. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12425. }
  12426. }
  12427. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12428. return;
  12429. }
  12430. // get default style
  12431. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12432. $prop['Combo'] = true;
  12433. // get annotation data
  12434. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12435. // set additional default options
  12436. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12437. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12438. $popt['da'] = $fontstyle;
  12439. // build appearance stream
  12440. $popt['ap'] = array();
  12441. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12442. $text = '';
  12443. foreach($values as $item) {
  12444. if (is_array($item)) {
  12445. $text .= $item[1]."\n";
  12446. } else {
  12447. $text .= $item."\n";
  12448. }
  12449. }
  12450. $tmpid = $this->startTemplate($w, $h, false);
  12451. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12452. $this->endTemplate();
  12453. --$this->n;
  12454. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12455. unset($this->xobjects[$tmpid]);
  12456. $popt['ap']['n'] .= 'Q EMC';
  12457. // merge options
  12458. $opt = array_merge($popt, $opt);
  12459. // set remaining annotation data
  12460. $opt['Subtype'] = 'Widget';
  12461. $opt['ft'] = 'Ch';
  12462. $opt['t'] = $name;
  12463. $opt['opt'] = $values;
  12464. unset($opt['mk']['ca']);
  12465. unset($opt['mk']['rc']);
  12466. unset($opt['mk']['ac']);
  12467. unset($opt['mk']['i']);
  12468. unset($opt['mk']['ri']);
  12469. unset($opt['mk']['ix']);
  12470. unset($opt['mk']['if']);
  12471. unset($opt['mk']['tp']);
  12472. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12473. if ($this->rtl) {
  12474. $this->x -= $w;
  12475. } else {
  12476. $this->x += $w;
  12477. }
  12478. }
  12479. /**
  12480. * Creates a CheckBox field
  12481. * @param $name (string) field name
  12482. * @param $w (int) width
  12483. * @param $checked (boolean) define the initial state.
  12484. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12485. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12486. * @param $onvalue (string) value to be returned if selected.
  12487. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12488. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12489. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12490. * @public
  12491. * @author Nicola Asuni
  12492. * @since 4.8.000 (2009-09-07)
  12493. */
  12494. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  12495. if ($x === '') {
  12496. $x = $this->x;
  12497. }
  12498. if ($y === '') {
  12499. $y = $this->y;
  12500. }
  12501. // check page for no-write regions and adapt page margins if necessary
  12502. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12503. if ($js) {
  12504. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  12505. return;
  12506. }
  12507. if (!isset($prop['value'])) {
  12508. $prop['value'] = array('Yes');
  12509. }
  12510. // get default style
  12511. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12512. $prop['borderStyle'] = 'inset';
  12513. // get annotation data
  12514. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12515. // set additional default options
  12516. $font = 'zapfdingbats';
  12517. if ($this->pdfa_mode) {
  12518. // all fonts must be embedded
  12519. $font = 'pdfa'.$font;
  12520. }
  12521. $this->AddFont($font);
  12522. $tmpfont = $this->getFontBuffer($font);
  12523. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12524. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12525. $popt['da'] = $fontstyle;
  12526. // build appearance stream
  12527. $popt['ap'] = array();
  12528. $popt['ap']['n'] = array();
  12529. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
  12530. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12531. $popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(110).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12532. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(111).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12533. // merge options
  12534. $opt = array_merge($popt, $opt);
  12535. // set remaining annotation data
  12536. $opt['Subtype'] = 'Widget';
  12537. $opt['ft'] = 'Btn';
  12538. $opt['t'] = $name;
  12539. if (TCPDF_STATIC::empty_string($onvalue)) {
  12540. $onvalue = 'Yes';
  12541. }
  12542. $opt['opt'] = array($onvalue);
  12543. if ($checked) {
  12544. $opt['v'] = array('/Yes');
  12545. $opt['as'] = 'Yes';
  12546. } else {
  12547. $opt['v'] = array('/Off');
  12548. $opt['as'] = 'Off';
  12549. }
  12550. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12551. if ($this->rtl) {
  12552. $this->x -= $w;
  12553. } else {
  12554. $this->x += $w;
  12555. }
  12556. }
  12557. /**
  12558. * Creates a button field
  12559. * @param $name (string) field name
  12560. * @param $w (int) width
  12561. * @param $h (int) height
  12562. * @param $caption (string) caption.
  12563. * @param $action (mixed) 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.
  12564. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12565. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12566. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12567. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12568. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12569. * @public
  12570. * @author Nicola Asuni
  12571. * @since 4.8.000 (2009-09-07)
  12572. */
  12573. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12574. if ($x === '') {
  12575. $x = $this->x;
  12576. }
  12577. if ($y === '') {
  12578. $y = $this->y;
  12579. }
  12580. // check page for no-write regions and adapt page margins if necessary
  12581. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12582. if ($js) {
  12583. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  12584. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  12585. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  12586. $this->javascript .= 'f'.$name.".highlight='push';\n";
  12587. $this->javascript .= 'f'.$name.".print=false;\n";
  12588. return;
  12589. }
  12590. // get default style
  12591. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12592. $prop['Pushbutton'] = 'true';
  12593. $prop['highlight'] = 'push';
  12594. $prop['display'] = 'display.noPrint';
  12595. // get annotation data
  12596. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12597. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12598. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12599. $popt['da'] = $fontstyle;
  12600. // build appearance stream
  12601. $popt['ap'] = array();
  12602. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12603. $tmpid = $this->startTemplate($w, $h, false);
  12604. $bw = (2 / $this->k); // border width
  12605. $border = array(
  12606. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12607. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  12608. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12609. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  12610. $this->SetFillColor(204);
  12611. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  12612. $this->endTemplate();
  12613. --$this->n;
  12614. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12615. unset($this->xobjects[$tmpid]);
  12616. $popt['ap']['n'] .= 'Q EMC';
  12617. // set additional default options
  12618. if (!isset($popt['mk'])) {
  12619. $popt['mk'] = array();
  12620. }
  12621. $ann_obj_id = ($this->n + 1);
  12622. if (!empty($action) AND !is_array($action)) {
  12623. $ann_obj_id = ($this->n + 2);
  12624. }
  12625. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  12626. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  12627. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  12628. // merge options
  12629. $opt = array_merge($popt, $opt);
  12630. // set remaining annotation data
  12631. $opt['Subtype'] = 'Widget';
  12632. $opt['ft'] = 'Btn';
  12633. $opt['t'] = $caption;
  12634. $opt['v'] = $name;
  12635. if (!empty($action)) {
  12636. if (is_array($action)) {
  12637. // form action options as on section 12.7.5 of PDF32000_2008.
  12638. $opt['aa'] = '/D <<';
  12639. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  12640. foreach ($action AS $key => $val) {
  12641. if (($key == 'S') AND in_array($val, $bmode)) {
  12642. $opt['aa'] .= ' /S /'.$val;
  12643. } elseif (($key == 'F') AND (!empty($val))) {
  12644. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  12645. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  12646. $opt['aa'] .= ' /Fields [';
  12647. foreach ($val AS $field) {
  12648. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  12649. }
  12650. $opt['aa'] .= ']';
  12651. } elseif (($key == 'Flags')) {
  12652. $ff = 0;
  12653. if (is_array($val)) {
  12654. foreach ($val AS $flag) {
  12655. switch ($flag) {
  12656. case 'Include/Exclude': {
  12657. $ff += 1 << 0;
  12658. break;
  12659. }
  12660. case 'IncludeNoValueFields': {
  12661. $ff += 1 << 1;
  12662. break;
  12663. }
  12664. case 'ExportFormat': {
  12665. $ff += 1 << 2;
  12666. break;
  12667. }
  12668. case 'GetMethod': {
  12669. $ff += 1 << 3;
  12670. break;
  12671. }
  12672. case 'SubmitCoordinates': {
  12673. $ff += 1 << 4;
  12674. break;
  12675. }
  12676. case 'XFDF': {
  12677. $ff += 1 << 5;
  12678. break;
  12679. }
  12680. case 'IncludeAppendSaves': {
  12681. $ff += 1 << 6;
  12682. break;
  12683. }
  12684. case 'IncludeAnnotations': {
  12685. $ff += 1 << 7;
  12686. break;
  12687. }
  12688. case 'SubmitPDF': {
  12689. $ff += 1 << 8;
  12690. break;
  12691. }
  12692. case 'CanonicalFormat': {
  12693. $ff += 1 << 9;
  12694. break;
  12695. }
  12696. case 'ExclNonUserAnnots': {
  12697. $ff += 1 << 10;
  12698. break;
  12699. }
  12700. case 'ExclFKey': {
  12701. $ff += 1 << 11;
  12702. break;
  12703. }
  12704. case 'EmbedForm': {
  12705. $ff += 1 << 13;
  12706. break;
  12707. }
  12708. }
  12709. }
  12710. } else {
  12711. $ff = intval($val);
  12712. }
  12713. $opt['aa'] .= ' /Flags '.$ff;
  12714. }
  12715. }
  12716. $opt['aa'] .= ' >>';
  12717. } else {
  12718. // Javascript action or raw action command
  12719. $js_obj_id = $this->addJavascriptObject($action);
  12720. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  12721. }
  12722. }
  12723. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12724. if ($this->rtl) {
  12725. $this->x -= $w;
  12726. } else {
  12727. $this->x += $w;
  12728. }
  12729. }
  12730. // --- END FORMS FIELDS ------------------------------------------------
  12731. /**
  12732. * Add certification signature (DocMDP or UR3)
  12733. * You can set only one signature type
  12734. * @protected
  12735. * @author Nicola Asuni
  12736. * @since 4.6.008 (2009-05-07)
  12737. */
  12738. protected function _putsignature() {
  12739. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  12740. return;
  12741. }
  12742. $sigobjid = ($this->sig_obj_id + 1);
  12743. $out = $this->_getobj($sigobjid)."\n";
  12744. $out .= '<< /Type /Sig';
  12745. $out .= ' /Filter /Adobe.PPKLite';
  12746. $out .= ' /SubFilter /adbe.pkcs7.detached';
  12747. $out .= ' '.TCPDF_STATIC::$byterange_string;
  12748. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  12749. $out .= ' /Reference ['; // array of signature reference dictionaries
  12750. $out .= ' << /Type /SigRef';
  12751. if ($this->signature_data['cert_type'] > 0) {
  12752. $out .= ' /TransformMethod /DocMDP';
  12753. $out .= ' /TransformParams <<';
  12754. $out .= ' /Type /TransformParams';
  12755. $out .= ' /P '.$this->signature_data['cert_type'];
  12756. $out .= ' /V /1.2';
  12757. } else {
  12758. $out .= ' /TransformMethod /UR3';
  12759. $out .= ' /TransformParams <<';
  12760. $out .= ' /Type /TransformParams';
  12761. $out .= ' /V /2.2';
  12762. if (!TCPDF_STATIC::empty_string($this->ur['document'])) {
  12763. $out .= ' /Document['.$this->ur['document'].']';
  12764. }
  12765. if (!TCPDF_STATIC::empty_string($this->ur['form'])) {
  12766. $out .= ' /Form['.$this->ur['form'].']';
  12767. }
  12768. if (!TCPDF_STATIC::empty_string($this->ur['signature'])) {
  12769. $out .= ' /Signature['.$this->ur['signature'].']';
  12770. }
  12771. if (!TCPDF_STATIC::empty_string($this->ur['annots'])) {
  12772. $out .= ' /Annots['.$this->ur['annots'].']';
  12773. }
  12774. if (!TCPDF_STATIC::empty_string($this->ur['ef'])) {
  12775. $out .= ' /EF['.$this->ur['ef'].']';
  12776. }
  12777. if (!TCPDF_STATIC::empty_string($this->ur['formex'])) {
  12778. $out .= ' /FormEX['.$this->ur['formex'].']';
  12779. }
  12780. }
  12781. $out .= ' >>'; // close TransformParams
  12782. // optional digest data (values must be calculated and replaced later)
  12783. //$out .= ' /Data ********** 0 R';
  12784. //$out .= ' /DigestMethod/MD5';
  12785. //$out .= ' /DigestLocation[********** 34]';
  12786. //$out .= ' /DigestValue<********************************>';
  12787. $out .= ' >>';
  12788. $out .= ' ]'; // end of reference
  12789. if (isset($this->signature_data['info']['Name']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Name'])) {
  12790. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  12791. }
  12792. if (isset($this->signature_data['info']['Location']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Location'])) {
  12793. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  12794. }
  12795. if (isset($this->signature_data['info']['Reason']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Reason'])) {
  12796. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  12797. }
  12798. if (isset($this->signature_data['info']['ContactInfo']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['ContactInfo'])) {
  12799. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  12800. }
  12801. $out .= ' /M '.$this->_datestring($sigobjid, $this->doc_modification_timestamp);
  12802. $out .= ' >>';
  12803. $out .= "\n".'endobj';
  12804. $this->_out($out);
  12805. }
  12806. /**
  12807. * Set User's Rights for PDF Reader
  12808. * WARNING: This is experimental and currently do not work.
  12809. * Check the PDF Reference 8.7.1 Transform Methods,
  12810. * Table 8.105 Entries in the UR transform parameters dictionary
  12811. * @param $enable (boolean) if true enable user's rights on PDF reader
  12812. * @param $document (string) 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.
  12813. * @param $annots (string) 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.
  12814. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  12815. * @param $signature (string) 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.
  12816. * @param $ef (string) Names specifying additional usage rights for named embedded files in the document. Valid names are /Create/Delete/Modify/Import, which permit the user to perform the named operation on named embedded files
  12817. Names specifying additional embedded-files-related usage rights for the document.
  12818. * @param $formex (string) Names specifying additional form-field-related usage rights. The only valid name is BarcodePlaintext, which permits text form field data to be encoded as a plaintext two-dimensional barcode.
  12819. * @public
  12820. * @author Nicola Asuni
  12821. * @since 2.9.000 (2008-03-26)
  12822. */
  12823. public function setUserRights(
  12824. $enable=true,
  12825. $document='/FullSave',
  12826. $annots='/Create/Delete/Modify/Copy/Import/Export',
  12827. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  12828. $signature='/Modify',
  12829. $ef='/Create/Delete/Modify/Import',
  12830. $formex='') {
  12831. $this->ur['enabled'] = $enable;
  12832. $this->ur['document'] = $document;
  12833. $this->ur['annots'] = $annots;
  12834. $this->ur['form'] = $form;
  12835. $this->ur['signature'] = $signature;
  12836. $this->ur['ef'] = $ef;
  12837. $this->ur['formex'] = $formex;
  12838. if (!$this->sign) {
  12839. $this->setSignature('', '', '', '', 0, array());
  12840. }
  12841. }
  12842. /**
  12843. * Enable document signature (requires the OpenSSL Library).
  12844. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  12845. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12846. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12847. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  12848. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  12849. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  12850. * @param $private_key_password (string) password
  12851. * @param $extracerts (string) 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.
  12852. * @param $cert_type (int) 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.
  12853. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  12854. * @public
  12855. * @author Nicola Asuni
  12856. * @since 4.6.005 (2009-04-24)
  12857. */
  12858. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  12859. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12860. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12861. // to convert pfx certificate to pem: openssl
  12862. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  12863. $this->sign = true;
  12864. ++$this->n;
  12865. $this->sig_obj_id = $this->n; // signature widget
  12866. ++$this->n; // signature object ($this->sig_obj_id + 1)
  12867. $this->signature_data = array();
  12868. if (strlen($signing_cert) == 0) {
  12869. $this->Error('Please provide a certificate file and password!');
  12870. }
  12871. if (strlen($private_key) == 0) {
  12872. $private_key = $signing_cert;
  12873. }
  12874. $this->signature_data['signcert'] = $signing_cert;
  12875. $this->signature_data['privkey'] = $private_key;
  12876. $this->signature_data['password'] = $private_key_password;
  12877. $this->signature_data['extracerts'] = $extracerts;
  12878. $this->signature_data['cert_type'] = $cert_type;
  12879. $this->signature_data['info'] = $info;
  12880. }
  12881. /**
  12882. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  12883. * @param $x (float) Abscissa of the upper-left corner.
  12884. * @param $y (float) Ordinate of the upper-left corner.
  12885. * @param $w (float) Width of the signature area.
  12886. * @param $h (float) Height of the signature area.
  12887. * @param $page (int) option page number (if < 0 the current page is used).
  12888. * @param $name (string) Name of the signature.
  12889. * @public
  12890. * @author Nicola Asuni
  12891. * @since 5.3.011 (2010-06-17)
  12892. */
  12893. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12894. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12895. }
  12896. /**
  12897. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  12898. * @param $x (float) Abscissa of the upper-left corner.
  12899. * @param $y (float) Ordinate of the upper-left corner.
  12900. * @param $w (float) Width of the signature area.
  12901. * @param $h (float) Height of the signature area.
  12902. * @param $page (int) option page number (if < 0 the current page is used).
  12903. * @param $name (string) Name of the signature.
  12904. * @public
  12905. * @author Nicola Asuni
  12906. * @since 5.9.101 (2011-07-06)
  12907. */
  12908. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12909. ++$this->n;
  12910. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12911. }
  12912. /**
  12913. * Get the array that defines the signature appearance (page and rectangle coordinates).
  12914. * @param $x (float) Abscissa of the upper-left corner.
  12915. * @param $y (float) Ordinate of the upper-left corner.
  12916. * @param $w (float) Width of the signature area.
  12917. * @param $h (float) Height of the signature area.
  12918. * @param $page (int) option page number (if < 0 the current page is used).
  12919. * @param $name (string) Name of the signature.
  12920. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  12921. * @protected
  12922. * @author Nicola Asuni
  12923. * @since 5.9.101 (2011-07-06)
  12924. */
  12925. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12926. $sigapp = array();
  12927. if (($page < 1) OR ($page > $this->numpages)) {
  12928. $sigapp['page'] = $this->page;
  12929. } else {
  12930. $sigapp['page'] = intval($page);
  12931. }
  12932. if (empty($name)) {
  12933. $sigapp['name'] = 'Signature';
  12934. } else {
  12935. $sigapp['name'] = $name;
  12936. }
  12937. $a = $x * $this->k;
  12938. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  12939. $c = $w * $this->k;
  12940. $d = $h * $this->k;
  12941. $sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
  12942. return $sigapp;
  12943. }
  12944. /**
  12945. * Create a new page group.
  12946. * NOTE: call this function before calling AddPage()
  12947. * @param $page (int) starting group page (leave empty for next page).
  12948. * @public
  12949. * @since 3.0.000 (2008-03-27)
  12950. */
  12951. public function startPageGroup($page='') {
  12952. if (empty($page)) {
  12953. $page = $this->page + 1;
  12954. }
  12955. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  12956. }
  12957. /**
  12958. * Set the starting page number.
  12959. * @param $num (int) Starting page number.
  12960. * @since 5.9.093 (2011-06-16)
  12961. * @public
  12962. */
  12963. public function setStartingPageNumber($num=1) {
  12964. $this->starting_page_number = max(0, intval($num));
  12965. }
  12966. /**
  12967. * Returns the string alias used right align page numbers.
  12968. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  12969. * @return string
  12970. * @since 5.9.099 (2011-06-27)
  12971. * @public
  12972. */
  12973. public function getAliasRightShift() {
  12974. // calculate aproximatively the ratio between widths of aliases and replacements.
  12975. $ref = '{'.TCPDF_STATIC::$alias_right_shift.'}{'.TCPDF_STATIC::$alias_tot_pages.'}{'.TCPDF_STATIC::$alias_num_page.'}';
  12976. $rep = str_repeat(' ', $this->GetNumChars($ref));
  12977. $wrep = $this->GetStringWidth($rep);
  12978. if ($wrep > 0) {
  12979. $wdiff = max(1, ($this->GetStringWidth($ref) / $wrep));
  12980. } else {
  12981. $wdiff = 1;
  12982. }
  12983. $sdiff = sprintf('%F', $wdiff);
  12984. $alias = TCPDF_STATIC::$alias_right_shift.$sdiff.'}';
  12985. if ($this->isUnicodeFont()) {
  12986. $alias = '{'.$alias;
  12987. }
  12988. return $alias;
  12989. }
  12990. /**
  12991. * Returns the string alias used for the total number of pages.
  12992. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  12993. * This alias will be replaced by the total number of pages in the document.
  12994. * @return string
  12995. * @since 4.0.018 (2008-08-08)
  12996. * @public
  12997. */
  12998. public function getAliasNbPages() {
  12999. if ($this->isUnicodeFont()) {
  13000. return '{'.TCPDF_STATIC::$alias_tot_pages.'}';
  13001. }
  13002. return TCPDF_STATIC::$alias_tot_pages;
  13003. }
  13004. /**
  13005. * Returns the string alias used for the page number.
  13006. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13007. * This alias will be replaced by the page number.
  13008. * @return string
  13009. * @since 4.5.000 (2009-01-02)
  13010. * @public
  13011. */
  13012. public function getAliasNumPage() {
  13013. if ($this->isUnicodeFont()) {
  13014. return '{'.TCPDF_STATIC::$alias_num_page.'}';
  13015. }
  13016. return TCPDF_STATIC::$alias_num_page;
  13017. }
  13018. /**
  13019. * Return the alias for the total number of pages in the current page group.
  13020. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13021. * This alias will be replaced by the total number of pages in this group.
  13022. * @return alias of the current page group
  13023. * @public
  13024. * @since 3.0.000 (2008-03-27)
  13025. */
  13026. public function getPageGroupAlias() {
  13027. if ($this->isUnicodeFont()) {
  13028. return '{'.TCPDF_STATIC::$alias_group_tot_pages.'}';
  13029. }
  13030. return TCPDF_STATIC::$alias_group_tot_pages;
  13031. }
  13032. /**
  13033. * Return the alias for the page number on the current page group.
  13034. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13035. * This alias will be replaced by the page number (relative to the belonging group).
  13036. * @return alias of the current page group
  13037. * @public
  13038. * @since 4.5.000 (2009-01-02)
  13039. */
  13040. public function getPageNumGroupAlias() {
  13041. if ($this->isUnicodeFont()) {
  13042. return '{'.TCPDF_STATIC::$alias_group_num_page.'}';
  13043. }
  13044. return TCPDF_STATIC::$alias_group_num_page;
  13045. }
  13046. /**
  13047. * Return the current page in the group.
  13048. * @return current page in the group
  13049. * @public
  13050. * @since 3.0.000 (2008-03-27)
  13051. */
  13052. public function getGroupPageNo() {
  13053. return $this->pagegroups[$this->currpagegroup];
  13054. }
  13055. /**
  13056. * Returns the current group page number formatted as a string.
  13057. * @public
  13058. * @since 4.3.003 (2008-11-18)
  13059. * @see PaneNo(), formatPageNumber()
  13060. */
  13061. public function getGroupPageNoFormatted() {
  13062. return TCPDF_STATIC::formatPageNumber($this->getGroupPageNo());
  13063. }
  13064. /**
  13065. * Returns the current page number formatted as a string.
  13066. * @public
  13067. * @since 4.2.005 (2008-11-06)
  13068. * @see PaneNo(), formatPageNumber()
  13069. */
  13070. public function PageNoFormatted() {
  13071. return TCPDF_STATIC::formatPageNumber($this->PageNo());
  13072. }
  13073. /**
  13074. * Put pdf layers.
  13075. * @protected
  13076. * @since 3.0.000 (2008-03-27)
  13077. */
  13078. protected function _putocg() {
  13079. if (empty($this->pdflayers)) {
  13080. return;
  13081. }
  13082. foreach ($this->pdflayers as $key => $layer) {
  13083. $this->pdflayers[$key]['objid'] = $this->_newobj();
  13084. $out = '<< /Type /OCG';
  13085. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  13086. $out .= ' /Usage <<';
  13087. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  13088. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  13089. $out .= ' >> >>';
  13090. $out .= "\n".'endobj';
  13091. $this->_out($out);
  13092. }
  13093. }
  13094. /**
  13095. * Start a new pdf layer.
  13096. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  13097. * @param $print (boolean) Set to true to print this layer.
  13098. * @param $view (boolean) Set to true to view this layer.
  13099. * @public
  13100. * @since 5.9.102 (2011-07-13)
  13101. */
  13102. public function startLayer($name='', $print=true, $view=true) {
  13103. if ($this->state != 2) {
  13104. return;
  13105. }
  13106. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  13107. if (empty($name)) {
  13108. $name = $layer;
  13109. } else {
  13110. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  13111. }
  13112. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
  13113. $this->openMarkedContent = true;
  13114. $this->_out('/OC /'.$layer.' BDC');
  13115. }
  13116. /**
  13117. * End the current PDF layer.
  13118. * @public
  13119. * @since 5.9.102 (2011-07-13)
  13120. */
  13121. public function endLayer() {
  13122. if ($this->state != 2) {
  13123. return;
  13124. }
  13125. if ($this->openMarkedContent) {
  13126. // close existing open marked-content layer
  13127. $this->_out('EMC');
  13128. $this->openMarkedContent = false;
  13129. }
  13130. }
  13131. /**
  13132. * Set the visibility of the successive elements.
  13133. * This can be useful, for instance, to put a background
  13134. * image or color that will show on screen but won't print.
  13135. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  13136. * @public
  13137. * @since 3.0.000 (2008-03-27)
  13138. */
  13139. public function setVisibility($v) {
  13140. if ($this->state != 2) {
  13141. return;
  13142. }
  13143. $this->endLayer();
  13144. switch($v) {
  13145. case 'print': {
  13146. $this->startLayer('Print', true, false);
  13147. break;
  13148. }
  13149. case 'view':
  13150. case 'screen': {
  13151. $this->startLayer('View', false, true);
  13152. break;
  13153. }
  13154. case 'all': {
  13155. $this->_out('');
  13156. break;
  13157. }
  13158. default: {
  13159. $this->Error('Incorrect visibility: '.$v);
  13160. break;
  13161. }
  13162. }
  13163. }
  13164. /**
  13165. * Add transparency parameters to the current extgstate
  13166. * @param $parms (array) parameters
  13167. * @return the number of extgstates
  13168. * @protected
  13169. * @since 3.0.000 (2008-03-27)
  13170. */
  13171. protected function addExtGState($parms) {
  13172. if ($this->pdfa_mode) {
  13173. // transparencies are not allowed in PDF/A mode
  13174. return;
  13175. }
  13176. // check if this ExtGState already exist
  13177. foreach ($this->extgstates as $i => $ext) {
  13178. if ($ext['parms'] == $parms) {
  13179. if ($this->inxobj) {
  13180. // we are inside an XObject template
  13181. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  13182. }
  13183. // return reference to existing ExtGState
  13184. return $i;
  13185. }
  13186. }
  13187. $n = (count($this->extgstates) + 1);
  13188. $this->extgstates[$n] = array('parms' => $parms);
  13189. if ($this->inxobj) {
  13190. // we are inside an XObject template
  13191. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  13192. }
  13193. return $n;
  13194. }
  13195. /**
  13196. * Add an extgstate
  13197. * @param $gs (array) extgstate
  13198. * @protected
  13199. * @since 3.0.000 (2008-03-27)
  13200. */
  13201. protected function setExtGState($gs) {
  13202. if ($this->pdfa_mode OR ($this->state != 2)) {
  13203. // transparency is not allowed in PDF/A mode
  13204. return;
  13205. }
  13206. $this->_out(sprintf('/GS%d gs', $gs));
  13207. }
  13208. /**
  13209. * Put extgstates for object transparency
  13210. * @protected
  13211. * @since 3.0.000 (2008-03-27)
  13212. */
  13213. protected function _putextgstates() {
  13214. foreach ($this->extgstates as $i => $ext) {
  13215. $this->extgstates[$i]['n'] = $this->_newobj();
  13216. $out = '<< /Type /ExtGState';
  13217. foreach ($ext['parms'] as $k => $v) {
  13218. if (is_float($v)) {
  13219. $v = sprintf('%F', $v);
  13220. } elseif ($v === true) {
  13221. $v = 'true';
  13222. } elseif ($v === false) {
  13223. $v = 'false';
  13224. }
  13225. $out .= ' /'.$k.' '.$v;
  13226. }
  13227. $out .= ' >>';
  13228. $out .= "\n".'endobj';
  13229. $this->_out($out);
  13230. }
  13231. }
  13232. /**
  13233. * Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
  13234. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13235. * @param $stroking (boolean) If true apply overprint for stroking operations.
  13236. * @param $nonstroking (boolean) If true apply overprint for painting operations other than stroking.
  13237. * @param $mode (integer) Overprint mode: (0 = each source colour component value replaces the value previously painted for the corresponding device colorant; 1 = a tint value of 0.0 for a source colour component shall leave the corresponding component of the previously painted colour unchanged).
  13238. * @public
  13239. * @since 5.9.152 (2012-03-23)
  13240. */
  13241. public function setOverprint($stroking=true, $nonstroking='', $mode=0) {
  13242. if ($this->state != 2) {
  13243. return;
  13244. }
  13245. $stroking = $stroking ? true : false;
  13246. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13247. // default value if not set
  13248. $nonstroking = $stroking;
  13249. } else {
  13250. $nonstroking = $nonstroking ? true : false;
  13251. }
  13252. if (($mode != 0) AND ($mode != 1)) {
  13253. $mode = 0;
  13254. }
  13255. $this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
  13256. $gs = $this->addExtGState($this->overprint);
  13257. $this->setExtGState($gs);
  13258. }
  13259. /**
  13260. * Get the overprint mode array (OP, op, OPM).
  13261. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13262. * @return array.
  13263. * @public
  13264. * @since 5.9.152 (2012-03-23)
  13265. */
  13266. public function getOverprint() {
  13267. return $this->overprint;
  13268. }
  13269. /**
  13270. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  13271. * @param $stroking (float) Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
  13272. * @param $bm (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  13273. * @param $nonstroking (float) Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
  13274. * @param $ais (boolean)
  13275. * @public
  13276. * @since 3.0.000 (2008-03-27)
  13277. */
  13278. public function setAlpha($stroking=1, $bm='Normal', $nonstroking='', $ais=false) {
  13279. if ($this->pdfa_mode) {
  13280. // transparency is not allowed in PDF/A mode
  13281. return;
  13282. }
  13283. $stroking = floatval($stroking);
  13284. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13285. // default value if not set
  13286. $nonstroking = $stroking;
  13287. } else {
  13288. $nonstroking = floatval($nonstroking);
  13289. }
  13290. if ($bm[0] == '/') {
  13291. // remove trailing slash
  13292. $bm = substr($bm, 1);
  13293. }
  13294. if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  13295. $bm = 'Normal';
  13296. }
  13297. $ais = $ais ? true : false;
  13298. $this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/'.$bm, 'AIS' => $ais);
  13299. $gs = $this->addExtGState($this->alpha);
  13300. $this->setExtGState($gs);
  13301. }
  13302. /**
  13303. * Get the alpha mode array (CA, ca, BM, AIS).
  13304. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13305. * @return array.
  13306. * @public
  13307. * @since 5.9.152 (2012-03-23)
  13308. */
  13309. public function getAlpha() {
  13310. return $this->alpha;
  13311. }
  13312. /**
  13313. * Set the default JPEG compression quality (1-100)
  13314. * @param $quality (int) JPEG quality, integer between 1 and 100
  13315. * @public
  13316. * @since 3.0.000 (2008-03-27)
  13317. */
  13318. public function setJPEGQuality($quality) {
  13319. if (($quality < 1) OR ($quality > 100)) {
  13320. $quality = 75;
  13321. }
  13322. $this->jpeg_quality = intval($quality);
  13323. }
  13324. /**
  13325. * Set the default number of columns in a row for HTML tables.
  13326. * @param $cols (int) number of columns
  13327. * @public
  13328. * @since 3.0.014 (2008-06-04)
  13329. */
  13330. public function setDefaultTableColumns($cols=4) {
  13331. $this->default_table_columns = intval($cols);
  13332. }
  13333. /**
  13334. * Set the height of the cell (line height) respect the font height.
  13335. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  13336. * @public
  13337. * @since 3.0.014 (2008-06-04)
  13338. */
  13339. public function setCellHeightRatio($h) {
  13340. $this->cell_height_ratio = $h;
  13341. }
  13342. /**
  13343. * return the height of cell repect font height.
  13344. * @public
  13345. * @since 4.0.012 (2008-07-24)
  13346. */
  13347. public function getCellHeightRatio() {
  13348. return $this->cell_height_ratio;
  13349. }
  13350. /**
  13351. * Set the PDF version (check PDF reference for valid values).
  13352. * @param $version (string) PDF document version.
  13353. * @public
  13354. * @since 3.1.000 (2008-06-09)
  13355. */
  13356. public function setPDFVersion($version='1.7') {
  13357. if ($this->pdfa_mode) {
  13358. // PDF/A mode
  13359. $this->PDFVersion = '1.4';
  13360. } else {
  13361. $this->PDFVersion = $version;
  13362. }
  13363. }
  13364. /**
  13365. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen