PageRenderTime 134ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 3ms

/lib/tcpdf/tcpdf.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 13905 lines | 8388 code | 560 blank | 4957 comment | 1723 complexity | cd6cd854a5768f9f5716d4ad9c3ac6b9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 6.0.031
  5. // Begin : 2002-08-03
  6. // Last Update : 2013-09-15
  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. // THANKS TO:
  71. //
  72. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  73. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  74. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  75. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  76. // dullus for text Justification.
  77. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  78. // Patrick Benny for text stretch suggestion on Cell().
  79. // Johannes Güntert for JavaScript support.
  80. // Denis Van Nuffelen for Dynamic Form.
  81. // Jacek Czekaj for multibyte justification
  82. // Anthony Ferrara for the reintroduction of legacy image methods.
  83. // Sourceforge user 1707880 (hucste) for line-through mode.
  84. // Larry Stanbery for page groups.
  85. // Martin Hall-May for transparency.
  86. // Aaron C. Spike for Polycurve method.
  87. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  88. // Moritz Wagner and Andreas Wurmser for graphic functions.
  89. // Andrew Whitehead for core fonts support.
  90. // Esteban Joël Marín for OpenType font conversion.
  91. // Teus Hagen for several suggestions and fixes.
  92. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  93. // Kosmas Papachristos for some CSS improvements.
  94. // Marcel Partap for some fixes.
  95. // Won Kyu Park for several suggestions, fixes and patches.
  96. // Dominik Dzienia for QR-code support.
  97. // Laurent Minguet for some suggestions.
  98. // Christian Deligant for some suggestions and fixes.
  99. // Travis Harris for crop mark suggestion.
  100. // Aleksey Kuznetsov for some suggestions and text shadows.
  101. // Jim Hanlon for several suggestions and patches.
  102. // Anyone else that has reported a bug or sent a suggestion.
  103. //============================================================+
  104. /**
  105. * @file
  106. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  107. * 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>
  108. * <h3>TCPDF main features are:</h3>
  109. * <ul>
  110. * <li>no external libraries are required for the basic functions;</li>
  111. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  112. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  113. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  114. * <li>font subsetting;</li>
  115. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  116. * <li>images, graphic (geometric figures) and transformation methods;
  117. * <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>
  118. * <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>
  119. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  120. * <li>automatic page header and footer management;</li>
  121. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  122. * <li>transactions to UNDO commands;</li>
  123. * <li>PDF annotations, including links, text and file attachments;</li>
  124. * <li>text rendering modes (fill, stroke and clipping);</li>
  125. * <li>multiple columns mode;</li>
  126. * <li>no-write page regions;</li>
  127. * <li>bookmarks, named destinations and table of content;</li>
  128. * <li>text hyphenation;</li>
  129. * <li>text stretching and spacing (tracking);</li>
  130. * <li>automatic page break, line break and text alignments including justification;</li>
  131. * <li>automatic page numbering and page groups;</li>
  132. * <li>move and delete pages;</li>
  133. * <li>page compression (requires php-zlib extension);</li>
  134. * <li>XOBject Templates;</li>
  135. * <li>Layers and object visibility;</li>
  136. * <li>PDF/A-1b support.</li>
  137. * </ul>
  138. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  139. * @package com.tecnick.tcpdf
  140. * @author Nicola Asuni
  141. * @version 6.0.031
  142. */
  143. // TCPDF configuration
  144. require_once(dirname(__FILE__).'/tcpdf_autoconfig.php');
  145. // TCPDF static font methods and data
  146. require_once(dirname(__FILE__).'/include/tcpdf_font_data.php');
  147. // TCPDF static font methods and data
  148. require_once(dirname(__FILE__).'/include/tcpdf_fonts.php');
  149. // TCPDF static color methods and data
  150. require_once(dirname(__FILE__).'/include/tcpdf_colors.php');
  151. // TCPDF static image methods and data
  152. require_once(dirname(__FILE__).'/include/tcpdf_images.php');
  153. // TCPDF static methods and data
  154. require_once(dirname(__FILE__).'/include/tcpdf_static.php');
  155. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  156. /**
  157. * @class TCPDF
  158. * PHP class for generating PDF documents without requiring external extensions.
  159. * 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>
  160. * @package com.tecnick.tcpdf
  161. * @brief PHP class for generating PDF documents without requiring external extensions.
  162. * @version 6.0.031
  163. * @author Nicola Asuni - info@tecnick.com
  164. */
  165. class TCPDF {
  166. // Protected properties
  167. /**
  168. * Current page number.
  169. * @protected
  170. */
  171. protected $page;
  172. /**
  173. * Current object number.
  174. * @protected
  175. */
  176. protected $n;
  177. /**
  178. * Array of object offsets.
  179. * @protected
  180. */
  181. protected $offsets = array();
  182. /**
  183. * Array of object IDs for each page.
  184. * @protected
  185. */
  186. protected $pageobjects = array();
  187. /**
  188. * Buffer holding in-memory PDF.
  189. * @protected
  190. */
  191. protected $buffer;
  192. /**
  193. * Array containing pages.
  194. * @protected
  195. */
  196. protected $pages = array();
  197. /**
  198. * Current document state.
  199. * @protected
  200. */
  201. protected $state;
  202. /**
  203. * Compression flag.
  204. * @protected
  205. */
  206. protected $compress;
  207. /**
  208. * Current page orientation (P = Portrait, L = Landscape).
  209. * @protected
  210. */
  211. protected $CurOrientation;
  212. /**
  213. * Page dimensions.
  214. * @protected
  215. */
  216. protected $pagedim = array();
  217. /**
  218. * Scale factor (number of points in user unit).
  219. * @protected
  220. */
  221. protected $k;
  222. /**
  223. * Width of page format in points.
  224. * @protected
  225. */
  226. protected $fwPt;
  227. /**
  228. * Height of page format in points.
  229. * @protected
  230. */
  231. protected $fhPt;
  232. /**
  233. * Current width of page in points.
  234. * @protected
  235. */
  236. protected $wPt;
  237. /**
  238. * Current height of page in points.
  239. * @protected
  240. */
  241. protected $hPt;
  242. /**
  243. * Current width of page in user unit.
  244. * @protected
  245. */
  246. protected $w;
  247. /**
  248. * Current height of page in user unit.
  249. * @protected
  250. */
  251. protected $h;
  252. /**
  253. * Left margin.
  254. * @protected
  255. */
  256. protected $lMargin;
  257. /**
  258. * Right margin.
  259. * @protected
  260. */
  261. protected $rMargin;
  262. /**
  263. * Cell left margin (used by regions).
  264. * @protected
  265. */
  266. protected $clMargin;
  267. /**
  268. * Cell right margin (used by regions).
  269. * @protected
  270. */
  271. protected $crMargin;
  272. /**
  273. * Top margin.
  274. * @protected
  275. */
  276. protected $tMargin;
  277. /**
  278. * Page break margin.
  279. * @protected
  280. */
  281. protected $bMargin;
  282. /**
  283. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  284. * @since 5.9.000 (2010-10-03)
  285. * @protected
  286. */
  287. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  288. /**
  289. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  290. * @since 5.9.000 (2010-10-04)
  291. * @protected
  292. */
  293. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  294. /**
  295. * Current horizontal position in user unit for cell positioning.
  296. * @protected
  297. */
  298. protected $x;
  299. /**
  300. * Current vertical position in user unit for cell positioning.
  301. * @protected
  302. */
  303. protected $y;
  304. /**
  305. * Height of last cell printed.
  306. * @protected
  307. */
  308. protected $lasth;
  309. /**
  310. * Line width in user unit.
  311. * @protected
  312. */
  313. protected $LineWidth;
  314. /**
  315. * Array of standard font names.
  316. * @protected
  317. */
  318. protected $CoreFonts;
  319. /**
  320. * Array of used fonts.
  321. * @protected
  322. */
  323. protected $fonts = array();
  324. /**
  325. * Array of font files.
  326. * @protected
  327. */
  328. protected $FontFiles = array();
  329. /**
  330. * Array of encoding differences.
  331. * @protected
  332. */
  333. protected $diffs = array();
  334. /**
  335. * Array of used images.
  336. * @protected
  337. */
  338. protected $images = array();
  339. /**
  340. * Array of cached files.
  341. * @protected
  342. */
  343. protected $cached_files = array();
  344. /**
  345. * Array of Annotations in pages.
  346. * @protected
  347. */
  348. protected $PageAnnots = array();
  349. /**
  350. * Array of internal links.
  351. * @protected
  352. */
  353. protected $links = array();
  354. /**
  355. * Current font family.
  356. * @protected
  357. */
  358. protected $FontFamily;
  359. /**
  360. * Current font style.
  361. * @protected
  362. */
  363. protected $FontStyle;
  364. /**
  365. * Current font ascent (distance between font top and baseline).
  366. * @protected
  367. * @since 2.8.000 (2007-03-29)
  368. */
  369. protected $FontAscent;
  370. /**
  371. * Current font descent (distance between font bottom and baseline).
  372. * @protected
  373. * @since 2.8.000 (2007-03-29)
  374. */
  375. protected $FontDescent;
  376. /**
  377. * Underlining flag.
  378. * @protected
  379. */
  380. protected $underline;
  381. /**
  382. * Overlining flag.
  383. * @protected
  384. */
  385. protected $overline;
  386. /**
  387. * Current font info.
  388. * @protected
  389. */
  390. protected $CurrentFont;
  391. /**
  392. * Current font size in points.
  393. * @protected
  394. */
  395. protected $FontSizePt;
  396. /**
  397. * Current font size in user unit.
  398. * @protected
  399. */
  400. protected $FontSize;
  401. /**
  402. * Commands for drawing color.
  403. * @protected
  404. */
  405. protected $DrawColor;
  406. /**
  407. * Commands for filling color.
  408. * @protected
  409. */
  410. protected $FillColor;
  411. /**
  412. * Commands for text color.
  413. * @protected
  414. */
  415. protected $TextColor;
  416. /**
  417. * Indicates whether fill and text colors are different.
  418. * @protected
  419. */
  420. protected $ColorFlag;
  421. /**
  422. * Automatic page breaking.
  423. * @protected
  424. */
  425. protected $AutoPageBreak;
  426. /**
  427. * Threshold used to trigger page breaks.
  428. * @protected
  429. */
  430. protected $PageBreakTrigger;
  431. /**
  432. * Flag set when processing page header.
  433. * @protected
  434. */
  435. protected $InHeader = false;
  436. /**
  437. * Flag set when processing page footer.
  438. * @protected
  439. */
  440. protected $InFooter = false;
  441. /**
  442. * Zoom display mode.
  443. * @protected
  444. */
  445. protected $ZoomMode;
  446. /**
  447. * Layout display mode.
  448. * @protected
  449. */
  450. protected $LayoutMode;
  451. /**
  452. * If true set the document information dictionary in Unicode.
  453. * @protected
  454. */
  455. protected $docinfounicode = true;
  456. /**
  457. * Document title.
  458. * @protected
  459. */
  460. protected $title = '';
  461. /**
  462. * Document subject.
  463. * @protected
  464. */
  465. protected $subject = '';
  466. /**
  467. * Document author.
  468. * @protected
  469. */
  470. protected $author = '';
  471. /**
  472. * Document keywords.
  473. * @protected
  474. */
  475. protected $keywords = '';
  476. /**
  477. * Document creator.
  478. * @protected
  479. */
  480. protected $creator = '';
  481. /**
  482. * Starting page number.
  483. * @protected
  484. */
  485. protected $starting_page_number = 1;
  486. /**
  487. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  488. * @since 2002-07-31
  489. * @author Nicola Asuni
  490. * @protected
  491. */
  492. protected $img_rb_x;
  493. /**
  494. * The right-bottom corner Y coordinate of last inserted image.
  495. * @since 2002-07-31
  496. * @author Nicola Asuni
  497. * @protected
  498. */
  499. protected $img_rb_y;
  500. /**
  501. * Adjusting factor to convert pixels to user units.
  502. * @since 2004-06-14
  503. * @author Nicola Asuni
  504. * @protected
  505. */
  506. protected $imgscale = 1;
  507. /**
  508. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  509. * @since 2005-01-02
  510. * @author Nicola Asuni
  511. * @protected
  512. */
  513. protected $isunicode = false;
  514. /**
  515. * PDF version.
  516. * @since 1.5.3
  517. * @protected
  518. */
  519. protected $PDFVersion = '1.7';
  520. /**
  521. * ID of the stored default header template (-1 = not set).
  522. * @protected
  523. */
  524. protected $header_xobjid = -1;
  525. /**
  526. * If true reset the Header Xobject template at each page
  527. * @protected
  528. */
  529. protected $header_xobj_autoreset = false;
  530. /**
  531. * Minimum distance between header and top page margin.
  532. * @protected
  533. */
  534. protected $header_margin;
  535. /**
  536. * Minimum distance between footer and bottom page margin.
  537. * @protected
  538. */
  539. protected $footer_margin;
  540. /**
  541. * Original left margin value.
  542. * @protected
  543. * @since 1.53.0.TC013
  544. */
  545. protected $original_lMargin;
  546. /**
  547. * Original right margin value.
  548. * @protected
  549. * @since 1.53.0.TC013
  550. */
  551. protected $original_rMargin;
  552. /**
  553. * Default font used on page header.
  554. * @protected
  555. */
  556. protected $header_font;
  557. /**
  558. * Default font used on page footer.
  559. * @protected
  560. */
  561. protected $footer_font;
  562. /**
  563. * Language templates.
  564. * @protected
  565. */
  566. protected $l;
  567. /**
  568. * Barcode to print on page footer (only if set).
  569. * @protected
  570. */
  571. protected $barcode = false;
  572. /**
  573. * Boolean flag to print/hide page header.
  574. * @protected
  575. */
  576. protected $print_header = true;
  577. /**
  578. * Boolean flag to print/hide page footer.
  579. * @protected
  580. */
  581. protected $print_footer = true;
  582. /**
  583. * Header image logo.
  584. * @protected
  585. */
  586. protected $header_logo = '';
  587. /**
  588. * Width of header image logo in user units.
  589. * @protected
  590. */
  591. protected $header_logo_width = 30;
  592. /**
  593. * Title to be printed on default page header.
  594. * @protected
  595. */
  596. protected $header_title = '';
  597. /**
  598. * String to pring on page header after title.
  599. * @protected
  600. */
  601. protected $header_string = '';
  602. /**
  603. * Color for header text (RGB array).
  604. * @since 5.9.174 (2012-07-25)
  605. * @protected
  606. */
  607. protected $header_text_color = array(0,0,0);
  608. /**
  609. * Color for header line (RGB array).
  610. * @since 5.9.174 (2012-07-25)
  611. * @protected
  612. */
  613. protected $header_line_color = array(0,0,0);
  614. /**
  615. * Color for footer text (RGB array).
  616. * @since 5.9.174 (2012-07-25)
  617. * @protected
  618. */
  619. protected $footer_text_color = array(0,0,0);
  620. /**
  621. * Color for footer line (RGB array).
  622. * @since 5.9.174 (2012-07-25)
  623. * @protected
  624. */
  625. protected $footer_line_color = array(0,0,0);
  626. /**
  627. * Text shadow data array.
  628. * @since 5.9.174 (2012-07-25)
  629. * @protected
  630. */
  631. protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
  632. /**
  633. * Default number of columns for html table.
  634. * @protected
  635. */
  636. protected $default_table_columns = 4;
  637. // variables for html parser
  638. /**
  639. * HTML PARSER: array to store current link and rendering styles.
  640. * @protected
  641. */
  642. protected $HREF = array();
  643. /**
  644. * List of available fonts on filesystem.
  645. * @protected
  646. */
  647. protected $fontlist = array();
  648. /**
  649. * Current foreground color.
  650. * @protected
  651. */
  652. protected $fgcolor;
  653. /**
  654. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  655. * @protected
  656. */
  657. protected $listordered = array();
  658. /**
  659. * HTML PARSER: array count list items on nested lists.
  660. * @protected
  661. */
  662. protected $listcount = array();
  663. /**
  664. * HTML PARSER: current list nesting level.
  665. * @protected
  666. */
  667. protected $listnum = 0;
  668. /**
  669. * HTML PARSER: indent amount for lists.
  670. * @protected
  671. */
  672. protected $listindent = 0;
  673. /**
  674. * HTML PARSER: current list indententation level.
  675. * @protected
  676. */
  677. protected $listindentlevel = 0;
  678. /**
  679. * Current background color.
  680. * @protected
  681. */
  682. protected $bgcolor;
  683. /**
  684. * Temporary font size in points.
  685. * @protected
  686. */
  687. protected $tempfontsize = 10;
  688. /**
  689. * Spacer string for LI tags.
  690. * @protected
  691. */
  692. protected $lispacer = '';
  693. /**
  694. * Default encoding.
  695. * @protected
  696. * @since 1.53.0.TC010
  697. */
  698. protected $encoding = 'UTF-8';
  699. /**
  700. * PHP internal encoding.
  701. * @protected
  702. * @since 1.53.0.TC016
  703. */
  704. protected $internal_encoding;
  705. /**
  706. * Boolean flag to indicate if the document language is Right-To-Left.
  707. * @protected
  708. * @since 2.0.000
  709. */
  710. protected $rtl = false;
  711. /**
  712. * Boolean flag used to force RTL or LTR string direction.
  713. * @protected
  714. * @since 2.0.000
  715. */
  716. protected $tmprtl = false;
  717. // --- Variables used for document encryption:
  718. /**
  719. * IBoolean flag indicating whether document is protected.
  720. * @protected
  721. * @since 2.0.000 (2008-01-02)
  722. */
  723. protected $encrypted;
  724. /**
  725. * Array containing encryption settings.
  726. * @protected
  727. * @since 5.0.005 (2010-05-11)
  728. */
  729. protected $encryptdata = array();
  730. /**
  731. * Last RC4 key encrypted (cached for optimisation).
  732. * @protected
  733. * @since 2.0.000 (2008-01-02)
  734. */
  735. protected $last_enc_key;
  736. /**
  737. * Last RC4 computed key.
  738. * @protected
  739. * @since 2.0.000 (2008-01-02)
  740. */
  741. protected $last_enc_key_c;
  742. /**
  743. * File ID (used on document trailer).
  744. * @protected
  745. * @since 5.0.005 (2010-05-12)
  746. */
  747. protected $file_id;
  748. // --- bookmark ---
  749. /**
  750. * Outlines for bookmark.
  751. * @protected
  752. * @since 2.1.002 (2008-02-12)
  753. */
  754. protected $outlines = array();
  755. /**
  756. * Outline root for bookmark.
  757. * @protected
  758. * @since 2.1.002 (2008-02-12)
  759. */
  760. protected $OutlineRoot;
  761. // --- javascript and form ---
  762. /**
  763. * Javascript code.
  764. * @protected
  765. * @since 2.1.002 (2008-02-12)
  766. */
  767. protected $javascript = '';
  768. /**
  769. * Javascript counter.
  770. * @protected
  771. * @since 2.1.002 (2008-02-12)
  772. */
  773. protected $n_js;
  774. /**
  775. * line through state
  776. * @protected
  777. * @since 2.8.000 (2008-03-19)
  778. */
  779. protected $linethrough;
  780. /**
  781. * Array with additional document-wide usage rights for the document.
  782. * @protected
  783. * @since 5.8.014 (2010-08-23)
  784. */
  785. protected $ur = array();
  786. /**
  787. * DPI (Dot Per Inch) Document Resolution (do not change).
  788. * @protected
  789. * @since 3.0.000 (2008-03-27)
  790. */
  791. protected $dpi = 72;
  792. /**
  793. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  794. * @protected
  795. * @since 3.0.000 (2008-03-27)
  796. */
  797. protected $newpagegroup = array();
  798. /**
  799. * Array that contains the number of pages in each page group.
  800. * @protected
  801. * @since 3.0.000 (2008-03-27)
  802. */
  803. protected $pagegroups = array();
  804. /**
  805. * Current page group number.
  806. * @protected
  807. * @since 3.0.000 (2008-03-27)
  808. */
  809. protected $currpagegroup = 0;
  810. /**
  811. * Array of transparency objects and parameters.
  812. * @protected
  813. * @since 3.0.000 (2008-03-27)
  814. */
  815. protected $extgstates;
  816. /**
  817. * Set the default JPEG compression quality (1-100).
  818. * @protected
  819. * @since 3.0.000 (2008-03-27)
  820. */
  821. protected $jpeg_quality;
  822. /**
  823. * Default cell height ratio.
  824. * @protected
  825. * @since 3.0.014 (2008-05-23)
  826. */
  827. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  828. /**
  829. * PDF viewer preferences.
  830. * @protected
  831. * @since 3.1.000 (2008-06-09)
  832. */
  833. protected $viewer_preferences;
  834. /**
  835. * A name object specifying how the document should be displayed when opened.
  836. * @protected
  837. * @since 3.1.000 (2008-06-09)
  838. */
  839. protected $PageMode;
  840. /**
  841. * Array for storing gradient information.
  842. * @protected
  843. * @since 3.1.000 (2008-06-09)
  844. */
  845. protected $gradients = array();
  846. /**
  847. * Array used to store positions inside the pages buffer (keys are the page numbers).
  848. * @protected
  849. * @since 3.2.000 (2008-06-26)
  850. */
  851. protected $intmrk = array();
  852. /**
  853. * Array used to store positions inside the pages buffer (keys are the page numbers).
  854. * @protected
  855. * @since 5.7.000 (2010-08-03)
  856. */
  857. protected $bordermrk = array();
  858. /**
  859. * Array used to store page positions to track empty pages (keys are the page numbers).
  860. * @protected
  861. * @since 5.8.007 (2010-08-18)
  862. */
  863. protected $emptypagemrk = array();
  864. /**
  865. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  866. * @protected
  867. * @since 4.6.021 (2009-07-20)
  868. */
  869. protected $cntmrk = array();
  870. /**
  871. * Array used to store footer positions of each page.
  872. * @protected
  873. * @since 3.2.000 (2008-07-01)
  874. */
  875. protected $footerpos = array();
  876. /**
  877. * Array used to store footer length of each page.
  878. * @protected
  879. * @since 4.0.014 (2008-07-29)
  880. */
  881. protected $footerlen = array();
  882. /**
  883. * Boolean flag to indicate if a new line is created.
  884. * @protected
  885. * @since 3.2.000 (2008-07-01)
  886. */
  887. protected $newline = true;
  888. /**
  889. * End position of the latest inserted line.
  890. * @protected
  891. * @since 3.2.000 (2008-07-01)
  892. */
  893. protected $endlinex = 0;
  894. /**
  895. * PDF string for width value of the last line.
  896. * @protected
  897. * @since 4.0.006 (2008-07-16)
  898. */
  899. protected $linestyleWidth = '';
  900. /**
  901. * PDF string for CAP value of the last line.
  902. * @protected
  903. * @since 4.0.006 (2008-07-16)
  904. */
  905. protected $linestyleCap = '0 J';
  906. /**
  907. * PDF string for join value of the last line.
  908. * @protected
  909. * @since 4.0.006 (2008-07-16)
  910. */
  911. protected $linestyleJoin = '0 j';
  912. /**
  913. * PDF string for dash value of the last line.
  914. * @protected
  915. * @since 4.0.006 (2008-07-16)
  916. */
  917. protected $linestyleDash = '[] 0 d';
  918. /**
  919. * Boolean flag to indicate if marked-content sequence is open.
  920. * @protected
  921. * @since 4.0.013 (2008-07-28)
  922. */
  923. protected $openMarkedContent = false;
  924. /**
  925. * Count the latest inserted vertical spaces on HTML.
  926. * @protected
  927. * @since 4.0.021 (2008-08-24)
  928. */
  929. protected $htmlvspace = 0;
  930. /**
  931. * Array of Spot colors.
  932. * @protected
  933. * @since 4.0.024 (2008-09-12)
  934. */
  935. protected $spot_colors = array();
  936. /**
  937. * Symbol used for HTML unordered list items.
  938. * @protected
  939. * @since 4.0.028 (2008-09-26)
  940. */
  941. protected $lisymbol = '';
  942. /**
  943. * String used to mark the beginning and end of EPS image blocks.
  944. * @protected
  945. * @since 4.1.000 (2008-10-18)
  946. */
  947. protected $epsmarker = 'x#!#EPS#!#x';
  948. /**
  949. * Array of transformation matrix.
  950. * @protected
  951. * @since 4.2.000 (2008-10-29)
  952. */
  953. protected $transfmatrix = array();
  954. /**
  955. * Current key for transformation matrix.
  956. * @protected
  957. * @since 4.8.005 (2009-09-17)
  958. */
  959. protected $transfmatrix_key = 0;
  960. /**
  961. * Booklet mode for double-sided pages.
  962. * @protected
  963. * @since 4.2.000 (2008-10-29)
  964. */
  965. protected $booklet = false;
  966. /**
  967. * Epsilon value used for float calculations.
  968. * @protected
  969. * @since 4.2.000 (2008-10-29)
  970. */
  971. protected $feps = 0.005;
  972. /**
  973. * Array used for custom vertical spaces for HTML tags.
  974. * @protected
  975. * @since 4.2.001 (2008-10-30)
  976. */
  977. protected $tagvspaces = array();
  978. /**
  979. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  980. * @protected
  981. * @since 4.2.007 (2008-11-12)
  982. */
  983. protected $customlistindent = -1;
  984. /**
  985. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  986. * @protected
  987. * @since 4.2.010 (2008-11-14)
  988. */
  989. protected $opencell = true;
  990. /**
  991. * Array of files to embedd.
  992. * @protected
  993. * @since 4.4.000 (2008-12-07)
  994. */
  995. protected $embeddedfiles = array();
  996. /**
  997. * Boolean flag to indicate if we are inside a PRE tag.
  998. * @protected
  999. * @since 4.4.001 (2008-12-08)
  1000. */
  1001. protected $premode = false;
  1002. /**
  1003. * Array used to store positions of graphics transformation blocks inside the page buffer.
  1004. * keys are the page numbers
  1005. * @protected
  1006. * @since 4.4.002 (2008-12-09)
  1007. */
  1008. protected $transfmrk = array();
  1009. /**
  1010. * Default color for html links.
  1011. * @protected
  1012. * @since 4.4.003 (2008-12-09)
  1013. */
  1014. protected $htmlLinkColorArray = array(0, 0, 255);
  1015. /**
  1016. * Default font style to add to html links.
  1017. * @protected
  1018. * @since 4.4.003 (2008-12-09)
  1019. */
  1020. protected $htmlLinkFontStyle = 'U';
  1021. /**
  1022. * Counts the number of pages.
  1023. * @protected
  1024. * @since 4.5.000 (2008-12-31)
  1025. */
  1026. protected $numpages = 0;
  1027. /**
  1028. * Array containing page lengths in bytes.
  1029. * @protected
  1030. * @since 4.5.000 (2008-12-31)
  1031. */
  1032. protected $pagelen = array();
  1033. /**
  1034. * Counts the number of pages.
  1035. * @protected
  1036. * @since 4.5.000 (2008-12-31)
  1037. */
  1038. protected $numimages = 0;
  1039. /**
  1040. * Store the image keys.
  1041. * @protected
  1042. * @since 4.5.000 (2008-12-31)
  1043. */
  1044. protected $imagekeys = array();
  1045. /**
  1046. * Length of the buffer in bytes.
  1047. * @protected
  1048. * @since 4.5.000 (2008-12-31)
  1049. */
  1050. protected $bufferlen = 0;
  1051. /**
  1052. * If true enables disk caching.
  1053. * @protected
  1054. * @since 4.5.000 (2008-12-31)
  1055. */
  1056. protected $diskcache = false;
  1057. /**
  1058. * Counts the number of fonts.
  1059. * @protected
  1060. * @since 4.5.000 (2009-01-02)
  1061. */
  1062. protected $numfonts = 0;
  1063. /**
  1064. * Store the font keys.
  1065. * @protected
  1066. * @since 4.5.000 (2009-01-02)
  1067. */
  1068. protected $fontkeys = array();
  1069. /**
  1070. * Store the font object IDs.
  1071. * @protected
  1072. * @since 4.8.001 (2009-09-09)
  1073. */
  1074. protected $font_obj_ids = array();
  1075. /**
  1076. * Store the fage status (true when opened, false when closed).
  1077. * @protected
  1078. * @since 4.5.000 (2009-01-02)
  1079. */
  1080. protected $pageopen = array();
  1081. /**
  1082. * Default monospace font.
  1083. * @protected
  1084. * @since 4.5.025 (2009-03-10)
  1085. */
  1086. protected $default_monospaced_font = 'courier';
  1087. /**
  1088. * Cloned copy of the current class object.
  1089. * @protected
  1090. * @since 4.5.029 (2009-03-19)
  1091. */
  1092. protected $objcopy;
  1093. /**
  1094. * Array used to store the lengths of cache files.
  1095. * @protected
  1096. * @since 4.5.029 (2009-03-19)
  1097. */
  1098. protected $cache_file_length = array();
  1099. /**
  1100. * Table header content to be repeated on each new page.
  1101. * @protected
  1102. * @since 4.5.030 (2009-03-20)
  1103. */
  1104. protected $thead = '';
  1105. /**
  1106. * Margins used for table header.
  1107. * @protected
  1108. * @since 4.5.030 (2009-03-20)
  1109. */
  1110. protected $theadMargins = array();
  1111. /**
  1112. * Boolean flag to enable document digital signature.
  1113. * @protected
  1114. * @since 4.6.005 (2009-04-24)
  1115. */
  1116. protected $sign = false;
  1117. /**
  1118. * Digital signature data.
  1119. * @protected
  1120. * @since 4.6.005 (2009-04-24)
  1121. */
  1122. protected $signature_data = array();
  1123. /**
  1124. * Digital signature max length.
  1125. * @protected
  1126. * @since 4.6.005 (2009-04-24)
  1127. */
  1128. protected $signature_max_length = 11742;
  1129. /**
  1130. * Data for digital signature appearance.
  1131. * @protected
  1132. * @since 5.3.011 (2010-06-16)
  1133. */
  1134. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1135. /**
  1136. * Array of empty digital signature appearances.
  1137. * @protected
  1138. * @since 5.9.101 (2011-07-06)
  1139. */
  1140. protected $empty_signature_appearance = array();
  1141. /**
  1142. * Regular expression used to find blank characters (required for word-wrapping).
  1143. * @protected
  1144. * @since 4.6.006 (2009-04-28)
  1145. */
  1146. protected $re_spaces = '/[^\S\xa0]/';
  1147. /**
  1148. * Array of $re_spaces parts.
  1149. * @protected
  1150. * @since 5.5.011 (2010-07-09)
  1151. */
  1152. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1153. /**
  1154. * Digital signature object ID.
  1155. * @protected
  1156. * @since 4.6.022 (2009-06-23)
  1157. */
  1158. protected $sig_obj_id = 0;
  1159. /**
  1160. * ID of page objects.
  1161. * @protected
  1162. * @since 4.7.000 (2009-08-29)
  1163. */
  1164. protected $page_obj_id = array();
  1165. /**
  1166. * List of form annotations IDs.
  1167. * @protected
  1168. * @since 4.8.000 (2009-09-07)
  1169. */
  1170. protected $form_obj_id = array();
  1171. /**
  1172. * 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.
  1173. * @protected
  1174. * @since 4.8.000 (2009-09-07)
  1175. */
  1176. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1177. /**
  1178. * Javascript objects array.
  1179. * @protected
  1180. * @since 4.8.000 (2009-09-07)
  1181. */
  1182. protected $js_objects = array();
  1183. /**
  1184. * Current form action (used during XHTML rendering).
  1185. * @protected
  1186. * @since 4.8.000 (2009-09-07)
  1187. */
  1188. protected $form_action = '';
  1189. /**
  1190. * Current form encryption type (used during XHTML rendering).
  1191. * @protected
  1192. * @since 4.8.000 (2009-09-07)
  1193. */
  1194. protected $form_enctype = 'application/x-www-form-urlencoded';
  1195. /**
  1196. * Current method to submit forms.
  1197. * @protected
  1198. * @since 4.8.000 (2009-09-07)
  1199. */
  1200. protected $form_mode = 'post';
  1201. /**
  1202. * List of fonts used on form fields (fontname => fontkey).
  1203. * @protected
  1204. * @since 4.8.001 (2009-09-09)
  1205. */
  1206. protected $annotation_fonts = array();
  1207. /**
  1208. * List of radio buttons parent objects.
  1209. * @protected
  1210. * @since 4.8.001 (2009-09-09)
  1211. */
  1212. protected $radiobutton_groups = array();
  1213. /**
  1214. * List of radio group objects IDs.
  1215. * @protected
  1216. * @since 4.8.001 (2009-09-09)
  1217. */
  1218. protected $radio_groups = array();
  1219. /**
  1220. * Text indentation value (used for text-indent CSS attribute).
  1221. * @protected
  1222. * @since 4.8.006 (2009-09-23)
  1223. */
  1224. protected $textindent = 0;
  1225. /**
  1226. * Store page number when startTransaction() is called.
  1227. * @protected
  1228. * @since 4.8.006 (2009-09-23)
  1229. */
  1230. protected $start_transaction_page = 0;
  1231. /**
  1232. * Store Y position when startTransaction() is called.
  1233. * @protected
  1234. * @since 4.9.001 (2010-03-28)
  1235. */
  1236. protected $start_transaction_y = 0;
  1237. /**
  1238. * True when we are printing the thead section on a new page.
  1239. * @protected
  1240. * @since 4.8.027 (2010-01-25)
  1241. */
  1242. protected $inthead = false;
  1243. /**
  1244. * Array of column measures (width, space, starting Y position).
  1245. * @protected
  1246. * @since 4.9.001 (2010-03-28)
  1247. */
  1248. protected $columns = array();
  1249. /**
  1250. * Number of colums.
  1251. * @protected
  1252. * @since 4.9.001 (2010-03-28)
  1253. */
  1254. protected $num_columns = 1;
  1255. /**
  1256. * Current column number.
  1257. * @protected
  1258. * @since 4.9.001 (2010-03-28)
  1259. */
  1260. protected $current_column = 0;
  1261. /**
  1262. * Starting page for columns.
  1263. * @protected
  1264. * @since 4.9.001 (2010-03-28)
  1265. */
  1266. protected $column_start_page = 0;
  1267. /**
  1268. * Maximum page and column selected.
  1269. * @protected
  1270. * @since 5.8.000 (2010-08-11)
  1271. */
  1272. protected $maxselcol = array('page' => 0, 'column' => 0);
  1273. /**
  1274. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1275. * @protected
  1276. * @since 5.8.000 (2010-08-11)
  1277. */
  1278. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1279. /**
  1280. * 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.
  1281. * @protected
  1282. * @since 4.9.008 (2010-04-03)
  1283. */
  1284. protected $textrendermode = 0;
  1285. /**
  1286. * Text stroke width in doc units.
  1287. * @protected
  1288. * @since 4.9.008 (2010-04-03)
  1289. */
  1290. protected $textstrokewidth = 0;
  1291. /**
  1292. * Current stroke color.
  1293. * @protected
  1294. * @since 4.9.008 (2010-04-03)
  1295. */
  1296. protected $strokecolor;
  1297. /**
  1298. * Default unit of measure for document.
  1299. * @protected
  1300. * @since 5.0.000 (2010-04-22)
  1301. */
  1302. protected $pdfunit = 'mm';
  1303. /**
  1304. * Boolean flag true when we are on TOC (Table Of Content) page.
  1305. * @protected
  1306. */
  1307. protected $tocpage = false;
  1308. /**
  1309. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1310. * @protected
  1311. * @since 5.0.000 (2010-04-26)
  1312. */
  1313. protected $rasterize_vector_images = false;
  1314. /**
  1315. * Boolean flag: if true enables font subsetting by default.
  1316. * @protected
  1317. * @since 5.3.002 (2010-06-07)
  1318. */
  1319. protected $font_subsetting = true;
  1320. /**
  1321. * Array of default graphic settings.
  1322. * @protected
  1323. * @since 5.5.008 (2010-07-02)
  1324. */
  1325. protected $default_graphic_vars = array();
  1326. /**
  1327. * Array of XObjects.
  1328. * @protected
  1329. * @since 5.8.014 (2010-08-23)
  1330. */
  1331. protected $xobjects = array();
  1332. /**
  1333. * Boolean value true when we are inside an XObject.
  1334. * @protected
  1335. * @since 5.8.017 (2010-08-24)
  1336. */
  1337. protected $inxobj = false;
  1338. /**
  1339. * Current XObject ID.
  1340. * @protected
  1341. * @since 5.8.017 (2010-08-24)
  1342. */
  1343. protected $xobjid = '';
  1344. /**
  1345. * Percentage of character stretching.
  1346. * @protected
  1347. * @since 5.9.000 (2010-09-29)
  1348. */
  1349. protected $font_stretching = 100;
  1350. /**
  1351. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1352. * @protected
  1353. * @since 5.9.000 (2010-09-29)
  1354. */
  1355. protected $font_spacing = 0;
  1356. /**
  1357. * Array of no-write regions.
  1358. * ('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)
  1359. * @protected
  1360. * @since 5.9.003 (2010-10-14)
  1361. */
  1362. protected $page_regions = array();
  1363. /**
  1364. * Boolean value true when page region check is active.
  1365. * @protected
  1366. */
  1367. protected $check_page_regions = true;
  1368. /**
  1369. * Array of PDF layers data.
  1370. * @protected
  1371. * @since 5.9.102 (2011-07-13)
  1372. */
  1373. protected $pdflayers = array();
  1374. /**
  1375. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1376. * @protected
  1377. * @since 5.9.097 (2011-06-23)
  1378. */
  1379. protected $dests = array();
  1380. /**
  1381. * Object ID for Named Destinations
  1382. * @protected
  1383. * @since 5.9.097 (2011-06-23)
  1384. */
  1385. protected $n_dests;
  1386. /**
  1387. * Embedded Files Names
  1388. * @protected
  1389. * @since 5.9.204 (2013-01-23)
  1390. */
  1391. protected $efnames = array();
  1392. /**
  1393. * Directory used for the last SVG image.
  1394. * @protected
  1395. * @since 5.0.000 (2010-05-05)
  1396. */
  1397. protected $svgdir = '';
  1398. /**
  1399. * Deafult unit of measure for SVG.
  1400. * @protected
  1401. * @since 5.0.000 (2010-05-02)
  1402. */
  1403. protected $svgunit = 'px';
  1404. /**
  1405. * Array of SVG gradients.
  1406. * @protected
  1407. * @since 5.0.000 (2010-05-02)
  1408. */
  1409. protected $svggradients = array();
  1410. /**
  1411. * ID of last SVG gradient.
  1412. * @protected
  1413. * @since 5.0.000 (2010-05-02)
  1414. */
  1415. protected $svggradientid = 0;
  1416. /**
  1417. * Boolean value true when in SVG defs group.
  1418. * @protected
  1419. * @since 5.0.000 (2010-05-02)
  1420. */
  1421. protected $svgdefsmode = false;
  1422. /**
  1423. * Array of SVG defs.
  1424. * @protected
  1425. * @since 5.0.000 (2010-05-02)
  1426. */
  1427. protected $svgdefs = array();
  1428. /**
  1429. * Boolean value true when in SVG clipPath tag.
  1430. * @protected
  1431. * @since 5.0.000 (2010-04-26)
  1432. */
  1433. protected $svgclipmode = false;
  1434. /**
  1435. * Array of SVG clipPath commands.
  1436. * @protected
  1437. * @since 5.0.000 (2010-05-02)
  1438. */
  1439. protected $svgclippaths = array();
  1440. /**
  1441. * Array of SVG clipPath tranformation matrix.
  1442. * @protected
  1443. * @since 5.8.022 (2010-08-31)
  1444. */
  1445. protected $svgcliptm = array();
  1446. /**
  1447. * ID of last SVG clipPath.
  1448. * @protected
  1449. * @since 5.0.000 (2010-05-02)
  1450. */
  1451. protected $svgclipid = 0;
  1452. /**
  1453. * SVG text.
  1454. * @protected
  1455. * @since 5.0.000 (2010-05-02)
  1456. */
  1457. protected $svgtext = '';
  1458. /**
  1459. * SVG text properties.
  1460. * @protected
  1461. * @since 5.8.013 (2010-08-23)
  1462. */
  1463. protected $svgtextmode = array();
  1464. /**
  1465. * Array of SVG properties.
  1466. * @protected
  1467. * @since 5.0.000 (2010-05-02)
  1468. */
  1469. protected $svgstyles = array(array(
  1470. 'alignment-baseline' => 'auto',
  1471. 'baseline-shift' => 'baseline',
  1472. 'clip' => 'auto',
  1473. 'clip-path' => 'none',
  1474. 'clip-rule' => 'nonzero',
  1475. 'color' => 'black',
  1476. 'color-interpolation' => 'sRGB',
  1477. 'color-interpolation-filters' => 'linearRGB',
  1478. 'color-profile' => 'auto',
  1479. 'color-rendering' => 'auto',
  1480. 'cursor' => 'auto',
  1481. 'direction' => 'ltr',
  1482. 'display' => 'inline',
  1483. 'dominant-baseline' => 'auto',
  1484. 'enable-background' => 'accumulate',
  1485. 'fill' => 'black',
  1486. 'fill-opacity' => 1,
  1487. 'fill-rule' => 'nonzero',
  1488. 'filter' => 'none',
  1489. 'flood-color' => 'black',
  1490. 'flood-opacity' => 1,
  1491. 'font' => '',
  1492. 'font-family' => 'helvetica',
  1493. 'font-size' => 'medium',
  1494. 'font-size-adjust' => 'none',
  1495. 'font-stretch' => 'normal',
  1496. 'font-style' => 'normal',
  1497. 'font-variant' => 'normal',
  1498. 'font-weight' => 'normal',
  1499. 'glyph-orientation-horizontal' => '0deg',
  1500. 'glyph-orientation-vertical' => 'auto',
  1501. 'image-rendering' => 'auto',
  1502. 'kerning' => 'auto',
  1503. 'letter-spacing' => 'normal',
  1504. 'lighting-color' => 'white',
  1505. 'marker' => '',
  1506. 'marker-end' => 'none',
  1507. 'marker-mid' => 'none',
  1508. 'marker-start' => 'none',
  1509. 'mask' => 'none',
  1510. 'opacity' => 1,
  1511. 'overflow' => 'auto',
  1512. 'pointer-events' => 'visiblePainted',
  1513. 'shape-rendering' => 'auto',
  1514. 'stop-color' => 'black',
  1515. 'stop-opacity' => 1,
  1516. 'stroke' => 'none',
  1517. 'stroke-dasharray' => 'none',
  1518. 'stroke-dashoffset' => 0,
  1519. 'stroke-linecap' => 'butt',
  1520. 'stroke-linejoin' => 'miter',
  1521. 'stroke-miterlimit' => 4,
  1522. 'stroke-opacity' => 1,
  1523. 'stroke-width' => 1,
  1524. 'text-anchor' => 'start',
  1525. 'text-decoration' => 'none',
  1526. 'text-rendering' => 'auto',
  1527. 'unicode-bidi' => 'normal',
  1528. 'visibility' => 'visible',
  1529. 'word-spacing' => 'normal',
  1530. 'writing-mode' => 'lr-tb',
  1531. 'text-color' => 'black',
  1532. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1533. ));
  1534. /**
  1535. * If true force sRGB color profile for all document.
  1536. * @protected
  1537. * @since 5.9.121 (2011-09-28)
  1538. */
  1539. protected $force_srgb = false;
  1540. /**
  1541. * If true set the document to PDF/A mode.
  1542. * @protected
  1543. * @since 5.9.121 (2011-09-27)
  1544. */
  1545. protected $pdfa_mode = false;
  1546. /**
  1547. * Document creation date-time
  1548. * @protected
  1549. * @since 5.9.152 (2012-03-22)
  1550. */
  1551. protected $doc_creation_timestamp;
  1552. /**
  1553. * Document modification date-time
  1554. * @protected
  1555. * @since 5.9.152 (2012-03-22)
  1556. */
  1557. protected $doc_modification_timestamp;
  1558. /**
  1559. * Custom XMP data.
  1560. * @protected
  1561. * @since 5.9.128 (2011-10-06)
  1562. */
  1563. protected $custom_xmp = '';
  1564. /**
  1565. * Overprint mode array.
  1566. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1567. * @protected
  1568. * @since 5.9.152 (2012-03-23)
  1569. */
  1570. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1571. /**
  1572. * Alpha mode array.
  1573. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1574. * @protected
  1575. * @since 5.9.152 (2012-03-23)
  1576. */
  1577. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1578. /**
  1579. * Define the page boundaries boxes to be set on document.
  1580. * @protected
  1581. * @since 5.9.152 (2012-03-23)
  1582. */
  1583. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1584. /**
  1585. * If true print TCPDF meta link.
  1586. * @protected
  1587. * @since 5.9.152 (2012-03-23)
  1588. */
  1589. protected $tcpdflink = true;
  1590. /**
  1591. * Cache array for computed GD gamma values.
  1592. * @protected
  1593. * @since 5.9.1632 (2012-06-05)
  1594. */
  1595. protected $gdgammacache = array();
  1596. //------------------------------------------------------------
  1597. // METHODS
  1598. //------------------------------------------------------------
  1599. /**
  1600. * This is the class constructor.
  1601. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1602. *
  1603. * 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.
  1604. *
  1605. * @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>
  1606. * @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.
  1607. * @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().
  1608. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1609. * @param $encoding (string) Charset encoding (used only when converting back html entities); default is UTF-8.
  1610. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1611. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1612. * @public
  1613. * @see getPageSizeFromFormat(), setPageFormat()
  1614. */
  1615. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1616. /* Set internal character encoding to ASCII */
  1617. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1618. $this->internal_encoding = mb_internal_encoding();
  1619. mb_internal_encoding('ASCII');
  1620. }
  1621. $this->font_obj_ids = array();
  1622. $this->page_obj_id = array();
  1623. $this->form_obj_id = array();
  1624. // set pdf/a mode
  1625. $this->pdfa_mode = $pdfa;
  1626. $this->force_srgb = false;
  1627. // set disk caching
  1628. $this->diskcache = $diskcache ? true : false;
  1629. // set language direction
  1630. $this->rtl = false;
  1631. $this->tmprtl = false;
  1632. // some checks
  1633. $this->_dochecks();
  1634. // initialization of properties
  1635. $this->isunicode = $unicode;
  1636. $this->page = 0;
  1637. $this->transfmrk[0] = array();
  1638. $this->pagedim = array();
  1639. $this->n = 2;
  1640. $this->buffer = '';
  1641. $this->pages = array();
  1642. $this->state = 0;
  1643. $this->fonts = array();
  1644. $this->FontFiles = array();
  1645. $this->diffs = array();
  1646. $this->images = array();
  1647. $this->links = array();
  1648. $this->gradients = array();
  1649. $this->InFooter = false;
  1650. $this->lasth = 0;
  1651. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1652. $this->FontStyle = '';
  1653. $this->FontSizePt = 12;
  1654. $this->underline = false;
  1655. $this->overline = false;
  1656. $this->linethrough = false;
  1657. $this->DrawColor = '0 G';
  1658. $this->FillColor = '0 g';
  1659. $this->TextColor = '0 g';
  1660. $this->ColorFlag = false;
  1661. $this->pdflayers = array();
  1662. // encryption values
  1663. $this->encrypted = false;
  1664. $this->last_enc_key = '';
  1665. // standard Unicode fonts
  1666. $this->CoreFonts = array(
  1667. 'courier'=>'Courier',
  1668. 'courierB'=>'Courier-Bold',
  1669. 'courierI'=>'Courier-Oblique',
  1670. 'courierBI'=>'Courier-BoldOblique',
  1671. 'helvetica'=>'Helvetica',
  1672. 'helveticaB'=>'Helvetica-Bold',
  1673. 'helveticaI'=>'Helvetica-Oblique',
  1674. 'helveticaBI'=>'Helvetica-BoldOblique',
  1675. 'times'=>'Times-Roman',
  1676. 'timesB'=>'Times-Bold',
  1677. 'timesI'=>'Times-Italic',
  1678. 'timesBI'=>'Times-BoldItalic',
  1679. 'symbol'=>'Symbol',
  1680. 'zapfdingbats'=>'ZapfDingbats'
  1681. );
  1682. // set scale factor
  1683. $this->setPageUnit($unit);
  1684. // set page format and orientation
  1685. $this->setPageFormat($format, $orientation);
  1686. // page margins (1 cm)
  1687. $margin = 28.35 / $this->k;
  1688. $this->SetMargins($margin, $margin);
  1689. $this->clMargin = $this->lMargin;
  1690. $this->crMargin = $this->rMargin;
  1691. // internal cell padding
  1692. $cpadding = $margin / 10;
  1693. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1694. // cell margins
  1695. $this->setCellMargins(0, 0, 0, 0);
  1696. // line width (0.2 mm)
  1697. $this->LineWidth = 0.57 / $this->k;
  1698. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1699. $this->linestyleCap = '0 J';
  1700. $this->linestyleJoin = '0 j';
  1701. $this->linestyleDash = '[] 0 d';
  1702. // automatic page break
  1703. $this->SetAutoPageBreak(true, (2 * $margin));
  1704. // full width display mode
  1705. $this->SetDisplayMode('fullwidth');
  1706. // compression
  1707. $this->SetCompression();
  1708. // set default PDF version number
  1709. $this->setPDFVersion();
  1710. $this->tcpdflink = true;
  1711. $this->encoding = $encoding;
  1712. $this->HREF = array();
  1713. $this->getFontsList();
  1714. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1715. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1716. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1717. $this->extgstates = array();
  1718. $this->setTextShadow();
  1719. // user's rights
  1720. $this->sign = false;
  1721. $this->ur['enabled'] = false;
  1722. $this->ur['document'] = '/FullSave';
  1723. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1724. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1725. $this->ur['signature'] = '/Modify';
  1726. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1727. $this->ur['formex'] = '';
  1728. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0', 'name' => 'Signature');
  1729. $this->empty_signature_appearance = array();
  1730. // set default JPEG quality
  1731. $this->jpeg_quality = 75;
  1732. // initialize some settings
  1733. TCPDF_FONTS::utf8Bidi(array(''), '', false, $this->isunicode, $this->CurrentFont);
  1734. // set default font
  1735. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1736. // check if PCRE Unicode support is enabled
  1737. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1738. // PCRE unicode support is turned ON
  1739. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1740. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1741. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1742. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1743. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1744. } else {
  1745. // PCRE unicode support is turned OFF
  1746. $this->setSpacesRE('/[^\S\xa0]/');
  1747. }
  1748. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1749. // set file ID for trailer
  1750. $serformat = (is_array($format) ? serialize($format) : $format);
  1751. $this->file_id = md5(TCPDF_STATIC::getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
  1752. // set document creation and modification timestamp
  1753. $this->doc_creation_timestamp = time();
  1754. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1755. // get default graphic vars
  1756. $this->default_graphic_vars = $this->getGraphicVars();
  1757. $this->header_xobj_autoreset = false;
  1758. $this->custom_xmp = '';
  1759. }
  1760. /**
  1761. * Default destructor.
  1762. * @public
  1763. * @since 1.53.0.TC016
  1764. */
  1765. public function __destruct() {
  1766. // restore internal encoding
  1767. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1768. mb_internal_encoding($this->internal_encoding);
  1769. }
  1770. // unset all class variables
  1771. $this->_destroy(true);
  1772. }
  1773. /**
  1774. * Set the units of measure for the document.
  1775. * @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.
  1776. * @public
  1777. * @since 3.0.015 (2008-06-06)
  1778. */
  1779. public function setPageUnit($unit) {
  1780. $unit = strtolower($unit);
  1781. //Set scale factor
  1782. switch ($unit) {
  1783. // points
  1784. case 'px':
  1785. case 'pt': {
  1786. $this->k = 1;
  1787. break;
  1788. }
  1789. // millimeters
  1790. case 'mm': {
  1791. $this->k = $this->dpi / 25.4;
  1792. break;
  1793. }
  1794. // centimeters
  1795. case 'cm': {
  1796. $this->k = $this->dpi / 2.54;
  1797. break;
  1798. }
  1799. // inches
  1800. case 'in': {
  1801. $this->k = $this->dpi;
  1802. break;
  1803. }
  1804. // unsupported unit
  1805. default : {
  1806. $this->Error('Incorrect unit: '.$unit);
  1807. break;
  1808. }
  1809. }
  1810. $this->pdfunit = $unit;
  1811. if (isset($this->CurOrientation)) {
  1812. $this->setPageOrientation($this->CurOrientation);
  1813. }
  1814. }
  1815. /**
  1816. * Change the format of the current page
  1817. * @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>
  1818. * <li>['format'] = page format name (one of the above);</li>
  1819. * <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>
  1820. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  1821. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  1822. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  1823. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  1824. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  1825. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  1826. * <li>['CropBox'] : the visible region of default user space:</li>
  1827. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  1828. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  1829. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  1830. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  1831. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  1832. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  1833. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  1834. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  1835. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  1836. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  1837. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  1838. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  1839. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  1840. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  1841. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  1842. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  1843. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  1844. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  1845. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  1846. * <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>
  1847. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  1848. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  1849. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  1850. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  1851. * <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>
  1852. * <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>
  1853. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  1854. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  1855. * <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>
  1856. * <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>
  1857. * <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>
  1858. * <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>
  1859. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  1860. * </ul>
  1861. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  1862. * <li>P or Portrait (default)</li>
  1863. * <li>L or Landscape</li>
  1864. * <li>'' (empty string) for automatic orientation</li>
  1865. * </ul>
  1866. * @protected
  1867. * @since 3.0.015 (2008-06-06)
  1868. * @see getPageSizeFromFormat()
  1869. */
  1870. protected function setPageFormat($format, $orientation='P') {
  1871. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  1872. // remove inherited values
  1873. unset($this->pagedim[$this->page]);
  1874. }
  1875. if (is_string($format)) {
  1876. // get page measures from format name
  1877. $pf = TCPDF_STATIC::getPageSizeFromFormat($format);
  1878. $this->fwPt = $pf[0];
  1879. $this->fhPt = $pf[1];
  1880. } else {
  1881. // the boundaries of the physical medium on which the page shall be displayed or printed
  1882. if (isset($format['MediaBox'])) {
  1883. $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);
  1884. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  1885. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  1886. } else {
  1887. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  1888. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  1889. } else {
  1890. if (!isset($format['format'])) {
  1891. // default value
  1892. $format['format'] = 'A4';
  1893. }
  1894. $pf = TCPDF_STATIC::getPageSizeFromFormat($format['format']);
  1895. }
  1896. $this->fwPt = $pf[0];
  1897. $this->fhPt = $pf[1];
  1898. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1899. }
  1900. // the visible region of default user space
  1901. if (isset($format['CropBox'])) {
  1902. $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);
  1903. }
  1904. // the region to which the contents of the page shall be clipped when output in a production environment
  1905. if (isset($format['BleedBox'])) {
  1906. $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);
  1907. }
  1908. // the intended dimensions of the finished page after trimming
  1909. if (isset($format['TrimBox'])) {
  1910. $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);
  1911. }
  1912. // the page's meaningful content (including potential white space)
  1913. if (isset($format['ArtBox'])) {
  1914. $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);
  1915. }
  1916. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  1917. if (isset($format['BoxColorInfo'])) {
  1918. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  1919. }
  1920. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  1921. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1922. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  1923. }
  1924. if (isset($format['PZ'])) {
  1925. // The page's preferred zoom (magnification) factor
  1926. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  1927. }
  1928. if (isset($format['trans'])) {
  1929. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  1930. if (isset($format['trans']['Dur'])) {
  1931. // The page's display duration
  1932. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  1933. }
  1934. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  1935. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  1936. // The transition style that shall be used when moving to this page from another during a presentation
  1937. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  1938. $valid_effect = array('Split', 'Blinds');
  1939. $valid_vals = array('H', 'V');
  1940. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  1941. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  1942. }
  1943. $valid_effect = array('Split', 'Box', 'Fly');
  1944. $valid_vals = array('I', 'O');
  1945. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  1946. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  1947. }
  1948. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  1949. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  1950. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  1951. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  1952. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  1953. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  1954. }
  1955. }
  1956. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  1957. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  1958. }
  1959. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  1960. $this->pagedim[$this->page]['trans']['B'] = 'true';
  1961. }
  1962. } else {
  1963. $this->pagedim[$this->page]['trans']['S'] = 'R';
  1964. }
  1965. if (isset($format['trans']['D'])) {
  1966. // The duration of the transition effect, in seconds
  1967. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  1968. } else {
  1969. $this->pagedim[$this->page]['trans']['D'] = 1;
  1970. }
  1971. }
  1972. }
  1973. $this->setPageOrientation($orientation);
  1974. }
  1975. /**
  1976. * Set page orientation.
  1977. * @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>
  1978. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  1979. * @param $bottommargin (float) bottom margin of the page.
  1980. * @public
  1981. * @since 3.0.015 (2008-06-06)
  1982. */
  1983. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1984. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  1985. // the boundaries of the physical medium on which the page shall be displayed or printed
  1986. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1987. }
  1988. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  1989. // the visible region of default user space
  1990. $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);
  1991. }
  1992. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  1993. // the region to which the contents of the page shall be clipped when output in a production environment
  1994. $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);
  1995. }
  1996. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  1997. // the intended dimensions of the finished page after trimming
  1998. $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);
  1999. }
  2000. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2001. // the page's meaningful content (including potential white space)
  2002. $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);
  2003. }
  2004. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2005. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2006. $this->pagedim[$this->page]['Rotate'] = 0;
  2007. }
  2008. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2009. // The page's preferred zoom (magnification) factor
  2010. $this->pagedim[$this->page]['PZ'] = 1;
  2011. }
  2012. if ($this->fwPt > $this->fhPt) {
  2013. // landscape
  2014. $default_orientation = 'L';
  2015. } else {
  2016. // portrait
  2017. $default_orientation = 'P';
  2018. }
  2019. $valid_orientations = array('P', 'L');
  2020. if (empty($orientation)) {
  2021. $orientation = $default_orientation;
  2022. } else {
  2023. $orientation = strtoupper($orientation{0});
  2024. }
  2025. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2026. $this->CurOrientation = $orientation;
  2027. $this->wPt = $this->fhPt;
  2028. $this->hPt = $this->fwPt;
  2029. } else {
  2030. $this->CurOrientation = $default_orientation;
  2031. $this->wPt = $this->fwPt;
  2032. $this->hPt = $this->fhPt;
  2033. }
  2034. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2035. // swap X and Y coordinates (change page orientation)
  2036. $this->pagedim = TCPDF_STATIC::swapPageBoxCoordinates($this->page, $this->pagedim);
  2037. }
  2038. $this->w = ($this->wPt / $this->k);
  2039. $this->h = ($this->hPt / $this->k);
  2040. if (TCPDF_STATIC::empty_string($autopagebreak)) {
  2041. if (isset($this->AutoPageBreak)) {
  2042. $autopagebreak = $this->AutoPageBreak;
  2043. } else {
  2044. $autopagebreak = true;
  2045. }
  2046. }
  2047. if (TCPDF_STATIC::empty_string($bottommargin)) {
  2048. if (isset($this->bMargin)) {
  2049. $bottommargin = $this->bMargin;
  2050. } else {
  2051. // default value = 2 cm
  2052. $bottommargin = 2 * 28.35 / $this->k;
  2053. }
  2054. }
  2055. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2056. // store page dimensions
  2057. $this->pagedim[$this->page]['w'] = $this->wPt;
  2058. $this->pagedim[$this->page]['h'] = $this->hPt;
  2059. $this->pagedim[$this->page]['wk'] = $this->w;
  2060. $this->pagedim[$this->page]['hk'] = $this->h;
  2061. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2062. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2063. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2064. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2065. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2066. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2067. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2068. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2069. }
  2070. /**
  2071. * Set regular expression to detect withespaces or word separators.
  2072. * The pattern delimiter must be the forward-slash character "/".
  2073. * Some example patterns are:
  2074. * <pre>
  2075. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2076. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2077. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2078. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2079. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2080. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2081. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2082. * </pre>
  2083. * @param $re (string) regular expression (leave empty for default).
  2084. * @public
  2085. * @since 4.6.016 (2009-06-15)
  2086. */
  2087. public function setSpacesRE($re='/[^\S\xa0]/') {
  2088. $this->re_spaces = $re;
  2089. $re_parts = explode('/', $re);
  2090. // get pattern parts
  2091. $this->re_space = array();
  2092. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2093. $this->re_space['p'] = $re_parts[1];
  2094. } else {
  2095. $this->re_space['p'] = '[\s]';
  2096. }
  2097. // set pattern modifiers
  2098. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2099. $this->re_space['m'] = $re_parts[2];
  2100. } else {
  2101. $this->re_space['m'] = '';
  2102. }
  2103. }
  2104. /**
  2105. * Enable or disable Right-To-Left language mode
  2106. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2107. * @param $resetx (Boolean) if true reset the X position on direction change.
  2108. * @public
  2109. * @since 2.0.000 (2008-01-03)
  2110. */
  2111. public function setRTL($enable, $resetx=true) {
  2112. $enable = $enable ? true : false;
  2113. $resetx = ($resetx AND ($enable != $this->rtl));
  2114. $this->rtl = $enable;
  2115. $this->tmprtl = false;
  2116. if ($resetx) {
  2117. $this->Ln(0);
  2118. }
  2119. }
  2120. /**
  2121. * Return the RTL status
  2122. * @return boolean
  2123. * @public
  2124. * @since 4.0.012 (2008-07-24)
  2125. */
  2126. public function getRTL() {
  2127. return $this->rtl;
  2128. }
  2129. /**
  2130. * Force temporary RTL language direction
  2131. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2132. * @public
  2133. * @since 2.1.000 (2008-01-09)
  2134. */
  2135. public function setTempRTL($mode) {
  2136. $newmode = false;
  2137. switch (strtoupper($mode)) {
  2138. case 'LTR':
  2139. case 'L': {
  2140. if ($this->rtl) {
  2141. $newmode = 'L';
  2142. }
  2143. break;
  2144. }
  2145. case 'RTL':
  2146. case 'R': {
  2147. if (!$this->rtl) {
  2148. $newmode = 'R';
  2149. }
  2150. break;
  2151. }
  2152. case false:
  2153. default: {
  2154. $newmode = false;
  2155. break;
  2156. }
  2157. }
  2158. $this->tmprtl = $newmode;
  2159. }
  2160. /**
  2161. * Return the current temporary RTL status
  2162. * @return boolean
  2163. * @public
  2164. * @since 4.8.014 (2009-11-04)
  2165. */
  2166. public function isRTLTextDir() {
  2167. return ($this->rtl OR ($this->tmprtl == 'R'));
  2168. }
  2169. /**
  2170. * Set the last cell height.
  2171. * @param $h (float) cell height.
  2172. * @author Nicola Asuni
  2173. * @public
  2174. * @since 1.53.0.TC034
  2175. */
  2176. public function setLastH($h) {
  2177. $this->lasth = $h;
  2178. }
  2179. /**
  2180. * Reset the last cell height.
  2181. * @public
  2182. * @since 5.9.000 (2010-10-03)
  2183. */
  2184. public function resetLastH() {
  2185. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2186. }
  2187. /**
  2188. * Get the last cell height.
  2189. * @return last cell height
  2190. * @public
  2191. * @since 4.0.017 (2008-08-05)
  2192. */
  2193. public function getLastH() {
  2194. return $this->lasth;
  2195. }
  2196. /**
  2197. * Set the adjusting factor to convert pixels to user units.
  2198. * @param $scale (float) adjusting factor to convert pixels to user units.
  2199. * @author Nicola Asuni
  2200. * @public
  2201. * @since 1.5.2
  2202. */
  2203. public function setImageScale($scale) {
  2204. $this->imgscale = $scale;
  2205. }
  2206. /**
  2207. * Returns the adjusting factor to convert pixels to user units.
  2208. * @return float adjusting factor to convert pixels to user units.
  2209. * @author Nicola Asuni
  2210. * @public
  2211. * @since 1.5.2
  2212. */
  2213. public function getImageScale() {
  2214. return $this->imgscale;
  2215. }
  2216. /**
  2217. * Returns an array of page dimensions:
  2218. * <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>
  2219. * @param $pagenum (int) page number (empty = current page)
  2220. * @return array of page dimensions.
  2221. * @author Nicola Asuni
  2222. * @public
  2223. * @since 4.5.027 (2009-03-16)
  2224. */
  2225. public function getPageDimensions($pagenum='') {
  2226. if (empty($pagenum)) {
  2227. $pagenum = $this->page;
  2228. }
  2229. return $this->pagedim[$pagenum];
  2230. }
  2231. /**
  2232. * Returns the page width in units.
  2233. * @param $pagenum (int) page number (empty = current page)
  2234. * @return int page width.
  2235. * @author Nicola Asuni
  2236. * @public
  2237. * @since 1.5.2
  2238. * @see getPageDimensions()
  2239. */
  2240. public function getPageWidth($pagenum='') {
  2241. if (empty($pagenum)) {
  2242. return $this->w;
  2243. }
  2244. return $this->pagedim[$pagenum]['w'];
  2245. }
  2246. /**
  2247. * Returns the page height in units.
  2248. * @param $pagenum (int) page number (empty = current page)
  2249. * @return int page height.
  2250. * @author Nicola Asuni
  2251. * @public
  2252. * @since 1.5.2
  2253. * @see getPageDimensions()
  2254. */
  2255. public function getPageHeight($pagenum='') {
  2256. if (empty($pagenum)) {
  2257. return $this->h;
  2258. }
  2259. return $this->pagedim[$pagenum]['h'];
  2260. }
  2261. /**
  2262. * Returns the page break margin.
  2263. * @param $pagenum (int) page number (empty = current page)
  2264. * @return int page break margin.
  2265. * @author Nicola Asuni
  2266. * @public
  2267. * @since 1.5.2
  2268. * @see getPageDimensions()
  2269. */
  2270. public function getBreakMargin($pagenum='') {
  2271. if (empty($pagenum)) {
  2272. return $this->bMargin;
  2273. }
  2274. return $this->pagedim[$pagenum]['bm'];
  2275. }
  2276. /**
  2277. * Returns the scale factor (number of points in user unit).
  2278. * @return int scale factor.
  2279. * @author Nicola Asuni
  2280. * @public
  2281. * @since 1.5.2
  2282. */
  2283. public function getScaleFactor() {
  2284. return $this->k;
  2285. }
  2286. /**
  2287. * Defines the left, top and right margins.
  2288. * @param $left (float) Left margin.
  2289. * @param $top (float) Top margin.
  2290. * @param $right (float) Right margin. Default value is the left one.
  2291. * @param $keepmargins (boolean) if true overwrites the default page margins
  2292. * @public
  2293. * @since 1.0
  2294. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  2295. */
  2296. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  2297. //Set left, top and right margins
  2298. $this->lMargin = $left;
  2299. $this->tMargin = $top;
  2300. if ($right == -1) {
  2301. $right = $left;
  2302. }
  2303. $this->rMargin = $right;
  2304. if ($keepmargins) {
  2305. // overwrite original values
  2306. $this->original_lMargin = $this->lMargin;
  2307. $this->original_rMargin = $this->rMargin;
  2308. }
  2309. }
  2310. /**
  2311. * 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.
  2312. * @param $margin (float) The margin.
  2313. * @public
  2314. * @since 1.4
  2315. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2316. */
  2317. public function SetLeftMargin($margin) {
  2318. //Set left margin
  2319. $this->lMargin = $margin;
  2320. if (($this->page > 0) AND ($this->x < $margin)) {
  2321. $this->x = $margin;
  2322. }
  2323. }
  2324. /**
  2325. * Defines the top margin. The method can be called before creating the first page.
  2326. * @param $margin (float) The margin.
  2327. * @public
  2328. * @since 1.5
  2329. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2330. */
  2331. public function SetTopMargin($margin) {
  2332. //Set top margin
  2333. $this->tMargin = $margin;
  2334. if (($this->page > 0) AND ($this->y < $margin)) {
  2335. $this->y = $margin;
  2336. }
  2337. }
  2338. /**
  2339. * Defines the right margin. The method can be called before creating the first page.
  2340. * @param $margin (float) The margin.
  2341. * @public
  2342. * @since 1.5
  2343. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2344. */
  2345. public function SetRightMargin($margin) {
  2346. $this->rMargin = $margin;
  2347. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  2348. $this->x = $this->w - $margin;
  2349. }
  2350. }
  2351. /**
  2352. * Set the same internal Cell padding for top, right, bottom, left-
  2353. * @param $pad (float) internal padding.
  2354. * @public
  2355. * @since 2.1.000 (2008-01-09)
  2356. * @see getCellPaddings(), setCellPaddings()
  2357. */
  2358. public function SetCellPadding($pad) {
  2359. if ($pad >= 0) {
  2360. $this->cell_padding['L'] = $pad;
  2361. $this->cell_padding['T'] = $pad;
  2362. $this->cell_padding['R'] = $pad;
  2363. $this->cell_padding['B'] = $pad;
  2364. }
  2365. }
  2366. /**
  2367. * Set the internal Cell paddings.
  2368. * @param $left (float) left padding
  2369. * @param $top (float) top padding
  2370. * @param $right (float) right padding
  2371. * @param $bottom (float) bottom padding
  2372. * @public
  2373. * @since 5.9.000 (2010-10-03)
  2374. * @see getCellPaddings(), SetCellPadding()
  2375. */
  2376. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  2377. if (($left !== '') AND ($left >= 0)) {
  2378. $this->cell_padding['L'] = $left;
  2379. }
  2380. if (($top !== '') AND ($top >= 0)) {
  2381. $this->cell_padding['T'] = $top;
  2382. }
  2383. if (($right !== '') AND ($right >= 0)) {
  2384. $this->cell_padding['R'] = $right;
  2385. }
  2386. if (($bottom !== '') AND ($bottom >= 0)) {
  2387. $this->cell_padding['B'] = $bottom;
  2388. }
  2389. }
  2390. /**
  2391. * Get the internal Cell padding array.
  2392. * @return array of padding values
  2393. * @public
  2394. * @since 5.9.000 (2010-10-03)
  2395. * @see setCellPaddings(), SetCellPadding()
  2396. */
  2397. public function getCellPaddings() {
  2398. return $this->cell_padding;
  2399. }
  2400. /**
  2401. * Set the internal Cell margins.
  2402. * @param $left (float) left margin
  2403. * @param $top (float) top margin
  2404. * @param $right (float) right margin
  2405. * @param $bottom (float) bottom margin
  2406. * @public
  2407. * @since 5.9.000 (2010-10-03)
  2408. * @see getCellMargins()
  2409. */
  2410. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  2411. if (($left !== '') AND ($left >= 0)) {
  2412. $this->cell_margin['L'] = $left;
  2413. }
  2414. if (($top !== '') AND ($top >= 0)) {
  2415. $this->cell_margin['T'] = $top;
  2416. }
  2417. if (($right !== '') AND ($right >= 0)) {
  2418. $this->cell_margin['R'] = $right;
  2419. }
  2420. if (($bottom !== '') AND ($bottom >= 0)) {
  2421. $this->cell_margin['B'] = $bottom;
  2422. }
  2423. }
  2424. /**
  2425. * Get the internal Cell margin array.
  2426. * @return array of margin values
  2427. * @public
  2428. * @since 5.9.000 (2010-10-03)
  2429. * @see setCellMargins()
  2430. */
  2431. public function getCellMargins() {
  2432. return $this->cell_margin;
  2433. }
  2434. /**
  2435. * Adjust the internal Cell padding array to take account of the line width.
  2436. * @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)))
  2437. * @return array of adjustments
  2438. * @public
  2439. * @since 5.9.000 (2010-10-03)
  2440. */
  2441. protected function adjustCellPadding($brd=0) {
  2442. if (empty($brd)) {
  2443. return;
  2444. }
  2445. if (is_string($brd)) {
  2446. // convert string to array
  2447. $slen = strlen($brd);
  2448. $newbrd = array();
  2449. for ($i = 0; $i < $slen; ++$i) {
  2450. $newbrd[$brd[$i]] = true;
  2451. }
  2452. $brd = $newbrd;
  2453. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  2454. $brd = array('LRTB' => true);
  2455. }
  2456. if (!is_array($brd)) {
  2457. return;
  2458. }
  2459. // store current cell padding
  2460. $cp = $this->cell_padding;
  2461. // select border mode
  2462. if (isset($brd['mode'])) {
  2463. $mode = $brd['mode'];
  2464. unset($brd['mode']);
  2465. } else {
  2466. $mode = 'normal';
  2467. }
  2468. // process borders
  2469. foreach ($brd as $border => $style) {
  2470. $line_width = $this->LineWidth;
  2471. if (is_array($style) AND isset($style['width'])) {
  2472. // get border width
  2473. $line_width = $style['width'];
  2474. }
  2475. $adj = 0; // line width inside the cell
  2476. switch ($mode) {
  2477. case 'ext': {
  2478. $adj = 0;
  2479. break;
  2480. }
  2481. case 'int': {
  2482. $adj = $line_width;
  2483. break;
  2484. }
  2485. case 'normal':
  2486. default: {
  2487. $adj = ($line_width / 2);
  2488. break;
  2489. }
  2490. }
  2491. // correct internal cell padding if required to avoid overlap between text and lines
  2492. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  2493. $this->cell_padding['T'] = $adj;
  2494. }
  2495. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  2496. $this->cell_padding['R'] = $adj;
  2497. }
  2498. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  2499. $this->cell_padding['B'] = $adj;
  2500. }
  2501. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  2502. $this->cell_padding['L'] = $adj;
  2503. }
  2504. }
  2505. 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']));
  2506. }
  2507. /**
  2508. * 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.
  2509. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  2510. * @param $margin (float) Distance from the bottom of the page.
  2511. * @public
  2512. * @since 1.0
  2513. * @see Cell(), MultiCell(), AcceptPageBreak()
  2514. */
  2515. public function SetAutoPageBreak($auto, $margin=0) {
  2516. $this->AutoPageBreak = $auto ? true : false;
  2517. $this->bMargin = $margin;
  2518. $this->PageBreakTrigger = $this->h - $margin;
  2519. }
  2520. /**
  2521. * Return the auto-page-break mode (true or false).
  2522. * @return boolean auto-page-break mode
  2523. * @public
  2524. * @since 5.9.088
  2525. */
  2526. public function getAutoPageBreak() {
  2527. return $this->AutoPageBreak;
  2528. }
  2529. /**
  2530. * Defines the way the document is to be displayed by the viewer.
  2531. * @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>
  2532. * @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>
  2533. * @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>
  2534. * @public
  2535. * @since 1.2
  2536. */
  2537. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  2538. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  2539. $this->ZoomMode = $zoom;
  2540. } else {
  2541. $this->Error('Incorrect zoom display mode: '.$zoom);
  2542. }
  2543. $this->LayoutMode = TCPDF_STATIC::getPageLayoutMode($layout);
  2544. $this->PageMode = TCPDF_STATIC::getPageMode($mode);
  2545. }
  2546. /**
  2547. * 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.
  2548. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  2549. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  2550. * @public
  2551. * @since 1.4
  2552. */
  2553. public function SetCompression($compress=true) {
  2554. if (function_exists('gzcompress')) {
  2555. $this->compress = $compress ? true : false;
  2556. } else {
  2557. $this->compress = false;
  2558. }
  2559. }
  2560. /**
  2561. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  2562. * @param $mode (boolean) If true force sRGB output intent.
  2563. * @public
  2564. * @since 5.9.121 (2011-09-28)
  2565. */
  2566. public function setSRGBmode($mode=false) {
  2567. $this->force_srgb = $mode ? true : false;
  2568. }
  2569. /**
  2570. * Turn on/off Unicode mode for document information dictionary (meta tags).
  2571. * This has effect only when unicode mode is set to false.
  2572. * @param $unicode (boolean) if true set the meta information in Unicode
  2573. * @since 5.9.027 (2010-12-01)
  2574. * @public
  2575. */
  2576. public function SetDocInfoUnicode($unicode=true) {
  2577. $this->docinfounicode = $unicode ? true : false;
  2578. }
  2579. /**
  2580. * Defines the title of the document.
  2581. * @param $title (string) The title.
  2582. * @public
  2583. * @since 1.2
  2584. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  2585. */
  2586. public function SetTitle($title) {
  2587. $this->title = $title;
  2588. }
  2589. /**
  2590. * Defines the subject of the document.
  2591. * @param $subject (string) The subject.
  2592. * @public
  2593. * @since 1.2
  2594. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  2595. */
  2596. public function SetSubject($subject) {
  2597. $this->subject = $subject;
  2598. }
  2599. /**
  2600. * Defines the author of the document.
  2601. * @param $author (string) The name of the author.
  2602. * @public
  2603. * @since 1.2
  2604. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  2605. */
  2606. public function SetAuthor($author) {
  2607. $this->author = $author;
  2608. }
  2609. /**
  2610. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  2611. * @param $keywords (string) The list of keywords.
  2612. * @public
  2613. * @since 1.2
  2614. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  2615. */
  2616. public function SetKeywords($keywords) {
  2617. $this->keywords = $keywords;
  2618. }
  2619. /**
  2620. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  2621. * @param $creator (string) The name of the creator.
  2622. * @public
  2623. * @since 1.2
  2624. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  2625. */
  2626. public function SetCreator($creator) {
  2627. $this->creator = $creator;
  2628. }
  2629. /**
  2630. * Throw an exception or print an error message and die if the K_TCPDF_PARSER_THROW_EXCEPTION_ERROR constant is set to true.
  2631. * @param $msg (string) The error message
  2632. * @public
  2633. * @since 1.0
  2634. */
  2635. public function Error($msg) {
  2636. // unset all class variables
  2637. $this->_destroy(true);
  2638. if (defined('K_TCPDF_THROW_EXCEPTION_ERROR') AND !K_TCPDF_THROW_EXCEPTION_ERROR) {
  2639. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2640. } else {
  2641. throw new Exception('TCPDF ERROR: '.$msg);
  2642. }
  2643. }
  2644. /**
  2645. * This method begins the generation of the PDF document.
  2646. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2647. * Note: no page is created by this method
  2648. * @public
  2649. * @since 1.0
  2650. * @see AddPage(), Close()
  2651. */
  2652. public function Open() {
  2653. $this->state = 1;
  2654. }
  2655. /**
  2656. * Terminates the PDF document.
  2657. * It is not necessary to call this method explicitly because Output() does it automatically.
  2658. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2659. * @public
  2660. * @since 1.0
  2661. * @see Open(), Output()
  2662. */
  2663. public function Close() {
  2664. if ($this->state == 3) {
  2665. return;
  2666. }
  2667. if ($this->page == 0) {
  2668. $this->AddPage();
  2669. }
  2670. $this->endLayer();
  2671. if ($this->tcpdflink) {
  2672. // save current graphic settings
  2673. $gvars = $this->getGraphicVars();
  2674. $this->setEqualColumns();
  2675. $this->lastpage(true);
  2676. $this->SetAutoPageBreak(false);
  2677. $this->x = 0;
  2678. $this->y = $this->h - (1 / $this->k);
  2679. $this->lMargin = 0;
  2680. $this->_out('q');
  2681. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  2682. $this->SetFont($font, '', 1);
  2683. $this->setTextRenderingMode(0, false, false);
  2684. $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";
  2685. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  2686. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  2687. $this->_out('Q');
  2688. // restore graphic settings
  2689. $this->setGraphicVars($gvars);
  2690. }
  2691. // close page
  2692. $this->endPage();
  2693. // close document
  2694. $this->_enddoc();
  2695. // unset all class variables (except critical ones)
  2696. $this->_destroy(false);
  2697. }
  2698. /**
  2699. * Move pointer at the specified document page and update page dimensions.
  2700. * @param $pnum (int) page number (1 ... numpages)
  2701. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2702. * @public
  2703. * @since 2.1.000 (2008-01-07)
  2704. * @see getPage(), lastpage(), getNumPages()
  2705. */
  2706. public function setPage($pnum, $resetmargins=false) {
  2707. if (($pnum == $this->page) AND ($this->state == 2)) {
  2708. return;
  2709. }
  2710. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  2711. $this->state = 2;
  2712. // save current graphic settings
  2713. //$gvars = $this->getGraphicVars();
  2714. $oldpage = $this->page;
  2715. $this->page = $pnum;
  2716. $this->wPt = $this->pagedim[$this->page]['w'];
  2717. $this->hPt = $this->pagedim[$this->page]['h'];
  2718. $this->w = $this->pagedim[$this->page]['wk'];
  2719. $this->h = $this->pagedim[$this->page]['hk'];
  2720. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2721. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2722. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2723. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2724. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2725. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2726. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2727. // restore graphic settings
  2728. //$this->setGraphicVars($gvars);
  2729. if ($resetmargins) {
  2730. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2731. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2732. $this->SetY($this->tMargin);
  2733. } else {
  2734. // account for booklet mode
  2735. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2736. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2737. $this->lMargin += $deltam;
  2738. $this->rMargin -= $deltam;
  2739. }
  2740. }
  2741. } else {
  2742. $this->Error('Wrong page number on setPage() function: '.$pnum);
  2743. }
  2744. }
  2745. /**
  2746. * Reset pointer to the last document page.
  2747. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2748. * @public
  2749. * @since 2.0.000 (2008-01-04)
  2750. * @see setPage(), getPage(), getNumPages()
  2751. */
  2752. public function lastPage($resetmargins=false) {
  2753. $this->setPage($this->getNumPages(), $resetmargins);
  2754. }
  2755. /**
  2756. * Get current document page number.
  2757. * @return int page number
  2758. * @public
  2759. * @since 2.1.000 (2008-01-07)
  2760. * @see setPage(), lastpage(), getNumPages()
  2761. */
  2762. public function getPage() {
  2763. return $this->page;
  2764. }
  2765. /**
  2766. * Get the total number of insered pages.
  2767. * @return int number of pages
  2768. * @public
  2769. * @since 2.1.000 (2008-01-07)
  2770. * @see setPage(), getPage(), lastpage()
  2771. */
  2772. public function getNumPages() {
  2773. return $this->numpages;
  2774. }
  2775. /**
  2776. * Adds a new TOC (Table Of Content) page to the document.
  2777. * @param $orientation (string) page orientation.
  2778. * @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().
  2779. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2780. * @public
  2781. * @since 5.0.001 (2010-05-06)
  2782. * @see AddPage(), startPage(), endPage(), endTOCPage()
  2783. */
  2784. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  2785. $this->AddPage($orientation, $format, $keepmargins, true);
  2786. }
  2787. /**
  2788. * Terminate the current TOC (Table Of Content) page
  2789. * @public
  2790. * @since 5.0.001 (2010-05-06)
  2791. * @see AddPage(), startPage(), endPage(), addTOCPage()
  2792. */
  2793. public function endTOCPage() {
  2794. $this->endPage(true);
  2795. }
  2796. /**
  2797. * 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).
  2798. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2799. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2800. * @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().
  2801. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2802. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  2803. * @public
  2804. * @since 1.0
  2805. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2806. */
  2807. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  2808. if ($this->inxobj) {
  2809. // we are inside an XObject template
  2810. return;
  2811. }
  2812. if (!isset($this->original_lMargin) OR $keepmargins) {
  2813. $this->original_lMargin = $this->lMargin;
  2814. }
  2815. if (!isset($this->original_rMargin) OR $keepmargins) {
  2816. $this->original_rMargin = $this->rMargin;
  2817. }
  2818. // terminate previous page
  2819. $this->endPage();
  2820. // start new page
  2821. $this->startPage($orientation, $format, $tocpage);
  2822. }
  2823. /**
  2824. * Terminate the current page
  2825. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  2826. * @public
  2827. * @since 4.2.010 (2008-11-14)
  2828. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  2829. */
  2830. public function endPage($tocpage=false) {
  2831. // check if page is already closed
  2832. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2833. return;
  2834. }
  2835. // print page footer
  2836. $this->setFooter();
  2837. // close page
  2838. $this->_endpage();
  2839. // mark page as closed
  2840. $this->pageopen[$this->page] = false;
  2841. if ($tocpage) {
  2842. $this->tocpage = false;
  2843. }
  2844. }
  2845. /**
  2846. * Starts a new page to the document. The page must be closed using the endPage() function.
  2847. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2848. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2849. * @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().
  2850. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  2851. * @since 4.2.010 (2008-11-14)
  2852. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2853. * @public
  2854. */
  2855. public function startPage($orientation='', $format='', $tocpage=false) {
  2856. if ($tocpage) {
  2857. $this->tocpage = true;
  2858. }
  2859. // move page numbers of documents to be attached
  2860. if ($this->tocpage) {
  2861. // move reference to unexistent pages (used for page attachments)
  2862. // adjust outlines
  2863. $tmpoutlines = $this->outlines;
  2864. foreach ($tmpoutlines as $key => $outline) {
  2865. if ($outline['p'] > $this->numpages) {
  2866. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  2867. }
  2868. }
  2869. // adjust dests
  2870. $tmpdests = $this->dests;
  2871. foreach ($tmpdests as $key => $dest) {
  2872. if ($dest['p'] > $this->numpages) {
  2873. $this->dests[$key]['p'] = ($dest['p'] + 1);
  2874. }
  2875. }
  2876. // adjust links
  2877. $tmplinks = $this->links;
  2878. foreach ($tmplinks as $key => $link) {
  2879. if ($link[0] > $this->numpages) {
  2880. $this->links[$key][0] = ($link[0] + 1);
  2881. }
  2882. }
  2883. }
  2884. if ($this->numpages > $this->page) {
  2885. // this page has been already added
  2886. $this->setPage($this->page + 1);
  2887. $this->SetY($this->tMargin);
  2888. return;
  2889. }
  2890. // start a new page
  2891. if ($this->state == 0) {
  2892. $this->Open();
  2893. }
  2894. ++$this->numpages;
  2895. $this->swapMargins($this->booklet);
  2896. // save current graphic settings
  2897. $gvars = $this->getGraphicVars();
  2898. // start new page
  2899. $this->_beginpage($orientation, $format);
  2900. // mark page as open
  2901. $this->pageopen[$this->page] = true;
  2902. // restore graphic settings
  2903. $this->setGraphicVars($gvars);
  2904. // mark this point
  2905. $this->setPageMark();
  2906. // print page header
  2907. $this->setHeader();
  2908. // restore graphic settings
  2909. $this->setGraphicVars($gvars);
  2910. // mark this point
  2911. $this->setPageMark();
  2912. // print table header (if any)
  2913. $this->setTableHeader();
  2914. // set mark for empty page check
  2915. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  2916. }
  2917. /**
  2918. * Set start-writing mark on current page stream used to put borders and fills.
  2919. * Borders and fills are always created after content and inserted on the position marked by this method.
  2920. * This function must be called after calling Image() function for a background image.
  2921. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2922. * @public
  2923. * @since 4.0.016 (2008-07-30)
  2924. */
  2925. public function setPageMark() {
  2926. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2927. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  2928. $this->setContentMark();
  2929. }
  2930. /**
  2931. * Set start-writing mark on selected page.
  2932. * Borders and fills are always created after content and inserted on the position marked by this method.
  2933. * @param $page (int) page number (default is the current page)
  2934. * @protected
  2935. * @since 4.6.021 (2009-07-20)
  2936. */
  2937. protected function setContentMark($page=0) {
  2938. if ($page <= 0) {
  2939. $page = $this->page;
  2940. }
  2941. if (isset($this->footerlen[$page])) {
  2942. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2943. } else {
  2944. $this->cntmrk[$page] = $this->pagelen[$page];
  2945. }
  2946. }
  2947. /**
  2948. * Set header data.
  2949. * @param $ln (string) header image logo
  2950. * @param $lw (string) header image logo width in mm
  2951. * @param $ht (string) string to print as title on document header
  2952. * @param $hs (string) string to print on document header
  2953. * @param $tc (array) RGB array color for text.
  2954. * @param $lc (array) RGB array color for line.
  2955. * @public
  2956. */
  2957. public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
  2958. $this->header_logo = $ln;
  2959. $this->header_logo_width = $lw;
  2960. $this->header_title = $ht;
  2961. $this->header_string = $hs;
  2962. $this->header_text_color = $tc;
  2963. $this->header_line_color = $lc;
  2964. }
  2965. /**
  2966. * Set footer data.
  2967. * @param $tc (array) RGB array color for text.
  2968. * @param $lc (array) RGB array color for line.
  2969. * @public
  2970. */
  2971. public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
  2972. $this->footer_text_color = $tc;
  2973. $this->footer_line_color = $lc;
  2974. }
  2975. /**
  2976. * Returns header data:
  2977. * <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>
  2978. * @return array()
  2979. * @public
  2980. * @since 4.0.012 (2008-07-24)
  2981. */
  2982. public function getHeaderData() {
  2983. $ret = array();
  2984. $ret['logo'] = $this->header_logo;
  2985. $ret['logo_width'] = $this->header_logo_width;
  2986. $ret['title'] = $this->header_title;
  2987. $ret['string'] = $this->header_string;
  2988. $ret['text_color'] = $this->header_text_color;
  2989. $ret['line_color'] = $this->header_line_color;
  2990. return $ret;
  2991. }
  2992. /**
  2993. * Set header margin.
  2994. * (minimum distance between header and top page margin)
  2995. * @param $hm (int) distance in user units
  2996. * @public
  2997. */
  2998. public function setHeaderMargin($hm=10) {
  2999. $this->header_margin = $hm;
  3000. }
  3001. /**
  3002. * Returns header margin in user units.
  3003. * @return float
  3004. * @since 4.0.012 (2008-07-24)
  3005. * @public
  3006. */
  3007. public function getHeaderMargin() {
  3008. return $this->header_margin;
  3009. }
  3010. /**
  3011. * Set footer margin.
  3012. * (minimum distance between footer and bottom page margin)
  3013. * @param $fm (int) distance in user units
  3014. * @public
  3015. */
  3016. public function setFooterMargin($fm=10) {
  3017. $this->footer_margin = $fm;
  3018. }
  3019. /**
  3020. * Returns footer margin in user units.
  3021. * @return float
  3022. * @since 4.0.012 (2008-07-24)
  3023. * @public
  3024. */
  3025. public function getFooterMargin() {
  3026. return $this->footer_margin;
  3027. }
  3028. /**
  3029. * Set a flag to print page header.
  3030. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3031. * @public
  3032. */
  3033. public function setPrintHeader($val=true) {
  3034. $this->print_header = $val ? true : false;
  3035. }
  3036. /**
  3037. * Set a flag to print page footer.
  3038. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3039. * @public
  3040. */
  3041. public function setPrintFooter($val=true) {
  3042. $this->print_footer = $val ? true : false;
  3043. }
  3044. /**
  3045. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3046. * @return float
  3047. * @public
  3048. */
  3049. public function getImageRBX() {
  3050. return $this->img_rb_x;
  3051. }
  3052. /**
  3053. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3054. * @return float
  3055. * @public
  3056. */
  3057. public function getImageRBY() {
  3058. return $this->img_rb_y;
  3059. }
  3060. /**
  3061. * Reset the xobject template used by Header() method.
  3062. * @public
  3063. */
  3064. public function resetHeaderTemplate() {
  3065. $this->header_xobjid = -1;
  3066. }
  3067. /**
  3068. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3069. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3070. * @public
  3071. */
  3072. public function setHeaderTemplateAutoreset($val=true) {
  3073. $this->header_xobj_autoreset = $val ? true : false;
  3074. }
  3075. /**
  3076. * This method is used to render the page header.
  3077. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3078. * @public
  3079. */
  3080. public function Header() {
  3081. if ($this->header_xobjid < 0) {
  3082. // start a new XObject Template
  3083. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3084. $headerfont = $this->getHeaderFont();
  3085. $headerdata = $this->getHeaderData();
  3086. $this->y = $this->header_margin;
  3087. if ($this->rtl) {
  3088. $this->x = $this->w - $this->original_rMargin;
  3089. } else {
  3090. $this->x = $this->original_lMargin;
  3091. }
  3092. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3093. $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3094. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3095. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3096. } elseif ($imgtype == 'svg') {
  3097. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3098. } else {
  3099. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3100. }
  3101. $imgy = $this->getImageRBY();
  3102. } else {
  3103. $imgy = $this->y;
  3104. }
  3105. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3106. // set starting margin for text data cell
  3107. if ($this->getRTL()) {
  3108. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3109. } else {
  3110. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3111. }
  3112. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3113. $this->SetTextColorArray($this->header_text_color);
  3114. // header title
  3115. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3116. $this->SetX($header_x);
  3117. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3118. // header string
  3119. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3120. $this->SetX($header_x);
  3121. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3122. // print an ending header line
  3123. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  3124. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3125. if ($this->rtl) {
  3126. $this->SetX($this->original_rMargin);
  3127. } else {
  3128. $this->SetX($this->original_lMargin);
  3129. }
  3130. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3131. $this->endTemplate();
  3132. }
  3133. // print header template
  3134. $x = 0;
  3135. $dx = 0;
  3136. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3137. // adjust margins for booklet mode
  3138. $dx = ($this->original_lMargin - $this->original_rMargin);
  3139. }
  3140. if ($this->rtl) {
  3141. $x = $this->w + $dx;
  3142. } else {
  3143. $x = 0 + $dx;
  3144. }
  3145. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3146. if ($this->header_xobj_autoreset) {
  3147. // reset header xobject template at each page
  3148. $this->header_xobjid = -1;
  3149. }
  3150. }
  3151. /**
  3152. * This method is used to render the page footer.
  3153. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3154. * @public
  3155. */
  3156. public function Footer() {
  3157. $cur_y = $this->y;
  3158. $this->SetTextColorArray($this->footer_text_color);
  3159. //set style for cell border
  3160. $line_width = (0.85 / $this->k);
  3161. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  3162. //print document barcode
  3163. $barcode = $this->getBarcode();
  3164. if (!empty($barcode)) {
  3165. $this->Ln($line_width);
  3166. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3167. $style = array(
  3168. 'position' => $this->rtl?'R':'L',
  3169. 'align' => $this->rtl?'R':'L',
  3170. 'stretch' => false,
  3171. 'fitwidth' => true,
  3172. 'cellfitalign' => '',
  3173. 'border' => false,
  3174. 'padding' => 0,
  3175. 'fgcolor' => array(0,0,0),
  3176. 'bgcolor' => false,
  3177. 'text' => false
  3178. );
  3179. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3180. }
  3181. $w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
  3182. if (empty($this->pagegroups)) {
  3183. $pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3184. } else {
  3185. $pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3186. }
  3187. $this->SetY($cur_y);
  3188. //Print page number
  3189. if ($this->getRTL()) {
  3190. $this->SetX($this->original_rMargin);
  3191. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3192. } else {
  3193. $this->SetX($this->original_lMargin);
  3194. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3195. }
  3196. }
  3197. /**
  3198. * This method is used to render the page header.
  3199. * @protected
  3200. * @since 4.0.012 (2008-07-24)
  3201. */
  3202. protected function setHeader() {
  3203. if (!$this->print_header OR ($this->state != 2)) {
  3204. return;
  3205. }
  3206. $this->InHeader = true;
  3207. $this->setGraphicVars($this->default_graphic_vars);
  3208. $temp_thead = $this->thead;
  3209. $temp_theadMargins = $this->theadMargins;
  3210. $lasth = $this->lasth;
  3211. $this->_out('q');
  3212. $this->rMargin = $this->original_rMargin;
  3213. $this->lMargin = $this->original_lMargin;
  3214. $this->SetCellPadding(0);
  3215. //set current position
  3216. if ($this->rtl) {
  3217. $this->SetXY($this->original_rMargin, $this->header_margin);
  3218. } else {
  3219. $this->SetXY($this->original_lMargin, $this->header_margin);
  3220. }
  3221. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3222. $this->Header();
  3223. //restore position
  3224. if ($this->rtl) {
  3225. $this->SetXY($this->original_rMargin, $this->tMargin);
  3226. } else {
  3227. $this->SetXY($this->original_lMargin, $this->tMargin);
  3228. }
  3229. $this->_out('Q');
  3230. $this->lasth = $lasth;
  3231. $this->thead = $temp_thead;
  3232. $this->theadMargins = $temp_theadMargins;
  3233. $this->newline = false;
  3234. $this->InHeader = false;
  3235. }
  3236. /**
  3237. * This method is used to render the page footer.
  3238. * @protected
  3239. * @since 4.0.012 (2008-07-24)
  3240. */
  3241. protected function setFooter() {
  3242. if ($this->state != 2) {
  3243. return;
  3244. }
  3245. $this->InFooter = true;
  3246. // save current graphic settings
  3247. $gvars = $this->getGraphicVars();
  3248. // mark this point
  3249. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  3250. $this->_out("\n");
  3251. if ($this->print_footer) {
  3252. $this->setGraphicVars($this->default_graphic_vars);
  3253. $this->current_column = 0;
  3254. $this->num_columns = 1;
  3255. $temp_thead = $this->thead;
  3256. $temp_theadMargins = $this->theadMargins;
  3257. $lasth = $this->lasth;
  3258. $this->_out('q');
  3259. $this->rMargin = $this->original_rMargin;
  3260. $this->lMargin = $this->original_lMargin;
  3261. $this->SetCellPadding(0);
  3262. //set current position
  3263. $footer_y = $this->h - $this->footer_margin;
  3264. if ($this->rtl) {
  3265. $this->SetXY($this->original_rMargin, $footer_y);
  3266. } else {
  3267. $this->SetXY($this->original_lMargin, $footer_y);
  3268. }
  3269. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  3270. $this->Footer();
  3271. //restore position
  3272. if ($this->rtl) {
  3273. $this->SetXY($this->original_rMargin, $this->tMargin);
  3274. } else {
  3275. $this->SetXY($this->original_lMargin, $this->tMargin);
  3276. }
  3277. $this->_out('Q');
  3278. $this->lasth = $lasth;
  3279. $this->thead = $temp_thead;
  3280. $this->theadMargins = $temp_theadMargins;
  3281. }
  3282. // restore graphic settings
  3283. $this->setGraphicVars($gvars);
  3284. $this->current_column = $gvars['current_column'];
  3285. $this->num_columns = $gvars['num_columns'];
  3286. // calculate footer length
  3287. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  3288. $this->InFooter = false;
  3289. }
  3290. /**
  3291. * Check if we are on the page body (excluding page header and footer).
  3292. * @return true if we are not in page header nor in page footer, false otherwise.
  3293. * @protected
  3294. * @since 5.9.091 (2011-06-15)
  3295. */
  3296. protected function inPageBody() {
  3297. return (($this->InHeader === false) AND ($this->InFooter === false));
  3298. }
  3299. /**
  3300. * This method is used to render the table header on new page (if any).
  3301. * @protected
  3302. * @since 4.5.030 (2009-03-25)
  3303. */
  3304. protected function setTableHeader() {
  3305. if ($this->num_columns > 1) {
  3306. // multi column mode
  3307. return;
  3308. }
  3309. if (isset($this->theadMargins['top'])) {
  3310. // restore the original top-margin
  3311. $this->tMargin = $this->theadMargins['top'];
  3312. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3313. $this->y = $this->tMargin;
  3314. }
  3315. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  3316. // set margins
  3317. $prev_lMargin = $this->lMargin;
  3318. $prev_rMargin = $this->rMargin;
  3319. $prev_cell_padding = $this->cell_padding;
  3320. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  3321. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  3322. $this->cell_padding = $this->theadMargins['cell_padding'];
  3323. if ($this->rtl) {
  3324. $this->x = $this->w - $this->rMargin;
  3325. } else {
  3326. $this->x = $this->lMargin;
  3327. }
  3328. // account for special "cell" mode
  3329. if ($this->theadMargins['cell']) {
  3330. if ($this->rtl) {
  3331. $this->x -= $this->cell_padding['R'];
  3332. } else {
  3333. $this->x += $this->cell_padding['L'];
  3334. }
  3335. }
  3336. // print table header
  3337. $this->writeHTML($this->thead, false, false, false, false, '');
  3338. // set new top margin to skip the table headers
  3339. if (!isset($this->theadMargins['top'])) {
  3340. $this->theadMargins['top'] = $this->tMargin;
  3341. }
  3342. // store end of header position
  3343. if (!isset($this->columns[0]['th'])) {
  3344. $this->columns[0]['th'] = array();
  3345. }
  3346. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  3347. $this->tMargin = $this->y;
  3348. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3349. $this->lasth = 0;
  3350. $this->lMargin = $prev_lMargin;
  3351. $this->rMargin = $prev_rMargin;
  3352. $this->cell_padding = $prev_cell_padding;
  3353. }
  3354. }
  3355. /**
  3356. * Returns the current page number.
  3357. * @return int page number
  3358. * @public
  3359. * @since 1.0
  3360. * @see getAliasNbPages()
  3361. */
  3362. public function PageNo() {
  3363. return $this->page;
  3364. }
  3365. /**
  3366. * Defines a new spot color.
  3367. * It can be expressed in RGB components or gray scale.
  3368. * The method can be called before the first page is created and the value is retained from page to page.
  3369. * @param $name (string) Full name of the spot color.
  3370. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  3371. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  3372. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  3373. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  3374. * @public
  3375. * @since 4.0.024 (2008-09-12)
  3376. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3377. */
  3378. public function AddSpotColor($name, $c, $m, $y, $k) {
  3379. if (!isset($this->spot_colors[$name])) {
  3380. $i = (1 + count($this->spot_colors));
  3381. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  3382. }
  3383. }
  3384. /**
  3385. * Set the spot color for the specified type ('draw', 'fill', 'text').
  3386. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3387. * @param $name (string) Name of the spot color.
  3388. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3389. * @return (string) PDF color command.
  3390. * @public
  3391. * @since 5.9.125 (2011-10-03)
  3392. */
  3393. public function setSpotColor($type, $name, $tint=100) {
  3394. $spotcolor = TCPDF_COLORS::getSpotColor($name, $this->spot_colors);
  3395. if ($spotcolor === false) {
  3396. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  3397. }
  3398. $tint = (max(0, min(100, $tint)) / 100);
  3399. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  3400. switch ($type) {
  3401. case 'draw': {
  3402. $pdfcolor .= sprintf('CS %F SCN', $tint);
  3403. $this->DrawColor = $pdfcolor;
  3404. $this->strokecolor = $spotcolor;
  3405. break;
  3406. }
  3407. case 'fill': {
  3408. $pdfcolor .= sprintf('cs %F scn', $tint);
  3409. $this->FillColor = $pdfcolor;
  3410. $this->bgcolor = $spotcolor;
  3411. break;
  3412. }
  3413. case 'text': {
  3414. $pdfcolor .= sprintf('cs %F scn', $tint);
  3415. $this->TextColor = $pdfcolor;
  3416. $this->fgcolor = $spotcolor;
  3417. break;
  3418. }
  3419. }
  3420. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3421. if ($this->state == 2) {
  3422. $this->_out($pdfcolor);
  3423. }
  3424. if ($this->inxobj) {
  3425. // we are inside an XObject template
  3426. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  3427. }
  3428. return $pdfcolor;
  3429. }
  3430. /**
  3431. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  3432. * @param $name (string) Name of the spot color.
  3433. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3434. * @public
  3435. * @since 4.0.024 (2008-09-12)
  3436. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3437. */
  3438. public function SetDrawSpotColor($name, $tint=100) {
  3439. $this->setSpotColor('draw', $name, $tint);
  3440. }
  3441. /**
  3442. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  3443. * @param $name (string) Name of the spot color.
  3444. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3445. * @public
  3446. * @since 4.0.024 (2008-09-12)
  3447. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  3448. */
  3449. public function SetFillSpotColor($name, $tint=100) {
  3450. $this->setSpotColor('fill', $name, $tint);
  3451. }
  3452. /**
  3453. * Defines the spot color used for text.
  3454. * @param $name (string) Name of the spot color.
  3455. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3456. * @public
  3457. * @since 4.0.024 (2008-09-12)
  3458. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  3459. */
  3460. public function SetTextSpotColor($name, $tint=100) {
  3461. $this->setSpotColor('text', $name, $tint);
  3462. }
  3463. /**
  3464. * Set the color array for the specified type ('draw', 'fill', 'text').
  3465. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3466. * The method can be called before the first page is created and the value is retained from page to page.
  3467. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3468. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  3469. * @param $ret (boolean) If true do not send the PDF command.
  3470. * @return (string) The PDF command or empty string.
  3471. * @public
  3472. * @since 3.1.000 (2008-06-11)
  3473. */
  3474. public function setColorArray($type, $color, $ret=false) {
  3475. if (is_array($color)) {
  3476. $color = array_values($color);
  3477. // component: grey, RGB red or CMYK cyan
  3478. $c = isset($color[0]) ? $color[0] : -1;
  3479. // component: RGB green or CMYK magenta
  3480. $m = isset($color[1]) ? $color[1] : -1;
  3481. // component: RGB blue or CMYK yellow
  3482. $y = isset($color[2]) ? $color[2] : -1;
  3483. // component: CMYK black
  3484. $k = isset($color[3]) ? $color[3] : -1;
  3485. // color name
  3486. $name = isset($color[4]) ? $color[4] : '';
  3487. if ($c >= 0) {
  3488. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  3489. }
  3490. }
  3491. return '';
  3492. }
  3493. /**
  3494. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  3495. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  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. * @return string the PDF command
  3500. * @public
  3501. * @since 3.1.000 (2008-06-11)
  3502. * @see SetDrawColor()
  3503. */
  3504. public function SetDrawColorArray($color, $ret=false) {
  3505. return $this->setColorArray('draw', $color, $ret);
  3506. }
  3507. /**
  3508. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  3509. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3510. * The method can be called before the first page is created and the value is retained from page to page.
  3511. * @param $color (array) Array of colors (1, 3 or 4 values).
  3512. * @param $ret (boolean) If true do not send the PDF command.
  3513. * @public
  3514. * @since 3.1.000 (2008-6-11)
  3515. * @see SetFillColor()
  3516. */
  3517. public function SetFillColorArray($color, $ret=false) {
  3518. return $this->setColorArray('fill', $color, $ret);
  3519. }
  3520. /**
  3521. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  3522. * The method can be called before the first page is created and the value is retained from page to page.
  3523. * @param $color (array) Array of colors (1, 3 or 4 values).
  3524. * @param $ret (boolean) If true do not send the PDF command.
  3525. * @public
  3526. * @since 3.1.000 (2008-6-11)
  3527. * @see SetFillColor()
  3528. */
  3529. public function SetTextColorArray($color, $ret=false) {
  3530. return $this->setColorArray('text', $color, $ret);
  3531. }
  3532. /**
  3533. * Defines the color used by the specified type ('draw', 'fill', 'text').
  3534. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3535. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3536. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3537. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3538. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3539. * @param $ret (boolean) If true do not send the command.
  3540. * @param $name (string) spot color name (if any)
  3541. * @return (string) The PDF command or empty string.
  3542. * @public
  3543. * @since 5.9.125 (2011-10-03)
  3544. */
  3545. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3546. // set default values
  3547. if (!is_numeric($col1)) {
  3548. $col1 = 0;
  3549. }
  3550. if (!is_numeric($col2)) {
  3551. $col2 = -1;
  3552. }
  3553. if (!is_numeric($col3)) {
  3554. $col3 = -1;
  3555. }
  3556. if (!is_numeric($col4)) {
  3557. $col4 = -1;
  3558. }
  3559. // set color by case
  3560. $suffix = '';
  3561. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  3562. // Grey scale
  3563. $col1 = max(0, min(255, $col1));
  3564. $intcolor = array('G' => $col1);
  3565. $pdfcolor = sprintf('%F ', ($col1 / 255));
  3566. $suffix = 'g';
  3567. } elseif ($col4 == -1) {
  3568. // RGB
  3569. $col1 = max(0, min(255, $col1));
  3570. $col2 = max(0, min(255, $col2));
  3571. $col3 = max(0, min(255, $col3));
  3572. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  3573. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  3574. $suffix = 'rg';
  3575. } else {
  3576. $col1 = max(0, min(100, $col1));
  3577. $col2 = max(0, min(100, $col2));
  3578. $col3 = max(0, min(100, $col3));
  3579. $col4 = max(0, min(100, $col4));
  3580. if (empty($name)) {
  3581. // CMYK
  3582. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  3583. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  3584. $suffix = 'k';
  3585. } else {
  3586. // SPOT COLOR
  3587. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  3588. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  3589. $pdfcolor = $this->setSpotColor($type, $name, 100);
  3590. }
  3591. }
  3592. switch ($type) {
  3593. case 'draw': {
  3594. $pdfcolor .= strtoupper($suffix);
  3595. $this->DrawColor = $pdfcolor;
  3596. $this->strokecolor = $intcolor;
  3597. break;
  3598. }
  3599. case 'fill': {
  3600. $pdfcolor .= $suffix;
  3601. $this->FillColor = $pdfcolor;
  3602. $this->bgcolor = $intcolor;
  3603. break;
  3604. }
  3605. case 'text': {
  3606. $pdfcolor .= $suffix;
  3607. $this->TextColor = $pdfcolor;
  3608. $this->fgcolor = $intcolor;
  3609. break;
  3610. }
  3611. }
  3612. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3613. if (($type != 'text') AND ($this->state == 2)) {
  3614. if (!$ret) {
  3615. $this->_out($pdfcolor);
  3616. }
  3617. return $pdfcolor;
  3618. }
  3619. return '';
  3620. }
  3621. /**
  3622. * 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.
  3623. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3624. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3625. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3626. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3627. * @param $ret (boolean) If true do not send the command.
  3628. * @param $name (string) spot color name (if any)
  3629. * @return string the PDF command
  3630. * @public
  3631. * @since 1.3
  3632. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  3633. */
  3634. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3635. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  3636. }
  3637. /**
  3638. * 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.
  3639. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3640. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3641. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3642. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3643. * @param $ret (boolean) If true do not send the command.
  3644. * @param $name (string) Spot color name (if any).
  3645. * @return (string) The PDF command.
  3646. * @public
  3647. * @since 1.3
  3648. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  3649. */
  3650. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3651. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  3652. }
  3653. /**
  3654. * 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.
  3655. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3656. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3657. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3658. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3659. * @param $ret (boolean) If true do not send the command.
  3660. * @param $name (string) Spot color name (if any).
  3661. * @return (string) Empty string.
  3662. * @public
  3663. * @since 1.3
  3664. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  3665. */
  3666. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3667. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  3668. }
  3669. /**
  3670. * Returns the length of a string in user unit. A font must be selected.<br>
  3671. * @param $s (string) The string whose length is to be computed
  3672. * @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.
  3673. * @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.
  3674. * @param $fontsize (float) Font size in points. The default value is the current size.
  3675. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3676. * @return mixed int total string length or array of characted widths
  3677. * @author Nicola Asuni
  3678. * @public
  3679. * @since 1.2
  3680. */
  3681. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3682. 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);
  3683. }
  3684. /**
  3685. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  3686. * @param $sa (string) The array of chars whose total length is to be computed
  3687. * @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.
  3688. * @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.
  3689. * @param $fontsize (float) Font size in points. The default value is the current size.
  3690. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3691. * @return mixed int total string length or array of characted widths
  3692. * @author Nicola Asuni
  3693. * @public
  3694. * @since 2.4.000 (2008-03-06)
  3695. */
  3696. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3697. // store current values
  3698. if (!TCPDF_STATIC::empty_string($fontname)) {
  3699. $prev_FontFamily = $this->FontFamily;
  3700. $prev_FontStyle = $this->FontStyle;
  3701. $prev_FontSizePt = $this->FontSizePt;
  3702. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  3703. }
  3704. // convert UTF-8 array to Latin1 if required
  3705. if ($this->isunicode AND (!$this->isUnicodeFont())) {
  3706. $sa = TCPDF_FONTS::UTF8ArrToLatin1Arr($sa);
  3707. }
  3708. $w = 0; // total width
  3709. $wa = array(); // array of characters widths
  3710. foreach ($sa as $ck => $char) {
  3711. // character width
  3712. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  3713. $wa[] = $cw;
  3714. $w += $cw;
  3715. }
  3716. // restore previous values
  3717. if (!TCPDF_STATIC::empty_string($fontname)) {
  3718. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  3719. }
  3720. if ($getarray) {
  3721. return $wa;
  3722. }
  3723. return $w;
  3724. }
  3725. /**
  3726. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  3727. * @param $char (int) The char code whose length is to be returned
  3728. * @param $notlast (boolean) If false ignore the font-spacing.
  3729. * @return float char width
  3730. * @author Nicola Asuni
  3731. * @public
  3732. * @since 2.4.000 (2008-03-06)
  3733. */
  3734. public function GetCharWidth($char, $notlast=true) {
  3735. // get raw width
  3736. $chw = $this->getRawCharWidth($char);
  3737. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  3738. // increase/decrease font spacing
  3739. $chw += $this->font_spacing;
  3740. }
  3741. if ($this->font_stretching != 100) {
  3742. // fixed stretching mode
  3743. $chw *= ($this->font_stretching / 100);
  3744. }
  3745. return $chw;
  3746. }
  3747. /**
  3748. * Returns the length of the char in user unit for the current font.
  3749. * @param $char (int) The char code whose length is to be returned
  3750. * @return float char width
  3751. * @author Nicola Asuni
  3752. * @public
  3753. * @since 5.9.000 (2010-09-28)
  3754. */
  3755. public function getRawCharWidth($char) {
  3756. if ($char == 173) {
  3757. // SHY character will not be printed
  3758. return (0);
  3759. }
  3760. if (isset($this->CurrentFont['cw'][$char])) {
  3761. $w = $this->CurrentFont['cw'][$char];
  3762. } elseif (isset($this->CurrentFont['dw'])) {
  3763. // default width
  3764. $w = $this->CurrentFont['dw'];
  3765. } elseif (isset($this->CurrentFont['cw'][32])) {
  3766. // default width
  3767. $w = $this->CurrentFont['cw'][32];
  3768. } else {
  3769. $w = 600;
  3770. }
  3771. return $this->getAbsFontMeasure($w);
  3772. }
  3773. /**
  3774. * Returns the numbero of characters in a string.
  3775. * @param $s (string) The input string.
  3776. * @return int number of characters
  3777. * @public
  3778. * @since 2.0.0001 (2008-01-07)
  3779. */
  3780. public function GetNumChars($s) {
  3781. if ($this->isUnicodeFont()) {
  3782. return count(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont));
  3783. }
  3784. return strlen($s);
  3785. }
  3786. /**
  3787. * Fill the list of available fonts ($this->fontlist).
  3788. * @protected
  3789. * @since 4.0.013 (2008-07-28)
  3790. */
  3791. protected function getFontsList() {
  3792. if (($fontsdir = opendir(TCPDF_FONTS::_getfontpath())) !== false) {
  3793. while (($file = readdir($fontsdir)) !== false) {
  3794. if (substr($file, -4) == '.php') {
  3795. array_push($this->fontlist, strtolower(basename($file, '.php')));
  3796. }
  3797. }
  3798. closedir($fontsdir);
  3799. }
  3800. }
  3801. /**
  3802. * Returns the unicode caracter specified by the value
  3803. * @param $c (int) UTF-8 value
  3804. * @return Returns the specified character.
  3805. * @since 2.3.000 (2008-03-05)
  3806. * @public
  3807. * @deprecated
  3808. */
  3809. public function unichr($c) {
  3810. return TCPDF_FONTS::unichr($c, $this->isunicode);
  3811. }
  3812. /**
  3813. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  3814. * @param $fontfile (string) Font file (full path).
  3815. * @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.
  3816. * @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.
  3817. * @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.
  3818. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  3819. * @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).
  3820. * @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.
  3821. * @param $addcbbox (boolean) If true includes the character bounding box information on the php font file.
  3822. * @return (string) TCPDF font name.
  3823. * @author Nicola Asuni
  3824. * @since 5.9.123 (2010-09-30)
  3825. * @public
  3826. * @deprecated
  3827. */
  3828. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1, $addcbbox=false) {
  3829. return TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
  3830. }
  3831. /**
  3832. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  3833. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  3834. * 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.
  3835. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  3836. * @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>
  3837. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3838. * @return array containing the font data, or false in case of error.
  3839. * @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.
  3840. * @public
  3841. * @since 1.5
  3842. * @see SetFont(), setFontSubsetting()
  3843. */
  3844. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  3845. if ($subset === 'default') {
  3846. $subset = $this->font_subsetting;
  3847. }
  3848. if ($this->pdfa_mode) {
  3849. $subset = false;
  3850. }
  3851. if (TCPDF_STATIC::empty_string($family)) {
  3852. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  3853. $family = $this->FontFamily;
  3854. } else {
  3855. $this->Error('Empty font family');
  3856. }
  3857. }
  3858. // move embedded styles on $style
  3859. if (substr($family, -1) == 'I') {
  3860. $style .= 'I';
  3861. $family = substr($family, 0, -1);
  3862. }
  3863. if (substr($family, -1) == 'B') {
  3864. $style .= 'B';
  3865. $family = substr($family, 0, -1);
  3866. }
  3867. // normalize family name
  3868. $family = strtolower($family);
  3869. if ((!$this->isunicode) AND ($family == 'arial')) {
  3870. $family = 'helvetica';
  3871. }
  3872. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  3873. $style = '';
  3874. }
  3875. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  3876. // all fonts must be embedded
  3877. $family = 'pdfa'.$family;
  3878. }
  3879. $tempstyle = strtoupper($style);
  3880. $style = '';
  3881. // underline
  3882. if (strpos($tempstyle, 'U') !== false) {
  3883. $this->underline = true;
  3884. } else {
  3885. $this->underline = false;
  3886. }
  3887. // line-through (deleted)
  3888. if (strpos($tempstyle, 'D') !== false) {
  3889. $this->linethrough = true;
  3890. } else {
  3891. $this->linethrough = false;
  3892. }
  3893. // overline
  3894. if (strpos($tempstyle, 'O') !== false) {
  3895. $this->overline = true;
  3896. } else {
  3897. $this->overline = false;
  3898. }
  3899. // bold
  3900. if (strpos($tempstyle, 'B') !== false) {
  3901. $style .= 'B';
  3902. }
  3903. // oblique
  3904. if (strpos($tempstyle, 'I') !== false) {
  3905. $style .= 'I';
  3906. }
  3907. $bistyle = $style;
  3908. $fontkey = $family.$style;
  3909. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  3910. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  3911. // check if the font has been already added
  3912. $fb = $this->getFontBuffer($fontkey);
  3913. if ($fb !== false) {
  3914. if ($this->inxobj) {
  3915. // we are inside an XObject template
  3916. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  3917. }
  3918. return $fontdata;
  3919. }
  3920. // get specified font directory (if any)
  3921. $fontdir = false;
  3922. if (!TCPDF_STATIC::empty_string($fontfile)) {
  3923. $fontdir = dirname($fontfile);
  3924. if (TCPDF_STATIC::empty_string($fontdir) OR ($fontdir == '.')) {
  3925. $fontdir = '';
  3926. } else {
  3927. $fontdir .= '/';
  3928. }
  3929. }
  3930. // true when the font style variation is missing
  3931. $missing_style = false;
  3932. // search and include font file
  3933. if (TCPDF_STATIC::empty_string($fontfile) OR (!@file_exists($fontfile))) {
  3934. // build a standard filenames for specified font
  3935. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  3936. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3937. if (TCPDF_STATIC::empty_string($fontfile)) {
  3938. $missing_style = true;
  3939. // try to remove the style part
  3940. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  3941. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3942. }
  3943. }
  3944. // include font file
  3945. if (!TCPDF_STATIC::empty_string($fontfile) AND (@file_exists($fontfile))) {
  3946. include($fontfile);
  3947. } else {
  3948. $this->Error('Could not include font definition file: '.$family.'');
  3949. }
  3950. // check font parameters
  3951. if ((!isset($type)) OR (!isset($cw))) {
  3952. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  3953. }
  3954. // SET default parameters
  3955. if (!isset($file) OR TCPDF_STATIC::empty_string($file)) {
  3956. $file = '';
  3957. }
  3958. if (!isset($enc) OR TCPDF_STATIC::empty_string($enc)) {
  3959. $enc = '';
  3960. }
  3961. if (!isset($cidinfo) OR TCPDF_STATIC::empty_string($cidinfo)) {
  3962. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  3963. $cidinfo['uni2cid'] = array();
  3964. }
  3965. if (!isset($ctg) OR TCPDF_STATIC::empty_string($ctg)) {
  3966. $ctg = '';
  3967. }
  3968. if (!isset($desc) OR TCPDF_STATIC::empty_string($desc)) {
  3969. $desc = array();
  3970. }
  3971. if (!isset($up) OR TCPDF_STATIC::empty_string($up)) {
  3972. $up = -100;
  3973. }
  3974. if (!isset($ut) OR TCPDF_STATIC::empty_string($ut)) {
  3975. $ut = 50;
  3976. }
  3977. if (!isset($cw) OR TCPDF_STATIC::empty_string($cw)) {
  3978. $cw = array();
  3979. }
  3980. if (!isset($dw) OR TCPDF_STATIC::empty_string($dw)) {
  3981. // set default width
  3982. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  3983. $dw = $desc['MissingWidth'];
  3984. } elseif (isset($cw[32])) {
  3985. $dw = $cw[32];
  3986. } else {
  3987. $dw = 600;
  3988. }
  3989. }
  3990. ++$this->numfonts;
  3991. if ($type == 'core') {
  3992. $name = $this->CoreFonts[$fontkey];
  3993. $subset = false;
  3994. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  3995. $subset = false;
  3996. } elseif ($type == 'TrueTypeUnicode') {
  3997. $enc = 'Identity-H';
  3998. } elseif ($type == 'cidfont0') {
  3999. if ($this->pdfa_mode) {
  4000. $this->Error('All fonts must be embedded in PDF/A mode!');
  4001. }
  4002. } else {
  4003. $this->Error('Unknow font type: '.$type.'');
  4004. }
  4005. // set name if unset
  4006. if (!isset($name) OR empty($name)) {
  4007. $name = $fontkey;
  4008. }
  4009. // create artificial font style variations if missing (only works with non-embedded fonts)
  4010. if (($type != 'core') AND $missing_style) {
  4011. // style variations
  4012. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4013. $name .= $styles[$bistyle];
  4014. // artificial bold
  4015. if (strpos($bistyle, 'B') !== false) {
  4016. if (isset($desc['StemV'])) {
  4017. // from normal to bold
  4018. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4019. } else {
  4020. // bold
  4021. $desc['StemV'] = 123;
  4022. }
  4023. }
  4024. // artificial italic
  4025. if (strpos($bistyle, 'I') !== false) {
  4026. if (isset($desc['ItalicAngle'])) {
  4027. $desc['ItalicAngle'] -= 11;
  4028. } else {
  4029. $desc['ItalicAngle'] = -11;
  4030. }
  4031. if (isset($desc['Flags'])) {
  4032. $desc['Flags'] |= 64; //bit 7
  4033. } else {
  4034. $desc['Flags'] = 64;
  4035. }
  4036. }
  4037. }
  4038. // check if the array of characters bounding boxes is defined
  4039. if (!isset($cbbox)) {
  4040. $cbbox = array();
  4041. }
  4042. // initialize subsetchars
  4043. $subsetchars = array_fill(0, 255, true);
  4044. $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));
  4045. if ($this->inxobj) {
  4046. // we are inside an XObject template
  4047. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4048. }
  4049. if (isset($diff) AND (!empty($diff))) {
  4050. //Search existing encodings
  4051. $d = 0;
  4052. $nb = count($this->diffs);
  4053. for ($i=1; $i <= $nb; ++$i) {
  4054. if ($this->diffs[$i] == $diff) {
  4055. $d = $i;
  4056. break;
  4057. }
  4058. }
  4059. if ($d == 0) {
  4060. $d = $nb + 1;
  4061. $this->diffs[$d] = $diff;
  4062. }
  4063. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4064. }
  4065. if (!TCPDF_STATIC::empty_string($file)) {
  4066. if (!isset($this->FontFiles[$file])) {
  4067. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4068. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4069. } elseif ($type != 'core') {
  4070. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4071. }
  4072. } else {
  4073. // update fontkeys that are sharing this font file
  4074. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4075. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4076. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4077. }
  4078. }
  4079. }
  4080. return $fontdata;
  4081. }
  4082. /**
  4083. * Sets the font used to print character strings.
  4084. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4085. * The method can be called before the first page is created and the font is retained from page to page.
  4086. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4087. * 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 />
  4088. * @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.
  4089. * @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.
  4090. * @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
  4091. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4092. * @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.
  4093. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4094. * @author Nicola Asuni
  4095. * @public
  4096. * @since 1.0
  4097. * @see AddFont(), SetFontSize()
  4098. */
  4099. public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4100. //Select a font; size given in points
  4101. if ($size === null) {
  4102. $size = $this->FontSizePt;
  4103. }
  4104. if ($size < 0) {
  4105. $size = 0;
  4106. }
  4107. // try to add font (if not already added)
  4108. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4109. $this->FontFamily = $fontdata['family'];
  4110. $this->FontStyle = $fontdata['style'];
  4111. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  4112. // save subset chars of the previous font
  4113. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4114. }
  4115. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4116. $this->SetFontSize($size, $out);
  4117. }
  4118. /**
  4119. * Defines the size of the current font.
  4120. * @param $size (float) The font size in points.
  4121. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4122. * @public
  4123. * @since 1.0
  4124. * @see SetFont()
  4125. */
  4126. public function SetFontSize($size, $out=true) {
  4127. // font size in points
  4128. $this->FontSizePt = $size;
  4129. // font size in user units
  4130. $this->FontSize = $size / $this->k;
  4131. // calculate some font metrics
  4132. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4133. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4134. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4135. } else {
  4136. $font_height = $size * 1.219;
  4137. }
  4138. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4139. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4140. }
  4141. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4142. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4143. }
  4144. if (!isset($font_ascent) AND !isset($font_descent)) {
  4145. // core font
  4146. $font_ascent = 0.76 * $font_height;
  4147. $font_descent = $font_height - $font_ascent;
  4148. } elseif (!isset($font_descent)) {
  4149. $font_descent = $font_height - $font_ascent;
  4150. } elseif (!isset($font_ascent)) {
  4151. $font_ascent = $font_height - $font_descent;
  4152. }
  4153. $this->FontAscent = ($font_ascent / $this->k);
  4154. $this->FontDescent = ($font_descent / $this->k);
  4155. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
  4156. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4157. }
  4158. }
  4159. /**
  4160. * Returns the bounding box of the current font in user units.
  4161. * @return array
  4162. * @public
  4163. * @since 5.9.152 (2012-03-23)
  4164. */
  4165. public function getFontBBox() {
  4166. $fbbox = array();
  4167. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4168. $tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4169. $fbbox = array_map(array($this,'getAbsFontMeasure'), $tmpbbox);
  4170. } else {
  4171. // Find max width
  4172. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  4173. $maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
  4174. } else {
  4175. $maxw = 0;
  4176. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  4177. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  4178. }
  4179. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  4180. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  4181. }
  4182. if (isset($this->CurrentFont['dw'])) {
  4183. $maxw = max($maxw, $this->CurrentFont['dw']);
  4184. }
  4185. foreach ($this->CurrentFont['cw'] as $char => $w) {
  4186. $maxw = max($maxw, $w);
  4187. }
  4188. if ($maxw == 0) {
  4189. $maxw = 600;
  4190. }
  4191. $maxw = $this->getAbsFontMeasure($maxw);
  4192. }
  4193. $fbbox = array(0, (0 - $this->FontDescent), $maxw, $this->FontAscent);
  4194. }
  4195. return $fbbox;
  4196. }
  4197. /**
  4198. * Convert a relative font measure into absolute value.
  4199. * @param $s (int) Font measure.
  4200. * @return float Absolute measure.
  4201. * @since 5.9.186 (2012-09-13)
  4202. */
  4203. public function getAbsFontMeasure($s) {
  4204. return ($s * $this->FontSize / 1000);
  4205. }
  4206. /**
  4207. * Returns the glyph bounding box of the specified character in the current font in user units.
  4208. * @param $char (int) Input character code.
  4209. * @return mixed array(xMin, yMin, xMax, yMax) or FALSE if not defined.
  4210. * @since 5.9.186 (2012-09-13)
  4211. */
  4212. public function getCharBBox($char) {
  4213. if (isset($this->CurrentFont['cbbox'][$char])) {
  4214. return array_map(array($this,'getAbsFontMeasure'), $this->CurrentFont['cbbox'][intval($char)]);
  4215. }
  4216. return false;
  4217. }
  4218. /**
  4219. * Return the font descent value
  4220. * @param $font (string) font name
  4221. * @param $style (string) font style
  4222. * @param $size (float) The size (in points)
  4223. * @return int font descent
  4224. * @public
  4225. * @author Nicola Asuni
  4226. * @since 4.9.003 (2010-03-30)
  4227. */
  4228. public function getFontDescent($font, $style='', $size=0) {
  4229. $fontdata = $this->AddFont($font, $style);
  4230. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4231. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4232. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4233. } else {
  4234. $descent = (1.219 * 0.24 * $size);
  4235. }
  4236. return ($descent / $this->k);
  4237. }
  4238. /**
  4239. * Return the font ascent value.
  4240. * @param $font (string) font name
  4241. * @param $style (string) font style
  4242. * @param $size (float) The size (in points)
  4243. * @return int font ascent
  4244. * @public
  4245. * @author Nicola Asuni
  4246. * @since 4.9.003 (2010-03-30)
  4247. */
  4248. public function getFontAscent($font, $style='', $size=0) {
  4249. $fontdata = $this->AddFont($font, $style);
  4250. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4251. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4252. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4253. } else {
  4254. $ascent = 1.219 * 0.76 * $size;
  4255. }
  4256. return ($ascent / $this->k);
  4257. }
  4258. /**
  4259. * Return true in the character is present in the specified font.
  4260. * @param $char (mixed) Character to check (integer value or string)
  4261. * @param $font (string) Font name (family name).
  4262. * @param $style (string) Font style.
  4263. * @return (boolean) true if the char is defined, false otherwise.
  4264. * @public
  4265. * @since 5.9.153 (2012-03-28)
  4266. */
  4267. public function isCharDefined($char, $font='', $style='') {
  4268. if (is_string($char)) {
  4269. // get character code
  4270. $char = TCPDF_FONTS::UTF8StringToArray($char, $this->isunicode, $this->CurrentFont);
  4271. $char = $char[0];
  4272. }
  4273. if (TCPDF_STATIC::empty_string($font)) {
  4274. if (TCPDF_STATIC::empty_string($style)) {
  4275. return (isset($this->CurrentFont['cw'][intval($char)]));
  4276. }
  4277. $font = $this->FontFamily;
  4278. }
  4279. $fontdata = $this->AddFont($font, $style);
  4280. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4281. return (isset($fontinfo['cw'][intval($char)]));
  4282. }
  4283. /**
  4284. * Replace missing font characters on selected font with specified substitutions.
  4285. * @param $text (string) Text to process.
  4286. * @param $font (string) Font name (family name).
  4287. * @param $style (string) Font style.
  4288. * @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.
  4289. * @return (string) Processed text.
  4290. * @public
  4291. * @since 5.9.153 (2012-03-28)
  4292. */
  4293. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  4294. if (empty($subs)) {
  4295. return $text;
  4296. }
  4297. if (TCPDF_STATIC::empty_string($font)) {
  4298. $font = $this->FontFamily;
  4299. }
  4300. $fontdata = $this->AddFont($font, $style);
  4301. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4302. $uniarr = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  4303. foreach ($uniarr as $k => $chr) {
  4304. if (!isset($fontinfo['cw'][$chr])) {
  4305. // this character is missing on the selected font
  4306. if (isset($subs[$chr])) {
  4307. // we have available substitutions
  4308. if (is_array($subs[$chr])) {
  4309. foreach($subs[$chr] as $s) {
  4310. if (isset($fontinfo['cw'][$s])) {
  4311. $uniarr[$k] = $s;
  4312. break;
  4313. }
  4314. }
  4315. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  4316. $uniarr[$k] = $subs[$chr];
  4317. }
  4318. }
  4319. }
  4320. }
  4321. return TCPDF_FONTS::UniArrSubString(TCPDF_FONTS::UTF8ArrayToUniArray($uniarr, $this->isunicode));
  4322. }
  4323. /**
  4324. * Defines the default monospaced font.
  4325. * @param $font (string) Font name.
  4326. * @public
  4327. * @since 4.5.025
  4328. */
  4329. public function SetDefaultMonospacedFont($font) {
  4330. $this->default_monospaced_font = $font;
  4331. }
  4332. /**
  4333. * 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 />
  4334. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4335. * @public
  4336. * @since 1.5
  4337. * @see Cell(), Write(), Image(), Link(), SetLink()
  4338. */
  4339. public function AddLink() {
  4340. //Create a new internal link
  4341. $n = count($this->links) + 1;
  4342. $this->links[$n] = array(0, 0);
  4343. return $n;
  4344. }
  4345. /**
  4346. * Defines the page and position a link points to.
  4347. * @param $link (int) The link identifier returned by AddLink()
  4348. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4349. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  4350. * @public
  4351. * @since 1.5
  4352. * @see AddLink()
  4353. */
  4354. public function SetLink($link, $y=0, $page=-1) {
  4355. if ($y == -1) {
  4356. $y = $this->y;
  4357. }
  4358. if ($page == -1) {
  4359. $page = $this->page;
  4360. }
  4361. $this->links[$link] = array($page, $y);
  4362. }
  4363. /**
  4364. * Puts a link on a rectangular area of the page.
  4365. * 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.
  4366. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4367. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4368. * @param $w (float) Width of the rectangle
  4369. * @param $h (float) Height of the rectangle
  4370. * @param $link (mixed) URL or identifier returned by AddLink()
  4371. * @param $spaces (int) number of spaces on the text to link
  4372. * @public
  4373. * @since 1.5
  4374. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4375. */
  4376. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  4377. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  4378. }
  4379. /**
  4380. * Puts a markup annotation on a rectangular area of the page.
  4381. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  4382. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4383. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4384. * @param $w (float) Width of the rectangle
  4385. * @param $h (float) Height of the rectangle
  4386. * @param $text (string) annotation text or alternate content
  4387. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  4388. * @param $spaces (int) number of spaces on the text to link
  4389. * @public
  4390. * @since 4.0.018 (2008-08-06)
  4391. */
  4392. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  4393. if ($this->inxobj) {
  4394. // store parameters for later use on template
  4395. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  4396. return;
  4397. }
  4398. if ($x === '') {
  4399. $x = $this->x;
  4400. }
  4401. if ($y === '') {
  4402. $y = $this->y;
  4403. }
  4404. // check page for no-write regions and adapt page margins if necessary
  4405. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  4406. // recalculate coordinates to account for graphic transformations
  4407. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  4408. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  4409. $maxid = count($this->transfmatrix[$i]) - 1;
  4410. for ($j=$maxid; $j >= 0; --$j) {
  4411. $ctm = $this->transfmatrix[$i][$j];
  4412. if (isset($ctm['a'])) {
  4413. $x = $x * $this->k;
  4414. $y = ($this->h - $y) * $this->k;
  4415. $w = $w * $this->k;
  4416. $h = $h * $this->k;
  4417. // top left
  4418. $xt = $x;
  4419. $yt = $y;
  4420. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4421. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4422. // top right
  4423. $xt = $x + $w;
  4424. $yt = $y;
  4425. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4426. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4427. // bottom left
  4428. $xt = $x;
  4429. $yt = $y - $h;
  4430. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4431. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4432. // bottom right
  4433. $xt = $x + $w;
  4434. $yt = $y - $h;
  4435. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4436. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4437. // new coordinates (rectangle area)
  4438. $x = min($x1, $x2, $x3, $x4);
  4439. $y = max($y1, $y2, $y3, $y4);
  4440. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  4441. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  4442. $x = $x / $this->k;
  4443. $y = $this->h - ($y / $this->k);
  4444. }
  4445. }
  4446. }
  4447. }
  4448. if ($this->page <= 0) {
  4449. $page = 1;
  4450. } else {
  4451. $page = $this->page;
  4452. }
  4453. if (!isset($this->PageAnnots[$page])) {
  4454. $this->PageAnnots[$page] = array();
  4455. }
  4456. $this->PageAnnots[$page][] = array('n' => ++$this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  4457. if (!$this->pdfa_mode) {
  4458. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!TCPDF_STATIC::empty_string($opt['FS']))
  4459. AND (@file_exists($opt['FS']) OR TCPDF_STATIC::isValidURL($opt['FS']))
  4460. AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  4461. $this->embeddedfiles[basename($opt['FS'])] = array('f' => ++$this->n, 'n' => ++$this->n, 'file' => $opt['FS']);
  4462. }
  4463. }
  4464. // Add widgets annotation's icons
  4465. if (isset($opt['mk']['i']) AND @file_exists($opt['mk']['i'])) {
  4466. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  4467. }
  4468. if (isset($opt['mk']['ri']) AND @file_exists($opt['mk']['ri'])) {
  4469. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4470. }
  4471. if (isset($opt['mk']['ix']) AND @file_exists($opt['mk']['ix'])) {
  4472. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4473. }
  4474. }
  4475. /**
  4476. * Embedd the attached files.
  4477. * @since 4.4.000 (2008-12-07)
  4478. * @protected
  4479. * @see Annotation()
  4480. */
  4481. protected function _putEmbeddedFiles() {
  4482. if ($this->pdfa_mode) {
  4483. // embedded files are not allowed in PDF/A mode
  4484. return;
  4485. }
  4486. reset($this->embeddedfiles);
  4487. foreach ($this->embeddedfiles as $filename => $filedata) {
  4488. $data = TCPDF_STATIC::fileGetContents($filedata['file']);
  4489. if ($data !== FALSE) {
  4490. $rawsize = strlen($data);
  4491. if ($rawsize > 0) {
  4492. // update name tree
  4493. $this->efnames[$filename] = $filedata['f'].' 0 R';
  4494. // embedded file specification object
  4495. $out = $this->_getobj($filedata['f'])."\n";
  4496. $out .= '<</Type /Filespec /F '.$this->_datastring($filename, $filedata['f']).' /EF <</F '.$filedata['n'].' 0 R>> >>';
  4497. $out .= "\n".'endobj';
  4498. $this->_out($out);
  4499. // embedded file object
  4500. $filter = '';
  4501. if ($this->compress) {
  4502. $data = gzcompress($data);
  4503. $filter = ' /Filter /FlateDecode';
  4504. }
  4505. $stream = $this->_getrawstream($data, $filedata['n']);
  4506. $out = $this->_getobj($filedata['n'])."\n";
  4507. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' /Params <</Size '.$rawsize.'>> >>';
  4508. $out .= ' stream'."\n".$stream."\n".'endstream';
  4509. $out .= "\n".'endobj';
  4510. $this->_out($out);
  4511. }
  4512. }
  4513. }
  4514. }
  4515. /**
  4516. * Prints a text cell at the specified position.
  4517. * This method allows to place a string precisely on the page.
  4518. * @param $x (float) Abscissa of the cell origin
  4519. * @param $y (float) Ordinate of the cell origin
  4520. * @param $txt (string) String to print
  4521. * @param $fstroke (int) outline size in user units (false = disable)
  4522. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  4523. * @param $ffill (boolean) if true fills the text
  4524. * @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)))
  4525. * @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.
  4526. * @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>
  4527. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4528. * @param $link (mixed) URL or identifier returned by AddLink().
  4529. * @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.
  4530. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4531. * @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>
  4532. * @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>
  4533. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  4534. * @public
  4535. * @since 1.0
  4536. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  4537. */
  4538. 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) {
  4539. $textrendermode = $this->textrendermode;
  4540. $textstrokewidth = $this->textstrokewidth;
  4541. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  4542. $this->SetXY($x, $y, $rtloff);
  4543. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  4544. // restore previous rendering mode
  4545. $this->textrendermode = $textrendermode;
  4546. $this->textstrokewidth = $textstrokewidth;
  4547. }
  4548. /**
  4549. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  4550. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  4551. * This method is called automatically and should not be called directly by the application.
  4552. * @return boolean
  4553. * @public
  4554. * @since 1.4
  4555. * @see SetAutoPageBreak()
  4556. */
  4557. public function AcceptPageBreak() {
  4558. if ($this->num_columns > 1) {
  4559. // multi column mode
  4560. if ($this->current_column < ($this->num_columns - 1)) {
  4561. // go to next column
  4562. $this->selectColumn($this->current_column + 1);
  4563. } elseif ($this->AutoPageBreak) {
  4564. // add a new page
  4565. $this->AddPage();
  4566. // set first column
  4567. $this->selectColumn(0);
  4568. }
  4569. // avoid page breaking from checkPageBreak()
  4570. return false;
  4571. }
  4572. return $this->AutoPageBreak;
  4573. }
  4574. /**
  4575. * Add page if needed.
  4576. * @param $h (float) Cell height. Default value: 0.
  4577. * @param $y (mixed) starting y position, leave empty for current position.
  4578. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  4579. * @return boolean true in case of page break, false otherwise.
  4580. * @since 3.2.000 (2008-07-01)
  4581. * @protected
  4582. */
  4583. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  4584. if (TCPDF_STATIC::empty_string($y)) {
  4585. $y = $this->y;
  4586. }
  4587. $current_page = $this->page;
  4588. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  4589. if ($addpage) {
  4590. //Automatic page break
  4591. $x = $this->x;
  4592. $this->AddPage($this->CurOrientation);
  4593. $this->y = $this->tMargin;
  4594. $oldpage = $this->page - 1;
  4595. if ($this->rtl) {
  4596. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  4597. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  4598. } else {
  4599. $this->x = $x;
  4600. }
  4601. } else {
  4602. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  4603. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  4604. } else {
  4605. $this->x = $x;
  4606. }
  4607. }
  4608. }
  4609. return true;
  4610. }
  4611. if ($current_page != $this->page) {
  4612. // account for columns mode
  4613. return true;
  4614. }
  4615. return false;
  4616. }
  4617. /**
  4618. * 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 />
  4619. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4620. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4621. * @param $h (float) Cell height. Default value: 0.
  4622. * @param $txt (string) String to print. Default value: empty string.
  4623. * @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)))
  4624. * @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.
  4625. * @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>
  4626. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4627. * @param $link (mixed) URL or identifier returned by AddLink().
  4628. * @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.
  4629. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4630. * @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>
  4631. * @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>
  4632. * @public
  4633. * @since 1.0
  4634. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  4635. */
  4636. 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') {
  4637. $prev_cell_margin = $this->cell_margin;
  4638. $prev_cell_padding = $this->cell_padding;
  4639. $this->adjustCellPadding($border);
  4640. if (!$ignore_min_height) {
  4641. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  4642. if ($h < $min_cell_height) {
  4643. $h = $min_cell_height;
  4644. }
  4645. }
  4646. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  4647. // apply text shadow if enabled
  4648. if ($this->txtshadow['enabled']) {
  4649. // save data
  4650. $x = $this->x;
  4651. $y = $this->y;
  4652. $bc = $this->bgcolor;
  4653. $fc = $this->fgcolor;
  4654. $sc = $this->strokecolor;
  4655. $alpha = $this->alpha;
  4656. // print shadow
  4657. $this->x += $this->txtshadow['depth_w'];
  4658. $this->y += $this->txtshadow['depth_h'];
  4659. $this->SetFillColorArray($this->txtshadow['color']);
  4660. $this->SetTextColorArray($this->txtshadow['color']);
  4661. $this->SetDrawColorArray($this->txtshadow['color']);
  4662. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4663. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  4664. }
  4665. if ($this->state == 2) {
  4666. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4667. }
  4668. //restore data
  4669. $this->x = $x;
  4670. $this->y = $y;
  4671. $this->SetFillColorArray($bc);
  4672. $this->SetTextColorArray($fc);
  4673. $this->SetDrawColorArray($sc);
  4674. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4675. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  4676. }
  4677. }
  4678. if ($this->state == 2) {
  4679. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4680. }
  4681. $this->cell_padding = $prev_cell_padding;
  4682. $this->cell_margin = $prev_cell_margin;
  4683. }
  4684. /**
  4685. * 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 />
  4686. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4687. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4688. * @param $h (float) Cell height. Default value: 0.
  4689. * @param $txt (string) String to print. Default value: empty string.
  4690. * @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)))
  4691. * @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.
  4692. * @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>
  4693. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4694. * @param $link (mixed) URL or identifier returned by AddLink().
  4695. * @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.
  4696. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4697. * @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>
  4698. * @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>
  4699. * @return string containing cell code
  4700. * @protected
  4701. * @since 1.0
  4702. * @see Cell()
  4703. */
  4704. 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') {
  4705. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  4706. $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
  4707. $prev_cell_margin = $this->cell_margin;
  4708. $prev_cell_padding = $this->cell_padding;
  4709. $txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode);
  4710. $rs = ''; //string to be returned
  4711. $this->adjustCellPadding($border);
  4712. if (!$ignore_min_height) {
  4713. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  4714. if ($h < $min_cell_height) {
  4715. $h = $min_cell_height;
  4716. }
  4717. }
  4718. $k = $this->k;
  4719. // check page for no-write regions and adapt page margins if necessary
  4720. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  4721. if ($this->rtl) {
  4722. $x = $this->x - $this->cell_margin['R'];
  4723. } else {
  4724. $x = $this->x + $this->cell_margin['L'];
  4725. }
  4726. $y = $this->y + $this->cell_margin['T'];
  4727. $prev_font_stretching = $this->font_stretching;
  4728. $prev_font_spacing = $this->font_spacing;
  4729. // cell vertical alignment
  4730. switch ($calign) {
  4731. case 'A': {
  4732. // font top
  4733. switch ($valign) {
  4734. case 'T': {
  4735. // top
  4736. $y -= $this->cell_padding['T'];
  4737. break;
  4738. }
  4739. case 'B': {
  4740. // bottom
  4741. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  4742. break;
  4743. }
  4744. default:
  4745. case 'C':
  4746. case 'M': {
  4747. // center
  4748. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  4749. break;
  4750. }
  4751. }
  4752. break;
  4753. }
  4754. case 'L': {
  4755. // font baseline
  4756. switch ($valign) {
  4757. case 'T': {
  4758. // top
  4759. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  4760. break;
  4761. }
  4762. case 'B': {
  4763. // bottom
  4764. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  4765. break;
  4766. }
  4767. default:
  4768. case 'C':
  4769. case 'M': {
  4770. // center
  4771. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  4772. break;
  4773. }
  4774. }
  4775. break;
  4776. }
  4777. case 'D': {
  4778. // font bottom
  4779. switch ($valign) {
  4780. case 'T': {
  4781. // top
  4782. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  4783. break;
  4784. }
  4785. case 'B': {
  4786. // bottom
  4787. $y -= ($h - $this->cell_padding['B']);
  4788. break;
  4789. }
  4790. default:
  4791. case 'C':
  4792. case 'M': {
  4793. // center
  4794. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  4795. break;
  4796. }
  4797. }
  4798. break;
  4799. }
  4800. case 'B': {
  4801. // cell bottom
  4802. $y -= $h;
  4803. break;
  4804. }
  4805. case 'C':
  4806. case 'M': {
  4807. // cell center
  4808. $y -= ($h / 2);
  4809. break;
  4810. }
  4811. default:
  4812. case 'T': {
  4813. // cell top
  4814. break;
  4815. }
  4816. }
  4817. // text vertical alignment
  4818. switch ($valign) {
  4819. case 'T': {
  4820. // top
  4821. $yt = $y + $this->cell_padding['T'];
  4822. break;
  4823. }
  4824. case 'B': {
  4825. // bottom
  4826. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  4827. break;
  4828. }
  4829. default:
  4830. case 'C':
  4831. case 'M': {
  4832. // center
  4833. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  4834. break;
  4835. }
  4836. }
  4837. $basefonty = $yt + $this->FontAscent;
  4838. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  4839. if ($this->rtl) {
  4840. $w = $x - $this->lMargin;
  4841. } else {
  4842. $w = $this->w - $this->rMargin - $x;
  4843. }
  4844. }
  4845. $s = '';
  4846. // fill and borders
  4847. if (is_string($border) AND (strlen($border) == 4)) {
  4848. // full border
  4849. $border = 1;
  4850. }
  4851. if ($fill OR ($border == 1)) {
  4852. if ($fill) {
  4853. $op = ($border == 1) ? 'B' : 'f';
  4854. } else {
  4855. $op = 'S';
  4856. }
  4857. if ($this->rtl) {
  4858. $xk = (($x - $w) * $k);
  4859. } else {
  4860. $xk = ($x * $k);
  4861. }
  4862. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  4863. }
  4864. // draw borders
  4865. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  4866. if ($txt != '') {
  4867. $txt2 = $txt;
  4868. if ($this->isunicode) {
  4869. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  4870. $txt2 = TCPDF_FONTS::UTF8ToLatin1($txt2, $this->isunicode, $this->CurrentFont);
  4871. } else {
  4872. $unicode = TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont); // array of UTF-8 unicode values
  4873. $unicode = TCPDF_FONTS::utf8Bidi($unicode, '', $this->tmprtl, $this->isunicode, $this->CurrentFont);
  4874. // replace thai chars (if any)
  4875. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  4876. // number of chars
  4877. $numchars = count($unicode);
  4878. // po pla, for far, for fan
  4879. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  4880. // do chada, to patak
  4881. $lowtail = array(0x0e0e, 0x0e0f);
  4882. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  4883. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  4884. // mai ek, mai tho, mai tri, mai chattawa, karan
  4885. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  4886. // sara u, sara uu, pinthu
  4887. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  4888. $output = array();
  4889. for ($i = 0; $i < $numchars; $i++) {
  4890. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  4891. $ch0 = $unicode[$i];
  4892. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  4893. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  4894. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  4895. if (in_array($ch0, $tonemark)) {
  4896. if ($chn == 0x0e33) {
  4897. // sara um
  4898. if (in_array($ch1, $longtail)) {
  4899. // tonemark at upper left
  4900. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4901. } else {
  4902. // tonemark at upper right (normal position)
  4903. $output[] = $ch0;
  4904. }
  4905. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  4906. // tonemark at lower left
  4907. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  4908. } elseif (in_array($ch1, $upvowel)) {
  4909. if (in_array($ch2, $longtail)) {
  4910. // tonemark at upper left
  4911. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4912. } else {
  4913. // tonemark at upper right (normal position)
  4914. $output[] = $ch0;
  4915. }
  4916. } else {
  4917. // tonemark at lower right
  4918. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  4919. }
  4920. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  4921. // add lower left nikhahit and sara aa
  4922. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  4923. $output[] = 0xf711;
  4924. $this->CurrentFont['subsetchars'][0xf711] = true;
  4925. $output[] = 0x0e32;
  4926. $this->CurrentFont['subsetchars'][0x0e32] = true;
  4927. } else {
  4928. $output[] = $ch0;
  4929. }
  4930. } elseif (in_array($ch1, $longtail)) {
  4931. if ($ch0 == 0x0e31) {
  4932. // lower left mai hun arkad
  4933. $output[] = $this->replaceChar($ch0, 0xf710);
  4934. } elseif (in_array($ch0, $upvowel)) {
  4935. // lower left
  4936. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  4937. } elseif ($ch0 == 0x0e47) {
  4938. // lower left mai tai koo
  4939. $output[] = $this->replaceChar($ch0, 0xf712);
  4940. } else {
  4941. // normal character
  4942. $output[] = $ch0;
  4943. }
  4944. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  4945. // lower vowel
  4946. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  4947. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  4948. // yo ying without lower part
  4949. $output[] = $this->replaceChar($ch0, 0xf70f);
  4950. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  4951. // tho santan without lower part
  4952. $output[] = $this->replaceChar($ch0, 0xf700);
  4953. } else {
  4954. $output[] = $ch0;
  4955. }
  4956. } else {
  4957. // non-thai character
  4958. $output[] = $unicode[$i];
  4959. }
  4960. }
  4961. $unicode = $output;
  4962. // update font subsetchars
  4963. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4964. } // end of K_THAI_TOPCHARS
  4965. $txt2 = TCPDF_FONTS::arrUTF8ToUTF16BE($unicode, false);
  4966. }
  4967. }
  4968. $txt2 = TCPDF_STATIC::_escape($txt2);
  4969. // get current text width (considering general font stretching and spacing)
  4970. $txwidth = $this->GetStringWidth($txt);
  4971. $width = $txwidth;
  4972. // check for stretch mode
  4973. if ($stretch > 0) {
  4974. // calculate ratio between cell width and text width
  4975. if ($width <= 0) {
  4976. $ratio = 1;
  4977. } else {
  4978. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  4979. }
  4980. // check if stretching is required
  4981. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  4982. // the text will be stretched to fit cell width
  4983. if ($stretch > 2) {
  4984. // set new character spacing
  4985. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  4986. } else {
  4987. // set new horizontal stretching
  4988. $this->font_stretching *= $ratio;
  4989. }
  4990. // recalculate text width (the text fills the entire cell)
  4991. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  4992. // reset alignment
  4993. $align = '';
  4994. }
  4995. }
  4996. if ($this->font_stretching != 100) {
  4997. // apply font stretching
  4998. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  4999. }
  5000. if ($this->font_spacing != 0) {
  5001. // increase/decrease font spacing
  5002. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  5003. }
  5004. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5005. $s .= 'q '.$this->TextColor.' ';
  5006. }
  5007. // rendering mode
  5008. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  5009. // count number of spaces
  5010. $ns = substr_count($txt, chr(32));
  5011. // Justification
  5012. $spacewidth = 0;
  5013. if (($align == 'J') AND ($ns > 0)) {
  5014. if ($this->isUnicodeFont()) {
  5015. // get string width without spaces
  5016. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5017. // calculate average space width
  5018. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / ($this->FontSize?$this->FontSize:1);
  5019. if ($this->font_stretching != 100) {
  5020. // word spacing is affected by stretching
  5021. $spacewidth /= ($this->font_stretching / 100);
  5022. }
  5023. // set word position to be used with TJ operator
  5024. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  5025. $unicode_justification = true;
  5026. } else {
  5027. // get string width
  5028. $width = $txwidth;
  5029. // new space width
  5030. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5031. if ($this->font_stretching != 100) {
  5032. // word spacing (Tw) is affected by stretching
  5033. $spacewidth /= ($this->font_stretching / 100);
  5034. }
  5035. // set word spacing
  5036. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5037. }
  5038. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5039. }
  5040. // replace carriage return characters
  5041. $txt2 = str_replace("\r", ' ', $txt2);
  5042. switch ($align) {
  5043. case 'C': {
  5044. $dx = ($w - $width) / 2;
  5045. break;
  5046. }
  5047. case 'R': {
  5048. if ($this->rtl) {
  5049. $dx = $this->cell_padding['R'];
  5050. } else {
  5051. $dx = $w - $width - $this->cell_padding['R'];
  5052. }
  5053. break;
  5054. }
  5055. case 'L': {
  5056. if ($this->rtl) {
  5057. $dx = $w - $width - $this->cell_padding['L'];
  5058. } else {
  5059. $dx = $this->cell_padding['L'];
  5060. }
  5061. break;
  5062. }
  5063. case 'J':
  5064. default: {
  5065. if ($this->rtl) {
  5066. $dx = $this->cell_padding['R'];
  5067. } else {
  5068. $dx = $this->cell_padding['L'];
  5069. }
  5070. break;
  5071. }
  5072. }
  5073. if ($this->rtl) {
  5074. $xdx = $x - $dx - $width;
  5075. } else {
  5076. $xdx = $x + $dx;
  5077. }
  5078. $xdk = $xdx * $k;
  5079. // print text
  5080. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5081. if (isset($uniblock)) {
  5082. // print overlapping characters as separate string
  5083. $xshift = 0; // horizontal shift
  5084. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5085. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5086. foreach ($uniblock as $uk => $uniarr) {
  5087. if (($uk % 2) == 0) {
  5088. // x space to skip
  5089. if ($spacewidth != 0) {
  5090. // justification shift
  5091. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5092. }
  5093. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5094. } else {
  5095. // character to print
  5096. $topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false);
  5097. $topchr = TCPDF_STATIC::_escape($topchr);
  5098. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5099. }
  5100. }
  5101. }
  5102. if ($this->underline) {
  5103. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5104. }
  5105. if ($this->linethrough) {
  5106. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5107. }
  5108. if ($this->overline) {
  5109. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5110. }
  5111. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5112. $s .= ' Q';
  5113. }
  5114. if ($link) {
  5115. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5116. }
  5117. }
  5118. // output cell
  5119. if ($s) {
  5120. // output cell
  5121. $rs .= $s;
  5122. if ($this->font_spacing != 0) {
  5123. // reset font spacing mode
  5124. $rs .= ' BT 0 Tc ET';
  5125. }
  5126. if ($this->font_stretching != 100) {
  5127. // reset font stretching mode
  5128. $rs .= ' BT 100 Tz ET';
  5129. }
  5130. }
  5131. // reset word spacing
  5132. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5133. $rs .= ' BT 0 Tw ET';
  5134. }
  5135. // reset stretching and spacing
  5136. $this->font_stretching = $prev_font_stretching;
  5137. $this->font_spacing = $prev_font_spacing;
  5138. $this->lasth = $h;
  5139. if ($ln > 0) {
  5140. //Go to the beginning of the next line
  5141. $this->y = $y + $h + $this->cell_margin['B'];
  5142. if ($ln == 1) {
  5143. if ($this->rtl) {
  5144. $this->x = $this->w - $this->rMargin;
  5145. } else {
  5146. $this->x = $this->lMargin;
  5147. }
  5148. }
  5149. } else {
  5150. // go left or right by case
  5151. if ($this->rtl) {
  5152. $this->x = $x - $w - $this->cell_margin['L'];
  5153. } else {
  5154. $this->x = $x + $w + $this->cell_margin['R'];
  5155. }
  5156. }
  5157. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5158. $rs = $gstyles.$rs;
  5159. $this->cell_padding = $prev_cell_padding;
  5160. $this->cell_margin = $prev_cell_margin;
  5161. return $rs;
  5162. }
  5163. /**
  5164. * Replace a char if is defined on the current font.
  5165. * @param $oldchar (int) Integer code (unicode) of the character to replace.
  5166. * @param $newchar (int) Integer code (unicode) of the new character.
  5167. * @return int the replaced char or the old char in case the new char i not defined
  5168. * @protected
  5169. * @since 5.9.167 (2012-06-22)
  5170. */
  5171. protected function replaceChar($oldchar, $newchar) {
  5172. if ($this->isCharDefined($newchar)) {
  5173. // add the new char on the subset list
  5174. $this->CurrentFont['subsetchars'][$newchar] = true;
  5175. // return the new character
  5176. return $newchar;
  5177. }
  5178. // return the old char
  5179. return $oldchar;
  5180. }
  5181. /**
  5182. * Returns the code to draw the cell border
  5183. * @param $x (float) X coordinate.
  5184. * @param $y (float) Y coordinate.
  5185. * @param $w (float) Cell width.
  5186. * @param $h (float) Cell height.
  5187. * @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)))
  5188. * @return string containing cell border code
  5189. * @protected
  5190. * @see SetLineStyle()
  5191. * @since 5.7.000 (2010-08-02)
  5192. */
  5193. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5194. $s = ''; // string to be returned
  5195. if (empty($brd)) {
  5196. return $s;
  5197. }
  5198. if ($brd == 1) {
  5199. $brd = array('LRTB' => true);
  5200. }
  5201. // calculate coordinates for border
  5202. $k = $this->k;
  5203. if ($this->rtl) {
  5204. $xeL = ($x - $w) * $k;
  5205. $xeR = $x * $k;
  5206. } else {
  5207. $xeL = $x * $k;
  5208. $xeR = ($x + $w) * $k;
  5209. }
  5210. $yeL = (($this->h - ($y + $h)) * $k);
  5211. $yeT = (($this->h - $y) * $k);
  5212. $xeT = $xeL;
  5213. $xeB = $xeR;
  5214. $yeR = $yeT;
  5215. $yeB = $yeL;
  5216. if (is_string($brd)) {
  5217. // convert string to array
  5218. $slen = strlen($brd);
  5219. $newbrd = array();
  5220. for ($i = 0; $i < $slen; ++$i) {
  5221. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5222. }
  5223. $brd = $newbrd;
  5224. }
  5225. if (isset($brd['mode'])) {
  5226. $mode = $brd['mode'];
  5227. unset($brd['mode']);
  5228. } else {
  5229. $mode = 'normal';
  5230. }
  5231. foreach ($brd as $border => $style) {
  5232. if (is_array($style) AND !empty($style)) {
  5233. // apply border style
  5234. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5235. $s .= $this->SetLineStyle($style, true)."\n";
  5236. }
  5237. switch ($mode) {
  5238. case 'ext': {
  5239. $off = (($this->LineWidth / 2) * $k);
  5240. $xL = $xeL - $off;
  5241. $xR = $xeR + $off;
  5242. $yT = $yeT + $off;
  5243. $yL = $yeL - $off;
  5244. $xT = $xL;
  5245. $xB = $xR;
  5246. $yR = $yT;
  5247. $yB = $yL;
  5248. $w += $this->LineWidth;
  5249. $h += $this->LineWidth;
  5250. break;
  5251. }
  5252. case 'int': {
  5253. $off = ($this->LineWidth / 2) * $k;
  5254. $xL = $xeL + $off;
  5255. $xR = $xeR - $off;
  5256. $yT = $yeT - $off;
  5257. $yL = $yeL + $off;
  5258. $xT = $xL;
  5259. $xB = $xR;
  5260. $yR = $yT;
  5261. $yB = $yL;
  5262. $w -= $this->LineWidth;
  5263. $h -= $this->LineWidth;
  5264. break;
  5265. }
  5266. case 'normal':
  5267. default: {
  5268. $xL = $xeL;
  5269. $xT = $xeT;
  5270. $xB = $xeB;
  5271. $xR = $xeR;
  5272. $yL = $yeL;
  5273. $yT = $yeT;
  5274. $yB = $yeB;
  5275. $yR = $yeR;
  5276. break;
  5277. }
  5278. }
  5279. // draw borders by case
  5280. if (strlen($border) == 4) {
  5281. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5282. } elseif (strlen($border) == 3) {
  5283. if (strpos($border,'B') === false) { // LTR
  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 .= sprintf('%F %F l ', $xB, $yB);
  5288. $s .= 'S ';
  5289. } elseif (strpos($border,'L') === false) { // TRB
  5290. $s .= sprintf('%F %F m ', $xT, $yT);
  5291. $s .= sprintf('%F %F l ', $xR, $yR);
  5292. $s .= sprintf('%F %F l ', $xB, $yB);
  5293. $s .= sprintf('%F %F l ', $xL, $yL);
  5294. $s .= 'S ';
  5295. } elseif (strpos($border,'T') === false) { // RBL
  5296. $s .= sprintf('%F %F m ', $xR, $yR);
  5297. $s .= sprintf('%F %F l ', $xB, $yB);
  5298. $s .= sprintf('%F %F l ', $xL, $yL);
  5299. $s .= sprintf('%F %F l ', $xT, $yT);
  5300. $s .= 'S ';
  5301. } elseif (strpos($border,'R') === false) { // BLT
  5302. $s .= sprintf('%F %F m ', $xB, $yB);
  5303. $s .= sprintf('%F %F l ', $xL, $yL);
  5304. $s .= sprintf('%F %F l ', $xT, $yT);
  5305. $s .= sprintf('%F %F l ', $xR, $yR);
  5306. $s .= 'S ';
  5307. }
  5308. } elseif (strlen($border) == 2) {
  5309. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5310. $s .= sprintf('%F %F m ', $xL, $yL);
  5311. $s .= sprintf('%F %F l ', $xT, $yT);
  5312. $s .= sprintf('%F %F l ', $xR, $yR);
  5313. $s .= 'S ';
  5314. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5315. $s .= sprintf('%F %F m ', $xT, $yT);
  5316. $s .= sprintf('%F %F l ', $xR, $yR);
  5317. $s .= sprintf('%F %F l ', $xB, $yB);
  5318. $s .= 'S ';
  5319. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5320. $s .= sprintf('%F %F m ', $xR, $yR);
  5321. $s .= sprintf('%F %F l ', $xB, $yB);
  5322. $s .= sprintf('%F %F l ', $xL, $yL);
  5323. $s .= 'S ';
  5324. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5325. $s .= sprintf('%F %F m ', $xB, $yB);
  5326. $s .= sprintf('%F %F l ', $xL, $yL);
  5327. $s .= sprintf('%F %F l ', $xT, $yT);
  5328. $s .= 'S ';
  5329. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5330. $s .= sprintf('%F %F m ', $xL, $yL);
  5331. $s .= sprintf('%F %F l ', $xT, $yT);
  5332. $s .= 'S ';
  5333. $s .= sprintf('%F %F m ', $xR, $yR);
  5334. $s .= sprintf('%F %F l ', $xB, $yB);
  5335. $s .= 'S ';
  5336. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5337. $s .= sprintf('%F %F m ', $xT, $yT);
  5338. $s .= sprintf('%F %F l ', $xR, $yR);
  5339. $s .= 'S ';
  5340. $s .= sprintf('%F %F m ', $xB, $yB);
  5341. $s .= sprintf('%F %F l ', $xL, $yL);
  5342. $s .= 'S ';
  5343. }
  5344. } else { // strlen($border) == 1
  5345. if (strpos($border,'L') !== false) { // L
  5346. $s .= sprintf('%F %F m ', $xL, $yL);
  5347. $s .= sprintf('%F %F l ', $xT, $yT);
  5348. $s .= 'S ';
  5349. } elseif (strpos($border,'T') !== false) { // T
  5350. $s .= sprintf('%F %F m ', $xT, $yT);
  5351. $s .= sprintf('%F %F l ', $xR, $yR);
  5352. $s .= 'S ';
  5353. } elseif (strpos($border,'R') !== false) { // R
  5354. $s .= sprintf('%F %F m ', $xR, $yR);
  5355. $s .= sprintf('%F %F l ', $xB, $yB);
  5356. $s .= 'S ';
  5357. } elseif (strpos($border,'B') !== false) { // B
  5358. $s .= sprintf('%F %F m ', $xB, $yB);
  5359. $s .= sprintf('%F %F l ', $xL, $yL);
  5360. $s .= 'S ';
  5361. }
  5362. }
  5363. if (is_array($style) AND !empty($style)) {
  5364. // reset border style to previous value
  5365. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5366. }
  5367. }
  5368. return $s;
  5369. }
  5370. /**
  5371. * This method allows printing text with line breaks.
  5372. * 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 />
  5373. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5374. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5375. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5376. * @param $txt (string) String to print
  5377. * @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)))
  5378. * @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>
  5379. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5380. * @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>
  5381. * @param $x (float) x position in user units
  5382. * @param $y (float) y position in user units
  5383. * @param $reseth (boolean) if true reset the last cell height (default true).
  5384. * @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.
  5385. * @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.
  5386. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5387. * @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.
  5388. * @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.
  5389. * @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).
  5390. * @return int Return the number of cells or 1 for html mode.
  5391. * @public
  5392. * @since 1.3
  5393. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5394. */
  5395. 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) {
  5396. $prev_cell_margin = $this->cell_margin;
  5397. $prev_cell_padding = $this->cell_padding;
  5398. // adjust internal padding
  5399. $this->adjustCellPadding($border);
  5400. $mc_padding = $this->cell_padding;
  5401. $mc_margin = $this->cell_margin;
  5402. $this->cell_padding['T'] = 0;
  5403. $this->cell_padding['B'] = 0;
  5404. $this->setCellMargins(0, 0, 0, 0);
  5405. if (TCPDF_STATIC::empty_string($this->lasth) OR $reseth) {
  5406. // reset row height
  5407. $this->resetLastH();
  5408. }
  5409. if (!TCPDF_STATIC::empty_string($y)) {
  5410. $this->SetY($y);
  5411. } else {
  5412. $y = $this->GetY();
  5413. }
  5414. $resth = 0;
  5415. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5416. // spit cell in more pages/columns
  5417. $newh = ($this->PageBreakTrigger - $y);
  5418. $resth = ($h - $newh); // cell to be printed on the next page/column
  5419. $h = $newh;
  5420. }
  5421. // get current page number
  5422. $startpage = $this->page;
  5423. // get current column
  5424. $startcolumn = $this->current_column;
  5425. if (!TCPDF_STATIC::empty_string($x)) {
  5426. $this->SetX($x);
  5427. } else {
  5428. $x = $this->GetX();
  5429. }
  5430. // check page for no-write regions and adapt page margins if necessary
  5431. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5432. // apply margins
  5433. $oy = $y + $mc_margin['T'];
  5434. if ($this->rtl) {
  5435. $ox = ($this->w - $x - $mc_margin['R']);
  5436. } else {
  5437. $ox = ($x + $mc_margin['L']);
  5438. }
  5439. $this->x = $ox;
  5440. $this->y = $oy;
  5441. // set width
  5442. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5443. if ($this->rtl) {
  5444. $w = ($this->x - $this->lMargin - $mc_margin['L']);
  5445. } else {
  5446. $w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
  5447. }
  5448. }
  5449. // store original margin values
  5450. $lMargin = $this->lMargin;
  5451. $rMargin = $this->rMargin;
  5452. if ($this->rtl) {
  5453. $this->rMargin = ($this->w - $this->x);
  5454. $this->lMargin = ($this->x - $w);
  5455. } else {
  5456. $this->lMargin = ($this->x);
  5457. $this->rMargin = ($this->w - $this->x - $w);
  5458. }
  5459. $this->clMargin = $this->lMargin;
  5460. $this->crMargin = $this->rMargin;
  5461. if ($autopadding) {
  5462. // add top padding
  5463. $this->y += $mc_padding['T'];
  5464. }
  5465. if ($ishtml) { // ******* Write HTML text
  5466. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5467. $nl = 1;
  5468. } else { // ******* Write simple text
  5469. $prev_FontSizePt = $this->FontSizePt;
  5470. // vertical alignment
  5471. if ($maxh > 0) {
  5472. // get text height
  5473. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5474. if ($fitcell) {
  5475. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5476. $fmin = 1;
  5477. $fmax = $this->FontSizePt;
  5478. $prev_text_height = $text_height;
  5479. $maxit = 100; // max number of iterations
  5480. while ($maxit > 0) {
  5481. $fmid = (($fmax + $fmin) / 2);
  5482. $this->SetFontSize($fmid, false);
  5483. $this->resetLastH();
  5484. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5485. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  5486. break;
  5487. } elseif ($text_height < $maxh) {
  5488. $fmin = $fmid;
  5489. } else {
  5490. $fmax = $fmid;
  5491. }
  5492. --$maxit;
  5493. }
  5494. $this->SetFontSize($this->FontSizePt);
  5495. }
  5496. if ($text_height < $maxh) {
  5497. if ($valign == 'M') {
  5498. // text vertically centered
  5499. $this->y += (($maxh - $text_height) / 2);
  5500. } elseif ($valign == 'B') {
  5501. // text vertically aligned on bottom
  5502. $this->y += ($maxh - $text_height);
  5503. }
  5504. }
  5505. }
  5506. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  5507. if ($fitcell) {
  5508. // restore font size
  5509. $this->SetFontSize($prev_FontSizePt);
  5510. }
  5511. }
  5512. if ($autopadding) {
  5513. // add bottom padding
  5514. $this->y += $mc_padding['B'];
  5515. }
  5516. // Get end-of-text Y position
  5517. $currentY = $this->y;
  5518. // get latest page number
  5519. $endpage = $this->page;
  5520. if ($resth > 0) {
  5521. $skip = ($endpage - $startpage);
  5522. $tmpresth = $resth;
  5523. while ($tmpresth > 0) {
  5524. if ($skip <= 0) {
  5525. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  5526. $this->checkPageBreak($this->PageBreakTrigger + 1);
  5527. }
  5528. if ($this->num_columns > 1) {
  5529. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  5530. } else {
  5531. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  5532. }
  5533. --$skip;
  5534. }
  5535. $currentY = $this->y;
  5536. $endpage = $this->page;
  5537. }
  5538. // get latest column
  5539. $endcolumn = $this->current_column;
  5540. if ($this->num_columns == 0) {
  5541. $this->num_columns = 1;
  5542. }
  5543. // disable page regions check
  5544. $check_page_regions = $this->check_page_regions;
  5545. $this->check_page_regions = false;
  5546. // get border modes
  5547. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  5548. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  5549. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  5550. // design borders around HTML cells.
  5551. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  5552. $ccode = '';
  5553. $this->setPage($page);
  5554. if ($this->num_columns < 2) {
  5555. // single-column mode
  5556. $this->SetX($x);
  5557. $this->y = $this->tMargin;
  5558. }
  5559. // account for margin changes
  5560. if ($page > $startpage) {
  5561. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  5562. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  5563. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  5564. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  5565. }
  5566. }
  5567. if ($startpage == $endpage) {
  5568. // single page
  5569. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  5570. $this->selectColumn($column);
  5571. if ($this->rtl) {
  5572. $this->x -= $mc_margin['R'];
  5573. } else {
  5574. $this->x += $mc_margin['L'];
  5575. }
  5576. if ($startcolumn == $endcolumn) { // single column
  5577. $cborder = $border;
  5578. $h = max($h, ($currentY - $oy));
  5579. $this->y = $oy;
  5580. } elseif ($column == $startcolumn) { // first column
  5581. $cborder = $border_start;
  5582. $this->y = $oy;
  5583. $h = $this->h - $this->y - $this->bMargin;
  5584. } elseif ($column == $endcolumn) { // end column
  5585. $cborder = $border_end;
  5586. $h = $currentY - $this->y;
  5587. if ($resth > $h) {
  5588. $h = $resth;
  5589. }
  5590. } else { // middle column
  5591. $cborder = $border_middle;
  5592. $h = $this->h - $this->y - $this->bMargin;
  5593. $resth -= $h;
  5594. }
  5595. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5596. } // end for each column
  5597. } elseif ($page == $startpage) { // first page
  5598. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  5599. $this->selectColumn($column);
  5600. if ($this->rtl) {
  5601. $this->x -= $mc_margin['R'];
  5602. } else {
  5603. $this->x += $mc_margin['L'];
  5604. }
  5605. if ($column == $startcolumn) { // first column
  5606. $cborder = $border_start;
  5607. $this->y = $oy;
  5608. $h = $this->h - $this->y - $this->bMargin;
  5609. } else { // middle column
  5610. $cborder = $border_middle;
  5611. $h = $this->h - $this->y - $this->bMargin;
  5612. $resth -= $h;
  5613. }
  5614. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5615. } // end for each column
  5616. } elseif ($page == $endpage) { // last page
  5617. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  5618. $this->selectColumn($column);
  5619. if ($this->rtl) {
  5620. $this->x -= $mc_margin['R'];
  5621. } else {
  5622. $this->x += $mc_margin['L'];
  5623. }
  5624. if ($column == $endcolumn) {
  5625. // end column
  5626. $cborder = $border_end;
  5627. $h = $currentY - $this->y;
  5628. if ($resth > $h) {
  5629. $h = $resth;
  5630. }
  5631. } else {
  5632. // middle column
  5633. $cborder = $border_middle;
  5634. $h = $this->h - $this->y - $this->bMargin;
  5635. $resth -= $h;
  5636. }
  5637. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5638. } // end for each column
  5639. } else { // middle page
  5640. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  5641. $this->selectColumn($column);
  5642. if ($this->rtl) {
  5643. $this->x -= $mc_margin['R'];
  5644. } else {
  5645. $this->x += $mc_margin['L'];
  5646. }
  5647. $cborder = $border_middle;
  5648. $h = $this->h - $this->y - $this->bMargin;
  5649. $resth -= $h;
  5650. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5651. } // end for each column
  5652. }
  5653. if ($cborder OR $fill) {
  5654. $offsetlen = strlen($ccode);
  5655. // draw border and fill
  5656. if ($this->inxobj) {
  5657. // we are inside an XObject template
  5658. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  5659. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  5660. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  5661. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  5662. } else {
  5663. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  5664. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  5665. }
  5666. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  5667. $pstart = substr($pagebuff, 0, $pagemark);
  5668. $pend = substr($pagebuff, $pagemark);
  5669. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  5670. } else {
  5671. if (end($this->transfmrk[$this->page]) !== false) {
  5672. $pagemarkkey = key($this->transfmrk[$this->page]);
  5673. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  5674. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  5675. } elseif ($this->InFooter) {
  5676. $pagemark = $this->footerpos[$this->page];
  5677. $this->footerpos[$this->page] += $offsetlen;
  5678. } else {
  5679. $pagemark = $this->intmrk[$this->page];
  5680. $this->intmrk[$this->page] += $offsetlen;
  5681. }
  5682. $pagebuff = $this->getPageBuffer($this->page);
  5683. $pstart = substr($pagebuff, 0, $pagemark);
  5684. $pend = substr($pagebuff, $pagemark);
  5685. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  5686. }
  5687. }
  5688. } // end for each page
  5689. // restore page regions check
  5690. $this->check_page_regions = $check_page_regions;
  5691. // Get end-of-cell Y position
  5692. $currentY = $this->GetY();
  5693. // restore previous values
  5694. if ($this->num_columns > 1) {
  5695. $this->selectColumn();
  5696. } else {
  5697. // restore original margins
  5698. $this->lMargin = $lMargin;
  5699. $this->rMargin = $rMargin;
  5700. if ($this->page > $startpage) {
  5701. // check for margin variations between pages (i.e. booklet mode)
  5702. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  5703. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  5704. if (($dl != 0) OR ($dr != 0)) {
  5705. $this->lMargin += $dl;
  5706. $this->rMargin += $dr;
  5707. }
  5708. }
  5709. }
  5710. if ($ln > 0) {
  5711. //Go to the beginning of the next line
  5712. $this->SetY($currentY + $mc_margin['B']);
  5713. if ($ln == 2) {
  5714. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5715. }
  5716. } else {
  5717. // go left or right by case
  5718. $this->setPage($startpage);
  5719. $this->y = $y;
  5720. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5721. }
  5722. $this->setContentMark();
  5723. $this->cell_padding = $prev_cell_padding;
  5724. $this->cell_margin = $prev_cell_margin;
  5725. $this->clMargin = $this->lMargin;
  5726. $this->crMargin = $this->rMargin;
  5727. return $nl;
  5728. }
  5729. /**
  5730. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  5731. * @param $txt (string) String for calculating his height
  5732. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5733. * @param $reseth (boolean) if true reset the last cell height (default false).
  5734. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5735. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5736. * @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)))
  5737. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5738. * @author Alexander Escalona Fernández, Nicola Asuni
  5739. * @public
  5740. * @since 4.5.011
  5741. */
  5742. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5743. if ($txt === '') {
  5744. // empty string
  5745. return 1;
  5746. }
  5747. // adjust internal padding
  5748. $prev_cell_padding = $this->cell_padding;
  5749. $prev_lasth = $this->lasth;
  5750. if (is_array($cellpadding)) {
  5751. $this->cell_padding = $cellpadding;
  5752. }
  5753. $this->adjustCellPadding($border);
  5754. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5755. if ($this->rtl) {
  5756. $w = $this->x - $this->lMargin;
  5757. } else {
  5758. $w = $this->w - $this->rMargin - $this->x;
  5759. }
  5760. }
  5761. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5762. if ($reseth) {
  5763. // reset row height
  5764. $this->resetLastH();
  5765. }
  5766. $lines = 1;
  5767. $sum = 0;
  5768. $chars = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont), $txt, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5769. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  5770. $length = count($chars);
  5771. $lastSeparator = -1;
  5772. for ($i = 0; $i < $length; ++$i) {
  5773. $charWidth = $charsWidth[$i];
  5774. if (preg_match($this->re_spaces, TCPDF_FONTS::unichr($chars[$i], $this->isunicode))) {
  5775. $lastSeparator = $i;
  5776. }
  5777. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  5778. ++$lines;
  5779. if ($chars[$i] == 10) {
  5780. $lastSeparator = -1;
  5781. $sum = 0;
  5782. } elseif ($lastSeparator != -1) {
  5783. $i = $lastSeparator;
  5784. $lastSeparator = -1;
  5785. $sum = 0;
  5786. } else {
  5787. $sum = $charWidth;
  5788. }
  5789. } else {
  5790. $sum += $charWidth;
  5791. }
  5792. }
  5793. if ($chars[($length - 1)] == 10) {
  5794. --$lines;
  5795. }
  5796. $this->cell_padding = $prev_cell_padding;
  5797. $this->lasth = $prev_lasth;
  5798. return $lines;
  5799. }
  5800. /**
  5801. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  5802. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  5803. * @pre
  5804. * // store current object
  5805. * $pdf->startTransaction();
  5806. * // store starting values
  5807. * $start_y = $pdf->GetY();
  5808. * $start_page = $pdf->getPage();
  5809. * // call your printing functions with your parameters
  5810. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5811. * $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);
  5812. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5813. * // get the new Y
  5814. * $end_y = $pdf->GetY();
  5815. * $end_page = $pdf->getPage();
  5816. * // calculate height
  5817. * $height = 0;
  5818. * if ($end_page == $start_page) {
  5819. * $height = $end_y - $start_y;
  5820. * } else {
  5821. * for ($page=$start_page; $page <= $end_page; ++$page) {
  5822. * $this->setPage($page);
  5823. * if ($page == $start_page) {
  5824. * // first page
  5825. * $height = $this->h - $start_y - $this->bMargin;
  5826. * } elseif ($page == $end_page) {
  5827. * // last page
  5828. * $height = $end_y - $this->tMargin;
  5829. * } else {
  5830. * $height = $this->h - $this->tMargin - $this->bMargin;
  5831. * }
  5832. * }
  5833. * }
  5834. * // restore previous object
  5835. * $pdf = $pdf->rollbackTransaction();
  5836. *
  5837. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5838. * @param $txt (string) String for calculating his height
  5839. * @param $reseth (boolean) if true reset the last cell height (default false).
  5840. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5841. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5842. * @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)))
  5843. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5844. * @author Nicola Asuni, Alexander Escalona Fernández
  5845. * @public
  5846. */
  5847. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5848. // adjust internal padding
  5849. $prev_cell_padding = $this->cell_padding;
  5850. $prev_lasth = $this->lasth;
  5851. if (is_array($cellpadding)) {
  5852. $this->cell_padding = $cellpadding;
  5853. }
  5854. $this->adjustCellPadding($border);
  5855. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  5856. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  5857. if ($autopadding) {
  5858. // add top and bottom padding
  5859. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  5860. }
  5861. $this->cell_padding = $prev_cell_padding;
  5862. $this->lasth = $prev_lasth;
  5863. return $height;
  5864. }
  5865. /**
  5866. * This method prints text from the current position.<br />
  5867. * @param $h (float) Line height
  5868. * @param $txt (string) String to print
  5869. * @param $link (mixed) URL or identifier returned by AddLink()
  5870. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5871. * @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>
  5872. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  5873. * @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.
  5874. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  5875. * @param $firstblock (boolean) if true the string is the starting of a line.
  5876. * @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.
  5877. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  5878. * @param $margin (array) margin array of the parent container
  5879. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  5880. * @public
  5881. * @since 1.5
  5882. */
  5883. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  5884. // check page for no-write regions and adapt page margins if necessary
  5885. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5886. if (strlen($txt) == 0) {
  5887. // fix empty text
  5888. $txt = ' ';
  5889. }
  5890. if ($margin === '') {
  5891. // set default margins
  5892. $margin = $this->cell_margin;
  5893. }
  5894. // remove carriage returns
  5895. $s = str_replace("\r", '', $txt);
  5896. // check if string contains arabic text
  5897. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $s)) {
  5898. $arabic = true;
  5899. } else {
  5900. $arabic = false;
  5901. }
  5902. // check if string contains RTL text
  5903. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $s)) {
  5904. $rtlmode = true;
  5905. } else {
  5906. $rtlmode = false;
  5907. }
  5908. // get a char width
  5909. $chrwidth = $this->GetCharWidth(46); // dot character
  5910. // get array of unicode values
  5911. $chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);
  5912. // calculate maximum width for a single character on string
  5913. $chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
  5914. array_walk($chrw, array($this, 'getRawCharWidth'));
  5915. $maxchwidth = max($chrw);
  5916. // get array of chars
  5917. $uchars = TCPDF_FONTS::UTF8ArrayToUniArray($chars, $this->isunicode);
  5918. // get the number of characters
  5919. $nb = count($chars);
  5920. // replacement for SHY character (minus symbol)
  5921. $shy_replacement = 45;
  5922. $shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
  5923. // widht for SHY replacement
  5924. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  5925. // max Y
  5926. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  5927. // page width
  5928. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  5929. // calculate remaining line width ($w)
  5930. if ($this->rtl) {
  5931. $w = $this->x - $this->lMargin;
  5932. } else {
  5933. $w = $this->w - $this->rMargin - $this->x;
  5934. }
  5935. // max column width
  5936. $wmax = ($w - $wadj);
  5937. if (!$firstline) {
  5938. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  5939. }
  5940. if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
  5941. // the maximum width character do not fit on column
  5942. return '';
  5943. }
  5944. // minimum row height
  5945. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  5946. $start_page = $this->page;
  5947. $i = 0; // character position
  5948. $j = 0; // current starting position
  5949. $sep = -1; // position of the last blank space
  5950. $shy = false; // true if the last blank is a soft hypen (SHY)
  5951. $l = 0; // current string length
  5952. $nl = 0; //number of lines
  5953. $linebreak = false;
  5954. $pc = 0; // previous character
  5955. // for each character
  5956. while ($i < $nb) {
  5957. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  5958. break;
  5959. }
  5960. //Get the current character
  5961. $c = $chars[$i];
  5962. if ($c == 10) { // 10 = "\n" = new line
  5963. //Explicit line break
  5964. if ($align == 'J') {
  5965. if ($this->rtl) {
  5966. $talign = 'R';
  5967. } else {
  5968. $talign = 'L';
  5969. }
  5970. } else {
  5971. $talign = $align;
  5972. }
  5973. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  5974. if ($firstline) {
  5975. $startx = $this->x;
  5976. $tmparr = array_slice($chars, $j, ($i - $j));
  5977. if ($rtlmode) {
  5978. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5979. }
  5980. $linew = $this->GetArrStringWidth($tmparr);
  5981. unset($tmparr);
  5982. if ($this->rtl) {
  5983. $this->endlinex = $startx - $linew;
  5984. } else {
  5985. $this->endlinex = $startx + $linew;
  5986. }
  5987. $w = $linew;
  5988. $tmpcellpadding = $this->cell_padding;
  5989. if ($maxh == 0) {
  5990. $this->SetCellPadding(0);
  5991. }
  5992. }
  5993. if ($firstblock AND $this->isRTLTextDir()) {
  5994. $tmpstr = $this->stringRightTrim($tmpstr);
  5995. }
  5996. // Skip newlines at the begining of a page or column
  5997. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  5998. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  5999. }
  6000. unset($tmpstr);
  6001. if ($firstline) {
  6002. $this->cell_padding = $tmpcellpadding;
  6003. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6004. }
  6005. ++$nl;
  6006. $j = $i + 1;
  6007. $l = 0;
  6008. $sep = -1;
  6009. $shy = false;
  6010. // account for margin changes
  6011. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6012. $this->AcceptPageBreak();
  6013. if ($this->rtl) {
  6014. $this->x -= $margin['R'];
  6015. } else {
  6016. $this->x += $margin['L'];
  6017. }
  6018. $this->lMargin += $margin['L'];
  6019. $this->rMargin += $margin['R'];
  6020. }
  6021. $w = $this->getRemainingWidth();
  6022. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  6023. } else {
  6024. // 160 is the non-breaking space.
  6025. // 173 is SHY (Soft Hypen).
  6026. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6027. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6028. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6029. if (($c != 160)
  6030. AND (($c == 173)
  6031. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  6032. OR (($c == 45)
  6033. AND ($i < ($nb - 1))
  6034. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($pc, $this->isunicode))
  6035. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  6036. )
  6037. )
  6038. ) {
  6039. // update last blank space position
  6040. $sep = $i;
  6041. // check if is a SHY
  6042. if (($c == 173) OR ($c == 45)) {
  6043. $shy = true;
  6044. if ($pc == 45) {
  6045. $tmp_shy_replacement_width = 0;
  6046. $tmp_shy_replacement_char = '';
  6047. } else {
  6048. $tmp_shy_replacement_width = $shy_replacement_width;
  6049. $tmp_shy_replacement_char = $shy_replacement_char;
  6050. }
  6051. } else {
  6052. $shy = false;
  6053. }
  6054. }
  6055. // update string length
  6056. if ($this->isUnicodeFont() AND ($arabic)) {
  6057. // with bidirectional algorithm some chars may be changed affecting the line length
  6058. // *** very slow ***
  6059. $l = $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl, $this->isunicode, $this->CurrentFont));
  6060. } else {
  6061. $l += $this->GetCharWidth($c);
  6062. }
  6063. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6064. // we have reached the end of column
  6065. if ($sep == -1) {
  6066. // check if the line was already started
  6067. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  6068. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  6069. // print a void cell and go to next line
  6070. $this->Cell($w, $h, '', 0, 1);
  6071. $linebreak = true;
  6072. if ($firstline) {
  6073. return (TCPDF_FONTS::UniArrSubString($uchars, $j));
  6074. }
  6075. } else {
  6076. // truncate the word because do not fit on column
  6077. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6078. if ($firstline) {
  6079. $startx = $this->x;
  6080. $tmparr = array_slice($chars, $j, ($i - $j));
  6081. if ($rtlmode) {
  6082. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6083. }
  6084. $linew = $this->GetArrStringWidth($tmparr);
  6085. unset($tmparr);
  6086. if ($this->rtl) {
  6087. $this->endlinex = $startx - $linew;
  6088. } else {
  6089. $this->endlinex = $startx + $linew;
  6090. }
  6091. $w = $linew;
  6092. $tmpcellpadding = $this->cell_padding;
  6093. if ($maxh == 0) {
  6094. $this->SetCellPadding(0);
  6095. }
  6096. }
  6097. if ($firstblock AND $this->isRTLTextDir()) {
  6098. $tmpstr = $this->stringRightTrim($tmpstr);
  6099. }
  6100. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6101. unset($tmpstr);
  6102. if ($firstline) {
  6103. $this->cell_padding = $tmpcellpadding;
  6104. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6105. }
  6106. $j = $i;
  6107. --$i;
  6108. }
  6109. } else {
  6110. // word wrapping
  6111. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6112. $endspace = 1;
  6113. } else {
  6114. $endspace = 0;
  6115. }
  6116. // check the length of the next string
  6117. $strrest = TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace));
  6118. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $this->stringTrim($strrest));
  6119. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6120. // truncate the word because do not fit on a full page width
  6121. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6122. if ($firstline) {
  6123. $startx = $this->x;
  6124. $tmparr = array_slice($chars, $j, ($i - $j));
  6125. if ($rtlmode) {
  6126. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6127. }
  6128. $linew = $this->GetArrStringWidth($tmparr);
  6129. unset($tmparr);
  6130. if ($this->rtl) {
  6131. $this->endlinex = ($startx - $linew);
  6132. } else {
  6133. $this->endlinex = ($startx + $linew);
  6134. }
  6135. $w = $linew;
  6136. $tmpcellpadding = $this->cell_padding;
  6137. if ($maxh == 0) {
  6138. $this->SetCellPadding(0);
  6139. }
  6140. }
  6141. if ($firstblock AND $this->isRTLTextDir()) {
  6142. $tmpstr = $this->stringRightTrim($tmpstr);
  6143. }
  6144. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6145. unset($tmpstr);
  6146. if ($firstline) {
  6147. $this->cell_padding = $tmpcellpadding;
  6148. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6149. }
  6150. $j = $i;
  6151. --$i;
  6152. } else {
  6153. // word wrapping
  6154. if ($shy) {
  6155. // add hypen (minus symbol) at the end of the line
  6156. $shy_width = $tmp_shy_replacement_width;
  6157. if ($this->rtl) {
  6158. $shy_char_left = $tmp_shy_replacement_char;
  6159. $shy_char_right = '';
  6160. } else {
  6161. $shy_char_left = '';
  6162. $shy_char_right = $tmp_shy_replacement_char;
  6163. }
  6164. } else {
  6165. $shy_width = 0;
  6166. $shy_char_left = '';
  6167. $shy_char_right = '';
  6168. }
  6169. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, ($sep + $endspace));
  6170. if ($firstline) {
  6171. $startx = $this->x;
  6172. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6173. if ($rtlmode) {
  6174. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6175. }
  6176. $linew = $this->GetArrStringWidth($tmparr);
  6177. unset($tmparr);
  6178. if ($this->rtl) {
  6179. $this->endlinex = $startx - $linew - $shy_width;
  6180. } else {
  6181. $this->endlinex = $startx + $linew + $shy_width;
  6182. }
  6183. $w = $linew;
  6184. $tmpcellpadding = $this->cell_padding;
  6185. if ($maxh == 0) {
  6186. $this->SetCellPadding(0);
  6187. }
  6188. }
  6189. // print the line
  6190. if ($firstblock AND $this->isRTLTextDir()) {
  6191. $tmpstr = $this->stringRightTrim($tmpstr);
  6192. }
  6193. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6194. unset($tmpstr);
  6195. if ($firstline) {
  6196. if ($chars[$sep] == 45) {
  6197. $endspace += 1;
  6198. }
  6199. // return the remaining text
  6200. $this->cell_padding = $tmpcellpadding;
  6201. return (TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace)));
  6202. }
  6203. $i = $sep;
  6204. $sep = -1;
  6205. $shy = false;
  6206. $j = ($i + 1);
  6207. }
  6208. }
  6209. // account for margin changes
  6210. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6211. $this->AcceptPageBreak();
  6212. if ($this->rtl) {
  6213. $this->x -= $margin['R'];
  6214. } else {
  6215. $this->x += $margin['L'];
  6216. }
  6217. $this->lMargin += $margin['L'];
  6218. $this->rMargin += $margin['R'];
  6219. }
  6220. $w = $this->getRemainingWidth();
  6221. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6222. if ($linebreak) {
  6223. $linebreak = false;
  6224. } else {
  6225. ++$nl;
  6226. $l = 0;
  6227. }
  6228. }
  6229. }
  6230. // save last character
  6231. $pc = $c;
  6232. ++$i;
  6233. } // end while i < nb
  6234. // print last substring (if any)
  6235. if ($l > 0) {
  6236. switch ($align) {
  6237. case 'J':
  6238. case 'C': {
  6239. $w = $w;
  6240. break;
  6241. }
  6242. case 'L': {
  6243. if ($this->rtl) {
  6244. $w = $w;
  6245. } else {
  6246. $w = $l;
  6247. }
  6248. break;
  6249. }
  6250. case 'R': {
  6251. if ($this->rtl) {
  6252. $w = $l;
  6253. } else {
  6254. $w = $w;
  6255. }
  6256. break;
  6257. }
  6258. default: {
  6259. $w = $l;
  6260. break;
  6261. }
  6262. }
  6263. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $nb);
  6264. if ($firstline) {
  6265. $startx = $this->x;
  6266. $tmparr = array_slice($chars, $j, ($nb - $j));
  6267. if ($rtlmode) {
  6268. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6269. }
  6270. $linew = $this->GetArrStringWidth($tmparr);
  6271. unset($tmparr);
  6272. if ($this->rtl) {
  6273. $this->endlinex = $startx - $linew;
  6274. } else {
  6275. $this->endlinex = $startx + $linew;
  6276. }
  6277. $w = $linew;
  6278. $tmpcellpadding = $this->cell_padding;
  6279. if ($maxh == 0) {
  6280. $this->SetCellPadding(0);
  6281. }
  6282. }
  6283. if ($firstblock AND $this->isRTLTextDir()) {
  6284. $tmpstr = $this->stringRightTrim($tmpstr);
  6285. }
  6286. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6287. unset($tmpstr);
  6288. if ($firstline) {
  6289. $this->cell_padding = $tmpcellpadding;
  6290. return (TCPDF_FONTS::UniArrSubString($uchars, $nb));
  6291. }
  6292. ++$nl;
  6293. }
  6294. if ($firstline) {
  6295. return '';
  6296. }
  6297. return $nl;
  6298. }
  6299. /**
  6300. * Returns the remaining width between the current position and margins.
  6301. * @return int Return the remaining width
  6302. * @protected
  6303. */
  6304. protected function getRemainingWidth() {
  6305. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6306. if ($this->rtl) {
  6307. return ($this->x - $this->lMargin);
  6308. } else {
  6309. return ($this->w - $this->rMargin - $this->x);
  6310. }
  6311. }
  6312. /**
  6313. * Set the block dimensions accounting for page breaks and page/column fitting
  6314. * @param $w (float) width
  6315. * @param $h (float) height
  6316. * @param $x (float) X coordinate
  6317. * @param $y (float) Y coodiante
  6318. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  6319. * @return array($w, $h, $x, $y)
  6320. * @protected
  6321. * @since 5.5.009 (2010-07-05)
  6322. */
  6323. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6324. if ($w <= 0) {
  6325. // set maximum width
  6326. $w = ($this->w - $this->lMargin - $this->rMargin);
  6327. if ($w <= 0) {
  6328. $w = 1;
  6329. }
  6330. }
  6331. if ($h <= 0) {
  6332. // set maximum height
  6333. $h = ($this->PageBreakTrigger - $this->tMargin);
  6334. if ($h <= 0) {
  6335. $h = 1;
  6336. }
  6337. }
  6338. // resize the block to be vertically contained on a single page or single column
  6339. if ($fitonpage OR $this->AutoPageBreak) {
  6340. $ratio_wh = ($w / $h);
  6341. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  6342. $h = $this->PageBreakTrigger - $this->tMargin;
  6343. $w = ($h * $ratio_wh);
  6344. }
  6345. // resize the block to be horizontally contained on a single page or single column
  6346. if ($fitonpage) {
  6347. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  6348. if ($w > $maxw) {
  6349. $w = $maxw;
  6350. $h = ($w / $ratio_wh);
  6351. }
  6352. }
  6353. }
  6354. // Check whether we need a new page or new column first as this does not fit
  6355. $prev_x = $this->x;
  6356. $prev_y = $this->y;
  6357. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  6358. $y = $this->y;
  6359. if ($this->rtl) {
  6360. $x += ($prev_x - $this->x);
  6361. } else {
  6362. $x += ($this->x - $prev_x);
  6363. }
  6364. $this->newline = true;
  6365. }
  6366. // resize the block to be contained on the remaining available page or column space
  6367. if ($fitonpage) {
  6368. $ratio_wh = ($w / $h);
  6369. if (($y + $h) > $this->PageBreakTrigger) {
  6370. $h = $this->PageBreakTrigger - $y;
  6371. $w = ($h * $ratio_wh);
  6372. }
  6373. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  6374. $w = $this->w - $this->rMargin - $x;
  6375. $h = ($w / $ratio_wh);
  6376. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  6377. $w = $x - $this->lMargin;
  6378. $h = ($w / $ratio_wh);
  6379. }
  6380. }
  6381. return array($w, $h, $x, $y);
  6382. }
  6383. /**
  6384. * Puts an image in the page.
  6385. * The upper-left corner must be given.
  6386. * The dimensions can be specified in different ways:<ul>
  6387. * <li>explicit width and height (expressed in user unit)</li>
  6388. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  6389. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  6390. * 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;
  6391. * The format can be specified explicitly or inferred from the file extension.<br />
  6392. * It is possible to put a link on the image.<br />
  6393. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  6394. * @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').
  6395. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  6396. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  6397. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6398. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6399. * @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.
  6400. * @param $link (mixed) URL or identifier returned by AddLink().
  6401. * @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>
  6402. * @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).
  6403. * @param $dpi (int) dot-per-inch resolution used on resize
  6404. * @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>
  6405. * @param $ismask (boolean) true if this image is a mask, false otherwise
  6406. * @param $imgmask (mixed) image object returned by this function or false
  6407. * @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)))
  6408. * @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).
  6409. * @param $hidden (boolean) If true do not display the image.
  6410. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  6411. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  6412. * @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.
  6413. * @return image information
  6414. * @public
  6415. * @since 1.1
  6416. */
  6417. 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()) {
  6418. if ($this->state != 2) {
  6419. return;
  6420. }
  6421. if ($x === '') {
  6422. $x = $this->x;
  6423. }
  6424. if ($y === '') {
  6425. $y = $this->y;
  6426. }
  6427. // check page for no-write regions and adapt page margins if necessary
  6428. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6429. $exurl = ''; // external streams
  6430. $imsize = FALSE;
  6431. // check if we are passing an image as file or string
  6432. if ($file[0] === '@') {
  6433. // image from string
  6434. $imgdata = substr($file, 1);
  6435. } else { // image file
  6436. if ($file{0} === '*') {
  6437. // image as external stream
  6438. $file = substr($file, 1);
  6439. $exurl = $file;
  6440. }
  6441. // check if is local file
  6442. if (!@file_exists($file)) {
  6443. // encode spaces on filename (file is probably an URL)
  6444. $file = str_replace(' ', '%20', $file);
  6445. }
  6446. if (@file_exists($file)) {
  6447. // get image dimensions
  6448. $imsize = @getimagesize($file);
  6449. }
  6450. if ($imsize === FALSE) {
  6451. $imgdata = TCPDF_STATIC::fileGetContents($file);
  6452. }
  6453. }
  6454. if (isset($imgdata) AND ($imgdata !== FALSE)) {
  6455. // copy image to cache
  6456. $file = TCPDF_STATIC::getObjFilename('img');
  6457. $fp = fopen($file, 'w');
  6458. fwrite($fp, $imgdata);
  6459. fclose($fp);
  6460. unset($imgdata);
  6461. $imsize = @getimagesize($file);
  6462. if ($imsize === FALSE) {
  6463. unlink($file);
  6464. } else {
  6465. $this->cached_files[] = $file;
  6466. }
  6467. }
  6468. if ($imsize === FALSE) {
  6469. if (($w > 0) AND ($h > 0)) {
  6470. // get measures from specified data
  6471. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6472. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6473. $imsize = array($pw, $ph);
  6474. } else {
  6475. $this->Error('[Image] Unable to get image: '.$file);
  6476. }
  6477. }
  6478. // file hash
  6479. $filehash = md5($this->file_id.$file);
  6480. // get original image width and height in pixels
  6481. list($pixw, $pixh) = $imsize;
  6482. // calculate image width and height on document
  6483. if (($w <= 0) AND ($h <= 0)) {
  6484. // convert image size to document unit
  6485. $w = $this->pixelsToUnits($pixw);
  6486. $h = $this->pixelsToUnits($pixh);
  6487. } elseif ($w <= 0) {
  6488. $w = $h * $pixw / $pixh;
  6489. } elseif ($h <= 0) {
  6490. $h = $w * $pixh / $pixw;
  6491. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  6492. if (strlen($fitbox) !== 2) {
  6493. // set default alignment
  6494. $fitbox = '--';
  6495. }
  6496. // scale image dimensions proportionally to fit within the ($w, $h) box
  6497. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  6498. // store current height
  6499. $oldh = $h;
  6500. // calculate new height
  6501. $h = $w * $pixh / $pixw;
  6502. // height difference
  6503. $hdiff = ($oldh - $h);
  6504. // vertical alignment
  6505. switch (strtoupper($fitbox{1})) {
  6506. case 'T': {
  6507. break;
  6508. }
  6509. case 'M': {
  6510. $y += ($hdiff / 2);
  6511. break;
  6512. }
  6513. case 'B': {
  6514. $y += $hdiff;
  6515. break;
  6516. }
  6517. }
  6518. } else {
  6519. // store current width
  6520. $oldw = $w;
  6521. // calculate new width
  6522. $w = $h * $pixw / $pixh;
  6523. // width difference
  6524. $wdiff = ($oldw - $w);
  6525. // horizontal alignment
  6526. switch (strtoupper($fitbox{0})) {
  6527. case 'L': {
  6528. if ($this->rtl) {
  6529. $x -= $wdiff;
  6530. }
  6531. break;
  6532. }
  6533. case 'C': {
  6534. if ($this->rtl) {
  6535. $x -= ($wdiff / 2);
  6536. } else {
  6537. $x += ($wdiff / 2);
  6538. }
  6539. break;
  6540. }
  6541. case 'R': {
  6542. if (!$this->rtl) {
  6543. $x += $wdiff;
  6544. }
  6545. break;
  6546. }
  6547. }
  6548. }
  6549. }
  6550. // fit the image on available space
  6551. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  6552. // calculate new minimum dimensions in pixels
  6553. $neww = round($w * $this->k * $dpi / $this->dpi);
  6554. $newh = round($h * $this->k * $dpi / $this->dpi);
  6555. // check if resize is necessary (resize is used only to reduce the image)
  6556. $newsize = ($neww * $newh);
  6557. $pixsize = ($pixw * $pixh);
  6558. if (intval($resize) == 2) {
  6559. $resize = true;
  6560. } elseif ($newsize >= $pixsize) {
  6561. $resize = false;
  6562. }
  6563. // check if image has been already added on document
  6564. $newimage = true;
  6565. if (in_array($file, $this->imagekeys)) {
  6566. $newimage = false;
  6567. // get existing image data
  6568. $info = $this->getImageBuffer($file);
  6569. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  6570. // check if the newer image is larger
  6571. $oldsize = ($info['w'] * $info['h']);
  6572. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6573. $newimage = true;
  6574. }
  6575. }
  6576. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  6577. // check for cached images with alpha channel
  6578. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  6579. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  6580. if (in_array($tempfile_plain, $this->imagekeys)) {
  6581. // get existing image data
  6582. $info = $this->getImageBuffer($tempfile_plain);
  6583. // check if the newer image is larger
  6584. $oldsize = ($info['w'] * $info['h']);
  6585. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6586. $newimage = true;
  6587. } else {
  6588. $newimage = false;
  6589. // embed mask image
  6590. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6591. // embed image, masked with previously embedded mask
  6592. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6593. }
  6594. }
  6595. }
  6596. if ($newimage) {
  6597. //First use of image, get info
  6598. $type = strtolower($type);
  6599. if ($type == '') {
  6600. $type = TCPDF_IMAGES::getImageFileType($file, $imsize);
  6601. } elseif ($type == 'jpg') {
  6602. $type = 'jpeg';
  6603. }
  6604. $mqr = TCPDF_STATIC::get_mqr();
  6605. TCPDF_STATIC::set_mqr(false);
  6606. // Specific image handlers (defined on TCPDF_IMAGES CLASS)
  6607. $mtd = '_parse'.$type;
  6608. // GD image handler function
  6609. $gdfunction = 'imagecreatefrom'.$type;
  6610. $info = false;
  6611. if ((method_exists('TCPDF_IMAGES', $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  6612. // TCPDF image functions
  6613. $info = TCPDF_IMAGES::$mtd($file);
  6614. if (($info === 'pngalpha') OR (isset($info['trns']) AND !empty($info['trns']))) {
  6615. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  6616. }
  6617. }
  6618. if (!$info) {
  6619. if (function_exists($gdfunction)) {
  6620. // GD library
  6621. $img = $gdfunction($file);
  6622. if ($resize) {
  6623. $imgr = imagecreatetruecolor($neww, $newh);
  6624. if (($type == 'gif') OR ($type == 'png')) {
  6625. $imgr = TCPDF_IMAGES::setGDImageTransparency($imgr, $img);
  6626. }
  6627. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  6628. if (($type == 'gif') OR ($type == 'png')) {
  6629. $info = TCPDF_IMAGES::_toPNG($imgr);
  6630. } else {
  6631. $info = TCPDF_IMAGES::_toJPEG($imgr, $this->jpeg_quality);
  6632. }
  6633. } else {
  6634. if (($type == 'gif') OR ($type == 'png')) {
  6635. $info = TCPDF_IMAGES::_toPNG($img);
  6636. } else {
  6637. $info = TCPDF_IMAGES::_toJPEG($img, $this->jpeg_quality);
  6638. }
  6639. }
  6640. } elseif (extension_loaded('imagick')) {
  6641. // ImageMagick library
  6642. $img = new Imagick();
  6643. if ($type == 'SVG') {
  6644. // get SVG file content
  6645. $svgimg = TCPDF_STATIC::fileGetContents($file);
  6646. if ($svgimg !== FALSE) {
  6647. // get width and height
  6648. $regs = array();
  6649. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  6650. $svgtag = $regs[1];
  6651. $tmp = array();
  6652. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6653. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6654. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  6655. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  6656. } else {
  6657. $ow = $w;
  6658. }
  6659. $tmp = array();
  6660. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6661. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6662. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  6663. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  6664. } else {
  6665. $oh = $h;
  6666. }
  6667. $tmp = array();
  6668. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  6669. $vbw = ($ow * $this->imgscale * $this->k);
  6670. $vbh = ($oh * $this->imgscale * $this->k);
  6671. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  6672. $svgtag = $vbox.$svgtag;
  6673. }
  6674. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  6675. }
  6676. $img->readImageBlob($svgimg);
  6677. }
  6678. } else {
  6679. $img->readImage($file);
  6680. }
  6681. if ($resize) {
  6682. $img->resizeImage($neww, $newh, 10, 1, false);
  6683. }
  6684. $img->setCompressionQuality($this->jpeg_quality);
  6685. $img->setImageFormat('jpeg');
  6686. $tempname = TCPDF_STATIC::getObjFilename('jpg');
  6687. $img->writeImage($tempname);
  6688. $info = TCPDF_IMAGES::_parsejpeg($tempname);
  6689. unlink($tempname);
  6690. $img->destroy();
  6691. } else {
  6692. return;
  6693. }
  6694. }
  6695. if ($info === false) {
  6696. //If false, we cannot process image
  6697. return;
  6698. }
  6699. TCPDF_STATIC::set_mqr($mqr);
  6700. if ($ismask) {
  6701. // force grayscale
  6702. $info['cs'] = 'DeviceGray';
  6703. }
  6704. if ($imgmask !== false) {
  6705. $info['masked'] = $imgmask;
  6706. }
  6707. if (!empty($exurl)) {
  6708. $info['exurl'] = $exurl;
  6709. }
  6710. // array of alternative images
  6711. $info['altimgs'] = $altimgs;
  6712. // add image to document
  6713. $info['i'] = $this->setImageBuffer($file, $info);
  6714. }
  6715. // set alignment
  6716. $this->img_rb_y = $y + $h;
  6717. // set alignment
  6718. if ($this->rtl) {
  6719. if ($palign == 'L') {
  6720. $ximg = $this->lMargin;
  6721. } elseif ($palign == 'C') {
  6722. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6723. } elseif ($palign == 'R') {
  6724. $ximg = $this->w - $this->rMargin - $w;
  6725. } else {
  6726. $ximg = $x - $w;
  6727. }
  6728. $this->img_rb_x = $ximg;
  6729. } else {
  6730. if ($palign == 'L') {
  6731. $ximg = $this->lMargin;
  6732. } elseif ($palign == 'C') {
  6733. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6734. } elseif ($palign == 'R') {
  6735. $ximg = $this->w - $this->rMargin - $w;
  6736. } else {
  6737. $ximg = $x;
  6738. }
  6739. $this->img_rb_x = $ximg + $w;
  6740. }
  6741. if ($ismask OR $hidden) {
  6742. // image is not displayed
  6743. return $info['i'];
  6744. }
  6745. $xkimg = $ximg * $this->k;
  6746. if (!$alt) {
  6747. // only non-alternative immages will be set
  6748. $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']));
  6749. }
  6750. if (!empty($border)) {
  6751. $bx = $this->x;
  6752. $by = $this->y;
  6753. $this->x = $ximg;
  6754. if ($this->rtl) {
  6755. $this->x += $w;
  6756. }
  6757. $this->y = $y;
  6758. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  6759. $this->x = $bx;
  6760. $this->y = $by;
  6761. }
  6762. if ($link) {
  6763. $this->Link($ximg, $y, $w, $h, $link, 0);
  6764. }
  6765. // set pointer to align the next text/objects
  6766. switch($align) {
  6767. case 'T': {
  6768. $this->y = $y;
  6769. $this->x = $this->img_rb_x;
  6770. break;
  6771. }
  6772. case 'M': {
  6773. $this->y = $y + round($h/2);
  6774. $this->x = $this->img_rb_x;
  6775. break;
  6776. }
  6777. case 'B': {
  6778. $this->y = $this->img_rb_y;
  6779. $this->x = $this->img_rb_x;
  6780. break;
  6781. }
  6782. case 'N': {
  6783. $this->SetY($this->img_rb_y);
  6784. break;
  6785. }
  6786. default:{
  6787. break;
  6788. }
  6789. }
  6790. $this->endlinex = $this->img_rb_x;
  6791. if ($this->inxobj) {
  6792. // we are inside an XObject template
  6793. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  6794. }
  6795. return $info['i'];
  6796. }
  6797. /**
  6798. * Extract info from a PNG image with alpha channel using the Imagick or GD library.
  6799. * @param $file (string) Name of the file containing the image.
  6800. * @param $x (float) Abscissa of the upper-left corner.
  6801. * @param $y (float) Ordinate of the upper-left corner.
  6802. * @param $wpx (float) Original width of the image in pixels.
  6803. * @param $hpx (float) original height of the image in pixels.
  6804. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6805. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6806. * @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.
  6807. * @param $link (mixed) URL or identifier returned by AddLink().
  6808. * @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>
  6809. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  6810. * @param $dpi (int) dot-per-inch resolution used on resize
  6811. * @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>
  6812. * @param $filehash (string) File hash used to build unique file names.
  6813. * @author Nicola Asuni
  6814. * @protected
  6815. * @since 4.3.007 (2008-12-04)
  6816. * @see Image()
  6817. */
  6818. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  6819. if (empty($filehash)) {
  6820. $filehash = md5($this->file_id.$file);
  6821. }
  6822. // create temp image file (without alpha channel)
  6823. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  6824. // create temp alpha file
  6825. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  6826. if (extension_loaded('imagick')) { // ImageMagick extension
  6827. // ImageMagick library
  6828. $img = new Imagick();
  6829. $img->readImage($file);
  6830. // clone image object
  6831. $imga = TCPDF_STATIC::objclone($img);
  6832. // extract alpha channel
  6833. if (method_exists($img, 'setImageAlphaChannel') AND defined('Imagick::ALPHACHANNEL_EXTRACT')) {
  6834. $img->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
  6835. } else {
  6836. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  6837. $img->negateImage(true);
  6838. }
  6839. $img->setImageFormat('png');
  6840. $img->writeImage($tempfile_alpha);
  6841. // remove alpha channel
  6842. if (method_exists($imga, 'setImageMatte')) {
  6843. $imga->setImageMatte(false);
  6844. } else {
  6845. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  6846. }
  6847. $imga->setImageFormat('png');
  6848. $imga->writeImage($tempfile_plain);
  6849. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  6850. // generate images
  6851. $img = imagecreatefrompng($file);
  6852. $imgalpha = imagecreate($wpx, $hpx);
  6853. // generate gray scale palette (0 -> 255)
  6854. for ($c = 0; $c < 256; ++$c) {
  6855. ImageColorAllocate($imgalpha, $c, $c, $c);
  6856. }
  6857. // extract alpha channel
  6858. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  6859. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  6860. $color = imagecolorat($img, $xpx, $ypx);
  6861. $alpha = $this->getGDgamma($color); // correct gamma
  6862. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  6863. }
  6864. }
  6865. imagepng($imgalpha, $tempfile_alpha);
  6866. imagedestroy($imgalpha);
  6867. // extract image without alpha channel
  6868. $imgplain = imagecreatetruecolor($wpx, $hpx);
  6869. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  6870. imagepng($imgplain, $tempfile_plain);
  6871. imagedestroy($imgplain);
  6872. } else {
  6873. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  6874. }
  6875. // embed mask image
  6876. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6877. // embed image, masked with previously embedded mask
  6878. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6879. // remove temp files
  6880. unlink($tempfile_alpha);
  6881. unlink($tempfile_plain);
  6882. }
  6883. /**
  6884. * Get the GD-corrected PNG gamma value from alpha color
  6885. * @param $c (int) alpha color
  6886. * @protected
  6887. * @since 4.3.007 (2008-12-04)
  6888. */
  6889. protected function getGDgamma($c) {
  6890. if (!isset($this->gdgammacache["'".$c."'"])) {
  6891. // shifts off the first 24 bits (where 8x3 are used for each color),
  6892. // and returns the remaining 7 allocated bits (commonly used for alpha)
  6893. $alpha = ($c >> 24);
  6894. // GD alpha is only 7 bit (0 -> 127)
  6895. $alpha = (((127 - $alpha) / 127) * 255);
  6896. // correct gamma
  6897. $this->gdgammacache["'".$c."'"] = (pow(($alpha / 255), 2.2) * 255);
  6898. // store the latest values on cache to improve performances
  6899. if (count($this->gdgammacache) > 8) {
  6900. // remove one element from the cache array
  6901. array_shift($this->gdgammacache);
  6902. }
  6903. }
  6904. return $this->gdgammacache["'".$c."'"];
  6905. }
  6906. /**
  6907. * Performs a line break.
  6908. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  6909. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  6910. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  6911. * @public
  6912. * @since 1.0
  6913. * @see Cell()
  6914. */
  6915. public function Ln($h='', $cell=false) {
  6916. 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'])) {
  6917. // revove vertical space from the top of the column
  6918. return;
  6919. }
  6920. if ($cell) {
  6921. if ($this->rtl) {
  6922. $cellpadding = $this->cell_padding['R'];
  6923. } else {
  6924. $cellpadding = $this->cell_padding['L'];
  6925. }
  6926. } else {
  6927. $cellpadding = 0;
  6928. }
  6929. if ($this->rtl) {
  6930. $this->x = $this->w - $this->rMargin - $cellpadding;
  6931. } else {
  6932. $this->x = $this->lMargin + $cellpadding;
  6933. }
  6934. if (is_string($h)) {
  6935. $this->y += $this->lasth;
  6936. } else {
  6937. $this->y += $h;
  6938. }
  6939. $this->newline = true;
  6940. }
  6941. /**
  6942. * Returns the relative X value of current position.
  6943. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  6944. * @return float
  6945. * @public
  6946. * @since 1.2
  6947. * @see SetX(), GetY(), SetY()
  6948. */
  6949. public function GetX() {
  6950. //Get x position
  6951. if ($this->rtl) {
  6952. return ($this->w - $this->x);
  6953. } else {
  6954. return $this->x;
  6955. }
  6956. }
  6957. /**
  6958. * Returns the absolute X value of current position.
  6959. * @return float
  6960. * @public
  6961. * @since 1.2
  6962. * @see SetX(), GetY(), SetY()
  6963. */
  6964. public function GetAbsX() {
  6965. return $this->x;
  6966. }
  6967. /**
  6968. * Returns the ordinate of the current position.
  6969. * @return float
  6970. * @public
  6971. * @since 1.0
  6972. * @see SetY(), GetX(), SetX()
  6973. */
  6974. public function GetY() {
  6975. return $this->y;
  6976. }
  6977. /**
  6978. * Defines the abscissa of the current position.
  6979. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  6980. * @param $x (float) The value of the abscissa in user units.
  6981. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  6982. * @public
  6983. * @since 1.2
  6984. * @see GetX(), GetY(), SetY(), SetXY()
  6985. */
  6986. public function SetX($x, $rtloff=false) {
  6987. $x = floatval($x);
  6988. if (!$rtloff AND $this->rtl) {
  6989. if ($x >= 0) {
  6990. $this->x = $this->w - $x;
  6991. } else {
  6992. $this->x = abs($x);
  6993. }
  6994. } else {
  6995. if ($x >= 0) {
  6996. $this->x = $x;
  6997. } else {
  6998. $this->x = $this->w + $x;
  6999. }
  7000. }
  7001. if ($this->x < 0) {
  7002. $this->x = 0;
  7003. }
  7004. if ($this->x > $this->w) {
  7005. $this->x = $this->w;
  7006. }
  7007. }
  7008. /**
  7009. * Moves the current abscissa back to the left margin and sets the ordinate.
  7010. * If the passed value is negative, it is relative to the bottom of the page.
  7011. * @param $y (float) The value of the ordinate in user units.
  7012. * @param $resetx (bool) if true (default) reset the X position.
  7013. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7014. * @public
  7015. * @since 1.0
  7016. * @see GetX(), GetY(), SetY(), SetXY()
  7017. */
  7018. public function SetY($y, $resetx=true, $rtloff=false) {
  7019. $y = floatval($y);
  7020. if ($resetx) {
  7021. //reset x
  7022. if (!$rtloff AND $this->rtl) {
  7023. $this->x = $this->w - $this->rMargin;
  7024. } else {
  7025. $this->x = $this->lMargin;
  7026. }
  7027. }
  7028. if ($y >= 0) {
  7029. $this->y = $y;
  7030. } else {
  7031. $this->y = $this->h + $y;
  7032. }
  7033. if ($this->y < 0) {
  7034. $this->y = 0;
  7035. }
  7036. if ($this->y > $this->h) {
  7037. $this->y = $this->h;
  7038. }
  7039. }
  7040. /**
  7041. * Defines the abscissa and ordinate of the current position.
  7042. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  7043. * @param $x (float) The value of the abscissa.
  7044. * @param $y (float) The value of the ordinate.
  7045. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7046. * @public
  7047. * @since 1.2
  7048. * @see SetX(), SetY()
  7049. */
  7050. public function SetXY($x, $y, $rtloff=false) {
  7051. $this->SetY($y, false, $rtloff);
  7052. $this->SetX($x, $rtloff);
  7053. }
  7054. /**
  7055. * Set the absolute X coordinate of the current pointer.
  7056. * @param $x (float) The value of the abscissa in user units.
  7057. * @public
  7058. * @since 5.9.186 (2012-09-13)
  7059. * @see setAbsX(), setAbsY(), SetAbsXY()
  7060. */
  7061. public function SetAbsX($x) {
  7062. $this->x = floatval($x);
  7063. }
  7064. /**
  7065. * Set the absolute Y coordinate of the current pointer.
  7066. * @param $y (float) (float) The value of the ordinate in user units.
  7067. * @public
  7068. * @since 5.9.186 (2012-09-13)
  7069. * @see setAbsX(), setAbsY(), SetAbsXY()
  7070. */
  7071. public function SetAbsY($y) {
  7072. $this->y = floatval($y);
  7073. }
  7074. /**
  7075. * Set the absolute X and Y coordinates of the current pointer.
  7076. * @param $x (float) The value of the abscissa in user units.
  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 SetAbsXY($x, $y) {
  7083. $this->SetAbsX($x);
  7084. $this->SetAbsY($y);
  7085. }
  7086. /**
  7087. * Send the document to a given destination: string, local file or browser.
  7088. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  7089. * The method first calls Close() if necessary to terminate the document.
  7090. * @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.
  7091. * @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>
  7092. * @public
  7093. * @since 1.0
  7094. * @see Close()
  7095. */
  7096. public function Output($name='doc.pdf', $dest='I') {
  7097. //Output PDF to some destination
  7098. //Finish document if necessary
  7099. if ($this->state < 3) {
  7100. $this->Close();
  7101. }
  7102. //Normalize parameters
  7103. if (is_bool($dest)) {
  7104. $dest = $dest ? 'D' : 'F';
  7105. }
  7106. $dest = strtoupper($dest);
  7107. if ($dest{0} != 'F') {
  7108. $name = preg_replace('/[\s]+/', '_', $name);
  7109. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  7110. }
  7111. if ($this->sign) {
  7112. // *** apply digital signature to the document ***
  7113. // get the document content
  7114. $pdfdoc = $this->getBuffer();
  7115. // remove last newline
  7116. $pdfdoc = substr($pdfdoc, 0, -1);
  7117. // Remove the original buffer
  7118. if (isset($this->diskcache) AND $this->diskcache) {
  7119. // remove buffer file from cache
  7120. unlink($this->buffer);
  7121. }
  7122. unset($this->buffer);
  7123. // remove filler space
  7124. $byterange_string_len = strlen(TCPDF_STATIC::$byterange_string);
  7125. // define the ByteRange
  7126. $byte_range = array();
  7127. $byte_range[0] = 0;
  7128. $byte_range[1] = strpos($pdfdoc, TCPDF_STATIC::$byterange_string) + $byterange_string_len + 10;
  7129. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  7130. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  7131. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  7132. // replace the ByteRange
  7133. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  7134. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  7135. $pdfdoc = str_replace(TCPDF_STATIC::$byterange_string, $byterange, $pdfdoc);
  7136. // write the document to a temporary folder
  7137. $tempdoc = TCPDF_STATIC::getObjFilename('tmppdf');
  7138. $f = fopen($tempdoc, 'wb');
  7139. if (!$f) {
  7140. $this->Error('Unable to create temporary file: '.$tempdoc);
  7141. }
  7142. $pdfdoc_length = strlen($pdfdoc);
  7143. fwrite($f, $pdfdoc, $pdfdoc_length);
  7144. fclose($f);
  7145. // get digital signature via openssl library
  7146. $tempsign = TCPDF_STATIC::getObjFilename('tmpsig');
  7147. if (empty($this->signature_data['extracerts'])) {
  7148. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  7149. } else {
  7150. 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']);
  7151. }
  7152. unlink($tempdoc);
  7153. // read signature
  7154. $signature = file_get_contents($tempsign);
  7155. unlink($tempsign);
  7156. // extract signature
  7157. $signature = substr($signature, $pdfdoc_length);
  7158. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  7159. $tmparr = explode("\n\n", $signature);
  7160. $signature = $tmparr[1];
  7161. unset($tmparr);
  7162. // decode signature
  7163. $signature = base64_decode(trim($signature));
  7164. // convert signature to hex
  7165. $signature = current(unpack('H*', $signature));
  7166. $signature = str_pad($signature, $this->signature_max_length, '0');
  7167. // disable disk caching
  7168. $this->diskcache = false;
  7169. // Add signature to the document
  7170. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  7171. $this->bufferlen = strlen($this->buffer);
  7172. }
  7173. switch($dest) {
  7174. case 'I': {
  7175. // Send PDF to the standard output
  7176. if (ob_get_contents()) {
  7177. $this->Error('Some data has already been output, can\'t send PDF file');
  7178. }
  7179. if (php_sapi_name() != 'cli') {
  7180. // send output to a browser
  7181. header('Content-Type: application/pdf');
  7182. if (headers_sent()) {
  7183. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7184. }
  7185. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7186. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7187. header('Pragma: public');
  7188. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7189. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7190. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7191. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7192. } else {
  7193. echo $this->getBuffer();
  7194. }
  7195. break;
  7196. }
  7197. case 'D': {
  7198. // download PDF as file
  7199. if (ob_get_contents()) {
  7200. $this->Error('Some data has already been output, can\'t send PDF file');
  7201. }
  7202. header('Content-Description: File Transfer');
  7203. if (headers_sent()) {
  7204. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7205. }
  7206. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7207. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7208. header('Pragma: public');
  7209. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7210. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7211. // force download dialog
  7212. if (strpos(php_sapi_name(), 'cgi') === false) {
  7213. header('Content-Type: application/force-download');
  7214. header('Content-Type: application/octet-stream', false);
  7215. header('Content-Type: application/download', false);
  7216. header('Content-Type: application/pdf', false);
  7217. } else {
  7218. header('Content-Type: application/pdf');
  7219. }
  7220. // use the Content-Disposition header to supply a recommended filename
  7221. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7222. header('Content-Transfer-Encoding: binary');
  7223. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7224. break;
  7225. }
  7226. case 'F':
  7227. case 'FI':
  7228. case 'FD': {
  7229. // save PDF to a local file
  7230. if ($this->diskcache) {
  7231. copy($this->buffer, $name);
  7232. } else {
  7233. $f = fopen($name, 'wb');
  7234. if (!$f) {
  7235. $this->Error('Unable to create output file: '.$name);
  7236. }
  7237. fwrite($f, $this->getBuffer(), $this->bufferlen);
  7238. fclose($f);
  7239. }
  7240. if ($dest == 'FI') {
  7241. // send headers to browser
  7242. header('Content-Type: application/pdf');
  7243. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7244. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7245. header('Pragma: public');
  7246. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7247. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7248. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7249. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7250. } elseif ($dest == 'FD') {
  7251. // send headers to browser
  7252. if (ob_get_contents()) {
  7253. $this->Error('Some data has already been output, can\'t send PDF file');
  7254. }
  7255. header('Content-Description: File Transfer');
  7256. if (headers_sent()) {
  7257. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7258. }
  7259. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7260. header('Pragma: public');
  7261. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7262. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7263. // force download dialog
  7264. if (strpos(php_sapi_name(), 'cgi') === false) {
  7265. header('Content-Type: application/force-download');
  7266. header('Content-Type: application/octet-stream', false);
  7267. header('Content-Type: application/download', false);
  7268. header('Content-Type: application/pdf', false);
  7269. } else {
  7270. header('Content-Type: application/pdf');
  7271. }
  7272. // use the Content-Disposition header to supply a recommended filename
  7273. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7274. header('Content-Transfer-Encoding: binary');
  7275. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7276. }
  7277. break;
  7278. }
  7279. case 'E': {
  7280. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  7281. $retval = 'Content-Type: application/pdf;'."\r\n";
  7282. $retval .= ' name="'.$name.'"'."\r\n";
  7283. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  7284. $retval .= 'Content-Disposition: attachment;'."\r\n";
  7285. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  7286. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  7287. return $retval;
  7288. }
  7289. case 'S': {
  7290. // returns PDF as a string
  7291. return $this->getBuffer();
  7292. }
  7293. default: {
  7294. $this->Error('Incorrect output destination: '.$dest);
  7295. }
  7296. }
  7297. return '';
  7298. }
  7299. /**
  7300. * Unset all class variables except the following critical variables.
  7301. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  7302. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  7303. * @public
  7304. * @since 4.5.016 (2009-02-24)
  7305. */
  7306. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  7307. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!TCPDF_STATIC::empty_string($this->buffer))) {
  7308. // remove buffer file from cache
  7309. unlink($this->buffer);
  7310. }
  7311. if ($destroyall AND isset($this->cached_files) AND !empty($this->cached_files)) {
  7312. // remove cached files
  7313. foreach ($this->cached_files as $cachefile) {
  7314. if (is_file($cachefile)) {
  7315. unlink($cachefile);
  7316. }
  7317. }
  7318. unset($this->cached_files);
  7319. }
  7320. foreach (array_keys(get_object_vars($this)) as $val) {
  7321. if ($destroyall OR (
  7322. ($val != 'internal_encoding')
  7323. AND ($val != 'state')
  7324. AND ($val != 'bufferlen')
  7325. AND ($val != 'buffer')
  7326. AND ($val != 'diskcache')
  7327. AND ($val != 'cached_files')
  7328. AND ($val != 'sign')
  7329. AND ($val != 'signature_data')
  7330. AND ($val != 'signature_max_length')
  7331. AND ($val != 'byterange_string')
  7332. )) {
  7333. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  7334. unset($this->$val);
  7335. }
  7336. }
  7337. }
  7338. }
  7339. /**
  7340. * Check for locale-related bug
  7341. * @protected
  7342. */
  7343. protected function _dochecks() {
  7344. //Check for locale-related bug
  7345. if (1.1 == 1) {
  7346. $this->Error('Don\'t alter the locale before including class file');
  7347. }
  7348. //Check for decimal separator
  7349. if (sprintf('%.1F', 1.0) != '1.0') {
  7350. setlocale(LC_NUMERIC, 'C');
  7351. }
  7352. }
  7353. /**
  7354. * Return an array containing variations for the basic page number alias.
  7355. * @param $a (string) Base alias.
  7356. * @return array of page number aliases
  7357. * @protected
  7358. */
  7359. protected function getInternalPageNumberAliases($a= '') {
  7360. $alias = array();
  7361. // build array of Unicode + ASCII variants (the order is important)
  7362. $alias = array('u' => array(), 'a' => array());
  7363. $u = '{'.$a.'}';
  7364. $alias['u'][] = TCPDF_STATIC::_escape($u);
  7365. if ($this->isunicode) {
  7366. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($u, $this->isunicode, $this->CurrentFont));
  7367. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($u, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7368. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($a, $this->isunicode, $this->CurrentFont));
  7369. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($a, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7370. }
  7371. $alias['a'][] = TCPDF_STATIC::_escape($a);
  7372. return $alias;
  7373. }
  7374. /**
  7375. * Return an array containing all internal page aliases.
  7376. * @return array of page number aliases
  7377. * @protected
  7378. */
  7379. protected function getAllInternalPageNumberAliases() {
  7380. $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);
  7381. $pnalias = array();
  7382. foreach($basic_alias as $k => $a) {
  7383. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  7384. }
  7385. return $pnalias;
  7386. }
  7387. /**
  7388. * Replace right shift page number aliases with spaces to correct right alignment.
  7389. * This works perfectly only when using monospaced fonts.
  7390. * @param $page (string) Page content.
  7391. * @param $aliases (array) Array of page aliases.
  7392. * @param $diff (int) initial difference to add.
  7393. * @return replaced page content.
  7394. * @protected
  7395. */
  7396. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  7397. foreach ($aliases as $type => $alias) {
  7398. foreach ($alias as $a) {
  7399. // find position of compensation factor
  7400. $startnum = (strpos($a, ':') + 1);
  7401. $a = substr($a, 0, $startnum);
  7402. if (($pos = strpos($page, $a)) !== false) {
  7403. // end of alias
  7404. $endnum = strpos($page, '}', $pos);
  7405. // string to be replaced
  7406. $aa = substr($page, $pos, ($endnum - $pos + 1));
  7407. // get compensation factor
  7408. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  7409. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  7410. $ratio = floatval($ratio);
  7411. if ($type == 'u') {
  7412. $chrdiff = floor(($diff + 12) * $ratio);
  7413. $shift = str_repeat(' ', $chrdiff);
  7414. $shift = TCPDF_FONTS::UTF8ToUTF16BE($shift, false, $this->isunicode, $this->CurrentFont);
  7415. } else {
  7416. $chrdiff = floor(($diff + 11) * $ratio);
  7417. $shift = str_repeat(' ', $chrdiff);
  7418. }
  7419. $page = str_replace($aa, $shift, $page);
  7420. }
  7421. }
  7422. }
  7423. return $page;
  7424. }
  7425. /**
  7426. * Set page boxes to be included on page descriptions.
  7427. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  7428. * @protected
  7429. */
  7430. protected function setPageBoxTypes($boxes) {
  7431. $this->page_boxes = array();
  7432. foreach ($boxes as $box) {
  7433. if (in_array($box, TCPDF_STATIC::$pageboxes)) {
  7434. $this->page_boxes[] = $box;
  7435. }
  7436. }
  7437. }
  7438. /**
  7439. * Output pages (and replace page number aliases).
  7440. * @protected
  7441. */
  7442. protected function _putpages() {
  7443. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  7444. // get internal aliases for page numbers
  7445. $pnalias = $this->getAllInternalPageNumberAliases();
  7446. $num_pages = $this->numpages;
  7447. $ptpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $num_pages - 1));
  7448. $ptpu = TCPDF_FONTS::UTF8ToUTF16BE($ptpa, false, $this->isunicode, $this->CurrentFont);
  7449. $ptp_num_chars = $this->GetNumChars($ptpa);
  7450. $pagegroupnum = 0;
  7451. $groupnum = 0;
  7452. $ptgu = 1;
  7453. $ptga = 1;
  7454. for ($n = 1; $n <= $num_pages; ++$n) {
  7455. // get current page
  7456. $temppage = $this->getPageBuffer($n);
  7457. $pagelen = strlen($temppage);
  7458. // set replacements for total pages number
  7459. $pnpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $n - 1));
  7460. $pnpu = TCPDF_FONTS::UTF8ToUTF16BE($pnpa, false, $this->isunicode, $this->CurrentFont);
  7461. $pnp_num_chars = $this->GetNumChars($pnpa);
  7462. $pdiff = 0; // difference used for right shift alignment of page numbers
  7463. $gdiff = 0; // difference used for right shift alignment of page group numbers
  7464. if (!empty($this->pagegroups)) {
  7465. if (isset($this->newpagegroup[$n])) {
  7466. $pagegroupnum = 0;
  7467. ++$groupnum;
  7468. $ptga = TCPDF_STATIC::formatPageNumber($this->pagegroups[$groupnum]);
  7469. $ptgu = TCPDF_FONTS::UTF8ToUTF16BE($ptga, false, $this->isunicode, $this->CurrentFont);
  7470. $ptg_num_chars = $this->GetNumChars($ptga);
  7471. }
  7472. ++$pagegroupnum;
  7473. $pnga = TCPDF_STATIC::formatPageNumber($pagegroupnum);
  7474. $pngu = TCPDF_FONTS::UTF8ToUTF16BE($pnga, false, $this->isunicode, $this->CurrentFont);
  7475. $png_num_chars = $this->GetNumChars($pnga);
  7476. // replace page numbers
  7477. $replace = array();
  7478. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  7479. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  7480. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  7481. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  7482. list($temppage, $gdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $gdiff);
  7483. }
  7484. // replace page numbers
  7485. $replace = array();
  7486. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  7487. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  7488. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  7489. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  7490. list($temppage, $pdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $pdiff);
  7491. // replace right shift alias
  7492. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  7493. // replace EPS marker
  7494. $temppage = str_replace($this->epsmarker, '', $temppage);
  7495. //Page
  7496. $this->page_obj_id[$n] = $this->_newobj();
  7497. $out = '<<';
  7498. $out .= ' /Type /Page';
  7499. $out .= ' /Parent 1 0 R';
  7500. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  7501. $out .= ' /Resources 2 0 R';
  7502. foreach ($this->page_boxes as $box) {
  7503. $out .= ' /'.$box;
  7504. $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']);
  7505. }
  7506. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  7507. $out .= ' /BoxColorInfo <<';
  7508. foreach ($this->page_boxes as $box) {
  7509. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  7510. $out .= ' /'.$box.' <<';
  7511. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  7512. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  7513. $out .= ' /C [';
  7514. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  7515. $out .= ' ]';
  7516. }
  7517. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  7518. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  7519. }
  7520. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  7521. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  7522. }
  7523. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  7524. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  7525. $out .= ' /D [';
  7526. foreach ($dashes as $dash) {
  7527. $out .= sprintf(' %F', ($dash * $this->k));
  7528. }
  7529. $out .= ' ]';
  7530. }
  7531. $out .= ' >>';
  7532. }
  7533. }
  7534. $out .= ' >>';
  7535. }
  7536. $out .= ' /Contents '.($this->n + 1).' 0 R';
  7537. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  7538. if (!$this->pdfa_mode) {
  7539. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  7540. }
  7541. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  7542. // page transitions
  7543. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  7544. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  7545. }
  7546. $out .= ' /Trans <<';
  7547. $out .= ' /Type /Trans';
  7548. if (isset($this->pagedim[$n]['trans']['S'])) {
  7549. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  7550. }
  7551. if (isset($this->pagedim[$n]['trans']['D'])) {
  7552. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  7553. }
  7554. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  7555. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  7556. }
  7557. if (isset($this->pagedim[$n]['trans']['M'])) {
  7558. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  7559. }
  7560. if (isset($this->pagedim[$n]['trans']['Di'])) {
  7561. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  7562. }
  7563. if (isset($this->pagedim[$n]['trans']['SS'])) {
  7564. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  7565. }
  7566. if (isset($this->pagedim[$n]['trans']['B'])) {
  7567. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  7568. }
  7569. $out .= ' >>';
  7570. }
  7571. $out .= $this->_getannotsrefs($n);
  7572. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  7573. $out .= ' >>';
  7574. $out .= "\n".'endobj';
  7575. $this->_out($out);
  7576. //Page content
  7577. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  7578. $this->_newobj();
  7579. $p = $this->_getrawstream($p);
  7580. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  7581. if ($this->diskcache) {
  7582. // remove temporary files
  7583. unlink($this->pages[$n]);
  7584. }
  7585. }
  7586. //Pages root
  7587. $out = $this->_getobj(1)."\n";
  7588. $out .= '<< /Type /Pages /Kids [';
  7589. foreach($this->page_obj_id as $page_obj) {
  7590. $out .= ' '.$page_obj.' 0 R';
  7591. }
  7592. $out .= ' ] /Count '.$num_pages.' >>';
  7593. $out .= "\n".'endobj';
  7594. $this->_out($out);
  7595. }
  7596. /**
  7597. * Output references to page annotations
  7598. * @param $n (int) page number
  7599. * @protected
  7600. * @author Nicola Asuni
  7601. * @since 4.7.000 (2008-08-29)
  7602. * @deprecated
  7603. */
  7604. protected function _putannotsrefs($n) {
  7605. $this->_out($this->_getannotsrefs($n));
  7606. }
  7607. /**
  7608. * Get references to page annotations.
  7609. * @param $n (int) page number
  7610. * @return string
  7611. * @protected
  7612. * @author Nicola Asuni
  7613. * @since 5.0.010 (2010-05-17)
  7614. */
  7615. protected function _getannotsrefs($n) {
  7616. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  7617. return '';
  7618. }
  7619. $out = ' /Annots [';
  7620. if (isset($this->PageAnnots[$n])) {
  7621. foreach ($this->PageAnnots[$n] as $key => $val) {
  7622. if (!in_array($val['n'], $this->radio_groups)) {
  7623. $out .= ' '.$val['n'].' 0 R';
  7624. }
  7625. }
  7626. // add radiobutton groups
  7627. if (isset($this->radiobutton_groups[$n])) {
  7628. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  7629. if (isset($data['n'])) {
  7630. $out .= ' '.$data['n'].' 0 R';
  7631. }
  7632. }
  7633. }
  7634. }
  7635. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  7636. // set reference for signature object
  7637. $out .= ' '.$this->sig_obj_id.' 0 R';
  7638. }
  7639. if (!empty($this->empty_signature_appearance)) {
  7640. foreach ($this->empty_signature_appearance as $esa) {
  7641. if ($esa['page'] == $n) {
  7642. // set reference for empty signature objects
  7643. $out .= ' '.$esa['objid'].' 0 R';
  7644. }
  7645. }
  7646. }
  7647. $out .= ' ]';
  7648. return $out;
  7649. }
  7650. /**
  7651. * Output annotations objects for all pages.
  7652. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  7653. * See section 12.5 of PDF 32000_2008 reference.
  7654. * @protected
  7655. * @author Nicola Asuni
  7656. * @since 4.0.018 (2008-08-06)
  7657. */
  7658. protected function _putannotsobjs() {
  7659. // reset object counter
  7660. for ($n=1; $n <= $this->numpages; ++$n) {
  7661. if (isset($this->PageAnnots[$n])) {
  7662. // set page annotations
  7663. foreach ($this->PageAnnots[$n] as $key => $pl) {
  7664. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  7665. // create annotation object for grouping radiobuttons
  7666. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  7667. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  7668. $annots = '<<';
  7669. $annots .= ' /Type /Annot';
  7670. $annots .= ' /Subtype /Widget';
  7671. $annots .= ' /Rect [0 0 0 0]';
  7672. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  7673. // read only
  7674. $annots .= ' /F 68';
  7675. $annots .= ' /Ff 49153';
  7676. } else {
  7677. $annots .= ' /F 4'; // default print for PDF/A
  7678. $annots .= ' /Ff 49152';
  7679. }
  7680. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  7681. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  7682. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $radio_button_obj_id);
  7683. }
  7684. $annots .= ' /FT /Btn';
  7685. $annots .= ' /Kids [';
  7686. $defval = '';
  7687. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  7688. if (isset($data['kid'])) {
  7689. $annots .= ' '.$data['kid'].' 0 R';
  7690. if ($data['def'] !== 'Off') {
  7691. $defval = $data['def'];
  7692. }
  7693. }
  7694. }
  7695. $annots .= ' ]';
  7696. if (!empty($defval)) {
  7697. $annots .= ' /V /'.$defval;
  7698. }
  7699. $annots .= ' >>';
  7700. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  7701. $this->form_obj_id[] = $radio_button_obj_id;
  7702. // store object id to be used on Parent entry of Kids
  7703. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  7704. }
  7705. $formfield = false;
  7706. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  7707. $a = $pl['x'] * $this->k;
  7708. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  7709. $c = $pl['w'] * $this->k;
  7710. $d = $pl['h'] * $this->k;
  7711. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  7712. // create new annotation object
  7713. $annots = '<</Type /Annot';
  7714. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  7715. $annots .= ' /Rect ['.$rect.']';
  7716. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  7717. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  7718. $annots .= ' /FT /'.$pl['opt']['ft'];
  7719. $formfield = true;
  7720. }
  7721. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  7722. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  7723. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  7724. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  7725. if (isset($pl['opt']['f'])) {
  7726. $fval = 0;
  7727. if (is_array($pl['opt']['f'])) {
  7728. foreach ($pl['opt']['f'] as $f) {
  7729. switch (strtolower($f)) {
  7730. case 'invisible': {
  7731. $fval += 1 << 0;
  7732. break;
  7733. }
  7734. case 'hidden': {
  7735. $fval += 1 << 1;
  7736. break;
  7737. }
  7738. case 'print': {
  7739. $fval += 1 << 2;
  7740. break;
  7741. }
  7742. case 'nozoom': {
  7743. $fval += 1 << 3;
  7744. break;
  7745. }
  7746. case 'norotate': {
  7747. $fval += 1 << 4;
  7748. break;
  7749. }
  7750. case 'noview': {
  7751. $fval += 1 << 5;
  7752. break;
  7753. }
  7754. case 'readonly': {
  7755. $fval += 1 << 6;
  7756. break;
  7757. }
  7758. case 'locked': {
  7759. $fval += 1 << 8;
  7760. break;
  7761. }
  7762. case 'togglenoview': {
  7763. $fval += 1 << 9;
  7764. break;
  7765. }
  7766. case 'lockedcontents': {
  7767. $fval += 1 << 10;
  7768. break;
  7769. }
  7770. default: {
  7771. break;
  7772. }
  7773. }
  7774. }
  7775. } else {
  7776. $fval = intval($pl['opt']['f']);
  7777. }
  7778. } else {
  7779. $fval = 4;
  7780. }
  7781. if ($this->pdfa_mode) {
  7782. // force print flag for PDF/A mode
  7783. $fval |= 4;
  7784. }
  7785. $annots .= ' /F '.intval($fval);
  7786. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  7787. $annots .= ' /AS /'.$pl['opt']['as'];
  7788. }
  7789. if (isset($pl['opt']['ap'])) {
  7790. // appearance stream
  7791. $annots .= ' /AP <<';
  7792. if (is_array($pl['opt']['ap'])) {
  7793. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  7794. // $apmode can be: n = normal; r = rollover; d = down;
  7795. $annots .= ' /'.strtoupper($apmode);
  7796. if (is_array($apdef)) {
  7797. $annots .= ' <<';
  7798. foreach ($apdef as $apstate => $stream) {
  7799. // reference to XObject that define the appearance for this mode-state
  7800. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  7801. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  7802. }
  7803. $annots .= ' >>';
  7804. } else {
  7805. // reference to XObject that define the appearance for this mode
  7806. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  7807. $annots .= ' '.$apsobjid.' 0 R';
  7808. }
  7809. }
  7810. } else {
  7811. $annots .= $pl['opt']['ap'];
  7812. }
  7813. $annots .= ' >>';
  7814. }
  7815. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  7816. $annots .= ' /BS <<';
  7817. $annots .= ' /Type /Border';
  7818. if (isset($pl['opt']['bs']['w'])) {
  7819. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  7820. }
  7821. $bstyles = array('S', 'D', 'B', 'I', 'U');
  7822. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  7823. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7824. }
  7825. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  7826. $annots .= ' /D [';
  7827. foreach ($pl['opt']['bs']['d'] as $cord) {
  7828. $annots .= ' '.intval($cord);
  7829. }
  7830. $annots .= ']';
  7831. }
  7832. $annots .= ' >>';
  7833. } else {
  7834. $annots .= ' /Border [';
  7835. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  7836. $annots .= intval($pl['opt']['border'][0]).' ';
  7837. $annots .= intval($pl['opt']['border'][1]).' ';
  7838. $annots .= intval($pl['opt']['border'][2]);
  7839. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  7840. $annots .= ' [';
  7841. foreach ($pl['opt']['border'][3] as $dash) {
  7842. $annots .= intval($dash).' ';
  7843. }
  7844. $annots .= ']';
  7845. }
  7846. } else {
  7847. $annots .= '0 0 0';
  7848. }
  7849. $annots .= ']';
  7850. }
  7851. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  7852. $annots .= ' /BE <<';
  7853. $bstyles = array('S', 'C');
  7854. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  7855. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7856. } else {
  7857. $annots .= ' /S /S';
  7858. }
  7859. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  7860. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  7861. }
  7862. $annots .= '>>';
  7863. }
  7864. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  7865. $annots .= ' /C '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['c']);
  7866. }
  7867. //$annots .= ' /StructParent ';
  7868. //$annots .= ' /OC ';
  7869. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  7870. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  7871. // this is a markup type
  7872. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  7873. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  7874. }
  7875. //$annots .= ' /Popup ';
  7876. if (isset($pl['opt']['ca'])) {
  7877. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  7878. }
  7879. if (isset($pl['opt']['rc'])) {
  7880. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  7881. }
  7882. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  7883. //$annots .= ' /IRT ';
  7884. if (isset($pl['opt']['subj'])) {
  7885. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  7886. }
  7887. //$annots .= ' /RT ';
  7888. //$annots .= ' /IT ';
  7889. //$annots .= ' /ExData ';
  7890. }
  7891. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  7892. // Annotation types
  7893. switch (strtolower($pl['opt']['subtype'])) {
  7894. case 'text': {
  7895. if (isset($pl['opt']['open'])) {
  7896. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  7897. }
  7898. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  7899. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  7900. $annots .= ' /Name /'.$pl['opt']['name'];
  7901. } else {
  7902. $annots .= ' /Name /Note';
  7903. }
  7904. $statemodels = array('Marked', 'Review');
  7905. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  7906. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7907. } else {
  7908. $pl['opt']['statemodel'] = 'Marked';
  7909. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7910. }
  7911. if ($pl['opt']['statemodel'] == 'Marked') {
  7912. $states = array('Accepted', 'Unmarked');
  7913. } else {
  7914. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  7915. }
  7916. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  7917. $annots .= ' /State /'.$pl['opt']['state'];
  7918. } else {
  7919. if ($pl['opt']['statemodel'] == 'Marked') {
  7920. $annots .= ' /State /Unmarked';
  7921. } else {
  7922. $annots .= ' /State /None';
  7923. }
  7924. }
  7925. break;
  7926. }
  7927. case 'link': {
  7928. if (is_string($pl['txt'])) {
  7929. if ($pl['txt'][0] == '#') {
  7930. // internal destination
  7931. $annots .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($pl['txt'], 1));
  7932. } elseif ($pl['txt'][0] == '%') {
  7933. // embedded PDF file
  7934. $filename = basename(substr($pl['txt'], 1));
  7935. $annots .= ' /A << /S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($n - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  7936. } elseif ($pl['txt'][0] == '*') {
  7937. // embedded generic file
  7938. $filename = basename(substr($pl['txt'], 1));
  7939. $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});';
  7940. $annots .= ' /A << /S /JavaScript /JS '.$this->_textstring($jsa, $annot_obj_id).'>>';
  7941. } else {
  7942. // external URI link
  7943. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  7944. }
  7945. } elseif (isset($this->links[$pl['txt']])) {
  7946. // internal link ID
  7947. $l = $this->links[$pl['txt']];
  7948. if (isset($this->page_obj_id[($l[0])])) {
  7949. $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)));
  7950. }
  7951. }
  7952. $hmodes = array('N', 'I', 'O', 'P');
  7953. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  7954. $annots .= ' /H /'.$pl['opt']['h'];
  7955. } else {
  7956. $annots .= ' /H /I';
  7957. }
  7958. //$annots .= ' /PA ';
  7959. //$annots .= ' /Quadpoints ';
  7960. break;
  7961. }
  7962. case 'freetext': {
  7963. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  7964. $annots .= ' /DA ('.$pl['opt']['da'].')';
  7965. }
  7966. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  7967. $annots .= ' /Q '.intval($pl['opt']['q']);
  7968. }
  7969. if (isset($pl['opt']['rc'])) {
  7970. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  7971. }
  7972. if (isset($pl['opt']['ds'])) {
  7973. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  7974. }
  7975. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  7976. $annots .= ' /CL [';
  7977. foreach ($pl['opt']['cl'] as $cl) {
  7978. $annots .= sprintf('%F ', $cl * $this->k);
  7979. }
  7980. $annots .= ']';
  7981. }
  7982. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  7983. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  7984. $annots .= ' /IT /'.$pl['opt']['it'];
  7985. }
  7986. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  7987. $l = $pl['opt']['rd'][0] * $this->k;
  7988. $r = $pl['opt']['rd'][1] * $this->k;
  7989. $t = $pl['opt']['rd'][2] * $this->k;
  7990. $b = $pl['opt']['rd'][3] * $this->k;
  7991. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  7992. }
  7993. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  7994. $annots .= ' /LE /'.$pl['opt']['le'];
  7995. }
  7996. break;
  7997. }
  7998. case 'line': {
  7999. break;
  8000. }
  8001. case 'square': {
  8002. break;
  8003. }
  8004. case 'circle': {
  8005. break;
  8006. }
  8007. case 'polygon': {
  8008. break;
  8009. }
  8010. case 'polyline': {
  8011. break;
  8012. }
  8013. case 'highlight': {
  8014. break;
  8015. }
  8016. case 'underline': {
  8017. break;
  8018. }
  8019. case 'squiggly': {
  8020. break;
  8021. }
  8022. case 'strikeout': {
  8023. break;
  8024. }
  8025. case 'stamp': {
  8026. break;
  8027. }
  8028. case 'caret': {
  8029. break;
  8030. }
  8031. case 'ink': {
  8032. break;
  8033. }
  8034. case 'popup': {
  8035. break;
  8036. }
  8037. case 'fileattachment': {
  8038. if ($this->pdfa_mode) {
  8039. // embedded files are not allowed in PDF/A mode
  8040. break;
  8041. }
  8042. if (!isset($pl['opt']['fs'])) {
  8043. break;
  8044. }
  8045. $filename = basename($pl['opt']['fs']);
  8046. if (isset($this->embeddedfiles[$filename]['f'])) {
  8047. $annots .= ' /FS '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8048. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  8049. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8050. $annots .= ' /Name /'.$pl['opt']['name'];
  8051. } else {
  8052. $annots .= ' /Name /PushPin';
  8053. }
  8054. // index (zero-based) of the annotation in the Annots array of this page
  8055. $this->embeddedfiles[$filename]['a'] = $key;
  8056. }
  8057. break;
  8058. }
  8059. case 'sound': {
  8060. if (!isset($pl['opt']['fs'])) {
  8061. break;
  8062. }
  8063. $filename = basename($pl['opt']['fs']);
  8064. if (isset($this->embeddedfiles[$filename]['f'])) {
  8065. // ... TO BE COMPLETED ...
  8066. // /R /C /B /E /CO /CP
  8067. $annots .= ' /Sound '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8068. $iconsapp = array('Speaker', 'Mic');
  8069. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8070. $annots .= ' /Name /'.$pl['opt']['name'];
  8071. } else {
  8072. $annots .= ' /Name /Speaker';
  8073. }
  8074. }
  8075. break;
  8076. }
  8077. case 'movie': {
  8078. break;
  8079. }
  8080. case 'widget': {
  8081. $hmode = array('N', 'I', 'O', 'P', 'T');
  8082. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  8083. $annots .= ' /H /'.$pl['opt']['h'];
  8084. }
  8085. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  8086. $annots .= ' /MK <<';
  8087. if (isset($pl['opt']['mk']['r'])) {
  8088. $annots .= ' /R '.$pl['opt']['mk']['r'];
  8089. }
  8090. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  8091. $annots .= ' /BC '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bc']);
  8092. }
  8093. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  8094. $annots .= ' /BG '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bg']);
  8095. }
  8096. if (isset($pl['opt']['mk']['ca'])) {
  8097. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  8098. }
  8099. if (isset($pl['opt']['mk']['rc'])) {
  8100. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  8101. }
  8102. if (isset($pl['opt']['mk']['ac'])) {
  8103. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  8104. }
  8105. if (isset($pl['opt']['mk']['i'])) {
  8106. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  8107. if ($info !== false) {
  8108. $annots .= ' /I '.$info['n'].' 0 R';
  8109. }
  8110. }
  8111. if (isset($pl['opt']['mk']['ri'])) {
  8112. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  8113. if ($info !== false) {
  8114. $annots .= ' /RI '.$info['n'].' 0 R';
  8115. }
  8116. }
  8117. if (isset($pl['opt']['mk']['ix'])) {
  8118. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  8119. if ($info !== false) {
  8120. $annots .= ' /IX '.$info['n'].' 0 R';
  8121. }
  8122. }
  8123. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  8124. $annots .= ' /IF <<';
  8125. $if_sw = array('A', 'B', 'S', 'N');
  8126. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  8127. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  8128. }
  8129. $if_s = array('A', 'P');
  8130. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  8131. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  8132. }
  8133. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  8134. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  8135. }
  8136. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  8137. $annots .= ' /FB true';
  8138. }
  8139. $annots .= '>>';
  8140. }
  8141. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  8142. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  8143. }
  8144. $annots .= '>>';
  8145. } // end MK
  8146. // --- Entries for field dictionaries ---
  8147. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8148. // set parent
  8149. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  8150. }
  8151. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8152. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  8153. }
  8154. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  8155. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  8156. }
  8157. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  8158. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  8159. }
  8160. if (isset($pl['opt']['ff'])) {
  8161. if (is_array($pl['opt']['ff'])) {
  8162. // array of bit settings
  8163. $flag = 0;
  8164. foreach($pl['opt']['ff'] as $val) {
  8165. $flag += 1 << ($val - 1);
  8166. }
  8167. } else {
  8168. $flag = intval($pl['opt']['ff']);
  8169. }
  8170. $annots .= ' /Ff '.$flag;
  8171. }
  8172. if (isset($pl['opt']['maxlen'])) {
  8173. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  8174. }
  8175. if (isset($pl['opt']['v'])) {
  8176. $annots .= ' /V';
  8177. if (is_array($pl['opt']['v'])) {
  8178. foreach ($pl['opt']['v'] AS $optval) {
  8179. if (is_float($optval)) {
  8180. $optval = sprintf('%F', $optval);
  8181. }
  8182. $annots .= ' '.$optval;
  8183. }
  8184. } else {
  8185. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  8186. }
  8187. }
  8188. if (isset($pl['opt']['dv'])) {
  8189. $annots .= ' /DV';
  8190. if (is_array($pl['opt']['dv'])) {
  8191. foreach ($pl['opt']['dv'] AS $optval) {
  8192. if (is_float($optval)) {
  8193. $optval = sprintf('%F', $optval);
  8194. }
  8195. $annots .= ' '.$optval;
  8196. }
  8197. } else {
  8198. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  8199. }
  8200. }
  8201. if (isset($pl['opt']['rv'])) {
  8202. $annots .= ' /RV';
  8203. if (is_array($pl['opt']['rv'])) {
  8204. foreach ($pl['opt']['rv'] AS $optval) {
  8205. if (is_float($optval)) {
  8206. $optval = sprintf('%F', $optval);
  8207. }
  8208. $annots .= ' '.$optval;
  8209. }
  8210. } else {
  8211. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  8212. }
  8213. }
  8214. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  8215. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  8216. }
  8217. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  8218. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  8219. }
  8220. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8221. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8222. }
  8223. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8224. $annots .= ' /Q '.intval($pl['opt']['q']);
  8225. }
  8226. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  8227. $annots .= ' /Opt [';
  8228. foreach($pl['opt']['opt'] AS $copt) {
  8229. if (is_array($copt)) {
  8230. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  8231. } else {
  8232. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  8233. }
  8234. }
  8235. $annots .= ']';
  8236. }
  8237. if (isset($pl['opt']['ti'])) {
  8238. $annots .= ' /TI '.intval($pl['opt']['ti']);
  8239. }
  8240. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  8241. $annots .= ' /I [';
  8242. foreach($pl['opt']['i'] AS $copt) {
  8243. $annots .= intval($copt).' ';
  8244. }
  8245. $annots .= ']';
  8246. }
  8247. break;
  8248. }
  8249. case 'screen': {
  8250. break;
  8251. }
  8252. case 'printermark': {
  8253. break;
  8254. }
  8255. case 'trapnet': {
  8256. break;
  8257. }
  8258. case 'watermark': {
  8259. break;
  8260. }
  8261. case '3d': {
  8262. break;
  8263. }
  8264. default: {
  8265. break;
  8266. }
  8267. }
  8268. $annots .= '>>';
  8269. // create new annotation object
  8270. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  8271. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8272. // store reference of form object
  8273. $this->form_obj_id[] = $annot_obj_id;
  8274. }
  8275. }
  8276. }
  8277. } // end for each page
  8278. }
  8279. /**
  8280. * Put appearance streams XObject used to define annotation's appearance states.
  8281. * @param $w (int) annotation width
  8282. * @param $h (int) annotation height
  8283. * @param $stream (string) appearance stream
  8284. * @return int object ID
  8285. * @protected
  8286. * @since 4.8.001 (2009-09-09)
  8287. */
  8288. protected function _putAPXObject($w=0, $h=0, $stream='') {
  8289. $stream = trim($stream);
  8290. $out = $this->_getobj()."\n";
  8291. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  8292. $out .= '<<';
  8293. $out .= ' /Type /XObject';
  8294. $out .= ' /Subtype /Form';
  8295. $out .= ' /FormType 1';
  8296. if ($this->compress) {
  8297. $stream = gzcompress($stream);
  8298. $out .= ' /Filter /FlateDecode';
  8299. }
  8300. $rect = sprintf('%F %F', $w, $h);
  8301. $out .= ' /BBox [0 0 '.$rect.']';
  8302. $out .= ' /Matrix [1 0 0 1 0 0]';
  8303. $out .= ' /Resources 2 0 R';
  8304. $stream = $this->_getrawstream($stream);
  8305. $out .= ' /Length '.strlen($stream);
  8306. $out .= ' >>';
  8307. $out .= ' stream'."\n".$stream."\n".'endstream';
  8308. $out .= "\n".'endobj';
  8309. $this->_out($out);
  8310. return $this->n;
  8311. }
  8312. /**
  8313. * Output fonts.
  8314. * @author Nicola Asuni
  8315. * @protected
  8316. */
  8317. protected function _putfonts() {
  8318. $nf = $this->n;
  8319. foreach ($this->diffs as $diff) {
  8320. //Encodings
  8321. $this->_newobj();
  8322. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  8323. }
  8324. $mqr = TCPDF_STATIC::get_mqr();
  8325. TCPDF_STATIC::set_mqr(false);
  8326. foreach ($this->FontFiles as $file => $info) {
  8327. // search and get font file to embedd
  8328. $fontfile = TCPDF_FONTS::getFontFullPath($file, $info['fontdir']);
  8329. if (!TCPDF_STATIC::empty_string($fontfile)) {
  8330. $font = file_get_contents($fontfile);
  8331. $compressed = (substr($file, -2) == '.z');
  8332. if ((!$compressed) AND (isset($info['length2']))) {
  8333. $header = (ord($font{0}) == 128);
  8334. if ($header) {
  8335. // strip first binary header
  8336. $font = substr($font, 6);
  8337. }
  8338. if ($header AND (ord($font[$info['length1']]) == 128)) {
  8339. // strip second binary header
  8340. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  8341. }
  8342. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  8343. if ($compressed) {
  8344. // uncompress font
  8345. $font = gzuncompress($font);
  8346. }
  8347. // merge subset characters
  8348. $subsetchars = array(); // used chars
  8349. foreach ($info['fontkeys'] as $fontkey) {
  8350. $fontinfo = $this->getFontBuffer($fontkey);
  8351. $subsetchars += $fontinfo['subsetchars'];
  8352. }
  8353. // rebuild a font subset
  8354. $font = TCPDF_FONTS::_getTrueTypeFontSubset($font, $subsetchars);
  8355. // calculate new font length
  8356. $info['length1'] = strlen($font);
  8357. if ($compressed) {
  8358. // recompress font
  8359. $font = gzcompress($font);
  8360. }
  8361. }
  8362. $this->_newobj();
  8363. $this->FontFiles[$file]['n'] = $this->n;
  8364. $stream = $this->_getrawstream($font);
  8365. $out = '<< /Length '.strlen($stream);
  8366. if ($compressed) {
  8367. $out .= ' /Filter /FlateDecode';
  8368. }
  8369. $out .= ' /Length1 '.$info['length1'];
  8370. if (isset($info['length2'])) {
  8371. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  8372. }
  8373. $out .= ' >>';
  8374. $out .= ' stream'."\n".$stream."\n".'endstream';
  8375. $out .= "\n".'endobj';
  8376. $this->_out($out);
  8377. }
  8378. }
  8379. TCPDF_STATIC::set_mqr($mqr);
  8380. foreach ($this->fontkeys as $k) {
  8381. //Font objects
  8382. $font = $this->getFontBuffer($k);
  8383. $type = $font['type'];
  8384. $name = $font['name'];
  8385. if ($type == 'core') {
  8386. // standard core font
  8387. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8388. $out .= '<</Type /Font';
  8389. $out .= ' /Subtype /Type1';
  8390. $out .= ' /BaseFont /'.$name;
  8391. $out .= ' /Name /F'.$font['i'];
  8392. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  8393. $out .= ' /Encoding /WinAnsiEncoding';
  8394. }
  8395. if ($k == 'helvetica') {
  8396. // add default font for annotations
  8397. $this->annotation_fonts[$k] = $font['i'];
  8398. }
  8399. $out .= ' >>';
  8400. $out .= "\n".'endobj';
  8401. $this->_out($out);
  8402. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  8403. // additional Type1 or TrueType font
  8404. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8405. $out .= '<</Type /Font';
  8406. $out .= ' /Subtype /'.$type;
  8407. $out .= ' /BaseFont /'.$name;
  8408. $out .= ' /Name /F'.$font['i'];
  8409. $out .= ' /FirstChar 32 /LastChar 255';
  8410. $out .= ' /Widths '.($this->n + 1).' 0 R';
  8411. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  8412. if ($font['enc']) {
  8413. if (isset($font['diff'])) {
  8414. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  8415. } else {
  8416. $out .= ' /Encoding /WinAnsiEncoding';
  8417. }
  8418. }
  8419. $out .= ' >>';
  8420. $out .= "\n".'endobj';
  8421. $this->_out($out);
  8422. // Widths
  8423. $this->_newobj();
  8424. $s = '[';
  8425. for ($i = 32; $i < 256; ++$i) {
  8426. if (isset($font['cw'][$i])) {
  8427. $s .= $font['cw'][$i].' ';
  8428. } else {
  8429. $s .= $font['dw'].' ';
  8430. }
  8431. }
  8432. $s .= ']';
  8433. $s .= "\n".'endobj';
  8434. $this->_out($s);
  8435. //Descriptor
  8436. $this->_newobj();
  8437. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8438. foreach ($font['desc'] as $fdk => $fdv) {
  8439. if (is_float($fdv)) {
  8440. $fdv = sprintf('%F', $fdv);
  8441. }
  8442. $s .= ' /'.$fdk.' '.$fdv.'';
  8443. }
  8444. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8445. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8446. }
  8447. $s .= '>>';
  8448. $s .= "\n".'endobj';
  8449. $this->_out($s);
  8450. } else {
  8451. // additional types
  8452. $mtd = '_put'.strtolower($type);
  8453. if (!method_exists($this, $mtd)) {
  8454. $this->Error('Unsupported font type: '.$type);
  8455. }
  8456. $this->$mtd($font);
  8457. }
  8458. }
  8459. }
  8460. /**
  8461. * Adds unicode fonts.<br>
  8462. * Based on PDF Reference 1.3 (section 5)
  8463. * @param $font (array) font data
  8464. * @protected
  8465. * @author Nicola Asuni
  8466. * @since 1.52.0.TC005 (2005-01-05)
  8467. */
  8468. protected function _puttruetypeunicode($font) {
  8469. $fontname = '';
  8470. if ($font['subset']) {
  8471. // change name for font subsetting
  8472. $subtag = sprintf('%06u', $font['i']);
  8473. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  8474. $fontname .= $subtag.'+';
  8475. }
  8476. $fontname .= $font['name'];
  8477. // Type0 Font
  8478. // A composite font composed of other fonts, organized hierarchically
  8479. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8480. $out .= '<< /Type /Font';
  8481. $out .= ' /Subtype /Type0';
  8482. $out .= ' /BaseFont /'.$fontname;
  8483. $out .= ' /Name /F'.$font['i'];
  8484. $out .= ' /Encoding /'.$font['enc'];
  8485. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  8486. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  8487. $out .= ' >>';
  8488. $out .= "\n".'endobj';
  8489. $this->_out($out);
  8490. // ToUnicode map for Identity-H
  8491. $stream = TCPDF_FONT_DATA::$uni_identity_h;
  8492. // ToUnicode Object
  8493. $this->_newobj();
  8494. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  8495. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8496. $stream = $this->_getrawstream($stream);
  8497. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  8498. // CIDFontType2
  8499. // A CIDFont whose glyph descriptions are based on TrueType font technology
  8500. $oid = $this->_newobj();
  8501. $out = '<< /Type /Font';
  8502. $out .= ' /Subtype /CIDFontType2';
  8503. $out .= ' /BaseFont /'.$fontname;
  8504. // A dictionary containing entries that define the character collection of the CIDFont.
  8505. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8506. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8507. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8508. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  8509. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8510. $out .= ' /DW '.$font['dw']; // default width
  8511. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, 0);
  8512. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8513. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  8514. }
  8515. $out .= ' >>';
  8516. $out .= "\n".'endobj';
  8517. $this->_out($out);
  8518. // Font descriptor
  8519. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  8520. $this->_newobj();
  8521. $out = '<< /Type /FontDescriptor';
  8522. $out .= ' /FontName /'.$fontname;
  8523. foreach ($font['desc'] as $key => $value) {
  8524. if (is_float($value)) {
  8525. $value = sprintf('%F', $value);
  8526. }
  8527. $out .= ' /'.$key.' '.$value;
  8528. }
  8529. $fontdir = false;
  8530. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8531. // A stream containing a TrueType font
  8532. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8533. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  8534. }
  8535. $out .= ' >>';
  8536. $out .= "\n".'endobj';
  8537. $this->_out($out);
  8538. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8539. $this->_newobj();
  8540. // Embed CIDToGIDMap
  8541. // A specification of the mapping from CIDs to glyph indices
  8542. // search and get CTG font file to embedd
  8543. $ctgfile = strtolower($font['ctg']);
  8544. // search and get ctg font file to embedd
  8545. $fontfile = TCPDF_FONTS::getFontFullPath($ctgfile, $fontdir);
  8546. if (TCPDF_STATIC::empty_string($fontfile)) {
  8547. $this->Error('Font file not found: '.$ctgfile);
  8548. }
  8549. $stream = $this->_getrawstream(file_get_contents($fontfile));
  8550. $out = '<< /Length '.strlen($stream).'';
  8551. if (substr($fontfile, -2) == '.z') { // check file extension
  8552. // Decompresses data encoded using the public-domain
  8553. // zlib/deflate compression method, reproducing the
  8554. // original text or binary data
  8555. $out .= ' /Filter /FlateDecode';
  8556. }
  8557. $out .= ' >>';
  8558. $out .= ' stream'."\n".$stream."\n".'endstream';
  8559. $out .= "\n".'endobj';
  8560. $this->_out($out);
  8561. }
  8562. }
  8563. /**
  8564. * Output CID-0 fonts.
  8565. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  8566. * @param $font (array) font data
  8567. * @protected
  8568. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  8569. * @since 3.2.000 (2008-06-23)
  8570. */
  8571. protected function _putcidfont0($font) {
  8572. $cidoffset = 0;
  8573. if (!isset($font['cw'][1])) {
  8574. $cidoffset = 31;
  8575. }
  8576. if (isset($font['cidinfo']['uni2cid'])) {
  8577. // convert unicode to cid.
  8578. $uni2cid = $font['cidinfo']['uni2cid'];
  8579. $cw = array();
  8580. foreach ($font['cw'] as $uni => $width) {
  8581. if (isset($uni2cid[$uni])) {
  8582. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  8583. } elseif ($uni < 256) {
  8584. $cw[$uni] = $width;
  8585. } // else unknown character
  8586. }
  8587. $font = array_merge($font, array('cw' => $cw));
  8588. }
  8589. $name = $font['name'];
  8590. $enc = $font['enc'];
  8591. if ($enc) {
  8592. $longname = $name.'-'.$enc;
  8593. } else {
  8594. $longname = $name;
  8595. }
  8596. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8597. $out .= '<</Type /Font';
  8598. $out .= ' /Subtype /Type0';
  8599. $out .= ' /BaseFont /'.$longname;
  8600. $out .= ' /Name /F'.$font['i'];
  8601. if ($enc) {
  8602. $out .= ' /Encoding /'.$enc;
  8603. }
  8604. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  8605. $out .= ' >>';
  8606. $out .= "\n".'endobj';
  8607. $this->_out($out);
  8608. $oid = $this->_newobj();
  8609. $out = '<</Type /Font';
  8610. $out .= ' /Subtype /CIDFontType0';
  8611. $out .= ' /BaseFont /'.$name;
  8612. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8613. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8614. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8615. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  8616. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8617. $out .= ' /DW '.$font['dw'];
  8618. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, $cidoffset);
  8619. $out .= ' >>';
  8620. $out .= "\n".'endobj';
  8621. $this->_out($out);
  8622. $this->_newobj();
  8623. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8624. foreach ($font['desc'] as $k => $v) {
  8625. if ($k != 'Style') {
  8626. if (is_float($v)) {
  8627. $v = sprintf('%F', $v);
  8628. }
  8629. $s .= ' /'.$k.' '.$v.'';
  8630. }
  8631. }
  8632. $s .= '>>';
  8633. $s .= "\n".'endobj';
  8634. $this->_out($s);
  8635. }
  8636. /**
  8637. * Output images.
  8638. * @protected
  8639. */
  8640. protected function _putimages() {
  8641. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8642. foreach ($this->imagekeys as $file) {
  8643. $info = $this->getImageBuffer($file);
  8644. // set object for alternate images array
  8645. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  8646. $altoid = $this->_newobj();
  8647. $out = '[';
  8648. foreach ($info['altimgs'] as $altimage) {
  8649. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  8650. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  8651. $out .= ' /DefaultForPrinting';
  8652. if ($altimage[1] === true) {
  8653. $out .= ' true';
  8654. } else {
  8655. $out .= ' false';
  8656. }
  8657. $out .= ' >>';
  8658. }
  8659. }
  8660. $out .= ' ]';
  8661. $out .= "\n".'endobj';
  8662. $this->_out($out);
  8663. }
  8664. // set image object
  8665. $oid = $this->_newobj();
  8666. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  8667. $this->setImageSubBuffer($file, 'n', $this->n);
  8668. $out = '<</Type /XObject';
  8669. $out .= ' /Subtype /Image';
  8670. $out .= ' /Width '.$info['w'];
  8671. $out .= ' /Height '.$info['h'];
  8672. if (array_key_exists('masked', $info)) {
  8673. $out .= ' /SMask '.($this->n - 1).' 0 R';
  8674. }
  8675. // set color space
  8676. $icc = false;
  8677. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  8678. // ICC Colour Space
  8679. $icc = true;
  8680. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  8681. } elseif ($info['cs'] == 'Indexed') {
  8682. // Indexed Colour Space
  8683. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  8684. } else {
  8685. // Device Colour Space
  8686. $out .= ' /ColorSpace /'.$info['cs'];
  8687. }
  8688. if ($info['cs'] == 'DeviceCMYK') {
  8689. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  8690. }
  8691. $out .= ' /BitsPerComponent '.$info['bpc'];
  8692. if (isset($altoid) AND ($altoid > 0)) {
  8693. // reference to alternate images dictionary
  8694. $out .= ' /Alternates '.$altoid.' 0 R';
  8695. }
  8696. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  8697. // external stream
  8698. $out .= ' /Length 0';
  8699. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  8700. if (isset($info['f'])) {
  8701. $out .= ' /FFilter /'.$info['f'];
  8702. }
  8703. $out .= ' >>';
  8704. $out .= ' stream'."\n".'endstream';
  8705. } else {
  8706. if (isset($info['f'])) {
  8707. $out .= ' /Filter /'.$info['f'];
  8708. }
  8709. if (isset($info['parms'])) {
  8710. $out .= ' '.$info['parms'];
  8711. }
  8712. if (isset($info['trns']) AND is_array($info['trns'])) {
  8713. $trns = '';
  8714. $count_info = count($info['trns']);
  8715. for ($i=0; $i < $count_info; ++$i) {
  8716. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  8717. }
  8718. $out .= ' /Mask ['.$trns.']';
  8719. }
  8720. $stream = $this->_getrawstream($info['data']);
  8721. $out .= ' /Length '.strlen($stream).' >>';
  8722. $out .= ' stream'."\n".$stream."\n".'endstream';
  8723. }
  8724. $out .= "\n".'endobj';
  8725. $this->_out($out);
  8726. if ($icc) {
  8727. // ICC colour profile
  8728. $this->_newobj();
  8729. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  8730. $icc = $this->_getrawstream($icc);
  8731. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  8732. } elseif ($info['cs'] == 'Indexed') {
  8733. // colour palette
  8734. $this->_newobj();
  8735. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  8736. $pal = $this->_getrawstream($pal);
  8737. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  8738. }
  8739. }
  8740. }
  8741. /**
  8742. * Output Form XObjects Templates.
  8743. * @author Nicola Asuni
  8744. * @since 5.8.017 (2010-08-24)
  8745. * @protected
  8746. * @see startTemplate(), endTemplate(), printTemplate()
  8747. */
  8748. protected function _putxobjects() {
  8749. foreach ($this->xobjects as $key => $data) {
  8750. if (isset($data['outdata'])) {
  8751. $stream = str_replace($this->epsmarker, '', trim($data['outdata']));
  8752. $out = $this->_getobj($data['n'])."\n";
  8753. $out .= '<<';
  8754. $out .= ' /Type /XObject';
  8755. $out .= ' /Subtype /Form';
  8756. $out .= ' /FormType 1';
  8757. if ($this->compress) {
  8758. $stream = gzcompress($stream);
  8759. $out .= ' /Filter /FlateDecode';
  8760. }
  8761. $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));
  8762. $out .= ' /Matrix [1 0 0 1 0 0]';
  8763. $out .= ' /Resources <<';
  8764. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8765. if (!$this->pdfa_mode) {
  8766. // transparency
  8767. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  8768. $out .= ' /ExtGState <<';
  8769. foreach ($data['extgstates'] as $k => $extgstate) {
  8770. if (isset($this->extgstates[$k]['name'])) {
  8771. $out .= ' /'.$this->extgstates[$k]['name'];
  8772. } else {
  8773. $out .= ' /GS'.$k;
  8774. }
  8775. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  8776. }
  8777. $out .= ' >>';
  8778. }
  8779. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  8780. $gp = '';
  8781. $gs = '';
  8782. foreach ($data['gradients'] as $id => $grad) {
  8783. // gradient patterns
  8784. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  8785. // gradient shadings
  8786. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  8787. }
  8788. $out .= ' /Pattern <<'.$gp.' >>';
  8789. $out .= ' /Shading <<'.$gs.' >>';
  8790. }
  8791. }
  8792. // spot colors
  8793. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  8794. $out .= ' /ColorSpace <<';
  8795. foreach ($data['spot_colors'] as $name => $color) {
  8796. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  8797. }
  8798. $out .= ' >>';
  8799. }
  8800. // fonts
  8801. if (!empty($data['fonts'])) {
  8802. $out .= ' /Font <<';
  8803. foreach ($data['fonts'] as $fontkey => $fontid) {
  8804. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  8805. }
  8806. $out .= ' >>';
  8807. }
  8808. // images or nested xobjects
  8809. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  8810. $out .= ' /XObject <<';
  8811. foreach ($data['images'] as $imgid) {
  8812. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  8813. }
  8814. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  8815. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  8816. }
  8817. $out .= ' >>';
  8818. }
  8819. $out .= ' >>'; //end resources
  8820. if (isset($data['group']) AND ($data['group'] !== false)) {
  8821. // set transparency group
  8822. $out .= ' /Group << /Type /Group /S /Transparency';
  8823. if (is_array($data['group'])) {
  8824. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  8825. $out .= ' /CS /'.$data['group']['CS'];
  8826. }
  8827. if (isset($data['group']['I'])) {
  8828. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  8829. }
  8830. if (isset($data['group']['K'])) {
  8831. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  8832. }
  8833. }
  8834. $out .= ' >>';
  8835. }
  8836. $stream = $this->_getrawstream($stream, $data['n']);
  8837. $out .= ' /Length '.strlen($stream);
  8838. $out .= ' >>';
  8839. $out .= ' stream'."\n".$stream."\n".'endstream';
  8840. $out .= "\n".'endobj';
  8841. $this->_out($out);
  8842. }
  8843. }
  8844. }
  8845. /**
  8846. * Output Spot Colors Resources.
  8847. * @protected
  8848. * @since 4.0.024 (2008-09-12)
  8849. */
  8850. protected function _putspotcolors() {
  8851. foreach ($this->spot_colors as $name => $color) {
  8852. $this->_newobj();
  8853. $this->spot_colors[$name]['n'] = $this->n;
  8854. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  8855. $out .= ' /DeviceCMYK <<';
  8856. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  8857. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  8858. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  8859. $out .= "\n".'endobj';
  8860. $this->_out($out);
  8861. }
  8862. }
  8863. /**
  8864. * Return XObjects Dictionary.
  8865. * @return string XObjects dictionary
  8866. * @protected
  8867. * @since 5.8.014 (2010-08-23)
  8868. */
  8869. protected function _getxobjectdict() {
  8870. $out = '';
  8871. foreach ($this->xobjects as $id => $objid) {
  8872. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  8873. }
  8874. return $out;
  8875. }
  8876. /**
  8877. * Output Resources Dictionary.
  8878. * @protected
  8879. */
  8880. protected function _putresourcedict() {
  8881. $out = $this->_getobj(2)."\n";
  8882. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8883. $out .= ' /Font <<';
  8884. foreach ($this->fontkeys as $fontkey) {
  8885. $font = $this->getFontBuffer($fontkey);
  8886. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  8887. }
  8888. $out .= ' >>';
  8889. $out .= ' /XObject <<';
  8890. $out .= $this->_getxobjectdict();
  8891. $out .= ' >>';
  8892. // layers
  8893. if (!empty($this->pdflayers)) {
  8894. $out .= ' /Properties <<';
  8895. foreach ($this->pdflayers as $layer) {
  8896. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  8897. }
  8898. $out .= ' >>';
  8899. }
  8900. if (!$this->pdfa_mode) {
  8901. // transparency
  8902. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  8903. $out .= ' /ExtGState <<';
  8904. foreach ($this->extgstates as $k => $extgstate) {
  8905. if (isset($extgstate['name'])) {
  8906. $out .= ' /'.$extgstate['name'];
  8907. } else {
  8908. $out .= ' /GS'.$k;
  8909. }
  8910. $out .= ' '.$extgstate['n'].' 0 R';
  8911. }
  8912. $out .= ' >>';
  8913. }
  8914. if (isset($this->gradients) AND !empty($this->gradients)) {
  8915. $gp = '';
  8916. $gs = '';
  8917. foreach ($this->gradients as $id => $grad) {
  8918. // gradient patterns
  8919. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  8920. // gradient shadings
  8921. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  8922. }
  8923. $out .= ' /Pattern <<'.$gp.' >>';
  8924. $out .= ' /Shading <<'.$gs.' >>';
  8925. }
  8926. }
  8927. // spot colors
  8928. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  8929. $out .= ' /ColorSpace <<';
  8930. foreach ($this->spot_colors as $color) {
  8931. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  8932. }
  8933. $out .= ' >>';
  8934. }
  8935. $out .= ' >>';
  8936. $out .= "\n".'endobj';
  8937. $this->_out($out);
  8938. }
  8939. /**
  8940. * Output Resources.
  8941. * @protected
  8942. */
  8943. protected function _putresources() {
  8944. $this->_putextgstates();
  8945. $this->_putocg();
  8946. $this->_putfonts();
  8947. $this->_putimages();
  8948. $this->_putspotcolors();
  8949. $this->_putshaders();
  8950. $this->_putxobjects();
  8951. $this->_putresourcedict();
  8952. $this->_putdests();
  8953. $this->_putEmbeddedFiles();
  8954. $this->_putannotsobjs();
  8955. $this->_putjavascript();
  8956. $this->_putbookmarks();
  8957. $this->_putencryption();
  8958. }
  8959. /**
  8960. * Adds some Metadata information (Document Information Dictionary)
  8961. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  8962. * @return int object id
  8963. * @protected
  8964. */
  8965. protected function _putinfo() {
  8966. $oid = $this->_newobj();
  8967. $out = '<<';
  8968. // store current isunicode value
  8969. $prev_isunicode = $this->isunicode;
  8970. if ($this->docinfounicode) {
  8971. $this->isunicode = true;
  8972. }
  8973. if (!TCPDF_STATIC::empty_string($this->title)) {
  8974. // The document's title.
  8975. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  8976. }
  8977. if (!TCPDF_STATIC::empty_string($this->author)) {
  8978. // The name of the person who created the document.
  8979. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  8980. }
  8981. if (!TCPDF_STATIC::empty_string($this->subject)) {
  8982. // The subject of the document.
  8983. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  8984. }
  8985. if (!TCPDF_STATIC::empty_string($this->keywords)) {
  8986. // Keywords associated with the document.
  8987. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  8988. }
  8989. if (!TCPDF_STATIC::empty_string($this->creator)) {
  8990. // 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.
  8991. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  8992. }
  8993. // restore previous isunicode value
  8994. $this->isunicode = $prev_isunicode;
  8995. // default producer
  8996. $out .= ' /Producer '.$this->_textstring(TCPDF_STATIC::getTCPDFProducer(), $oid);
  8997. // The date and time the document was created, in human-readable form
  8998. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  8999. // The date and time the document was most recently modified, in human-readable form
  9000. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  9001. // A name object indicating whether the document has been modified to include trapping information
  9002. $out .= ' /Trapped /False';
  9003. $out .= ' >>';
  9004. $out .= "\n".'endobj';
  9005. $this->_out($out);
  9006. return $oid;
  9007. }
  9008. /**
  9009. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  9010. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  9011. * @param $xmp (string) Custom XMP data.
  9012. * @since 5.9.128 (2011-10-06)
  9013. * @public
  9014. */
  9015. public function setExtraXMP($xmp) {
  9016. $this->custom_xmp = $xmp;
  9017. }
  9018. /**
  9019. * Put XMP data object and return ID.
  9020. * @return (int) The object ID.
  9021. * @since 5.9.121 (2011-09-28)
  9022. * @protected
  9023. */
  9024. protected function _putXMP() {
  9025. $oid = $this->_newobj();
  9026. // store current isunicode value
  9027. $prev_isunicode = $this->isunicode;
  9028. $this->isunicode = true;
  9029. $prev_encrypted = $this->encrypted;
  9030. $this->encrypted = false;
  9031. // set XMP data
  9032. $xmp = '<?xpacket begin="'.TCPDF_FONTS::unichr(0xfeff, $this->isunicode).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  9033. $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";
  9034. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  9035. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  9036. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  9037. $xmp .= "\t\t\t".'<dc:title>'."\n";
  9038. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9039. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->title).'</rdf:li>'."\n";
  9040. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9041. $xmp .= "\t\t\t".'</dc:title>'."\n";
  9042. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  9043. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  9044. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->author).'</rdf:li>'."\n";
  9045. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  9046. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  9047. $xmp .= "\t\t\t".'<dc:description>'."\n";
  9048. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9049. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->subject).'</rdf:li>'."\n";
  9050. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9051. $xmp .= "\t\t\t".'</dc:description>'."\n";
  9052. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  9053. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9054. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->keywords).' TCPDF</rdf:li>'."\n";
  9055. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9056. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  9057. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9058. // convert doc creation date format
  9059. $dcdate = TCPDF_STATIC::getFormattedDate($this->doc_creation_timestamp);
  9060. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  9061. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  9062. $doccreationdate .= '+'.substr($dcdate, 15, 2).':'.substr($dcdate, 18, 2);
  9063. $doccreationdate = TCPDF_STATIC::_escapeXML($doccreationdate);
  9064. // convert doc modification date format
  9065. $dmdate = TCPDF_STATIC::getFormattedDate($this->doc_modification_timestamp);
  9066. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  9067. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  9068. $docmoddate .= '+'.substr($dmdate, 15, 2).':'.substr($dmdate, 18, 2);
  9069. $docmoddate = TCPDF_STATIC::_escapeXML($docmoddate);
  9070. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  9071. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  9072. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  9073. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  9074. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  9075. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9076. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  9077. $xmp .= "\t\t\t".'<pdf:Keywords>'.TCPDF_STATIC::_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  9078. $xmp .= "\t\t\t".'<pdf:Producer>'.TCPDF_STATIC::_escapeXML(TCPDF_STATIC::getTCPDFProducer()).'</pdf:Producer>'."\n";
  9079. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9080. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  9081. $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);
  9082. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  9083. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  9084. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9085. if ($this->pdfa_mode) {
  9086. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  9087. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  9088. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  9089. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9090. }
  9091. // XMP extension schemas
  9092. $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";
  9093. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  9094. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9095. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9096. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  9097. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  9098. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  9099. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9100. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9101. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  9102. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  9103. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  9104. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9105. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9106. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9107. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9108. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  9109. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  9110. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  9111. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9112. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9113. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9114. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9115. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9116. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  9117. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  9118. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  9119. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9120. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9121. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9122. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9123. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  9124. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  9125. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  9126. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9127. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9128. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9129. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  9130. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  9131. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9132. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\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>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  9136. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  9137. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</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".'</rdf:Seq>'."\n";
  9140. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9141. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9142. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9143. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  9144. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9145. $xmp .= "\t".'</rdf:RDF>'."\n";
  9146. $xmp .= $this->custom_xmp;
  9147. $xmp .= '</x:xmpmeta>'."\n";
  9148. $xmp .= '<?xpacket end="w"?>';
  9149. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  9150. // restore previous isunicode value
  9151. $this->isunicode = $prev_isunicode;
  9152. $this->encrypted = $prev_encrypted;
  9153. $this->_out($out);
  9154. return $oid;
  9155. }
  9156. /**
  9157. * Output Catalog.
  9158. * @return int object id
  9159. * @protected
  9160. */
  9161. protected function _putcatalog() {
  9162. // put XMP
  9163. $xmpobj = $this->_putXMP();
  9164. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  9165. if ($this->pdfa_mode OR $this->force_srgb) {
  9166. $iccobj = $this->_newobj();
  9167. $icc = file_get_contents(dirname(__FILE__).'/include/sRGB.icc');
  9168. $filter = '';
  9169. if ($this->compress) {
  9170. $filter = ' /Filter /FlateDecode';
  9171. $icc = gzcompress($icc);
  9172. }
  9173. $icc = $this->_getrawstream($icc);
  9174. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  9175. }
  9176. // start catalog
  9177. $oid = $this->_newobj();
  9178. $out = '<< /Type /Catalog';
  9179. $out .= ' /Version /'.$this->PDFVersion;
  9180. //$out .= ' /Extensions <<>>';
  9181. $out .= ' /Pages 1 0 R';
  9182. //$out .= ' /PageLabels ' //...;
  9183. $out .= ' /Names <<';
  9184. if ((!$this->pdfa_mode) AND !empty($this->n_js)) {
  9185. $out .= ' /JavaScript '.$this->n_js;
  9186. }
  9187. if (!empty($this->efnames)) {
  9188. $out .= ' /EmbeddedFiles <</Names [';
  9189. foreach ($this->efnames AS $fn => $fref) {
  9190. $out .= ' '.$this->_datastring($fn).' '.$fref;
  9191. }
  9192. $out .= ' ]>>';
  9193. }
  9194. $out .= ' >>';
  9195. if (!empty($this->dests)) {
  9196. $out .= ' /Dests '.($this->n_dests).' 0 R';
  9197. }
  9198. $out .= $this->_putviewerpreferences();
  9199. if (isset($this->LayoutMode) AND (!TCPDF_STATIC::empty_string($this->LayoutMode))) {
  9200. $out .= ' /PageLayout /'.$this->LayoutMode;
  9201. }
  9202. if (isset($this->PageMode) AND (!TCPDF_STATIC::empty_string($this->PageMode))) {
  9203. $out .= ' /PageMode /'.$this->PageMode;
  9204. }
  9205. if (count($this->outlines) > 0) {
  9206. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  9207. $out .= ' /PageMode /UseOutlines';
  9208. }
  9209. //$out .= ' /Threads []';
  9210. if ($this->ZoomMode == 'fullpage') {
  9211. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  9212. } elseif ($this->ZoomMode == 'fullwidth') {
  9213. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  9214. } elseif ($this->ZoomMode == 'real') {
  9215. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  9216. } elseif (!is_string($this->ZoomMode)) {
  9217. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  9218. }
  9219. //$out .= ' /AA <<>>';
  9220. //$out .= ' /URI <<>>';
  9221. $out .= ' /Metadata '.$xmpobj.' 0 R';
  9222. //$out .= ' /StructTreeRoot <<>>';
  9223. //$out .= ' /MarkInfo <<>>';
  9224. if (isset($this->l['a_meta_language'])) {
  9225. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  9226. }
  9227. //$out .= ' /SpiderInfo <<>>';
  9228. // set OutputIntent to sRGB IEC61966-2.1 if required
  9229. if ($this->pdfa_mode OR $this->force_srgb) {
  9230. $out .= ' /OutputIntents [<<';
  9231. $out .= ' /Type /OutputIntent';
  9232. $out .= ' /S /GTS_PDFA1';
  9233. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9234. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9235. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  9236. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9237. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  9238. $out .= ' >>]';
  9239. }
  9240. //$out .= ' /PieceInfo <<>>';
  9241. if (!empty($this->pdflayers)) {
  9242. $lyrobjs = '';
  9243. $lyrobjs_print = '';
  9244. $lyrobjs_view = '';
  9245. foreach ($this->pdflayers as $layer) {
  9246. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  9247. if ($layer['print']) {
  9248. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  9249. }
  9250. if ($layer['view']) {
  9251. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  9252. }
  9253. }
  9254. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  9255. $out .= ' /D <<';
  9256. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  9257. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  9258. $out .= ' /BaseState /ON';
  9259. $out .= ' /ON ['.$lyrobjs_print.']';
  9260. $out .= ' /OFF ['.$lyrobjs_view.']';
  9261. $out .= ' /Intent /View';
  9262. $out .= ' /AS [';
  9263. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  9264. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  9265. $out .= ' ]';
  9266. $out .= ' /Order ['.$lyrobjs.']';
  9267. $out .= ' /ListMode /AllPages';
  9268. //$out .= ' /RBGroups ['..']';
  9269. //$out .= ' /Locked ['..']';
  9270. $out .= ' >>';
  9271. $out .= ' >>';
  9272. }
  9273. // AcroForm
  9274. if (!empty($this->form_obj_id)
  9275. OR ($this->sign AND isset($this->signature_data['cert_type']))
  9276. OR !empty($this->empty_signature_appearance)) {
  9277. $out .= ' /AcroForm <<';
  9278. $objrefs = '';
  9279. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9280. // set reference for signature object
  9281. $objrefs .= $this->sig_obj_id.' 0 R';
  9282. }
  9283. if (!empty($this->empty_signature_appearance)) {
  9284. foreach ($this->empty_signature_appearance as $esa) {
  9285. // set reference for empty signature objects
  9286. $objrefs .= ' '.$esa['objid'].' 0 R';
  9287. }
  9288. }
  9289. if (!empty($this->form_obj_id)) {
  9290. foreach($this->form_obj_id as $objid) {
  9291. $objrefs .= ' '.$objid.' 0 R';
  9292. }
  9293. }
  9294. $out .= ' /Fields ['.$objrefs.']';
  9295. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  9296. $out .= ' /NeedAppearances false';
  9297. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9298. if ($this->signature_data['cert_type'] > 0) {
  9299. $out .= ' /SigFlags 3';
  9300. } else {
  9301. $out .= ' /SigFlags 1';
  9302. }
  9303. }
  9304. //$out .= ' /CO ';
  9305. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  9306. $out .= ' /DR <<';
  9307. $out .= ' /Font <<';
  9308. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  9309. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  9310. }
  9311. $out .= ' >> >>';
  9312. }
  9313. $font = $this->getFontBuffer('helvetica');
  9314. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  9315. $out .= ' /Q '.(($this->rtl)?'2':'0');
  9316. //$out .= ' /XFA ';
  9317. $out .= ' >>';
  9318. // signatures
  9319. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9320. if ($this->signature_data['cert_type'] > 0) {
  9321. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  9322. } else {
  9323. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  9324. }
  9325. }
  9326. }
  9327. //$out .= ' /Legal <<>>';
  9328. //$out .= ' /Requirements []';
  9329. //$out .= ' /Collection <<>>';
  9330. //$out .= ' /NeedsRendering true';
  9331. $out .= ' >>';
  9332. $out .= "\n".'endobj';
  9333. $this->_out($out);
  9334. return $oid;
  9335. }
  9336. /**
  9337. * Output viewer preferences.
  9338. * @return string for viewer preferences
  9339. * @author Nicola asuni
  9340. * @since 3.1.000 (2008-06-09)
  9341. * @protected
  9342. */
  9343. protected function _putviewerpreferences() {
  9344. $vp = $this->viewer_preferences;
  9345. $out = ' /ViewerPreferences <<';
  9346. if ($this->rtl) {
  9347. $out .= ' /Direction /R2L';
  9348. } else {
  9349. $out .= ' /Direction /L2R';
  9350. }
  9351. if (isset($vp['HideToolbar']) AND ($vp['HideToolbar'])) {
  9352. $out .= ' /HideToolbar true';
  9353. }
  9354. if (isset($vp['HideMenubar']) AND ($vp['HideMenubar'])) {
  9355. $out .= ' /HideMenubar true';
  9356. }
  9357. if (isset($vp['HideWindowUI']) AND ($vp['HideWindowUI'])) {
  9358. $out .= ' /HideWindowUI true';
  9359. }
  9360. if (isset($vp['FitWindow']) AND ($vp['FitWindow'])) {
  9361. $out .= ' /FitWindow true';
  9362. }
  9363. if (isset($vp['CenterWindow']) AND ($vp['CenterWindow'])) {
  9364. $out .= ' /CenterWindow true';
  9365. }
  9366. if (isset($vp['DisplayDocTitle']) AND ($vp['DisplayDocTitle'])) {
  9367. $out .= ' /DisplayDocTitle true';
  9368. }
  9369. if (isset($vp['NonFullScreenPageMode'])) {
  9370. $out .= ' /NonFullScreenPageMode /'.$vp['NonFullScreenPageMode'];
  9371. }
  9372. if (isset($vp['ViewArea'])) {
  9373. $out .= ' /ViewArea /'.$vp['ViewArea'];
  9374. }
  9375. if (isset($vp['ViewClip'])) {
  9376. $out .= ' /ViewClip /'.$vp['ViewClip'];
  9377. }
  9378. if (isset($vp['PrintArea'])) {
  9379. $out .= ' /PrintArea /'.$vp['PrintArea'];
  9380. }
  9381. if (isset($vp['PrintClip'])) {
  9382. $out .= ' /PrintClip /'.$vp['PrintClip'];
  9383. }
  9384. if (isset($vp['PrintScaling'])) {
  9385. $out .= ' /PrintScaling /'.$vp['PrintScaling'];
  9386. }
  9387. if (isset($vp['Duplex']) AND (!TCPDF_STATIC::empty_string($vp['Duplex']))) {
  9388. $out .= ' /Duplex /'.$vp['Duplex'];
  9389. }
  9390. if (isset($vp['PickTrayByPDFSize'])) {
  9391. if ($vp['PickTrayByPDFSize']) {
  9392. $out .= ' /PickTrayByPDFSize true';
  9393. } else {
  9394. $out .= ' /PickTrayByPDFSize false';
  9395. }
  9396. }
  9397. if (isset($vp['PrintPageRange'])) {
  9398. $PrintPageRangeNum = '';
  9399. foreach ($vp['PrintPageRange'] as $k => $v) {
  9400. $PrintPageRangeNum .= ' '.($v - 1).'';
  9401. }
  9402. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  9403. }
  9404. if (isset($vp['NumCopies'])) {
  9405. $out .= ' /NumCopies '.intval($vp['NumCopies']);
  9406. }
  9407. $out .= ' >>';
  9408. return $out;
  9409. }
  9410. /**
  9411. * Output PDF File Header (7.5.2).
  9412. * @protected
  9413. */
  9414. protected function _putheader() {
  9415. $this->_out('%PDF-'.$this->PDFVersion);
  9416. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  9417. }
  9418. /**
  9419. * Output end of document (EOF).
  9420. * @protected
  9421. */
  9422. protected function _enddoc() {
  9423. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  9424. // save subset chars of the previous font
  9425. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  9426. }
  9427. $this->state = 1;
  9428. $this->_putheader();
  9429. $this->_putpages();
  9430. $this->_putresources();
  9431. // empty signature fields
  9432. if (!empty($this->empty_signature_appearance)) {
  9433. foreach ($this->empty_signature_appearance as $key => $esa) {
  9434. // widget annotation for empty signature
  9435. $out = $this->_getobj($esa['objid'])."\n";
  9436. $out .= '<< /Type /Annot';
  9437. $out .= ' /Subtype /Widget';
  9438. $out .= ' /Rect ['.$esa['rect'].']';
  9439. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  9440. $out .= ' /F 4';
  9441. $out .= ' /FT /Sig';
  9442. $signame = $esa['name'].sprintf(' [%03d]', ($key + 1));
  9443. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  9444. $out .= ' /Ff 0';
  9445. $out .= ' >>';
  9446. $out .= "\n".'endobj';
  9447. $this->_out($out);
  9448. }
  9449. }
  9450. // Signature
  9451. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9452. // widget annotation for signature
  9453. $out = $this->_getobj($this->sig_obj_id)."\n";
  9454. $out .= '<< /Type /Annot';
  9455. $out .= ' /Subtype /Widget';
  9456. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  9457. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  9458. $out .= ' /F 4';
  9459. $out .= ' /FT /Sig';
  9460. $out .= ' /T '.$this->_textstring($this->signature_appearance['name'], $this->sig_obj_id);
  9461. $out .= ' /Ff 0';
  9462. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  9463. $out .= ' >>';
  9464. $out .= "\n".'endobj';
  9465. $this->_out($out);
  9466. // signature
  9467. $this->_putsignature();
  9468. }
  9469. // Info
  9470. $objid_info = $this->_putinfo();
  9471. // Catalog
  9472. $objid_catalog = $this->_putcatalog();
  9473. // Cross-ref
  9474. $o = $this->bufferlen;
  9475. // XREF section
  9476. $this->_out('xref');
  9477. $this->_out('0 '.($this->n + 1));
  9478. $this->_out('0000000000 65535 f ');
  9479. $freegen = ($this->n + 2);
  9480. for ($i=1; $i <= $this->n; ++$i) {
  9481. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  9482. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  9483. ++$freegen;
  9484. } else {
  9485. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  9486. }
  9487. }
  9488. // TRAILER
  9489. $out = 'trailer'."\n";
  9490. $out .= '<<';
  9491. $out .= ' /Size '.($this->n + 1);
  9492. $out .= ' /Root '.$objid_catalog.' 0 R';
  9493. $out .= ' /Info '.$objid_info.' 0 R';
  9494. if ($this->encrypted) {
  9495. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  9496. }
  9497. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  9498. $out .= ' >>';
  9499. $this->_out($out);
  9500. $this->_out('startxref');
  9501. $this->_out($o);
  9502. $this->_out('%%EOF');
  9503. $this->state = 3; // end-of-doc
  9504. if ($this->diskcache) {
  9505. // remove temporary files used for images
  9506. foreach ($this->imagekeys as $key) {
  9507. // remove temporary files
  9508. unlink($this->images[$key]);
  9509. }
  9510. foreach ($this->fontkeys as $key) {
  9511. // remove temporary files
  9512. unlink($this->fonts[$key]);
  9513. }
  9514. }
  9515. }
  9516. /**
  9517. * Initialize a new page.
  9518. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  9519. * @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().
  9520. * @protected
  9521. * @see getPageSizeFromFormat(), setPageFormat()
  9522. */
  9523. protected function _beginpage($orientation='', $format='') {
  9524. ++$this->page;
  9525. $this->pageobjects[$this->page] = array();
  9526. $this->setPageBuffer($this->page, '');
  9527. // initialize array for graphics tranformation positions inside a page buffer
  9528. $this->transfmrk[$this->page] = array();
  9529. $this->state = 2;
  9530. if (TCPDF_STATIC::empty_string($orientation)) {
  9531. if (isset($this->CurOrientation)) {
  9532. $orientation = $this->CurOrientation;
  9533. } elseif ($this->fwPt > $this->fhPt) {
  9534. // landscape
  9535. $orientation = 'L';
  9536. } else {
  9537. // portrait
  9538. $orientation = 'P';
  9539. }
  9540. }
  9541. if (TCPDF_STATIC::empty_string($format)) {
  9542. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  9543. $this->setPageOrientation($orientation);
  9544. } else {
  9545. $this->setPageFormat($format, $orientation);
  9546. }
  9547. if ($this->rtl) {
  9548. $this->x = $this->w - $this->rMargin;
  9549. } else {
  9550. $this->x = $this->lMargin;
  9551. }
  9552. $this->y = $this->tMargin;
  9553. if (isset($this->newpagegroup[$this->page])) {
  9554. // start a new group
  9555. $this->currpagegroup = $this->newpagegroup[$this->page];
  9556. $this->pagegroups[$this->currpagegroup] = 1;
  9557. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  9558. ++$this->pagegroups[$this->currpagegroup];
  9559. }
  9560. }
  9561. /**
  9562. * Mark end of page.
  9563. * @protected
  9564. */
  9565. protected function _endpage() {
  9566. $this->setVisibility('all');
  9567. $this->state = 1;
  9568. }
  9569. /**
  9570. * Begin a new object and return the object number.
  9571. * @return int object number
  9572. * @protected
  9573. */
  9574. protected function _newobj() {
  9575. $this->_out($this->_getobj());
  9576. return $this->n;
  9577. }
  9578. /**
  9579. * Return the starting object string for the selected object ID.
  9580. * @param $objid (int) Object ID (leave empty to get a new ID).
  9581. * @return string the starting object string
  9582. * @protected
  9583. * @since 5.8.009 (2010-08-20)
  9584. */
  9585. protected function _getobj($objid='') {
  9586. if ($objid === '') {
  9587. ++$this->n;
  9588. $objid = $this->n;
  9589. }
  9590. $this->offsets[$objid] = $this->bufferlen;
  9591. $this->pageobjects[$this->page][] = $objid;
  9592. return $objid.' 0 obj';
  9593. }
  9594. /**
  9595. * Underline text.
  9596. * @param $x (int) X coordinate
  9597. * @param $y (int) Y coordinate
  9598. * @param $txt (string) text to underline
  9599. * @protected
  9600. */
  9601. protected function _dounderline($x, $y, $txt) {
  9602. $w = $this->GetStringWidth($txt);
  9603. return $this->_dounderlinew($x, $y, $w);
  9604. }
  9605. /**
  9606. * Underline for rectangular text area.
  9607. * @param $x (int) X coordinate
  9608. * @param $y (int) Y coordinate
  9609. * @param $w (int) width to underline
  9610. * @protected
  9611. * @since 4.8.008 (2009-09-29)
  9612. */
  9613. protected function _dounderlinew($x, $y, $w) {
  9614. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9615. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  9616. }
  9617. /**
  9618. * Line through text.
  9619. * @param $x (int) X coordinate
  9620. * @param $y (int) Y coordinate
  9621. * @param $txt (string) text to linethrough
  9622. * @protected
  9623. */
  9624. protected function _dolinethrough($x, $y, $txt) {
  9625. $w = $this->GetStringWidth($txt);
  9626. return $this->_dolinethroughw($x, $y, $w);
  9627. }
  9628. /**
  9629. * Line through for rectangular text area.
  9630. * @param $x (int) X coordinate
  9631. * @param $y (int) Y coordinate
  9632. * @param $w (int) line length (width)
  9633. * @protected
  9634. * @since 4.9.008 (2009-09-29)
  9635. */
  9636. protected function _dolinethroughw($x, $y, $w) {
  9637. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9638. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  9639. }
  9640. /**
  9641. * Overline text.
  9642. * @param $x (int) X coordinate
  9643. * @param $y (int) Y coordinate
  9644. * @param $txt (string) text to overline
  9645. * @protected
  9646. * @since 4.9.015 (2010-04-19)
  9647. */
  9648. protected function _dooverline($x, $y, $txt) {
  9649. $w = $this->GetStringWidth($txt);
  9650. return $this->_dooverlinew($x, $y, $w);
  9651. }
  9652. /**
  9653. * Overline for rectangular text area.
  9654. * @param $x (int) X coordinate
  9655. * @param $y (int) Y coordinate
  9656. * @param $w (int) width to overline
  9657. * @protected
  9658. * @since 4.9.015 (2010-04-19)
  9659. */
  9660. protected function _dooverlinew($x, $y, $w) {
  9661. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9662. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  9663. }
  9664. /**
  9665. * Format a data string for meta information
  9666. * @param $s (string) data string to escape.
  9667. * @param $n (int) object ID
  9668. * @return string escaped string.
  9669. * @protected
  9670. */
  9671. protected function _datastring($s, $n=0) {
  9672. if ($n == 0) {
  9673. $n = $this->n;
  9674. }
  9675. $s = $this->_encrypt_data($n, $s);
  9676. return '('. TCPDF_STATIC::_escape($s).')';
  9677. }
  9678. /**
  9679. * Set the document creation timestamp
  9680. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  9681. * @public
  9682. * @since 5.9.152 (2012-03-23)
  9683. */
  9684. public function setDocCreationTimestamp($time) {
  9685. if (is_string($time)) {
  9686. $time = TCPDF_STATIC::getTimestamp($time);
  9687. }
  9688. $this->doc_creation_timestamp = intval($time);
  9689. }
  9690. /**
  9691. * Set the document modification timestamp
  9692. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  9693. * @public
  9694. * @since 5.9.152 (2012-03-23)
  9695. */
  9696. public function setDocModificationTimestamp($time) {
  9697. if (is_string($time)) {
  9698. $time = TCPDF_STATIC::getTimestamp($time);
  9699. }
  9700. $this->doc_modification_timestamp = intval($time);
  9701. }
  9702. /**
  9703. * Returns document creation timestamp in seconds.
  9704. * @return (int) Creation timestamp in seconds.
  9705. * @public
  9706. * @since 5.9.152 (2012-03-23)
  9707. */
  9708. public function getDocCreationTimestamp() {
  9709. return $this->doc_creation_timestamp;
  9710. }
  9711. /**
  9712. * Returns document modification timestamp in seconds.
  9713. * @return (int) Modfication timestamp in seconds.
  9714. * @public
  9715. * @since 5.9.152 (2012-03-23)
  9716. */
  9717. public function getDocModificationTimestamp() {
  9718. return $this->doc_modification_timestamp;
  9719. }
  9720. /**
  9721. * Returns a formatted date for meta information
  9722. * @param $n (int) Object ID.
  9723. * @param $timestamp (int) Timestamp to convert.
  9724. * @return string escaped date string.
  9725. * @protected
  9726. * @since 4.6.028 (2009-08-25)
  9727. */
  9728. protected function _datestring($n=0, $timestamp=0) {
  9729. if ((empty($timestamp)) OR ($timestamp < 0)) {
  9730. $timestamp = $this->doc_creation_timestamp;
  9731. }
  9732. return $this->_datastring('D:'.TCPDF_STATIC::getFormattedDate($timestamp), $n);
  9733. }
  9734. /**
  9735. * Format a text string for meta information
  9736. * @param $s (string) string to escape.
  9737. * @param $n (int) object ID
  9738. * @return string escaped string.
  9739. * @protected
  9740. */
  9741. protected function _textstring($s, $n=0) {
  9742. if ($this->isunicode) {
  9743. //Convert string to UTF-16BE
  9744. $s = TCPDF_FONTS::UTF8ToUTF16BE($s, true, $this->isunicode, $this->CurrentFont);
  9745. }
  9746. return $this->_datastring($s, $n);
  9747. }
  9748. /**
  9749. * THIS METHOD IS DEPRECATED
  9750. * Format a text string
  9751. * @param $s (string) string to escape.
  9752. * @return string escaped string.
  9753. * @protected
  9754. * @deprecated
  9755. */
  9756. protected function _escapetext($s) {
  9757. if ($this->isunicode) {
  9758. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  9759. $s = TCPDF_FONTS::UTF8ToLatin1($s, $this->isunicode, $this->CurrentFont);
  9760. } else {
  9761. //Convert string to UTF-16BE and reverse RTL language
  9762. $s = TCPDF_FONTS::utf8StrRev($s, false, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  9763. }
  9764. }
  9765. return TCPDF_STATIC::_escape($s);
  9766. }
  9767. /**
  9768. * get raw output stream.
  9769. * @param $s (string) string to output.
  9770. * @param $n (int) object reference for encryption mode
  9771. * @protected
  9772. * @author Nicola Asuni
  9773. * @since 5.5.000 (2010-06-22)
  9774. */
  9775. protected function _getrawstream($s, $n=0) {
  9776. if ($n <= 0) {
  9777. // default to current object
  9778. $n = $this->n;
  9779. }
  9780. return $this->_encrypt_data($n, $s);
  9781. }
  9782. /**
  9783. * Format output stream (DEPRECATED).
  9784. * @param $s (string) string to output.
  9785. * @param $n (int) object reference for encryption mode
  9786. * @protected
  9787. * @deprecated
  9788. */
  9789. protected function _getstream($s, $n=0) {
  9790. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  9791. }
  9792. /**
  9793. * Output a stream (DEPRECATED).
  9794. * @param $s (string) string to output.
  9795. * @param $n (int) object reference for encryption mode
  9796. * @protected
  9797. * @deprecated
  9798. */
  9799. protected function _putstream($s, $n=0) {
  9800. $this->_out($this->_getstream($s, $n));
  9801. }
  9802. /**
  9803. * Output a string to the document.
  9804. * @param $s (string) string to output.
  9805. * @protected
  9806. */
  9807. protected function _out($s) {
  9808. if ($this->state == 2) {
  9809. if ($this->inxobj) {
  9810. // we are inside an XObject template
  9811. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  9812. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  9813. // puts data before page footer
  9814. $pagebuff = $this->getPageBuffer($this->page);
  9815. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  9816. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  9817. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  9818. // update footer position
  9819. $this->footerpos[$this->page] += strlen($s."\n");
  9820. } else {
  9821. // set page data
  9822. $this->setPageBuffer($this->page, $s."\n", true);
  9823. }
  9824. } elseif ($this->state > 0) {
  9825. // set general data
  9826. $this->setBuffer($s."\n");
  9827. }
  9828. }
  9829. /**
  9830. * Set header font.
  9831. * @param $font (array) font
  9832. * @public
  9833. * @since 1.1
  9834. */
  9835. public function setHeaderFont($font) {
  9836. $this->header_font = $font;
  9837. }
  9838. /**
  9839. * Get header font.
  9840. * @return array()
  9841. * @public
  9842. * @since 4.0.012 (2008-07-24)
  9843. */
  9844. public function getHeaderFont() {
  9845. return $this->header_font;
  9846. }
  9847. /**
  9848. * Set footer font.
  9849. * @param $font (array) font
  9850. * @public
  9851. * @since 1.1
  9852. */
  9853. public function setFooterFont($font) {
  9854. $this->footer_font = $font;
  9855. }
  9856. /**
  9857. * Get Footer font.
  9858. * @return array()
  9859. * @public
  9860. * @since 4.0.012 (2008-07-24)
  9861. */
  9862. public function getFooterFont() {
  9863. return $this->footer_font;
  9864. }
  9865. /**
  9866. * Set language array.
  9867. * @param $language (array)
  9868. * @public
  9869. * @since 1.1
  9870. */
  9871. public function setLanguageArray($language) {
  9872. $this->l = $language;
  9873. if (isset($this->l['a_meta_dir'])) {
  9874. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  9875. } else {
  9876. $this->rtl = false;
  9877. }
  9878. }
  9879. /**
  9880. * Returns the PDF data.
  9881. * @public
  9882. */
  9883. public function getPDFData() {
  9884. if ($this->state < 3) {
  9885. $this->Close();
  9886. }
  9887. return $this->buffer;
  9888. }
  9889. /**
  9890. * Output anchor link.
  9891. * @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;)
  9892. * @param $name (string) link name
  9893. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  9894. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  9895. * @param $color (array) array of RGB text color
  9896. * @param $style (string) font style (U, D, B, I)
  9897. * @param $firstblock (boolean) if true the string is the starting of a line.
  9898. * @return the number of cells used or the remaining text if $firstline = true;
  9899. * @public
  9900. */
  9901. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  9902. if (isset($url[1]) AND ($url[0] == '#') AND is_numeric($url[1])) {
  9903. // convert url to internal link
  9904. $lnkdata = explode(',', $url);
  9905. if (isset($lnkdata[0])) {
  9906. $page = intval(substr($lnkdata[0], 1));
  9907. if (empty($page) OR ($page <= 0)) {
  9908. $page = $this->page;
  9909. }
  9910. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  9911. $lnky = floatval($lnkdata[1]);
  9912. } else {
  9913. $lnky = 0;
  9914. }
  9915. $url = $this->AddLink();
  9916. $this->SetLink($url, $lnky, $page);
  9917. }
  9918. }
  9919. // store current settings
  9920. $prevcolor = $this->fgcolor;
  9921. $prevstyle = $this->FontStyle;
  9922. if (empty($color)) {
  9923. $this->SetTextColorArray($this->htmlLinkColorArray);
  9924. } else {
  9925. $this->SetTextColorArray($color);
  9926. }
  9927. if ($style == -1) {
  9928. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  9929. } else {
  9930. $this->SetFont('', $this->FontStyle.$style);
  9931. }
  9932. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  9933. // restore settings
  9934. $this->SetFont('', $prevstyle);
  9935. $this->SetTextColorArray($prevcolor);
  9936. return $ret;
  9937. }
  9938. /**
  9939. * Converts pixels to User's Units.
  9940. * @param $px (int) pixels
  9941. * @return float value in user's unit
  9942. * @public
  9943. * @see setImageScale(), getImageScale()
  9944. */
  9945. public function pixelsToUnits($px) {
  9946. return ($px / ($this->imgscale * $this->k));
  9947. }
  9948. /**
  9949. * Reverse function for htmlentities.
  9950. * Convert entities in UTF-8.
  9951. * @param $text_to_convert (string) Text to convert.
  9952. * @return string converted text string
  9953. * @public
  9954. */
  9955. public function unhtmlentities($text_to_convert) {
  9956. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  9957. }
  9958. // ENCRYPTION METHODS ----------------------------------
  9959. /**
  9960. * Compute encryption key depending on object number where the encrypted data is stored.
  9961. * This is used for all strings and streams without crypt filter specifier.
  9962. * @param $n (int) object number
  9963. * @return int object key
  9964. * @protected
  9965. * @author Nicola Asuni
  9966. * @since 2.0.000 (2008-01-02)
  9967. */
  9968. protected function _objectkey($n) {
  9969. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  9970. if ($this->encryptdata['mode'] == 2) { // AES-128
  9971. // AES padding
  9972. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  9973. }
  9974. $objkey = substr(TCPDF_STATIC::_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  9975. $objkey = substr($objkey, 0, 16);
  9976. return $objkey;
  9977. }
  9978. /**
  9979. * Encrypt the input string.
  9980. * @param $n (int) object number
  9981. * @param $s (string) data string to encrypt
  9982. * @return encrypted string
  9983. * @protected
  9984. * @author Nicola Asuni
  9985. * @since 5.0.005 (2010-05-11)
  9986. */
  9987. protected function _encrypt_data($n, $s) {
  9988. if (!$this->encrypted) {
  9989. return $s;
  9990. }
  9991. switch ($this->encryptdata['mode']) {
  9992. case 0: // RC4-40
  9993. case 1: { // RC4-128
  9994. $s = TCPDF_STATIC::_RC4($this->_objectkey($n), $s, $this->last_enc_key, $this->last_enc_key_c);
  9995. break;
  9996. }
  9997. case 2: { // AES-128
  9998. $s = TCPDF_STATIC::_AES($this->_objectkey($n), $s);
  9999. break;
  10000. }
  10001. case 3: { // AES-256
  10002. $s = TCPDF_STATIC::_AES($this->encryptdata['key'], $s);
  10003. break;
  10004. }
  10005. }
  10006. return $s;
  10007. }
  10008. /**
  10009. * Put encryption on PDF document.
  10010. * @protected
  10011. * @author Nicola Asuni
  10012. * @since 2.0.000 (2008-01-02)
  10013. */
  10014. protected function _putencryption() {
  10015. if (!$this->encrypted) {
  10016. return;
  10017. }
  10018. $this->encryptdata['objid'] = $this->_newobj();
  10019. $out = '<<';
  10020. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  10021. $this->encryptdata['Filter'] = 'Standard';
  10022. }
  10023. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  10024. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  10025. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  10026. }
  10027. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  10028. $this->encryptdata['V'] = 1;
  10029. }
  10030. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  10031. $out .= ' /V '.$this->encryptdata['V'];
  10032. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  10033. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  10034. $out .= ' /Length '.$this->encryptdata['Length'];
  10035. } else {
  10036. $out .= ' /Length 40';
  10037. }
  10038. if ($this->encryptdata['V'] >= 4) {
  10039. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  10040. $this->encryptdata['StmF'] = 'Identity';
  10041. }
  10042. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  10043. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10044. $this->encryptdata['StrF'] = 'Identity';
  10045. }
  10046. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  10047. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  10048. $out .= ' /CF <<';
  10049. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  10050. $out .= ' /Type /CryptFilter';
  10051. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  10052. // The method used
  10053. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  10054. if ($this->encryptdata['pubkey']) {
  10055. $out .= ' /Recipients [';
  10056. foreach ($this->encryptdata['Recipients'] as $rec) {
  10057. $out .= ' <'.$rec.'>';
  10058. }
  10059. $out .= ' ]';
  10060. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  10061. $out .= ' /EncryptMetadata false';
  10062. } else {
  10063. $out .= ' /EncryptMetadata true';
  10064. }
  10065. }
  10066. } else {
  10067. $out .= ' /CFM /None';
  10068. }
  10069. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  10070. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  10071. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  10072. } else {
  10073. $out .= ' /AuthEvent /DocOpen';
  10074. }
  10075. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  10076. // The bit length of the encryption key.
  10077. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  10078. }
  10079. $out .= ' >> >>';
  10080. }
  10081. // The name of the crypt filter that shall be used by default when decrypting streams.
  10082. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  10083. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10084. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  10085. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  10086. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  10087. $out .= ' /EFF /'.$this->encryptdata[''];
  10088. }
  10089. }
  10090. // Additional encryption dictionary entries for the standard security handler
  10091. if ($this->encryptdata['pubkey']) {
  10092. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  10093. $out .= ' /Recipients [';
  10094. foreach ($this->encryptdata['Recipients'] as $rec) {
  10095. $out .= ' <'.$rec.'>';
  10096. }
  10097. $out .= ' ]';
  10098. }
  10099. } else {
  10100. $out .= ' /R';
  10101. if ($this->encryptdata['V'] == 5) { // AES-256
  10102. $out .= ' 5';
  10103. $out .= ' /OE ('.TCPDF_STATIC::_escape($this->encryptdata['OE']).')';
  10104. $out .= ' /UE ('.TCPDF_STATIC::_escape($this->encryptdata['UE']).')';
  10105. $out .= ' /Perms ('.TCPDF_STATIC::_escape($this->encryptdata['perms']).')';
  10106. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  10107. $out .= ' 4';
  10108. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  10109. $out .= ' 2';
  10110. } else { // RC-128
  10111. $out .= ' 3';
  10112. }
  10113. $out .= ' /O ('.TCPDF_STATIC::_escape($this->encryptdata['O']).')';
  10114. $out .= ' /U ('.TCPDF_STATIC::_escape($this->encryptdata['U']).')';
  10115. $out .= ' /P '.$this->encryptdata['P'];
  10116. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  10117. $out .= ' /EncryptMetadata false';
  10118. } else {
  10119. $out .= ' /EncryptMetadata true';
  10120. }
  10121. }
  10122. $out .= ' >>';
  10123. $out .= "\n".'endobj';
  10124. $this->_out($out);
  10125. }
  10126. /**
  10127. * Compute U value (used for encryption)
  10128. * @return string U value
  10129. * @protected
  10130. * @since 2.0.000 (2008-01-02)
  10131. * @author Nicola Asuni
  10132. */
  10133. protected function _Uvalue() {
  10134. if ($this->encryptdata['mode'] == 0) { // RC4-40
  10135. return TCPDF_STATIC::_RC4($this->encryptdata['key'], TCPDF_STATIC::$enc_padding, $this->last_enc_key, $this->last_enc_key_c);
  10136. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  10137. $tmp = TCPDF_STATIC::_md5_16(TCPDF_STATIC::$enc_padding.$this->encryptdata['fileid']);
  10138. $enc = TCPDF_STATIC::_RC4($this->encryptdata['key'], $tmp, $this->last_enc_key, $this->last_enc_key_c);
  10139. $len = strlen($tmp);
  10140. for ($i = 1; $i <= 19; ++$i) {
  10141. $ek = '';
  10142. for ($j = 0; $j < $len; ++$j) {
  10143. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  10144. }
  10145. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10146. }
  10147. $enc .= str_repeat("\x00", 16);
  10148. return substr($enc, 0, 32);
  10149. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10150. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10151. // User Validation Salt
  10152. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  10153. // User Key Salt
  10154. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  10155. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  10156. }
  10157. }
  10158. /**
  10159. * Compute UE value (used for encryption)
  10160. * @return string UE value
  10161. * @protected
  10162. * @since 5.9.006 (2010-10-19)
  10163. * @author Nicola Asuni
  10164. */
  10165. protected function _UEvalue() {
  10166. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  10167. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  10168. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  10169. }
  10170. /**
  10171. * Compute O value (used for encryption)
  10172. * @return string O value
  10173. * @protected
  10174. * @since 2.0.000 (2008-01-02)
  10175. * @author Nicola Asuni
  10176. */
  10177. protected function _Ovalue() {
  10178. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  10179. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['owner_password']);
  10180. if ($this->encryptdata['mode'] > 0) {
  10181. for ($i = 0; $i < 50; ++$i) {
  10182. $tmp = TCPDF_STATIC::_md5_16($tmp);
  10183. }
  10184. }
  10185. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  10186. $enc = TCPDF_STATIC::_RC4($owner_key, $this->encryptdata['user_password'], $this->last_enc_key, $this->last_enc_key_c);
  10187. if ($this->encryptdata['mode'] > 0) {
  10188. $len = strlen($owner_key);
  10189. for ($i = 1; $i <= 19; ++$i) {
  10190. $ek = '';
  10191. for ($j = 0; $j < $len; ++$j) {
  10192. $ek .= chr(ord($owner_key[$j]) ^ $i);
  10193. }
  10194. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10195. }
  10196. }
  10197. return $enc;
  10198. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10199. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10200. // Owner Validation Salt
  10201. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  10202. // Owner Key Salt
  10203. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  10204. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  10205. }
  10206. }
  10207. /**
  10208. * Compute OE value (used for encryption)
  10209. * @return string OE value
  10210. * @protected
  10211. * @since 5.9.006 (2010-10-19)
  10212. * @author Nicola Asuni
  10213. */
  10214. protected function _OEvalue() {
  10215. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  10216. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  10217. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  10218. }
  10219. /**
  10220. * Convert password for AES-256 encryption mode
  10221. * @param $password (string) password
  10222. * @return string password
  10223. * @protected
  10224. * @since 5.9.006 (2010-10-19)
  10225. * @author Nicola Asuni
  10226. */
  10227. protected function _fixAES256Password($password) {
  10228. $psw = ''; // password to be returned
  10229. $psw_array = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($password, $this->isunicode, $this->CurrentFont), $password, $this->rtl, $this->isunicode, $this->CurrentFont);
  10230. foreach ($psw_array as $c) {
  10231. $psw .= TCPDF_FONTS::unichr($c, $this->isunicode);
  10232. }
  10233. return substr($psw, 0, 127);
  10234. }
  10235. /**
  10236. * Compute encryption key
  10237. * @protected
  10238. * @since 2.0.000 (2008-01-02)
  10239. * @author Nicola Asuni
  10240. */
  10241. protected function _generateencryptionkey() {
  10242. $keybytelen = ($this->encryptdata['Length'] / 8);
  10243. if (!$this->encryptdata['pubkey']) { // standard mode
  10244. if ($this->encryptdata['mode'] == 3) { // AES-256
  10245. // generate 256 bit random key
  10246. $this->encryptdata['key'] = substr(hash('sha256', TCPDF_STATIC::getRandomSeed(), true), 0, $keybytelen);
  10247. // truncate passwords
  10248. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  10249. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  10250. // Compute U value
  10251. $this->encryptdata['U'] = $this->_Uvalue();
  10252. // Compute UE value
  10253. $this->encryptdata['UE'] = $this->_UEvalue();
  10254. // Compute O value
  10255. $this->encryptdata['O'] = $this->_Ovalue();
  10256. // Compute OE value
  10257. $this->encryptdata['OE'] = $this->_OEvalue();
  10258. // Compute P value
  10259. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10260. // Computing the encryption dictionary's Perms (permissions) value
  10261. $perms = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  10262. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  10263. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  10264. $perms .= 'F';
  10265. } else {
  10266. $perms .= 'T';
  10267. }
  10268. $perms .= 'adb'; // bytes 9-11
  10269. $perms .= 'nick'; // bytes 12-15
  10270. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  10271. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  10272. } else { // RC4-40, RC4-128, AES-128
  10273. // Pad passwords
  10274. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10275. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10276. // Compute O value
  10277. $this->encryptdata['O'] = $this->_Ovalue();
  10278. // get default permissions (reverse byte order)
  10279. $permissions = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']);
  10280. // Compute encryption key
  10281. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  10282. if ($this->encryptdata['mode'] > 0) {
  10283. for ($i = 0; $i < 50; ++$i) {
  10284. $tmp = TCPDF_STATIC::_md5_16(substr($tmp, 0, $keybytelen));
  10285. }
  10286. }
  10287. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  10288. // Compute U value
  10289. $this->encryptdata['U'] = $this->_Uvalue();
  10290. // Compute P value
  10291. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10292. }
  10293. } else { // Public-Key mode
  10294. // random 20-byte seed
  10295. $seed = sha1(TCPDF_STATIC::getRandomSeed(), true);
  10296. $recipient_bytes = '';
  10297. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  10298. // for each public certificate
  10299. if (isset($pubkey['p'])) {
  10300. $pkprotection = TCPDF_STATIC::getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  10301. } else {
  10302. $pkprotection = $this->encryptdata['protection'];
  10303. }
  10304. // get default permissions (reverse byte order)
  10305. $pkpermissions = TCPDF_STATIC::getEncPermissionsString($pkprotection);
  10306. // envelope data
  10307. $envelope = $seed.$pkpermissions;
  10308. // write the envelope data to a temporary file
  10309. $tempkeyfile = TCPDF_STATIC::getObjFilename('tmpkey');
  10310. $f = fopen($tempkeyfile, 'wb');
  10311. if (!$f) {
  10312. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  10313. }
  10314. $envelope_length = strlen($envelope);
  10315. fwrite($f, $envelope, $envelope_length);
  10316. fclose($f);
  10317. $tempencfile = TCPDF_STATIC::getObjFilename('tmpenc');
  10318. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  10319. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  10320. }
  10321. unlink($tempkeyfile);
  10322. // read encryption signature
  10323. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  10324. unlink($tempencfile);
  10325. // extract signature
  10326. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  10327. $tmparr = explode("\n\n", $signature);
  10328. $signature = trim($tmparr[1]);
  10329. unset($tmparr);
  10330. // decode signature
  10331. $signature = base64_decode($signature);
  10332. // convert signature to hex
  10333. $hexsignature = current(unpack('H*', $signature));
  10334. // store signature on recipients array
  10335. $this->encryptdata['Recipients'][] = $hexsignature;
  10336. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  10337. $recipient_bytes .= $signature;
  10338. }
  10339. // calculate encryption key
  10340. if ($this->encryptdata['mode'] == 3) { // AES-256
  10341. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  10342. } else { // RC4-40, RC4-128, AES-128
  10343. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  10344. }
  10345. }
  10346. }
  10347. /**
  10348. * Set document protection
  10349. * Remark: the protection against modification is for people who have the full Acrobat product.
  10350. * 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.
  10351. * 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.
  10352. * @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>
  10353. * @param $user_pass (String) user password. Empty by default.
  10354. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  10355. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  10356. * @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')))
  10357. * @public
  10358. * @since 2.0.000 (2008-01-02)
  10359. * @author Nicola Asuni
  10360. */
  10361. 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) {
  10362. if ($this->pdfa_mode) {
  10363. // encryption is not allowed in PDF/A mode
  10364. return;
  10365. }
  10366. $this->encryptdata['protection'] = TCPDF_STATIC::getUserPermissionCode($permissions, $mode);
  10367. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  10368. // public-key mode
  10369. $this->encryptdata['pubkeys'] = $pubkeys;
  10370. if ($mode == 0) {
  10371. // public-Key Security requires at least 128 bit
  10372. $mode = 1;
  10373. }
  10374. if (!function_exists('openssl_pkcs7_encrypt')) {
  10375. $this->Error('Public-Key Security requires openssl library.');
  10376. }
  10377. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  10378. $this->encryptdata['pubkey'] = true;
  10379. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  10380. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  10381. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  10382. } else {
  10383. // standard mode (password mode)
  10384. $this->encryptdata['pubkey'] = false;
  10385. $this->encryptdata['Filter'] = 'Standard';
  10386. $this->encryptdata['StmF'] = 'StdCF';
  10387. $this->encryptdata['StrF'] = 'StdCF';
  10388. }
  10389. if ($mode > 1) { // AES
  10390. if (!extension_loaded('mcrypt')) {
  10391. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  10392. }
  10393. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  10394. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  10395. }
  10396. if (($mode == 3) AND !function_exists('hash')) {
  10397. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  10398. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  10399. }
  10400. }
  10401. if ($owner_pass === null) {
  10402. $owner_pass = md5(TCPDF_STATIC::getRandomSeed());
  10403. }
  10404. $this->encryptdata['user_password'] = $user_pass;
  10405. $this->encryptdata['owner_password'] = $owner_pass;
  10406. $this->encryptdata['mode'] = $mode;
  10407. switch ($mode) {
  10408. case 0: { // RC4 40 bit
  10409. $this->encryptdata['V'] = 1;
  10410. $this->encryptdata['Length'] = 40;
  10411. $this->encryptdata['CF']['CFM'] = 'V2';
  10412. break;
  10413. }
  10414. case 1: { // RC4 128 bit
  10415. $this->encryptdata['V'] = 2;
  10416. $this->encryptdata['Length'] = 128;
  10417. $this->encryptdata['CF']['CFM'] = 'V2';
  10418. if ($this->encryptdata['pubkey']) {
  10419. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  10420. $this->encryptdata['Recipients'] = array();
  10421. }
  10422. break;
  10423. }
  10424. case 2: { // AES 128 bit
  10425. $this->encryptdata['V'] = 4;
  10426. $this->encryptdata['Length'] = 128;
  10427. $this->encryptdata['CF']['CFM'] = 'AESV2';
  10428. $this->encryptdata['CF']['Length'] = 128;
  10429. if ($this->encryptdata['pubkey']) {
  10430. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10431. $this->encryptdata['Recipients'] = array();
  10432. }
  10433. break;
  10434. }
  10435. case 3: { // AES 256 bit
  10436. $this->encryptdata['V'] = 5;
  10437. $this->encryptdata['Length'] = 256;
  10438. $this->encryptdata['CF']['CFM'] = 'AESV3';
  10439. $this->encryptdata['CF']['Length'] = 256;
  10440. if ($this->encryptdata['pubkey']) {
  10441. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10442. $this->encryptdata['Recipients'] = array();
  10443. }
  10444. break;
  10445. }
  10446. }
  10447. $this->encrypted = true;
  10448. $this->encryptdata['fileid'] = TCPDF_STATIC::convertHexStringToString($this->file_id);
  10449. $this->_generateencryptionkey();
  10450. }
  10451. // END OF ENCRYPTION FUNCTIONS -------------------------
  10452. // START TRANSFORMATIONS SECTION -----------------------
  10453. /**
  10454. * Starts a 2D tranformation saving current graphic state.
  10455. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  10456. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10457. * @public
  10458. * @since 2.1.000 (2008-01-07)
  10459. * @see StartTransform(), StopTransform()
  10460. */
  10461. public function StartTransform() {
  10462. if ($this->state != 2) {
  10463. return;
  10464. }
  10465. $this->_out('q');
  10466. if ($this->inxobj) {
  10467. // we are inside an XObject template
  10468. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10469. } else {
  10470. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  10471. }
  10472. ++$this->transfmatrix_key;
  10473. $this->transfmatrix[$this->transfmatrix_key] = array();
  10474. }
  10475. /**
  10476. * Stops a 2D tranformation restoring previous graphic state.
  10477. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  10478. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10479. * @public
  10480. * @since 2.1.000 (2008-01-07)
  10481. * @see StartTransform(), StopTransform()
  10482. */
  10483. public function StopTransform() {
  10484. if ($this->state != 2) {
  10485. return;
  10486. }
  10487. $this->_out('Q');
  10488. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  10489. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  10490. --$this->transfmatrix_key;
  10491. }
  10492. if ($this->inxobj) {
  10493. // we are inside an XObject template
  10494. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  10495. } else {
  10496. array_pop($this->transfmrk[$this->page]);
  10497. }
  10498. }
  10499. /**
  10500. * Horizontal Scaling.
  10501. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10502. * @param $x (int) abscissa of the scaling center. Default is current x position
  10503. * @param $y (int) ordinate of the scaling center. Default is current y position
  10504. * @public
  10505. * @since 2.1.000 (2008-01-07)
  10506. * @see StartTransform(), StopTransform()
  10507. */
  10508. public function ScaleX($s_x, $x='', $y='') {
  10509. $this->Scale($s_x, 100, $x, $y);
  10510. }
  10511. /**
  10512. * Vertical Scaling.
  10513. * @param $s_y (float) scaling factor for height 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 ScaleY($s_y, $x='', $y='') {
  10521. $this->Scale(100, $s_y, $x, $y);
  10522. }
  10523. /**
  10524. * Vertical and horizontal proportional Scaling.
  10525. * @param $s (float) scaling factor for width and 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 ScaleXY($s, $x='', $y='') {
  10533. $this->Scale($s, $s, $x, $y);
  10534. }
  10535. /**
  10536. * Vertical and horizontal non-proportional Scaling.
  10537. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10538. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  10539. * @param $x (int) abscissa of the scaling center. Default is current x position
  10540. * @param $y (int) ordinate of the scaling center. Default is current y position
  10541. * @public
  10542. * @since 2.1.000 (2008-01-07)
  10543. * @see StartTransform(), StopTransform()
  10544. */
  10545. public function Scale($s_x, $s_y, $x='', $y='') {
  10546. if ($x === '') {
  10547. $x = $this->x;
  10548. }
  10549. if ($y === '') {
  10550. $y = $this->y;
  10551. }
  10552. if (($s_x == 0) OR ($s_y == 0)) {
  10553. $this->Error('Please do not use values equal to zero for scaling');
  10554. }
  10555. $y = ($this->h - $y) * $this->k;
  10556. $x *= $this->k;
  10557. //calculate elements of transformation matrix
  10558. $s_x /= 100;
  10559. $s_y /= 100;
  10560. $tm = array();
  10561. $tm[0] = $s_x;
  10562. $tm[1] = 0;
  10563. $tm[2] = 0;
  10564. $tm[3] = $s_y;
  10565. $tm[4] = $x * (1 - $s_x);
  10566. $tm[5] = $y * (1 - $s_y);
  10567. //scale the coordinate system
  10568. $this->Transform($tm);
  10569. }
  10570. /**
  10571. * Horizontal Mirroring.
  10572. * @param $x (int) abscissa of the point. Default is current x position
  10573. * @public
  10574. * @since 2.1.000 (2008-01-07)
  10575. * @see StartTransform(), StopTransform()
  10576. */
  10577. public function MirrorH($x='') {
  10578. $this->Scale(-100, 100, $x);
  10579. }
  10580. /**
  10581. * Verical Mirroring.
  10582. * @param $y (int) ordinate of the point. Default is current y position
  10583. * @public
  10584. * @since 2.1.000 (2008-01-07)
  10585. * @see StartTransform(), StopTransform()
  10586. */
  10587. public function MirrorV($y='') {
  10588. $this->Scale(100, -100, '', $y);
  10589. }
  10590. /**
  10591. * Point reflection mirroring.
  10592. * @param $x (int) abscissa of the point. Default is current x position
  10593. * @param $y (int) ordinate of the point. Default is current y position
  10594. * @public
  10595. * @since 2.1.000 (2008-01-07)
  10596. * @see StartTransform(), StopTransform()
  10597. */
  10598. public function MirrorP($x='',$y='') {
  10599. $this->Scale(-100, -100, $x, $y);
  10600. }
  10601. /**
  10602. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  10603. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  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 MirrorL($angle=0, $x='',$y='') {
  10611. $this->Scale(-100, 100, $x, $y);
  10612. $this->Rotate(-2*($angle-90), $x, $y);
  10613. }
  10614. /**
  10615. * Translate graphic object horizontally.
  10616. * @param $t_x (int) movement to the right (or left for RTL)
  10617. * @public
  10618. * @since 2.1.000 (2008-01-07)
  10619. * @see StartTransform(), StopTransform()
  10620. */
  10621. public function TranslateX($t_x) {
  10622. $this->Translate($t_x, 0);
  10623. }
  10624. /**
  10625. * Translate graphic object vertically.
  10626. * @param $t_y (int) movement to the bottom
  10627. * @public
  10628. * @since 2.1.000 (2008-01-07)
  10629. * @see StartTransform(), StopTransform()
  10630. */
  10631. public function TranslateY($t_y) {
  10632. $this->Translate(0, $t_y);
  10633. }
  10634. /**
  10635. * Translate graphic object horizontally and vertically.
  10636. * @param $t_x (int) movement to the right
  10637. * @param $t_y (int) movement to the bottom
  10638. * @public
  10639. * @since 2.1.000 (2008-01-07)
  10640. * @see StartTransform(), StopTransform()
  10641. */
  10642. public function Translate($t_x, $t_y) {
  10643. //calculate elements of transformation matrix
  10644. $tm = array();
  10645. $tm[0] = 1;
  10646. $tm[1] = 0;
  10647. $tm[2] = 0;
  10648. $tm[3] = 1;
  10649. $tm[4] = $t_x * $this->k;
  10650. $tm[5] = -$t_y * $this->k;
  10651. //translate the coordinate system
  10652. $this->Transform($tm);
  10653. }
  10654. /**
  10655. * Rotate object.
  10656. * @param $angle (float) angle in degrees for counter-clockwise rotation
  10657. * @param $x (int) abscissa of the rotation center. Default is current x position
  10658. * @param $y (int) ordinate of the rotation center. Default is current y position
  10659. * @public
  10660. * @since 2.1.000 (2008-01-07)
  10661. * @see StartTransform(), StopTransform()
  10662. */
  10663. public function Rotate($angle, $x='', $y='') {
  10664. if ($x === '') {
  10665. $x = $this->x;
  10666. }
  10667. if ($y === '') {
  10668. $y = $this->y;
  10669. }
  10670. $y = ($this->h - $y) * $this->k;
  10671. $x *= $this->k;
  10672. //calculate elements of transformation matrix
  10673. $tm = array();
  10674. $tm[0] = cos(deg2rad($angle));
  10675. $tm[1] = sin(deg2rad($angle));
  10676. $tm[2] = -$tm[1];
  10677. $tm[3] = $tm[0];
  10678. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  10679. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  10680. //rotate the coordinate system around ($x,$y)
  10681. $this->Transform($tm);
  10682. }
  10683. /**
  10684. * Skew horizontally.
  10685. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10686. * @param $x (int) abscissa of the skewing center. default is current x position
  10687. * @param $y (int) ordinate of the skewing center. default is current y position
  10688. * @public
  10689. * @since 2.1.000 (2008-01-07)
  10690. * @see StartTransform(), StopTransform()
  10691. */
  10692. public function SkewX($angle_x, $x='', $y='') {
  10693. $this->Skew($angle_x, 0, $x, $y);
  10694. }
  10695. /**
  10696. * Skew vertically.
  10697. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  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 SkewY($angle_y, $x='', $y='') {
  10705. $this->Skew(0, $angle_y, $x, $y);
  10706. }
  10707. /**
  10708. * Skew.
  10709. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10710. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10711. * @param $x (int) abscissa of the skewing center. default is current x position
  10712. * @param $y (int) ordinate of the skewing center. default is current y position
  10713. * @public
  10714. * @since 2.1.000 (2008-01-07)
  10715. * @see StartTransform(), StopTransform()
  10716. */
  10717. public function Skew($angle_x, $angle_y, $x='', $y='') {
  10718. if ($x === '') {
  10719. $x = $this->x;
  10720. }
  10721. if ($y === '') {
  10722. $y = $this->y;
  10723. }
  10724. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  10725. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  10726. }
  10727. $x *= $this->k;
  10728. $y = ($this->h - $y) * $this->k;
  10729. //calculate elements of transformation matrix
  10730. $tm = array();
  10731. $tm[0] = 1;
  10732. $tm[1] = tan(deg2rad($angle_y));
  10733. $tm[2] = tan(deg2rad($angle_x));
  10734. $tm[3] = 1;
  10735. $tm[4] = -$tm[2] * $y;
  10736. $tm[5] = -$tm[1] * $x;
  10737. //skew the coordinate system
  10738. $this->Transform($tm);
  10739. }
  10740. /**
  10741. * Apply graphic transformations.
  10742. * @param $tm (array) transformation matrix
  10743. * @protected
  10744. * @since 2.1.000 (2008-01-07)
  10745. * @see StartTransform(), StopTransform()
  10746. */
  10747. protected function Transform($tm) {
  10748. if ($this->state != 2) {
  10749. return;
  10750. }
  10751. $this->_out(sprintf('%F %F %F %F %F %F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  10752. // add tranformation matrix
  10753. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  10754. // update transformation mark
  10755. if ($this->inxobj) {
  10756. // we are inside an XObject template
  10757. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  10758. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  10759. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10760. }
  10761. } elseif (end($this->transfmrk[$this->page]) !== false) {
  10762. $key = key($this->transfmrk[$this->page]);
  10763. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  10764. }
  10765. }
  10766. // END TRANSFORMATIONS SECTION -------------------------
  10767. // START GRAPHIC FUNCTIONS SECTION ---------------------
  10768. // The following section is based on the code provided by David Hernandez Sanz
  10769. /**
  10770. * 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.
  10771. * @param $width (float) The width.
  10772. * @public
  10773. * @since 1.0
  10774. * @see Line(), Rect(), Cell(), MultiCell()
  10775. */
  10776. public function SetLineWidth($width) {
  10777. //Set line width
  10778. $this->LineWidth = $width;
  10779. $this->linestyleWidth = sprintf('%F w', ($width * $this->k));
  10780. if ($this->state == 2) {
  10781. $this->_out($this->linestyleWidth);
  10782. }
  10783. }
  10784. /**
  10785. * Returns the current the line width.
  10786. * @return int Line width
  10787. * @public
  10788. * @since 2.1.000 (2008-01-07)
  10789. * @see Line(), SetLineWidth()
  10790. */
  10791. public function GetLineWidth() {
  10792. return $this->LineWidth;
  10793. }
  10794. /**
  10795. * Set line style.
  10796. * @param $style (array) Line style. Array with keys among the following:
  10797. * <ul>
  10798. * <li>width (float): Width of the line in user units.</li>
  10799. * <li>cap (string): Type of cap to put on the line. Possible values are:
  10800. * butt, round, square. The difference between "square" and "butt" is that
  10801. * "square" projects a flat end past the end of the line.</li>
  10802. * <li>join (string): Type of join. Possible values are: miter, round,
  10803. * bevel.</li>
  10804. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  10805. * series of length values, which are the lengths of the on and off dashes.
  10806. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  10807. * 1 off, 2 on, 1 off, ...</li>
  10808. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  10809. * the point at which the pattern starts.</li>
  10810. * <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>
  10811. * </ul>
  10812. * @param $ret (boolean) if true do not send the command.
  10813. * @return string the PDF command
  10814. * @public
  10815. * @since 2.1.000 (2008-01-08)
  10816. */
  10817. public function SetLineStyle($style, $ret=false) {
  10818. $s = ''; // string to be returned
  10819. if (!is_array($style)) {
  10820. return;
  10821. }
  10822. if (isset($style['width'])) {
  10823. $this->LineWidth = $style['width'];
  10824. $this->linestyleWidth = sprintf('%F w', ($style['width'] * $this->k));
  10825. $s .= $this->linestyleWidth.' ';
  10826. }
  10827. if (isset($style['cap'])) {
  10828. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  10829. if (isset($ca[$style['cap']])) {
  10830. $this->linestyleCap = $ca[$style['cap']].' J';
  10831. $s .= $this->linestyleCap.' ';
  10832. }
  10833. }
  10834. if (isset($style['join'])) {
  10835. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  10836. if (isset($ja[$style['join']])) {
  10837. $this->linestyleJoin = $ja[$style['join']].' j';
  10838. $s .= $this->linestyleJoin.' ';
  10839. }
  10840. }
  10841. if (isset($style['dash'])) {
  10842. $dash_string = '';
  10843. if ($style['dash']) {
  10844. if (preg_match('/^.+,/', $style['dash']) > 0) {
  10845. $tab = explode(',', $style['dash']);
  10846. } else {
  10847. $tab = array($style['dash']);
  10848. }
  10849. $dash_string = '';
  10850. foreach ($tab as $i => $v) {
  10851. if ($i) {
  10852. $dash_string .= ' ';
  10853. }
  10854. $dash_string .= sprintf('%F', $v);
  10855. }
  10856. }
  10857. if (!isset($style['phase']) OR !$style['dash']) {
  10858. $style['phase'] = 0;
  10859. }
  10860. $this->linestyleDash = sprintf('[%s] %F d', $dash_string, $style['phase']);
  10861. $s .= $this->linestyleDash.' ';
  10862. }
  10863. if (isset($style['color'])) {
  10864. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  10865. }
  10866. if (!$ret AND ($this->state == 2)) {
  10867. $this->_out($s);
  10868. }
  10869. return $s;
  10870. }
  10871. /**
  10872. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  10873. * @param $x (float) Abscissa of point.
  10874. * @param $y (float) Ordinate of point.
  10875. * @protected
  10876. * @since 2.1.000 (2008-01-08)
  10877. */
  10878. protected function _outPoint($x, $y) {
  10879. if ($this->state == 2) {
  10880. $this->_out(sprintf('%F %F m', ($x * $this->k), (($this->h - $y) * $this->k)));
  10881. }
  10882. }
  10883. /**
  10884. * Append a straight line segment from the current point to the point (x, y).
  10885. * The new current point shall be (x, y).
  10886. * @param $x (float) Abscissa of end point.
  10887. * @param $y (float) Ordinate of end point.
  10888. * @protected
  10889. * @since 2.1.000 (2008-01-08)
  10890. */
  10891. protected function _outLine($x, $y) {
  10892. if ($this->state == 2) {
  10893. $this->_out(sprintf('%F %F l', ($x * $this->k), (($this->h - $y) * $this->k)));
  10894. }
  10895. }
  10896. /**
  10897. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  10898. * @param $x (float) Abscissa of upper-left corner.
  10899. * @param $y (float) Ordinate of upper-left corner.
  10900. * @param $w (float) Width.
  10901. * @param $h (float) Height.
  10902. * @param $op (string) options
  10903. * @protected
  10904. * @since 2.1.000 (2008-01-08)
  10905. */
  10906. protected function _outRect($x, $y, $w, $h, $op) {
  10907. if ($this->state == 2) {
  10908. $this->_out(sprintf('%F %F %F %F re %s', ($x * $this->k), (($this->h - $y) * $this->k), ($w * $this->k), (-$h * $this->k), $op));
  10909. }
  10910. }
  10911. /**
  10912. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bézier control points.
  10913. * The new current point shall be (x3, y3).
  10914. * @param $x1 (float) Abscissa of control point 1.
  10915. * @param $y1 (float) Ordinate of control point 1.
  10916. * @param $x2 (float) Abscissa of control point 2.
  10917. * @param $y2 (float) Ordinate of control point 2.
  10918. * @param $x3 (float) Abscissa of end point.
  10919. * @param $y3 (float) Ordinate of end point.
  10920. * @protected
  10921. * @since 2.1.000 (2008-01-08)
  10922. */
  10923. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  10924. if ($this->state == 2) {
  10925. $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)));
  10926. }
  10927. }
  10928. /**
  10929. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the Bézier control points.
  10930. * The new current point shall be (x3, y3).
  10931. * @param $x2 (float) Abscissa of control point 2.
  10932. * @param $y2 (float) Ordinate of control point 2.
  10933. * @param $x3 (float) Abscissa of end point.
  10934. * @param $y3 (float) Ordinate of end point.
  10935. * @protected
  10936. * @since 4.9.019 (2010-04-26)
  10937. */
  10938. protected function _outCurveV($x2, $y2, $x3, $y3) {
  10939. if ($this->state == 2) {
  10940. $this->_out(sprintf('%F %F %F %F v', ($x2 * $this->k), (($this->h - $y2) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10941. }
  10942. }
  10943. /**
  10944. * Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the Bézier control points.
  10945. * The new current point shall be (x3, y3).
  10946. * @param $x1 (float) Abscissa of control point 1.
  10947. * @param $y1 (float) Ordinate of control point 1.
  10948. * @param $x3 (float) Abscissa of end point.
  10949. * @param $y3 (float) Ordinate of end point.
  10950. * @protected
  10951. * @since 2.1.000 (2008-01-08)
  10952. */
  10953. protected function _outCurveY($x1, $y1, $x3, $y3) {
  10954. if ($this->state == 2) {
  10955. $this->_out(sprintf('%F %F %F %F y', ($x1 * $this->k), (($this->h - $y1) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10956. }
  10957. }
  10958. /**
  10959. * Draws a line between two points.
  10960. * @param $x1 (float) Abscissa of first point.
  10961. * @param $y1 (float) Ordinate of first point.
  10962. * @param $x2 (float) Abscissa of second point.
  10963. * @param $y2 (float) Ordinate of second point.
  10964. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  10965. * @public
  10966. * @since 1.0
  10967. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  10968. */
  10969. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  10970. if ($this->state != 2) {
  10971. return;
  10972. }
  10973. if (is_array($style)) {
  10974. $this->SetLineStyle($style);
  10975. }
  10976. $this->_outPoint($x1, $y1);
  10977. $this->_outLine($x2, $y2);
  10978. $this->_out('S');
  10979. }
  10980. /**
  10981. * Draws a rectangle.
  10982. * @param $x (float) Abscissa of upper-left corner.
  10983. * @param $y (float) Ordinate of upper-left corner.
  10984. * @param $w (float) Width.
  10985. * @param $h (float) Height.
  10986. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  10987. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  10988. * <ul>
  10989. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  10990. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  10991. * </ul>
  10992. * If a key is not present or is null, the correspondent border is not drawn. Default value: default line style (empty array).
  10993. * @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).
  10994. * @public
  10995. * @since 1.0
  10996. * @see SetLineStyle()
  10997. */
  10998. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  10999. if ($this->state != 2) {
  11000. return;
  11001. }
  11002. if (empty($style)) {
  11003. $style = 'S';
  11004. }
  11005. if (!(strpos($style, 'F') === false) AND !empty($fill_color)) {
  11006. // set background color
  11007. $this->SetFillColorArray($fill_color);
  11008. }
  11009. if (!empty($border_style)) {
  11010. if (isset($border_style['all']) AND !empty($border_style['all'])) {
  11011. //set global style for border
  11012. $this->SetLineStyle($border_style['all']);
  11013. $border_style = array();
  11014. } else {
  11015. // remove stroke operator from style
  11016. $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*' );
  11017. if (isset($opnostroke[$style])) {
  11018. $style = $opnostroke[$style];
  11019. }
  11020. }
  11021. }
  11022. if (!empty($style)) {
  11023. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11024. $this->_outRect($x, $y, $w, $h, $op);
  11025. }
  11026. if (!empty($border_style)) {
  11027. $border_style2 = array();
  11028. foreach ($border_style as $line => $value) {
  11029. $length = strlen($line);
  11030. for ($i = 0; $i < $length; ++$i) {
  11031. $border_style2[$line[$i]] = $value;
  11032. }
  11033. }
  11034. $border_style = $border_style2;
  11035. if (isset($border_style['L']) AND $border_style['L']) {
  11036. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  11037. }
  11038. if (isset($border_style['T']) AND $border_style['T']) {
  11039. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  11040. }
  11041. if (isset($border_style['R']) AND $border_style['R']) {
  11042. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  11043. }
  11044. if (isset($border_style['B']) AND $border_style['B']) {
  11045. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  11046. }
  11047. }
  11048. }
  11049. /**
  11050. * Draws a Bezier curve.
  11051. * The Bezier curve is a tangent to the line between the control points at
  11052. * either end of the curve.
  11053. * @param $x0 (float) Abscissa of start point.
  11054. * @param $y0 (float) Ordinate of start point.
  11055. * @param $x1 (float) Abscissa of control point 1.
  11056. * @param $y1 (float) Ordinate of control point 1.
  11057. * @param $x2 (float) Abscissa of control point 2.
  11058. * @param $y2 (float) Ordinate of control point 2.
  11059. * @param $x3 (float) Abscissa of end point.
  11060. * @param $y3 (float) Ordinate of end point.
  11061. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11062. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11063. * @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).
  11064. * @public
  11065. * @see SetLineStyle()
  11066. * @since 2.1.000 (2008-01-08)
  11067. */
  11068. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  11069. if ($this->state != 2) {
  11070. return;
  11071. }
  11072. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11073. $this->SetFillColorArray($fill_color);
  11074. }
  11075. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11076. if ($line_style) {
  11077. $this->SetLineStyle($line_style);
  11078. }
  11079. $this->_outPoint($x0, $y0);
  11080. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11081. $this->_out($op);
  11082. }
  11083. /**
  11084. * Draws a poly-Bezier curve.
  11085. * Each Bezier curve segment is a tangent to the line between the control points at
  11086. * either end of the curve.
  11087. * @param $x0 (float) Abscissa of start point.
  11088. * @param $y0 (float) Ordinate of start point.
  11089. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  11090. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11091. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11092. * @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).
  11093. * @public
  11094. * @see SetLineStyle()
  11095. * @since 3.0008 (2008-05-12)
  11096. */
  11097. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  11098. if ($this->state != 2) {
  11099. return;
  11100. }
  11101. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11102. $this->SetFillColorArray($fill_color);
  11103. }
  11104. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11105. if ($op == 'f') {
  11106. $line_style = array();
  11107. }
  11108. if ($line_style) {
  11109. $this->SetLineStyle($line_style);
  11110. }
  11111. $this->_outPoint($x0, $y0);
  11112. foreach ($segments as $segment) {
  11113. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  11114. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11115. }
  11116. $this->_out($op);
  11117. }
  11118. /**
  11119. * Draws an ellipse.
  11120. * An ellipse is formed from n Bezier curves.
  11121. * @param $x0 (float) Abscissa of center point.
  11122. * @param $y0 (float) Ordinate of center point.
  11123. * @param $rx (float) Horizontal radius.
  11124. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11125. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11126. * @param $astart: (float) Angle start of draw line. Default value: 0.
  11127. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  11128. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11129. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  11130. * @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).
  11131. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11132. * @author Nicola Asuni
  11133. * @public
  11134. * @since 2.1.000 (2008-01-08)
  11135. */
  11136. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11137. if ($this->state != 2) {
  11138. return;
  11139. }
  11140. if (TCPDF_STATIC::empty_string($ry) OR ($ry == 0)) {
  11141. $ry = $rx;
  11142. }
  11143. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11144. $this->SetFillColorArray($fill_color);
  11145. }
  11146. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11147. if ($op == 'f') {
  11148. $line_style = array();
  11149. }
  11150. if ($line_style) {
  11151. $this->SetLineStyle($line_style);
  11152. }
  11153. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  11154. $this->_out($op);
  11155. }
  11156. /**
  11157. * Append an elliptical arc to the current path.
  11158. * An ellipse is formed from n Bezier curves.
  11159. * @param $xc (float) Abscissa of center point.
  11160. * @param $yc (float) Ordinate of center point.
  11161. * @param $rx (float) Horizontal radius.
  11162. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11163. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  11164. * @param $angs: (float) Angle start of draw line. Default value: 0.
  11165. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  11166. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  11167. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11168. * @param $startpoint (boolean) if true output a starting point.
  11169. * @param $ccw (boolean) if true draws in counter-clockwise.
  11170. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  11171. * @return array bounding box coordinates (x min, y min, x max, y max)
  11172. * @author Nicola Asuni
  11173. * @protected
  11174. * @since 4.9.019 (2010-04-26)
  11175. */
  11176. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  11177. $k = $this->k;
  11178. if ($nc < 2) {
  11179. $nc = 2;
  11180. }
  11181. $xmin = 2147483647;
  11182. $ymin = 2147483647;
  11183. $xmax = 0;
  11184. $ymax = 0;
  11185. if ($pie) {
  11186. // center of the arc
  11187. $this->_outPoint($xc, $yc);
  11188. }
  11189. $xang = deg2rad((float) $xang);
  11190. $angs = deg2rad((float) $angs);
  11191. $angf = deg2rad((float) $angf);
  11192. if ($svg) {
  11193. $as = $angs;
  11194. $af = $angf;
  11195. } else {
  11196. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  11197. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  11198. }
  11199. if ($as < 0) {
  11200. $as += (2 * M_PI);
  11201. }
  11202. if ($af < 0) {
  11203. $af += (2 * M_PI);
  11204. }
  11205. if ($ccw AND ($as > $af)) {
  11206. // reverse rotation
  11207. $as -= (2 * M_PI);
  11208. } elseif (!$ccw AND ($as < $af)) {
  11209. // reverse rotation
  11210. $af -= (2 * M_PI);
  11211. }
  11212. $total_angle = ($af - $as);
  11213. if ($nc < 2) {
  11214. $nc = 2;
  11215. }
  11216. // total arcs to draw
  11217. $nc *= (2 * abs($total_angle) / M_PI);
  11218. $nc = round($nc) + 1;
  11219. // angle of each arc
  11220. $arcang = ($total_angle / $nc);
  11221. // center point in PDF coordinates
  11222. $x0 = $xc;
  11223. $y0 = ($this->h - $yc);
  11224. // starting angle
  11225. $ang = $as;
  11226. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  11227. $cos_xang = cos($xang);
  11228. $sin_xang = sin($xang);
  11229. $cos_ang = cos($ang);
  11230. $sin_ang = sin($ang);
  11231. // first arc point
  11232. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11233. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11234. // first Bezier control point
  11235. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11236. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11237. if ($pie) {
  11238. // line from center to arc starting point
  11239. $this->_outLine($px1, $this->h - $py1);
  11240. } elseif ($startpoint) {
  11241. // arc starting point
  11242. $this->_outPoint($px1, $this->h - $py1);
  11243. }
  11244. // draw arcs
  11245. for ($i = 1; $i <= $nc; ++$i) {
  11246. // starting angle
  11247. $ang = $as + ($i * $arcang);
  11248. if ($i == $nc) {
  11249. $ang = $af;
  11250. }
  11251. $cos_ang = cos($ang);
  11252. $sin_ang = sin($ang);
  11253. // second arc point
  11254. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11255. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11256. // second Bezier control point
  11257. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11258. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11259. // draw arc
  11260. $cx1 = ($px1 + $qx1);
  11261. $cy1 = ($this->h - ($py1 + $qy1));
  11262. $cx2 = ($px2 - $qx2);
  11263. $cy2 = ($this->h - ($py2 - $qy2));
  11264. $cx3 = $px2;
  11265. $cy3 = ($this->h - $py2);
  11266. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  11267. // get bounding box coordinates
  11268. $xmin = min($xmin, $cx1, $cx2, $cx3);
  11269. $ymin = min($ymin, $cy1, $cy2, $cy3);
  11270. $xmax = max($xmax, $cx1, $cx2, $cx3);
  11271. $ymax = max($ymax, $cy1, $cy2, $cy3);
  11272. // move to next point
  11273. $px1 = $px2;
  11274. $py1 = $py2;
  11275. $qx1 = $qx2;
  11276. $qy1 = $qy2;
  11277. }
  11278. if ($pie) {
  11279. $this->_outLine($xc, $yc);
  11280. // get bounding box coordinates
  11281. $xmin = min($xmin, $xc);
  11282. $ymin = min($ymin, $yc);
  11283. $xmax = max($xmax, $xc);
  11284. $ymax = max($ymax, $yc);
  11285. }
  11286. return array($xmin, $ymin, $xmax, $ymax);
  11287. }
  11288. /**
  11289. * Draws a circle.
  11290. * A circle is formed from n Bezier curves.
  11291. * @param $x0 (float) Abscissa of center point.
  11292. * @param $y0 (float) Ordinate of center point.
  11293. * @param $r (float) Radius.
  11294. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  11295. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  11296. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11297. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11298. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11299. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  11300. * @public
  11301. * @since 2.1.000 (2008-01-08)
  11302. */
  11303. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11304. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  11305. }
  11306. /**
  11307. * Draws a polygonal line
  11308. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11309. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11310. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11311. * <ul>
  11312. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11313. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11314. * </ul>
  11315. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11316. * @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).
  11317. * @since 4.8.003 (2009-09-15)
  11318. * @public
  11319. */
  11320. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  11321. $this->Polygon($p, $style, $line_style, $fill_color, false);
  11322. }
  11323. /**
  11324. * Draws a polygon.
  11325. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11326. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11327. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11328. * <ul>
  11329. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11330. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11331. * </ul>
  11332. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11333. * @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).
  11334. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  11335. * @public
  11336. * @since 2.1.000 (2008-01-08)
  11337. */
  11338. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  11339. if ($this->state != 2) {
  11340. return;
  11341. }
  11342. $nc = count($p); // number of coordinates
  11343. $np = $nc / 2; // number of points
  11344. if ($closed) {
  11345. // close polygon by adding the first 2 points at the end (one line)
  11346. for ($i = 0; $i < 4; ++$i) {
  11347. $p[$nc + $i] = $p[$i];
  11348. }
  11349. // copy style for the last added line
  11350. if (isset($line_style[0])) {
  11351. $line_style[$np] = $line_style[0];
  11352. }
  11353. $nc += 4;
  11354. }
  11355. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11356. $this->SetFillColorArray($fill_color);
  11357. }
  11358. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11359. if ($op == 'f') {
  11360. $line_style = array();
  11361. }
  11362. $draw = true;
  11363. if ($line_style) {
  11364. if (isset($line_style['all'])) {
  11365. $this->SetLineStyle($line_style['all']);
  11366. } else {
  11367. $draw = false;
  11368. if ($op == 'B') {
  11369. // draw fill
  11370. $op = 'f';
  11371. $this->_outPoint($p[0], $p[1]);
  11372. for ($i = 2; $i < $nc; $i = $i + 2) {
  11373. $this->_outLine($p[$i], $p[$i + 1]);
  11374. }
  11375. $this->_out($op);
  11376. }
  11377. // draw outline
  11378. $this->_outPoint($p[0], $p[1]);
  11379. for ($i = 2; $i < $nc; $i = $i + 2) {
  11380. $line_num = ($i / 2) - 1;
  11381. if (isset($line_style[$line_num])) {
  11382. if ($line_style[$line_num] != 0) {
  11383. if (is_array($line_style[$line_num])) {
  11384. $this->_out('S');
  11385. $this->SetLineStyle($line_style[$line_num]);
  11386. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  11387. $this->_outLine($p[$i], $p[$i + 1]);
  11388. $this->_out('S');
  11389. $this->_outPoint($p[$i], $p[$i + 1]);
  11390. } else {
  11391. $this->_outLine($p[$i], $p[$i + 1]);
  11392. }
  11393. }
  11394. } else {
  11395. $this->_outLine($p[$i], $p[$i + 1]);
  11396. }
  11397. }
  11398. $this->_out($op);
  11399. }
  11400. }
  11401. if ($draw) {
  11402. $this->_outPoint($p[0], $p[1]);
  11403. for ($i = 2; $i < $nc; $i = $i + 2) {
  11404. $this->_outLine($p[$i], $p[$i + 1]);
  11405. }
  11406. $this->_out($op);
  11407. }
  11408. }
  11409. /**
  11410. * Draws a regular polygon.
  11411. * @param $x0 (float) Abscissa of center point.
  11412. * @param $y0 (float) Ordinate of center point.
  11413. * @param $r: (float) Radius of inscribed circle.
  11414. * @param $ns (integer) Number of sides.
  11415. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  11416. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  11417. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11418. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11419. * <ul>
  11420. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  11421. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  11422. * </ul>
  11423. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11424. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11425. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11426. * <ul>
  11427. * <li>D or empty string: Draw (default).</li>
  11428. * <li>F: Fill.</li>
  11429. * <li>DF or FD: Draw and fill.</li>
  11430. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11431. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11432. * </ul>
  11433. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11434. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11435. * @public
  11436. * @since 2.1.000 (2008-01-08)
  11437. */
  11438. 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()) {
  11439. if (3 > $ns) {
  11440. $ns = 3;
  11441. }
  11442. if ($draw_circle) {
  11443. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11444. }
  11445. $p = array();
  11446. for ($i = 0; $i < $ns; ++$i) {
  11447. $a = $angle + ($i * 360 / $ns);
  11448. $a_rad = deg2rad((float) $a);
  11449. $p[] = $x0 + ($r * sin($a_rad));
  11450. $p[] = $y0 + ($r * cos($a_rad));
  11451. }
  11452. $this->Polygon($p, $style, $line_style, $fill_color);
  11453. }
  11454. /**
  11455. * Draws a star polygon
  11456. * @param $x0 (float) Abscissa of center point.
  11457. * @param $y0 (float) Ordinate of center point.
  11458. * @param $r (float) Radius of inscribed circle.
  11459. * @param $nv (integer) Number of vertices.
  11460. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  11461. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11462. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  11463. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11464. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11465. * <ul>
  11466. * <li>all: Line style of all sides. Array like for
  11467. * SetLineStyle().</li>
  11468. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  11469. * </ul>
  11470. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11471. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11472. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11473. * <ul>
  11474. * <li>D or empty string: Draw (default).</li>
  11475. * <li>F: Fill.</li>
  11476. * <li>DF or FD: Draw and fill.</li>
  11477. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11478. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11479. * </ul>
  11480. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11481. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11482. * @public
  11483. * @since 2.1.000 (2008-01-08)
  11484. */
  11485. 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()) {
  11486. if ($nv < 2) {
  11487. $nv = 2;
  11488. }
  11489. if ($draw_circle) {
  11490. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11491. }
  11492. $p2 = array();
  11493. $visited = array();
  11494. for ($i = 0; $i < $nv; ++$i) {
  11495. $a = $angle + ($i * 360 / $nv);
  11496. $a_rad = deg2rad((float) $a);
  11497. $p2[] = $x0 + ($r * sin($a_rad));
  11498. $p2[] = $y0 + ($r * cos($a_rad));
  11499. $visited[] = false;
  11500. }
  11501. $p = array();
  11502. $i = 0;
  11503. do {
  11504. $p[] = $p2[$i * 2];
  11505. $p[] = $p2[($i * 2) + 1];
  11506. $visited[$i] = true;
  11507. $i += $ng;
  11508. $i %= $nv;
  11509. } while (!$visited[$i]);
  11510. $this->Polygon($p, $style, $line_style, $fill_color);
  11511. }
  11512. /**
  11513. * Draws a rounded rectangle.
  11514. * @param $x (float) Abscissa of upper-left corner.
  11515. * @param $y (float) Ordinate of upper-left corner.
  11516. * @param $w (float) Width.
  11517. * @param $h (float) Height.
  11518. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  11519. * @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").
  11520. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11521. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11522. * @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).
  11523. * @public
  11524. * @since 2.1.000 (2008-01-08)
  11525. */
  11526. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11527. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  11528. }
  11529. /**
  11530. * Draws a rounded rectangle.
  11531. * @param $x (float) Abscissa of upper-left corner.
  11532. * @param $y (float) Ordinate of upper-left corner.
  11533. * @param $w (float) Width.
  11534. * @param $h (float) Height.
  11535. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  11536. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  11537. * @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").
  11538. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11539. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11540. * @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).
  11541. * @public
  11542. * @since 4.9.019 (2010-04-22)
  11543. */
  11544. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11545. if ($this->state != 2) {
  11546. return;
  11547. }
  11548. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  11549. // Not rounded
  11550. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  11551. return;
  11552. }
  11553. // Rounded
  11554. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11555. $this->SetFillColorArray($fill_color);
  11556. }
  11557. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11558. if ($op == 'f') {
  11559. $border_style = array();
  11560. }
  11561. if ($border_style) {
  11562. $this->SetLineStyle($border_style);
  11563. }
  11564. $MyArc = 4 / 3 * (sqrt(2) - 1);
  11565. $this->_outPoint($x + $rx, $y);
  11566. $xc = $x + $w - $rx;
  11567. $yc = $y + $ry;
  11568. $this->_outLine($xc, $y);
  11569. if ($round_corner[0]) {
  11570. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  11571. } else {
  11572. $this->_outLine($x + $w, $y);
  11573. }
  11574. $xc = $x + $w - $rx;
  11575. $yc = $y + $h - $ry;
  11576. $this->_outLine($x + $w, $yc);
  11577. if ($round_corner[1]) {
  11578. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  11579. } else {
  11580. $this->_outLine($x + $w, $y + $h);
  11581. }
  11582. $xc = $x + $rx;
  11583. $yc = $y + $h - $ry;
  11584. $this->_outLine($xc, $y + $h);
  11585. if ($round_corner[2]) {
  11586. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  11587. } else {
  11588. $this->_outLine($x, $y + $h);
  11589. }
  11590. $xc = $x + $rx;
  11591. $yc = $y + $ry;
  11592. $this->_outLine($x, $yc);
  11593. if ($round_corner[3]) {
  11594. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  11595. } else {
  11596. $this->_outLine($x, $y);
  11597. $this->_outLine($x + $rx, $y);
  11598. }
  11599. $this->_out($op);
  11600. }
  11601. /**
  11602. * Draws a grahic arrow.
  11603. * @param $x0 (float) Abscissa of first point.
  11604. * @param $y0 (float) Ordinate of first point.
  11605. * @param $x1 (float) Abscissa of second point.
  11606. * @param $y1 (float) Ordinate of second point.
  11607. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  11608. * @param $arm_size (float) length of arrowhead arms
  11609. * @param $arm_angle (int) angle between an arm and the shaft
  11610. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  11611. * @since 4.6.018 (2009-07-10)
  11612. */
  11613. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  11614. // getting arrow direction angle
  11615. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  11616. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  11617. if ($dir_angle < 0) {
  11618. $dir_angle += (2 * M_PI);
  11619. }
  11620. $arm_angle = deg2rad($arm_angle);
  11621. $sx1 = $x1;
  11622. $sy1 = $y1;
  11623. if ($head_style > 0) {
  11624. // calculate the stopping point for the arrow shaft
  11625. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  11626. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  11627. }
  11628. // main arrow line / shaft
  11629. $this->Line($x0, $y0, $sx1, $sy1);
  11630. // left arrowhead arm tip
  11631. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  11632. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  11633. // right arrowhead arm tip
  11634. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  11635. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  11636. $mode = 'D';
  11637. $style = array();
  11638. switch ($head_style) {
  11639. case 0: {
  11640. // draw only arrowhead arms
  11641. $mode = 'D';
  11642. $style = array(1, 1, 0);
  11643. break;
  11644. }
  11645. case 1: {
  11646. // draw closed arrowhead, but no fill
  11647. $mode = 'D';
  11648. break;
  11649. }
  11650. case 2: {
  11651. // closed and filled arrowhead
  11652. $mode = 'DF';
  11653. break;
  11654. }
  11655. case 3: {
  11656. // filled arrowhead
  11657. $mode = 'F';
  11658. break;
  11659. }
  11660. }
  11661. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  11662. }
  11663. // END GRAPHIC FUNCTIONS SECTION -----------------------
  11664. /**
  11665. * Add a Named Destination.
  11666. * NOTE: destination names are unique, so only last entry will be saved.
  11667. * @param $name (string) Destination name.
  11668. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  11669. * @param $page (int) Target page number (leave empty for current page).
  11670. * @param $x (float) X position in user units of the destiantion on the selected page (default = -1 = current position;).
  11671. * @return (string) Stripped named destination identifier or false in case of error.
  11672. * @public
  11673. * @author Christian Deligant, Nicola Asuni
  11674. * @since 5.9.097 (2011-06-23)
  11675. */
  11676. public function setDestination($name, $y=-1, $page='', $x=-1) {
  11677. // remove unsupported characters
  11678. $name = TCPDF_STATIC::encodeNameObject($name);
  11679. if (TCPDF_STATIC::empty_string($name)) {
  11680. return false;
  11681. }
  11682. if ($y == -1) {
  11683. $y = $this->GetY();
  11684. } elseif ($y < 0) {
  11685. $y = 0;
  11686. } elseif ($y > $this->h) {
  11687. $y = $this->h;
  11688. }
  11689. if ($x == -1) {
  11690. $x = $this->GetX();
  11691. } elseif ($x < 0) {
  11692. $x = 0;
  11693. } elseif ($x > $this->w) {
  11694. $x = $this->w;
  11695. }
  11696. if (empty($page)) {
  11697. $page = $this->PageNo();
  11698. if (empty($page)) {
  11699. return;
  11700. }
  11701. }
  11702. $this->dests[$name] = array('x' => $x, 'y' => $y, 'p' => $page);
  11703. return $name;
  11704. }
  11705. /**
  11706. * Return the Named Destination array.
  11707. * @return (array) Named Destination array.
  11708. * @public
  11709. * @author Nicola Asuni
  11710. * @since 5.9.097 (2011-06-23)
  11711. */
  11712. public function getDestination() {
  11713. return $this->dests;
  11714. }
  11715. /**
  11716. * Insert Named Destinations.
  11717. * @protected
  11718. * @author Johannes Güntert, Nicola Asuni
  11719. * @since 5.9.098 (2011-06-23)
  11720. */
  11721. protected function _putdests() {
  11722. if (empty($this->dests)) {
  11723. return;
  11724. }
  11725. $this->n_dests = $this->_newobj();
  11726. $out = ' <<';
  11727. foreach($this->dests as $name => $o) {
  11728. $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)));
  11729. }
  11730. $out .= ' >>';
  11731. $out .= "\n".'endobj';
  11732. $this->_out($out);
  11733. }
  11734. /**
  11735. * Adds a bookmark - alias for Bookmark().
  11736. * @param $txt (string) Bookmark description.
  11737. * @param $level (int) Bookmark level (minimum value is 0).
  11738. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11739. * @param $page (int) Target page number (leave empty for current page).
  11740. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11741. * @param $color (array) RGB color array (values from 0 to 255).
  11742. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11743. * @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).
  11744. * @public
  11745. */
  11746. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11747. $this->Bookmark($txt, $level, $y, $page, $style, $color, $x, $link);
  11748. }
  11749. /**
  11750. * Adds a 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. * @since 2.1.002 (2008-02-12)
  11761. */
  11762. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11763. if ($level < 0) {
  11764. $level = 0;
  11765. }
  11766. if (isset($this->outlines[0])) {
  11767. $lastoutline = end($this->outlines);
  11768. $maxlevel = $lastoutline['l'] + 1;
  11769. } else {
  11770. $maxlevel = 0;
  11771. }
  11772. if ($level > $maxlevel) {
  11773. $level = $maxlevel;
  11774. }
  11775. if ($y == -1) {
  11776. $y = $this->GetY();
  11777. } elseif ($y < 0) {
  11778. $y = 0;
  11779. } elseif ($y > $this->h) {
  11780. $y = $this->h;
  11781. }
  11782. if ($x == -1) {
  11783. $x = $this->GetX();
  11784. } elseif ($x < 0) {
  11785. $x = 0;
  11786. } elseif ($x > $this->w) {
  11787. $x = $this->w;
  11788. }
  11789. if (empty($page)) {
  11790. $page = $this->PageNo();
  11791. if (empty($page)) {
  11792. return;
  11793. }
  11794. }
  11795. $this->outlines[] = array('t' => $txt, 'l' => $level, 'x' => $x, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color, 'u' => $link);
  11796. }
  11797. /**
  11798. * Sort bookmarks for page and key.
  11799. * @protected
  11800. * @since 5.9.119 (2011-09-19)
  11801. */
  11802. protected function sortBookmarks() {
  11803. // get sorting columns
  11804. $outline_p = array();
  11805. $outline_y = array();
  11806. foreach ($this->outlines as $key => $row) {
  11807. $outline_p[$key] = $row['p'];
  11808. $outline_k[$key] = $key;
  11809. }
  11810. // sort outlines by page and original position
  11811. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  11812. }
  11813. /**
  11814. * Create a bookmark PDF string.
  11815. * @protected
  11816. * @author Olivier Plathey, Nicola Asuni
  11817. * @since 2.1.002 (2008-02-12)
  11818. */
  11819. protected function _putbookmarks() {
  11820. $nb = count($this->outlines);
  11821. if ($nb == 0) {
  11822. return;
  11823. }
  11824. // sort bookmarks
  11825. $this->sortBookmarks();
  11826. $lru = array();
  11827. $level = 0;
  11828. foreach ($this->outlines as $i => $o) {
  11829. if ($o['l'] > 0) {
  11830. $parent = $lru[($o['l'] - 1)];
  11831. //Set parent and last pointers
  11832. $this->outlines[$i]['parent'] = $parent;
  11833. $this->outlines[$parent]['last'] = $i;
  11834. if ($o['l'] > $level) {
  11835. //Level increasing: set first pointer
  11836. $this->outlines[$parent]['first'] = $i;
  11837. }
  11838. } else {
  11839. $this->outlines[$i]['parent'] = $nb;
  11840. }
  11841. if (($o['l'] <= $level) AND ($i > 0)) {
  11842. //Set prev and next pointers
  11843. $prev = $lru[$o['l']];
  11844. $this->outlines[$prev]['next'] = $i;
  11845. $this->outlines[$i]['prev'] = $prev;
  11846. }
  11847. $lru[$o['l']] = $i;
  11848. $level = $o['l'];
  11849. }
  11850. //Outline items
  11851. $n = $this->n + 1;
  11852. $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';
  11853. foreach ($this->outlines as $i => $o) {
  11854. $oid = $this->_newobj();
  11855. // covert HTML title to string
  11856. $title = preg_replace($nltags, "\n", $o['t']);
  11857. $title = preg_replace("/[\r]+/si", '', $title);
  11858. $title = preg_replace("/[\n]+/si", "\n", $title);
  11859. $title = strip_tags($title);
  11860. $title = $this->stringTrim($title);
  11861. $out = '<</Title '.$this->_textstring($title, $oid);
  11862. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  11863. if (isset($o['prev'])) {
  11864. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  11865. }
  11866. if (isset($o['next'])) {
  11867. $out .= ' /Next '.($n + $o['next']).' 0 R';
  11868. }
  11869. if (isset($o['first'])) {
  11870. $out .= ' /First '.($n + $o['first']).' 0 R';
  11871. }
  11872. if (isset($o['last'])) {
  11873. $out .= ' /Last '.($n + $o['last']).' 0 R';
  11874. }
  11875. if (isset($o['u']) AND !empty($o['u'])) {
  11876. // link
  11877. if (is_string($o['u'])) {
  11878. if ($o['u'][0] == '#') {
  11879. // internal destination
  11880. $out .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($o['u'], 1));
  11881. } elseif ($o['u'][0] == '%') {
  11882. // embedded PDF file
  11883. $filename = basename(substr($o['u'], 1));
  11884. $out .= ' /A <</S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($o['p'] - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  11885. } elseif ($o['u'][0] == '*') {
  11886. // embedded generic file
  11887. $filename = basename(substr($o['u'], 1));
  11888. $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});';
  11889. $out .= ' /A <</S /JavaScript /JS '.$this->_textstring($jsa, $oid).'>>';
  11890. } else {
  11891. // external URI link
  11892. $out .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($o['u']), $oid).'>>';
  11893. }
  11894. } elseif (isset($this->links[$o['u']])) {
  11895. // internal link ID
  11896. $l = $this->links[$o['u']];
  11897. if (isset($this->page_obj_id[($l[0])])) {
  11898. $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)));
  11899. }
  11900. }
  11901. } elseif (isset($this->page_obj_id[($o['p'])])) {
  11902. // link to a page
  11903. $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)));
  11904. }
  11905. // set font style
  11906. $style = 0;
  11907. if (!empty($o['s'])) {
  11908. // bold
  11909. if (strpos($o['s'], 'B') !== false) {
  11910. $style |= 2;
  11911. }
  11912. // oblique
  11913. if (strpos($o['s'], 'I') !== false) {
  11914. $style |= 1;
  11915. }
  11916. }
  11917. $out .= sprintf(' /F %d', $style);
  11918. // set bookmark color
  11919. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  11920. $color = array_values($o['c']);
  11921. $out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  11922. } else {
  11923. // black
  11924. $out .= ' /C [0.0 0.0 0.0]';
  11925. }
  11926. $out .= ' /Count 0'; // normally closed item
  11927. $out .= ' >>';
  11928. $out .= "\n".'endobj';
  11929. $this->_out($out);
  11930. }
  11931. //Outline root
  11932. $this->OutlineRoot = $this->_newobj();
  11933. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  11934. }
  11935. // --- JAVASCRIPT ------------------------------------------------------
  11936. /**
  11937. * Adds a javascript
  11938. * @param $script (string) Javascript code
  11939. * @public
  11940. * @author Johannes Güntert, Nicola Asuni
  11941. * @since 2.1.002 (2008-02-12)
  11942. */
  11943. public function IncludeJS($script) {
  11944. $this->javascript .= $script;
  11945. }
  11946. /**
  11947. * Adds a javascript object and return object ID
  11948. * @param $script (string) Javascript code
  11949. * @param $onload (boolean) if true executes this object when opening the document
  11950. * @return int internal object ID
  11951. * @public
  11952. * @author Nicola Asuni
  11953. * @since 4.8.000 (2009-09-07)
  11954. */
  11955. public function addJavascriptObject($script, $onload=false) {
  11956. if ($this->pdfa_mode) {
  11957. // javascript is not allowed in PDF/A mode
  11958. return false;
  11959. }
  11960. ++$this->n;
  11961. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  11962. return $this->n;
  11963. }
  11964. /**
  11965. * Create a javascript PDF string.
  11966. * @protected
  11967. * @author Johannes Güntert, Nicola Asuni
  11968. * @since 2.1.002 (2008-02-12)
  11969. */
  11970. protected function _putjavascript() {
  11971. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  11972. return;
  11973. }
  11974. if (strpos($this->javascript, 'this.addField') > 0) {
  11975. if (!$this->ur['enabled']) {
  11976. //$this->setUserRights();
  11977. }
  11978. // the following two lines are used to avoid form fields duplication after saving
  11979. // The addField method only works when releasing user rights (UR3)
  11980. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  11981. $jsb = "getField('tcpdfdocsaved').value='saved';";
  11982. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  11983. }
  11984. // name tree for javascript
  11985. $this->n_js = '<< /Names [';
  11986. if (!empty($this->javascript)) {
  11987. $this->n_js .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  11988. }
  11989. if (!empty($this->js_objects)) {
  11990. foreach ($this->js_objects as $key => $val) {
  11991. if ($val['onload']) {
  11992. $this->n_js .= ' (JS'.$key.') '.$key.' 0 R';
  11993. }
  11994. }
  11995. }
  11996. $this->n_js .= ' ] >>';
  11997. // default Javascript object
  11998. if (!empty($this->javascript)) {
  11999. $obj_id = $this->_newobj();
  12000. $out = '<< /S /JavaScript';
  12001. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  12002. $out .= ' >>';
  12003. $out .= "\n".'endobj';
  12004. $this->_out($out);
  12005. }
  12006. // additional Javascript objects
  12007. if (!empty($this->js_objects)) {
  12008. foreach ($this->js_objects as $key => $val) {
  12009. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  12010. $this->_out($out);
  12011. }
  12012. }
  12013. }
  12014. /**
  12015. * Adds a javascript form field.
  12016. * @param $type (string) field type
  12017. * @param $name (string) field name
  12018. * @param $x (int) horizontal position
  12019. * @param $y (int) vertical position
  12020. * @param $w (int) width
  12021. * @param $h (int) height
  12022. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12023. * @protected
  12024. * @author Denis Van Nuffelen, Nicola Asuni
  12025. * @since 2.1.002 (2008-02-12)
  12026. */
  12027. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  12028. if ($this->rtl) {
  12029. $x = $x - $w;
  12030. }
  12031. // the followind avoid fields duplication after saving the document
  12032. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  12033. $k = $this->k;
  12034. $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";
  12035. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  12036. while (list($key, $val) = each($prop)) {
  12037. if (strcmp(substr($key, -5), 'Color') == 0) {
  12038. $val = TCPDF_COLORS::_JScolor($val);
  12039. } else {
  12040. $val = "'".$val."'";
  12041. }
  12042. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  12043. }
  12044. if ($this->rtl) {
  12045. $this->x -= $w;
  12046. } else {
  12047. $this->x += $w;
  12048. }
  12049. $this->javascript .= '}';
  12050. }
  12051. // --- FORM FIELDS -----------------------------------------------------
  12052. /**
  12053. * Set default properties for form fields.
  12054. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12055. * @public
  12056. * @author Nicola Asuni
  12057. * @since 4.8.000 (2009-09-06)
  12058. */
  12059. public function setFormDefaultProp($prop=array()) {
  12060. $this->default_form_prop = $prop;
  12061. }
  12062. /**
  12063. * Return the default properties for form fields.
  12064. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12065. * @public
  12066. * @author Nicola Asuni
  12067. * @since 4.8.000 (2009-09-06)
  12068. */
  12069. public function getFormDefaultProp() {
  12070. return $this->default_form_prop;
  12071. }
  12072. /**
  12073. * Creates a text field
  12074. * @param $name (string) field name
  12075. * @param $w (float) Width of the rectangle
  12076. * @param $h (float) Height of the rectangle
  12077. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12078. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12079. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12080. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12081. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12082. * @public
  12083. * @author Nicola Asuni
  12084. * @since 4.8.000 (2009-09-07)
  12085. */
  12086. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12087. if ($x === '') {
  12088. $x = $this->x;
  12089. }
  12090. if ($y === '') {
  12091. $y = $this->y;
  12092. }
  12093. // check page for no-write regions and adapt page margins if necessary
  12094. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12095. if ($js) {
  12096. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  12097. return;
  12098. }
  12099. // get default style
  12100. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12101. // get annotation data
  12102. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12103. // set default appearance stream
  12104. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12105. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12106. $popt['da'] = $fontstyle;
  12107. // build appearance stream
  12108. $popt['ap'] = array();
  12109. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12110. $text = '';
  12111. if (isset($prop['value']) AND !empty($prop['value'])) {
  12112. $text = $prop['value'];
  12113. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  12114. $text = $opt['v'];
  12115. }
  12116. $tmpid = $this->startTemplate($w, $h, false);
  12117. $align = '';
  12118. if (isset($popt['q'])) {
  12119. switch ($popt['q']) {
  12120. case 0: {
  12121. $align = 'L';
  12122. break;
  12123. }
  12124. case 1: {
  12125. $align = 'C';
  12126. break;
  12127. }
  12128. case 2: {
  12129. $align = 'R';
  12130. break;
  12131. }
  12132. default: {
  12133. $align = '';
  12134. break;
  12135. }
  12136. }
  12137. }
  12138. $this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12139. $this->endTemplate();
  12140. --$this->n;
  12141. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12142. unset($this->xobjects[$tmpid]);
  12143. $popt['ap']['n'] .= 'Q EMC';
  12144. // merge options
  12145. $opt = array_merge($popt, $opt);
  12146. // remove some conflicting options
  12147. unset($opt['bs']);
  12148. // set remaining annotation data
  12149. $opt['Subtype'] = 'Widget';
  12150. $opt['ft'] = 'Tx';
  12151. $opt['t'] = $name;
  12152. // Additional annotation's parameters (check _putannotsobj() method):
  12153. //$opt['f']
  12154. //$opt['as']
  12155. //$opt['bs']
  12156. //$opt['be']
  12157. //$opt['c']
  12158. //$opt['border']
  12159. //$opt['h']
  12160. //$opt['mk'];
  12161. //$opt['mk']['r']
  12162. //$opt['mk']['bc'];
  12163. //$opt['mk']['bg'];
  12164. unset($opt['mk']['ca']);
  12165. unset($opt['mk']['rc']);
  12166. unset($opt['mk']['ac']);
  12167. unset($opt['mk']['i']);
  12168. unset($opt['mk']['ri']);
  12169. unset($opt['mk']['ix']);
  12170. unset($opt['mk']['if']);
  12171. //$opt['mk']['if']['sw'];
  12172. //$opt['mk']['if']['s'];
  12173. //$opt['mk']['if']['a'];
  12174. //$opt['mk']['if']['fb'];
  12175. unset($opt['mk']['tp']);
  12176. //$opt['tu']
  12177. //$opt['tm']
  12178. //$opt['ff']
  12179. //$opt['v']
  12180. //$opt['dv']
  12181. //$opt['a']
  12182. //$opt['aa']
  12183. //$opt['q']
  12184. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12185. if ($this->rtl) {
  12186. $this->x -= $w;
  12187. } else {
  12188. $this->x += $w;
  12189. }
  12190. }
  12191. /**
  12192. * Creates a RadioButton field.
  12193. * @param $name (string) Field name.
  12194. * @param $w (int) Width of the radio button.
  12195. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12196. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12197. * @param $onvalue (string) Value to be returned if selected.
  12198. * @param $checked (boolean) Define the initial state.
  12199. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12200. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12201. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12202. * @public
  12203. * @author Nicola Asuni
  12204. * @since 4.8.000 (2009-09-07)
  12205. */
  12206. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  12207. if ($x === '') {
  12208. $x = $this->x;
  12209. }
  12210. if ($y === '') {
  12211. $y = $this->y;
  12212. }
  12213. // check page for no-write regions and adapt page margins if necessary
  12214. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12215. if ($js) {
  12216. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  12217. return;
  12218. }
  12219. if (TCPDF_STATIC::empty_string($onvalue)) {
  12220. $onvalue = 'On';
  12221. }
  12222. if ($checked) {
  12223. $defval = $onvalue;
  12224. } else {
  12225. $defval = 'Off';
  12226. }
  12227. // set font
  12228. $font = 'zapfdingbats';
  12229. if ($this->pdfa_mode) {
  12230. // all fonts must be embedded
  12231. $font = 'pdfa'.$font;
  12232. }
  12233. $this->AddFont($font);
  12234. $tmpfont = $this->getFontBuffer($font);
  12235. // set data for parent group
  12236. if (!isset($this->radiobutton_groups[$this->page])) {
  12237. $this->radiobutton_groups[$this->page] = array();
  12238. }
  12239. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  12240. $this->radiobutton_groups[$this->page][$name] = array();
  12241. ++$this->n;
  12242. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  12243. $this->radio_groups[] = $this->n;
  12244. }
  12245. $kid = ($this->n + 1);
  12246. // save object ID to be added on Kids entry on parent object
  12247. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  12248. // get default style
  12249. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12250. $prop['NoToggleToOff'] = 'true';
  12251. $prop['Radio'] = 'true';
  12252. $prop['borderStyle'] = 'inset';
  12253. // get annotation data
  12254. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12255. // set additional default options
  12256. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12257. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12258. $popt['da'] = $fontstyle;
  12259. // build appearance stream
  12260. $popt['ap'] = array();
  12261. $popt['ap']['n'] = array();
  12262. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
  12263. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12264. $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);
  12265. $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);
  12266. if (!isset($popt['mk'])) {
  12267. $popt['mk'] = array();
  12268. }
  12269. $popt['mk']['ca'] = '(l)';
  12270. // merge options
  12271. $opt = array_merge($popt, $opt);
  12272. // set remaining annotation data
  12273. $opt['Subtype'] = 'Widget';
  12274. $opt['ft'] = 'Btn';
  12275. if ($checked) {
  12276. $opt['v'] = array('/'.$onvalue);
  12277. $opt['as'] = $onvalue;
  12278. } else {
  12279. $opt['as'] = 'Off';
  12280. }
  12281. // store readonly flag
  12282. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  12283. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  12284. }
  12285. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  12286. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12287. if ($this->rtl) {
  12288. $this->x -= $w;
  12289. } else {
  12290. $this->x += $w;
  12291. }
  12292. }
  12293. /**
  12294. * Creates a List-box field
  12295. * @param $name (string) field name
  12296. * @param $w (int) width
  12297. * @param $h (int) height
  12298. * @param $values (array) array containing the list of values.
  12299. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12300. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12301. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12302. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12303. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12304. * @public
  12305. * @author Nicola Asuni
  12306. * @since 4.8.000 (2009-09-07)
  12307. */
  12308. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12309. if ($x === '') {
  12310. $x = $this->x;
  12311. }
  12312. if ($y === '') {
  12313. $y = $this->y;
  12314. }
  12315. // check page for no-write regions and adapt page margins if necessary
  12316. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12317. if ($js) {
  12318. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  12319. $s = '';
  12320. foreach ($values as $value) {
  12321. if (is_array($value)) {
  12322. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12323. } else {
  12324. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12325. }
  12326. }
  12327. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12328. return;
  12329. }
  12330. // get default style
  12331. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12332. // get annotation data
  12333. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12334. // set additional default values
  12335. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12336. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12337. $popt['da'] = $fontstyle;
  12338. // build appearance stream
  12339. $popt['ap'] = array();
  12340. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12341. $text = '';
  12342. foreach($values as $item) {
  12343. if (is_array($item)) {
  12344. $text .= $item[1]."\n";
  12345. } else {
  12346. $text .= $item."\n";
  12347. }
  12348. }
  12349. $tmpid = $this->startTemplate($w, $h, false);
  12350. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12351. $this->endTemplate();
  12352. --$this->n;
  12353. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12354. unset($this->xobjects[$tmpid]);
  12355. $popt['ap']['n'] .= 'Q EMC';
  12356. // merge options
  12357. $opt = array_merge($popt, $opt);
  12358. // set remaining annotation data
  12359. $opt['Subtype'] = 'Widget';
  12360. $opt['ft'] = 'Ch';
  12361. $opt['t'] = $name;
  12362. $opt['opt'] = $values;
  12363. unset($opt['mk']['ca']);
  12364. unset($opt['mk']['rc']);
  12365. unset($opt['mk']['ac']);
  12366. unset($opt['mk']['i']);
  12367. unset($opt['mk']['ri']);
  12368. unset($opt['mk']['ix']);
  12369. unset($opt['mk']['if']);
  12370. unset($opt['mk']['tp']);
  12371. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12372. if ($this->rtl) {
  12373. $this->x -= $w;
  12374. } else {
  12375. $this->x += $w;
  12376. }
  12377. }
  12378. /**
  12379. * Creates a Combo-box field
  12380. * @param $name (string) field name
  12381. * @param $w (int) width
  12382. * @param $h (int) height
  12383. * @param $values (array) array containing the list of values.
  12384. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12385. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12386. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12387. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12388. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12389. * @public
  12390. * @author Nicola Asuni
  12391. * @since 4.8.000 (2009-09-07)
  12392. */
  12393. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12394. if ($x === '') {
  12395. $x = $this->x;
  12396. }
  12397. if ($y === '') {
  12398. $y = $this->y;
  12399. }
  12400. // check page for no-write regions and adapt page margins if necessary
  12401. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12402. if ($js) {
  12403. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  12404. $s = '';
  12405. foreach ($values as $value) {
  12406. if (is_array($value)) {
  12407. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12408. } else {
  12409. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12410. }
  12411. }
  12412. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12413. return;
  12414. }
  12415. // get default style
  12416. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12417. $prop['Combo'] = true;
  12418. // get annotation data
  12419. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12420. // set additional default options
  12421. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12422. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12423. $popt['da'] = $fontstyle;
  12424. // build appearance stream
  12425. $popt['ap'] = array();
  12426. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12427. $text = '';
  12428. foreach($values as $item) {
  12429. if (is_array($item)) {
  12430. $text .= $item[1]."\n";
  12431. } else {
  12432. $text .= $item."\n";
  12433. }
  12434. }
  12435. $tmpid = $this->startTemplate($w, $h, false);
  12436. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12437. $this->endTemplate();
  12438. --$this->n;
  12439. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12440. unset($this->xobjects[$tmpid]);
  12441. $popt['ap']['n'] .= 'Q EMC';
  12442. // merge options
  12443. $opt = array_merge($popt, $opt);
  12444. // set remaining annotation data
  12445. $opt['Subtype'] = 'Widget';
  12446. $opt['ft'] = 'Ch';
  12447. $opt['t'] = $name;
  12448. $opt['opt'] = $values;
  12449. unset($opt['mk']['ca']);
  12450. unset($opt['mk']['rc']);
  12451. unset($opt['mk']['ac']);
  12452. unset($opt['mk']['i']);
  12453. unset($opt['mk']['ri']);
  12454. unset($opt['mk']['ix']);
  12455. unset($opt['mk']['if']);
  12456. unset($opt['mk']['tp']);
  12457. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12458. if ($this->rtl) {
  12459. $this->x -= $w;
  12460. } else {
  12461. $this->x += $w;
  12462. }
  12463. }
  12464. /**
  12465. * Creates a CheckBox field
  12466. * @param $name (string) field name
  12467. * @param $w (int) width
  12468. * @param $checked (boolean) define the initial state.
  12469. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12470. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12471. * @param $onvalue (string) value to be returned if selected.
  12472. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12473. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12474. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12475. * @public
  12476. * @author Nicola Asuni
  12477. * @since 4.8.000 (2009-09-07)
  12478. */
  12479. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  12480. if ($x === '') {
  12481. $x = $this->x;
  12482. }
  12483. if ($y === '') {
  12484. $y = $this->y;
  12485. }
  12486. // check page for no-write regions and adapt page margins if necessary
  12487. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12488. if ($js) {
  12489. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  12490. return;
  12491. }
  12492. if (!isset($prop['value'])) {
  12493. $prop['value'] = array('Yes');
  12494. }
  12495. // get default style
  12496. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12497. $prop['borderStyle'] = 'inset';
  12498. // get annotation data
  12499. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12500. // set additional default options
  12501. $font = 'zapfdingbats';
  12502. if ($this->pdfa_mode) {
  12503. // all fonts must be embedded
  12504. $font = 'pdfa'.$font;
  12505. }
  12506. $this->AddFont($font);
  12507. $tmpfont = $this->getFontBuffer($font);
  12508. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12509. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12510. $popt['da'] = $fontstyle;
  12511. // build appearance stream
  12512. $popt['ap'] = array();
  12513. $popt['ap']['n'] = array();
  12514. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
  12515. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12516. $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);
  12517. $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);
  12518. // merge options
  12519. $opt = array_merge($popt, $opt);
  12520. // set remaining annotation data
  12521. $opt['Subtype'] = 'Widget';
  12522. $opt['ft'] = 'Btn';
  12523. $opt['t'] = $name;
  12524. if (TCPDF_STATIC::empty_string($onvalue)) {
  12525. $onvalue = 'Yes';
  12526. }
  12527. $opt['opt'] = array($onvalue);
  12528. if ($checked) {
  12529. $opt['v'] = array('/Yes');
  12530. $opt['as'] = 'Yes';
  12531. } else {
  12532. $opt['v'] = array('/Off');
  12533. $opt['as'] = 'Off';
  12534. }
  12535. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12536. if ($this->rtl) {
  12537. $this->x -= $w;
  12538. } else {
  12539. $this->x += $w;
  12540. }
  12541. }
  12542. /**
  12543. * Creates a button field
  12544. * @param $name (string) field name
  12545. * @param $w (int) width
  12546. * @param $h (int) height
  12547. * @param $caption (string) caption.
  12548. * @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.
  12549. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12550. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12551. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12552. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12553. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12554. * @public
  12555. * @author Nicola Asuni
  12556. * @since 4.8.000 (2009-09-07)
  12557. */
  12558. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12559. if ($x === '') {
  12560. $x = $this->x;
  12561. }
  12562. if ($y === '') {
  12563. $y = $this->y;
  12564. }
  12565. // check page for no-write regions and adapt page margins if necessary
  12566. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12567. if ($js) {
  12568. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  12569. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  12570. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  12571. $this->javascript .= 'f'.$name.".highlight='push';\n";
  12572. $this->javascript .= 'f'.$name.".print=false;\n";
  12573. return;
  12574. }
  12575. // get default style
  12576. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12577. $prop['Pushbutton'] = 'true';
  12578. $prop['highlight'] = 'push';
  12579. $prop['display'] = 'display.noPrint';
  12580. // get annotation data
  12581. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12582. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12583. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12584. $popt['da'] = $fontstyle;
  12585. // build appearance stream
  12586. $popt['ap'] = array();
  12587. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12588. $tmpid = $this->startTemplate($w, $h, false);
  12589. $bw = (2 / $this->k); // border width
  12590. $border = array(
  12591. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12592. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  12593. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12594. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  12595. $this->SetFillColor(204);
  12596. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  12597. $this->endTemplate();
  12598. --$this->n;
  12599. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12600. unset($this->xobjects[$tmpid]);
  12601. $popt['ap']['n'] .= 'Q EMC';
  12602. // set additional default options
  12603. if (!isset($popt['mk'])) {
  12604. $popt['mk'] = array();
  12605. }
  12606. $ann_obj_id = ($this->n + 1);
  12607. if (!empty($action) AND !is_array($action)) {
  12608. $ann_obj_id = ($this->n + 2);
  12609. }
  12610. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  12611. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  12612. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  12613. // merge options
  12614. $opt = array_merge($popt, $opt);
  12615. // set remaining annotation data
  12616. $opt['Subtype'] = 'Widget';
  12617. $opt['ft'] = 'Btn';
  12618. $opt['t'] = $caption;
  12619. $opt['v'] = $name;
  12620. if (!empty($action)) {
  12621. if (is_array($action)) {
  12622. // form action options as on section 12.7.5 of PDF32000_2008.
  12623. $opt['aa'] = '/D <<';
  12624. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  12625. foreach ($action AS $key => $val) {
  12626. if (($key == 'S') AND in_array($val, $bmode)) {
  12627. $opt['aa'] .= ' /S /'.$val;
  12628. } elseif (($key == 'F') AND (!empty($val))) {
  12629. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  12630. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  12631. $opt['aa'] .= ' /Fields [';
  12632. foreach ($val AS $field) {
  12633. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  12634. }
  12635. $opt['aa'] .= ']';
  12636. } elseif (($key == 'Flags')) {
  12637. $ff = 0;
  12638. if (is_array($val)) {
  12639. foreach ($val AS $flag) {
  12640. switch ($flag) {
  12641. case 'Include/Exclude': {
  12642. $ff += 1 << 0;
  12643. break;
  12644. }
  12645. case 'IncludeNoValueFields': {
  12646. $ff += 1 << 1;
  12647. break;
  12648. }
  12649. case 'ExportFormat': {
  12650. $ff += 1 << 2;
  12651. break;
  12652. }
  12653. case 'GetMethod': {
  12654. $ff += 1 << 3;
  12655. break;
  12656. }
  12657. case 'SubmitCoordinates': {
  12658. $ff += 1 << 4;
  12659. break;
  12660. }
  12661. case 'XFDF': {
  12662. $ff += 1 << 5;
  12663. break;
  12664. }
  12665. case 'IncludeAppendSaves': {
  12666. $ff += 1 << 6;
  12667. break;
  12668. }
  12669. case 'IncludeAnnotations': {
  12670. $ff += 1 << 7;
  12671. break;
  12672. }
  12673. case 'SubmitPDF': {
  12674. $ff += 1 << 8;
  12675. break;
  12676. }
  12677. case 'CanonicalFormat': {
  12678. $ff += 1 << 9;
  12679. break;
  12680. }
  12681. case 'ExclNonUserAnnots': {
  12682. $ff += 1 << 10;
  12683. break;
  12684. }
  12685. case 'ExclFKey': {
  12686. $ff += 1 << 11;
  12687. break;
  12688. }
  12689. case 'EmbedForm': {
  12690. $ff += 1 << 13;
  12691. break;
  12692. }
  12693. }
  12694. }
  12695. } else {
  12696. $ff = intval($val);
  12697. }
  12698. $opt['aa'] .= ' /Flags '.$ff;
  12699. }
  12700. }
  12701. $opt['aa'] .= ' >>';
  12702. } else {
  12703. // Javascript action or raw action command
  12704. $js_obj_id = $this->addJavascriptObject($action);
  12705. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  12706. }
  12707. }
  12708. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12709. if ($this->rtl) {
  12710. $this->x -= $w;
  12711. } else {
  12712. $this->x += $w;
  12713. }
  12714. }
  12715. // --- END FORMS FIELDS ------------------------------------------------
  12716. /**
  12717. * Add certification signature (DocMDP or UR3)
  12718. * You can set only one signature type
  12719. * @protected
  12720. * @author Nicola Asuni
  12721. * @since 4.6.008 (2009-05-07)
  12722. */
  12723. protected function _putsignature() {
  12724. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  12725. return;
  12726. }
  12727. $sigobjid = ($this->sig_obj_id + 1);
  12728. $out = $this->_getobj($sigobjid)."\n";
  12729. $out .= '<< /Type /Sig';
  12730. $out .= ' /Filter /Adobe.PPKLite';
  12731. $out .= ' /SubFilter /adbe.pkcs7.detached';
  12732. $out .= ' '.TCPDF_STATIC::$byterange_string;
  12733. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  12734. $out .= ' /Reference ['; // array of signature reference dictionaries
  12735. $out .= ' << /Type /SigRef';
  12736. if ($this->signature_data['cert_type'] > 0) {
  12737. $out .= ' /TransformMethod /DocMDP';
  12738. $out .= ' /TransformParams <<';
  12739. $out .= ' /Type /TransformParams';
  12740. $out .= ' /P '.$this->signature_data['cert_type'];
  12741. $out .= ' /V /1.2';
  12742. } else {
  12743. $out .= ' /TransformMethod /UR3';
  12744. $out .= ' /TransformParams <<';
  12745. $out .= ' /Type /TransformParams';
  12746. $out .= ' /V /2.2';
  12747. if (!TCPDF_STATIC::empty_string($this->ur['document'])) {
  12748. $out .= ' /Document['.$this->ur['document'].']';
  12749. }
  12750. if (!TCPDF_STATIC::empty_string($this->ur['form'])) {
  12751. $out .= ' /Form['.$this->ur['form'].']';
  12752. }
  12753. if (!TCPDF_STATIC::empty_string($this->ur['signature'])) {
  12754. $out .= ' /Signature['.$this->ur['signature'].']';
  12755. }
  12756. if (!TCPDF_STATIC::empty_string($this->ur['annots'])) {
  12757. $out .= ' /Annots['.$this->ur['annots'].']';
  12758. }
  12759. if (!TCPDF_STATIC::empty_string($this->ur['ef'])) {
  12760. $out .= ' /EF['.$this->ur['ef'].']';
  12761. }
  12762. if (!TCPDF_STATIC::empty_string($this->ur['formex'])) {
  12763. $out .= ' /FormEX['.$this->ur['formex'].']';
  12764. }
  12765. }
  12766. $out .= ' >>'; // close TransformParams
  12767. // optional digest data (values must be calculated and replaced later)
  12768. //$out .= ' /Data ********** 0 R';
  12769. //$out .= ' /DigestMethod/MD5';
  12770. //$out .= ' /DigestLocation[********** 34]';
  12771. //$out .= ' /DigestValue<********************************>';
  12772. $out .= ' >>';
  12773. $out .= ' ]'; // end of reference
  12774. if (isset($this->signature_data['info']['Name']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Name'])) {
  12775. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  12776. }
  12777. if (isset($this->signature_data['info']['Location']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Location'])) {
  12778. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  12779. }
  12780. if (isset($this->signature_data['info']['Reason']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Reason'])) {
  12781. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  12782. }
  12783. if (isset($this->signature_data['info']['ContactInfo']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['ContactInfo'])) {
  12784. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  12785. }
  12786. $out .= ' /M '.$this->_datestring($sigobjid, $this->doc_modification_timestamp);
  12787. $out .= ' >>';
  12788. $out .= "\n".'endobj';
  12789. $this->_out($out);
  12790. }
  12791. /**
  12792. * Set User's Rights for PDF Reader
  12793. * WARNING: This is experimental and currently do not work.
  12794. * Check the PDF Reference 8.7.1 Transform Methods,
  12795. * Table 8.105 Entries in the UR transform parameters dictionary
  12796. * @param $enable (boolean) if true enable user's rights on PDF reader
  12797. * @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.
  12798. * @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.
  12799. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  12800. * @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.
  12801. * @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
  12802. Names specifying additional embedded-files-related usage rights for the document.
  12803. * @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.
  12804. * @public
  12805. * @author Nicola Asuni
  12806. * @since 2.9.000 (2008-03-26)
  12807. */
  12808. public function setUserRights(
  12809. $enable=true,
  12810. $document='/FullSave',
  12811. $annots='/Create/Delete/Modify/Copy/Import/Export',
  12812. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  12813. $signature='/Modify',
  12814. $ef='/Create/Delete/Modify/Import',
  12815. $formex='') {
  12816. $this->ur['enabled'] = $enable;
  12817. $this->ur['document'] = $document;
  12818. $this->ur['annots'] = $annots;
  12819. $this->ur['form'] = $form;
  12820. $this->ur['signature'] = $signature;
  12821. $this->ur['ef'] = $ef;
  12822. $this->ur['formex'] = $formex;
  12823. if (!$this->sign) {
  12824. $this->setSignature('', '', '', '', 0, array());
  12825. }
  12826. }
  12827. /**
  12828. * Enable document signature (requires the OpenSSL Library).
  12829. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  12830. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12831. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12832. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  12833. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  12834. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  12835. * @param $private_key_password (string) password
  12836. * @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.
  12837. * @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.
  12838. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  12839. * @public
  12840. * @author Nicola Asuni
  12841. * @since 4.6.005 (2009-04-24)
  12842. */
  12843. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  12844. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12845. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12846. // to convert pfx certificate to pem: openssl
  12847. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  12848. $this->sign = true;
  12849. ++$this->n;
  12850. $this->sig_obj_id = $this->n; // signature widget
  12851. ++$this->n; // signature object ($this->sig_obj_id + 1)
  12852. $this->signature_data = array();
  12853. if (strlen($signing_cert) == 0) {
  12854. $this->Error('Please provide a certificate file and password!');
  12855. }
  12856. if (strlen($private_key) == 0) {
  12857. $private_key = $signing_cert;
  12858. }
  12859. $this->signature_data['signcert'] = $signing_cert;
  12860. $this->signature_data['privkey'] = $private_key;
  12861. $this->signature_data['password'] = $private_key_password;
  12862. $this->signature_data['extracerts'] = $extracerts;
  12863. $this->signature_data['cert_type'] = $cert_type;
  12864. $this->signature_data['info'] = $info;
  12865. }
  12866. /**
  12867. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  12868. * @param $x (float) Abscissa of the upper-left corner.
  12869. * @param $y (float) Ordinate of the upper-left corner.
  12870. * @param $w (float) Width of the signature area.
  12871. * @param $h (float) Height of the signature area.
  12872. * @param $page (int) option page number (if < 0 the current page is used).
  12873. * @param $name (string) Name of the signature.
  12874. * @public
  12875. * @author Nicola Asuni
  12876. * @since 5.3.011 (2010-06-17)
  12877. */
  12878. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12879. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12880. }
  12881. /**
  12882. * Add an empty 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.9.101 (2011-07-06)
  12892. */
  12893. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12894. ++$this->n;
  12895. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12896. }
  12897. /**
  12898. * Get the array that defines the signature appearance (page and rectangle coordinates).
  12899. * @param $x (float) Abscissa of the upper-left corner.
  12900. * @param $y (float) Ordinate of the upper-left corner.
  12901. * @param $w (float) Width of the signature area.
  12902. * @param $h (float) Height of the signature area.
  12903. * @param $page (int) option page number (if < 0 the current page is used).
  12904. * @param $name (string) Name of the signature.
  12905. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  12906. * @protected
  12907. * @author Nicola Asuni
  12908. * @since 5.9.101 (2011-07-06)
  12909. */
  12910. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12911. $sigapp = array();
  12912. if (($page < 1) OR ($page > $this->numpages)) {
  12913. $sigapp['page'] = $this->page;
  12914. } else {
  12915. $sigapp['page'] = intval($page);
  12916. }
  12917. if (empty($name)) {
  12918. $sigapp['name'] = 'Signature';
  12919. } else {
  12920. $sigapp['name'] = $name;
  12921. }
  12922. $a = $x * $this->k;
  12923. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  12924. $c = $w * $this->k;
  12925. $d = $h * $this->k;
  12926. $sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
  12927. return $sigapp;
  12928. }
  12929. /**
  12930. * Create a new page group.
  12931. * NOTE: call this function before calling AddPage()
  12932. * @param $page (int) starting group page (leave empty for next page).
  12933. * @public
  12934. * @since 3.0.000 (2008-03-27)
  12935. */
  12936. public function startPageGroup($page='') {
  12937. if (empty($page)) {
  12938. $page = $this->page + 1;
  12939. }
  12940. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  12941. }
  12942. /**
  12943. * Set the starting page number.
  12944. * @param $num (int) Starting page number.
  12945. * @since 5.9.093 (2011-06-16)
  12946. * @public
  12947. */
  12948. public function setStartingPageNumber($num=1) {
  12949. $this->starting_page_number = max(0, intval($num));
  12950. }
  12951. /**
  12952. * Returns the string alias used right align page numbers.
  12953. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  12954. * @return string
  12955. * @since 5.9.099 (2011-06-27)
  12956. * @public
  12957. */
  12958. public function getAliasRightShift() {
  12959. // calculate aproximatively the ratio between widths of aliases and replacements.
  12960. $ref = '{'.TCPDF_STATIC::$alias_right_shift.'}{'.TCPDF_STATIC::$alias_tot_pages.'}{'.TCPDF_STATIC::$alias_num_page.'}';
  12961. $rep = str_repeat(' ', $this->GetNumChars($ref));
  12962. $wrep = $this->GetStringWidth($rep);
  12963. if ($wrep > 0) {
  12964. $wdiff = max(1, ($this->GetStringWidth($ref) / $wrep));
  12965. } else {
  12966. $wdiff = 1;
  12967. }
  12968. $sdiff = sprintf('%F', $wdiff);
  12969. $alias = TCPDF_STATIC::$alias_right_shift.$sdiff.'}';
  12970. if ($this->isUnicodeFont()) {
  12971. $alias = '{'.$alias;
  12972. }
  12973. return $alias;
  12974. }
  12975. /**
  12976. * Returns the string alias used for the total number of pages.
  12977. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  12978. * This alias will be replaced by the total number of pages in the document.
  12979. * @return string
  12980. * @since 4.0.018 (2008-08-08)
  12981. * @public
  12982. */
  12983. public function getAliasNbPages() {
  12984. if ($this->isUnicodeFont()) {
  12985. return '{'.TCPDF_STATIC::$alias_tot_pages.'}';
  12986. }
  12987. return TCPDF_STATIC::$alias_tot_pages;
  12988. }
  12989. /**
  12990. * Returns the string alias used for the page number.
  12991. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  12992. * This alias will be replaced by the page number.
  12993. * @return string
  12994. * @since 4.5.000 (2009-01-02)
  12995. * @public
  12996. */
  12997. public function getAliasNumPage() {
  12998. if ($this->isUnicodeFont()) {
  12999. return '{'.TCPDF_STATIC::$alias_num_page.'}';
  13000. }
  13001. return TCPDF_STATIC::$alias_num_page;
  13002. }
  13003. /**
  13004. * Return the alias for the total number of pages in the current page group.
  13005. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13006. * This alias will be replaced by the total number of pages in this group.
  13007. * @return alias of the current page group
  13008. * @public
  13009. * @since 3.0.000 (2008-03-27)
  13010. */
  13011. public function getPageGroupAlias() {
  13012. if ($this->isUnicodeFont()) {
  13013. return '{'.TCPDF_STATIC::$alias_group_tot_pages.'}';
  13014. }
  13015. return TCPDF_STATIC::$alias_group_tot_pages;
  13016. }
  13017. /**
  13018. * Return the alias for the page number on the current page group.
  13019. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13020. * This alias will be replaced by the page number (relative to the belonging group).
  13021. * @return alias of the current page group
  13022. * @public
  13023. * @since 4.5.000 (2009-01-02)
  13024. */
  13025. public function getPageNumGroupAlias() {
  13026. if ($this->isUnicodeFont()) {
  13027. return '{'.TCPDF_STATIC::$alias_group_num_page.'}';
  13028. }
  13029. return TCPDF_STATIC::$alias_group_num_page;
  13030. }
  13031. /**
  13032. * Return the current page in the group.
  13033. * @return current page in the group
  13034. * @public
  13035. * @since 3.0.000 (2008-03-27)
  13036. */
  13037. public function getGroupPageNo() {
  13038. return $this->pagegroups[$this->currpagegroup];
  13039. }
  13040. /**
  13041. * Returns the current group page number formatted as a string.
  13042. * @public
  13043. * @since 4.3.003 (2008-11-18)
  13044. * @see PaneNo(), formatPageNumber()
  13045. */
  13046. public function getGroupPageNoFormatted() {
  13047. return TCPDF_STATIC::formatPageNumber($this->getGroupPageNo());
  13048. }
  13049. /**
  13050. * Returns the current page number formatted as a string.
  13051. * @public
  13052. * @since 4.2.005 (2008-11-06)
  13053. * @see PaneNo(), formatPageNumber()
  13054. */
  13055. public function PageNoFormatted() {
  13056. return TCPDF_STATIC::formatPageNumber($this->PageNo());
  13057. }
  13058. /**
  13059. * Put pdf layers.
  13060. * @protected
  13061. * @since 3.0.000 (2008-03-27)
  13062. */
  13063. protected function _putocg() {
  13064. if (empty($this->pdflayers)) {
  13065. return;
  13066. }
  13067. foreach ($this->pdflayers as $key => $layer) {
  13068. $this->pdflayers[$key]['objid'] = $this->_newobj();
  13069. $out = '<< /Type /OCG';
  13070. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  13071. $out .= ' /Usage <<';
  13072. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  13073. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  13074. $out .= ' >> >>';
  13075. $out .= "\n".'endobj';
  13076. $this->_out($out);
  13077. }
  13078. }
  13079. /**
  13080. * Start a new pdf layer.
  13081. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  13082. * @param $print (boolean) Set to true to print this layer.
  13083. * @param $view (boolean) Set to true to view this layer.
  13084. * @public
  13085. * @since 5.9.102 (2011-07-13)
  13086. */
  13087. public function startLayer($name='', $print=true, $view=true) {
  13088. if ($this->state != 2) {
  13089. return;
  13090. }
  13091. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  13092. if (empty($name)) {
  13093. $name = $layer;
  13094. } else {
  13095. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  13096. }
  13097. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
  13098. $this->openMarkedContent = true;
  13099. $this->_out('/OC /'.$layer.' BDC');
  13100. }
  13101. /**
  13102. * End the current PDF layer.
  13103. * @public
  13104. * @since 5.9.102 (2011-07-13)
  13105. */
  13106. public function endLayer() {
  13107. if ($this->state != 2) {
  13108. return;
  13109. }
  13110. if ($this->openMarkedContent) {
  13111. // close existing open marked-content layer
  13112. $this->_out('EMC');
  13113. $this->openMarkedContent = false;
  13114. }
  13115. }
  13116. /**
  13117. * Set the visibility of the successive elements.
  13118. * This can be useful, for instance, to put a background
  13119. * image or color that will show on screen but won't print.
  13120. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  13121. * @public
  13122. * @since 3.0.000 (2008-03-27)
  13123. */
  13124. public function setVisibility($v) {
  13125. if ($this->state != 2) {
  13126. return;
  13127. }
  13128. $this->endLayer();
  13129. switch($v) {
  13130. case 'print': {
  13131. $this->startLayer('Print', true, false);
  13132. break;
  13133. }
  13134. case 'view':
  13135. case 'screen': {
  13136. $this->startLayer('View', false, true);
  13137. break;
  13138. }
  13139. case 'all': {
  13140. $this->_out('');
  13141. break;
  13142. }
  13143. default: {
  13144. $this->Error('Incorrect visibility: '.$v);
  13145. break;
  13146. }
  13147. }
  13148. }
  13149. /**
  13150. * Add transparency parameters to the current extgstate
  13151. * @param $parms (array) parameters
  13152. * @return the number of extgstates
  13153. * @protected
  13154. * @since 3.0.000 (2008-03-27)
  13155. */
  13156. protected function addExtGState($parms) {
  13157. if ($this->pdfa_mode) {
  13158. // transparencies are not allowed in PDF/A mode
  13159. return;
  13160. }
  13161. // check if this ExtGState already exist
  13162. foreach ($this->extgstates as $i => $ext) {
  13163. if ($ext['parms'] == $parms) {
  13164. if ($this->inxobj) {
  13165. // we are inside an XObject template
  13166. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  13167. }
  13168. // return reference to existing ExtGState
  13169. return $i;
  13170. }
  13171. }
  13172. $n = (count($this->extgstates) + 1);
  13173. $this->extgstates[$n] = array('parms' => $parms);
  13174. if ($this->inxobj) {
  13175. // we are inside an XObject template
  13176. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  13177. }
  13178. return $n;
  13179. }
  13180. /**
  13181. * Add an extgstate
  13182. * @param $gs (array) extgstate
  13183. * @protected
  13184. * @since 3.0.000 (2008-03-27)
  13185. */
  13186. protected function setExtGState($gs) {
  13187. if ($this->pdfa_mode OR ($this->state != 2)) {
  13188. // transparency is not allowed in PDF/A mode
  13189. return;
  13190. }
  13191. $this->_out(sprintf('/GS%d gs', $gs));
  13192. }
  13193. /**
  13194. * Put extgstates for object transparency
  13195. * @protected
  13196. * @since 3.0.000 (2008-03-27)
  13197. */
  13198. protected function _putextgstates() {
  13199. foreach ($this->extgstates as $i => $ext) {
  13200. $this->extgstates[$i]['n'] = $this->_newobj();
  13201. $out = '<< /Type /ExtGState';
  13202. foreach ($ext['parms'] as $k => $v) {
  13203. if (is_float($v)) {
  13204. $v = sprintf('%F', $v);
  13205. } elseif ($v === true) {
  13206. $v = 'true';
  13207. } elseif ($v === false) {
  13208. $v = 'false';
  13209. }
  13210. $out .= ' /'.$k.' '.$v;
  13211. }
  13212. $out .= ' >>';
  13213. $out .= "\n".'endobj';
  13214. $this->_out($out);
  13215. }
  13216. }
  13217. /**
  13218. * Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
  13219. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13220. * @param $stroking (boolean) If true apply overprint for stroking operations.
  13221. * @param $nonstroking (boolean) If true apply overprint for painting operations other than stroking.
  13222. * @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).
  13223. * @public
  13224. * @since 5.9.152 (2012-03-23)
  13225. */
  13226. public function setOverprint($stroking=true, $nonstroking='', $mode=0) {
  13227. if ($this->state != 2) {
  13228. return;
  13229. }
  13230. $stroking = $stroking ? true : false;
  13231. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13232. // default value if not set
  13233. $nonstroking = $stroking;
  13234. } else {
  13235. $nonstroking = $nonstroking ? true : false;
  13236. }
  13237. if (($mode != 0) AND ($mode != 1)) {
  13238. $mode = 0;
  13239. }
  13240. $this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
  13241. $gs = $this->addExtGState($this->overprint);
  13242. $this->setExtGState($gs);
  13243. }
  13244. /**
  13245. * Get the overprint mode array (OP, op, OPM).
  13246. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13247. * @return array.
  13248. * @public
  13249. * @since 5.9.152 (2012-03-23)
  13250. */
  13251. public function getOverprint() {
  13252. return $this->overprint;
  13253. }
  13254. /**
  13255. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  13256. * @param $stroking (float) Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
  13257. * @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
  13258. * @param $nonstroking (float) Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
  13259. * @param $ais (boolean)
  13260. * @public
  13261. * @since 3.0.000 (2008-03-27)
  13262. */
  13263. public function setAlpha($stroking=1, $bm='Normal', $nonstroking='', $ais=false) {
  13264. if ($this->pdfa_mode) {
  13265. // transparency is not allowed in PDF/A mode
  13266. return;
  13267. }
  13268. $stroking = floatval($stroking);
  13269. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13270. // default value if not set
  13271. $nonstroking = $stroking;
  13272. } else {
  13273. $nonstroking = floatval($nonstroking);
  13274. }
  13275. if ($bm[0] == '/') {
  13276. // remove trailing slash
  13277. $bm = substr($bm, 1);
  13278. }
  13279. if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  13280. $bm = 'Normal';
  13281. }
  13282. $ais = $ais ? true : false;
  13283. $this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/'.$bm, 'AIS' => $ais);
  13284. $gs = $this->addExtGState($this->alpha);
  13285. $this->setExtGState($gs);
  13286. }
  13287. /**
  13288. * Get the alpha mode array (CA, ca, BM, AIS).
  13289. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13290. * @return array.
  13291. * @public
  13292. * @since 5.9.152 (2012-03-23)
  13293. */
  13294. public function getAlpha() {
  13295. return $this->alpha;
  13296. }
  13297. /**
  13298. * Set the default JPEG compression quality (1-100)
  13299. * @param $quality (int) JPEG quality, integer between 1 and 100
  13300. * @public
  13301. * @since 3.0.000 (2008-03-27)
  13302. */
  13303. public function setJPEGQuality($quality) {
  13304. if (($quality < 1) OR ($quality > 100)) {
  13305. $quality = 75;
  13306. }
  13307. $this->jpeg_quality = intval($quality);
  13308. }
  13309. /**
  13310. * Set the default number of columns in a row for HTML tables.
  13311. * @param $cols (int) number of columns
  13312. * @public
  13313. * @since 3.0.014 (2008-06-04)
  13314. */
  13315. public function setDefaultTableColumns($cols=4) {
  13316. $this->default_table_columns = intval($cols);
  13317. }
  13318. /**
  13319. * Set the height of the cell (line height) respect the font height.
  13320. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  13321. * @public
  13322. * @since 3.0.014 (2008-06-04)
  13323. */
  13324. public function setCellHeightRatio($h) {
  13325. $this->cell_height_ratio = $h;
  13326. }
  13327. /**
  13328. * return the height of cell repect font height.
  13329. * @public
  13330. * @since 4.0.012 (2008-07-24)
  13331. */
  13332. public function getCellHeightRatio() {
  13333. return $this->cell_height_ratio;
  13334. }
  13335. /**
  13336. * Set the PDF version (check PDF reference for valid values).
  13337. * @param $version (string) PDF document version.
  13338. * @public
  13339. * @since 3.1.000 (2008-06-09)
  13340. */
  13341. public function setPDFVersion($version='1.7') {
  13342. if ($this->pdfa_mode) {
  13343. // PDF/A mode
  13344. $this->PDFVersion = '1.4';
  13345. } else {