PageRenderTime 184ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 2ms

/tcpdf/tcpdf.php

https://bitbucket.org/neerav/wp-post-to-pdf
PHP | 13953 lines | 8849 code | 485 blank | 4619 comment | 1692 complexity | 9db76093dbf4e39a1397b6ba36b1a55f MD5 | raw file
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.150
  5. // Begin : 2002-08-03
  6. // Last Update : 2012-03-16
  7. // Author : Nicola Asuni - Tecnick.com LTD - Manor Coach House, Church Hill, Aldershot, Hants, GU12 4RQ, UK - www.tecnick.com - info@tecnick.com
  8. // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2012 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute 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/kerning);
  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-trough 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. // Anyone that has reported a bug or sent a suggestion.
  101. //============================================================+
  102. /**
  103. * @file
  104. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  105. * 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>
  106. * <h3>TCPDF main features are:</h3>
  107. * <ul>
  108. * <li>no external libraries are required for the basic functions;</li>
  109. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  110. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  111. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  112. * <li>font subsetting;</li>
  113. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  114. * <li>images, graphic (geometric figures) and transformation methods;
  115. * <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>
  116. * <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>
  117. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  118. * <li>automatic page header and footer management;</li>
  119. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  120. * <li>transactions to UNDO commands;</li>
  121. * <li>PDF annotations, including links, text and file attachments;</li>
  122. * <li>text rendering modes (fill, stroke and clipping);</li>
  123. * <li>multiple columns mode;</li>
  124. * <li>no-write page regions;</li>
  125. * <li>bookmarks, named destinations and table of content;</li>
  126. * <li>text hyphenation;</li>
  127. * <li>text stretching and spacing (tracking/kerning);</li>
  128. * <li>automatic page break, line break and text alignments including justification;</li>
  129. * <li>automatic page numbering and page groups;</li>
  130. * <li>move and delete pages;</li>
  131. * <li>page compression (requires php-zlib extension);</li>
  132. * <li>XOBject Templates;</li>
  133. * <li>Layers and object visibility;</li>
  134. * <li>PDF/A-1b support.</li>
  135. * </ul>
  136. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  137. * @package com.tecnick.tcpdf
  138. * @author Nicola Asuni
  139. * @version 5.9.150
  140. */
  141. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  142. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  143. /**
  144. * @class TCPDF
  145. * PHP class for generating PDF documents without requiring external extensions.
  146. * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  147. * @package com.tecnick.tcpdf
  148. * @brief PHP class for generating PDF documents without requiring external extensions.
  149. * @version 5.9.150
  150. * @author Nicola Asuni - info@tecnick.com
  151. */
  152. class TCPDF {
  153. // private properties
  154. /**
  155. * Current TCPDF version.
  156. * @private
  157. */
  158. private $tcpdf_version = '5.9.150';
  159. // Protected properties
  160. /**
  161. * Current page number.
  162. * @protected
  163. */
  164. protected $page;
  165. /**
  166. * Current object number.
  167. * @protected
  168. */
  169. protected $n;
  170. /**
  171. * Array of object offsets.
  172. * @protected
  173. */
  174. protected $offsets;
  175. /**
  176. * Buffer holding in-memory PDF.
  177. * @protected
  178. */
  179. protected $buffer;
  180. /**
  181. * Array containing pages.
  182. * @protected
  183. */
  184. protected $pages = array();
  185. /**
  186. * Current document state.
  187. * @protected
  188. */
  189. protected $state;
  190. /**
  191. * Compression flag.
  192. * @protected
  193. */
  194. protected $compress;
  195. /**
  196. * Current page orientation (P = Portrait, L = Landscape).
  197. * @protected
  198. */
  199. protected $CurOrientation;
  200. /**
  201. * Page dimensions.
  202. * @protected
  203. */
  204. protected $pagedim = array();
  205. /**
  206. * Scale factor (number of points in user unit).
  207. * @protected
  208. */
  209. protected $k;
  210. /**
  211. * Width of page format in points.
  212. * @protected
  213. */
  214. protected $fwPt;
  215. /**
  216. * Height of page format in points.
  217. * @protected
  218. */
  219. protected $fhPt;
  220. /**
  221. * Current width of page in points.
  222. * @protected
  223. */
  224. protected $wPt;
  225. /**
  226. * Current height of page in points.
  227. * @protected
  228. */
  229. protected $hPt;
  230. /**
  231. * Current width of page in user unit.
  232. * @protected
  233. */
  234. protected $w;
  235. /**
  236. * Current height of page in user unit.
  237. * @protected
  238. */
  239. protected $h;
  240. /**
  241. * Left margin.
  242. * @protected
  243. */
  244. protected $lMargin;
  245. /**
  246. * Top margin.
  247. * @protected
  248. */
  249. protected $tMargin;
  250. /**
  251. * Right margin.
  252. * @protected
  253. */
  254. protected $rMargin;
  255. /**
  256. * Page break margin.
  257. * @protected
  258. */
  259. protected $bMargin;
  260. /**
  261. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  262. * @since 5.9.000 (2010-10-03)
  263. * @protected
  264. */
  265. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  266. /**
  267. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  268. * @since 5.9.000 (2010-10-04)
  269. * @protected
  270. */
  271. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  272. /**
  273. * Current horizontal position in user unit for cell positioning.
  274. * @protected
  275. */
  276. protected $x;
  277. /**
  278. * Current vertical position in user unit for cell positioning.
  279. * @protected
  280. */
  281. protected $y;
  282. /**
  283. * Height of last cell printed.
  284. * @protected
  285. */
  286. protected $lasth;
  287. /**
  288. * Line width in user unit.
  289. * @protected
  290. */
  291. protected $LineWidth;
  292. /**
  293. * Array of standard font names.
  294. * @protected
  295. */
  296. protected $CoreFonts;
  297. /**
  298. * Array of used fonts.
  299. * @protected
  300. */
  301. protected $fonts = array();
  302. /**
  303. * Array of font files.
  304. * @protected
  305. */
  306. protected $FontFiles = array();
  307. /**
  308. * Array of encoding differences.
  309. * @protected
  310. */
  311. protected $diffs = array();
  312. /**
  313. * Array of used images.
  314. * @protected
  315. */
  316. protected $images = array();
  317. /**
  318. * Array of Annotations in pages.
  319. * @protected
  320. */
  321. protected $PageAnnots = array();
  322. /**
  323. * Array of internal links.
  324. * @protected
  325. */
  326. protected $links = array();
  327. /**
  328. * Current font family.
  329. * @protected
  330. */
  331. protected $FontFamily;
  332. /**
  333. * Current font style.
  334. * @protected
  335. */
  336. protected $FontStyle;
  337. /**
  338. * Current font ascent (distance between font top and baseline).
  339. * @protected
  340. * @since 2.8.000 (2007-03-29)
  341. */
  342. protected $FontAscent;
  343. /**
  344. * Current font descent (distance between font bottom and baseline).
  345. * @protected
  346. * @since 2.8.000 (2007-03-29)
  347. */
  348. protected $FontDescent;
  349. /**
  350. * Underlining flag.
  351. * @protected
  352. */
  353. protected $underline;
  354. /**
  355. * Overlining flag.
  356. * @protected
  357. */
  358. protected $overline;
  359. /**
  360. * Current font info.
  361. * @protected
  362. */
  363. protected $CurrentFont;
  364. /**
  365. * Current font size in points.
  366. * @protected
  367. */
  368. protected $FontSizePt;
  369. /**
  370. * Current font size in user unit.
  371. * @protected
  372. */
  373. protected $FontSize;
  374. /**
  375. * Commands for drawing color.
  376. * @protected
  377. */
  378. protected $DrawColor;
  379. /**
  380. * Commands for filling color.
  381. * @protected
  382. */
  383. protected $FillColor;
  384. /**
  385. * Commands for text color.
  386. * @protected
  387. */
  388. protected $TextColor;
  389. /**
  390. * Indicates whether fill and text colors are different.
  391. * @protected
  392. */
  393. protected $ColorFlag;
  394. /**
  395. * Automatic page breaking.
  396. * @protected
  397. */
  398. protected $AutoPageBreak;
  399. /**
  400. * Threshold used to trigger page breaks.
  401. * @protected
  402. */
  403. protected $PageBreakTrigger;
  404. /**
  405. * Flag set when processing page header.
  406. * @protected
  407. */
  408. protected $InHeader = false;
  409. /**
  410. * Flag set when processing page footer.
  411. * @protected
  412. */
  413. protected $InFooter = false;
  414. /**
  415. * Zoom display mode.
  416. * @protected
  417. */
  418. protected $ZoomMode;
  419. /**
  420. * Layout display mode.
  421. * @protected
  422. */
  423. protected $LayoutMode;
  424. /**
  425. * If true set the document information dictionary in Unicode.
  426. * @protected
  427. */
  428. protected $docinfounicode = true;
  429. /**
  430. * Document title.
  431. * @protected
  432. */
  433. protected $title = '';
  434. /**
  435. * Document subject.
  436. * @protected
  437. */
  438. protected $subject = '';
  439. /**
  440. * Document author.
  441. * @protected
  442. */
  443. protected $author = '';
  444. /**
  445. * Document keywords.
  446. * @protected
  447. */
  448. protected $keywords = '';
  449. /**
  450. * Document creator.
  451. * @protected
  452. */
  453. protected $creator = '';
  454. /**
  455. * Starting page number.
  456. * @protected
  457. */
  458. protected $starting_page_number = 1;
  459. /**
  460. * String alias for total number of pages.
  461. * @protected
  462. */
  463. protected $alias_tot_pages = '{:ptp:}';
  464. /**
  465. * String alias for page number.
  466. * @protected
  467. */
  468. protected $alias_num_page = '{:pnp:}';
  469. /**
  470. * String alias for total number of pages in a single group.
  471. * @protected
  472. */
  473. protected $alias_group_tot_pages = '{:ptg:}';
  474. /**
  475. * String alias for group page number.
  476. * @protected
  477. */
  478. protected $alias_group_num_page = '{:png:}';
  479. /**
  480. * String alias for right shift compensation used to correctly align page numbers on the right.
  481. * @protected
  482. */
  483. protected $alias_right_shift = '{rsc:';
  484. /**
  485. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  486. * @since 2002-07-31
  487. * @author Nicola Asuni
  488. * @protected
  489. */
  490. protected $img_rb_x;
  491. /**
  492. * The right-bottom corner Y coordinate of last inserted image.
  493. * @since 2002-07-31
  494. * @author Nicola Asuni
  495. * @protected
  496. */
  497. protected $img_rb_y;
  498. /**
  499. * Adjusting factor to convert pixels to user units.
  500. * @since 2004-06-14
  501. * @author Nicola Asuni
  502. * @protected
  503. */
  504. protected $imgscale = 1;
  505. /**
  506. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  507. * @since 2005-01-02
  508. * @author Nicola Asuni
  509. * @protected
  510. */
  511. protected $isunicode = false;
  512. /**
  513. * Object containing unicode data.
  514. * @since 5.9.004 (2010-10-18)
  515. * @author Nicola Asuni
  516. * @protected
  517. */
  518. protected $unicode;
  519. /**
  520. * Object containing font encoding maps.
  521. * @since 5.9.123 (2011-10-01)
  522. * @author Nicola Asuni
  523. * @protected
  524. */
  525. protected $encmaps;
  526. /**
  527. * PDF version.
  528. * @since 1.5.3
  529. * @protected
  530. */
  531. protected $PDFVersion = '1.7';
  532. /**
  533. * ID of the stored default header template (-1 = not set).
  534. * @protected
  535. */
  536. protected $header_xobjid = -1;
  537. /**
  538. * If true reset the Header Xobject template at each page
  539. * @protected
  540. */
  541. protected $header_xobj_autoreset = false;
  542. /**
  543. * Minimum distance between header and top page margin.
  544. * @protected
  545. */
  546. protected $header_margin;
  547. /**
  548. * Minimum distance between footer and bottom page margin.
  549. * @protected
  550. */
  551. protected $footer_margin;
  552. /**
  553. * Original left margin value.
  554. * @protected
  555. * @since 1.53.0.TC013
  556. */
  557. protected $original_lMargin;
  558. /**
  559. * Original right margin value.
  560. * @protected
  561. * @since 1.53.0.TC013
  562. */
  563. protected $original_rMargin;
  564. /**
  565. * Default font used on page header.
  566. * @protected
  567. */
  568. protected $header_font;
  569. /**
  570. * Default font used on page footer.
  571. * @protected
  572. */
  573. protected $footer_font;
  574. /**
  575. * Language templates.
  576. * @protected
  577. */
  578. protected $l;
  579. /**
  580. * Barcode to print on page footer (only if set).
  581. * @protected
  582. */
  583. protected $barcode = false;
  584. /**
  585. * Boolean flag to print/hide page header.
  586. * @protected
  587. */
  588. protected $print_header = true;
  589. /**
  590. * Boolean flag to print/hide page footer.
  591. * @protected
  592. */
  593. protected $print_footer = true;
  594. /**
  595. * Header image logo.
  596. * @protected
  597. */
  598. protected $header_logo = '';
  599. /**
  600. * Width of header image logo in user units.
  601. * @protected
  602. */
  603. protected $header_logo_width = 30;
  604. /**
  605. * Title to be printed on default page header.
  606. * @protected
  607. */
  608. protected $header_title = '';
  609. /**
  610. * String to pring on page header after title.
  611. * @protected
  612. */
  613. protected $header_string = '';
  614. /**
  615. * Default number of columns for html table.
  616. * @protected
  617. */
  618. protected $default_table_columns = 4;
  619. // variables for html parser
  620. /**
  621. * HTML PARSER: array to store current link and rendering styles.
  622. * @protected
  623. */
  624. protected $HREF = array();
  625. /**
  626. * List of available fonts on filesystem.
  627. * @protected
  628. */
  629. protected $fontlist = array();
  630. /**
  631. * Current foreground color.
  632. * @protected
  633. */
  634. protected $fgcolor;
  635. /**
  636. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  637. * @protected
  638. */
  639. protected $listordered = array();
  640. /**
  641. * HTML PARSER: array count list items on nested lists.
  642. * @protected
  643. */
  644. protected $listcount = array();
  645. /**
  646. * HTML PARSER: current list nesting level.
  647. * @protected
  648. */
  649. protected $listnum = 0;
  650. /**
  651. * HTML PARSER: indent amount for lists.
  652. * @protected
  653. */
  654. protected $listindent = 0;
  655. /**
  656. * HTML PARSER: current list indententation level.
  657. * @protected
  658. */
  659. protected $listindentlevel = 0;
  660. /**
  661. * Current background color.
  662. * @protected
  663. */
  664. protected $bgcolor;
  665. /**
  666. * Temporary font size in points.
  667. * @protected
  668. */
  669. protected $tempfontsize = 10;
  670. /**
  671. * Spacer string for LI tags.
  672. * @protected
  673. */
  674. protected $lispacer = '';
  675. /**
  676. * Default encoding.
  677. * @protected
  678. * @since 1.53.0.TC010
  679. */
  680. protected $encoding = 'UTF-8';
  681. /**
  682. * PHP internal encoding.
  683. * @protected
  684. * @since 1.53.0.TC016
  685. */
  686. protected $internal_encoding;
  687. /**
  688. * Boolean flag to indicate if the document language is Right-To-Left.
  689. * @protected
  690. * @since 2.0.000
  691. */
  692. protected $rtl = false;
  693. /**
  694. * Boolean flag used to force RTL or LTR string direction.
  695. * @protected
  696. * @since 2.0.000
  697. */
  698. protected $tmprtl = false;
  699. // --- Variables used for document encryption:
  700. /**
  701. * IBoolean flag indicating whether document is protected.
  702. * @protected
  703. * @since 2.0.000 (2008-01-02)
  704. */
  705. protected $encrypted;
  706. /**
  707. * Array containing encryption settings.
  708. * @protected
  709. * @since 5.0.005 (2010-05-11)
  710. */
  711. protected $encryptdata = array();
  712. /**
  713. * Last RC4 key encrypted (cached for optimisation).
  714. * @protected
  715. * @since 2.0.000 (2008-01-02)
  716. */
  717. protected $last_enc_key;
  718. /**
  719. * Last RC4 computed key.
  720. * @protected
  721. * @since 2.0.000 (2008-01-02)
  722. */
  723. protected $last_enc_key_c;
  724. /**
  725. * Encryption padding string.
  726. * @protected
  727. */
  728. protected $enc_padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  729. /**
  730. * File ID (used on document trailer).
  731. * @protected
  732. * @since 5.0.005 (2010-05-12)
  733. */
  734. protected $file_id;
  735. // --- bookmark ---
  736. /**
  737. * Outlines for bookmark.
  738. * @protected
  739. * @since 2.1.002 (2008-02-12)
  740. */
  741. protected $outlines = array();
  742. /**
  743. * Outline root for bookmark.
  744. * @protected
  745. * @since 2.1.002 (2008-02-12)
  746. */
  747. protected $OutlineRoot;
  748. // --- javascript and form ---
  749. /**
  750. * Javascript code.
  751. * @protected
  752. * @since 2.1.002 (2008-02-12)
  753. */
  754. protected $javascript = '';
  755. /**
  756. * Javascript counter.
  757. * @protected
  758. * @since 2.1.002 (2008-02-12)
  759. */
  760. protected $n_js;
  761. /**
  762. * line trough state
  763. * @protected
  764. * @since 2.8.000 (2008-03-19)
  765. */
  766. protected $linethrough;
  767. /**
  768. * Array with additional document-wide usage rights for the document.
  769. * @protected
  770. * @since 5.8.014 (2010-08-23)
  771. */
  772. protected $ur = array();
  773. /**
  774. * DPI (Dot Per Inch) Document Resolution (do not change).
  775. * @protected
  776. * @since 3.0.000 (2008-03-27)
  777. */
  778. protected $dpi = 72;
  779. /**
  780. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  781. * @protected
  782. * @since 3.0.000 (2008-03-27)
  783. */
  784. protected $newpagegroup = array();
  785. /**
  786. * Array that contains the number of pages in each page group.
  787. * @protected
  788. * @since 3.0.000 (2008-03-27)
  789. */
  790. protected $pagegroups = array();
  791. /**
  792. * Current page group number.
  793. * @protected
  794. * @since 3.0.000 (2008-03-27)
  795. */
  796. protected $currpagegroup = 0;
  797. /**
  798. * Array of transparency objects and parameters.
  799. * @protected
  800. * @since 3.0.000 (2008-03-27)
  801. */
  802. protected $extgstates;
  803. /**
  804. * Set the default JPEG compression quality (1-100).
  805. * @protected
  806. * @since 3.0.000 (2008-03-27)
  807. */
  808. protected $jpeg_quality;
  809. /**
  810. * Default cell height ratio.
  811. * @protected
  812. * @since 3.0.014 (2008-05-23)
  813. */
  814. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  815. /**
  816. * PDF viewer preferences.
  817. * @protected
  818. * @since 3.1.000 (2008-06-09)
  819. */
  820. protected $viewer_preferences;
  821. /**
  822. * A name object specifying how the document should be displayed when opened.
  823. * @protected
  824. * @since 3.1.000 (2008-06-09)
  825. */
  826. protected $PageMode;
  827. /**
  828. * Array for storing gradient information.
  829. * @protected
  830. * @since 3.1.000 (2008-06-09)
  831. */
  832. protected $gradients = array();
  833. /**
  834. * Array used to store positions inside the pages buffer (keys are the page numbers).
  835. * @protected
  836. * @since 3.2.000 (2008-06-26)
  837. */
  838. protected $intmrk = array();
  839. /**
  840. * Array used to store positions inside the pages buffer (keys are the page numbers).
  841. * @protected
  842. * @since 5.7.000 (2010-08-03)
  843. */
  844. protected $bordermrk = array();
  845. /**
  846. * Array used to store page positions to track empty pages (keys are the page numbers).
  847. * @protected
  848. * @since 5.8.007 (2010-08-18)
  849. */
  850. protected $emptypagemrk = array();
  851. /**
  852. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  853. * @protected
  854. * @since 4.6.021 (2009-07-20)
  855. */
  856. protected $cntmrk = array();
  857. /**
  858. * Array used to store footer positions of each page.
  859. * @protected
  860. * @since 3.2.000 (2008-07-01)
  861. */
  862. protected $footerpos = array();
  863. /**
  864. * Array used to store footer length of each page.
  865. * @protected
  866. * @since 4.0.014 (2008-07-29)
  867. */
  868. protected $footerlen = array();
  869. /**
  870. * Boolean flag to indicate if a new line is created.
  871. * @protected
  872. * @since 3.2.000 (2008-07-01)
  873. */
  874. protected $newline = true;
  875. /**
  876. * End position of the latest inserted line.
  877. * @protected
  878. * @since 3.2.000 (2008-07-01)
  879. */
  880. protected $endlinex = 0;
  881. /**
  882. * PDF string for width value of the last line.
  883. * @protected
  884. * @since 4.0.006 (2008-07-16)
  885. */
  886. protected $linestyleWidth = '';
  887. /**
  888. * PDF string for CAP value of the last line.
  889. * @protected
  890. * @since 4.0.006 (2008-07-16)
  891. */
  892. protected $linestyleCap = '0 J';
  893. /**
  894. * PDF string for join value of the last line.
  895. * @protected
  896. * @since 4.0.006 (2008-07-16)
  897. */
  898. protected $linestyleJoin = '0 j';
  899. /**
  900. * PDF string for dash value of the last line.
  901. * @protected
  902. * @since 4.0.006 (2008-07-16)
  903. */
  904. protected $linestyleDash = '[] 0 d';
  905. /**
  906. * Boolean flag to indicate if marked-content sequence is open.
  907. * @protected
  908. * @since 4.0.013 (2008-07-28)
  909. */
  910. protected $openMarkedContent = false;
  911. /**
  912. * Count the latest inserted vertical spaces on HTML.
  913. * @protected
  914. * @since 4.0.021 (2008-08-24)
  915. */
  916. protected $htmlvspace = 0;
  917. /**
  918. * Array of Spot colors.
  919. * @protected
  920. * @since 4.0.024 (2008-09-12)
  921. */
  922. protected $spot_colors = array();
  923. /**
  924. * Symbol used for HTML unordered list items.
  925. * @protected
  926. * @since 4.0.028 (2008-09-26)
  927. */
  928. protected $lisymbol = '';
  929. /**
  930. * String used to mark the beginning and end of EPS image blocks.
  931. * @protected
  932. * @since 4.1.000 (2008-10-18)
  933. */
  934. protected $epsmarker = 'x#!#EPS#!#x';
  935. /**
  936. * Array of transformation matrix.
  937. * @protected
  938. * @since 4.2.000 (2008-10-29)
  939. */
  940. protected $transfmatrix = array();
  941. /**
  942. * Current key for transformation matrix.
  943. * @protected
  944. * @since 4.8.005 (2009-09-17)
  945. */
  946. protected $transfmatrix_key = 0;
  947. /**
  948. * Booklet mode for double-sided pages.
  949. * @protected
  950. * @since 4.2.000 (2008-10-29)
  951. */
  952. protected $booklet = false;
  953. /**
  954. * Epsilon value used for float calculations.
  955. * @protected
  956. * @since 4.2.000 (2008-10-29)
  957. */
  958. protected $feps = 0.005;
  959. /**
  960. * Array used for custom vertical spaces for HTML tags.
  961. * @protected
  962. * @since 4.2.001 (2008-10-30)
  963. */
  964. protected $tagvspaces = array();
  965. /**
  966. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  967. * @protected
  968. * @since 4.2.007 (2008-11-12)
  969. */
  970. protected $customlistindent = -1;
  971. /**
  972. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  973. * @protected
  974. * @since 4.2.010 (2008-11-14)
  975. */
  976. protected $opencell = true;
  977. /**
  978. * Array of files to embedd.
  979. * @protected
  980. * @since 4.4.000 (2008-12-07)
  981. */
  982. protected $embeddedfiles = array();
  983. /**
  984. * Boolean flag to indicate if we are inside a PRE tag.
  985. * @protected
  986. * @since 4.4.001 (2008-12-08)
  987. */
  988. protected $premode = false;
  989. /**
  990. * Array used to store positions of graphics transformation blocks inside the page buffer.
  991. * keys are the page numbers
  992. * @protected
  993. * @since 4.4.002 (2008-12-09)
  994. */
  995. protected $transfmrk = array();
  996. /**
  997. * Default color for html links.
  998. * @protected
  999. * @since 4.4.003 (2008-12-09)
  1000. */
  1001. protected $htmlLinkColorArray = array(0, 0, 255);
  1002. /**
  1003. * Default font style to add to html links.
  1004. * @protected
  1005. * @since 4.4.003 (2008-12-09)
  1006. */
  1007. protected $htmlLinkFontStyle = 'U';
  1008. /**
  1009. * Counts the number of pages.
  1010. * @protected
  1011. * @since 4.5.000 (2008-12-31)
  1012. */
  1013. protected $numpages = 0;
  1014. /**
  1015. * Array containing page lengths in bytes.
  1016. * @protected
  1017. * @since 4.5.000 (2008-12-31)
  1018. */
  1019. protected $pagelen = array();
  1020. /**
  1021. * Counts the number of pages.
  1022. * @protected
  1023. * @since 4.5.000 (2008-12-31)
  1024. */
  1025. protected $numimages = 0;
  1026. /**
  1027. * Store the image keys.
  1028. * @protected
  1029. * @since 4.5.000 (2008-12-31)
  1030. */
  1031. protected $imagekeys = array();
  1032. /**
  1033. * Length of the buffer in bytes.
  1034. * @protected
  1035. * @since 4.5.000 (2008-12-31)
  1036. */
  1037. protected $bufferlen = 0;
  1038. /**
  1039. * If true enables disk caching.
  1040. * @protected
  1041. * @since 4.5.000 (2008-12-31)
  1042. */
  1043. protected $diskcache = false;
  1044. /**
  1045. * Counts the number of fonts.
  1046. * @protected
  1047. * @since 4.5.000 (2009-01-02)
  1048. */
  1049. protected $numfonts = 0;
  1050. /**
  1051. * Store the font keys.
  1052. * @protected
  1053. * @since 4.5.000 (2009-01-02)
  1054. */
  1055. protected $fontkeys = array();
  1056. /**
  1057. * Store the font object IDs.
  1058. * @protected
  1059. * @since 4.8.001 (2009-09-09)
  1060. */
  1061. protected $font_obj_ids = array();
  1062. /**
  1063. * Store the fage status (true when opened, false when closed).
  1064. * @protected
  1065. * @since 4.5.000 (2009-01-02)
  1066. */
  1067. protected $pageopen = array();
  1068. /**
  1069. * Default monospace font.
  1070. * @protected
  1071. * @since 4.5.025 (2009-03-10)
  1072. */
  1073. protected $default_monospaced_font = 'courier';
  1074. /**
  1075. * Cloned copy of the current class object.
  1076. * @protected
  1077. * @since 4.5.029 (2009-03-19)
  1078. */
  1079. protected $objcopy;
  1080. /**
  1081. * Array used to store the lengths of cache files.
  1082. * @protected
  1083. * @since 4.5.029 (2009-03-19)
  1084. */
  1085. protected $cache_file_length = array();
  1086. /**
  1087. * Table header content to be repeated on each new page.
  1088. * @protected
  1089. * @since 4.5.030 (2009-03-20)
  1090. */
  1091. protected $thead = '';
  1092. /**
  1093. * Margins used for table header.
  1094. * @protected
  1095. * @since 4.5.030 (2009-03-20)
  1096. */
  1097. protected $theadMargins = array();
  1098. /**
  1099. * Cache array for UTF8StringToArray() method.
  1100. * @protected
  1101. * @since 4.5.037 (2009-04-07)
  1102. */
  1103. protected $cache_UTF8StringToArray = array();
  1104. /**
  1105. * Maximum size of cache array used for UTF8StringToArray() method.
  1106. * @protected
  1107. * @since 4.5.037 (2009-04-07)
  1108. */
  1109. protected $cache_maxsize_UTF8StringToArray = 8;
  1110. /**
  1111. * Current size of cache array used for UTF8StringToArray() method.
  1112. * @protected
  1113. * @since 4.5.037 (2009-04-07)
  1114. */
  1115. protected $cache_size_UTF8StringToArray = 0;
  1116. /**
  1117. * Boolean flag to enable document digital signature.
  1118. * @protected
  1119. * @since 4.6.005 (2009-04-24)
  1120. */
  1121. protected $sign = false;
  1122. /**
  1123. * Digital signature data.
  1124. * @protected
  1125. * @since 4.6.005 (2009-04-24)
  1126. */
  1127. protected $signature_data = array();
  1128. /**
  1129. * Digital signature max length.
  1130. * @protected
  1131. * @since 4.6.005 (2009-04-24)
  1132. */
  1133. protected $signature_max_length = 11742;
  1134. /**
  1135. * Data for digital signature appearance.
  1136. * @protected
  1137. * @since 5.3.011 (2010-06-16)
  1138. */
  1139. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1140. /**
  1141. * Array of empty digital signature appearances.
  1142. * @protected
  1143. * @since 5.9.101 (2011-07-06)
  1144. */
  1145. protected $empty_signature_appearance = array();
  1146. /**
  1147. * Regular expression used to find blank characters (required for word-wrapping).
  1148. * @protected
  1149. * @since 4.6.006 (2009-04-28)
  1150. */
  1151. protected $re_spaces = '/[^\S\xa0]/';
  1152. /**
  1153. * Array of $re_spaces parts.
  1154. * @protected
  1155. * @since 5.5.011 (2010-07-09)
  1156. */
  1157. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1158. /**
  1159. * Digital signature object ID.
  1160. * @protected
  1161. * @since 4.6.022 (2009-06-23)
  1162. */
  1163. protected $sig_obj_id = 0;
  1164. /**
  1165. * ByteRange placemark used during digital signature process.
  1166. * @protected
  1167. * @since 4.6.028 (2009-08-25)
  1168. */
  1169. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1170. /**
  1171. * Placemark used during digital signature process.
  1172. * @protected
  1173. * @since 4.6.028 (2009-08-25)
  1174. */
  1175. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1176. /**
  1177. * ID of page objects.
  1178. * @protected
  1179. * @since 4.7.000 (2009-08-29)
  1180. */
  1181. protected $page_obj_id = array();
  1182. /**
  1183. * List of form annotations IDs.
  1184. * @protected
  1185. * @since 4.8.000 (2009-09-07)
  1186. */
  1187. protected $form_obj_id = array();
  1188. /**
  1189. * 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.
  1190. * @protected
  1191. * @since 4.8.000 (2009-09-07)
  1192. */
  1193. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1194. /**
  1195. * Javascript objects array.
  1196. * @protected
  1197. * @since 4.8.000 (2009-09-07)
  1198. */
  1199. protected $js_objects = array();
  1200. /**
  1201. * Current form action (used during XHTML rendering).
  1202. * @protected
  1203. * @since 4.8.000 (2009-09-07)
  1204. */
  1205. protected $form_action = '';
  1206. /**
  1207. * Current form encryption type (used during XHTML rendering).
  1208. * @protected
  1209. * @since 4.8.000 (2009-09-07)
  1210. */
  1211. protected $form_enctype = 'application/x-www-form-urlencoded';
  1212. /**
  1213. * Current method to submit forms.
  1214. * @protected
  1215. * @since 4.8.000 (2009-09-07)
  1216. */
  1217. protected $form_mode = 'post';
  1218. /**
  1219. * List of fonts used on form fields (fontname => fontkey).
  1220. * @protected
  1221. * @since 4.8.001 (2009-09-09)
  1222. */
  1223. protected $annotation_fonts = array();
  1224. /**
  1225. * List of radio buttons parent objects.
  1226. * @protected
  1227. * @since 4.8.001 (2009-09-09)
  1228. */
  1229. protected $radiobutton_groups = array();
  1230. /**
  1231. * List of radio group objects IDs.
  1232. * @protected
  1233. * @since 4.8.001 (2009-09-09)
  1234. */
  1235. protected $radio_groups = array();
  1236. /**
  1237. * Text indentation value (used for text-indent CSS attribute).
  1238. * @protected
  1239. * @since 4.8.006 (2009-09-23)
  1240. */
  1241. protected $textindent = 0;
  1242. /**
  1243. * Store page number when startTransaction() is called.
  1244. * @protected
  1245. * @since 4.8.006 (2009-09-23)
  1246. */
  1247. protected $start_transaction_page = 0;
  1248. /**
  1249. * Store Y position when startTransaction() is called.
  1250. * @protected
  1251. * @since 4.9.001 (2010-03-28)
  1252. */
  1253. protected $start_transaction_y = 0;
  1254. /**
  1255. * True when we are printing the thead section on a new page.
  1256. * @protected
  1257. * @since 4.8.027 (2010-01-25)
  1258. */
  1259. protected $inthead = false;
  1260. /**
  1261. * Array of column measures (width, space, starting Y position).
  1262. * @protected
  1263. * @since 4.9.001 (2010-03-28)
  1264. */
  1265. protected $columns = array();
  1266. /**
  1267. * Number of colums.
  1268. * @protected
  1269. * @since 4.9.001 (2010-03-28)
  1270. */
  1271. protected $num_columns = 1;
  1272. /**
  1273. * Current column number.
  1274. * @protected
  1275. * @since 4.9.001 (2010-03-28)
  1276. */
  1277. protected $current_column = 0;
  1278. /**
  1279. * Starting page for columns.
  1280. * @protected
  1281. * @since 4.9.001 (2010-03-28)
  1282. */
  1283. protected $column_start_page = 0;
  1284. /**
  1285. * Maximum page and column selected.
  1286. * @protected
  1287. * @since 5.8.000 (2010-08-11)
  1288. */
  1289. protected $maxselcol = array('page' => 0, 'column' => 0);
  1290. /**
  1291. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1292. * @protected
  1293. * @since 5.8.000 (2010-08-11)
  1294. */
  1295. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1296. /**
  1297. * 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.
  1298. * @protected
  1299. * @since 4.9.008 (2010-04-03)
  1300. */
  1301. protected $textrendermode = 0;
  1302. /**
  1303. * Text stroke width in doc units.
  1304. * @protected
  1305. * @since 4.9.008 (2010-04-03)
  1306. */
  1307. protected $textstrokewidth = 0;
  1308. /**
  1309. * Current stroke color.
  1310. * @protected
  1311. * @since 4.9.008 (2010-04-03)
  1312. */
  1313. protected $strokecolor;
  1314. /**
  1315. * Default unit of measure for document.
  1316. * @protected
  1317. * @since 5.0.000 (2010-04-22)
  1318. */
  1319. protected $pdfunit = 'mm';
  1320. /**
  1321. * Boolean flag true when we are on TOC (Table Of Content) page.
  1322. * @protected
  1323. */
  1324. protected $tocpage = false;
  1325. /**
  1326. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1327. * @protected
  1328. * @since 5.0.000 (2010-04-26)
  1329. */
  1330. protected $rasterize_vector_images = false;
  1331. /**
  1332. * Boolean flag: if true enables font subsetting by default.
  1333. * @protected
  1334. * @since 5.3.002 (2010-06-07)
  1335. */
  1336. protected $font_subsetting = true;
  1337. /**
  1338. * Array of default graphic settings.
  1339. * @protected
  1340. * @since 5.5.008 (2010-07-02)
  1341. */
  1342. protected $default_graphic_vars = array();
  1343. /**
  1344. * Array of XObjects.
  1345. * @protected
  1346. * @since 5.8.014 (2010-08-23)
  1347. */
  1348. protected $xobjects = array();
  1349. /**
  1350. * Boolean value true when we are inside an XObject.
  1351. * @protected
  1352. * @since 5.8.017 (2010-08-24)
  1353. */
  1354. protected $inxobj = false;
  1355. /**
  1356. * Current XObject ID.
  1357. * @protected
  1358. * @since 5.8.017 (2010-08-24)
  1359. */
  1360. protected $xobjid = '';
  1361. /**
  1362. * Percentage of character stretching.
  1363. * @protected
  1364. * @since 5.9.000 (2010-09-29)
  1365. */
  1366. protected $font_stretching = 100;
  1367. /**
  1368. * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
  1369. * @protected
  1370. * @since 5.9.000 (2010-09-29)
  1371. */
  1372. protected $font_spacing = 0;
  1373. /**
  1374. * Array of no-write regions.
  1375. * ('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)
  1376. * @protected
  1377. * @since 5.9.003 (2010-10-14)
  1378. */
  1379. protected $page_regions = array();
  1380. /**
  1381. * Array containing HTML color names and values.
  1382. * @protected
  1383. * @since 5.9.004 (2010-10-18)
  1384. */
  1385. protected $webcolor = array();
  1386. /**
  1387. * Array containing spot color names and values.
  1388. * @protected
  1389. * @since 5.9.012 (2010-11-11)
  1390. */
  1391. protected $spotcolor = array();
  1392. /**
  1393. * Array of PDF layers data.
  1394. * @protected
  1395. * @since 5.9.102 (2011-07-13)
  1396. */
  1397. protected $pdflayers = array();
  1398. /**
  1399. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1400. * @protected
  1401. * @since 5.9.097 (2011-06-23)
  1402. */
  1403. protected $dests = array();
  1404. /**
  1405. * Object ID for Named Destinations
  1406. * @protected
  1407. * @since 5.9.097 (2011-06-23)
  1408. */
  1409. protected $n_dests;
  1410. /**
  1411. * Directory used for the last SVG image.
  1412. * @protected
  1413. * @since 5.0.000 (2010-05-05)
  1414. */
  1415. protected $svgdir = '';
  1416. /**
  1417. * Deafult unit of measure for SVG.
  1418. * @protected
  1419. * @since 5.0.000 (2010-05-02)
  1420. */
  1421. protected $svgunit = 'px';
  1422. /**
  1423. * Array of SVG gradients.
  1424. * @protected
  1425. * @since 5.0.000 (2010-05-02)
  1426. */
  1427. protected $svggradients = array();
  1428. /**
  1429. * ID of last SVG gradient.
  1430. * @protected
  1431. * @since 5.0.000 (2010-05-02)
  1432. */
  1433. protected $svggradientid = 0;
  1434. /**
  1435. * Boolean value true when in SVG defs group.
  1436. * @protected
  1437. * @since 5.0.000 (2010-05-02)
  1438. */
  1439. protected $svgdefsmode = false;
  1440. /**
  1441. * Array of SVG defs.
  1442. * @protected
  1443. * @since 5.0.000 (2010-05-02)
  1444. */
  1445. protected $svgdefs = array();
  1446. /**
  1447. * Boolean value true when in SVG clipPath tag.
  1448. * @protected
  1449. * @since 5.0.000 (2010-04-26)
  1450. */
  1451. protected $svgclipmode = false;
  1452. /**
  1453. * Array of SVG clipPath commands.
  1454. * @protected
  1455. * @since 5.0.000 (2010-05-02)
  1456. */
  1457. protected $svgclippaths = array();
  1458. /**
  1459. * Array of SVG clipPath tranformation matrix.
  1460. * @protected
  1461. * @since 5.8.022 (2010-08-31)
  1462. */
  1463. protected $svgcliptm = array();
  1464. /**
  1465. * ID of last SVG clipPath.
  1466. * @protected
  1467. * @since 5.0.000 (2010-05-02)
  1468. */
  1469. protected $svgclipid = 0;
  1470. /**
  1471. * SVG text.
  1472. * @protected
  1473. * @since 5.0.000 (2010-05-02)
  1474. */
  1475. protected $svgtext = '';
  1476. /**
  1477. * SVG text properties.
  1478. * @protected
  1479. * @since 5.8.013 (2010-08-23)
  1480. */
  1481. protected $svgtextmode = array();
  1482. /**
  1483. * Array of hinheritable SVG properties.
  1484. * @protected
  1485. * @since 5.0.000 (2010-05-02)
  1486. */
  1487. protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
  1488. /**
  1489. * Array of SVG properties.
  1490. * @protected
  1491. * @since 5.0.000 (2010-05-02)
  1492. */
  1493. protected $svgstyles = array(array(
  1494. 'alignment-baseline' => 'auto',
  1495. 'baseline-shift' => 'baseline',
  1496. 'clip' => 'auto',
  1497. 'clip-path' => 'none',
  1498. 'clip-rule' => 'nonzero',
  1499. 'color' => 'black',
  1500. 'color-interpolation' => 'sRGB',
  1501. 'color-interpolation-filters' => 'linearRGB',
  1502. 'color-profile' => 'auto',
  1503. 'color-rendering' => 'auto',
  1504. 'cursor' => 'auto',
  1505. 'direction' => 'ltr',
  1506. 'display' => 'inline',
  1507. 'dominant-baseline' => 'auto',
  1508. 'enable-background' => 'accumulate',
  1509. 'fill' => 'black',
  1510. 'fill-opacity' => 1,
  1511. 'fill-rule' => 'nonzero',
  1512. 'filter' => 'none',
  1513. 'flood-color' => 'black',
  1514. 'flood-opacity' => 1,
  1515. 'font' => '',
  1516. 'font-family' => 'helvetica',
  1517. 'font-size' => 'medium',
  1518. 'font-size-adjust' => 'none',
  1519. 'font-stretch' => 'normal',
  1520. 'font-style' => 'normal',
  1521. 'font-variant' => 'normal',
  1522. 'font-weight' => 'normal',
  1523. 'glyph-orientation-horizontal' => '0deg',
  1524. 'glyph-orientation-vertical' => 'auto',
  1525. 'image-rendering' => 'auto',
  1526. 'kerning' => 'auto',
  1527. 'letter-spacing' => 'normal',
  1528. 'lighting-color' => 'white',
  1529. 'marker' => '',
  1530. 'marker-end' => 'none',
  1531. 'marker-mid' => 'none',
  1532. 'marker-start' => 'none',
  1533. 'mask' => 'none',
  1534. 'opacity' => 1,
  1535. 'overflow' => 'auto',
  1536. 'pointer-events' => 'visiblePainted',
  1537. 'shape-rendering' => 'auto',
  1538. 'stop-color' => 'black',
  1539. 'stop-opacity' => 1,
  1540. 'stroke' => 'none',
  1541. 'stroke-dasharray' => 'none',
  1542. 'stroke-dashoffset' => 0,
  1543. 'stroke-linecap' => 'butt',
  1544. 'stroke-linejoin' => 'miter',
  1545. 'stroke-miterlimit' => 4,
  1546. 'stroke-opacity' => 1,
  1547. 'stroke-width' => 1,
  1548. 'text-anchor' => 'start',
  1549. 'text-decoration' => 'none',
  1550. 'text-rendering' => 'auto',
  1551. 'unicode-bidi' => 'normal',
  1552. 'visibility' => 'visible',
  1553. 'word-spacing' => 'normal',
  1554. 'writing-mode' => 'lr-tb',
  1555. 'text-color' => 'black',
  1556. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1557. ));
  1558. /**
  1559. * If true force sRGB color profile for all document.
  1560. * @protected
  1561. * @since 5.9.121 (2011-09-28)
  1562. */
  1563. protected $force_srgb = false;
  1564. /**
  1565. * If true set the document to PDF/A mode.
  1566. * @protected
  1567. * @since 5.9.121 (2011-09-27)
  1568. */
  1569. protected $pdfa_mode = false;
  1570. /**
  1571. * Document creation date
  1572. * @protected
  1573. * @since 5.9.121 (2011-09-28)
  1574. */
  1575. protected $doc_date;
  1576. /**
  1577. * Custom XMP data.
  1578. * @protected
  1579. * @since 5.9.128 (2011-10-06)
  1580. */
  1581. protected $custom_xmp = '';
  1582. //------------------------------------------------------------
  1583. // METHODS
  1584. //------------------------------------------------------------
  1585. /**
  1586. * This is the class constructor.
  1587. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1588. * @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>
  1589. * @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.
  1590. * @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().
  1591. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1592. * @param $encoding (string) Charset encoding; default is UTF-8.
  1593. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1594. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1595. * @public
  1596. * @see getPageSizeFromFormat(), setPageFormat()
  1597. */
  1598. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1599. /* Set internal character encoding to ASCII */
  1600. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1601. $this->internal_encoding = mb_internal_encoding();
  1602. mb_internal_encoding('ASCII');
  1603. }
  1604. // get array of HTML colors
  1605. require(dirname(__FILE__).'/htmlcolors.php');
  1606. $this->webcolor = $webcolor;
  1607. // get array of custom spot colors
  1608. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1609. require(dirname(__FILE__).'/spotcolors.php');
  1610. $this->spotcolor = $spotcolor;
  1611. } else {
  1612. $this->spotcolor = array();
  1613. }
  1614. require_once(dirname(__FILE__).'/unicode_data.php');
  1615. $this->unicode = new TCPDF_UNICODE_DATA();
  1616. require_once(dirname(__FILE__).'/encodings_maps.php');
  1617. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1618. $this->font_obj_ids = array();
  1619. $this->page_obj_id = array();
  1620. $this->form_obj_id = array();
  1621. // set pdf/a mode
  1622. $this->pdfa_mode = $pdfa;
  1623. $this->force_srgb = false;
  1624. // set disk caching
  1625. $this->diskcache = $diskcache ? true : false;
  1626. // set language direction
  1627. $this->rtl = false;
  1628. $this->tmprtl = false;
  1629. // some checks
  1630. $this->_dochecks();
  1631. // initialization of properties
  1632. $this->isunicode = $unicode;
  1633. $this->page = 0;
  1634. $this->transfmrk[0] = array();
  1635. $this->pagedim = array();
  1636. $this->n = 2;
  1637. $this->buffer = '';
  1638. $this->pages = array();
  1639. $this->state = 0;
  1640. $this->fonts = array();
  1641. $this->FontFiles = array();
  1642. $this->diffs = array();
  1643. $this->images = array();
  1644. $this->links = array();
  1645. $this->gradients = array();
  1646. $this->InFooter = false;
  1647. $this->lasth = 0;
  1648. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1649. $this->FontStyle = '';
  1650. $this->FontSizePt = 12;
  1651. $this->underline = false;
  1652. $this->overline = false;
  1653. $this->linethrough = false;
  1654. $this->DrawColor = '0 G';
  1655. $this->FillColor = '0 g';
  1656. $this->TextColor = '0 g';
  1657. $this->ColorFlag = false;
  1658. $this->pdflayers = array();
  1659. // encryption values
  1660. $this->encrypted = false;
  1661. $this->last_enc_key = '';
  1662. // standard Unicode fonts
  1663. $this->CoreFonts = array(
  1664. 'courier'=>'Courier',
  1665. 'courierB'=>'Courier-Bold',
  1666. 'courierI'=>'Courier-Oblique',
  1667. 'courierBI'=>'Courier-BoldOblique',
  1668. 'helvetica'=>'Helvetica',
  1669. 'helveticaB'=>'Helvetica-Bold',
  1670. 'helveticaI'=>'Helvetica-Oblique',
  1671. 'helveticaBI'=>'Helvetica-BoldOblique',
  1672. 'times'=>'Times-Roman',
  1673. 'timesB'=>'Times-Bold',
  1674. 'timesI'=>'Times-Italic',
  1675. 'timesBI'=>'Times-BoldItalic',
  1676. 'symbol'=>'Symbol',
  1677. 'zapfdingbats'=>'ZapfDingbats'
  1678. );
  1679. // set scale factor
  1680. $this->setPageUnit($unit);
  1681. // set page format and orientation
  1682. $this->setPageFormat($format, $orientation);
  1683. // page margins (1 cm)
  1684. $margin = 28.35 / $this->k;
  1685. $this->SetMargins($margin, $margin);
  1686. // internal cell padding
  1687. $cpadding = $margin / 10;
  1688. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1689. // cell margins
  1690. $this->setCellMargins(0, 0, 0, 0);
  1691. // line width (0.2 mm)
  1692. $this->LineWidth = 0.57 / $this->k;
  1693. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1694. $this->linestyleCap = '0 J';
  1695. $this->linestyleJoin = '0 j';
  1696. $this->linestyleDash = '[] 0 d';
  1697. // automatic page break
  1698. $this->SetAutoPageBreak(true, (2 * $margin));
  1699. // full width display mode
  1700. $this->SetDisplayMode('fullwidth');
  1701. // compression
  1702. $this->SetCompression();
  1703. // set default PDF version number
  1704. $this->setPDFVersion();
  1705. $this->encoding = $encoding;
  1706. $this->HREF = array();
  1707. $this->getFontsList();
  1708. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1709. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1710. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1711. $this->extgstates = array();
  1712. // user's rights
  1713. $this->sign = false;
  1714. $this->ur['enabled'] = false;
  1715. $this->ur['document'] = '/FullSave';
  1716. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1717. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1718. $this->ur['signature'] = '/Modify';
  1719. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1720. $this->ur['formex'] = '';
  1721. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1722. $this->empty_signature_appearance = array();
  1723. // set default JPEG quality
  1724. $this->jpeg_quality = 75;
  1725. // initialize some settings
  1726. $this->utf8Bidi(array(''), '');
  1727. // set default font
  1728. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1729. // check if PCRE Unicode support is enabled
  1730. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1731. // PCRE unicode support is turned ON
  1732. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1733. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1734. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1735. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1736. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1737. } else {
  1738. // PCRE unicode support is turned OFF
  1739. $this->setSpacesRE('/[^\S\xa0]/');
  1740. }
  1741. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1742. // set file ID for trailer
  1743. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1744. // set document date
  1745. $this->doc_date = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  1746. // get default graphic vars
  1747. $this->default_graphic_vars = $this->getGraphicVars();
  1748. $this->header_xobj_autoreset = false;
  1749. $this->custom_xmp = '';
  1750. }
  1751. /**
  1752. * Default destructor.
  1753. * @public
  1754. * @since 1.53.0.TC016
  1755. */
  1756. public function __destruct() {
  1757. // restore internal encoding
  1758. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1759. mb_internal_encoding($this->internal_encoding);
  1760. }
  1761. // unset all class variables
  1762. $this->_destroy(true);
  1763. }
  1764. /**
  1765. * Return the current TCPDF version.
  1766. * @return TCPDF version string
  1767. * @public
  1768. * @since 5.9.012 (2010-11-10)
  1769. */
  1770. public function getTCPDFVersion() {
  1771. return $this->tcpdf_version;
  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. * Get page dimensions from format name.
  1817. * @param $format (mixed) The format name. It can be: <ul>
  1818. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1819. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1820. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1821. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1822. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1823. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1824. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1825. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1826. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1827. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1828. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1829. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1830. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1831. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1832. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1833. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1834. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1835. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1836. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1837. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1838. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1839. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1840. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1841. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1842. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1843. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1844. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1845. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1846. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1847. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1848. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1849. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1850. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1851. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1852. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1853. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1854. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1855. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1856. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1857. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1858. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1859. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1860. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1861. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1862. * <li><b>SIS 014711 E Series</b></li>
  1863. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1864. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1865. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1866. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1867. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1868. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1869. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1870. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1871. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1872. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1873. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1874. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1875. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1876. * <li><b>SIS 014711 G Series</b></li>
  1877. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1878. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1879. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1880. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1881. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1882. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1883. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1884. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1885. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1886. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1887. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1888. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1889. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1890. * <li><b>ISO Press</b></li>
  1891. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1892. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1893. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1894. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1895. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1896. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1897. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1898. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1899. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1900. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1901. * <li><b>German DIN 476</b></li>
  1902. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1903. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1904. * <li><b>Variations on the ISO Standard</b></li>
  1905. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1906. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1907. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1908. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1909. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1910. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1911. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1912. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1913. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1914. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1915. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1916. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1917. * <li><b>ANSI Series</b></li>
  1918. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1919. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1920. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1921. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1922. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1923. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1924. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1925. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1926. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1927. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1928. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1929. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1930. * <li><b>Other North American Paper Sizes</b></li>
  1931. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1932. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1933. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1934. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1935. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1936. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1937. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1938. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1939. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1940. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1941. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1942. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1943. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1944. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1945. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1946. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1947. * <li><b>North American Architectural Sizes</b></li>
  1948. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1949. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1950. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1951. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1952. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1953. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1954. * <li><b>Announcement Envelopes</b></li>
  1955. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1956. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1957. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1958. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  1959. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  1960. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  1961. * <li><b>Commercial Envelopes</b></li>
  1962. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  1963. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  1964. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  1965. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  1966. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  1967. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  1968. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  1969. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  1970. * <li><b>Catalogue Envelopes</b></li>
  1971. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  1972. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  1973. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  1974. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  1975. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  1976. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  1977. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  1978. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  1979. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  1980. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  1981. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  1982. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  1983. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  1984. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  1985. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  1986. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  1987. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  1988. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  1989. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  1990. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  1991. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  1992. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  1993. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  1994. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  1995. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  1996. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  1997. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  1998. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  1999. * <li><b>PA Series</b></li>
  2000. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2001. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2002. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2003. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2004. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2005. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2006. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2007. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2008. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2009. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2010. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2011. * <li><b>Standard Photographic Print Sizes</b></li>
  2012. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2013. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2014. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2015. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2016. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2017. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2018. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2019. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2020. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2021. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2022. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2023. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2024. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2025. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2026. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2027. * <li><b>Common Newspaper Sizes</b></li>
  2028. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2029. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2030. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2031. * <li><b>Business Cards</b></li>
  2032. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2033. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2034. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2035. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2036. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2037. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2038. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2039. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2040. * <li><b>Billboards</b></li>
  2041. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2042. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2043. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2044. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2045. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2046. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2047. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2048. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2049. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2050. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2051. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2052. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2053. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2054. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2055. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2056. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2057. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2058. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2059. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2060. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2061. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2062. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2063. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2064. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2065. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2066. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2067. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2068. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2069. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2070. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2071. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2072. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2073. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2074. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2075. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2076. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2077. * <li><b>Old Imperial Belgian</b></li>
  2078. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2079. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2080. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2081. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2082. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2083. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2084. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2085. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2086. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2087. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2088. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2089. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2090. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2091. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2092. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2093. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2094. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2095. * <li><b>Old Imperial French</b></li>
  2096. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2097. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2098. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2099. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2100. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2101. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2102. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2103. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2104. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2105. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2106. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2107. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2108. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2109. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2110. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2111. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2112. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2113. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2114. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2115. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2116. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2117. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2118. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2119. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2120. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2121. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2122. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2123. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2124. * </ul>
  2125. * @return array containing page width and height in points
  2126. * @public
  2127. * @since 5.0.010 (2010-05-17)
  2128. */
  2129. public function getPageSizeFromFormat($format) {
  2130. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2131. switch (strtoupper($format)) {
  2132. // ISO 216 A Series + 2 SIS 014711 extensions
  2133. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2134. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2135. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2136. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2137. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2138. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2139. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2140. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2141. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2142. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2143. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2144. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2145. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2146. // ISO 216 B Series + 2 SIS 014711 extensions
  2147. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2148. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2149. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2150. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2151. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2152. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2153. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2154. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2155. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2156. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2157. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2158. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2159. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2160. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2161. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2162. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2163. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2164. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2165. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2166. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2167. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2168. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2169. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2170. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2171. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2172. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2173. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2174. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2175. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2176. // SIS 014711 E Series
  2177. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2178. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2179. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2180. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2181. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2182. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2183. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2184. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2185. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2186. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2187. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2188. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2189. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2190. // SIS 014711 G Series
  2191. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2192. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2193. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2194. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2195. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2196. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2197. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2198. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2199. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2200. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2201. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2202. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2203. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2204. // ISO Press
  2205. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2206. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2207. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2208. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2209. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2210. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2211. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2212. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2213. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2214. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2215. // German DIN 476
  2216. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2217. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2218. // Variations on the ISO Standard
  2219. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2220. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2221. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2222. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2223. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2224. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2225. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2226. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2227. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2228. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2229. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2230. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2231. // ANSI Series
  2232. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2233. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2234. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2235. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2236. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2237. // Traditional 'Loose' North American Paper Sizes
  2238. case 'USLEDGER':
  2239. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2240. case 'ORGANIZERK':
  2241. case 'BIBLE':
  2242. case 'USTABLOID':
  2243. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2244. case 'ORGANIZERM':
  2245. case 'USLETTER':
  2246. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2247. case 'USLEGAL':
  2248. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2249. case 'GOVERNMENTLETTER':
  2250. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2251. case 'JUNIORLEGAL':
  2252. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2253. // Other North American Paper Sizes
  2254. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2255. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2256. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2257. case 'GOVERNMENTLEGAL':
  2258. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2259. case 'MONARCH':
  2260. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2261. case 'ORGANIZERL':
  2262. case 'STATEMENT':
  2263. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2264. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2265. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2266. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2267. // Canadian standard CAN 2-9.60M
  2268. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2269. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2270. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2271. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2272. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2273. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2274. // North American Architectural Sizes
  2275. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2276. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2277. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2278. case 'BROADSHEET':
  2279. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2280. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2281. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2282. // --- North American Envelope Sizes ---
  2283. // - Announcement Envelopes
  2284. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2285. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2286. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2287. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2288. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2289. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2290. // - Commercial Envelopes
  2291. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2292. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2293. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2294. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2295. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2296. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2297. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2298. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2299. // - Catalogue Envelopes
  2300. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2301. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2302. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2303. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2304. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2305. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2306. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2307. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2308. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2309. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2310. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2311. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2312. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2313. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2314. // Japanese (JIS P 0138-61) Standard B-Series
  2315. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2316. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2317. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2318. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2319. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2320. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2321. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2322. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2323. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2324. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2325. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2326. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2327. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2328. // PA Series
  2329. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2330. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2331. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2332. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2333. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2334. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2335. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2336. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2337. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2338. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2339. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2340. // Standard Photographic Print Sizes
  2341. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2342. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2343. case 'L':
  2344. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2345. case 'KG':
  2346. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2347. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2348. case '2L':
  2349. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2350. case '8P':
  2351. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2352. case '6P':
  2353. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2354. case '6PW':
  2355. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2356. case '4P':
  2357. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2358. case '4PW':
  2359. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2360. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2361. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2362. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2363. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2364. // Common Newspaper Sizes
  2365. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2366. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2367. case 'NEWSPAPER_TABLOID':
  2368. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2369. // Business Cards
  2370. case 'CREDIT_CARD':
  2371. case 'BUSINESS_CARD':
  2372. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2373. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2374. case 'BUSINESS_CARD_IT':
  2375. case 'BUSINESS_CARD_UK':
  2376. case 'BUSINESS_CARD_FR':
  2377. case 'BUSINESS_CARD_DE':
  2378. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2379. case 'BUSINESS_CARD_CA':
  2380. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2381. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2382. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2383. case 'BUSINESS_CARD_AU':
  2384. case 'BUSINESS_CARD_DK':
  2385. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2386. case 'BUSINESS_CARD_RU':
  2387. case 'BUSINESS_CARD_CZ':
  2388. case 'BUSINESS_CARD_FI':
  2389. case 'BUSINESS_CARD_HU':
  2390. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2391. // Billboards
  2392. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2393. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2394. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2395. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2396. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2397. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2398. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2399. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2400. // Old European Sizes
  2401. // - Old Imperial English Sizes
  2402. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2403. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2404. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2405. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2406. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2407. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2408. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2409. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2410. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2411. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2412. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2413. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2414. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2415. case 'EN_SHEET':
  2416. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2417. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2418. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2419. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2420. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2421. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2422. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2423. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2424. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2425. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2426. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2427. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2428. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2429. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2430. // - Old Imperial Belgian Sizes
  2431. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2432. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2433. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2434. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2435. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2436. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2437. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2438. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2439. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2440. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2441. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2442. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2443. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2444. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2445. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2446. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2447. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2448. // - Old Imperial French Sizes
  2449. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2450. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2451. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2452. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2453. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2454. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2455. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2456. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2457. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2458. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2459. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2460. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2461. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2462. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2463. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2464. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2465. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2466. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2467. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2468. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2469. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2470. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2471. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2472. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2473. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2474. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2475. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2476. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2477. // DEFAULT ISO A4
  2478. default: {$pf = array( 595.276, 841.890); break;}
  2479. }
  2480. return $pf;
  2481. }
  2482. /**
  2483. * Change the format of the current page
  2484. * @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>
  2485. * <li>['format'] = page format name (one of the above);</li>
  2486. * <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>
  2487. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2488. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2489. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2490. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2491. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2492. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2493. * <li>['CropBox'] : the visible region of default user space:</li>
  2494. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2495. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2496. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2497. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2498. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2499. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2500. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2501. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2502. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2503. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2504. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2505. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2506. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2507. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2508. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2509. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2510. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2511. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2512. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2513. * <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>
  2514. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2515. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2516. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2517. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2518. * <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>
  2519. * <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>
  2520. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2521. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2522. * <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>
  2523. * <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>
  2524. * <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>
  2525. * <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>
  2526. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2527. * </ul>
  2528. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2529. * <li>P or Portrait (default)</li>
  2530. * <li>L or Landscape</li>
  2531. * <li>'' (empty string) for automatic orientation</li>
  2532. * </ul>
  2533. * @protected
  2534. * @since 3.0.015 (2008-06-06)
  2535. * @see getPageSizeFromFormat()
  2536. */
  2537. protected function setPageFormat($format, $orientation='P') {
  2538. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2539. // remove inherited values
  2540. unset($this->pagedim[$this->page]);
  2541. }
  2542. if (is_string($format)) {
  2543. // get page measures from format name
  2544. $pf = $this->getPageSizeFromFormat($format);
  2545. $this->fwPt = $pf[0];
  2546. $this->fhPt = $pf[1];
  2547. } else {
  2548. // the boundaries of the physical medium on which the page shall be displayed or printed
  2549. if (isset($format['MediaBox'])) {
  2550. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2551. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2552. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2553. } else {
  2554. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2555. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2556. } else {
  2557. if (!isset($format['format'])) {
  2558. // default value
  2559. $format['format'] = 'A4';
  2560. }
  2561. $pf = $this->getPageSizeFromFormat($format['format']);
  2562. }
  2563. $this->fwPt = $pf[0];
  2564. $this->fhPt = $pf[1];
  2565. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2566. }
  2567. // the visible region of default user space
  2568. if (isset($format['CropBox'])) {
  2569. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2570. }
  2571. // the region to which the contents of the page shall be clipped when output in a production environment
  2572. if (isset($format['BleedBox'])) {
  2573. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2574. }
  2575. // the intended dimensions of the finished page after trimming
  2576. if (isset($format['TrimBox'])) {
  2577. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2578. }
  2579. // the page's meaningful content (including potential white space)
  2580. if (isset($format['ArtBox'])) {
  2581. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2582. }
  2583. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2584. if (isset($format['BoxColorInfo'])) {
  2585. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2586. }
  2587. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2588. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2589. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2590. }
  2591. if (isset($format['PZ'])) {
  2592. // The page's preferred zoom (magnification) factor
  2593. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2594. }
  2595. if (isset($format['trans'])) {
  2596. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2597. if (isset($format['trans']['Dur'])) {
  2598. // The page's display duration
  2599. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2600. }
  2601. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2602. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2603. // The transition style that shall be used when moving to this page from another during a presentation
  2604. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2605. $valid_effect = array('Split', 'Blinds');
  2606. $valid_vals = array('H', 'V');
  2607. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2608. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2609. }
  2610. $valid_effect = array('Split', 'Box', 'Fly');
  2611. $valid_vals = array('I', 'O');
  2612. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2613. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2614. }
  2615. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2616. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2617. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2618. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2619. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2620. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2621. }
  2622. }
  2623. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2624. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2625. }
  2626. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2627. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2628. }
  2629. } else {
  2630. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2631. }
  2632. if (isset($format['trans']['D'])) {
  2633. // The duration of the transition effect, in seconds
  2634. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2635. } else {
  2636. $this->pagedim[$this->page]['trans']['D'] = 1;
  2637. }
  2638. }
  2639. }
  2640. $this->setPageOrientation($orientation);
  2641. }
  2642. /**
  2643. * Set page boundaries.
  2644. * @param $page (int) page number
  2645. * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
  2646. * @param $llx (float) lower-left x coordinate in user units
  2647. * @param $lly (float) lower-left y coordinate in user units
  2648. * @param $urx (float) upper-right x coordinate in user units
  2649. * @param $ury (float) upper-right y coordinate in user units
  2650. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2651. * @public
  2652. * @since 5.0.010 (2010-05-17)
  2653. */
  2654. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2655. if (!isset($this->pagedim[$page])) {
  2656. // initialize array
  2657. $this->pagedim[$page] = array();
  2658. }
  2659. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2660. if (!in_array($type, $pageboxes)) {
  2661. return;
  2662. }
  2663. if ($points) {
  2664. $k = 1;
  2665. } else {
  2666. $k = $this->k;
  2667. }
  2668. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2669. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2670. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2671. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2672. }
  2673. /**
  2674. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2675. * @param $page (int) page number
  2676. * @protected
  2677. * @since 5.0.010 (2010-05-17)
  2678. */
  2679. protected function swapPageBoxCoordinates($page) {
  2680. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2681. foreach ($pageboxes as $type) {
  2682. // swap X and Y coordinates
  2683. if (isset($this->pagedim[$page][$type])) {
  2684. $tmp = $this->pagedim[$page][$type]['llx'];
  2685. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2686. $this->pagedim[$page][$type]['lly'] = $tmp;
  2687. $tmp = $this->pagedim[$page][$type]['urx'];
  2688. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2689. $this->pagedim[$page][$type]['ury'] = $tmp;
  2690. }
  2691. }
  2692. }
  2693. /**
  2694. * Set page orientation.
  2695. * @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>
  2696. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2697. * @param $bottommargin (float) bottom margin of the page.
  2698. * @public
  2699. * @since 3.0.015 (2008-06-06)
  2700. */
  2701. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2702. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2703. // the boundaries of the physical medium on which the page shall be displayed or printed
  2704. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2705. }
  2706. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2707. // the visible region of default user space
  2708. $this->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);
  2709. }
  2710. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2711. // the region to which the contents of the page shall be clipped when output in a production environment
  2712. $this->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);
  2713. }
  2714. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2715. // the intended dimensions of the finished page after trimming
  2716. $this->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);
  2717. }
  2718. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2719. // the page's meaningful content (including potential white space)
  2720. $this->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);
  2721. }
  2722. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2723. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2724. $this->pagedim[$this->page]['Rotate'] = 0;
  2725. }
  2726. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2727. // The page's preferred zoom (magnification) factor
  2728. $this->pagedim[$this->page]['PZ'] = 1;
  2729. }
  2730. if ($this->fwPt > $this->fhPt) {
  2731. // landscape
  2732. $default_orientation = 'L';
  2733. } else {
  2734. // portrait
  2735. $default_orientation = 'P';
  2736. }
  2737. $valid_orientations = array('P', 'L');
  2738. if (empty($orientation)) {
  2739. $orientation = $default_orientation;
  2740. } else {
  2741. $orientation = strtoupper($orientation{0});
  2742. }
  2743. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2744. $this->CurOrientation = $orientation;
  2745. $this->wPt = $this->fhPt;
  2746. $this->hPt = $this->fwPt;
  2747. } else {
  2748. $this->CurOrientation = $default_orientation;
  2749. $this->wPt = $this->fwPt;
  2750. $this->hPt = $this->fhPt;
  2751. }
  2752. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2753. // swap X and Y coordinates (change page orientation)
  2754. $this->swapPageBoxCoordinates($this->page);
  2755. }
  2756. $this->w = $this->wPt / $this->k;
  2757. $this->h = $this->hPt / $this->k;
  2758. if ($this->empty_string($autopagebreak)) {
  2759. if (isset($this->AutoPageBreak)) {
  2760. $autopagebreak = $this->AutoPageBreak;
  2761. } else {
  2762. $autopagebreak = true;
  2763. }
  2764. }
  2765. if ($this->empty_string($bottommargin)) {
  2766. if (isset($this->bMargin)) {
  2767. $bottommargin = $this->bMargin;
  2768. } else {
  2769. // default value = 2 cm
  2770. $bottommargin = 2 * 28.35 / $this->k;
  2771. }
  2772. }
  2773. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2774. // store page dimensions
  2775. $this->pagedim[$this->page]['w'] = $this->wPt;
  2776. $this->pagedim[$this->page]['h'] = $this->hPt;
  2777. $this->pagedim[$this->page]['wk'] = $this->w;
  2778. $this->pagedim[$this->page]['hk'] = $this->h;
  2779. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2780. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2781. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2782. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2783. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2784. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2785. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2786. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2787. }
  2788. /**
  2789. * Set regular expression to detect withespaces or word separators.
  2790. * The pattern delimiter must be the forward-slash character "/".
  2791. * Some example patterns are:
  2792. * <pre>
  2793. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2794. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2795. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2796. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2797. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2798. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2799. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2800. * </pre>
  2801. * @param $re (string) regular expression (leave empty for default).
  2802. * @public
  2803. * @since 4.6.016 (2009-06-15)
  2804. */
  2805. public function setSpacesRE($re='/[^\S\xa0]/') {
  2806. $this->re_spaces = $re;
  2807. $re_parts = explode('/', $re);
  2808. // get pattern parts
  2809. $this->re_space = array();
  2810. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2811. $this->re_space['p'] = $re_parts[1];
  2812. } else {
  2813. $this->re_space['p'] = '[\s]';
  2814. }
  2815. // set pattern modifiers
  2816. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2817. $this->re_space['m'] = $re_parts[2];
  2818. } else {
  2819. $this->re_space['m'] = '';
  2820. }
  2821. }
  2822. /**
  2823. * Enable or disable Right-To-Left language mode
  2824. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2825. * @param $resetx (Boolean) if true reset the X position on direction change.
  2826. * @public
  2827. * @since 2.0.000 (2008-01-03)
  2828. */
  2829. public function setRTL($enable, $resetx=true) {
  2830. $enable = $enable ? true : false;
  2831. $resetx = ($resetx AND ($enable != $this->rtl));
  2832. $this->rtl = $enable;
  2833. $this->tmprtl = false;
  2834. if ($resetx) {
  2835. $this->Ln(0);
  2836. }
  2837. }
  2838. /**
  2839. * Return the RTL status
  2840. * @return boolean
  2841. * @public
  2842. * @since 4.0.012 (2008-07-24)
  2843. */
  2844. public function getRTL() {
  2845. return $this->rtl;
  2846. }
  2847. /**
  2848. * Force temporary RTL language direction
  2849. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2850. * @public
  2851. * @since 2.1.000 (2008-01-09)
  2852. */
  2853. public function setTempRTL($mode) {
  2854. $newmode = false;
  2855. switch (strtoupper($mode)) {
  2856. case 'LTR':
  2857. case 'L': {
  2858. if ($this->rtl) {
  2859. $newmode = 'L';
  2860. }
  2861. break;
  2862. }
  2863. case 'RTL':
  2864. case 'R': {
  2865. if (!$this->rtl) {
  2866. $newmode = 'R';
  2867. }
  2868. break;
  2869. }
  2870. case false:
  2871. default: {
  2872. $newmode = false;
  2873. break;
  2874. }
  2875. }
  2876. $this->tmprtl = $newmode;
  2877. }
  2878. /**
  2879. * Return the current temporary RTL status
  2880. * @return boolean
  2881. * @public
  2882. * @since 4.8.014 (2009-11-04)
  2883. */
  2884. public function isRTLTextDir() {
  2885. return ($this->rtl OR ($this->tmprtl == 'R'));
  2886. }
  2887. /**
  2888. * Set the last cell height.
  2889. * @param $h (float) cell height.
  2890. * @author Nicola Asuni
  2891. * @public
  2892. * @since 1.53.0.TC034
  2893. */
  2894. public function setLastH($h) {
  2895. $this->lasth = $h;
  2896. }
  2897. /**
  2898. * Reset the last cell height.
  2899. * @public
  2900. * @since 5.9.000 (2010-10-03)
  2901. */
  2902. public function resetLastH() {
  2903. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2904. }
  2905. /**
  2906. * Get the last cell height.
  2907. * @return last cell height
  2908. * @public
  2909. * @since 4.0.017 (2008-08-05)
  2910. */
  2911. public function getLastH() {
  2912. return $this->lasth;
  2913. }
  2914. /**
  2915. * Set the adjusting factor to convert pixels to user units.
  2916. * @param $scale (float) adjusting factor to convert pixels to user units.
  2917. * @author Nicola Asuni
  2918. * @public
  2919. * @since 1.5.2
  2920. */
  2921. public function setImageScale($scale) {
  2922. $this->imgscale = $scale;
  2923. }
  2924. /**
  2925. * Returns the adjusting factor to convert pixels to user units.
  2926. * @return float adjusting factor to convert pixels to user units.
  2927. * @author Nicola Asuni
  2928. * @public
  2929. * @since 1.5.2
  2930. */
  2931. public function getImageScale() {
  2932. return $this->imgscale;
  2933. }
  2934. /**
  2935. * Returns an array of page dimensions:
  2936. * <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>
  2937. * @param $pagenum (int) page number (empty = current page)
  2938. * @return array of page dimensions.
  2939. * @author Nicola Asuni
  2940. * @public
  2941. * @since 4.5.027 (2009-03-16)
  2942. */
  2943. public function getPageDimensions($pagenum='') {
  2944. if (empty($pagenum)) {
  2945. $pagenum = $this->page;
  2946. }
  2947. return $this->pagedim[$pagenum];
  2948. }
  2949. /**
  2950. * Returns the page width in units.
  2951. * @param $pagenum (int) page number (empty = current page)
  2952. * @return int page width.
  2953. * @author Nicola Asuni
  2954. * @public
  2955. * @since 1.5.2
  2956. * @see getPageDimensions()
  2957. */
  2958. public function getPageWidth($pagenum='') {
  2959. if (empty($pagenum)) {
  2960. return $this->w;
  2961. }
  2962. return $this->pagedim[$pagenum]['w'];
  2963. }
  2964. /**
  2965. * Returns the page height in units.
  2966. * @param $pagenum (int) page number (empty = current page)
  2967. * @return int page height.
  2968. * @author Nicola Asuni
  2969. * @public
  2970. * @since 1.5.2
  2971. * @see getPageDimensions()
  2972. */
  2973. public function getPageHeight($pagenum='') {
  2974. if (empty($pagenum)) {
  2975. return $this->h;
  2976. }
  2977. return $this->pagedim[$pagenum]['h'];
  2978. }
  2979. /**
  2980. * Returns the page break margin.
  2981. * @param $pagenum (int) page number (empty = current page)
  2982. * @return int page break margin.
  2983. * @author Nicola Asuni
  2984. * @public
  2985. * @since 1.5.2
  2986. * @see getPageDimensions()
  2987. */
  2988. public function getBreakMargin($pagenum='') {
  2989. if (empty($pagenum)) {
  2990. return $this->bMargin;
  2991. }
  2992. return $this->pagedim[$pagenum]['bm'];
  2993. }
  2994. /**
  2995. * Returns the scale factor (number of points in user unit).
  2996. * @return int scale factor.
  2997. * @author Nicola Asuni
  2998. * @public
  2999. * @since 1.5.2
  3000. */
  3001. public function getScaleFactor() {
  3002. return $this->k;
  3003. }
  3004. /**
  3005. * Defines the left, top and right margins.
  3006. * @param $left (float) Left margin.
  3007. * @param $top (float) Top margin.
  3008. * @param $right (float) Right margin. Default value is the left one.
  3009. * @param $keepmargins (boolean) if true overwrites the default page margins
  3010. * @public
  3011. * @since 1.0
  3012. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3013. */
  3014. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3015. //Set left, top and right margins
  3016. $this->lMargin = $left;
  3017. $this->tMargin = $top;
  3018. if ($right == -1) {
  3019. $right = $left;
  3020. }
  3021. $this->rMargin = $right;
  3022. if ($keepmargins) {
  3023. // overwrite original values
  3024. $this->original_lMargin = $this->lMargin;
  3025. $this->original_rMargin = $this->rMargin;
  3026. }
  3027. }
  3028. /**
  3029. * 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.
  3030. * @param $margin (float) The margin.
  3031. * @public
  3032. * @since 1.4
  3033. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3034. */
  3035. public function SetLeftMargin($margin) {
  3036. //Set left margin
  3037. $this->lMargin = $margin;
  3038. if (($this->page > 0) AND ($this->x < $margin)) {
  3039. $this->x = $margin;
  3040. }
  3041. }
  3042. /**
  3043. * Defines the top margin. The method can be called before creating the first page.
  3044. * @param $margin (float) The margin.
  3045. * @public
  3046. * @since 1.5
  3047. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3048. */
  3049. public function SetTopMargin($margin) {
  3050. //Set top margin
  3051. $this->tMargin = $margin;
  3052. if (($this->page > 0) AND ($this->y < $margin)) {
  3053. $this->y = $margin;
  3054. }
  3055. }
  3056. /**
  3057. * Defines the right margin. The method can be called before creating the first page.
  3058. * @param $margin (float) The margin.
  3059. * @public
  3060. * @since 1.5
  3061. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3062. */
  3063. public function SetRightMargin($margin) {
  3064. $this->rMargin = $margin;
  3065. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3066. $this->x = $this->w - $margin;
  3067. }
  3068. }
  3069. /**
  3070. * Set the same internal Cell padding for top, right, bottom, left-
  3071. * @param $pad (float) internal padding.
  3072. * @public
  3073. * @since 2.1.000 (2008-01-09)
  3074. * @see getCellPaddings(), setCellPaddings()
  3075. */
  3076. public function SetCellPadding($pad) {
  3077. if ($pad >= 0) {
  3078. $this->cell_padding['L'] = $pad;
  3079. $this->cell_padding['T'] = $pad;
  3080. $this->cell_padding['R'] = $pad;
  3081. $this->cell_padding['B'] = $pad;
  3082. }
  3083. }
  3084. /**
  3085. * Set the internal Cell paddings.
  3086. * @param $left (float) left padding
  3087. * @param $top (float) top padding
  3088. * @param $right (float) right padding
  3089. * @param $bottom (float) bottom padding
  3090. * @public
  3091. * @since 5.9.000 (2010-10-03)
  3092. * @see getCellPaddings(), SetCellPadding()
  3093. */
  3094. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3095. if (($left !== '') AND ($left >= 0)) {
  3096. $this->cell_padding['L'] = $left;
  3097. }
  3098. if (($top !== '') AND ($top >= 0)) {
  3099. $this->cell_padding['T'] = $top;
  3100. }
  3101. if (($right !== '') AND ($right >= 0)) {
  3102. $this->cell_padding['R'] = $right;
  3103. }
  3104. if (($bottom !== '') AND ($bottom >= 0)) {
  3105. $this->cell_padding['B'] = $bottom;
  3106. }
  3107. }
  3108. /**
  3109. * Get the internal Cell padding array.
  3110. * @return array of padding values
  3111. * @public
  3112. * @since 5.9.000 (2010-10-03)
  3113. * @see setCellPaddings(), SetCellPadding()
  3114. */
  3115. public function getCellPaddings() {
  3116. return $this->cell_padding;
  3117. }
  3118. /**
  3119. * Set the internal Cell margins.
  3120. * @param $left (float) left margin
  3121. * @param $top (float) top margin
  3122. * @param $right (float) right margin
  3123. * @param $bottom (float) bottom margin
  3124. * @public
  3125. * @since 5.9.000 (2010-10-03)
  3126. * @see getCellMargins()
  3127. */
  3128. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3129. if (($left !== '') AND ($left >= 0)) {
  3130. $this->cell_margin['L'] = $left;
  3131. }
  3132. if (($top !== '') AND ($top >= 0)) {
  3133. $this->cell_margin['T'] = $top;
  3134. }
  3135. if (($right !== '') AND ($right >= 0)) {
  3136. $this->cell_margin['R'] = $right;
  3137. }
  3138. if (($bottom !== '') AND ($bottom >= 0)) {
  3139. $this->cell_margin['B'] = $bottom;
  3140. }
  3141. }
  3142. /**
  3143. * Get the internal Cell margin array.
  3144. * @return array of margin values
  3145. * @public
  3146. * @since 5.9.000 (2010-10-03)
  3147. * @see setCellMargins()
  3148. */
  3149. public function getCellMargins() {
  3150. return $this->cell_margin;
  3151. }
  3152. /**
  3153. * Adjust the internal Cell padding array to take account of the line width.
  3154. * @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)))
  3155. * @return array of adjustments
  3156. * @public
  3157. * @since 5.9.000 (2010-10-03)
  3158. */
  3159. protected function adjustCellPadding($brd=0) {
  3160. if (empty($brd)) {
  3161. return;
  3162. }
  3163. if (is_string($brd)) {
  3164. // convert string to array
  3165. $slen = strlen($brd);
  3166. $newbrd = array();
  3167. for ($i = 0; $i < $slen; ++$i) {
  3168. $newbrd[$brd[$i]] = true;
  3169. }
  3170. $brd = $newbrd;
  3171. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3172. $brd = array('LRTB' => true);
  3173. }
  3174. if (!is_array($brd)) {
  3175. return;
  3176. }
  3177. // store current cell padding
  3178. $cp = $this->cell_padding;
  3179. // select border mode
  3180. if (isset($brd['mode'])) {
  3181. $mode = $brd['mode'];
  3182. unset($brd['mode']);
  3183. } else {
  3184. $mode = 'normal';
  3185. }
  3186. // process borders
  3187. foreach ($brd as $border => $style) {
  3188. $line_width = $this->LineWidth;
  3189. if (is_array($style) AND isset($style['width'])) {
  3190. // get border width
  3191. $line_width = $style['width'];
  3192. }
  3193. $adj = 0; // line width inside the cell
  3194. switch ($mode) {
  3195. case 'ext': {
  3196. $adj = 0;
  3197. break;
  3198. }
  3199. case 'int': {
  3200. $adj = $line_width;
  3201. break;
  3202. }
  3203. case 'normal':
  3204. default: {
  3205. $adj = ($line_width / 2);
  3206. break;
  3207. }
  3208. }
  3209. // correct internal cell padding if required to avoid overlap between text and lines
  3210. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3211. $this->cell_padding['T'] = $adj;
  3212. }
  3213. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3214. $this->cell_padding['R'] = $adj;
  3215. }
  3216. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3217. $this->cell_padding['B'] = $adj;
  3218. }
  3219. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3220. $this->cell_padding['L'] = $adj;
  3221. }
  3222. }
  3223. 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']));
  3224. }
  3225. /**
  3226. * 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.
  3227. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3228. * @param $margin (float) Distance from the bottom of the page.
  3229. * @public
  3230. * @since 1.0
  3231. * @see Cell(), MultiCell(), AcceptPageBreak()
  3232. */
  3233. public function SetAutoPageBreak($auto, $margin=0) {
  3234. $this->AutoPageBreak = $auto ? true : false;
  3235. $this->bMargin = $margin;
  3236. $this->PageBreakTrigger = $this->h - $margin;
  3237. }
  3238. /**
  3239. * Return the auto-page-break mode (true or false).
  3240. * @return boolean auto-page-break mode
  3241. * @public
  3242. * @since 5.9.088
  3243. */
  3244. public function getAutoPageBreak() {
  3245. return $this->AutoPageBreak;
  3246. }
  3247. /**
  3248. * Defines the way the document is to be displayed by the viewer.
  3249. * @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>
  3250. * @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>
  3251. * @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>
  3252. * @public
  3253. * @since 1.2
  3254. */
  3255. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3256. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3257. $this->ZoomMode = $zoom;
  3258. } else {
  3259. $this->Error('Incorrect zoom display mode: '.$zoom);
  3260. }
  3261. switch ($layout) {
  3262. case 'default':
  3263. case 'single':
  3264. case 'SinglePage': {
  3265. $this->LayoutMode = 'SinglePage';
  3266. break;
  3267. }
  3268. case 'continuous':
  3269. case 'OneColumn': {
  3270. $this->LayoutMode = 'OneColumn';
  3271. break;
  3272. }
  3273. case 'two':
  3274. case 'TwoColumnLeft': {
  3275. $this->LayoutMode = 'TwoColumnLeft';
  3276. break;
  3277. }
  3278. case 'TwoColumnRight': {
  3279. $this->LayoutMode = 'TwoColumnRight';
  3280. break;
  3281. }
  3282. case 'TwoPageLeft': {
  3283. $this->LayoutMode = 'TwoPageLeft';
  3284. break;
  3285. }
  3286. case 'TwoPageRight': {
  3287. $this->LayoutMode = 'TwoPageRight';
  3288. break;
  3289. }
  3290. default: {
  3291. $this->LayoutMode = 'SinglePage';
  3292. }
  3293. }
  3294. switch ($mode) {
  3295. case 'UseNone': {
  3296. $this->PageMode = 'UseNone';
  3297. break;
  3298. }
  3299. case 'UseOutlines': {
  3300. $this->PageMode = 'UseOutlines';
  3301. break;
  3302. }
  3303. case 'UseThumbs': {
  3304. $this->PageMode = 'UseThumbs';
  3305. break;
  3306. }
  3307. case 'FullScreen': {
  3308. $this->PageMode = 'FullScreen';
  3309. break;
  3310. }
  3311. case 'UseOC': {
  3312. $this->PageMode = 'UseOC';
  3313. break;
  3314. }
  3315. case '': {
  3316. $this->PageMode = 'UseAttachments';
  3317. break;
  3318. }
  3319. default: {
  3320. $this->PageMode = 'UseNone';
  3321. }
  3322. }
  3323. }
  3324. /**
  3325. * 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.
  3326. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3327. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3328. * @public
  3329. * @since 1.4
  3330. */
  3331. public function SetCompression($compress=true) {
  3332. if (function_exists('gzcompress')) {
  3333. $this->compress = $compress ? true : false;
  3334. } else {
  3335. $this->compress = false;
  3336. }
  3337. }
  3338. /**
  3339. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3340. * @param $mode (boolean) If true force sRGB output intent.
  3341. * @public
  3342. * @since 5.9.121 (2011-09-28)
  3343. */
  3344. public function setSRGBmode($mode=false) {
  3345. $this->force_srgb = $mode ? true : false;
  3346. }
  3347. /**
  3348. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3349. * This has effect only when unicode mode is set to false.
  3350. * @param $unicode (boolean) if true set the meta information in Unicode
  3351. * @since 5.9.027 (2010-12-01)
  3352. * @public
  3353. */
  3354. public function SetDocInfoUnicode($unicode=true) {
  3355. $this->docinfounicode = $unicode ? true : false;
  3356. }
  3357. /**
  3358. * Defines the title of the document.
  3359. * @param $title (string) The title.
  3360. * @public
  3361. * @since 1.2
  3362. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3363. */
  3364. public function SetTitle($title) {
  3365. $this->title = $title;
  3366. }
  3367. /**
  3368. * Defines the subject of the document.
  3369. * @param $subject (string) The subject.
  3370. * @public
  3371. * @since 1.2
  3372. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3373. */
  3374. public function SetSubject($subject) {
  3375. $this->subject = $subject;
  3376. }
  3377. /**
  3378. * Defines the author of the document.
  3379. * @param $author (string) The name of the author.
  3380. * @public
  3381. * @since 1.2
  3382. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3383. */
  3384. public function SetAuthor($author) {
  3385. $this->author = $author;
  3386. }
  3387. /**
  3388. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3389. * @param $keywords (string) The list of keywords.
  3390. * @public
  3391. * @since 1.2
  3392. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3393. */
  3394. public function SetKeywords($keywords) {
  3395. $this->keywords = $keywords;
  3396. }
  3397. /**
  3398. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3399. * @param $creator (string) The name of the creator.
  3400. * @public
  3401. * @since 1.2
  3402. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3403. */
  3404. public function SetCreator($creator) {
  3405. $this->creator = $creator;
  3406. }
  3407. /**
  3408. * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
  3409. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3410. * @param $msg (string) The error message
  3411. * @public
  3412. * @since 1.0
  3413. */
  3414. public function Error($msg) {
  3415. // unset all class variables
  3416. $this->_destroy(true);
  3417. // exit program and print error
  3418. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3419. }
  3420. /**
  3421. * This method begins the generation of the PDF document.
  3422. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3423. * Note: no page is created by this method
  3424. * @public
  3425. * @since 1.0
  3426. * @see AddPage(), Close()
  3427. */
  3428. public function Open() {
  3429. $this->state = 1;
  3430. }
  3431. /**
  3432. * Terminates the PDF document.
  3433. * It is not necessary to call this method explicitly because Output() does it automatically.
  3434. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3435. * @public
  3436. * @since 1.0
  3437. * @see Open(), Output()
  3438. */
  3439. public function Close() {
  3440. if ($this->state == 3) {
  3441. return;
  3442. }
  3443. if ($this->page == 0) {
  3444. $this->AddPage();
  3445. }
  3446. $this->endLayer();
  3447. // save current graphic settings
  3448. $gvars = $this->getGraphicVars();
  3449. $this->setEqualColumns();
  3450. $this->lastpage(true);
  3451. $this->SetAutoPageBreak(false);
  3452. $this->x = 0;
  3453. $this->y = $this->h - (1 / $this->k);
  3454. $this->lMargin = 0;
  3455. $this->_out('q');
  3456. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  3457. $this->SetFont($font, '', 1);
  3458. $this->setTextRenderingMode(0, false, false);
  3459. $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";
  3460. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3461. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3462. $this->_out('Q');
  3463. // restore graphic settings
  3464. $this->setGraphicVars($gvars);
  3465. // close page
  3466. $this->endPage();
  3467. // close document
  3468. $this->_enddoc();
  3469. // unset all class variables (except critical ones)
  3470. $this->_destroy(false);
  3471. }
  3472. /**
  3473. * Move pointer at the specified document page and update page dimensions.
  3474. * @param $pnum (int) page number (1 ... numpages)
  3475. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3476. * @public
  3477. * @since 2.1.000 (2008-01-07)
  3478. * @see getPage(), lastpage(), getNumPages()
  3479. */
  3480. public function setPage($pnum, $resetmargins=false) {
  3481. if (($pnum == $this->page) AND ($this->state == 2)) {
  3482. return;
  3483. }
  3484. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3485. $this->state = 2;
  3486. // save current graphic settings
  3487. //$gvars = $this->getGraphicVars();
  3488. $oldpage = $this->page;
  3489. $this->page = $pnum;
  3490. $this->wPt = $this->pagedim[$this->page]['w'];
  3491. $this->hPt = $this->pagedim[$this->page]['h'];
  3492. $this->w = $this->pagedim[$this->page]['wk'];
  3493. $this->h = $this->pagedim[$this->page]['hk'];
  3494. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3495. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3496. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3497. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3498. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3499. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3500. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3501. // restore graphic settings
  3502. //$this->setGraphicVars($gvars);
  3503. if ($resetmargins) {
  3504. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3505. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3506. $this->SetY($this->tMargin);
  3507. } else {
  3508. // account for booklet mode
  3509. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3510. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3511. $this->lMargin += $deltam;
  3512. $this->rMargin -= $deltam;
  3513. }
  3514. }
  3515. } else {
  3516. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3517. }
  3518. }
  3519. /**
  3520. * Reset pointer to the last document page.
  3521. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3522. * @public
  3523. * @since 2.0.000 (2008-01-04)
  3524. * @see setPage(), getPage(), getNumPages()
  3525. */
  3526. public function lastPage($resetmargins=false) {
  3527. $this->setPage($this->getNumPages(), $resetmargins);
  3528. }
  3529. /**
  3530. * Get current document page number.
  3531. * @return int page number
  3532. * @public
  3533. * @since 2.1.000 (2008-01-07)
  3534. * @see setPage(), lastpage(), getNumPages()
  3535. */
  3536. public function getPage() {
  3537. return $this->page;
  3538. }
  3539. /**
  3540. * Get the total number of insered pages.
  3541. * @return int number of pages
  3542. * @public
  3543. * @since 2.1.000 (2008-01-07)
  3544. * @see setPage(), getPage(), lastpage()
  3545. */
  3546. public function getNumPages() {
  3547. return $this->numpages;
  3548. }
  3549. /**
  3550. * Adds a new TOC (Table Of Content) page to the document.
  3551. * @param $orientation (string) page orientation.
  3552. * @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().
  3553. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3554. * @public
  3555. * @since 5.0.001 (2010-05-06)
  3556. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3557. */
  3558. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3559. $this->AddPage($orientation, $format, $keepmargins, true);
  3560. }
  3561. /**
  3562. * Terminate the current TOC (Table Of Content) page
  3563. * @public
  3564. * @since 5.0.001 (2010-05-06)
  3565. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3566. */
  3567. public function endTOCPage() {
  3568. $this->endPage(true);
  3569. }
  3570. /**
  3571. * 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).
  3572. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3573. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3574. * @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().
  3575. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3576. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3577. * @public
  3578. * @since 1.0
  3579. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3580. */
  3581. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3582. if ($this->inxobj) {
  3583. // we are inside an XObject template
  3584. return;
  3585. }
  3586. if (!isset($this->original_lMargin) OR $keepmargins) {
  3587. $this->original_lMargin = $this->lMargin;
  3588. }
  3589. if (!isset($this->original_rMargin) OR $keepmargins) {
  3590. $this->original_rMargin = $this->rMargin;
  3591. }
  3592. // terminate previous page
  3593. $this->endPage();
  3594. // start new page
  3595. $this->startPage($orientation, $format, $tocpage);
  3596. }
  3597. /**
  3598. * Terminate the current page
  3599. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3600. * @public
  3601. * @since 4.2.010 (2008-11-14)
  3602. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3603. */
  3604. public function endPage($tocpage=false) {
  3605. // check if page is already closed
  3606. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3607. return;
  3608. }
  3609. // print page footer
  3610. $this->setFooter();
  3611. // close page
  3612. $this->_endpage();
  3613. // mark page as closed
  3614. $this->pageopen[$this->page] = false;
  3615. if ($tocpage) {
  3616. $this->tocpage = false;
  3617. }
  3618. }
  3619. /**
  3620. * Starts a new page to the document. The page must be closed using the endPage() function.
  3621. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3622. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3623. * @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().
  3624. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3625. * @since 4.2.010 (2008-11-14)
  3626. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3627. * @public
  3628. */
  3629. public function startPage($orientation='', $format='', $tocpage=false) {
  3630. if ($tocpage) {
  3631. $this->tocpage = true;
  3632. }
  3633. // move page numbers of documents to be attached
  3634. if ($this->tocpage) {
  3635. // move reference to unexistent pages (used for page attachments)
  3636. // adjust outlines
  3637. $tmpoutlines = $this->outlines;
  3638. foreach ($tmpoutlines as $key => $outline) {
  3639. if ($outline['p'] > $this->numpages) {
  3640. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3641. }
  3642. }
  3643. // adjust dests
  3644. $tmpdests = $this->dests;
  3645. foreach ($tmpdests as $key => $dest) {
  3646. if ($dest['p'] > $this->numpages) {
  3647. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3648. }
  3649. }
  3650. // adjust links
  3651. $tmplinks = $this->links;
  3652. foreach ($tmplinks as $key => $link) {
  3653. if ($link[0] > $this->numpages) {
  3654. $this->links[$key][0] = ($link[0] + 1);
  3655. }
  3656. }
  3657. }
  3658. if ($this->numpages > $this->page) {
  3659. // this page has been already added
  3660. $this->setPage($this->page + 1);
  3661. $this->SetY($this->tMargin);
  3662. return;
  3663. }
  3664. // start a new page
  3665. if ($this->state == 0) {
  3666. $this->Open();
  3667. }
  3668. ++$this->numpages;
  3669. $this->swapMargins($this->booklet);
  3670. // save current graphic settings
  3671. $gvars = $this->getGraphicVars();
  3672. // start new page
  3673. $this->_beginpage($orientation, $format);
  3674. // mark page as open
  3675. $this->pageopen[$this->page] = true;
  3676. // restore graphic settings
  3677. $this->setGraphicVars($gvars);
  3678. // mark this point
  3679. $this->setPageMark();
  3680. // print page header
  3681. $this->setHeader();
  3682. // restore graphic settings
  3683. $this->setGraphicVars($gvars);
  3684. // mark this point
  3685. $this->setPageMark();
  3686. // print table header (if any)
  3687. $this->setTableHeader();
  3688. // set mark for empty page check
  3689. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3690. }
  3691. /**
  3692. * Set start-writing mark on current page stream used to put borders and fills.
  3693. * Borders and fills are always created after content and inserted on the position marked by this method.
  3694. * This function must be called after calling Image() function for a background image.
  3695. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3696. * @public
  3697. * @since 4.0.016 (2008-07-30)
  3698. */
  3699. public function setPageMark() {
  3700. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3701. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3702. $this->setContentMark();
  3703. }
  3704. /**
  3705. * Set start-writing mark on selected page.
  3706. * Borders and fills are always created after content and inserted on the position marked by this method.
  3707. * @param $page (int) page number (default is the current page)
  3708. * @protected
  3709. * @since 4.6.021 (2009-07-20)
  3710. */
  3711. protected function setContentMark($page=0) {
  3712. if ($page <= 0) {
  3713. $page = $this->page;
  3714. }
  3715. if (isset($this->footerlen[$page])) {
  3716. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3717. } else {
  3718. $this->cntmrk[$page] = $this->pagelen[$page];
  3719. }
  3720. }
  3721. /**
  3722. * Set header data.
  3723. * @param $ln (string) header image logo
  3724. * @param $lw (string) header image logo width in mm
  3725. * @param $ht (string) string to print as title on document header
  3726. * @param $hs (string) string to print on document header
  3727. * @public
  3728. */
  3729. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3730. $this->header_logo = $ln;
  3731. $this->header_logo_width = $lw;
  3732. $this->header_title = $ht;
  3733. $this->header_string = $hs;
  3734. }
  3735. /**
  3736. * Returns header data:
  3737. * <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>
  3738. * @return array()
  3739. * @public
  3740. * @since 4.0.012 (2008-07-24)
  3741. */
  3742. public function getHeaderData() {
  3743. $ret = array();
  3744. $ret['logo'] = $this->header_logo;
  3745. $ret['logo_width'] = $this->header_logo_width;
  3746. $ret['title'] = $this->header_title;
  3747. $ret['string'] = $this->header_string;
  3748. return $ret;
  3749. }
  3750. /**
  3751. * Set header margin.
  3752. * (minimum distance between header and top page margin)
  3753. * @param $hm (int) distance in user units
  3754. * @public
  3755. */
  3756. public function setHeaderMargin($hm=10) {
  3757. $this->header_margin = $hm;
  3758. }
  3759. /**
  3760. * Returns header margin in user units.
  3761. * @return float
  3762. * @since 4.0.012 (2008-07-24)
  3763. * @public
  3764. */
  3765. public function getHeaderMargin() {
  3766. return $this->header_margin;
  3767. }
  3768. /**
  3769. * Set footer margin.
  3770. * (minimum distance between footer and bottom page margin)
  3771. * @param $fm (int) distance in user units
  3772. * @public
  3773. */
  3774. public function setFooterMargin($fm=10) {
  3775. $this->footer_margin = $fm;
  3776. }
  3777. /**
  3778. * Returns footer margin in user units.
  3779. * @return float
  3780. * @since 4.0.012 (2008-07-24)
  3781. * @public
  3782. */
  3783. public function getFooterMargin() {
  3784. return $this->footer_margin;
  3785. }
  3786. /**
  3787. * Set a flag to print page header.
  3788. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3789. * @public
  3790. */
  3791. public function setPrintHeader($val=true) {
  3792. $this->print_header = $val ? true : false;
  3793. }
  3794. /**
  3795. * Set a flag to print page footer.
  3796. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3797. * @public
  3798. */
  3799. public function setPrintFooter($val=true) {
  3800. $this->print_footer = $val ? true : false;
  3801. }
  3802. /**
  3803. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3804. * @return float
  3805. * @public
  3806. */
  3807. public function getImageRBX() {
  3808. return $this->img_rb_x;
  3809. }
  3810. /**
  3811. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3812. * @return float
  3813. * @public
  3814. */
  3815. public function getImageRBY() {
  3816. return $this->img_rb_y;
  3817. }
  3818. /**
  3819. * Reset the xobject template used by Header() method.
  3820. * @public
  3821. */
  3822. public function resetHeaderTemplate() {
  3823. $this->header_xobjid = -1;
  3824. }
  3825. /**
  3826. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3827. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3828. * @public
  3829. */
  3830. public function setHeaderTemplateAutoreset($val=true) {
  3831. $this->header_xobj_autoreset = $val ? true : false;
  3832. }
  3833. /**
  3834. * This method is used to render the page header.
  3835. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3836. * @public
  3837. */
  3838. public function Header() {
  3839. if ($this->header_xobjid < 0) {
  3840. // start a new XObject Template
  3841. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3842. $headerfont = $this->getHeaderFont();
  3843. $headerdata = $this->getHeaderData();
  3844. $this->y = $this->header_margin;
  3845. if ($this->rtl) {
  3846. $this->x = $this->w - $this->original_rMargin;
  3847. } else {
  3848. $this->x = $this->original_lMargin;
  3849. }
  3850. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3851. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3852. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3853. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3854. } elseif ($imgtype == 'svg') {
  3855. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3856. } else {
  3857. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3858. }
  3859. $imgy = $this->getImageRBY();
  3860. } else {
  3861. $imgy = $this->y;
  3862. }
  3863. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3864. // set starting margin for text data cell
  3865. if ($this->getRTL()) {
  3866. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3867. } else {
  3868. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3869. }
  3870. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3871. $this->SetTextColor(0, 0, 0);
  3872. // header title
  3873. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3874. $this->SetX($header_x);
  3875. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3876. // header string
  3877. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3878. $this->SetX($header_x);
  3879. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3880. // print an ending header line
  3881. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3882. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3883. if ($this->rtl) {
  3884. $this->SetX($this->original_rMargin);
  3885. } else {
  3886. $this->SetX($this->original_lMargin);
  3887. }
  3888. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3889. $this->endTemplate();
  3890. }
  3891. // print header template
  3892. $x = 0;
  3893. $dx = 0;
  3894. if ($this->booklet AND (($this->page % 2) == 0)) {
  3895. // adjust margins for booklet mode
  3896. $dx = ($this->original_lMargin - $this->original_rMargin);
  3897. }
  3898. if ($this->rtl) {
  3899. $x = $this->w + $dx;
  3900. } else {
  3901. $x = 0 + $dx;
  3902. }
  3903. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3904. if ($this->header_xobj_autoreset) {
  3905. // reset header xobject template at each page
  3906. $this->header_xobjid = -1;
  3907. }
  3908. }
  3909. /**
  3910. * This method is used to render the page footer.
  3911. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3912. * @public
  3913. */
  3914. public function Footer() {
  3915. $cur_y = $this->y;
  3916. $this->SetTextColor(0, 0, 0);
  3917. //set style for cell border
  3918. $line_width = 0.85 / $this->k;
  3919. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3920. //print document barcode
  3921. $barcode = $this->getBarcode();
  3922. if (!empty($barcode)) {
  3923. $this->Ln($line_width);
  3924. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3925. $style = array(
  3926. 'position' => $this->rtl?'R':'L',
  3927. 'align' => $this->rtl?'R':'L',
  3928. 'stretch' => false,
  3929. 'fitwidth' => true,
  3930. 'cellfitalign' => '',
  3931. 'border' => false,
  3932. 'padding' => 0,
  3933. 'fgcolor' => array(0,0,0),
  3934. 'bgcolor' => false,
  3935. 'text' => false
  3936. );
  3937. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3938. }
  3939. if (empty($this->pagegroups)) {
  3940. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3941. } else {
  3942. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3943. }
  3944. $this->SetY($cur_y);
  3945. //Print page number
  3946. if ($this->getRTL()) {
  3947. $this->SetX($this->original_rMargin);
  3948. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3949. } else {
  3950. $this->SetX($this->original_lMargin);
  3951. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3952. }
  3953. }
  3954. /**
  3955. * This method is used to render the page header.
  3956. * @protected
  3957. * @since 4.0.012 (2008-07-24)
  3958. */
  3959. protected function setHeader() {
  3960. if (!$this->print_header) {
  3961. return;
  3962. }
  3963. $this->InHeader = true;
  3964. $this->setGraphicVars($this->default_graphic_vars);
  3965. $temp_thead = $this->thead;
  3966. $temp_theadMargins = $this->theadMargins;
  3967. $lasth = $this->lasth;
  3968. $this->_out('q');
  3969. $this->rMargin = $this->original_rMargin;
  3970. $this->lMargin = $this->original_lMargin;
  3971. $this->SetCellPadding(0);
  3972. //set current position
  3973. if ($this->rtl) {
  3974. $this->SetXY($this->original_rMargin, $this->header_margin);
  3975. } else {
  3976. $this->SetXY($this->original_lMargin, $this->header_margin);
  3977. }
  3978. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3979. $this->Header();
  3980. //restore position
  3981. if ($this->rtl) {
  3982. $this->SetXY($this->original_rMargin, $this->tMargin);
  3983. } else {
  3984. $this->SetXY($this->original_lMargin, $this->tMargin);
  3985. }
  3986. $this->_out('Q');
  3987. $this->lasth = $lasth;
  3988. $this->thead = $temp_thead;
  3989. $this->theadMargins = $temp_theadMargins;
  3990. $this->newline = false;
  3991. $this->InHeader = false;
  3992. }
  3993. /**
  3994. * This method is used to render the page footer.
  3995. * @protected
  3996. * @since 4.0.012 (2008-07-24)
  3997. */
  3998. protected function setFooter() {
  3999. //Page footer
  4000. $this->InFooter = true;
  4001. // save current graphic settings
  4002. $gvars = $this->getGraphicVars();
  4003. // mark this point
  4004. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4005. $this->_out("\n");
  4006. if ($this->print_footer) {
  4007. $this->setGraphicVars($this->default_graphic_vars);
  4008. $this->current_column = 0;
  4009. $this->num_columns = 1;
  4010. $temp_thead = $this->thead;
  4011. $temp_theadMargins = $this->theadMargins;
  4012. $lasth = $this->lasth;
  4013. $this->_out('q');
  4014. $this->rMargin = $this->original_rMargin;
  4015. $this->lMargin = $this->original_lMargin;
  4016. $this->SetCellPadding(0);
  4017. //set current position
  4018. $footer_y = $this->h - $this->footer_margin;
  4019. if ($this->rtl) {
  4020. $this->SetXY($this->original_rMargin, $footer_y);
  4021. } else {
  4022. $this->SetXY($this->original_lMargin, $footer_y);
  4023. }
  4024. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4025. $this->Footer();
  4026. //restore position
  4027. if ($this->rtl) {
  4028. $this->SetXY($this->original_rMargin, $this->tMargin);
  4029. } else {
  4030. $this->SetXY($this->original_lMargin, $this->tMargin);
  4031. }
  4032. $this->_out('Q');
  4033. $this->lasth = $lasth;
  4034. $this->thead = $temp_thead;
  4035. $this->theadMargins = $temp_theadMargins;
  4036. }
  4037. // restore graphic settings
  4038. $this->setGraphicVars($gvars);
  4039. $this->current_column = $gvars['current_column'];
  4040. $this->num_columns = $gvars['num_columns'];
  4041. // calculate footer length
  4042. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4043. $this->InFooter = false;
  4044. }
  4045. /**
  4046. * Check if we are on the page body (excluding page header and footer).
  4047. * @return true if we are not in page header nor in page footer, false otherwise.
  4048. * @protected
  4049. * @since 5.9.091 (2011-06-15)
  4050. */
  4051. protected function inPageBody() {
  4052. return (($this->InHeader === false) AND ($this->InFooter === false));
  4053. }
  4054. /**
  4055. * This method is used to render the table header on new page (if any).
  4056. * @protected
  4057. * @since 4.5.030 (2009-03-25)
  4058. */
  4059. protected function setTableHeader() {
  4060. if ($this->num_columns > 1) {
  4061. // multi column mode
  4062. return;
  4063. }
  4064. if (isset($this->theadMargins['top'])) {
  4065. // restore the original top-margin
  4066. $this->tMargin = $this->theadMargins['top'];
  4067. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4068. $this->y = $this->tMargin;
  4069. }
  4070. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4071. // set margins
  4072. $prev_lMargin = $this->lMargin;
  4073. $prev_rMargin = $this->rMargin;
  4074. $prev_cell_padding = $this->cell_padding;
  4075. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4076. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4077. $this->cell_padding = $this->theadMargins['cell_padding'];
  4078. if ($this->rtl) {
  4079. $this->x = $this->w - $this->rMargin;
  4080. } else {
  4081. $this->x = $this->lMargin;
  4082. }
  4083. // account for special "cell" mode
  4084. if ($this->theadMargins['cell']) {
  4085. if ($this->rtl) {
  4086. $this->x -= $this->cell_padding['R'];
  4087. } else {
  4088. $this->x += $this->cell_padding['L'];
  4089. }
  4090. }
  4091. // print table header
  4092. $this->writeHTML($this->thead, false, false, false, false, '');
  4093. // set new top margin to skip the table headers
  4094. if (!isset($this->theadMargins['top'])) {
  4095. $this->theadMargins['top'] = $this->tMargin;
  4096. }
  4097. // store end of header position
  4098. if (!isset($this->columns[0]['th'])) {
  4099. $this->columns[0]['th'] = array();
  4100. }
  4101. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4102. $this->tMargin = $this->y;
  4103. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4104. $this->lasth = 0;
  4105. $this->lMargin = $prev_lMargin;
  4106. $this->rMargin = $prev_rMargin;
  4107. $this->cell_padding = $prev_cell_padding;
  4108. }
  4109. }
  4110. /**
  4111. * Returns the current page number.
  4112. * @return int page number
  4113. * @public
  4114. * @since 1.0
  4115. * @see getAliasNbPages()
  4116. */
  4117. public function PageNo() {
  4118. return $this->page;
  4119. }
  4120. /**
  4121. * Defines a new spot color.
  4122. * It can be expressed in RGB components or gray scale.
  4123. * The method can be called before the first page is created and the value is retained from page to page.
  4124. * @param $name (string) Full name of the spot color.
  4125. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4126. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4127. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4128. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4129. * @public
  4130. * @since 4.0.024 (2008-09-12)
  4131. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4132. */
  4133. public function AddSpotColor($name, $c, $m, $y, $k) {
  4134. if (!isset($this->spot_colors[$name])) {
  4135. $i = (1 + count($this->spot_colors));
  4136. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  4137. }
  4138. }
  4139. /**
  4140. * Return the Spot color array.
  4141. * @param $name (string) Name of the spot color.
  4142. * @return (array) Spot color array or false if not defined.
  4143. * @public
  4144. * @since 5.9.125 (2011-10-03)
  4145. */
  4146. public function getSpotColor($name) {
  4147. if (isset($this->spot_colors[$name])) {
  4148. return $this->spot_colors[$name];
  4149. }
  4150. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4151. $color = strtolower($color);
  4152. if (isset($this->spotcolor[$color])) {
  4153. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4154. return $this->spot_colors[$this->spotcolor[$color][4]];
  4155. }
  4156. return false;
  4157. }
  4158. /**
  4159. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4160. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4161. * @param $name (string) Name of the spot color.
  4162. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4163. * @return (string) PDF color command.
  4164. * @public
  4165. * @since 5.9.125 (2011-10-03)
  4166. */
  4167. public function setSpotColor($type, $name, $tint=100) {
  4168. $spotcolor = $this->getSpotColor($name);
  4169. if ($spotcolor === false) {
  4170. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4171. }
  4172. $tint = (max(0, min(100, $tint)) / 100);
  4173. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4174. switch ($type) {
  4175. case 'draw': {
  4176. $pdfcolor .= sprintf('CS %.3F SCN', $tint);
  4177. $this->DrawColor = $pdfcolor;
  4178. $this->strokecolor = $spotcolor;
  4179. break;
  4180. }
  4181. case 'fill': {
  4182. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4183. $this->FillColor = $pdfcolor;
  4184. $this->bgcolor = $spotcolor;
  4185. break;
  4186. }
  4187. case 'text': {
  4188. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4189. $this->TextColor = $pdfcolor;
  4190. $this->fgcolor = $spotcolor;
  4191. break;
  4192. }
  4193. }
  4194. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4195. if ($this->page > 0) {
  4196. $this->_out($pdfcolor);
  4197. }
  4198. if ($this->inxobj) {
  4199. // we are inside an XObject template
  4200. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4201. }
  4202. return $pdfcolor;
  4203. }
  4204. /**
  4205. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4206. * @param $name (string) Name of the spot color.
  4207. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4208. * @public
  4209. * @since 4.0.024 (2008-09-12)
  4210. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4211. */
  4212. public function SetDrawSpotColor($name, $tint=100) {
  4213. $this->setSpotColor('draw', $name, $tint);
  4214. }
  4215. /**
  4216. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4217. * @param $name (string) Name of the spot color.
  4218. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4219. * @public
  4220. * @since 4.0.024 (2008-09-12)
  4221. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4222. */
  4223. public function SetFillSpotColor($name, $tint=100) {
  4224. $this->setSpotColor('fill', $name, $tint);
  4225. }
  4226. /**
  4227. * Defines the spot color used for text.
  4228. * @param $name (string) Name of the spot color.
  4229. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4230. * @public
  4231. * @since 4.0.024 (2008-09-12)
  4232. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4233. */
  4234. public function SetTextSpotColor($name, $tint=100) {
  4235. $this->setSpotColor('text', $name, $tint);
  4236. }
  4237. /**
  4238. * Set the color array for the specified type ('draw', 'fill', 'text').
  4239. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4240. * The method can be called before the first page is created and the value is retained from page to page.
  4241. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4242. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  4243. * @param $ret (boolean) If true do not send the PDF command.
  4244. * @return (string) The PDF command or empty string.
  4245. * @public
  4246. * @since 3.1.000 (2008-06-11)
  4247. */
  4248. public function setColorArray($type, $color, $ret=false) {
  4249. if (is_array($color)) {
  4250. $color = array_values($color);
  4251. // component: grey, RGB red or CMYK cyan
  4252. $c = isset($color[0]) ? $color[0] : -1;
  4253. // component: RGB green or CMYK magenta
  4254. $m = isset($color[1]) ? $color[1] : -1;
  4255. // component: RGB blue or CMYK yellow
  4256. $y = isset($color[2]) ? $color[2] : -1;
  4257. // component: CMYK black
  4258. $k = isset($color[3]) ? $color[3] : -1;
  4259. // color name
  4260. $name = isset($color[4]) ? $color[4] : '';
  4261. if ($c >= 0) {
  4262. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  4263. }
  4264. }
  4265. return '';
  4266. }
  4267. /**
  4268. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4269. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4270. * The method can be called before the first page is created and the value is retained from page to page.
  4271. * @param $color (array) Array of colors (1, 3 or 4 values).
  4272. * @param $ret (boolean) If true do not send the PDF command.
  4273. * @return string the PDF command
  4274. * @public
  4275. * @since 3.1.000 (2008-06-11)
  4276. * @see SetDrawColor()
  4277. */
  4278. public function SetDrawColorArray($color, $ret=false) {
  4279. return $this->setColorArray('draw', $color, $ret);
  4280. }
  4281. /**
  4282. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4283. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4284. * The method can be called before the first page is created and the value is retained from page to page.
  4285. * @param $color (array) Array of colors (1, 3 or 4 values).
  4286. * @param $ret (boolean) If true do not send the PDF command.
  4287. * @public
  4288. * @since 3.1.000 (2008-6-11)
  4289. * @see SetFillColor()
  4290. */
  4291. public function SetFillColorArray($color, $ret=false) {
  4292. return $this->setColorArray('fill', $color, $ret);
  4293. }
  4294. /**
  4295. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4296. * The method can be called before the first page is created and the value is retained from page to page.
  4297. * @param $color (array) Array of colors (1, 3 or 4 values).
  4298. * @param $ret (boolean) If true do not send the PDF command.
  4299. * @public
  4300. * @since 3.1.000 (2008-6-11)
  4301. * @see SetFillColor()
  4302. */
  4303. public function SetTextColorArray($color, $ret=false) {
  4304. return $this->setColorArray('text', $color, $ret);
  4305. }
  4306. /**
  4307. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4308. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4309. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4310. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4311. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4312. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4313. * @param $ret (boolean) If true do not send the command.
  4314. * @param $name (string) spot color name (if any)
  4315. * @return (string) The PDF command or empty string.
  4316. * @public
  4317. * @since 5.9.125 (2011-10-03)
  4318. */
  4319. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4320. // set default values
  4321. if (!is_numeric($col1)) {
  4322. $col1 = 0;
  4323. }
  4324. if (!is_numeric($col2)) {
  4325. $col2 = -1;
  4326. }
  4327. if (!is_numeric($col3)) {
  4328. $col3 = -1;
  4329. }
  4330. if (!is_numeric($col4)) {
  4331. $col4 = -1;
  4332. }
  4333. // set color by case
  4334. $suffix = '';
  4335. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4336. // Grey scale
  4337. $col1 = max(0, min(255, $col1));
  4338. $intcolor = array('G' => $col1);
  4339. $pdfcolor = sprintf('%.3F ', ($col1 / 255));
  4340. $suffix = 'g';
  4341. } elseif ($col4 == -1) {
  4342. // RGB
  4343. $col1 = max(0, min(255, $col1));
  4344. $col2 = max(0, min(255, $col2));
  4345. $col3 = max(0, min(255, $col3));
  4346. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4347. $pdfcolor = sprintf('%.3F %.3F %.3F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4348. $suffix = 'rg';
  4349. } else {
  4350. $col1 = max(0, min(100, $col1));
  4351. $col2 = max(0, min(100, $col2));
  4352. $col3 = max(0, min(100, $col3));
  4353. $col4 = max(0, min(100, $col4));
  4354. if (empty($name)) {
  4355. // CMYK
  4356. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4357. $pdfcolor = sprintf('%.3F %.3F %.3F %.3F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4358. $suffix = 'k';
  4359. } else {
  4360. // SPOT COLOR
  4361. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4362. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4363. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4364. }
  4365. }
  4366. switch ($type) {
  4367. case 'draw': {
  4368. $pdfcolor .= strtoupper($suffix);
  4369. $this->DrawColor = $pdfcolor;
  4370. $this->strokecolor = $intcolor;
  4371. break;
  4372. }
  4373. case 'fill': {
  4374. $pdfcolor .= $suffix;
  4375. $this->FillColor = $pdfcolor;
  4376. $this->bgcolor = $intcolor;
  4377. break;
  4378. }
  4379. case 'text': {
  4380. $pdfcolor .= $suffix;
  4381. $this->TextColor = $pdfcolor;
  4382. $this->fgcolor = $intcolor;
  4383. break;
  4384. }
  4385. }
  4386. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4387. if (($type != 'text') AND ($this->page > 0)) {
  4388. if (!$ret) {
  4389. $this->_out($pdfcolor);
  4390. }
  4391. return $pdfcolor;
  4392. }
  4393. return '';
  4394. }
  4395. /**
  4396. * Convert a color array into a string representation.
  4397. * @param $c (array) Array of colors.
  4398. * @return (string) The color array representation.
  4399. * @protected
  4400. * @since 5.9.137 (2011-12-01)
  4401. */
  4402. protected function getColorStringFromArray($c) {
  4403. $c = array_values($c);
  4404. $color = '[';
  4405. switch (count($c)) {
  4406. case 4: {
  4407. // CMYK
  4408. $color .= sprintf('%.3F %.3F %.3F %.3F', (max(0, min(100, floatval($c[0]))) / 100), (max(0, min(100, floatval($c[1]))) / 100), (max(0, min(100, floatval($c[2]))) / 100), (max(0, min(100, floatval($c[3]))) / 100));
  4409. break;
  4410. }
  4411. case 3: {
  4412. // RGB
  4413. $color .= sprintf('%.3F %.3F %.3F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  4414. break;
  4415. }
  4416. case 1: {
  4417. // grayscale
  4418. $color .= sprintf('%.3F', (max(0, min(255, floatval($c[0]))) / 255));
  4419. break;
  4420. }
  4421. }
  4422. $color .= ']';
  4423. return $color;
  4424. }
  4425. /**
  4426. * 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.
  4427. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4428. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4429. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4430. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4431. * @param $ret (boolean) If true do not send the command.
  4432. * @param $name (string) spot color name (if any)
  4433. * @return string the PDF command
  4434. * @public
  4435. * @since 1.3
  4436. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4437. */
  4438. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4439. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4440. }
  4441. /**
  4442. * 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.
  4443. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4444. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4445. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4446. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4447. * @param $ret (boolean) If true do not send the command.
  4448. * @param $name (string) Spot color name (if any).
  4449. * @return (string) The PDF command.
  4450. * @public
  4451. * @since 1.3
  4452. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4453. */
  4454. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4455. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4456. }
  4457. /**
  4458. * 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.
  4459. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4460. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4461. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4462. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4463. * @param $ret (boolean) If true do not send the command.
  4464. * @param $name (string) Spot color name (if any).
  4465. * @return (string) Empty string.
  4466. * @public
  4467. * @since 1.3
  4468. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4469. */
  4470. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4471. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4472. }
  4473. /**
  4474. * Returns the length of a string in user unit. A font must be selected.<br>
  4475. * @param $s (string) The string whose length is to be computed
  4476. * @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.
  4477. * @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-trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
  4478. * @param $fontsize (float) Font size in points. The default value is the current size.
  4479. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4480. * @return mixed int total string length or array of characted widths
  4481. * @author Nicola Asuni
  4482. * @public
  4483. * @since 1.2
  4484. */
  4485. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4486. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4487. }
  4488. /**
  4489. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4490. * @param $sa (string) The array of chars whose total length is to be computed
  4491. * @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.
  4492. * @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 trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
  4493. * @param $fontsize (float) Font size in points. The default value is the current size.
  4494. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4495. * @return mixed int total string length or array of characted widths
  4496. * @author Nicola Asuni
  4497. * @public
  4498. * @since 2.4.000 (2008-03-06)
  4499. */
  4500. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4501. // store current values
  4502. if (!$this->empty_string($fontname)) {
  4503. $prev_FontFamily = $this->FontFamily;
  4504. $prev_FontStyle = $this->FontStyle;
  4505. $prev_FontSizePt = $this->FontSizePt;
  4506. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4507. }
  4508. // convert UTF-8 array to Latin1 if required
  4509. $sa = $this->UTF8ArrToLatin1($sa);
  4510. $w = 0; // total width
  4511. $wa = array(); // array of characters widths
  4512. foreach ($sa as $ck => $char) {
  4513. // character width
  4514. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4515. $wa[] = $cw;
  4516. $w += $cw;
  4517. }
  4518. // restore previous values
  4519. if (!$this->empty_string($fontname)) {
  4520. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4521. }
  4522. if ($getarray) {
  4523. return $wa;
  4524. }
  4525. return $w;
  4526. }
  4527. /**
  4528. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4529. * @param $char (int) The char code whose length is to be returned
  4530. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4531. * @return float char width
  4532. * @author Nicola Asuni
  4533. * @public
  4534. * @since 2.4.000 (2008-03-06)
  4535. */
  4536. public function GetCharWidth($char, $notlast=true) {
  4537. // get raw width
  4538. $chw = $this->getRawCharWidth($char);
  4539. if (($this->font_spacing != 0) AND $notlast) {
  4540. // increase/decrease font spacing
  4541. $chw += $this->font_spacing;
  4542. }
  4543. if ($this->font_stretching != 100) {
  4544. // fixed stretching mode
  4545. $chw *= ($this->font_stretching / 100);
  4546. }
  4547. return $chw;
  4548. }
  4549. /**
  4550. * Returns the length of the char in user unit for the current font.
  4551. * @param $char (int) The char code whose length is to be returned
  4552. * @return float char width
  4553. * @author Nicola Asuni
  4554. * @public
  4555. * @since 5.9.000 (2010-09-28)
  4556. */
  4557. public function getRawCharWidth($char) {
  4558. if ($char == 173) {
  4559. // SHY character will not be printed
  4560. return (0);
  4561. }
  4562. if (isset($this->CurrentFont['cw'][$char])) {
  4563. $w = $this->CurrentFont['cw'][$char];
  4564. } elseif (isset($this->CurrentFont['dw'])) {
  4565. // default width
  4566. $w = $this->CurrentFont['dw'];
  4567. } elseif (isset($this->CurrentFont['cw'][32])) {
  4568. // default width
  4569. $w = $this->CurrentFont['cw'][32];
  4570. } else {
  4571. $w = 600;
  4572. }
  4573. return ($w * $this->FontSize / 1000);
  4574. }
  4575. /**
  4576. * Returns the numbero of characters in a string.
  4577. * @param $s (string) The input string.
  4578. * @return int number of characters
  4579. * @public
  4580. * @since 2.0.0001 (2008-01-07)
  4581. */
  4582. public function GetNumChars($s) {
  4583. if ($this->isUnicodeFont()) {
  4584. return count($this->UTF8StringToArray($s));
  4585. }
  4586. return strlen($s);
  4587. }
  4588. /**
  4589. * Fill the list of available fonts ($this->fontlist).
  4590. * @protected
  4591. * @since 4.0.013 (2008-07-28)
  4592. */
  4593. protected function getFontsList() {
  4594. $fontsdir = opendir($this->_getfontpath());
  4595. while (($file = readdir($fontsdir)) !== false) {
  4596. if (substr($file, -4) == '.php') {
  4597. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4598. }
  4599. }
  4600. closedir($fontsdir);
  4601. }
  4602. /**
  4603. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4604. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4605. * 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.
  4606. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4607. * @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>
  4608. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4609. * @return array containing the font data, or false in case of error.
  4610. * @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.
  4611. * @public
  4612. * @since 1.5
  4613. * @see SetFont(), setFontSubsetting()
  4614. */
  4615. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4616. if ($subset === 'default') {
  4617. $subset = $this->font_subsetting;
  4618. }
  4619. if ($this->pdfa_mode) {
  4620. $subset = false;
  4621. }
  4622. if ($this->empty_string($family)) {
  4623. if (!$this->empty_string($this->FontFamily)) {
  4624. $family = $this->FontFamily;
  4625. } else {
  4626. $this->Error('Empty font family');
  4627. }
  4628. }
  4629. // move embedded styles on $style
  4630. if (substr($family, -1) == 'I') {
  4631. $style .= 'I';
  4632. $family = substr($family, 0, -1);
  4633. }
  4634. if (substr($family, -1) == 'B') {
  4635. $style .= 'B';
  4636. $family = substr($family, 0, -1);
  4637. }
  4638. // normalize family name
  4639. $family = strtolower($family);
  4640. if ((!$this->isunicode) AND ($family == 'arial')) {
  4641. $family = 'helvetica';
  4642. }
  4643. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4644. $style = '';
  4645. }
  4646. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4647. // all fonts must be embedded
  4648. $family = 'pdfa'.$family;
  4649. }
  4650. $tempstyle = strtoupper($style);
  4651. $style = '';
  4652. // underline
  4653. if (strpos($tempstyle, 'U') !== false) {
  4654. $this->underline = true;
  4655. } else {
  4656. $this->underline = false;
  4657. }
  4658. // line-through (deleted)
  4659. if (strpos($tempstyle, 'D') !== false) {
  4660. $this->linethrough = true;
  4661. } else {
  4662. $this->linethrough = false;
  4663. }
  4664. // overline
  4665. if (strpos($tempstyle, 'O') !== false) {
  4666. $this->overline = true;
  4667. } else {
  4668. $this->overline = false;
  4669. }
  4670. // bold
  4671. if (strpos($tempstyle, 'B') !== false) {
  4672. $style .= 'B';
  4673. }
  4674. // oblique
  4675. if (strpos($tempstyle, 'I') !== false) {
  4676. $style .= 'I';
  4677. }
  4678. $bistyle = $style;
  4679. $fontkey = $family.$style;
  4680. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4681. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4682. // check if the font has been already added
  4683. $fb = $this->getFontBuffer($fontkey);
  4684. if ($fb !== false) {
  4685. if ($this->inxobj) {
  4686. // we are inside an XObject template
  4687. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4688. }
  4689. return $fontdata;
  4690. }
  4691. if (isset($type)) {
  4692. unset($type);
  4693. }
  4694. if (isset($cw)) {
  4695. unset($cw);
  4696. }
  4697. // get specified font directory (if any)
  4698. $fontdir = false;
  4699. if (!$this->empty_string($fontfile)) {
  4700. $fontdir = dirname($fontfile);
  4701. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4702. $fontdir = '';
  4703. } else {
  4704. $fontdir .= '/';
  4705. }
  4706. }
  4707. $missing_style = false; // true when the font style variation is missing
  4708. // search and include font file
  4709. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4710. // build a standard filenames for specified font
  4711. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4712. // search files on various directories
  4713. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4714. $fontfile = $fontdir.$tmp_fontfile;
  4715. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4716. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4717. } elseif (file_exists($tmp_fontfile)) {
  4718. $fontfile = $tmp_fontfile;
  4719. } elseif (!$this->empty_string($style)) {
  4720. $missing_style = true;
  4721. // try to remove the style part
  4722. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4723. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4724. $fontfile = $fontdir.$tmp_fontfile;
  4725. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4726. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4727. } else {
  4728. $fontfile = $tmp_fontfile;
  4729. }
  4730. }
  4731. }
  4732. // include font file
  4733. if (file_exists($fontfile)) {
  4734. include($fontfile);
  4735. } else {
  4736. $this->Error('Could not include font definition file: '.$family.'');
  4737. }
  4738. // check font parameters
  4739. if ((!isset($type)) OR (!isset($cw))) {
  4740. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4741. }
  4742. // SET default parameters
  4743. if (!isset($file) OR $this->empty_string($file)) {
  4744. $file = '';
  4745. }
  4746. if (!isset($enc) OR $this->empty_string($enc)) {
  4747. $enc = '';
  4748. }
  4749. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4750. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4751. $cidinfo['uni2cid'] = array();
  4752. }
  4753. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4754. $ctg = '';
  4755. }
  4756. if (!isset($desc) OR $this->empty_string($desc)) {
  4757. $desc = array();
  4758. }
  4759. if (!isset($up) OR $this->empty_string($up)) {
  4760. $up = -100;
  4761. }
  4762. if (!isset($ut) OR $this->empty_string($ut)) {
  4763. $ut = 50;
  4764. }
  4765. if (!isset($cw) OR $this->empty_string($cw)) {
  4766. $cw = array();
  4767. }
  4768. if (!isset($dw) OR $this->empty_string($dw)) {
  4769. // set default width
  4770. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4771. $dw = $desc['MissingWidth'];
  4772. } elseif (isset($cw[32])) {
  4773. $dw = $cw[32];
  4774. } else {
  4775. $dw = 600;
  4776. }
  4777. }
  4778. ++$this->numfonts;
  4779. if ($type == 'core') {
  4780. $name = $this->CoreFonts[$fontkey];
  4781. $subset = false;
  4782. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4783. $subset = false;
  4784. } elseif ($type == 'TrueTypeUnicode') {
  4785. $enc = 'Identity-H';
  4786. } elseif ($type == 'cidfont0') {
  4787. if ($this->pdfa_mode) {
  4788. $this->Error('All fonts must be embedded in PDF/A mode!');
  4789. }
  4790. } else {
  4791. $this->Error('Unknow font type: '.$type.'');
  4792. }
  4793. // set name if unset
  4794. if (!isset($name) OR empty($name)) {
  4795. $name = $fontkey;
  4796. }
  4797. // create artificial font style variations if missing (only works with non-embedded fonts)
  4798. if (($type != 'core') AND $missing_style) {
  4799. // style variations
  4800. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4801. $name .= $styles[$bistyle];
  4802. // artificial bold
  4803. if (strpos($bistyle, 'B') !== false) {
  4804. if (isset($desc['StemV'])) {
  4805. // from normal to bold
  4806. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4807. } else {
  4808. // bold
  4809. $desc['StemV'] = 123;
  4810. }
  4811. }
  4812. // artificial italic
  4813. if (strpos($bistyle, 'I') !== false) {
  4814. if (isset($desc['ItalicAngle'])) {
  4815. $desc['ItalicAngle'] -= 11;
  4816. } else {
  4817. $desc['ItalicAngle'] = -11;
  4818. }
  4819. if (isset($desc['Flags'])) {
  4820. $desc['Flags'] |= 64; //bit 7
  4821. } else {
  4822. $desc['Flags'] = 64;
  4823. }
  4824. }
  4825. }
  4826. // initialize subsetchars to contain default ASCII values (0-255)
  4827. $subsetchars = array_fill(0, 256, true);
  4828. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  4829. if ($this->inxobj) {
  4830. // we are inside an XObject template
  4831. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4832. }
  4833. if (isset($diff) AND (!empty($diff))) {
  4834. //Search existing encodings
  4835. $d = 0;
  4836. $nb = count($this->diffs);
  4837. for ($i=1; $i <= $nb; ++$i) {
  4838. if ($this->diffs[$i] == $diff) {
  4839. $d = $i;
  4840. break;
  4841. }
  4842. }
  4843. if ($d == 0) {
  4844. $d = $nb + 1;
  4845. $this->diffs[$d] = $diff;
  4846. }
  4847. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4848. }
  4849. if (!$this->empty_string($file)) {
  4850. if (!isset($this->FontFiles[$file])) {
  4851. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4852. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4853. } elseif ($type != 'core') {
  4854. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4855. }
  4856. } else {
  4857. // update fontkeys that are sharing this font file
  4858. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4859. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4860. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4861. }
  4862. }
  4863. }
  4864. return $fontdata;
  4865. }
  4866. /**
  4867. * Sets the font used to print character strings.
  4868. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4869. * The method can be called before the first page is created and the font is retained from page to page.
  4870. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4871. * 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 />
  4872. * @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.
  4873. * @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 trough</li><li>O: overline</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
  4874. * @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
  4875. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4876. * @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.
  4877. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4878. * @author Nicola Asuni
  4879. * @public
  4880. * @since 1.0
  4881. * @see AddFont(), SetFontSize()
  4882. */
  4883. public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default', $out=true) {
  4884. //Select a font; size given in points
  4885. if ($size == 0) {
  4886. $size = $this->FontSizePt;
  4887. }
  4888. // try to add font (if not already added)
  4889. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4890. $this->FontFamily = $fontdata['family'];
  4891. $this->FontStyle = $fontdata['style'];
  4892. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4893. $this->SetFontSize($size, $out);
  4894. }
  4895. /**
  4896. * Defines the size of the current font.
  4897. * @param $size (float) The font size in points.
  4898. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4899. * @public
  4900. * @since 1.0
  4901. * @see SetFont()
  4902. */
  4903. public function SetFontSize($size, $out=true) {
  4904. // font size in points
  4905. $this->FontSizePt = $size;
  4906. // font size in user units
  4907. $this->FontSize = $size / $this->k;
  4908. // calculate some font metrics
  4909. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4910. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4911. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4912. } else {
  4913. $font_height = $size * 1.219;
  4914. }
  4915. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4916. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4917. }
  4918. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4919. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4920. }
  4921. if (!isset($font_ascent) AND !isset($font_descent)) {
  4922. // core font
  4923. $font_ascent = 0.76 * $font_height;
  4924. $font_descent = $font_height - $font_ascent;
  4925. } elseif (!isset($font_descent)) {
  4926. $font_descent = $font_height - $font_ascent;
  4927. } elseif (!isset($font_ascent)) {
  4928. $font_ascent = $font_height - $font_descent;
  4929. }
  4930. $this->FontAscent = ($font_ascent / $this->k);
  4931. $this->FontDescent = ($font_descent / $this->k);
  4932. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4933. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4934. }
  4935. }
  4936. /**
  4937. * Return the font descent value
  4938. * @param $font (string) font name
  4939. * @param $style (string) font style
  4940. * @param $size (float) The size (in points)
  4941. * @return int font descent
  4942. * @public
  4943. * @author Nicola Asuni
  4944. * @since 4.9.003 (2010-03-30)
  4945. */
  4946. public function getFontDescent($font, $style='', $size=0) {
  4947. $fontdata = $this->AddFont($font, $style);
  4948. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4949. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4950. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4951. } else {
  4952. $descent = 1.219 * 0.24 * $size;
  4953. }
  4954. return ($descent / $this->k);
  4955. }
  4956. /**
  4957. * Return the font ascent value
  4958. * @param $font (string) font name
  4959. * @param $style (string) font style
  4960. * @param $size (float) The size (in points)
  4961. * @return int font ascent
  4962. * @public
  4963. * @author Nicola Asuni
  4964. * @since 4.9.003 (2010-03-30)
  4965. */
  4966. public function getFontAscent($font, $style='', $size=0) {
  4967. $fontdata = $this->AddFont($font, $style);
  4968. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4969. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4970. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4971. } else {
  4972. $ascent = 1.219 * 0.76 * $size;
  4973. }
  4974. return ($ascent / $this->k);
  4975. }
  4976. /**
  4977. * Defines the default monospaced font.
  4978. * @param $font (string) Font name.
  4979. * @public
  4980. * @since 4.5.025
  4981. */
  4982. public function SetDefaultMonospacedFont($font) {
  4983. $this->default_monospaced_font = $font;
  4984. }
  4985. /**
  4986. * 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 />
  4987. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4988. * @public
  4989. * @since 1.5
  4990. * @see Cell(), Write(), Image(), Link(), SetLink()
  4991. */
  4992. public function AddLink() {
  4993. //Create a new internal link
  4994. $n = count($this->links) + 1;
  4995. $this->links[$n] = array(0, 0);
  4996. return $n;
  4997. }
  4998. /**
  4999. * Defines the page and position a link points to.
  5000. * @param $link (int) The link identifier returned by AddLink()
  5001. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  5002. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  5003. * @public
  5004. * @since 1.5
  5005. * @see AddLink()
  5006. */
  5007. public function SetLink($link, $y=0, $page=-1) {
  5008. if ($y == -1) {
  5009. $y = $this->y;
  5010. }
  5011. if ($page == -1) {
  5012. $page = $this->page;
  5013. }
  5014. $this->links[$link] = array($page, $y);
  5015. }
  5016. /**
  5017. * Puts a link on a rectangular area of the page.
  5018. * 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.
  5019. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5020. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5021. * @param $w (float) Width of the rectangle
  5022. * @param $h (float) Height of the rectangle
  5023. * @param $link (mixed) URL or identifier returned by AddLink()
  5024. * @param $spaces (int) number of spaces on the text to link
  5025. * @public
  5026. * @since 1.5
  5027. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  5028. */
  5029. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5030. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5031. }
  5032. /**
  5033. * Puts a markup annotation on a rectangular area of the page.
  5034. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5035. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5036. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5037. * @param $w (float) Width of the rectangle
  5038. * @param $h (float) Height of the rectangle
  5039. * @param $text (string) annotation text or alternate content
  5040. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5041. * @param $spaces (int) number of spaces on the text to link
  5042. * @public
  5043. * @since 4.0.018 (2008-08-06)
  5044. */
  5045. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5046. if ($this->inxobj) {
  5047. // store parameters for later use on template
  5048. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5049. return;
  5050. }
  5051. if ($x === '') {
  5052. $x = $this->x;
  5053. }
  5054. if ($y === '') {
  5055. $y = $this->y;
  5056. }
  5057. // check page for no-write regions and adapt page margins if necessary
  5058. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5059. // recalculate coordinates to account for graphic transformations
  5060. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5061. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5062. $maxid = count($this->transfmatrix[$i]) - 1;
  5063. for ($j=$maxid; $j >= 0; --$j) {
  5064. $ctm = $this->transfmatrix[$i][$j];
  5065. if (isset($ctm['a'])) {
  5066. $x = $x * $this->k;
  5067. $y = ($this->h - $y) * $this->k;
  5068. $w = $w * $this->k;
  5069. $h = $h * $this->k;
  5070. // top left
  5071. $xt = $x;
  5072. $yt = $y;
  5073. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5074. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5075. // top right
  5076. $xt = $x + $w;
  5077. $yt = $y;
  5078. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5079. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5080. // bottom left
  5081. $xt = $x;
  5082. $yt = $y - $h;
  5083. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5084. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5085. // bottom right
  5086. $xt = $x + $w;
  5087. $yt = $y - $h;
  5088. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5089. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5090. // new coordinates (rectangle area)
  5091. $x = min($x1, $x2, $x3, $x4);
  5092. $y = max($y1, $y2, $y3, $y4);
  5093. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5094. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5095. $x = $x / $this->k;
  5096. $y = $this->h - ($y / $this->k);
  5097. }
  5098. }
  5099. }
  5100. }
  5101. if ($this->page <= 0) {
  5102. $page = 1;
  5103. } else {
  5104. $page = $this->page;
  5105. }
  5106. if (!isset($this->PageAnnots[$page])) {
  5107. $this->PageAnnots[$page] = array();
  5108. }
  5109. ++$this->n;
  5110. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5111. if (!$this->pdfa_mode) {
  5112. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  5113. ++$this->n;
  5114. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5115. }
  5116. }
  5117. // Add widgets annotation's icons
  5118. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5119. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5120. }
  5121. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5122. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5123. }
  5124. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5125. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5126. }
  5127. }
  5128. /**
  5129. * Embedd the attached files.
  5130. * @since 4.4.000 (2008-12-07)
  5131. * @protected
  5132. * @see Annotation()
  5133. */
  5134. protected function _putEmbeddedFiles() {
  5135. if ($this->pdfa_mode) {
  5136. // embedded files are not allowed in PDF/A mode
  5137. return;
  5138. }
  5139. reset($this->embeddedfiles);
  5140. foreach ($this->embeddedfiles as $filename => $filedata) {
  5141. $data = file_get_contents($filedata['file']);
  5142. $filter = '';
  5143. if ($this->compress) {
  5144. $data = gzcompress($data);
  5145. $filter = ' /Filter /FlateDecode';
  5146. }
  5147. $stream = $this->_getrawstream($data, $filedata['n']);
  5148. $out = $this->_getobj($filedata['n'])."\n";
  5149. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5150. $out .= ' stream'."\n".$stream."\n".'endstream';
  5151. $out .= "\n".'endobj';
  5152. $this->_out($out);
  5153. }
  5154. }
  5155. /**
  5156. * Prints a text cell at the specified position.
  5157. * This method allows to place a string precisely on the page.
  5158. * @param $x (float) Abscissa of the cell origin
  5159. * @param $y (float) Ordinate of the cell origin
  5160. * @param $txt (string) String to print
  5161. * @param $fstroke (int) outline size in user units (false = disable)
  5162. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5163. * @param $ffill (boolean) if true fills the text
  5164. * @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)))
  5165. * @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.
  5166. * @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>
  5167. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5168. * @param $link (mixed) URL or identifier returned by AddLink().
  5169. * @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.
  5170. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5171. * @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>
  5172. * @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>
  5173. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5174. * @public
  5175. * @since 1.0
  5176. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5177. */
  5178. 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) {
  5179. $textrendermode = $this->textrendermode;
  5180. $textstrokewidth = $this->textstrokewidth;
  5181. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5182. $this->SetXY($x, $y, $rtloff);
  5183. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5184. // restore previous rendering mode
  5185. $this->textrendermode = $textrendermode;
  5186. $this->textstrokewidth = $textstrokewidth;
  5187. }
  5188. /**
  5189. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5190. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5191. * This method is called automatically and should not be called directly by the application.
  5192. * @return boolean
  5193. * @public
  5194. * @since 1.4
  5195. * @see SetAutoPageBreak()
  5196. */
  5197. public function AcceptPageBreak() {
  5198. if ($this->num_columns > 1) {
  5199. // multi column mode
  5200. if ($this->current_column < ($this->num_columns - 1)) {
  5201. // go to next column
  5202. $this->selectColumn($this->current_column + 1);
  5203. } else {
  5204. // add a new page
  5205. $this->AddPage();
  5206. // set first column
  5207. $this->selectColumn(0);
  5208. }
  5209. // avoid page breaking from checkPageBreak()
  5210. return false;
  5211. }
  5212. return $this->AutoPageBreak;
  5213. }
  5214. /**
  5215. * Add page if needed.
  5216. * @param $h (float) Cell height. Default value: 0.
  5217. * @param $y (mixed) starting y position, leave empty for current position.
  5218. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5219. * @return boolean true in case of page break, false otherwise.
  5220. * @since 3.2.000 (2008-07-01)
  5221. * @protected
  5222. */
  5223. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5224. if ($this->empty_string($y)) {
  5225. $y = $this->y;
  5226. }
  5227. $current_page = $this->page;
  5228. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5229. if ($addpage) {
  5230. //Automatic page break
  5231. $x = $this->x;
  5232. $this->AddPage($this->CurOrientation);
  5233. $this->y = $this->tMargin;
  5234. $oldpage = $this->page - 1;
  5235. if ($this->rtl) {
  5236. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5237. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5238. } else {
  5239. $this->x = $x;
  5240. }
  5241. } else {
  5242. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5243. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5244. } else {
  5245. $this->x = $x;
  5246. }
  5247. }
  5248. }
  5249. return true;
  5250. }
  5251. if ($current_page != $this->page) {
  5252. // account for columns mode
  5253. return true;
  5254. }
  5255. return false;
  5256. }
  5257. /**
  5258. * Removes SHY characters from text.
  5259. * Unicode Data:<ul>
  5260. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5261. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5262. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5263. * <li>HTML Entity (named): "&amp;shy;"</li>
  5264. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5265. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5266. * <li>UTF-8 character: chr(194).chr(173)</li>
  5267. * </ul>
  5268. * @param $txt (string) input string
  5269. * @return string without SHY characters.
  5270. * @public
  5271. * @since (4.5.019) 2009-02-28
  5272. */
  5273. public function removeSHY($txt='') {
  5274. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5275. if (!$this->isunicode) {
  5276. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5277. }
  5278. return $txt;
  5279. }
  5280. /**
  5281. * 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 />
  5282. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5283. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5284. * @param $h (float) Cell height. Default value: 0.
  5285. * @param $txt (string) String to print. Default value: empty string.
  5286. * @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)))
  5287. * @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.
  5288. * @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>
  5289. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5290. * @param $link (mixed) URL or identifier returned by AddLink().
  5291. * @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.
  5292. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5293. * @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>
  5294. * @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>
  5295. * @public
  5296. * @since 1.0
  5297. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5298. */
  5299. 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') {
  5300. $prev_cell_margin = $this->cell_margin;
  5301. $prev_cell_padding = $this->cell_padding;
  5302. $this->adjustCellPadding($border);
  5303. if (!$ignore_min_height) {
  5304. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5305. if ($h < $min_cell_height) {
  5306. $h = $min_cell_height;
  5307. }
  5308. }
  5309. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5310. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5311. $this->cell_padding = $prev_cell_padding;
  5312. $this->cell_margin = $prev_cell_margin;
  5313. }
  5314. /**
  5315. * 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 />
  5316. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5317. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5318. * @param $h (float) Cell height. Default value: 0.
  5319. * @param $txt (string) String to print. Default value: empty string.
  5320. * @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)))
  5321. * @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.
  5322. * @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>
  5323. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5324. * @param $link (mixed) URL or identifier returned by AddLink().
  5325. * @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.
  5326. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5327. * @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>
  5328. * @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>
  5329. * @return string containing cell code
  5330. * @protected
  5331. * @since 1.0
  5332. * @see Cell()
  5333. */
  5334. 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') {
  5335. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5336. $txt = str_replace($this->unichr(160), ' ', $txt);
  5337. $prev_cell_margin = $this->cell_margin;
  5338. $prev_cell_padding = $this->cell_padding;
  5339. $txt = $this->removeSHY($txt);
  5340. $rs = ''; //string to be returned
  5341. $this->adjustCellPadding($border);
  5342. if (!$ignore_min_height) {
  5343. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5344. if ($h < $min_cell_height) {
  5345. $h = $min_cell_height;
  5346. }
  5347. }
  5348. $k = $this->k;
  5349. // check page for no-write regions and adapt page margins if necessary
  5350. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5351. if ($this->rtl) {
  5352. $x = $this->x - $this->cell_margin['R'];
  5353. } else {
  5354. $x = $this->x + $this->cell_margin['L'];
  5355. }
  5356. $y = $this->y + $this->cell_margin['T'];
  5357. $prev_font_stretching = $this->font_stretching;
  5358. $prev_font_spacing = $this->font_spacing;
  5359. // cell vertical alignment
  5360. switch ($calign) {
  5361. case 'A': {
  5362. // font top
  5363. switch ($valign) {
  5364. case 'T': {
  5365. // top
  5366. $y -= $this->cell_padding['T'];
  5367. break;
  5368. }
  5369. case 'B': {
  5370. // bottom
  5371. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5372. break;
  5373. }
  5374. default:
  5375. case 'C':
  5376. case 'M': {
  5377. // center
  5378. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5379. break;
  5380. }
  5381. }
  5382. break;
  5383. }
  5384. case 'L': {
  5385. // font baseline
  5386. switch ($valign) {
  5387. case 'T': {
  5388. // top
  5389. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5390. break;
  5391. }
  5392. case 'B': {
  5393. // bottom
  5394. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5395. break;
  5396. }
  5397. default:
  5398. case 'C':
  5399. case 'M': {
  5400. // center
  5401. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5402. break;
  5403. }
  5404. }
  5405. break;
  5406. }
  5407. case 'D': {
  5408. // font bottom
  5409. switch ($valign) {
  5410. case 'T': {
  5411. // top
  5412. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5413. break;
  5414. }
  5415. case 'B': {
  5416. // bottom
  5417. $y -= ($h - $this->cell_padding['B']);
  5418. break;
  5419. }
  5420. default:
  5421. case 'C':
  5422. case 'M': {
  5423. // center
  5424. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5425. break;
  5426. }
  5427. }
  5428. break;
  5429. }
  5430. case 'B': {
  5431. // cell bottom
  5432. $y -= $h;
  5433. break;
  5434. }
  5435. case 'C':
  5436. case 'M': {
  5437. // cell center
  5438. $y -= ($h / 2);
  5439. break;
  5440. }
  5441. default:
  5442. case 'T': {
  5443. // cell top
  5444. break;
  5445. }
  5446. }
  5447. // text vertical alignment
  5448. switch ($valign) {
  5449. case 'T': {
  5450. // top
  5451. $yt = $y + $this->cell_padding['T'];
  5452. break;
  5453. }
  5454. case 'B': {
  5455. // bottom
  5456. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5457. break;
  5458. }
  5459. default:
  5460. case 'C':
  5461. case 'M': {
  5462. // center
  5463. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5464. break;
  5465. }
  5466. }
  5467. $basefonty = $yt + $this->FontAscent;
  5468. if ($this->empty_string($w) OR ($w <= 0)) {
  5469. if ($this->rtl) {
  5470. $w = $x - $this->lMargin;
  5471. } else {
  5472. $w = $this->w - $this->rMargin - $x;
  5473. }
  5474. }
  5475. $s = '';
  5476. // fill and borders
  5477. if (is_string($border) AND (strlen($border) == 4)) {
  5478. // full border
  5479. $border = 1;
  5480. }
  5481. if ($fill OR ($border == 1)) {
  5482. if ($fill) {
  5483. $op = ($border == 1) ? 'B' : 'f';
  5484. } else {
  5485. $op = 'S';
  5486. }
  5487. if ($this->rtl) {
  5488. $xk = (($x - $w) * $k);
  5489. } else {
  5490. $xk = ($x * $k);
  5491. }
  5492. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5493. }
  5494. // draw borders
  5495. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5496. if ($txt != '') {
  5497. $txt2 = $txt;
  5498. if ($this->isunicode) {
  5499. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5500. $txt2 = $this->UTF8ToLatin1($txt2);
  5501. } else {
  5502. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5503. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5504. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5505. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5506. // NOTE: this doesn't work with HTML justification
  5507. // Symbols that could overlap on the font top (only works in LTR)
  5508. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5509. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5510. $numchars = count($unicode); // number of chars
  5511. $unik = 0;
  5512. $uniblock = array();
  5513. $uniblock[$unik] = array();
  5514. $uniblock[$unik][] = $unicode[0];
  5515. // resolve overlapping conflicts by splitting the string in several parts
  5516. for ($i = 1; $i < $numchars; ++$i) {
  5517. // check if symbols overlaps at top
  5518. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5519. // move symbols to another array
  5520. ++$unik;
  5521. $uniblock[$unik] = array();
  5522. $uniblock[$unik][] = $unicode[$i];
  5523. ++$unik;
  5524. $uniblock[$unik] = array();
  5525. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5526. } else {
  5527. $uniblock[$unik][] = $unicode[$i];
  5528. }
  5529. }
  5530. // ---- END OF Fix for bug #2977340
  5531. }
  5532. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5533. }
  5534. }
  5535. $txt2 = $this->_escape($txt2);
  5536. // get current text width (considering general font stretching and spacing)
  5537. $txwidth = $this->GetStringWidth($txt);
  5538. $width = $txwidth;
  5539. // check for stretch mode
  5540. if ($stretch > 0) {
  5541. // calculate ratio between cell width and text width
  5542. if ($width <= 0) {
  5543. $ratio = 1;
  5544. } else {
  5545. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5546. }
  5547. // check if stretching is required
  5548. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5549. // the text will be stretched to fit cell width
  5550. if ($stretch > 2) {
  5551. // set new character spacing
  5552. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5553. } else {
  5554. // set new horizontal stretching
  5555. $this->font_stretching *= $ratio;
  5556. }
  5557. // recalculate text width (the text fills the entire cell)
  5558. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5559. // reset alignment
  5560. $align = '';
  5561. }
  5562. }
  5563. if ($this->font_stretching != 100) {
  5564. // apply font stretching
  5565. $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
  5566. }
  5567. if ($this->font_spacing != 0) {
  5568. // increase/decrease font spacing
  5569. $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
  5570. }
  5571. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5572. $s .= 'q '.$this->TextColor.' ';
  5573. }
  5574. // rendering mode
  5575. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  5576. // count number of spaces
  5577. $ns = substr_count($txt, chr(32));
  5578. // Justification
  5579. $spacewidth = 0;
  5580. if (($align == 'J') AND ($ns > 0)) {
  5581. if ($this->isUnicodeFont()) {
  5582. // get string width without spaces
  5583. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5584. // calculate average space width
  5585. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5586. if ($this->font_stretching != 100) {
  5587. // word spacing is affected by stretching
  5588. $spacewidth /= ($this->font_stretching / 100);
  5589. }
  5590. // set word position to be used with TJ operator
  5591. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  5592. $unicode_justification = true;
  5593. } else {
  5594. // get string width
  5595. $width = $txwidth;
  5596. // new space width
  5597. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5598. if ($this->font_stretching != 100) {
  5599. // word spacing (Tw) is affected by stretching
  5600. $spacewidth /= ($this->font_stretching / 100);
  5601. }
  5602. // set word spacing
  5603. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  5604. }
  5605. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5606. }
  5607. // replace carriage return characters
  5608. $txt2 = str_replace("\r", ' ', $txt2);
  5609. switch ($align) {
  5610. case 'C': {
  5611. $dx = ($w - $width) / 2;
  5612. break;
  5613. }
  5614. case 'R': {
  5615. if ($this->rtl) {
  5616. $dx = $this->cell_padding['R'];
  5617. } else {
  5618. $dx = $w - $width - $this->cell_padding['R'];
  5619. }
  5620. break;
  5621. }
  5622. case 'L': {
  5623. if ($this->rtl) {
  5624. $dx = $w - $width - $this->cell_padding['L'];
  5625. } else {
  5626. $dx = $this->cell_padding['L'];
  5627. }
  5628. break;
  5629. }
  5630. case 'J':
  5631. default: {
  5632. if ($this->rtl) {
  5633. $dx = $this->cell_padding['R'];
  5634. } else {
  5635. $dx = $this->cell_padding['L'];
  5636. }
  5637. break;
  5638. }
  5639. }
  5640. if ($this->rtl) {
  5641. $xdx = $x - $dx - $width;
  5642. } else {
  5643. $xdx = $x + $dx;
  5644. }
  5645. $xdk = $xdx * $k;
  5646. // print text
  5647. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5648. if (isset($uniblock)) {
  5649. // print overlapping characters as separate string
  5650. $xshift = 0; // horizontal shift
  5651. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5652. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5653. foreach ($uniblock as $uk => $uniarr) {
  5654. if (($uk % 2) == 0) {
  5655. // x space to skip
  5656. if ($spacewidth != 0) {
  5657. // justification shift
  5658. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5659. }
  5660. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5661. } else {
  5662. // character to print
  5663. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5664. $topchr = $this->_escape($topchr);
  5665. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5666. }
  5667. }
  5668. }
  5669. if ($this->underline) {
  5670. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5671. }
  5672. if ($this->linethrough) {
  5673. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5674. }
  5675. if ($this->overline) {
  5676. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5677. }
  5678. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5679. $s .= ' Q';
  5680. }
  5681. if ($link) {
  5682. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5683. }
  5684. }
  5685. // output cell
  5686. if ($s) {
  5687. // output cell
  5688. $rs .= $s;
  5689. if ($this->font_spacing != 0) {
  5690. // reset font spacing mode
  5691. $rs .= ' BT 0 Tc ET';
  5692. }
  5693. if ($this->font_stretching != 100) {
  5694. // reset font stretching mode
  5695. $rs .= ' BT 100 Tz ET';
  5696. }
  5697. }
  5698. // reset word spacing
  5699. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5700. $rs .= ' BT 0 Tw ET';
  5701. }
  5702. // reset stretching and spacing
  5703. $this->font_stretching = $prev_font_stretching;
  5704. $this->font_spacing = $prev_font_spacing;
  5705. $this->lasth = $h;
  5706. if ($ln > 0) {
  5707. //Go to the beginning of the next line
  5708. $this->y = $y + $h + $this->cell_margin['B'];
  5709. if ($ln == 1) {
  5710. if ($this->rtl) {
  5711. $this->x = $this->w - $this->rMargin;
  5712. } else {
  5713. $this->x = $this->lMargin;
  5714. }
  5715. }
  5716. } else {
  5717. // go left or right by case
  5718. if ($this->rtl) {
  5719. $this->x = $x - $w - $this->cell_margin['L'];
  5720. } else {
  5721. $this->x = $x + $w + $this->cell_margin['R'];
  5722. }
  5723. }
  5724. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5725. $rs = $gstyles.$rs;
  5726. $this->cell_padding = $prev_cell_padding;
  5727. $this->cell_margin = $prev_cell_margin;
  5728. return $rs;
  5729. }
  5730. /**
  5731. * Returns the code to draw the cell border
  5732. * @param $x (float) X coordinate.
  5733. * @param $y (float) Y coordinate.
  5734. * @param $w (float) Cell width.
  5735. * @param $h (float) Cell height.
  5736. * @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)))
  5737. * @return string containing cell border code
  5738. * @protected
  5739. * @see SetLineStyle()
  5740. * @since 5.7.000 (2010-08-02)
  5741. */
  5742. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5743. $s = ''; // string to be returned
  5744. if (empty($brd)) {
  5745. return $s;
  5746. }
  5747. if ($brd == 1) {
  5748. $brd = array('LRTB' => true);
  5749. }
  5750. // calculate coordinates for border
  5751. $k = $this->k;
  5752. if ($this->rtl) {
  5753. $xeL = ($x - $w) * $k;
  5754. $xeR = $x * $k;
  5755. } else {
  5756. $xeL = $x * $k;
  5757. $xeR = ($x + $w) * $k;
  5758. }
  5759. $yeL = (($this->h - ($y + $h)) * $k);
  5760. $yeT = (($this->h - $y) * $k);
  5761. $xeT = $xeL;
  5762. $xeB = $xeR;
  5763. $yeR = $yeT;
  5764. $yeB = $yeL;
  5765. if (is_string($brd)) {
  5766. // convert string to array
  5767. $slen = strlen($brd);
  5768. $newbrd = array();
  5769. for ($i = 0; $i < $slen; ++$i) {
  5770. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5771. }
  5772. $brd = $newbrd;
  5773. }
  5774. if (isset($brd['mode'])) {
  5775. $mode = $brd['mode'];
  5776. unset($brd['mode']);
  5777. } else {
  5778. $mode = 'normal';
  5779. }
  5780. foreach ($brd as $border => $style) {
  5781. if (is_array($style) AND !empty($style)) {
  5782. // apply border style
  5783. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5784. $s .= $this->SetLineStyle($style, true)."\n";
  5785. }
  5786. switch ($mode) {
  5787. case 'ext': {
  5788. $off = (($this->LineWidth / 2) * $k);
  5789. $xL = $xeL - $off;
  5790. $xR = $xeR + $off;
  5791. $yT = $yeT + $off;
  5792. $yL = $yeL - $off;
  5793. $xT = $xL;
  5794. $xB = $xR;
  5795. $yR = $yT;
  5796. $yB = $yL;
  5797. $w += $this->LineWidth;
  5798. $h += $this->LineWidth;
  5799. break;
  5800. }
  5801. case 'int': {
  5802. $off = ($this->LineWidth / 2) * $k;
  5803. $xL = $xeL + $off;
  5804. $xR = $xeR - $off;
  5805. $yT = $yeT - $off;
  5806. $yL = $yeL + $off;
  5807. $xT = $xL;
  5808. $xB = $xR;
  5809. $yR = $yT;
  5810. $yB = $yL;
  5811. $w -= $this->LineWidth;
  5812. $h -= $this->LineWidth;
  5813. break;
  5814. }
  5815. case 'normal':
  5816. default: {
  5817. $xL = $xeL;
  5818. $xT = $xeT;
  5819. $xB = $xeB;
  5820. $xR = $xeR;
  5821. $yL = $yeL;
  5822. $yT = $yeT;
  5823. $yB = $yeB;
  5824. $yR = $yeR;
  5825. break;
  5826. }
  5827. }
  5828. // draw borders by case
  5829. if (strlen($border) == 4) {
  5830. $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5831. } elseif (strlen($border) == 3) {
  5832. if (strpos($border,'B') === false) { // LTR
  5833. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5834. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5835. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5836. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5837. $s .= 'S ';
  5838. } elseif (strpos($border,'L') === false) { // TRB
  5839. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5840. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5841. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5842. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5843. $s .= 'S ';
  5844. } elseif (strpos($border,'T') === false) { // RBL
  5845. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5846. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5847. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5848. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5849. $s .= 'S ';
  5850. } elseif (strpos($border,'R') === false) { // BLT
  5851. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5852. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5853. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5854. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5855. $s .= 'S ';
  5856. }
  5857. } elseif (strlen($border) == 2) {
  5858. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5859. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5860. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5861. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5862. $s .= 'S ';
  5863. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5864. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5865. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5866. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5867. $s .= 'S ';
  5868. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5869. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5870. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5871. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5872. $s .= 'S ';
  5873. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5874. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5875. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5876. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5877. $s .= 'S ';
  5878. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5879. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5880. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5881. $s .= 'S ';
  5882. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5883. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5884. $s .= 'S ';
  5885. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5886. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5887. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5888. $s .= 'S ';
  5889. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5890. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5891. $s .= 'S ';
  5892. }
  5893. } else { // strlen($border) == 1
  5894. if (strpos($border,'L') !== false) { // L
  5895. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5896. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5897. $s .= 'S ';
  5898. } elseif (strpos($border,'T') !== false) { // T
  5899. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5900. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5901. $s .= 'S ';
  5902. } elseif (strpos($border,'R') !== false) { // R
  5903. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5904. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5905. $s .= 'S ';
  5906. } elseif (strpos($border,'B') !== false) { // B
  5907. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5908. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5909. $s .= 'S ';
  5910. }
  5911. }
  5912. if (is_array($style) AND !empty($style)) {
  5913. // reset border style to previous value
  5914. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5915. }
  5916. }
  5917. return $s;
  5918. }
  5919. /**
  5920. * This method allows printing text with line breaks.
  5921. * 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 />
  5922. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5923. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5924. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5925. * @param $txt (string) String to print
  5926. * @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)))
  5927. * @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>
  5928. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5929. * @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>
  5930. * @param $x (float) x position in user units
  5931. * @param $y (float) y position in user units
  5932. * @param $reseth (boolean) if true reset the last cell height (default true).
  5933. * @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.
  5934. * @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.
  5935. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5936. * @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.
  5937. * @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.
  5938. * @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).
  5939. * @return int Return the number of cells or 1 for html mode.
  5940. * @public
  5941. * @since 1.3
  5942. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5943. */
  5944. 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) {
  5945. $prev_cell_margin = $this->cell_margin;
  5946. $prev_cell_padding = $this->cell_padding;
  5947. // adjust internal padding
  5948. $this->adjustCellPadding($border);
  5949. $mc_padding = $this->cell_padding;
  5950. $mc_margin = $this->cell_margin;
  5951. $this->cell_padding['T'] = 0;
  5952. $this->cell_padding['B'] = 0;
  5953. $this->setCellMargins(0, 0, 0, 0);
  5954. if ($this->empty_string($this->lasth) OR $reseth) {
  5955. // reset row height
  5956. $this->resetLastH();
  5957. }
  5958. if (!$this->empty_string($y)) {
  5959. $this->SetY($y);
  5960. } else {
  5961. $y = $this->GetY();
  5962. }
  5963. $resth = 0;
  5964. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5965. // spit cell in more pages/columns
  5966. $newh = ($this->PageBreakTrigger - $y);
  5967. $resth = ($h - $newh); // cell to be printed on the next page/column
  5968. $h = $newh;
  5969. }
  5970. // get current page number
  5971. $startpage = $this->page;
  5972. // get current column
  5973. $startcolumn = $this->current_column;
  5974. if (!$this->empty_string($x)) {
  5975. $this->SetX($x);
  5976. } else {
  5977. $x = $this->GetX();
  5978. }
  5979. // check page for no-write regions and adapt page margins if necessary
  5980. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5981. // apply margins
  5982. $oy = $y + $mc_margin['T'];
  5983. if ($this->rtl) {
  5984. $ox = $this->w - $x - $mc_margin['R'];
  5985. } else {
  5986. $ox = $x + $mc_margin['L'];
  5987. }
  5988. $this->x = $ox;
  5989. $this->y = $oy;
  5990. // set width
  5991. if ($this->empty_string($w) OR ($w <= 0)) {
  5992. if ($this->rtl) {
  5993. $w = $this->x - $this->lMargin - $mc_margin['L'];
  5994. } else {
  5995. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  5996. }
  5997. }
  5998. // store original margin values
  5999. $lMargin = $this->lMargin;
  6000. $rMargin = $this->rMargin;
  6001. if ($this->rtl) {
  6002. $this->rMargin = $this->w - $this->x;
  6003. $this->lMargin = $this->x - $w;
  6004. } else {
  6005. $this->lMargin = $this->x;
  6006. $this->rMargin = $this->w - $this->x - $w;
  6007. }
  6008. if ($autopadding) {
  6009. // add top padding
  6010. $this->y += $mc_padding['T'];
  6011. }
  6012. if ($ishtml) { // ******* Write HTML text
  6013. $this->writeHTML($txt, true, false, $reseth, true, $align);
  6014. $nl = 1;
  6015. } else { // ******* Write simple text
  6016. $prev_FontSizePt = $this->FontSizePt;
  6017. // vertical alignment
  6018. if ($maxh > 0) {
  6019. // get text height
  6020. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6021. if ($fitcell) {
  6022. // try to reduce font size to fit text on cell (use a quick search algorithm)
  6023. $fmin = 1;
  6024. $fmax = $this->FontSizePt;
  6025. $prev_text_height = $text_height;
  6026. $maxit = 100; // max number of iterations
  6027. while ($maxit > 0) {
  6028. $fmid = (($fmax + $fmin) / 2);
  6029. $this->SetFontSize($fmid, false);
  6030. $this->resetLastH();
  6031. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6032. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6033. break;
  6034. } elseif ($text_height < $maxh) {
  6035. $fmin = $fmid;
  6036. } else {
  6037. $fmax = $fmid;
  6038. }
  6039. --$maxit;
  6040. }
  6041. $this->SetFontSize($this->FontSizePt);
  6042. }
  6043. if ($text_height < $maxh) {
  6044. if ($valign == 'M') {
  6045. // text vertically centered
  6046. $this->y += (($maxh - $text_height) / 2);
  6047. } elseif ($valign == 'B') {
  6048. // text vertically aligned on bottom
  6049. $this->y += ($maxh - $text_height);
  6050. }
  6051. }
  6052. }
  6053. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6054. if ($fitcell) {
  6055. // restore font size
  6056. $this->SetFontSize($prev_FontSizePt);
  6057. }
  6058. }
  6059. if ($autopadding) {
  6060. // add bottom padding
  6061. $this->y += $mc_padding['B'];
  6062. }
  6063. // Get end-of-text Y position
  6064. $currentY = $this->y;
  6065. // get latest page number
  6066. $endpage = $this->page;
  6067. if ($resth > 0) {
  6068. $skip = ($endpage - $startpage);
  6069. $tmpresth = $resth;
  6070. while ($tmpresth > 0) {
  6071. if ($skip <= 0) {
  6072. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6073. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6074. }
  6075. if ($this->num_columns > 1) {
  6076. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6077. } else {
  6078. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6079. }
  6080. --$skip;
  6081. }
  6082. $currentY = $this->y;
  6083. $endpage = $this->page;
  6084. }
  6085. // get latest column
  6086. $endcolumn = $this->current_column;
  6087. if ($this->num_columns == 0) {
  6088. $this->num_columns = 1;
  6089. }
  6090. // get border modes
  6091. $border_start = $this->getBorderMode($border, $position='start');
  6092. $border_end = $this->getBorderMode($border, $position='end');
  6093. $border_middle = $this->getBorderMode($border, $position='middle');
  6094. // design borders around HTML cells.
  6095. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6096. $ccode = '';
  6097. $this->setPage($page);
  6098. if ($this->num_columns < 2) {
  6099. // single-column mode
  6100. $this->SetX($x);
  6101. $this->y = $this->tMargin;
  6102. }
  6103. // account for margin changes
  6104. if ($page > $startpage) {
  6105. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6106. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6107. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6108. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6109. }
  6110. }
  6111. if ($startpage == $endpage) {
  6112. // single page
  6113. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6114. $this->selectColumn($column);
  6115. if ($this->rtl) {
  6116. $this->x -= $mc_margin['R'];
  6117. } else {
  6118. $this->x += $mc_margin['L'];
  6119. }
  6120. if ($startcolumn == $endcolumn) { // single column
  6121. $cborder = $border;
  6122. $h = max($h, ($currentY - $oy));
  6123. $this->y = $oy;
  6124. } elseif ($column == $startcolumn) { // first column
  6125. $cborder = $border_start;
  6126. $this->y = $oy;
  6127. $h = $this->h - $this->y - $this->bMargin;
  6128. } elseif ($column == $endcolumn) { // end column
  6129. $cborder = $border_end;
  6130. $h = $currentY - $this->y;
  6131. if ($resth > $h) {
  6132. $h = $resth;
  6133. }
  6134. } else { // middle column
  6135. $cborder = $border_middle;
  6136. $h = $this->h - $this->y - $this->bMargin;
  6137. $resth -= $h;
  6138. }
  6139. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6140. } // end for each column
  6141. } elseif ($page == $startpage) { // first page
  6142. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6143. $this->selectColumn($column);
  6144. if ($this->rtl) {
  6145. $this->x -= $mc_margin['R'];
  6146. } else {
  6147. $this->x += $mc_margin['L'];
  6148. }
  6149. if ($column == $startcolumn) { // first column
  6150. $cborder = $border_start;
  6151. $this->y = $oy;
  6152. $h = $this->h - $this->y - $this->bMargin;
  6153. } else { // middle column
  6154. $cborder = $border_middle;
  6155. $h = $this->h - $this->y - $this->bMargin;
  6156. $resth -= $h;
  6157. }
  6158. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6159. } // end for each column
  6160. } elseif ($page == $endpage) { // last page
  6161. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6162. $this->selectColumn($column);
  6163. if ($this->rtl) {
  6164. $this->x -= $mc_margin['R'];
  6165. } else {
  6166. $this->x += $mc_margin['L'];
  6167. }
  6168. if ($column == $endcolumn) {
  6169. // end column
  6170. $cborder = $border_end;
  6171. $h = $currentY - $this->y;
  6172. if ($resth > $h) {
  6173. $h = $resth;
  6174. }
  6175. } else {
  6176. // middle column
  6177. $cborder = $border_middle;
  6178. $h = $this->h - $this->y - $this->bMargin;
  6179. $resth -= $h;
  6180. }
  6181. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6182. } // end for each column
  6183. } else { // middle page
  6184. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6185. $this->selectColumn($column);
  6186. if ($this->rtl) {
  6187. $this->x -= $mc_margin['R'];
  6188. } else {
  6189. $this->x += $mc_margin['L'];
  6190. }
  6191. $cborder = $border_middle;
  6192. $h = $this->h - $this->y - $this->bMargin;
  6193. $resth -= $h;
  6194. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6195. } // end for each column
  6196. }
  6197. if ($cborder OR $fill) {
  6198. $offsetlen = strlen($ccode);
  6199. // draw border and fill
  6200. if ($this->inxobj) {
  6201. // we are inside an XObject template
  6202. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6203. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6204. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6205. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6206. } else {
  6207. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6208. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6209. }
  6210. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6211. $pstart = substr($pagebuff, 0, $pagemark);
  6212. $pend = substr($pagebuff, $pagemark);
  6213. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6214. } else {
  6215. if (end($this->transfmrk[$this->page]) !== false) {
  6216. $pagemarkkey = key($this->transfmrk[$this->page]);
  6217. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6218. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6219. } elseif ($this->InFooter) {
  6220. $pagemark = $this->footerpos[$this->page];
  6221. $this->footerpos[$this->page] += $offsetlen;
  6222. } else {
  6223. $pagemark = $this->intmrk[$this->page];
  6224. $this->intmrk[$this->page] += $offsetlen;
  6225. }
  6226. $pagebuff = $this->getPageBuffer($this->page);
  6227. $pstart = substr($pagebuff, 0, $pagemark);
  6228. $pend = substr($pagebuff, $pagemark);
  6229. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6230. }
  6231. }
  6232. } // end for each page
  6233. // Get end-of-cell Y position
  6234. $currentY = $this->GetY();
  6235. // restore previous values
  6236. if ($this->num_columns > 1) {
  6237. $this->selectColumn();
  6238. } else {
  6239. // restore original margins
  6240. $this->lMargin = $lMargin;
  6241. $this->rMargin = $rMargin;
  6242. if ($this->page > $startpage) {
  6243. // check for margin variations between pages (i.e. booklet mode)
  6244. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6245. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6246. if (($dl != 0) OR ($dr != 0)) {
  6247. $this->lMargin += $dl;
  6248. $this->rMargin += $dr;
  6249. }
  6250. }
  6251. }
  6252. if ($ln > 0) {
  6253. //Go to the beginning of the next line
  6254. $this->SetY($currentY + $mc_margin['B']);
  6255. if ($ln == 2) {
  6256. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6257. }
  6258. } else {
  6259. // go left or right by case
  6260. $this->setPage($startpage);
  6261. $this->y = $y;
  6262. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6263. }
  6264. $this->setContentMark();
  6265. $this->cell_padding = $prev_cell_padding;
  6266. $this->cell_margin = $prev_cell_margin;
  6267. return $nl;
  6268. }
  6269. /**
  6270. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6271. * @param $brd (mixed) Indicates if borders must be drawn around the cell block. 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: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6272. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6273. * @return border mode array
  6274. * @protected
  6275. * @since 4.4.002 (2008-12-09)
  6276. */
  6277. protected function getBorderMode($brd, $position='start') {
  6278. if ((!$this->opencell) OR empty($brd)) {
  6279. return $brd;
  6280. }
  6281. if ($brd == 1) {
  6282. $brd = 'LTRB';
  6283. }
  6284. if (is_string($brd)) {
  6285. // convert string to array
  6286. $slen = strlen($brd);
  6287. $newbrd = array();
  6288. for ($i = 0; $i < $slen; ++$i) {
  6289. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6290. }
  6291. $brd = $newbrd;
  6292. }
  6293. foreach ($brd as $border => $style) {
  6294. switch ($position) {
  6295. case 'start': {
  6296. if (strpos($border, 'B') !== false) {
  6297. // remove bottom line
  6298. $newkey = str_replace('B', '', $border);
  6299. if (strlen($newkey) > 0) {
  6300. $brd[$newkey] = $style;
  6301. }
  6302. unset($brd[$border]);
  6303. }
  6304. break;
  6305. }
  6306. case 'middle': {
  6307. if (strpos($border, 'B') !== false) {
  6308. // remove bottom line
  6309. $newkey = str_replace('B', '', $border);
  6310. if (strlen($newkey) > 0) {
  6311. $brd[$newkey] = $style;
  6312. }
  6313. unset($brd[$border]);
  6314. $border = $newkey;
  6315. }
  6316. if (strpos($border, 'T') !== false) {
  6317. // remove bottom line
  6318. $newkey = str_replace('T', '', $border);
  6319. if (strlen($newkey) > 0) {
  6320. $brd[$newkey] = $style;
  6321. }
  6322. unset($brd[$border]);
  6323. }
  6324. break;
  6325. }
  6326. case 'end': {
  6327. if (strpos($border, 'T') !== false) {
  6328. // remove bottom line
  6329. $newkey = str_replace('T', '', $border);
  6330. if (strlen($newkey) > 0) {
  6331. $brd[$newkey] = $style;
  6332. }
  6333. unset($brd[$border]);
  6334. }
  6335. break;
  6336. }
  6337. }
  6338. }
  6339. return $brd;
  6340. }
  6341. /**
  6342. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6343. * @param $txt (string) String for calculating his height
  6344. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6345. * @param $reseth (boolean) if true reset the last cell height (default false).
  6346. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6347. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6348. * @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)))
  6349. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6350. * @author Alexander Escalona Fernández, Nicola Asuni
  6351. * @public
  6352. * @since 4.5.011
  6353. */
  6354. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6355. if ($txt === '') {
  6356. // empty string
  6357. return 1;
  6358. }
  6359. // adjust internal padding
  6360. $prev_cell_padding = $this->cell_padding;
  6361. $prev_lasth = $this->lasth;
  6362. if (is_array($cellpadding)) {
  6363. $this->cell_padding = $cellpadding;
  6364. }
  6365. $this->adjustCellPadding($border);
  6366. if ($this->empty_string($w) OR ($w <= 0)) {
  6367. if ($this->rtl) {
  6368. $w = $this->x - $this->lMargin;
  6369. } else {
  6370. $w = $this->w - $this->rMargin - $this->x;
  6371. }
  6372. }
  6373. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6374. if ($reseth) {
  6375. // reset row height
  6376. $this->resetLastH();
  6377. }
  6378. $lines = 1;
  6379. $sum = 0;
  6380. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6381. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6382. $length = count($chars);
  6383. $lastSeparator = -1;
  6384. for ($i = 0; $i < $length; ++$i) {
  6385. $charWidth = $charsWidth[$i];
  6386. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6387. $lastSeparator = $i;
  6388. }
  6389. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6390. ++$lines;
  6391. if ($chars[$i] == 10) {
  6392. $lastSeparator = -1;
  6393. $sum = 0;
  6394. } elseif ($lastSeparator != -1) {
  6395. $i = $lastSeparator;
  6396. $lastSeparator = -1;
  6397. $sum = 0;
  6398. } else {
  6399. $sum = $charWidth;
  6400. }
  6401. } else {
  6402. $sum += $charWidth;
  6403. }
  6404. }
  6405. if ($chars[($length - 1)] == 10) {
  6406. --$lines;
  6407. }
  6408. $this->cell_padding = $prev_cell_padding;
  6409. $this->lasth = $prev_lasth;
  6410. return $lines;
  6411. }
  6412. /**
  6413. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6414. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6415. * @pre
  6416. * // store current object
  6417. * $pdf->startTransaction();
  6418. * // store starting values
  6419. * $start_y = $pdf->GetY();
  6420. * $start_page = $pdf->getPage();
  6421. * // call your printing functions with your parameters
  6422. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6423. * $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);
  6424. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6425. * // get the new Y
  6426. * $end_y = $pdf->GetY();
  6427. * $end_page = $pdf->getPage();
  6428. * // calculate height
  6429. * $height = 0;
  6430. * if ($end_page == $start_page) {
  6431. * $height = $end_y - $start_y;
  6432. * } else {
  6433. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6434. * $this->setPage($page);
  6435. * if ($page == $start_page) {
  6436. * // first page
  6437. * $height = $this->h - $start_y - $this->bMargin;
  6438. * } elseif ($page == $end_page) {
  6439. * // last page
  6440. * $height = $end_y - $this->tMargin;
  6441. * } else {
  6442. * $height = $this->h - $this->tMargin - $this->bMargin;
  6443. * }
  6444. * }
  6445. * }
  6446. * // restore previous object
  6447. * $pdf = $pdf->rollbackTransaction();
  6448. *
  6449. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6450. * @param $txt (string) String for calculating his height
  6451. * @param $reseth (boolean) if true reset the last cell height (default false).
  6452. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6453. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6454. * @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)))
  6455. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6456. * @author Nicola Asuni, Alexander Escalona Fernández
  6457. * @public
  6458. */
  6459. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6460. // adjust internal padding
  6461. $prev_cell_padding = $this->cell_padding;
  6462. $prev_lasth = $this->lasth;
  6463. if (is_array($cellpadding)) {
  6464. $this->cell_padding = $cellpadding;
  6465. }
  6466. $this->adjustCellPadding($border);
  6467. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6468. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6469. if ($autopadding) {
  6470. // add top and bottom padding
  6471. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6472. }
  6473. $this->cell_padding = $prev_cell_padding;
  6474. $this->lasth = $prev_lasth;
  6475. return $height;
  6476. }
  6477. /**
  6478. * This method prints text from the current position.<br />
  6479. * @param $h (float) Line height
  6480. * @param $txt (string) String to print
  6481. * @param $link (mixed) URL or identifier returned by AddLink()
  6482. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6483. * @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>
  6484. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6485. * @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.
  6486. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6487. * @param $firstblock (boolean) if true the string is the starting of a line.
  6488. * @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  6489. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6490. * @param $margin (array) margin array of the parent container
  6491. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6492. * @public
  6493. * @since 1.5
  6494. */
  6495. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6496. // check page for no-write regions and adapt page margins if necessary
  6497. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6498. if (strlen($txt) == 0) {
  6499. // fix empty text
  6500. $txt = ' ';
  6501. }
  6502. if ($margin === '') {
  6503. // set default margins
  6504. $margin = $this->cell_margin;
  6505. }
  6506. // remove carriage returns
  6507. $s = str_replace("\r", '', $txt);
  6508. // check if string contains arabic text
  6509. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6510. $arabic = true;
  6511. } else {
  6512. $arabic = false;
  6513. }
  6514. // check if string contains RTL text
  6515. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6516. $rtlmode = true;
  6517. } else {
  6518. $rtlmode = false;
  6519. }
  6520. // get a char width
  6521. $chrwidth = $this->GetCharWidth(46); // dot character
  6522. // get array of unicode values
  6523. $chars = $this->UTF8StringToArray($s);
  6524. // get array of chars
  6525. $uchars = $this->UTF8ArrayToUniArray($chars);
  6526. // get the number of characters
  6527. $nb = count($chars);
  6528. // replacement for SHY character (minus symbol)
  6529. $shy_replacement = 45;
  6530. $shy_replacement_char = $this->unichr($shy_replacement);
  6531. // widht for SHY replacement
  6532. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6533. // max Y
  6534. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6535. // page width
  6536. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6537. // calculate remaining line width ($w)
  6538. if ($this->rtl) {
  6539. $w = $this->x - $this->lMargin;
  6540. } else {
  6541. $w = $this->w - $this->rMargin - $this->x;
  6542. }
  6543. // max column width
  6544. $wmax = $w - $wadj;
  6545. if (!$firstline) {
  6546. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6547. }
  6548. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6549. // a single character do not fit on column
  6550. return '';
  6551. }
  6552. // minimum row height
  6553. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6554. $start_page = $this->page;
  6555. $i = 0; // character position
  6556. $j = 0; // current starting position
  6557. $sep = -1; // position of the last blank space
  6558. $shy = false; // true if the last blank is a soft hypen (SHY)
  6559. $l = 0; // current string length
  6560. $nl = 0; //number of lines
  6561. $linebreak = false;
  6562. $pc = 0; // previous character
  6563. // for each character
  6564. while ($i < $nb) {
  6565. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6566. break;
  6567. }
  6568. //Get the current character
  6569. $c = $chars[$i];
  6570. if ($c == 10) { // 10 = "\n" = new line
  6571. //Explicit line break
  6572. if ($align == 'J') {
  6573. if ($this->rtl) {
  6574. $talign = 'R';
  6575. } else {
  6576. $talign = 'L';
  6577. }
  6578. } else {
  6579. $talign = $align;
  6580. }
  6581. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6582. if ($firstline) {
  6583. $startx = $this->x;
  6584. $tmparr = array_slice($chars, $j, ($i - $j));
  6585. if ($rtlmode) {
  6586. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6587. }
  6588. $linew = $this->GetArrStringWidth($tmparr);
  6589. unset($tmparr);
  6590. if ($this->rtl) {
  6591. $this->endlinex = $startx - $linew;
  6592. } else {
  6593. $this->endlinex = $startx + $linew;
  6594. }
  6595. $w = $linew;
  6596. $tmpcellpadding = $this->cell_padding;
  6597. if ($maxh == 0) {
  6598. $this->SetCellPadding(0);
  6599. }
  6600. }
  6601. if ($firstblock AND $this->isRTLTextDir()) {
  6602. $tmpstr = $this->stringRightTrim($tmpstr);
  6603. }
  6604. // Skip newlines at the begining of a page or column
  6605. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6606. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6607. }
  6608. unset($tmpstr);
  6609. if ($firstline) {
  6610. $this->cell_padding = $tmpcellpadding;
  6611. return ($this->UniArrSubString($uchars, $i));
  6612. }
  6613. ++$nl;
  6614. $j = $i + 1;
  6615. $l = 0;
  6616. $sep = -1;
  6617. $shy = false;
  6618. // account for margin changes
  6619. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6620. $this->AcceptPageBreak();
  6621. if ($this->rtl) {
  6622. $this->x -= $margin['R'];
  6623. } else {
  6624. $this->x += $margin['L'];
  6625. }
  6626. $this->lMargin += $margin['L'];
  6627. $this->rMargin += $margin['R'];
  6628. }
  6629. $w = $this->getRemainingWidth();
  6630. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6631. } else {
  6632. // 160 is the non-breaking space.
  6633. // 173 is SHY (Soft Hypen).
  6634. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6635. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6636. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6637. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6638. // update last blank space position
  6639. $sep = $i;
  6640. // check if is a SHY
  6641. if ($c == 173) {
  6642. $shy = true;
  6643. if ($pc == 45) {
  6644. $tmp_shy_replacement_width = 0;
  6645. $tmp_shy_replacement_char = '';
  6646. } else {
  6647. $tmp_shy_replacement_width = $shy_replacement_width;
  6648. $tmp_shy_replacement_char = $shy_replacement_char;
  6649. }
  6650. } else {
  6651. $shy = false;
  6652. }
  6653. }
  6654. // update string length
  6655. if ($this->isUnicodeFont() AND ($arabic)) {
  6656. // with bidirectional algorithm some chars may be changed affecting the line length
  6657. // *** very slow ***
  6658. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6659. } else {
  6660. $l += $this->GetCharWidth($c);
  6661. }
  6662. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6663. // we have reached the end of column
  6664. if ($sep == -1) {
  6665. // check if the line was already started
  6666. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6667. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6668. // print a void cell and go to next line
  6669. $this->Cell($w, $h, '', 0, 1);
  6670. $linebreak = true;
  6671. if ($firstline) {
  6672. return ($this->UniArrSubString($uchars, $j));
  6673. }
  6674. } else {
  6675. // truncate the word because do not fit on column
  6676. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6677. if ($firstline) {
  6678. $startx = $this->x;
  6679. $tmparr = array_slice($chars, $j, ($i - $j));
  6680. if ($rtlmode) {
  6681. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6682. }
  6683. $linew = $this->GetArrStringWidth($tmparr);
  6684. unset($tmparr);
  6685. if ($this->rtl) {
  6686. $this->endlinex = $startx - $linew;
  6687. } else {
  6688. $this->endlinex = $startx + $linew;
  6689. }
  6690. $w = $linew;
  6691. $tmpcellpadding = $this->cell_padding;
  6692. if ($maxh == 0) {
  6693. $this->SetCellPadding(0);
  6694. }
  6695. }
  6696. if ($firstblock AND $this->isRTLTextDir()) {
  6697. $tmpstr = $this->stringRightTrim($tmpstr);
  6698. }
  6699. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6700. unset($tmpstr);
  6701. if ($firstline) {
  6702. $this->cell_padding = $tmpcellpadding;
  6703. return ($this->UniArrSubString($uchars, $i));
  6704. }
  6705. $j = $i;
  6706. --$i;
  6707. }
  6708. } else {
  6709. // word wrapping
  6710. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6711. $endspace = 1;
  6712. } else {
  6713. $endspace = 0;
  6714. }
  6715. // check the length of the next string
  6716. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6717. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6718. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6719. // truncate the word because do not fit on a full page width
  6720. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6721. if ($firstline) {
  6722. $startx = $this->x;
  6723. $tmparr = array_slice($chars, $j, ($i - $j));
  6724. if ($rtlmode) {
  6725. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6726. }
  6727. $linew = $this->GetArrStringWidth($tmparr);
  6728. unset($tmparr);
  6729. if ($this->rtl) {
  6730. $this->endlinex = $startx - $linew;
  6731. } else {
  6732. $this->endlinex = $startx + $linew;
  6733. }
  6734. $w = $linew;
  6735. $tmpcellpadding = $this->cell_padding;
  6736. if ($maxh == 0) {
  6737. $this->SetCellPadding(0);
  6738. }
  6739. }
  6740. if ($firstblock AND $this->isRTLTextDir()) {
  6741. $tmpstr = $this->stringRightTrim($tmpstr);
  6742. }
  6743. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6744. unset($tmpstr);
  6745. if ($firstline) {
  6746. $this->cell_padding = $tmpcellpadding;
  6747. return ($this->UniArrSubString($uchars, $i));
  6748. }
  6749. $j = $i;
  6750. --$i;
  6751. } else {
  6752. // word wrapping
  6753. if ($shy) {
  6754. // add hypen (minus symbol) at the end of the line
  6755. $shy_width = $tmp_shy_replacement_width;
  6756. if ($this->rtl) {
  6757. $shy_char_left = $tmp_shy_replacement_char;
  6758. $shy_char_right = '';
  6759. } else {
  6760. $shy_char_left = '';
  6761. $shy_char_right = $tmp_shy_replacement_char;
  6762. }
  6763. } else {
  6764. $shy_width = 0;
  6765. $shy_char_left = '';
  6766. $shy_char_right = '';
  6767. }
  6768. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6769. if ($firstline) {
  6770. $startx = $this->x;
  6771. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6772. if ($rtlmode) {
  6773. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6774. }
  6775. $linew = $this->GetArrStringWidth($tmparr);
  6776. unset($tmparr);
  6777. if ($this->rtl) {
  6778. $this->endlinex = $startx - $linew - $shy_width;
  6779. } else {
  6780. $this->endlinex = $startx + $linew + $shy_width;
  6781. }
  6782. $w = $linew;
  6783. $tmpcellpadding = $this->cell_padding;
  6784. if ($maxh == 0) {
  6785. $this->SetCellPadding(0);
  6786. }
  6787. }
  6788. // print the line
  6789. if ($firstblock AND $this->isRTLTextDir()) {
  6790. $tmpstr = $this->stringRightTrim($tmpstr);
  6791. }
  6792. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6793. unset($tmpstr);
  6794. if ($firstline) {
  6795. // return the remaining text
  6796. $this->cell_padding = $tmpcellpadding;
  6797. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6798. }
  6799. $i = $sep;
  6800. $sep = -1;
  6801. $shy = false;
  6802. $j = ($i+1);
  6803. }
  6804. }
  6805. // account for margin changes
  6806. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6807. $this->AcceptPageBreak();
  6808. if ($this->rtl) {
  6809. $this->x -= $margin['R'];
  6810. } else {
  6811. $this->x += $margin['L'];
  6812. }
  6813. $this->lMargin += $margin['L'];
  6814. $this->rMargin += $margin['R'];
  6815. }
  6816. $w = $this->getRemainingWidth();
  6817. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6818. if ($linebreak) {
  6819. $linebreak = false;
  6820. } else {
  6821. ++$nl;
  6822. $l = 0;
  6823. }
  6824. }
  6825. }
  6826. // save last character
  6827. $pc = $c;
  6828. ++$i;
  6829. } // end while i < nb
  6830. // print last substring (if any)
  6831. if ($l > 0) {
  6832. switch ($align) {
  6833. case 'J':
  6834. case 'C': {
  6835. $w = $w;
  6836. break;
  6837. }
  6838. case 'L': {
  6839. if ($this->rtl) {
  6840. $w = $w;
  6841. } else {
  6842. $w = $l;
  6843. }
  6844. break;
  6845. }
  6846. case 'R': {
  6847. if ($this->rtl) {
  6848. $w = $l;
  6849. } else {
  6850. $w = $w;
  6851. }
  6852. break;
  6853. }
  6854. default: {
  6855. $w = $l;
  6856. break;
  6857. }
  6858. }
  6859. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  6860. if ($firstline) {
  6861. $startx = $this->x;
  6862. $tmparr = array_slice($chars, $j, ($nb - $j));
  6863. if ($rtlmode) {
  6864. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6865. }
  6866. $linew = $this->GetArrStringWidth($tmparr);
  6867. unset($tmparr);
  6868. if ($this->rtl) {
  6869. $this->endlinex = $startx - $linew;
  6870. } else {
  6871. $this->endlinex = $startx + $linew;
  6872. }
  6873. $w = $linew;
  6874. $tmpcellpadding = $this->cell_padding;
  6875. if ($maxh == 0) {
  6876. $this->SetCellPadding(0);
  6877. }
  6878. }
  6879. if ($firstblock AND $this->isRTLTextDir()) {
  6880. $tmpstr = $this->stringRightTrim($tmpstr);
  6881. }
  6882. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6883. unset($tmpstr);
  6884. if ($firstline) {
  6885. $this->cell_padding = $tmpcellpadding;
  6886. return ($this->UniArrSubString($uchars, $nb));
  6887. }
  6888. ++$nl;
  6889. }
  6890. if ($firstline) {
  6891. return '';
  6892. }
  6893. return $nl;
  6894. }
  6895. /**
  6896. * Returns the remaining width between the current position and margins.
  6897. * @return int Return the remaining width
  6898. * @protected
  6899. */
  6900. protected function getRemainingWidth() {
  6901. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6902. if ($this->rtl) {
  6903. return ($this->x - $this->lMargin);
  6904. } else {
  6905. return ($this->w - $this->rMargin - $this->x);
  6906. }
  6907. }
  6908. /**
  6909. * Extract a slice of the $strarr array and return it as string.
  6910. * @param $strarr (string) The input array of characters.
  6911. * @param $start (int) the starting element of $strarr.
  6912. * @param $end (int) first element that will not be returned.
  6913. * @return Return part of a string
  6914. * @public
  6915. */
  6916. public function UTF8ArrSubString($strarr, $start='', $end='') {
  6917. if (strlen($start) == 0) {
  6918. $start = 0;
  6919. }
  6920. if (strlen($end) == 0) {
  6921. $end = count($strarr);
  6922. }
  6923. $string = '';
  6924. for ($i=$start; $i < $end; ++$i) {
  6925. $string .= $this->unichr($strarr[$i]);
  6926. }
  6927. return $string;
  6928. }
  6929. /**
  6930. * Extract a slice of the $uniarr array and return it as string.
  6931. * @param $uniarr (string) The input array of characters.
  6932. * @param $start (int) the starting element of $strarr.
  6933. * @param $end (int) first element that will not be returned.
  6934. * @return Return part of a string
  6935. * @public
  6936. * @since 4.5.037 (2009-04-07)
  6937. */
  6938. public function UniArrSubString($uniarr, $start='', $end='') {
  6939. if (strlen($start) == 0) {
  6940. $start = 0;
  6941. }
  6942. if (strlen($end) == 0) {
  6943. $end = count($uniarr);
  6944. }
  6945. $string = '';
  6946. for ($i=$start; $i < $end; ++$i) {
  6947. $string .= $uniarr[$i];
  6948. }
  6949. return $string;
  6950. }
  6951. /**
  6952. * Convert an array of UTF8 values to array of unicode characters
  6953. * @param $ta (string) The input array of UTF8 values.
  6954. * @return Return array of unicode characters
  6955. * @public
  6956. * @since 4.5.037 (2009-04-07)
  6957. */
  6958. public function UTF8ArrayToUniArray($ta) {
  6959. return array_map(array($this, 'unichr'), $ta);
  6960. }
  6961. /**
  6962. * Returns the unicode caracter specified by UTF-8 value
  6963. * @param $c (int) UTF-8 value
  6964. * @return Returns the specified character.
  6965. * @author Miguel Perez, Nicola Asuni
  6966. * @public
  6967. * @since 2.3.000 (2008-03-05)
  6968. */
  6969. public function unichr($c) {
  6970. if (!$this->isunicode) {
  6971. return chr($c);
  6972. } elseif ($c <= 0x7F) {
  6973. // one byte
  6974. return chr($c);
  6975. } elseif ($c <= 0x7FF) {
  6976. // two bytes
  6977. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  6978. } elseif ($c <= 0xFFFF) {
  6979. // three bytes
  6980. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6981. } elseif ($c <= 0x10FFFF) {
  6982. // four bytes
  6983. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6984. } else {
  6985. return '';
  6986. }
  6987. }
  6988. /**
  6989. * Return the image type given the file name or array returned by getimagesize() function.
  6990. * @param $imgfile (string) image file name
  6991. * @param $iminfo (array) array of image information returned by getimagesize() function.
  6992. * @return string image type
  6993. * @since 4.8.017 (2009-11-27)
  6994. */
  6995. public function getImageFileType($imgfile, $iminfo=array()) {
  6996. $type = '';
  6997. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  6998. $mime = explode('/', $iminfo['mime']);
  6999. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  7000. $type = strtolower(trim($mime[1]));
  7001. }
  7002. }
  7003. if (empty($type)) {
  7004. $fileinfo = pathinfo($imgfile);
  7005. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  7006. $type = strtolower(trim($fileinfo['extension']));
  7007. }
  7008. }
  7009. if ($type == 'jpg') {
  7010. $type = 'jpeg';
  7011. }
  7012. return $type;
  7013. }
  7014. /**
  7015. * Set the block dimensions accounting for page breaks and page/column fitting
  7016. * @param $w (float) width
  7017. * @param $h (float) height
  7018. * @param $x (float) X coordinate
  7019. * @param $y (float) Y coodiante
  7020. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  7021. * @return array($w, $h, $x, $y)
  7022. * @protected
  7023. * @since 5.5.009 (2010-07-05)
  7024. */
  7025. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  7026. if ($w <= 0) {
  7027. // set maximum width
  7028. $w = ($this->w - $this->lMargin - $this->rMargin);
  7029. }
  7030. if ($h <= 0) {
  7031. // set maximum height
  7032. $h = ($this->PageBreakTrigger - $this->tMargin);
  7033. }
  7034. // resize the block to be vertically contained on a single page or single column
  7035. if ($fitonpage OR $this->AutoPageBreak) {
  7036. $ratio_wh = ($w / $h);
  7037. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7038. $h = $this->PageBreakTrigger - $this->tMargin;
  7039. $w = ($h * $ratio_wh);
  7040. }
  7041. // resize the block to be horizontally contained on a single page or single column
  7042. if ($fitonpage) {
  7043. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7044. if ($w > $maxw) {
  7045. $w = $maxw;
  7046. $h = ($w / $ratio_wh);
  7047. }
  7048. }
  7049. }
  7050. // Check whether we need a new page or new column first as this does not fit
  7051. $prev_x = $this->x;
  7052. $prev_y = $this->y;
  7053. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7054. $y = $this->y;
  7055. if ($this->rtl) {
  7056. $x += ($prev_x - $this->x);
  7057. } else {
  7058. $x += ($this->x - $prev_x);
  7059. }
  7060. $this->newline = true;
  7061. }
  7062. // resize the block to be contained on the remaining available page or column space
  7063. if ($fitonpage) {
  7064. $ratio_wh = ($w / $h);
  7065. if (($y + $h) > $this->PageBreakTrigger) {
  7066. $h = $this->PageBreakTrigger - $y;
  7067. $w = ($h * $ratio_wh);
  7068. }
  7069. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7070. $w = $this->w - $this->rMargin - $x;
  7071. $h = ($w / $ratio_wh);
  7072. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7073. $w = $x - $this->lMargin;
  7074. $h = ($w / $ratio_wh);
  7075. }
  7076. }
  7077. return array($w, $h, $x, $y);
  7078. }
  7079. /**
  7080. * Puts an image in the page.
  7081. * The upper-left corner must be given.
  7082. * The dimensions can be specified in different ways:<ul>
  7083. * <li>explicit width and height (expressed in user unit)</li>
  7084. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7085. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7086. * 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;
  7087. * The format can be specified explicitly or inferred from the file extension.<br />
  7088. * It is possible to put a link on the image.<br />
  7089. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7090. * @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').
  7091. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7092. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7093. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7094. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7095. * @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.
  7096. * @param $link (mixed) URL or identifier returned by AddLink().
  7097. * @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>
  7098. * @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).
  7099. * @param $dpi (int) dot-per-inch resolution used on resize
  7100. * @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>
  7101. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7102. * @param $imgmask (mixed) image object returned by this function or false
  7103. * @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)))
  7104. * @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).
  7105. * @param $hidden (boolean) If true do not display the image.
  7106. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7107. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7108. * @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.
  7109. * @return image information
  7110. * @public
  7111. * @since 1.1
  7112. */
  7113. 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()) {
  7114. if ($x === '') {
  7115. $x = $this->x;
  7116. }
  7117. if ($y === '') {
  7118. $y = $this->y;
  7119. }
  7120. // check page for no-write regions and adapt page margins if necessary
  7121. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7122. $cached_file = false; // true when the file is cached
  7123. $exurl = ''; // external streams
  7124. // check if we are passing an image as file or string
  7125. if ($file[0] === '@') {
  7126. // image from string
  7127. $imgdata = substr($file, 1);
  7128. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7129. $fp = fopen($file, 'w');
  7130. fwrite($fp, $imgdata);
  7131. fclose($fp);
  7132. unset($imgdata);
  7133. $cached_file = true;
  7134. $imsize = @getimagesize($file);
  7135. if ($imsize === FALSE) {
  7136. unlink($file);
  7137. $cached_file = false;
  7138. }
  7139. } else { // image file
  7140. if ($file{0} === '*') {
  7141. // image as external stream
  7142. $file = substr($file, 1);
  7143. $exurl = $file;
  7144. }
  7145. // check if is local file
  7146. if (!@file_exists($file)) {
  7147. // encode spaces on filename (file is probably an URL)
  7148. $file = str_replace(' ', '%20', $file);
  7149. }
  7150. if (@file_exists($file)) {
  7151. // get image dimensions
  7152. $imsize = @getimagesize($file);
  7153. } else {
  7154. $imsize = false;
  7155. }
  7156. if ($imsize === FALSE) {
  7157. if (function_exists('curl_init')) {
  7158. // try to get remote file data using cURL
  7159. $cs = curl_init(); // curl session
  7160. curl_setopt($cs, CURLOPT_URL, $file);
  7161. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7162. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7163. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7164. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7165. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7166. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7167. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7168. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7169. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7170. $imgdata = curl_exec($cs);
  7171. curl_close($cs);
  7172. if ($imgdata !== FALSE) {
  7173. // copy image to cache
  7174. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7175. $fp = fopen($file, 'w');
  7176. fwrite($fp, $imgdata);
  7177. fclose($fp);
  7178. unset($imgdata);
  7179. $cached_file = true;
  7180. $imsize = @getimagesize($file);
  7181. if ($imsize === FALSE) {
  7182. unlink($file);
  7183. $cached_file = false;
  7184. }
  7185. }
  7186. } elseif (($w > 0) AND ($h > 0)) {
  7187. // get measures from specified data
  7188. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7189. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7190. $imsize = array($pw, $ph);
  7191. }
  7192. }
  7193. }
  7194. if ($imsize === FALSE) {
  7195. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7196. // get measures from specified data
  7197. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7198. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7199. $imsize = array($pw, $ph);
  7200. } else {
  7201. $this->Error('[Image] Unable to get image: '.$file);
  7202. }
  7203. }
  7204. // file hash
  7205. $filehash = md5($file);
  7206. // get original image width and height in pixels
  7207. list($pixw, $pixh) = $imsize;
  7208. // calculate image width and height on document
  7209. if (($w <= 0) AND ($h <= 0)) {
  7210. // convert image size to document unit
  7211. $w = $this->pixelsToUnits($pixw);
  7212. $h = $this->pixelsToUnits($pixh);
  7213. } elseif ($w <= 0) {
  7214. $w = $h * $pixw / $pixh;
  7215. } elseif ($h <= 0) {
  7216. $h = $w * $pixh / $pixw;
  7217. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7218. if (strlen($fitbox) !== 2) {
  7219. // set default alignment
  7220. $fitbox = '--';
  7221. }
  7222. // scale image dimensions proportionally to fit within the ($w, $h) box
  7223. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7224. // store current height
  7225. $oldh = $h;
  7226. // calculate new height
  7227. $h = $w * $pixh / $pixw;
  7228. // height difference
  7229. $hdiff = ($oldh - $h);
  7230. // vertical alignment
  7231. switch (strtoupper($fitbox{1})) {
  7232. case 'T': {
  7233. break;
  7234. }
  7235. case 'M': {
  7236. $y += ($hdiff / 2);
  7237. break;
  7238. }
  7239. case 'B': {
  7240. $y += $hdiff;
  7241. break;
  7242. }
  7243. }
  7244. } else {
  7245. // store current width
  7246. $oldw = $w;
  7247. // calculate new width
  7248. $w = $h * $pixw / $pixh;
  7249. // width difference
  7250. $wdiff = ($oldw - $w);
  7251. // horizontal alignment
  7252. switch (strtoupper($fitbox{0})) {
  7253. case 'L': {
  7254. if ($this->rtl) {
  7255. $x -= $wdiff;
  7256. }
  7257. break;
  7258. }
  7259. case 'C': {
  7260. if ($this->rtl) {
  7261. $x -= ($wdiff / 2);
  7262. } else {
  7263. $x += ($wdiff / 2);
  7264. }
  7265. break;
  7266. }
  7267. case 'R': {
  7268. if (!$this->rtl) {
  7269. $x += $wdiff;
  7270. }
  7271. break;
  7272. }
  7273. }
  7274. }
  7275. }
  7276. // fit the image on available space
  7277. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7278. // calculate new minimum dimensions in pixels
  7279. $neww = round($w * $this->k * $dpi / $this->dpi);
  7280. $newh = round($h * $this->k * $dpi / $this->dpi);
  7281. // check if resize is necessary (resize is used only to reduce the image)
  7282. $newsize = ($neww * $newh);
  7283. $pixsize = ($pixw * $pixh);
  7284. if (intval($resize) == 2) {
  7285. $resize = true;
  7286. } elseif ($newsize >= $pixsize) {
  7287. $resize = false;
  7288. }
  7289. // check if image has been already added on document
  7290. $newimage = true;
  7291. if (in_array($file, $this->imagekeys)) {
  7292. $newimage = false;
  7293. // get existing image data
  7294. $info = $this->getImageBuffer($file);
  7295. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7296. // check if the newer image is larger
  7297. $oldsize = ($info['w'] * $info['h']);
  7298. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7299. $newimage = true;
  7300. }
  7301. }
  7302. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7303. // check for cached images with alpha channel
  7304. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7305. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7306. if (in_array($tempfile_plain, $this->imagekeys)) {
  7307. // get existing image data
  7308. $info = $this->getImageBuffer($tempfile_plain);
  7309. // check if the newer image is larger
  7310. $oldsize = ($info['w'] * $info['h']);
  7311. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7312. $newimage = true;
  7313. } else {
  7314. $newimage = false;
  7315. // embed mask image
  7316. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7317. // embed image, masked with previously embedded mask
  7318. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7319. }
  7320. }
  7321. }
  7322. if ($newimage) {
  7323. //First use of image, get info
  7324. $type = strtolower($type);
  7325. if ($type == '') {
  7326. $type = $this->getImageFileType($file, $imsize);
  7327. } elseif ($type == 'jpg') {
  7328. $type = 'jpeg';
  7329. }
  7330. $mqr = $this->get_mqr();
  7331. $this->set_mqr(false);
  7332. // Specific image handlers
  7333. $mtd = '_parse'.$type;
  7334. // GD image handler function
  7335. $gdfunction = 'imagecreatefrom'.$type;
  7336. $info = false;
  7337. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7338. // TCPDF image functions
  7339. $info = $this->$mtd($file);
  7340. if ($info == 'pngalpha') {
  7341. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7342. }
  7343. }
  7344. if (!$info) {
  7345. if (function_exists($gdfunction)) {
  7346. // GD library
  7347. $img = $gdfunction($file);
  7348. if ($resize) {
  7349. $imgr = imagecreatetruecolor($neww, $newh);
  7350. if (($type == 'gif') OR ($type == 'png')) {
  7351. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7352. }
  7353. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7354. if (($type == 'gif') OR ($type == 'png')) {
  7355. $info = $this->_toPNG($imgr);
  7356. } else {
  7357. $info = $this->_toJPEG($imgr);
  7358. }
  7359. } else {
  7360. if (($type == 'gif') OR ($type == 'png')) {
  7361. $info = $this->_toPNG($img);
  7362. } else {
  7363. $info = $this->_toJPEG($img);
  7364. }
  7365. }
  7366. } elseif (extension_loaded('imagick')) {
  7367. // ImageMagick library
  7368. $img = new Imagick();
  7369. if ($type == 'SVG') {
  7370. // get SVG file content
  7371. $svgimg = file_get_contents($file);
  7372. // get width and height
  7373. $regs = array();
  7374. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7375. $svgtag = $regs[1];
  7376. $tmp = array();
  7377. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7378. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7379. $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
  7380. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7381. } else {
  7382. $ow = $w;
  7383. }
  7384. $tmp = array();
  7385. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7386. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7387. $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
  7388. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7389. } else {
  7390. $oh = $h;
  7391. }
  7392. $tmp = array();
  7393. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7394. $vbw = ($ow * $this->imgscale * $this->k);
  7395. $vbh = ($oh * $this->imgscale * $this->k);
  7396. $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
  7397. $svgtag = $vbox.$svgtag;
  7398. }
  7399. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7400. }
  7401. $img->readImageBlob($svgimg);
  7402. } else {
  7403. $img->readImage($file);
  7404. }
  7405. if ($resize) {
  7406. $img->resizeImage($neww, $newh, 10, 1, false);
  7407. }
  7408. $img->setCompressionQuality($this->jpeg_quality);
  7409. $img->setImageFormat('jpeg');
  7410. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7411. $img->writeImage($tempname);
  7412. $info = $this->_parsejpeg($tempname);
  7413. unlink($tempname);
  7414. $img->destroy();
  7415. } else {
  7416. return;
  7417. }
  7418. }
  7419. if ($info === false) {
  7420. //If false, we cannot process image
  7421. return;
  7422. }
  7423. $this->set_mqr($mqr);
  7424. if ($ismask) {
  7425. // force grayscale
  7426. $info['cs'] = 'DeviceGray';
  7427. }
  7428. $info['i'] = $this->numimages;
  7429. if (!in_array($file, $this->imagekeys)) {
  7430. ++$info['i'];
  7431. }
  7432. if ($imgmask !== false) {
  7433. $info['masked'] = $imgmask;
  7434. }
  7435. if (!empty($exurl)) {
  7436. $info['exurl'] = $exurl;
  7437. }
  7438. // array of alternative images
  7439. $info['altimgs'] = $altimgs;
  7440. // add image to document
  7441. $this->setImageBuffer($file, $info);
  7442. }
  7443. if ($cached_file) {
  7444. // remove cached file
  7445. unlink($file);
  7446. }
  7447. // set alignment
  7448. $this->img_rb_y = $y + $h;
  7449. // set alignment
  7450. if ($this->rtl) {
  7451. if ($palign == 'L') {
  7452. $ximg = $this->lMargin;
  7453. } elseif ($palign == 'C') {
  7454. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7455. } elseif ($palign == 'R') {
  7456. $ximg = $this->w - $this->rMargin - $w;
  7457. } else {
  7458. $ximg = $x - $w;
  7459. }
  7460. $this->img_rb_x = $ximg;
  7461. } else {
  7462. if ($palign == 'L') {
  7463. $ximg = $this->lMargin;
  7464. } elseif ($palign == 'C') {
  7465. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7466. } elseif ($palign == 'R') {
  7467. $ximg = $this->w - $this->rMargin - $w;
  7468. } else {
  7469. $ximg = $x;
  7470. }
  7471. $this->img_rb_x = $ximg + $w;
  7472. }
  7473. if ($ismask OR $hidden) {
  7474. // image is not displayed
  7475. return $info['i'];
  7476. }
  7477. $xkimg = $ximg * $this->k;
  7478. if (!$alt) {
  7479. // only non-alternative immages will be set
  7480. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  7481. }
  7482. if (!empty($border)) {
  7483. $bx = $this->x;
  7484. $by = $this->y;
  7485. $this->x = $ximg;
  7486. if ($this->rtl) {
  7487. $this->x += $w;
  7488. }
  7489. $this->y = $y;
  7490. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7491. $this->x = $bx;
  7492. $this->y = $by;
  7493. }
  7494. if ($link) {
  7495. $this->Link($ximg, $y, $w, $h, $link, 0);
  7496. }
  7497. // set pointer to align the next text/objects
  7498. switch($align) {
  7499. case 'T': {
  7500. $this->y = $y;
  7501. $this->x = $this->img_rb_x;
  7502. break;
  7503. }
  7504. case 'M': {
  7505. $this->y = $y + round($h/2);
  7506. $this->x = $this->img_rb_x;
  7507. break;
  7508. }
  7509. case 'B': {
  7510. $this->y = $this->img_rb_y;
  7511. $this->x = $this->img_rb_x;
  7512. break;
  7513. }
  7514. case 'N': {
  7515. $this->SetY($this->img_rb_y);
  7516. break;
  7517. }
  7518. default:{
  7519. break;
  7520. }
  7521. }
  7522. $this->endlinex = $this->img_rb_x;
  7523. if ($this->inxobj) {
  7524. // we are inside an XObject template
  7525. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7526. }
  7527. return $info['i'];
  7528. }
  7529. /**
  7530. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7531. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7532. * @since 4.6.025 (2009-08-17)
  7533. */
  7534. public function set_mqr($mqr) {
  7535. if (!defined('PHP_VERSION_ID')) {
  7536. $version = PHP_VERSION;
  7537. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7538. }
  7539. if (PHP_VERSION_ID < 50300) {
  7540. @set_magic_quotes_runtime($mqr);
  7541. }
  7542. }
  7543. /**
  7544. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7545. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7546. * @since 4.6.025 (2009-08-17)
  7547. */
  7548. public function get_mqr() {
  7549. if (!defined('PHP_VERSION_ID')) {
  7550. $version = PHP_VERSION;
  7551. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7552. }
  7553. if (PHP_VERSION_ID < 50300) {
  7554. return @get_magic_quotes_runtime();
  7555. }
  7556. return 0;
  7557. }
  7558. /**
  7559. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7560. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7561. * @param $image (image) Image object.
  7562. * return image JPEG image object.
  7563. * @protected
  7564. */
  7565. protected function _toJPEG($image) {
  7566. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7567. imagejpeg($image, $tempname, $this->jpeg_quality);
  7568. imagedestroy($image);
  7569. $retvars = $this->_parsejpeg($tempname);
  7570. // tidy up by removing temporary image
  7571. unlink($tempname);
  7572. return $retvars;
  7573. }
  7574. /**
  7575. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7576. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7577. * @param $image (image) Image object.
  7578. * return image PNG image object.
  7579. * @protected
  7580. * @since 4.9.016 (2010-04-20)
  7581. */
  7582. protected function _toPNG($image) {
  7583. // set temporary image file name
  7584. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7585. // turn off interlaced mode
  7586. imageinterlace($image, 0);
  7587. // create temporary PNG image
  7588. imagepng($image, $tempname);
  7589. // remove image from memory
  7590. imagedestroy($image);
  7591. // get PNG image data
  7592. $retvars = $this->_parsepng($tempname);
  7593. // tidy up by removing temporary image
  7594. unlink($tempname);
  7595. return $retvars;
  7596. }
  7597. /**
  7598. * Set the transparency for the given GD image.
  7599. * @param $new_image (image) GD image object
  7600. * @param $image (image) GD image object.
  7601. * return GD image object.
  7602. * @protected
  7603. * @since 4.9.016 (2010-04-20)
  7604. */
  7605. protected function _setGDImageTransparency($new_image, $image) {
  7606. // transparency index
  7607. $tid = imagecolortransparent($image);
  7608. // default transparency color
  7609. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7610. if ($tid >= 0) {
  7611. // get the colors for the transparency index
  7612. $tcol = imagecolorsforindex($image, $tid);
  7613. }
  7614. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7615. imagefill($new_image, 0, 0, $tid);
  7616. imagecolortransparent($new_image, $tid);
  7617. return $new_image;
  7618. }
  7619. /**
  7620. * Extract info from a JPEG file without using the GD library.
  7621. * @param $file (string) image file to parse
  7622. * @return array structure containing the image data
  7623. * @protected
  7624. */
  7625. protected function _parsejpeg($file) {
  7626. $a = getimagesize($file);
  7627. if (empty($a)) {
  7628. $this->Error('Missing or incorrect image file: '.$file);
  7629. }
  7630. if ($a[2] != 2) {
  7631. $this->Error('Not a JPEG file: '.$file);
  7632. }
  7633. // bits per pixel
  7634. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7635. // number of image channels
  7636. if (!isset($a['channels'])) {
  7637. $channels = 3;
  7638. } else {
  7639. $channels = intval($a['channels']);
  7640. }
  7641. // default colour space
  7642. switch ($channels) {
  7643. case 1: {
  7644. $colspace = 'DeviceGray';
  7645. break;
  7646. }
  7647. case 3: {
  7648. $colspace = 'DeviceRGB';
  7649. break;
  7650. }
  7651. case 4: {
  7652. $colspace = 'DeviceCMYK';
  7653. break;
  7654. }
  7655. default: {
  7656. $channels = 3;
  7657. $colspace = 'DeviceRGB';
  7658. break;
  7659. }
  7660. }
  7661. // get file content
  7662. $data = file_get_contents($file);
  7663. // check for embedded ICC profile
  7664. $icc = array();
  7665. $offset = 0;
  7666. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7667. // get ICC sequence length
  7668. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7669. // marker sequence number
  7670. $msn = max(1, ord($data[($pos + 12)]));
  7671. // number of markers (total of APP2 used)
  7672. $nom = max(1, ord($data[($pos + 13)]));
  7673. // get sequence segment
  7674. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7675. // move forward to next sequence
  7676. $offset = ($pos + 14 + $length);
  7677. }
  7678. // order and compact ICC segments
  7679. if (count($icc) > 0) {
  7680. ksort($icc);
  7681. $icc = implode('', $icc);
  7682. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7683. // invalid ICC profile
  7684. $icc = false;
  7685. }
  7686. } else {
  7687. $icc = false;
  7688. }
  7689. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7690. }
  7691. /**
  7692. * Extract info from a PNG file without using the GD library.
  7693. * @param $file (string) image file to parse
  7694. * @return array structure containing the image data
  7695. * @protected
  7696. */
  7697. protected function _parsepng($file) {
  7698. $f = fopen($file, 'rb');
  7699. if ($f === false) {
  7700. $this->Error('Can\'t open image file: '.$file);
  7701. }
  7702. //Check signature
  7703. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7704. $this->Error('Not a PNG file: '.$file);
  7705. }
  7706. //Read header chunk
  7707. fread($f, 4);
  7708. if (fread($f, 4) != 'IHDR') {
  7709. $this->Error('Incorrect PNG file: '.$file);
  7710. }
  7711. $w = $this->_freadint($f);
  7712. $h = $this->_freadint($f);
  7713. $bpc = ord(fread($f, 1));
  7714. if ($bpc > 8) {
  7715. //$this->Error('16-bit depth not supported: '.$file);
  7716. fclose($f);
  7717. return false;
  7718. }
  7719. $ct = ord(fread($f, 1));
  7720. if ($ct == 0) {
  7721. $colspace = 'DeviceGray';
  7722. } elseif ($ct == 2) {
  7723. $colspace = 'DeviceRGB';
  7724. } elseif ($ct == 3) {
  7725. $colspace = 'Indexed';
  7726. } else {
  7727. // alpha channel
  7728. fclose($f);
  7729. return 'pngalpha';
  7730. }
  7731. if (ord(fread($f, 1)) != 0) {
  7732. //$this->Error('Unknown compression method: '.$file);
  7733. fclose($f);
  7734. return false;
  7735. }
  7736. if (ord(fread($f, 1)) != 0) {
  7737. //$this->Error('Unknown filter method: '.$file);
  7738. fclose($f);
  7739. return false;
  7740. }
  7741. if (ord(fread($f, 1)) != 0) {
  7742. //$this->Error('Interlacing not supported: '.$file);
  7743. fclose($f);
  7744. return false;
  7745. }
  7746. fread($f, 4);
  7747. $channels = ($ct == 2 ? 3 : 1);
  7748. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7749. //Scan chunks looking for palette, transparency and image data
  7750. $pal = '';
  7751. $trns = '';
  7752. $data = '';
  7753. $icc = false;
  7754. do {
  7755. $n = $this->_freadint($f);
  7756. $type = fread($f, 4);
  7757. if ($type == 'PLTE') {
  7758. // read palette
  7759. $pal = $this->rfread($f, $n);
  7760. fread($f, 4);
  7761. } elseif ($type == 'tRNS') {
  7762. // read transparency info
  7763. $t = $this->rfread($f, $n);
  7764. if ($ct == 0) {
  7765. $trns = array(ord($t{1}));
  7766. } elseif ($ct == 2) {
  7767. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7768. } else {
  7769. $pos = strpos($t, chr(0));
  7770. if ($pos !== false) {
  7771. $trns = array($pos);
  7772. }
  7773. }
  7774. fread($f, 4);
  7775. } elseif ($type == 'IDAT') {
  7776. // read image data block
  7777. $data .= $this->rfread($f, $n);
  7778. fread($f, 4);
  7779. } elseif ($type == 'iCCP') {
  7780. // skip profile name and null separator
  7781. $len = 0;
  7782. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7783. ++$len;
  7784. }
  7785. // get compression method
  7786. if (ord(fread($f, 1)) != 0) {
  7787. //$this->Error('Unknown filter method: '.$file);
  7788. fclose($f);
  7789. return false;
  7790. }
  7791. // read ICC Color Profile
  7792. $icc = $this->rfread($f, ($n - $len - 2));
  7793. // decompress profile
  7794. $icc = gzuncompress($icc);
  7795. fread($f, 4);
  7796. } elseif ($type == 'IEND') {
  7797. break;
  7798. } else {
  7799. $this->rfread($f, $n + 4);
  7800. }
  7801. } while ($n);
  7802. if (($colspace == 'Indexed') AND (empty($pal))) {
  7803. //$this->Error('Missing palette in '.$file);
  7804. fclose($f);
  7805. return false;
  7806. }
  7807. fclose($f);
  7808. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7809. }
  7810. /**
  7811. * Binary-safe and URL-safe file read.
  7812. * Reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met: length bytes have been read; EOF (end of file) is reached.
  7813. * @param $handle (resource)
  7814. * @param $length (int)
  7815. * @return Returns the read string or FALSE in case of error.
  7816. * @author Nicola Asuni
  7817. * @protected
  7818. * @since 4.5.027 (2009-03-16)
  7819. */
  7820. protected function rfread($handle, $length) {
  7821. $data = fread($handle, $length);
  7822. if ($data === false) {
  7823. return false;
  7824. }
  7825. $rest = $length - strlen($data);
  7826. if ($rest > 0) {
  7827. $data .= $this->rfread($handle, $rest);
  7828. }
  7829. return $data;
  7830. }
  7831. /**
  7832. * Extract info from a PNG image with alpha channel using the GD library.
  7833. * @param $file (string) Name of the file containing the image.
  7834. * @param $x (float) Abscissa of the upper-left corner.
  7835. * @param $y (float) Ordinate of the upper-left corner.
  7836. * @param $wpx (float) Original width of the image in pixels.
  7837. * @param $hpx (float) original height of the image in pixels.
  7838. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7839. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7840. * @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.
  7841. * @param $link (mixed) URL or identifier returned by AddLink().
  7842. * @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>
  7843. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7844. * @param $dpi (int) dot-per-inch resolution used on resize
  7845. * @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>
  7846. * @param $filehash (string) File hash used to build unique file names.
  7847. * @author Nicola Asuni
  7848. * @protected
  7849. * @since 4.3.007 (2008-12-04)
  7850. * @see Image()
  7851. */
  7852. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  7853. if (empty($filehash)) {
  7854. $filehash = md5($file);
  7855. }
  7856. // create temp image file (without alpha channel)
  7857. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7858. // create temp alpha file
  7859. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7860. if (extension_loaded('imagick')) { // ImageMagick extension
  7861. // ImageMagick library
  7862. $img = new Imagick();
  7863. $img->readImage($file);
  7864. // clone image object
  7865. $imga = $img->clone();
  7866. // extract alpha channel
  7867. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  7868. $img->negateImage(true);
  7869. $img->setImageFormat('png');
  7870. $img->writeImage($tempfile_alpha);
  7871. // remove alpha channel
  7872. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  7873. $imga->setImageFormat('png');
  7874. $imga->writeImage($tempfile_plain);
  7875. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  7876. // generate images
  7877. $img = imagecreatefrompng($file);
  7878. $imgalpha = imagecreate($wpx, $hpx);
  7879. // generate gray scale palette (0 -> 255)
  7880. for ($c = 0; $c < 256; ++$c) {
  7881. ImageColorAllocate($imgalpha, $c, $c, $c);
  7882. }
  7883. // extract alpha channel
  7884. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7885. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7886. $color = imagecolorat($img, $xpx, $ypx);
  7887. $alpha = ($color >> 24); // shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
  7888. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  7889. $alpha = $this->getGDgamma($alpha); // correct gamma
  7890. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  7891. }
  7892. }
  7893. imagepng($imgalpha, $tempfile_alpha);
  7894. imagedestroy($imgalpha);
  7895. // extract image without alpha channel
  7896. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7897. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7898. imagepng($imgplain, $tempfile_plain);
  7899. imagedestroy($imgplain);
  7900. } else {
  7901. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  7902. }
  7903. // embed mask image
  7904. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7905. // embed image, masked with previously embedded mask
  7906. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7907. // remove temp files
  7908. unlink($tempfile_alpha);
  7909. unlink($tempfile_plain);
  7910. }
  7911. /**
  7912. * Correct the gamma value to be used with GD library
  7913. * @param $v (float) the gamma value to be corrected
  7914. * @protected
  7915. * @since 4.3.007 (2008-12-04)
  7916. */
  7917. protected function getGDgamma($v) {
  7918. return (pow(($v / 255), 2.2) * 255);
  7919. }
  7920. /**
  7921. * Performs a line break.
  7922. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7923. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  7924. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  7925. * @public
  7926. * @since 1.0
  7927. * @see Cell()
  7928. */
  7929. public function Ln($h='', $cell=false) {
  7930. 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'])) {
  7931. // revove vertical space from the top of the column
  7932. return;
  7933. }
  7934. if ($cell) {
  7935. if ($this->rtl) {
  7936. $cellpadding = $this->cell_padding['R'];
  7937. } else {
  7938. $cellpadding = $this->cell_padding['L'];
  7939. }
  7940. } else {
  7941. $cellpadding = 0;
  7942. }
  7943. if ($this->rtl) {
  7944. $this->x = $this->w - $this->rMargin - $cellpadding;
  7945. } else {
  7946. $this->x = $this->lMargin + $cellpadding;
  7947. }
  7948. if (is_string($h)) {
  7949. $this->y += $this->lasth;
  7950. } else {
  7951. $this->y += $h;
  7952. }
  7953. $this->newline = true;
  7954. }
  7955. /**
  7956. * Returns the relative X value of current position.
  7957. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7958. * @return float
  7959. * @public
  7960. * @since 1.2
  7961. * @see SetX(), GetY(), SetY()
  7962. */
  7963. public function GetX() {
  7964. //Get x position
  7965. if ($this->rtl) {
  7966. return ($this->w - $this->x);
  7967. } else {
  7968. return $this->x;
  7969. }
  7970. }
  7971. /**
  7972. * Returns the absolute X value of current position.
  7973. * @return float
  7974. * @public
  7975. * @since 1.2
  7976. * @see SetX(), GetY(), SetY()
  7977. */
  7978. public function GetAbsX() {
  7979. return $this->x;
  7980. }
  7981. /**
  7982. * Returns the ordinate of the current position.
  7983. * @return float
  7984. * @public
  7985. * @since 1.0
  7986. * @see SetY(), GetX(), SetX()
  7987. */
  7988. public function GetY() {
  7989. return $this->y;
  7990. }
  7991. /**
  7992. * Defines the abscissa of the current position.
  7993. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7994. * @param $x (float) The value of the abscissa.
  7995. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7996. * @public
  7997. * @since 1.2
  7998. * @see GetX(), GetY(), SetY(), SetXY()
  7999. */
  8000. public function SetX($x, $rtloff=false) {
  8001. if (!$rtloff AND $this->rtl) {
  8002. if ($x >= 0) {
  8003. $this->x = $this->w - $x;
  8004. } else {
  8005. $this->x = abs($x);
  8006. }
  8007. } else {
  8008. if ($x >= 0) {
  8009. $this->x = $x;
  8010. } else {
  8011. $this->x = $this->w + $x;
  8012. }
  8013. }
  8014. if ($this->x < 0) {
  8015. $this->x = 0;
  8016. }
  8017. if ($this->x > $this->w) {
  8018. $this->x = $this->w;
  8019. }
  8020. }
  8021. /**
  8022. * Moves the current abscissa back to the left margin and sets the ordinate.
  8023. * If the passed value is negative, it is relative to the bottom of the page.
  8024. * @param $y (float) The value of the ordinate.
  8025. * @param $resetx (bool) if true (default) reset the X position.
  8026. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8027. * @public
  8028. * @since 1.0
  8029. * @see GetX(), GetY(), SetY(), SetXY()
  8030. */
  8031. public function SetY($y, $resetx=true, $rtloff=false) {
  8032. if ($resetx) {
  8033. //reset x
  8034. if (!$rtloff AND $this->rtl) {
  8035. $this->x = $this->w - $this->rMargin;
  8036. } else {
  8037. $this->x = $this->lMargin;
  8038. }
  8039. }
  8040. if ($y >= 0) {
  8041. $this->y = $y;
  8042. } else {
  8043. $this->y = $this->h + $y;
  8044. }
  8045. if ($this->y < 0) {
  8046. $this->y = 0;
  8047. }
  8048. if ($this->y > $this->h) {
  8049. $this->y = $this->h;
  8050. }
  8051. }
  8052. /**
  8053. * Defines the abscissa and ordinate of the current position.
  8054. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8055. * @param $x (float) The value of the abscissa.
  8056. * @param $y (float) The value of the ordinate.
  8057. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8058. * @public
  8059. * @since 1.2
  8060. * @see SetX(), SetY()
  8061. */
  8062. public function SetXY($x, $y, $rtloff=false) {
  8063. $this->SetY($y, false, $rtloff);
  8064. $this->SetX($x, $rtloff);
  8065. }
  8066. /**
  8067. * Ouput input data and compress it if possible.
  8068. * @param $data (string) Data to output.
  8069. * @param $length (int) Data length in bytes.
  8070. * @protected
  8071. * @since 5.9.086
  8072. */
  8073. protected function sendOutputData($data, $length) {
  8074. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8075. // the content length may vary if the server is using compression
  8076. header('Content-Length: '.$length);
  8077. }
  8078. echo $data;
  8079. }
  8080. /**
  8081. * Send the document to a given destination: string, local file or browser.
  8082. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8083. * The method first calls Close() if necessary to terminate the document.
  8084. * @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.
  8085. * @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>
  8086. * @public
  8087. * @since 1.0
  8088. * @see Close()
  8089. */
  8090. public function Output($name='doc.pdf', $dest='I') {
  8091. //Output PDF to some destination
  8092. //Finish document if necessary
  8093. if ($this->state < 3) {
  8094. $this->Close();
  8095. }
  8096. //Normalize parameters
  8097. if (is_bool($dest)) {
  8098. $dest = $dest ? 'D' : 'F';
  8099. }
  8100. $dest = strtoupper($dest);
  8101. if ($dest{0} != 'F') {
  8102. $name = preg_replace('/[\s]+/', '_', $name);
  8103. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8104. }
  8105. if ($this->sign) {
  8106. // *** apply digital signature to the document ***
  8107. // get the document content
  8108. $pdfdoc = $this->getBuffer();
  8109. // remove last newline
  8110. $pdfdoc = substr($pdfdoc, 0, -1);
  8111. // Remove the original buffer
  8112. if (isset($this->diskcache) AND $this->diskcache) {
  8113. // remove buffer file from cache
  8114. unlink($this->buffer);
  8115. }
  8116. unset($this->buffer);
  8117. // remove filler space
  8118. $byterange_string_len = strlen($this->byterange_string);
  8119. // define the ByteRange
  8120. $byte_range = array();
  8121. $byte_range[0] = 0;
  8122. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8123. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8124. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8125. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8126. // replace the ByteRange
  8127. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8128. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8129. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8130. // write the document to a temporary folder
  8131. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8132. $f = fopen($tempdoc, 'wb');
  8133. if (!$f) {
  8134. $this->Error('Unable to create temporary file: '.$tempdoc);
  8135. }
  8136. $pdfdoc_length = strlen($pdfdoc);
  8137. fwrite($f, $pdfdoc, $pdfdoc_length);
  8138. fclose($f);
  8139. // get digital signature via openssl library
  8140. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8141. if (empty($this->signature_data['extracerts'])) {
  8142. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8143. } else {
  8144. 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']);
  8145. }
  8146. unlink($tempdoc);
  8147. // read signature
  8148. $signature = file_get_contents($tempsign);
  8149. unlink($tempsign);
  8150. // extract signature
  8151. $signature = substr($signature, $pdfdoc_length);
  8152. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8153. $tmparr = explode("\n\n", $signature);
  8154. $signature = $tmparr[1];
  8155. unset($tmparr);
  8156. // decode signature
  8157. $signature = base64_decode(trim($signature));
  8158. // convert signature to hex
  8159. $signature = current(unpack('H*', $signature));
  8160. $signature = str_pad($signature, $this->signature_max_length, '0');
  8161. // disable disk caching
  8162. $this->diskcache = false;
  8163. // Add signature to the document
  8164. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8165. $this->bufferlen = strlen($this->buffer);
  8166. }
  8167. switch($dest) {
  8168. case 'I': {
  8169. // Send PDF to the standard output
  8170. if (ob_get_contents()) {
  8171. $this->Error('Some data has already been output, can\'t send PDF file');
  8172. }
  8173. if (php_sapi_name() != 'cli') {
  8174. // send output to a browser
  8175. header('Content-Type: application/pdf');
  8176. if (headers_sent()) {
  8177. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8178. }
  8179. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8180. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8181. header('Pragma: public');
  8182. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8183. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8184. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8185. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8186. } else {
  8187. echo $this->getBuffer();
  8188. }
  8189. break;
  8190. }
  8191. case 'D': {
  8192. // download PDF as file
  8193. if (ob_get_contents()) {
  8194. $this->Error('Some data has already been output, can\'t send PDF file');
  8195. }
  8196. header('Content-Description: File Transfer');
  8197. if (headers_sent()) {
  8198. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8199. }
  8200. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8201. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8202. header('Pragma: public');
  8203. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8204. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8205. // force download dialog
  8206. if (strpos(php_sapi_name(), 'cgi') === false) {
  8207. header('Content-Type: application/force-download');
  8208. header('Content-Type: application/octet-stream', false);
  8209. header('Content-Type: application/download', false);
  8210. header('Content-Type: application/pdf', false);
  8211. } else {
  8212. header('Content-Type: application/pdf');
  8213. }
  8214. // use the Content-Disposition header to supply a recommended filename
  8215. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8216. header('Content-Transfer-Encoding: binary');
  8217. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8218. break;
  8219. }
  8220. case 'F':
  8221. case 'FI':
  8222. case 'FD': {
  8223. // save PDF to a local file
  8224. if ($this->diskcache) {
  8225. copy($this->buffer, $name);
  8226. } else {
  8227. $f = fopen($name, 'wb');
  8228. if (!$f) {
  8229. $this->Error('Unable to create output file: '.$name);
  8230. }
  8231. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8232. fclose($f);
  8233. }
  8234. if ($dest == 'FI') {
  8235. // send headers to browser
  8236. header('Content-Type: application/pdf');
  8237. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8238. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8239. header('Pragma: public');
  8240. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8241. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8242. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8243. $this->sendOutputData(file_get_contents($name), filesize($name));
  8244. } elseif ($dest == 'FD') {
  8245. // send headers to browser
  8246. if (ob_get_contents()) {
  8247. $this->Error('Some data has already been output, can\'t send PDF file');
  8248. }
  8249. header('Content-Description: File Transfer');
  8250. if (headers_sent()) {
  8251. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8252. }
  8253. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8254. header('Pragma: public');
  8255. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8256. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8257. // force download dialog
  8258. if (strpos(php_sapi_name(), 'cgi') === false) {
  8259. header('Content-Type: application/force-download');
  8260. header('Content-Type: application/octet-stream', false);
  8261. header('Content-Type: application/download', false);
  8262. header('Content-Type: application/pdf', false);
  8263. } else {
  8264. header('Content-Type: application/pdf');
  8265. }
  8266. // use the Content-Disposition header to supply a recommended filename
  8267. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8268. header('Content-Transfer-Encoding: binary');
  8269. $this->sendOutputData(file_get_contents($name), filesize($name));
  8270. }
  8271. break;
  8272. }
  8273. case 'E': {
  8274. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8275. $retval = 'Content-Type: application/pdf;'."\r\n";
  8276. $retval .= ' name="'.$name.'"'."\r\n";
  8277. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8278. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8279. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8280. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8281. return $retval;
  8282. }
  8283. case 'S': {
  8284. // returns PDF as a string
  8285. return $this->getBuffer();
  8286. }
  8287. default: {
  8288. $this->Error('Incorrect output destination: '.$dest);
  8289. }
  8290. }
  8291. return '';
  8292. }
  8293. /**
  8294. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8295. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8296. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8297. * @public
  8298. * @since 4.5.016 (2009-02-24)
  8299. */
  8300. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8301. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8302. // remove buffer file from cache
  8303. unlink($this->buffer);
  8304. }
  8305. foreach (array_keys(get_object_vars($this)) as $val) {
  8306. if ($destroyall OR (
  8307. ($val != 'internal_encoding')
  8308. AND ($val != 'state')
  8309. AND ($val != 'bufferlen')
  8310. AND ($val != 'buffer')
  8311. AND ($val != 'diskcache')
  8312. AND ($val != 'sign')
  8313. AND ($val != 'signature_data')
  8314. AND ($val != 'signature_max_length')
  8315. AND ($val != 'byterange_string')
  8316. )) {
  8317. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8318. unset($this->$val);
  8319. }
  8320. }
  8321. }
  8322. }
  8323. /**
  8324. * Check for locale-related bug
  8325. * @protected
  8326. */
  8327. protected function _dochecks() {
  8328. //Check for locale-related bug
  8329. if (1.1 == 1) {
  8330. $this->Error('Don\'t alter the locale before including class file');
  8331. }
  8332. //Check for decimal separator
  8333. if (sprintf('%.1F', 1.0) != '1.0') {
  8334. setlocale(LC_NUMERIC, 'C');
  8335. }
  8336. }
  8337. /**
  8338. * Return fonts path
  8339. * @return string
  8340. * @protected
  8341. */
  8342. protected function _getfontpath() {
  8343. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8344. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8345. }
  8346. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8347. }
  8348. /**
  8349. * Return an array containing variations for the basic page number alias.
  8350. * @param $a (string) Base alias.
  8351. * @return array of page number aliases
  8352. * @protected
  8353. */
  8354. protected function getInternalPageNumberAliases($a= '') {
  8355. $alias = array();
  8356. // build array of Unicode + ASCII variants (the order is important)
  8357. $alias = array('u' => array(), 'a' => array());
  8358. $u = '{'.$a.'}';
  8359. $alias['u'][] = $this->_escape($u);
  8360. if ($this->isunicode) {
  8361. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8362. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8363. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8364. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8365. }
  8366. $alias['a'][] = $this->_escape($a);
  8367. return $alias;
  8368. }
  8369. /**
  8370. * Return an array containing all internal page aliases.
  8371. * @return array of page number aliases
  8372. * @protected
  8373. */
  8374. protected function getAllInternalPageNumberAliases() {
  8375. $basic_alias = array($this->alias_tot_pages, $this->alias_num_page, $this->alias_group_tot_pages, $this->alias_group_num_page, $this->alias_right_shift);
  8376. $pnalias = array();
  8377. foreach($basic_alias as $k => $a) {
  8378. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8379. }
  8380. return $pnalias;
  8381. }
  8382. /**
  8383. * Replace page number aliases with number.
  8384. * @param $page (string) Page content.
  8385. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8386. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8387. * @return replaced page content and updated $diff parameter as array.
  8388. * @protected
  8389. */
  8390. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8391. foreach ($replace as $rep) {
  8392. foreach ($rep[3] as $a) {
  8393. if (strpos($page, $a) !== false) {
  8394. $page = str_replace($a, $rep[0], $page);
  8395. $diff += ($rep[2] - $rep[1]);
  8396. }
  8397. }
  8398. }
  8399. return array($page, $diff);
  8400. }
  8401. /**
  8402. * Replace right shift page number aliases with spaces to correct right alignment.
  8403. * This works perfectly only when using monospaced fonts.
  8404. * @param $page (string) Page content.
  8405. * @param $aliases (array) Array of page aliases.
  8406. * @param $diff (int) initial difference to add.
  8407. * @return replaced page content.
  8408. * @protected
  8409. */
  8410. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8411. foreach ($aliases as $type => $alias) {
  8412. foreach ($alias as $a) {
  8413. // find position of compensation factor
  8414. $startnum = (strpos($a, ':') + 1);
  8415. $a = substr($a, 0, $startnum);
  8416. if (($pos = strpos($page, $a)) !== false) {
  8417. // end of alias
  8418. $endnum = strpos($page, '}', $pos);
  8419. // string to be replaced
  8420. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8421. // get compensation factor
  8422. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8423. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8424. $ratio = floatval($ratio);
  8425. if ($type == 'u') {
  8426. $chrdiff = floor(($diff + 12) * $ratio);
  8427. $shift = str_repeat(' ', $chrdiff);
  8428. $shift = $this->UTF8ToUTF16BE($shift, false);
  8429. } else {
  8430. $chrdiff = floor(($diff + 11) * $ratio);
  8431. $shift = str_repeat(' ', $chrdiff);
  8432. }
  8433. $page = str_replace($aa, $shift, $page);
  8434. }
  8435. }
  8436. }
  8437. return $page;
  8438. }
  8439. /**
  8440. * Output pages (and replace page number aliases).
  8441. * @protected
  8442. */
  8443. protected function _putpages() {
  8444. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8445. // get internal aliases for page numbers
  8446. $pnalias = $this->getAllInternalPageNumberAliases();
  8447. $num_pages = $this->numpages;
  8448. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8449. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8450. $ptp_num_chars = $this->GetNumChars($ptpa);
  8451. $pagegroupnum = 0;
  8452. $groupnum = 0;
  8453. $ptgu = 1;
  8454. $ptga = 1;
  8455. for ($n = 1; $n <= $num_pages; ++$n) {
  8456. // get current page
  8457. $temppage = $this->getPageBuffer($n);
  8458. $pagelen = strlen($temppage);
  8459. // set replacements for total pages number
  8460. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8461. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8462. $pnp_num_chars = $this->GetNumChars($pnpa);
  8463. $pdiff = 0; // difference used for right shift alignment of page numbers
  8464. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8465. if (!empty($this->pagegroups)) {
  8466. if (isset($this->newpagegroup[$n])) {
  8467. $pagegroupnum = 0;
  8468. ++$groupnum;
  8469. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8470. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8471. $ptg_num_chars = $this->GetNumChars($ptga);
  8472. }
  8473. ++$pagegroupnum;
  8474. $pnga = $this->formatPageNumber($pagegroupnum);
  8475. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8476. $png_num_chars = $this->GetNumChars($pnga);
  8477. // replace page numbers
  8478. $replace = array();
  8479. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8480. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8481. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8482. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8483. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8484. }
  8485. // replace page numbers
  8486. $replace = array();
  8487. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8488. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8489. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8490. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8491. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8492. // replace right shift alias
  8493. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8494. // replace EPS marker
  8495. $temppage = str_replace($this->epsmarker, '', $temppage);
  8496. //Page
  8497. $this->page_obj_id[$n] = $this->_newobj();
  8498. $out = '<<';
  8499. $out .= ' /Type /Page';
  8500. $out .= ' /Parent 1 0 R';
  8501. $out .= ' /LastModified '.$this->_datestring();
  8502. $out .= ' /Resources 2 0 R';
  8503. $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8504. foreach ($boxes as $box) {
  8505. $out .= ' /'.$box;
  8506. $out .= sprintf(' [%.2F %.2F %.2F %.2F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  8507. }
  8508. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8509. $out .= ' /BoxColorInfo <<';
  8510. foreach ($boxes as $box) {
  8511. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8512. $out .= ' /'.$box.' <<';
  8513. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8514. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8515. $out .= ' /C [';
  8516. $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  8517. $out .= ' ]';
  8518. }
  8519. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8520. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8521. }
  8522. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8523. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8524. }
  8525. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8526. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8527. $out .= ' /D [';
  8528. foreach ($dashes as $dash) {
  8529. $out .= sprintf(' %.3F', ($dash * $this->k));
  8530. }
  8531. $out .= ' ]';
  8532. }
  8533. $out .= ' >>';
  8534. }
  8535. }
  8536. $out .= ' >>';
  8537. }
  8538. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8539. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8540. if (!$this->pdfa_mode) {
  8541. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8542. }
  8543. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8544. // page transitions
  8545. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8546. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8547. }
  8548. $out .= ' /Trans <<';
  8549. $out .= ' /Type /Trans';
  8550. if (isset($this->pagedim[$n]['trans']['S'])) {
  8551. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8552. }
  8553. if (isset($this->pagedim[$n]['trans']['D'])) {
  8554. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8555. }
  8556. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8557. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8558. }
  8559. if (isset($this->pagedim[$n]['trans']['M'])) {
  8560. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8561. }
  8562. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8563. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8564. }
  8565. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8566. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8567. }
  8568. if (isset($this->pagedim[$n]['trans']['B'])) {
  8569. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8570. }
  8571. $out .= ' >>';
  8572. }
  8573. $out .= $this->_getannotsrefs($n);
  8574. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8575. $out .= ' >>';
  8576. $out .= "\n".'endobj';
  8577. $this->_out($out);
  8578. //Page content
  8579. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8580. $this->_newobj();
  8581. $p = $this->_getrawstream($p);
  8582. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8583. if ($this->diskcache) {
  8584. // remove temporary files
  8585. unlink($this->pages[$n]);
  8586. }
  8587. }
  8588. //Pages root
  8589. $out = $this->_getobj(1)."\n";
  8590. $out .= '<< /Type /Pages /Kids [';
  8591. foreach($this->page_obj_id as $page_obj) {
  8592. $out .= ' '.$page_obj.' 0 R';
  8593. }
  8594. $out .= ' ] /Count '.$num_pages.' >>';
  8595. $out .= "\n".'endobj';
  8596. $this->_out($out);
  8597. }
  8598. /**
  8599. * Output references to page annotations
  8600. * @param $n (int) page number
  8601. * @protected
  8602. * @author Nicola Asuni
  8603. * @since 4.7.000 (2008-08-29)
  8604. * @deprecated
  8605. */
  8606. protected function _putannotsrefs($n) {
  8607. $this->_out($this->_getannotsrefs($n));
  8608. }
  8609. /**
  8610. * Get references to page annotations.
  8611. * @param $n (int) page number
  8612. * @return string
  8613. * @protected
  8614. * @author Nicola Asuni
  8615. * @since 5.0.010 (2010-05-17)
  8616. */
  8617. protected function _getannotsrefs($n) {
  8618. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8619. return '';
  8620. }
  8621. $out = ' /Annots [';
  8622. if (isset($this->PageAnnots[$n])) {
  8623. foreach ($this->PageAnnots[$n] as $key => $val) {
  8624. if (!in_array($val['n'], $this->radio_groups)) {
  8625. $out .= ' '.$val['n'].' 0 R';
  8626. }
  8627. }
  8628. // add radiobutton groups
  8629. if (isset($this->radiobutton_groups[$n])) {
  8630. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8631. if (isset($data['n'])) {
  8632. $out .= ' '.$data['n'].' 0 R';
  8633. }
  8634. }
  8635. }
  8636. }
  8637. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8638. // set reference for signature object
  8639. $out .= ' '.$this->sig_obj_id.' 0 R';
  8640. }
  8641. if (!empty($this->empty_signature_appearance)) {
  8642. foreach ($this->empty_signature_appearance as $esa) {
  8643. if ($esa['page'] == $n) {
  8644. // set reference for empty signature objects
  8645. $out .= ' '.$esa['objid'].' 0 R';
  8646. }
  8647. }
  8648. }
  8649. $out .= ' ]';
  8650. return $out;
  8651. }
  8652. /**
  8653. * Output annotations objects for all pages.
  8654. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8655. * See section 12.5 of PDF 32000_2008 reference.
  8656. * @protected
  8657. * @author Nicola Asuni
  8658. * @since 4.0.018 (2008-08-06)
  8659. */
  8660. protected function _putannotsobjs() {
  8661. // reset object counter
  8662. for ($n=1; $n <= $this->numpages; ++$n) {
  8663. if (isset($this->PageAnnots[$n])) {
  8664. // set page annotations
  8665. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8666. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8667. // create annotation object for grouping radiobuttons
  8668. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8669. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8670. $annots = '<<';
  8671. $annots .= ' /Type /Annot';
  8672. $annots .= ' /Subtype /Widget';
  8673. $annots .= ' /Rect [0 0 0 0]';
  8674. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  8675. // read only
  8676. $annots .= ' /F 68';
  8677. $annots .= ' /Ff 49153';
  8678. } else {
  8679. $annots .= ' /F 4'; // default print for PDF/A
  8680. $annots .= ' /Ff 49152';
  8681. }
  8682. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8683. $annots .= ' /FT /Btn';
  8684. $annots .= ' /Kids [';
  8685. $defval = '';
  8686. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8687. if (isset($data['kid'])) {
  8688. $annots .= ' '.$data['kid'].' 0 R';
  8689. if ($data['def'] !== 'Off') {
  8690. $defval = $data['def'];
  8691. }
  8692. }
  8693. }
  8694. $annots .= ' ]';
  8695. if (!empty($defval)) {
  8696. $annots .= ' /V /'.$defval;
  8697. }
  8698. $annots .= ' >>';
  8699. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8700. $this->form_obj_id[] = $radio_button_obj_id;
  8701. // store object id to be used on Parent entry of Kids
  8702. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8703. }
  8704. $formfield = false;
  8705. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8706. $a = $pl['x'] * $this->k;
  8707. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8708. $c = $pl['w'] * $this->k;
  8709. $d = $pl['h'] * $this->k;
  8710. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  8711. // create new annotation object
  8712. $annots = '<</Type /Annot';
  8713. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8714. $annots .= ' /Rect ['.$rect.']';
  8715. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8716. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8717. $annots .= ' /FT /'.$pl['opt']['ft'];
  8718. $formfield = true;
  8719. }
  8720. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8721. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8722. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8723. $annots .= ' /M '.$this->_datestring($annot_obj_id);
  8724. if (isset($pl['opt']['f'])) {
  8725. $fval = 0;
  8726. if (is_array($pl['opt']['f'])) {
  8727. foreach ($pl['opt']['f'] as $f) {
  8728. switch (strtolower($f)) {
  8729. case 'invisible': {
  8730. $fval += 1 << 0;
  8731. break;
  8732. }
  8733. case 'hidden': {
  8734. $fval += 1 << 1;
  8735. break;
  8736. }
  8737. case 'print': {
  8738. $fval += 1 << 2;
  8739. break;
  8740. }
  8741. case 'nozoom': {
  8742. $fval += 1 << 3;
  8743. break;
  8744. }
  8745. case 'norotate': {
  8746. $fval += 1 << 4;
  8747. break;
  8748. }
  8749. case 'noview': {
  8750. $fval += 1 << 5;
  8751. break;
  8752. }
  8753. case 'readonly': {
  8754. $fval += 1 << 6;
  8755. break;
  8756. }
  8757. case 'locked': {
  8758. $fval += 1 << 8;
  8759. break;
  8760. }
  8761. case 'togglenoview': {
  8762. $fval += 1 << 9;
  8763. break;
  8764. }
  8765. case 'lockedcontents': {
  8766. $fval += 1 << 10;
  8767. break;
  8768. }
  8769. default: {
  8770. break;
  8771. }
  8772. }
  8773. }
  8774. } else {
  8775. $fval = intval($pl['opt']['f']);
  8776. }
  8777. } else {
  8778. $fval = 4;
  8779. }
  8780. if ($this->pdfa_mode) {
  8781. // force print flag for PDF/A mode
  8782. $fval |= 4;
  8783. }
  8784. $annots .= ' /F '.intval($fval);
  8785. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8786. $annots .= ' /AS /'.$pl['opt']['as'];
  8787. }
  8788. if (isset($pl['opt']['ap'])) {
  8789. // appearance stream
  8790. $annots .= ' /AP <<';
  8791. if (is_array($pl['opt']['ap'])) {
  8792. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8793. // $apmode can be: n = normal; r = rollover; d = down;
  8794. $annots .= ' /'.strtoupper($apmode);
  8795. if (is_array($apdef)) {
  8796. $annots .= ' <<';
  8797. foreach ($apdef as $apstate => $stream) {
  8798. // reference to XObject that define the appearance for this mode-state
  8799. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8800. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8801. }
  8802. $annots .= ' >>';
  8803. } else {
  8804. // reference to XObject that define the appearance for this mode
  8805. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8806. $annots .= ' '.$apsobjid.' 0 R';
  8807. }
  8808. }
  8809. } else {
  8810. $annots .= $pl['opt']['ap'];
  8811. }
  8812. $annots .= ' >>';
  8813. }
  8814. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8815. $annots .= ' /BS <<';
  8816. $annots .= ' /Type /Border';
  8817. if (isset($pl['opt']['bs']['w'])) {
  8818. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8819. }
  8820. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8821. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8822. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8823. }
  8824. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8825. $annots .= ' /D [';
  8826. foreach ($pl['opt']['bs']['d'] as $cord) {
  8827. $annots .= ' '.intval($cord);
  8828. }
  8829. $annots .= ']';
  8830. }
  8831. $annots .= ' >>';
  8832. } else {
  8833. $annots .= ' /Border [';
  8834. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8835. $annots .= intval($pl['opt']['border'][0]).' ';
  8836. $annots .= intval($pl['opt']['border'][1]).' ';
  8837. $annots .= intval($pl['opt']['border'][2]);
  8838. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  8839. $annots .= ' [';
  8840. foreach ($pl['opt']['border'][3] as $dash) {
  8841. $annots .= intval($dash).' ';
  8842. }
  8843. $annots .= ']';
  8844. }
  8845. } else {
  8846. $annots .= '0 0 0';
  8847. }
  8848. $annots .= ']';
  8849. }
  8850. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  8851. $annots .= ' /BE <<';
  8852. $bstyles = array('S', 'C');
  8853. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  8854. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8855. } else {
  8856. $annots .= ' /S /S';
  8857. }
  8858. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8859. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  8860. }
  8861. $annots .= '>>';
  8862. }
  8863. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8864. $annots .= ' /C '.$this->getColorStringFromArray($pl['opt']['c']);
  8865. }
  8866. //$annots .= ' /StructParent ';
  8867. //$annots .= ' /OC ';
  8868. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8869. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8870. // this is a markup type
  8871. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8872. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8873. }
  8874. //$annots .= ' /Popup ';
  8875. if (isset($pl['opt']['ca'])) {
  8876. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  8877. }
  8878. if (isset($pl['opt']['rc'])) {
  8879. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8880. }
  8881. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
  8882. //$annots .= ' /IRT ';
  8883. if (isset($pl['opt']['subj'])) {
  8884. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8885. }
  8886. //$annots .= ' /RT ';
  8887. //$annots .= ' /IT ';
  8888. //$annots .= ' /ExData ';
  8889. }
  8890. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8891. // Annotation types
  8892. switch (strtolower($pl['opt']['subtype'])) {
  8893. case 'text': {
  8894. if (isset($pl['opt']['open'])) {
  8895. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8896. }
  8897. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8898. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8899. $annots .= ' /Name /'.$pl['opt']['name'];
  8900. } else {
  8901. $annots .= ' /Name /Note';
  8902. }
  8903. $statemodels = array('Marked', 'Review');
  8904. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8905. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8906. } else {
  8907. $pl['opt']['statemodel'] = 'Marked';
  8908. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8909. }
  8910. if ($pl['opt']['statemodel'] == 'Marked') {
  8911. $states = array('Accepted', 'Unmarked');
  8912. } else {
  8913. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8914. }
  8915. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  8916. $annots .= ' /State /'.$pl['opt']['state'];
  8917. } else {
  8918. if ($pl['opt']['statemodel'] == 'Marked') {
  8919. $annots .= ' /State /Unmarked';
  8920. } else {
  8921. $annots .= ' /State /None';
  8922. }
  8923. }
  8924. break;
  8925. }
  8926. case 'link': {
  8927. if (is_string($pl['txt'])) {
  8928. // external URI link
  8929. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8930. } else {
  8931. // internal link
  8932. $l = $this->links[$pl['txt']];
  8933. if (isset($this->page_obj_id[($l[0])])) {
  8934. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  8935. }
  8936. }
  8937. $hmodes = array('N', 'I', 'O', 'P');
  8938. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8939. $annots .= ' /H /'.$pl['opt']['h'];
  8940. } else {
  8941. $annots .= ' /H /I';
  8942. }
  8943. //$annots .= ' /PA ';
  8944. //$annots .= ' /Quadpoints ';
  8945. break;
  8946. }
  8947. case 'freetext': {
  8948. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8949. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8950. }
  8951. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8952. $annots .= ' /Q '.intval($pl['opt']['q']);
  8953. }
  8954. if (isset($pl['opt']['rc'])) {
  8955. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8956. }
  8957. if (isset($pl['opt']['ds'])) {
  8958. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8959. }
  8960. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8961. $annots .= ' /CL [';
  8962. foreach ($pl['opt']['cl'] as $cl) {
  8963. $annots .= sprintf('%.4F ', $cl * $this->k);
  8964. }
  8965. $annots .= ']';
  8966. }
  8967. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8968. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8969. $annots .= ' /IT /'.$pl['opt']['it'];
  8970. }
  8971. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8972. $l = $pl['opt']['rd'][0] * $this->k;
  8973. $r = $pl['opt']['rd'][1] * $this->k;
  8974. $t = $pl['opt']['rd'][2] * $this->k;
  8975. $b = $pl['opt']['rd'][3] * $this->k;
  8976. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  8977. }
  8978. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8979. $annots .= ' /LE /'.$pl['opt']['le'];
  8980. }
  8981. break;
  8982. }
  8983. case 'line': {
  8984. break;
  8985. }
  8986. case 'square': {
  8987. break;
  8988. }
  8989. case 'circle': {
  8990. break;
  8991. }
  8992. case 'polygon': {
  8993. break;
  8994. }
  8995. case 'polyline': {
  8996. break;
  8997. }
  8998. case 'highlight': {
  8999. break;
  9000. }
  9001. case 'underline': {
  9002. break;
  9003. }
  9004. case 'squiggly': {
  9005. break;
  9006. }
  9007. case 'strikeout': {
  9008. break;
  9009. }
  9010. case 'stamp': {
  9011. break;
  9012. }
  9013. case 'caret': {
  9014. break;
  9015. }
  9016. case 'ink': {
  9017. break;
  9018. }
  9019. case 'popup': {
  9020. break;
  9021. }
  9022. case 'fileattachment': {
  9023. if ($this->pdfa_mode) {
  9024. // embedded files are not allowed in PDF/A mode
  9025. break;
  9026. }
  9027. if (!isset($pl['opt']['fs'])) {
  9028. break;
  9029. }
  9030. $filename = basename($pl['opt']['fs']);
  9031. if (isset($this->embeddedfiles[$filename]['n'])) {
  9032. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9033. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9034. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9035. $annots .= ' /Name /'.$pl['opt']['name'];
  9036. } else {
  9037. $annots .= ' /Name /PushPin';
  9038. }
  9039. }
  9040. break;
  9041. }
  9042. case 'sound': {
  9043. if (!isset($pl['opt']['fs'])) {
  9044. break;
  9045. }
  9046. $filename = basename($pl['opt']['fs']);
  9047. if (isset($this->embeddedfiles[$filename]['n'])) {
  9048. // ... TO BE COMPLETED ...
  9049. // /R /C /B /E /CO /CP
  9050. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9051. $iconsapp = array('Speaker', 'Mic');
  9052. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9053. $annots .= ' /Name /'.$pl['opt']['name'];
  9054. } else {
  9055. $annots .= ' /Name /Speaker';
  9056. }
  9057. }
  9058. break;
  9059. }
  9060. case 'movie': {
  9061. break;
  9062. }
  9063. case 'widget': {
  9064. $hmode = array('N', 'I', 'O', 'P', 'T');
  9065. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9066. $annots .= ' /H /'.$pl['opt']['h'];
  9067. }
  9068. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9069. $annots .= ' /MK <<';
  9070. if (isset($pl['opt']['mk']['r'])) {
  9071. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9072. }
  9073. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9074. $annots .= ' /BC '.$this->getColorStringFromArray($pl['opt']['mk']['bc']);
  9075. }
  9076. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9077. $annots .= ' /BG '.$this->getColorStringFromArray($pl['opt']['mk']['bg']);
  9078. }
  9079. if (isset($pl['opt']['mk']['ca'])) {
  9080. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9081. }
  9082. if (isset($pl['opt']['mk']['rc'])) {
  9083. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9084. }
  9085. if (isset($pl['opt']['mk']['ac'])) {
  9086. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9087. }
  9088. if (isset($pl['opt']['mk']['i'])) {
  9089. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9090. if ($info !== false) {
  9091. $annots .= ' /I '.$info['n'].' 0 R';
  9092. }
  9093. }
  9094. if (isset($pl['opt']['mk']['ri'])) {
  9095. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9096. if ($info !== false) {
  9097. $annots .= ' /RI '.$info['n'].' 0 R';
  9098. }
  9099. }
  9100. if (isset($pl['opt']['mk']['ix'])) {
  9101. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9102. if ($info !== false) {
  9103. $annots .= ' /IX '.$info['n'].' 0 R';
  9104. }
  9105. }
  9106. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9107. $annots .= ' /IF <<';
  9108. $if_sw = array('A', 'B', 'S', 'N');
  9109. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9110. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9111. }
  9112. $if_s = array('A', 'P');
  9113. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9114. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9115. }
  9116. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9117. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9118. }
  9119. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9120. $annots .= ' /FB true';
  9121. }
  9122. $annots .= '>>';
  9123. }
  9124. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9125. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9126. }
  9127. $annots .= '>>';
  9128. } // end MK
  9129. // --- Entries for field dictionaries ---
  9130. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9131. // set parent
  9132. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9133. }
  9134. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9135. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9136. }
  9137. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9138. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9139. }
  9140. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9141. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9142. }
  9143. if (isset($pl['opt']['ff'])) {
  9144. if (is_array($pl['opt']['ff'])) {
  9145. // array of bit settings
  9146. $flag = 0;
  9147. foreach($pl['opt']['ff'] as $val) {
  9148. $flag += 1 << ($val - 1);
  9149. }
  9150. } else {
  9151. $flag = intval($pl['opt']['ff']);
  9152. }
  9153. $annots .= ' /Ff '.$flag;
  9154. }
  9155. if (isset($pl['opt']['maxlen'])) {
  9156. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9157. }
  9158. if (isset($pl['opt']['v'])) {
  9159. $annots .= ' /V';
  9160. if (is_array($pl['opt']['v'])) {
  9161. foreach ($pl['opt']['v'] AS $optval) {
  9162. if (is_float($optval)) {
  9163. $optval = sprintf('%.2F', $optval);
  9164. }
  9165. $annots .= ' '.$optval;
  9166. }
  9167. } else {
  9168. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9169. }
  9170. }
  9171. if (isset($pl['opt']['dv'])) {
  9172. $annots .= ' /DV';
  9173. if (is_array($pl['opt']['dv'])) {
  9174. foreach ($pl['opt']['dv'] AS $optval) {
  9175. if (is_float($optval)) {
  9176. $optval = sprintf('%.2F', $optval);
  9177. }
  9178. $annots .= ' '.$optval;
  9179. }
  9180. } else {
  9181. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9182. }
  9183. }
  9184. if (isset($pl['opt']['rv'])) {
  9185. $annots .= ' /RV';
  9186. if (is_array($pl['opt']['rv'])) {
  9187. foreach ($pl['opt']['rv'] AS $optval) {
  9188. if (is_float($optval)) {
  9189. $optval = sprintf('%.2F', $optval);
  9190. }
  9191. $annots .= ' '.$optval;
  9192. }
  9193. } else {
  9194. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9195. }
  9196. }
  9197. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9198. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9199. }
  9200. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9201. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9202. }
  9203. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9204. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9205. }
  9206. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9207. $annots .= ' /Q '.intval($pl['opt']['q']);
  9208. }
  9209. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9210. $annots .= ' /Opt [';
  9211. foreach($pl['opt']['opt'] AS $copt) {
  9212. if (is_array($copt)) {
  9213. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9214. } else {
  9215. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9216. }
  9217. }
  9218. $annots .= ']';
  9219. }
  9220. if (isset($pl['opt']['ti'])) {
  9221. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9222. }
  9223. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9224. $annots .= ' /I [';
  9225. foreach($pl['opt']['i'] AS $copt) {
  9226. $annots .= intval($copt).' ';
  9227. }
  9228. $annots .= ']';
  9229. }
  9230. break;
  9231. }
  9232. case 'screen': {
  9233. break;
  9234. }
  9235. case 'printermark': {
  9236. break;
  9237. }
  9238. case 'trapnet': {
  9239. break;
  9240. }
  9241. case 'watermark': {
  9242. break;
  9243. }
  9244. case '3d': {
  9245. break;
  9246. }
  9247. default: {
  9248. break;
  9249. }
  9250. }
  9251. $annots .= '>>';
  9252. // create new annotation object
  9253. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9254. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9255. // store reference of form object
  9256. $this->form_obj_id[] = $annot_obj_id;
  9257. }
  9258. }
  9259. }
  9260. } // end for each page
  9261. }
  9262. /**
  9263. * Put appearance streams XObject used to define annotation's appearance states.
  9264. * @param $w (int) annotation width
  9265. * @param $h (int) annotation height
  9266. * @param $stream (string) appearance stream
  9267. * @return int object ID
  9268. * @protected
  9269. * @since 4.8.001 (2009-09-09)
  9270. */
  9271. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9272. $stream = trim($stream);
  9273. $out = $this->_getobj()."\n";
  9274. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9275. $out .= '<<';
  9276. $out .= ' /Type /XObject';
  9277. $out .= ' /Subtype /Form';
  9278. $out .= ' /FormType 1';
  9279. if ($this->compress) {
  9280. $stream = gzcompress($stream);
  9281. $out .= ' /Filter /FlateDecode';
  9282. }
  9283. $rect = sprintf('%.2F %.2F', $w, $h);
  9284. $out .= ' /BBox [0 0 '.$rect.']';
  9285. $out .= ' /Matrix [1 0 0 1 0 0]';
  9286. $out .= ' /Resources 2 0 R';
  9287. $stream = $this->_getrawstream($stream);
  9288. $out .= ' /Length '.strlen($stream);
  9289. $out .= ' >>';
  9290. $out .= ' stream'."\n".$stream."\n".'endstream';
  9291. $out .= "\n".'endobj';
  9292. $this->_out($out);
  9293. return $this->n;
  9294. }
  9295. /**
  9296. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9297. * @param $str (string) string from where to extract value
  9298. * @param $offset (int) point from where to read the data
  9299. * @return int 32 bit value
  9300. * @author Nicola Asuni
  9301. * @protected
  9302. * @since 5.2.000 (2010-06-02)
  9303. */
  9304. protected function _getULONG($str, $offset) {
  9305. $v = unpack('Ni', substr($str, $offset, 4));
  9306. return $v['i'];
  9307. }
  9308. /**
  9309. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9310. * @param $str (string) string from where to extract value
  9311. * @param $offset (int) point from where to read the data
  9312. * @return int 16 bit value
  9313. * @author Nicola Asuni
  9314. * @protected
  9315. * @since 5.2.000 (2010-06-02)
  9316. */
  9317. protected function _getUSHORT($str, $offset) {
  9318. $v = unpack('ni', substr($str, $offset, 2));
  9319. return $v['i'];
  9320. }
  9321. /**
  9322. * Get SHORT from string (Big Endian 16-bit signed integer).
  9323. * @param $str (string) string from where to extract value
  9324. * @param $offset (int) point from where to read the data
  9325. * @return int 16 bit value
  9326. * @author Nicola Asuni
  9327. * @protected
  9328. * @since 5.2.000 (2010-06-02)
  9329. */
  9330. protected function _getSHORT($str, $offset) {
  9331. $v = unpack('si', substr($str, $offset, 2));
  9332. return $v['i'];
  9333. }
  9334. /**
  9335. * Get FWORD from string (Big Endian 16-bit signed integer).
  9336. * @param $str (string) string from where to extract value
  9337. * @param $offset (int) point from where to read the data
  9338. * @return int 16 bit value
  9339. * @author Nicola Asuni
  9340. * @protected
  9341. * @since 5.9.123 (2011-09-30)
  9342. */
  9343. protected function _getFWORD($str, $offset) {
  9344. $v = $this->_getUSHORT($str, $offset);
  9345. if ($v > 0x7fff) {
  9346. $v -= 0x10000;
  9347. }
  9348. return $v;
  9349. }
  9350. /**
  9351. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  9352. * @param $str (string) string from where to extract value
  9353. * @param $offset (int) point from where to read the data
  9354. * @return int 16 bit value
  9355. * @author Nicola Asuni
  9356. * @protected
  9357. * @since 5.9.123 (2011-09-30)
  9358. */
  9359. protected function _getUFWORD($str, $offset) {
  9360. $v = $this->_getUSHORT($str, $offset);
  9361. return $v;
  9362. }
  9363. /**
  9364. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9365. * @param $str (string) string from where to extract value
  9366. * @param $offset (int) point from where to read the data
  9367. * @return int 16 bit value
  9368. * @author Nicola Asuni
  9369. * @protected
  9370. * @since 5.9.123 (2011-09-30)
  9371. */
  9372. protected function _getFIXED($str, $offset) {
  9373. // mantissa
  9374. $m = $this->_getFWORD($str, $offset);
  9375. // fraction
  9376. $f = $this->_getUSHORT($str, ($offset + 2));
  9377. $v = floatval(''.$m.'.'.$f.'');
  9378. return $v;
  9379. }
  9380. /**
  9381. * Get BYTE from string (8-bit unsigned integer).
  9382. * @param $str (string) String from where to extract value.
  9383. * @param $offset (int) Point from where to read the data.
  9384. * @return int 8 bit value
  9385. * @author Nicola Asuni
  9386. * @protected
  9387. * @since 5.2.000 (2010-06-02)
  9388. */
  9389. protected function _getBYTE($str, $offset) {
  9390. $v = unpack('Ci', substr($str, $offset, 1));
  9391. return $v['i'];
  9392. }
  9393. /**
  9394. * Update the CIDToGIDMap string with a new value.
  9395. * @param $map (string) CIDToGIDMap.
  9396. * @param $cid (int) CID value.
  9397. * @param $gid (int) GID value.
  9398. * @return (string) CIDToGIDMap.
  9399. * @author Nicola Asuni
  9400. * @protected
  9401. * @since 5.9.123 (2011-09-29)
  9402. */
  9403. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9404. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9405. if ($gid > 0xFFFF) {
  9406. $gid -= 0x10000;
  9407. }
  9408. $map[($cid * 2)] = chr($gid >> 8);
  9409. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9410. }
  9411. return $map;
  9412. }
  9413. /**
  9414. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9415. * @param $fontfile (string) TrueType or Type1 font file (full path).
  9416. * @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.
  9417. * @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.
  9418. * @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.
  9419. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9420. * @return (string) TCPDF font name.
  9421. * @author Nicola Asuni
  9422. * @public
  9423. * @since 5.9.123 (2010-09-30)
  9424. */
  9425. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='') {
  9426. if (!file_exists($fontfile)) {
  9427. $this->Error('Could not find file: '.$fontfile.'');
  9428. }
  9429. // font metrics
  9430. $fmetric = array();
  9431. // build new font name for TCPDF compatibility
  9432. $font_path_parts = pathinfo($fontfile);
  9433. if (!isset($font_path_parts['filename'])) {
  9434. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9435. }
  9436. $font_name = strtolower($font_path_parts['filename']);
  9437. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9438. $search = array('bold', 'oblique', 'italic', 'regular');
  9439. $replace = array('b', 'i', 'i', '');
  9440. $font_name = str_replace($search, $replace, $font_name);
  9441. if (empty($font_name)) {
  9442. // set generic name
  9443. $font_name = 'tcpdffont';
  9444. }
  9445. // set output path
  9446. if (empty($outpath)) {
  9447. $outpath = $this->_getfontpath();
  9448. }
  9449. // check if this font already exist
  9450. if (file_exists($outpath.$font_name.'.php')) {
  9451. // this font already exist (delete it from fonts folder to rebuild it)
  9452. return $font_name;
  9453. }
  9454. $fmetric['file'] = $font_name.'.z';
  9455. $fmetric['ctg'] = $font_name.'.ctg.z';
  9456. // get font data
  9457. $font = file_get_contents($fontfile);
  9458. $fmetric['originalsize'] = strlen($font);
  9459. // autodetect font type
  9460. if (empty($fonttype)) {
  9461. if ($this->_getULONG($font, 0) == 0x10000) {
  9462. // True Type (Unicode or not)
  9463. $fonttype = 'TrueTypeUnicode';
  9464. } elseif (substr($font, 0, 4) == 'OTTO') {
  9465. // Open Type (Unicode or not)
  9466. $this->Error('Unsupported font format: OpenType with CFF data.');
  9467. } else {
  9468. // Type 1
  9469. $fonttype = 'Type1';
  9470. }
  9471. }
  9472. // set font type
  9473. switch ($fonttype) {
  9474. case 'CID0CT':
  9475. case 'CID0CS':
  9476. case 'CID0KR':
  9477. case 'CID0JP': {
  9478. $fmetric['type'] = 'cidfont0';
  9479. break;
  9480. }
  9481. case 'Type1': {
  9482. $fmetric['type'] = 'Type1';
  9483. if (empty($enc) AND (($flags & 4) == 0)) {
  9484. $enc = 'cp1252';
  9485. }
  9486. break;
  9487. }
  9488. case 'TrueType': {
  9489. $fmetric['type'] = 'TrueType';
  9490. break;
  9491. }
  9492. case 'TrueTypeUnicode':
  9493. default: {
  9494. $fmetric['type'] = 'TrueTypeUnicode';
  9495. break;
  9496. }
  9497. }
  9498. // set encoding maps (if any)
  9499. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9500. $fmetric['diff'] = '';
  9501. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9502. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9503. // build differences from reference encoding
  9504. $enc_ref = $this->encmaps->encmap['cp1252'];
  9505. $enc_target = $this->encmaps->encmap[$enc];
  9506. $last = 0;
  9507. for ($i = 32; $i <= 255; ++$i) {
  9508. if ($enc_target != $enc_ref[$i]) {
  9509. if ($i != ($last + 1)) {
  9510. $fmetric['diff'] .= $i.' ';
  9511. }
  9512. $last = $i;
  9513. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9514. }
  9515. }
  9516. }
  9517. }
  9518. // parse the font by type
  9519. if ($fmetric['type'] == 'Type1') {
  9520. // ---------- TYPE 1 ----------
  9521. // read first segment
  9522. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9523. if ($a['marker'] != 128) {
  9524. $this->Error('Font file is not a valid binary Type1');
  9525. }
  9526. $fmetric['size1'] = $a['size'];
  9527. $data = substr($font, 6, $fmetric['size1']);
  9528. // read second segment
  9529. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9530. if ($a['marker'] != 128) {
  9531. $this->Error('Font file is not a valid binary Type1');
  9532. }
  9533. $fmetric['size2'] = $a['size'];
  9534. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9535. $data .= $encrypted;
  9536. // store compressed font
  9537. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9538. fwrite($fp, gzcompress($data));
  9539. fclose($fp);
  9540. // get font info
  9541. $fmetric['Flags'] = $flags;
  9542. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9543. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9544. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9545. $fmetric['bbox'] = trim($matches[1]);
  9546. $bv = explode(' ', $fmetric['bbox']);
  9547. $fmetric['Ascent'] = intval($bv[3]);
  9548. $fmetric['Descent'] = intval($bv[1]);
  9549. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9550. $fmetric['italicAngle'] = intval($matches[1]);
  9551. if ($fmetric['italicAngle'] != 0) {
  9552. $fmetric['Flags'] |= 64;
  9553. }
  9554. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9555. $fmetric['underlinePosition'] = intval($matches[1]);
  9556. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9557. $fmetric['underlineThickness'] = intval($matches[1]);
  9558. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9559. if ($matches[1] == 'true') {
  9560. $fmetric['Flags'] |= 1;
  9561. }
  9562. // get internal map
  9563. $imap = array();
  9564. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9565. foreach ($fmap as $v) {
  9566. $imap[$v[2]] = $v[1];
  9567. }
  9568. }
  9569. // decrypt eexec encrypted part
  9570. $r = 55665; // eexec encryption constant
  9571. $c1 = 52845;
  9572. $c2 = 22719;
  9573. $elen = strlen($encrypted);
  9574. $eplain = '';
  9575. for ($i = 0; $i < $elen; ++$i) {
  9576. $chr = ord($encrypted[$i]);
  9577. $eplain .= chr($chr ^ ($r >> 8));
  9578. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9579. }
  9580. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9581. if ($matches[1] == 'true') {
  9582. $fmetric['Flags'] |= 0x40000;
  9583. }
  9584. }
  9585. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9586. $fmetric['StemV'] = intval($matches[1]);
  9587. } else {
  9588. $fmetric['StemV'] = 70;
  9589. }
  9590. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9591. $fmetric['StemH'] = intval($matches[1]);
  9592. } else {
  9593. $fmetric['StemH'] = 30;
  9594. }
  9595. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9596. $bv = explode(' ', $matches[1]);
  9597. if (count($bv) >= 6) {
  9598. $v1 = intval($bv[2]);
  9599. $v2 = intval($bv[4]);
  9600. if ($v1 <= $v2) {
  9601. $fmetric['XHeight'] = $v1;
  9602. $fmetric['CapHeight'] = $v2;
  9603. } else {
  9604. $fmetric['XHeight'] = $v2;
  9605. $fmetric['CapHeight'] = $v1;
  9606. }
  9607. } else {
  9608. $fmetric['XHeight'] = 450;
  9609. $fmetric['CapHeight'] = 700;
  9610. }
  9611. } else {
  9612. $fmetric['XHeight'] = 450;
  9613. $fmetric['CapHeight'] = 700;
  9614. }
  9615. // get the number of random bytes at the beginning of charstrings
  9616. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9617. $lenIV = intval($matches[1]);
  9618. } else {
  9619. $lenIV = 4;
  9620. }
  9621. $fmetric['Leading'] = 0;
  9622. // get charstring data
  9623. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9624. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9625. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9626. $enc_map = $this->encmaps->encmap[$enc];
  9627. } else {
  9628. $enc_map = false;
  9629. }
  9630. $fmetric['cw'] = '';
  9631. $fmetric['MaxWidth'] = 0;
  9632. $cwidths = array();
  9633. foreach ($matches as $k => $v) {
  9634. $cid = 0;
  9635. if (isset($imap[$v[1]])) {
  9636. $cid = $imap[$v[1]];
  9637. } elseif ($enc_map !== false) {
  9638. $cid = array_search($v[1], $enc_map);
  9639. if ($cid === false) {
  9640. $cid = 0;
  9641. } elseif ($cid > 1000) {
  9642. $cid -= 1000;
  9643. }
  9644. }
  9645. // decrypt charstring encrypted part
  9646. $r = 4330; // charstring encryption constant
  9647. $c1 = 52845;
  9648. $c2 = 22719;
  9649. $cd = $v[2];
  9650. $clen = strlen($cd);
  9651. $ccom = array();
  9652. for ($i = 0; $i < $clen; ++$i) {
  9653. $chr = ord($cd[$i]);
  9654. $ccom[] = ($chr ^ ($r >> 8));
  9655. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9656. }
  9657. // decode numbers
  9658. $cdec = array();
  9659. $ck = 0;
  9660. $i = $lenIV;
  9661. while ($i < $clen) {
  9662. if ($ccom[$i] < 32) {
  9663. $cdec[$ck] = $ccom[$i];
  9664. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  9665. // hsbw command: update width
  9666. $cwidths[$cid] = $cdec[($ck - 1)];
  9667. }
  9668. ++$i;
  9669. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  9670. $cdec[$ck] = ($ccom[$i] - 139);
  9671. ++$i;
  9672. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  9673. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  9674. $i += 2;
  9675. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  9676. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  9677. $i += 2;
  9678. } elseif ($ccom[$i] == 255) {
  9679. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  9680. $vsval = unpack('li', $sval);
  9681. $cdec[$ck] = $vsval['i'];
  9682. $i += 5;
  9683. }
  9684. ++$ck;
  9685. }
  9686. } // end for each matches
  9687. $fmetric['MissingWidth'] = $cwidths[0];
  9688. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  9689. $fmetric['AvgWidth'] = 0;
  9690. // set chars widths
  9691. for ($cid = 0; $cid <= 255; ++$cid) {
  9692. if (isset($cwidths[$cid])) {
  9693. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  9694. $fmetric['MaxWidth'] = $cwidths[$cid];
  9695. }
  9696. $fmetric['AvgWidth'] += $cwidths[$cid];
  9697. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  9698. } else {
  9699. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  9700. }
  9701. }
  9702. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  9703. } else {
  9704. // ---------- TRUE TYPE ----------
  9705. if ($fmetric['type'] != 'cidfont0') {
  9706. // store compressed font
  9707. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9708. fwrite($fp, gzcompress($font));
  9709. fclose($fp);
  9710. }
  9711. $offset = 0; // offset position of the font data
  9712. if ($this->_getULONG($font, $offset) != 0x10000) {
  9713. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9714. return $font;
  9715. }
  9716. $offset += 4;
  9717. // get number of tables
  9718. $numTables = $this->_getUSHORT($font, $offset);
  9719. $offset += 2;
  9720. // skip searchRange, entrySelector and rangeShift
  9721. $offset += 6;
  9722. // tables array
  9723. $table = array();
  9724. // ---------- get tables ----------
  9725. for ($i = 0; $i < $numTables; ++$i) {
  9726. // get table info
  9727. $tag = substr($font, $offset, 4);
  9728. $offset += 4;
  9729. $table[$tag] = array();
  9730. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9731. $offset += 4;
  9732. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9733. $offset += 4;
  9734. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9735. $offset += 4;
  9736. }
  9737. // check magicNumber
  9738. $offset = $table['head']['offset'] + 12;
  9739. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9740. // magicNumber must be 0x5F0F3CF5
  9741. return $font;
  9742. }
  9743. $offset += 4;
  9744. $offset += 2; // skip flags
  9745. // get FUnits
  9746. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  9747. $offset += 2;
  9748. // units ratio constant
  9749. $urk = (1000 / $fmetric['unitsPerEm']);
  9750. $offset += 16; // skip created, modified
  9751. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  9752. $offset += 2;
  9753. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  9754. $offset += 2;
  9755. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  9756. $offset += 2;
  9757. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  9758. $offset += 2;
  9759. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  9760. $macStyle = $this->_getUSHORT($font, $offset);
  9761. $offset += 2;
  9762. // PDF font flags
  9763. $fmetric['Flags'] = $flags;
  9764. if (($macStyle & 2) == 2) {
  9765. // italic flag
  9766. $fmetric['Flags'] |= 64;
  9767. }
  9768. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9769. $offset = $table['head']['offset'] + 50;
  9770. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9771. $offset += 2;
  9772. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9773. $indexToLoc = array();
  9774. $offset = $table['loca']['offset'];
  9775. if ($short_offset) {
  9776. // short version
  9777. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9778. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9779. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9780. $offset += 2;
  9781. }
  9782. } else {
  9783. // long version
  9784. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9785. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9786. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9787. $offset += 4;
  9788. }
  9789. }
  9790. // get glyphs indexes of chars from cmap table
  9791. $offset = $table['cmap']['offset'] + 2;
  9792. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9793. $offset += 2;
  9794. $encodingTables = array();
  9795. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9796. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9797. $offset += 2;
  9798. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9799. $offset += 2;
  9800. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9801. $offset += 4;
  9802. }
  9803. // ---------- get os/2 metrics ----------
  9804. $offset = $table['OS/2']['offset'];
  9805. $offset += 2; // skip version
  9806. // xAvgCharWidth
  9807. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  9808. $offset += 2;
  9809. // usWeightClass
  9810. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  9811. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  9812. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  9813. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  9814. $offset += 2;
  9815. $offset += 2; // usWidthClass
  9816. $fsType = $this->_getSHORT($font, $offset);
  9817. $offset += 2;
  9818. if ($fsType == 2) {
  9819. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  9820. }
  9821. // ---------- get font name ----------
  9822. $fmetric['name'] = '';
  9823. $offset = $table['name']['offset'];
  9824. $offset += 2; // skip Format selector (=0).
  9825. // Number of NameRecords that follow n.
  9826. $numNameRecords = $this->_getUSHORT($font, $offset);
  9827. $offset += 2;
  9828. // Offset to start of string storage (from start of table).
  9829. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  9830. $offset += 2;
  9831. for ($i = 0; $i < $numNameRecords; ++$i) {
  9832. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  9833. // Name ID.
  9834. $nameID = $this->_getUSHORT($font, $offset);
  9835. $offset += 2;
  9836. if ($nameID == 6) {
  9837. // String length (in bytes).
  9838. $stringLength = $this->_getUSHORT($font, $offset);
  9839. $offset += 2;
  9840. // String offset from start of storage area (in bytes).
  9841. $stringOffset = $this->_getUSHORT($font, $offset);
  9842. $offset += 2;
  9843. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  9844. $fmetric['name'] = substr($font, $offset, $stringLength);
  9845. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  9846. break;
  9847. } else {
  9848. $offset += 4; // skip String length, String offset
  9849. }
  9850. }
  9851. if (empty($fmetric['name'])) {
  9852. $fmetric['name'] = $font_name;
  9853. }
  9854. // ---------- get post data ----------
  9855. $offset = $table['post']['offset'];
  9856. $offset += 4; // skip Format Type
  9857. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  9858. $offset += 4;
  9859. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  9860. $offset += 2;
  9861. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  9862. $offset += 2;
  9863. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  9864. $offset += 2;
  9865. if ($isFixedPitch) {
  9866. $fmetric['Flags'] |= 1;
  9867. }
  9868. // ---------- get hhea data ----------
  9869. $offset = $table['hhea']['offset'];
  9870. $offset += 4; // skip Table version number
  9871. // Ascender
  9872. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  9873. $offset += 2;
  9874. // Descender
  9875. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  9876. $offset += 2;
  9877. // LineGap
  9878. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  9879. $offset += 2;
  9880. // advanceWidthMax
  9881. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  9882. $offset += 2;
  9883. $offset += 22; // skip some values
  9884. // get the number of hMetric entries in hmtx table
  9885. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  9886. // ---------- get maxp data ----------
  9887. $offset = $table['maxp']['offset'];
  9888. $offset += 4; // skip Table version number
  9889. // get the the number of glyphs in the font.
  9890. $numGlyphs = $this->_getUSHORT($font, $offset);
  9891. // ---------- get CIDToGIDMap ----------
  9892. $ctg = array();
  9893. foreach ($encodingTables as $enctable) {
  9894. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  9895. $modesymbol = true;
  9896. } else {
  9897. $modesymbol = false;
  9898. }
  9899. $offset = $table['cmap']['offset'] + $enctable['offset'];
  9900. $format = $this->_getUSHORT($font, $offset);
  9901. $offset += 2;
  9902. switch ($format) {
  9903. case 0: { // Format 0: Byte encoding table
  9904. $offset += 4; // skip length and version/language
  9905. for ($c = 0; $c < 256; ++$c) {
  9906. $g = $this->_getBYTE($font, $offset);
  9907. $ctg[$c] = $g;
  9908. ++$offset;
  9909. }
  9910. break;
  9911. }
  9912. case 2: { // Format 2: High-byte mapping through table
  9913. $offset += 4; // skip length and version/language
  9914. $numSubHeaders = 0;
  9915. for ($i = 0; $i < 256; ++$i) {
  9916. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  9917. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  9918. $offset += 2;
  9919. if ($numSubHeaders < $subHeaderKeys[$i]) {
  9920. $numSubHeaders = $subHeaderKeys[$i];
  9921. }
  9922. }
  9923. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  9924. ++$numSubHeaders;
  9925. // read subHeader structures
  9926. $subHeaders = array();
  9927. $numGlyphIndexArray = 0;
  9928. for ($k = 0; $k < $numSubHeaders; ++$k) {
  9929. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  9930. $offset += 2;
  9931. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  9932. $offset += 2;
  9933. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  9934. $offset += 2;
  9935. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  9936. $offset += 2;
  9937. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  9938. $subHeaders[$k]['idRangeOffset'] /= 2;
  9939. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  9940. }
  9941. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  9942. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  9943. $offset += 2;
  9944. }
  9945. for ($i = 0; $i < 256; ++$i) {
  9946. $k = $subHeaderKeys[$i];
  9947. if ($k == 0) {
  9948. // one byte code
  9949. $c = $i;
  9950. $g = $glyphIndexArray[0];
  9951. $ctg[$c] = $g;
  9952. } else {
  9953. // two bytes code
  9954. $start_byte = $subHeaders[$k]['firstCode'];
  9955. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  9956. for ($j = $start_byte; $j < $end_byte; ++$j) {
  9957. // combine high and low bytes
  9958. $c = (($i << 8) + $j);
  9959. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  9960. $g = $glyphIndexArray[$idRangeOffset];
  9961. $g += ($idDelta[$k] - 65536);
  9962. if ($g < 0) {
  9963. $g = 0;
  9964. }
  9965. $ctg[$c] = $g;
  9966. }
  9967. }
  9968. }
  9969. break;
  9970. }
  9971. case 4: { // Format 4: Segment mapping to delta values
  9972. $length = $this->_getUSHORT($font, $offset);
  9973. $offset += 2;
  9974. $offset += 2; // skip version/language
  9975. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  9976. $offset += 2;
  9977. $offset += 6; // skip searchRange, entrySelector, rangeShift
  9978. $endCount = array(); // array of end character codes for each segment
  9979. for ($k = 0; $k < $segCount; ++$k) {
  9980. $endCount[$k] = $this->_getUSHORT($font, $offset);
  9981. $offset += 2;
  9982. }
  9983. $offset += 2; // skip reservedPad
  9984. $startCount = array(); // array of start character codes for each segment
  9985. for ($k = 0; $k < $segCount; ++$k) {
  9986. $startCount[$k] = $this->_getUSHORT($font, $offset);
  9987. $offset += 2;
  9988. }
  9989. $idDelta = array(); // delta for all character codes in segment
  9990. for ($k = 0; $k < $segCount; ++$k) {
  9991. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  9992. $offset += 2;
  9993. }
  9994. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  9995. for ($k = 0; $k < $segCount; ++$k) {
  9996. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  9997. $offset += 2;
  9998. }
  9999. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10000. $glyphIdArray = array(); // glyph index array
  10001. for ($k = 0; $k < $gidlen; ++$k) {
  10002. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10003. $offset += 2;
  10004. }
  10005. for ($k = 0; $k < $segCount; ++$k) {
  10006. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10007. if ($idRangeOffset[$k] == 0) {
  10008. $g = $c;
  10009. } else {
  10010. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10011. $g = $glyphIdArray[$gid];
  10012. }
  10013. $g += ($idDelta[$k] - 65536);
  10014. if ($g < 0) {
  10015. $g = 0;
  10016. }
  10017. $ctg[$c] = $g;
  10018. }
  10019. }
  10020. break;
  10021. }
  10022. case 6: { // Format 6: Trimmed table mapping
  10023. $offset += 4; // skip length and version/language
  10024. $firstCode = $this->_getUSHORT($font, $offset);
  10025. $offset += 2;
  10026. $entryCount = $this->_getUSHORT($font, $offset);
  10027. $offset += 2;
  10028. for ($k = 0; $k < $entryCount; ++$k) {
  10029. $c = ($k + $firstCode);
  10030. $g = $this->_getUSHORT($font, $offset);
  10031. $ctg[$c] = $g;
  10032. $offset += 2;
  10033. }
  10034. break;
  10035. }
  10036. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10037. $offset += 10; // skip reserved, length and version/language
  10038. for ($k = 0; $k < 8192; ++$k) {
  10039. $is32[$k] = $this->_getBYTE($font, $offset);
  10040. ++$offset;
  10041. }
  10042. $nGroups = $this->_getULONG($font, $offset);
  10043. $offset += 4;
  10044. for ($i = 0; $i < $nGroups; ++$i) {
  10045. $startCharCode = $this->_getULONG($font, $offset);
  10046. $offset += 4;
  10047. $endCharCode = $this->_getULONG($font, $offset);
  10048. $offset += 4;
  10049. $startGlyphID = $this->_getULONG($font, $offset);
  10050. $offset += 4;
  10051. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10052. $is32idx = floor($c / 8);
  10053. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10054. $c = $k;
  10055. } else {
  10056. // 32 bit format
  10057. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10058. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10059. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10060. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10061. }
  10062. $ctg[$c] = $g;
  10063. ++$startGlyphID;
  10064. }
  10065. }
  10066. break;
  10067. }
  10068. case 10: { // Format 10: Trimmed array
  10069. $offset += 10; // skip reserved, length and version/language
  10070. $startCharCode = $this->_getULONG($font, $offset);
  10071. $offset += 4;
  10072. $numChars = $this->_getULONG($font, $offset);
  10073. $offset += 4;
  10074. for ($k = 0; $k < $numChars; ++$k) {
  10075. $c = ($k + $startCharCode);
  10076. $g = $this->_getUSHORT($font, $offset);
  10077. $ctg[$c] = $g;
  10078. $offset += 2;
  10079. }
  10080. break;
  10081. }
  10082. case 12: { // Format 12: Segmented coverage
  10083. $offset += 10; // skip length and version/language
  10084. $nGroups = $this->_getULONG($font, $offset);
  10085. $offset += 4;
  10086. for ($k = 0; $k < $nGroups; ++$k) {
  10087. $startCharCode = $this->_getULONG($font, $offset);
  10088. $offset += 4;
  10089. $endCharCode = $this->_getULONG($font, $offset);
  10090. $offset += 4;
  10091. $startGlyphCode = $this->_getULONG($font, $offset);
  10092. $offset += 4;
  10093. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10094. $ctg[$c] = $startGlyphCode;
  10095. ++$startGlyphCode;
  10096. }
  10097. }
  10098. break;
  10099. }
  10100. case 13: { // Format 13: Many-to-one range mappings
  10101. // to be implemented ...
  10102. break;
  10103. }
  10104. case 14: { // Format 14: Unicode Variation Sequences
  10105. // to be implemented ...
  10106. break;
  10107. }
  10108. }
  10109. }
  10110. if (!isset($ctg[0])) {
  10111. $ctg[0] = 0;
  10112. }
  10113. // get xHeight (height of x)
  10114. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10115. $yMin = $this->_getFWORD($font, $offset);
  10116. $offset += 4;
  10117. $yMax = $this->_getFWORD($font, $offset);
  10118. $offset += 2;
  10119. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10120. // get CapHeight (height of H)
  10121. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10122. $yMin = $this->_getFWORD($font, $offset);
  10123. $offset += 4;
  10124. $yMax = $this->_getFWORD($font, $offset);
  10125. $offset += 2;
  10126. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10127. // ceate widths array
  10128. $cw = array();
  10129. $offset = $table['hmtx']['offset'];
  10130. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10131. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10132. $offset += 4; // skip lsb
  10133. }
  10134. if ($numberOfHMetrics < $numGlyphs) {
  10135. // fill missing widths with the last value
  10136. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10137. }
  10138. $fmetric['MissingWidth'] = $cw[0];
  10139. $fmetric['cw'] = '';
  10140. for ($cid = 0; $cid <= 65535; ++$cid) {
  10141. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10142. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10143. }
  10144. }
  10145. } // end of true type
  10146. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10147. $fmetric['type'] == 'TrueType';
  10148. }
  10149. // ---------- create php font file ----------
  10150. $pfile = '<'.'?'.'php'."\n";
  10151. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10152. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10153. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10154. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10155. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10156. if ($fmetric['MissingWidth'] > 0) {
  10157. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10158. } else {
  10159. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10160. }
  10161. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10162. if ($fmetric['type'] == 'Type1') {
  10163. // Type 1
  10164. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10165. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10166. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10167. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10168. } else {
  10169. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10170. if ($fmetric['type'] == 'cidfont0') {
  10171. // CID-0
  10172. switch ($fonttype) {
  10173. case 'CID0JP': {
  10174. $pfile .= '// Japanese'."\n";
  10175. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10176. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10177. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10178. break;
  10179. }
  10180. case 'CID0KR': {
  10181. $pfile .= '// Korean'."\n";
  10182. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10183. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10184. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10185. break;
  10186. }
  10187. case 'CID0CS': {
  10188. $pfile .= '// Chinese Simplified'."\n";
  10189. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10190. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10191. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10192. break;
  10193. }
  10194. case 'CID0CT':
  10195. default: {
  10196. $pfile .= '// Chinese Traditional'."\n";
  10197. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10198. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10199. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10200. break;
  10201. }
  10202. }
  10203. } else {
  10204. // TrueType
  10205. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10206. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10207. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10208. // create CIDToGIDMap
  10209. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10210. foreach ($ctg as $cid => $gid) {
  10211. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10212. }
  10213. // store compressed CIDToGIDMap
  10214. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10215. fwrite($fp, gzcompress($cidtogidmap));
  10216. fclose($fp);
  10217. }
  10218. }
  10219. $pfile .= '$desc=array(';
  10220. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10221. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10222. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10223. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10224. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10225. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10226. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10227. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10228. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10229. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10230. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10231. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10232. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10233. $pfile .= ');'."\n";
  10234. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10235. $pfile .= '// --- EOF ---'."\n";
  10236. // store file
  10237. $fp = fopen($outpath.$font_name.'.php', 'w');
  10238. fwrite($fp, $pfile);
  10239. fclose($fp);
  10240. // return TCPDF font name
  10241. return $font_name;
  10242. }
  10243. /**
  10244. * Returns a subset of the TrueType font data without the unused glyphs.
  10245. * @param $font (string) TrueType font data.
  10246. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10247. * @return (string) A subset of TrueType font data without the unused glyphs.
  10248. * @author Nicola Asuni
  10249. * @protected
  10250. * @since 5.2.000 (2010-06-02)
  10251. */
  10252. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10253. ksort($subsetchars);
  10254. $offset = 0; // offset position of the font data
  10255. if ($this->_getULONG($font, $offset) != 0x10000) {
  10256. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10257. return $font;
  10258. }
  10259. $offset += 4;
  10260. // get number of tables
  10261. $numTables = $this->_getUSHORT($font, $offset);
  10262. $offset += 2;
  10263. // skip searchRange, entrySelector and rangeShift
  10264. $offset += 6;
  10265. // tables array
  10266. $table = array();
  10267. // for each table
  10268. for ($i = 0; $i < $numTables; ++$i) {
  10269. // get table info
  10270. $tag = substr($font, $offset, 4);
  10271. $offset += 4;
  10272. $table[$tag] = array();
  10273. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10274. $offset += 4;
  10275. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10276. $offset += 4;
  10277. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10278. $offset += 4;
  10279. }
  10280. // check magicNumber
  10281. $offset = $table['head']['offset'] + 12;
  10282. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10283. // magicNumber must be 0x5F0F3CF5
  10284. return $font;
  10285. }
  10286. $offset += 4;
  10287. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10288. $offset = $table['head']['offset'] + 50;
  10289. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10290. $offset += 2;
  10291. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10292. $indexToLoc = array();
  10293. $offset = $table['loca']['offset'];
  10294. if ($short_offset) {
  10295. // short version
  10296. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10297. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10298. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10299. $offset += 2;
  10300. }
  10301. } else {
  10302. // long version
  10303. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10304. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10305. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10306. $offset += 4;
  10307. }
  10308. }
  10309. // get glyphs indexes of chars from cmap table
  10310. $subsetglyphs = array(); // glyph IDs on key
  10311. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10312. $offset = $table['cmap']['offset'] + 2;
  10313. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10314. $offset += 2;
  10315. $encodingTables = array();
  10316. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10317. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10318. $offset += 2;
  10319. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10320. $offset += 2;
  10321. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10322. $offset += 4;
  10323. }
  10324. foreach ($encodingTables as $enctable) {
  10325. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  10326. $modesymbol = true;
  10327. } else {
  10328. $modesymbol = false;
  10329. }
  10330. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10331. $format = $this->_getUSHORT($font, $offset);
  10332. $offset += 2;
  10333. switch ($format) {
  10334. case 0: { // Format 0: Byte encoding table
  10335. $offset += 4; // skip length and version/language
  10336. for ($c = 0; $c < 256; ++$c) {
  10337. if (isset($subsetchars[$c])) {
  10338. $g = $this->_getBYTE($font, $offset);
  10339. $subsetglyphs[$g] = true;
  10340. }
  10341. ++$offset;
  10342. }
  10343. break;
  10344. }
  10345. case 2: { // Format 2: High-byte mapping through table
  10346. $offset += 4; // skip length and version/language
  10347. $numSubHeaders = 0;
  10348. for ($i = 0; $i < 256; ++$i) {
  10349. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10350. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10351. $offset += 2;
  10352. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10353. $numSubHeaders = $subHeaderKeys[$i];
  10354. }
  10355. }
  10356. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10357. ++$numSubHeaders;
  10358. // read subHeader structures
  10359. $subHeaders = array();
  10360. $numGlyphIndexArray = 0;
  10361. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10362. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10363. $offset += 2;
  10364. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10365. $offset += 2;
  10366. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  10367. $offset += 2;
  10368. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10369. $offset += 2;
  10370. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10371. $subHeaders[$k]['idRangeOffset'] /= 2;
  10372. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10373. }
  10374. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10375. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10376. $offset += 2;
  10377. }
  10378. for ($i = 0; $i < 256; ++$i) {
  10379. $k = $subHeaderKeys[$i];
  10380. if ($k == 0) {
  10381. // one byte code
  10382. $c = $i;
  10383. if (isset($subsetchars[$c])) {
  10384. $g = $glyphIndexArray[0];
  10385. $subsetglyphs[$g] = true;
  10386. }
  10387. } else {
  10388. // two bytes code
  10389. $start_byte = $subHeaders[$k]['firstCode'];
  10390. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10391. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10392. // combine high and low bytes
  10393. $c = (($i << 8) + $j);
  10394. if (isset($subsetchars[$c])) {
  10395. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10396. $g = $glyphIndexArray[$idRangeOffset];
  10397. $g += ($idDelta[$k] - 65536);
  10398. if ($g < 0) {
  10399. $g = 0;
  10400. }
  10401. $subsetglyphs[$g] = true;
  10402. }
  10403. }
  10404. }
  10405. }
  10406. break;
  10407. }
  10408. case 4: { // Format 4: Segment mapping to delta values
  10409. $length = $this->_getUSHORT($font, $offset);
  10410. $offset += 2;
  10411. $offset += 2; // skip version/language
  10412. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10413. $offset += 2;
  10414. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10415. $endCount = array(); // array of end character codes for each segment
  10416. for ($k = 0; $k < $segCount; ++$k) {
  10417. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10418. $offset += 2;
  10419. }
  10420. $offset += 2; // skip reservedPad
  10421. $startCount = array(); // array of start character codes for each segment
  10422. for ($k = 0; $k < $segCount; ++$k) {
  10423. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10424. $offset += 2;
  10425. }
  10426. $idDelta = array(); // delta for all character codes in segment
  10427. for ($k = 0; $k < $segCount; ++$k) {
  10428. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10429. $offset += 2;
  10430. }
  10431. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10432. for ($k = 0; $k < $segCount; ++$k) {
  10433. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10434. $offset += 2;
  10435. }
  10436. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10437. $glyphIdArray = array(); // glyph index array
  10438. for ($k = 0; $k < $gidlen; ++$k) {
  10439. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10440. $offset += 2;
  10441. }
  10442. for ($k = 0; $k < $segCount; ++$k) {
  10443. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10444. if (isset($subsetchars[$c])) {
  10445. if ($idRangeOffset[$k] == 0) {
  10446. $g = $c;
  10447. } else {
  10448. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10449. $g = $glyphIdArray[$gid];
  10450. }
  10451. $g += ($idDelta[$k] - 65536);
  10452. if ($g < 0) {
  10453. $g = 0;
  10454. }
  10455. $subsetglyphs[$g] = true;
  10456. }
  10457. }
  10458. }
  10459. break;
  10460. }
  10461. case 6: { // Format 6: Trimmed table mapping
  10462. $offset += 4; // skip length and version/language
  10463. $firstCode = $this->_getUSHORT($font, $offset);
  10464. $offset += 2;
  10465. $entryCount = $this->_getUSHORT($font, $offset);
  10466. $offset += 2;
  10467. for ($k = 0; $k < $entryCount; ++$k) {
  10468. $c = ($k + $firstCode);
  10469. if (isset($subsetchars[$c])) {
  10470. $g = $this->_getUSHORT($font, $offset);
  10471. $subsetglyphs[$g] = true;
  10472. }
  10473. $offset += 2;
  10474. }
  10475. break;
  10476. }
  10477. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10478. $offset += 10; // skip reserved, length and version/language
  10479. for ($k = 0; $k < 8192; ++$k) {
  10480. $is32[$k] = $this->_getBYTE($font, $offset);
  10481. ++$offset;
  10482. }
  10483. $nGroups = $this->_getULONG($font, $offset);
  10484. $offset += 4;
  10485. for ($i = 0; $i < $nGroups; ++$i) {
  10486. $startCharCode = $this->_getULONG($font, $offset);
  10487. $offset += 4;
  10488. $endCharCode = $this->_getULONG($font, $offset);
  10489. $offset += 4;
  10490. $startGlyphID = $this->_getULONG($font, $offset);
  10491. $offset += 4;
  10492. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10493. $is32idx = floor($c / 8);
  10494. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10495. $c = $k;
  10496. } else {
  10497. // 32 bit format
  10498. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10499. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10500. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10501. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10502. }
  10503. if (isset($subsetchars[$c])) {
  10504. $subsetglyphs[$startGlyphID] = true;
  10505. }
  10506. ++$startGlyphID;
  10507. }
  10508. }
  10509. break;
  10510. }
  10511. case 10: { // Format 10: Trimmed array
  10512. $offset += 10; // skip reserved, length and version/language
  10513. $startCharCode = $this->_getULONG($font, $offset);
  10514. $offset += 4;
  10515. $numChars = $this->_getULONG($font, $offset);
  10516. $offset += 4;
  10517. for ($k = 0; $k < $numChars; ++$k) {
  10518. $c = ($k + $startCharCode);
  10519. if (isset($subsetchars[$c])) {
  10520. $g = $this->_getUSHORT($font, $offset);
  10521. $subsetglyphs[$g] = true;
  10522. }
  10523. $offset += 2;
  10524. }
  10525. break;
  10526. }
  10527. case 12: { // Format 12: Segmented coverage
  10528. $offset += 10; // skip length and version/language
  10529. $nGroups = $this->_getULONG($font, $offset);
  10530. $offset += 4;
  10531. for ($k = 0; $k < $nGroups; ++$k) {
  10532. $startCharCode = $this->_getULONG($font, $offset);
  10533. $offset += 4;
  10534. $endCharCode = $this->_getULONG($font, $offset);
  10535. $offset += 4;
  10536. $startGlyphCode = $this->_getULONG($font, $offset);
  10537. $offset += 4;
  10538. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10539. if (isset($subsetchars[$c])) {
  10540. $subsetglyphs[$startGlyphCode] = true;
  10541. }
  10542. ++$startGlyphCode;
  10543. }
  10544. }
  10545. break;
  10546. }
  10547. case 13: { // Format 13: Many-to-one range mappings
  10548. // to be implemented ...
  10549. break;
  10550. }
  10551. case 14: { // Format 14: Unicode Variation Sequences
  10552. // to be implemented ...
  10553. break;
  10554. }
  10555. }
  10556. }
  10557. // include all parts of composite glyphs
  10558. $new_sga = $subsetglyphs;
  10559. while (!empty($new_sga)) {
  10560. $sga = $new_sga;
  10561. $new_sga = array();
  10562. foreach ($sga as $key => $val) {
  10563. if (isset($indexToLoc[$key])) {
  10564. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10565. $numberOfContours = $this->_getSHORT($font, $offset);
  10566. $offset += 2;
  10567. if ($numberOfContours < 0) { // composite glyph
  10568. $offset += 8; // skip xMin, yMin, xMax, yMax
  10569. do {
  10570. $flags = $this->_getUSHORT($font, $offset);
  10571. $offset += 2;
  10572. $glyphIndex = $this->_getUSHORT($font, $offset);
  10573. $offset += 2;
  10574. if (!isset($subsetglyphs[$glyphIndex])) {
  10575. // add missing glyphs
  10576. $new_sga[$glyphIndex] = true;
  10577. }
  10578. // skip some bytes by case
  10579. if ($flags & 1) {
  10580. $offset += 4;
  10581. } else {
  10582. $offset += 2;
  10583. }
  10584. if ($flags & 8) {
  10585. $offset += 2;
  10586. } elseif ($flags & 64) {
  10587. $offset += 4;
  10588. } elseif ($flags & 128) {
  10589. $offset += 8;
  10590. }
  10591. } while ($flags & 32);
  10592. }
  10593. }
  10594. }
  10595. $subsetglyphs += $new_sga;
  10596. }
  10597. // sort glyphs by key (and remove duplicates)
  10598. ksort($subsetglyphs);
  10599. // build new glyf and loca tables
  10600. $glyf = '';
  10601. $loca = '';
  10602. $offset = 0;
  10603. $glyf_offset = $table['glyf']['offset'];
  10604. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10605. if (isset($subsetglyphs[$i])) {
  10606. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10607. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10608. } else {
  10609. $length = 0;
  10610. }
  10611. if ($short_offset) {
  10612. $loca .= pack('n', ($offset / 2));
  10613. } else {
  10614. $loca .= pack('N', $offset);
  10615. }
  10616. $offset += $length;
  10617. }
  10618. // array of table names to preserve (loca and glyf tables will be added later)
  10619. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10620. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10621. // get the tables to preserve
  10622. $offset = 12;
  10623. foreach ($table as $tag => $val) {
  10624. if (in_array($tag, $table_names)) {
  10625. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10626. if ($tag == 'head') {
  10627. // set the checkSumAdjustment to 0
  10628. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10629. }
  10630. $pad = 4 - ($table[$tag]['length'] % 4);
  10631. if ($pad != 4) {
  10632. // the length of a table must be a multiple of four bytes
  10633. $table[$tag]['length'] += $pad;
  10634. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10635. }
  10636. $table[$tag]['offset'] = $offset;
  10637. $offset += $table[$tag]['length'];
  10638. // check sum is not changed (so keep the following line commented)
  10639. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10640. } else {
  10641. unset($table[$tag]);
  10642. }
  10643. }
  10644. // add loca
  10645. $table['loca']['data'] = $loca;
  10646. $table['loca']['length'] = strlen($loca);
  10647. $pad = 4 - ($table['loca']['length'] % 4);
  10648. if ($pad != 4) {
  10649. // the length of a table must be a multiple of four bytes
  10650. $table['loca']['length'] += $pad;
  10651. $table['loca']['data'] .= str_repeat("\x0", $pad);
  10652. }
  10653. $table['loca']['offset'] = $offset;
  10654. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  10655. $offset += $table['loca']['length'];
  10656. // add glyf
  10657. $table['glyf']['data'] = $glyf;
  10658. $table['glyf']['length'] = strlen($glyf);
  10659. $pad = 4 - ($table['glyf']['length'] % 4);
  10660. if ($pad != 4) {
  10661. // the length of a table must be a multiple of four bytes
  10662. $table['glyf']['length'] += $pad;
  10663. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  10664. }
  10665. $table['glyf']['offset'] = $offset;
  10666. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  10667. // rebuild font
  10668. $font = '';
  10669. $font .= pack('N', 0x10000); // sfnt version
  10670. $numTables = count($table);
  10671. $font .= pack('n', $numTables); // numTables
  10672. $entrySelector = floor(log($numTables, 2));
  10673. $searchRange = pow(2, $entrySelector) * 16;
  10674. $rangeShift = ($numTables * 16) - $searchRange;
  10675. $font .= pack('n', $searchRange); // searchRange
  10676. $font .= pack('n', $entrySelector); // entrySelector
  10677. $font .= pack('n', $rangeShift); // rangeShift
  10678. $offset = ($numTables * 16);
  10679. foreach ($table as $tag => $data) {
  10680. $font .= $tag; // tag
  10681. $font .= pack('N', $data['checkSum']); // checkSum
  10682. $font .= pack('N', ($data['offset'] + $offset)); // offset
  10683. $font .= pack('N', $data['length']); // length
  10684. }
  10685. foreach ($table as $data) {
  10686. $font .= $data['data'];
  10687. }
  10688. // set checkSumAdjustment on head table
  10689. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  10690. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  10691. return $font;
  10692. }
  10693. /**
  10694. * Returs the checksum of a TTF table.
  10695. * @param $table (string) table to check
  10696. * @param $length (int) length of table in bytes
  10697. * @return int checksum
  10698. * @author Nicola Asuni
  10699. * @protected
  10700. * @since 5.2.000 (2010-06-02)
  10701. */
  10702. protected function _getTTFtableChecksum($table, $length) {
  10703. $sum = 0;
  10704. $tlen = ($length / 4);
  10705. $offset = 0;
  10706. for ($i = 0; $i < $tlen; ++$i) {
  10707. $v = unpack('Ni', substr($table, $offset, 4));
  10708. $sum += $v['i'];
  10709. $offset += 4;
  10710. }
  10711. $sum = unpack('Ni', pack('N', $sum));
  10712. return $sum['i'];
  10713. }
  10714. /**
  10715. * Outputs font widths
  10716. * @param $font (array) font data
  10717. * @param $cidoffset (int) offset for CID values
  10718. * @return PDF command string for font widths
  10719. * @author Nicola Asuni
  10720. * @protected
  10721. * @since 4.4.000 (2008-12-07)
  10722. */
  10723. protected function _putfontwidths($font, $cidoffset=0) {
  10724. ksort($font['cw']);
  10725. $rangeid = 0;
  10726. $range = array();
  10727. $prevcid = -2;
  10728. $prevwidth = -1;
  10729. $interval = false;
  10730. // for each character
  10731. foreach ($font['cw'] as $cid => $width) {
  10732. $cid -= $cidoffset;
  10733. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  10734. // ignore the unused characters (font subsetting)
  10735. continue;
  10736. }
  10737. if ($width != $font['dw']) {
  10738. if ($cid == ($prevcid + 1)) {
  10739. // consecutive CID
  10740. if ($width == $prevwidth) {
  10741. if ($width == $range[$rangeid][0]) {
  10742. $range[$rangeid][] = $width;
  10743. } else {
  10744. array_pop($range[$rangeid]);
  10745. // new range
  10746. $rangeid = $prevcid;
  10747. $range[$rangeid] = array();
  10748. $range[$rangeid][] = $prevwidth;
  10749. $range[$rangeid][] = $width;
  10750. }
  10751. $interval = true;
  10752. $range[$rangeid]['interval'] = true;
  10753. } else {
  10754. if ($interval) {
  10755. // new range
  10756. $rangeid = $cid;
  10757. $range[$rangeid] = array();
  10758. $range[$rangeid][] = $width;
  10759. } else {
  10760. $range[$rangeid][] = $width;
  10761. }
  10762. $interval = false;
  10763. }
  10764. } else {
  10765. // new range
  10766. $rangeid = $cid;
  10767. $range[$rangeid] = array();
  10768. $range[$rangeid][] = $width;
  10769. $interval = false;
  10770. }
  10771. $prevcid = $cid;
  10772. $prevwidth = $width;
  10773. }
  10774. }
  10775. // optimize ranges
  10776. $prevk = -1;
  10777. $nextk = -1;
  10778. $prevint = false;
  10779. foreach ($range as $k => $ws) {
  10780. $cws = count($ws);
  10781. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  10782. if (isset($range[$k]['interval'])) {
  10783. unset($range[$k]['interval']);
  10784. }
  10785. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  10786. unset($range[$k]);
  10787. } else {
  10788. $prevk = $k;
  10789. }
  10790. $nextk = $k + $cws;
  10791. if (isset($ws['interval'])) {
  10792. if ($cws > 3) {
  10793. $prevint = true;
  10794. } else {
  10795. $prevint = false;
  10796. }
  10797. unset($range[$k]['interval']);
  10798. --$nextk;
  10799. } else {
  10800. $prevint = false;
  10801. }
  10802. }
  10803. // output data
  10804. $w = '';
  10805. foreach ($range as $k => $ws) {
  10806. if (count(array_count_values($ws)) == 1) {
  10807. // interval mode is more compact
  10808. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  10809. } else {
  10810. // range mode
  10811. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  10812. }
  10813. }
  10814. return '/W ['.$w.' ]';
  10815. }
  10816. /**
  10817. * Output fonts.
  10818. * @author Nicola Asuni
  10819. * @protected
  10820. */
  10821. protected function _putfonts() {
  10822. $nf = $this->n;
  10823. foreach ($this->diffs as $diff) {
  10824. //Encodings
  10825. $this->_newobj();
  10826. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  10827. }
  10828. $mqr = $this->get_mqr();
  10829. $this->set_mqr(false);
  10830. foreach ($this->FontFiles as $file => $info) {
  10831. // search and get font file to embedd
  10832. $fontdir = $info['fontdir'];
  10833. $file = strtolower($file);
  10834. $fontfile = '';
  10835. // search files on various directories
  10836. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  10837. $fontfile = $fontdir.$file;
  10838. } elseif (file_exists($this->_getfontpath().$file)) {
  10839. $fontfile = $this->_getfontpath().$file;
  10840. } elseif (file_exists($file)) {
  10841. $fontfile = $file;
  10842. }
  10843. if (!$this->empty_string($fontfile)) {
  10844. $font = file_get_contents($fontfile);
  10845. $compressed = (substr($file, -2) == '.z');
  10846. if ((!$compressed) AND (isset($info['length2']))) {
  10847. $header = (ord($font{0}) == 128);
  10848. if ($header) {
  10849. // strip first binary header
  10850. $font = substr($font, 6);
  10851. }
  10852. if ($header AND (ord($font[$info['length1']]) == 128)) {
  10853. // strip second binary header
  10854. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  10855. }
  10856. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  10857. if ($compressed) {
  10858. // uncompress font
  10859. $font = gzuncompress($font);
  10860. }
  10861. // merge subset characters
  10862. $subsetchars = array(); // used chars
  10863. foreach ($info['fontkeys'] as $fontkey) {
  10864. $fontinfo = $this->getFontBuffer($fontkey);
  10865. $subsetchars += $fontinfo['subsetchars'];
  10866. }
  10867. // rebuild a font subset
  10868. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  10869. // calculate new font length
  10870. $info['length1'] = strlen($font);
  10871. if ($compressed) {
  10872. // recompress font
  10873. $font = gzcompress($font);
  10874. }
  10875. }
  10876. $this->_newobj();
  10877. $this->FontFiles[$file]['n'] = $this->n;
  10878. $stream = $this->_getrawstream($font);
  10879. $out = '<< /Length '.strlen($stream);
  10880. if ($compressed) {
  10881. $out .= ' /Filter /FlateDecode';
  10882. }
  10883. $out .= ' /Length1 '.$info['length1'];
  10884. if (isset($info['length2'])) {
  10885. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  10886. }
  10887. $out .= ' >>';
  10888. $out .= ' stream'."\n".$stream."\n".'endstream';
  10889. $out .= "\n".'endobj';
  10890. $this->_out($out);
  10891. }
  10892. }
  10893. $this->set_mqr($mqr);
  10894. foreach ($this->fontkeys as $k) {
  10895. //Font objects
  10896. $font = $this->getFontBuffer($k);
  10897. $type = $font['type'];
  10898. $name = $font['name'];
  10899. if ($type == 'core') {
  10900. // standard core font
  10901. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10902. $out .= '<</Type /Font';
  10903. $out .= ' /Subtype /Type1';
  10904. $out .= ' /BaseFont /'.$name;
  10905. $out .= ' /Name /F'.$font['i'];
  10906. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  10907. $out .= ' /Encoding /WinAnsiEncoding';
  10908. }
  10909. if ($k == 'helvetica') {
  10910. // add default font for annotations
  10911. $this->annotation_fonts[$k] = $font['i'];
  10912. }
  10913. $out .= ' >>';
  10914. $out .= "\n".'endobj';
  10915. $this->_out($out);
  10916. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  10917. // additional Type1 or TrueType font
  10918. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10919. $out .= '<</Type /Font';
  10920. $out .= ' /Subtype /'.$type;
  10921. $out .= ' /BaseFont /'.$name;
  10922. $out .= ' /Name /F'.$font['i'];
  10923. $out .= ' /FirstChar 32 /LastChar 255';
  10924. $out .= ' /Widths '.($this->n + 1).' 0 R';
  10925. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  10926. if ($font['enc']) {
  10927. if (isset($font['diff'])) {
  10928. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  10929. } else {
  10930. $out .= ' /Encoding /WinAnsiEncoding';
  10931. }
  10932. }
  10933. $out .= ' >>';
  10934. $out .= "\n".'endobj';
  10935. $this->_out($out);
  10936. // Widths
  10937. $this->_newobj();
  10938. $s = '[';
  10939. for ($i = 32; $i < 256; ++$i) {
  10940. $s .= $font['cw'][$i].' ';
  10941. }
  10942. $s .= ']';
  10943. $s .= "\n".'endobj';
  10944. $this->_out($s);
  10945. //Descriptor
  10946. $this->_newobj();
  10947. $s = '<</Type /FontDescriptor /FontName /'.$name;
  10948. foreach ($font['desc'] as $fdk => $fdv) {
  10949. if (is_float($fdv)) {
  10950. $fdv = sprintf('%.3F', $fdv);
  10951. }
  10952. $s .= ' /'.$fdk.' '.$fdv.'';
  10953. }
  10954. if (!$this->empty_string($font['file'])) {
  10955. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  10956. }
  10957. $s .= '>>';
  10958. $s .= "\n".'endobj';
  10959. $this->_out($s);
  10960. } else {
  10961. // additional types
  10962. $mtd = '_put'.strtolower($type);
  10963. if (!method_exists($this, $mtd)) {
  10964. $this->Error('Unsupported font type: '.$type);
  10965. }
  10966. $this->$mtd($font);
  10967. }
  10968. }
  10969. }
  10970. /**
  10971. * Adds unicode fonts.<br>
  10972. * Based on PDF Reference 1.3 (section 5)
  10973. * @param $font (array) font data
  10974. * @protected
  10975. * @author Nicola Asuni
  10976. * @since 1.52.0.TC005 (2005-01-05)
  10977. */
  10978. protected function _puttruetypeunicode($font) {
  10979. $fontname = '';
  10980. if ($font['subset']) {
  10981. // change name for font subsetting
  10982. $subtag = sprintf('%06u', $font['i']);
  10983. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  10984. $fontname .= $subtag.'+';
  10985. }
  10986. $fontname .= $font['name'];
  10987. // Type0 Font
  10988. // A composite font composed of other fonts, organized hierarchically
  10989. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  10990. $out .= '<< /Type /Font';
  10991. $out .= ' /Subtype /Type0';
  10992. $out .= ' /BaseFont /'.$fontname;
  10993. $out .= ' /Name /F'.$font['i'];
  10994. $out .= ' /Encoding /'.$font['enc'];
  10995. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  10996. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  10997. $out .= ' >>';
  10998. $out .= "\n".'endobj';
  10999. $this->_out($out);
  11000. // ToUnicode map for Identity-H
  11001. $stream = "/CIDInit /ProcSet findresource begin\n";
  11002. $stream .= "12 dict begin\n";
  11003. $stream .= "begincmap\n";
  11004. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  11005. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  11006. $stream .= "/CMapType 2 def\n";
  11007. $stream .= "/WMode 0 def\n";
  11008. $stream .= "1 begincodespacerange\n";
  11009. $stream .= "<0000> <FFFF>\n";
  11010. $stream .= "endcodespacerange\n";
  11011. $stream .= "100 beginbfrange\n";
  11012. $stream .= "<0000> <00ff> <0000>\n";
  11013. $stream .= "<0100> <01ff> <0100>\n";
  11014. $stream .= "<0200> <02ff> <0200>\n";
  11015. $stream .= "<0300> <03ff> <0300>\n";
  11016. $stream .= "<0400> <04ff> <0400>\n";
  11017. $stream .= "<0500> <05ff> <0500>\n";
  11018. $stream .= "<0600> <06ff> <0600>\n";
  11019. $stream .= "<0700> <07ff> <0700>\n";
  11020. $stream .= "<0800> <08ff> <0800>\n";
  11021. $stream .= "<0900> <09ff> <0900>\n";
  11022. $stream .= "<0a00> <0aff> <0a00>\n";
  11023. $stream .= "<0b00> <0bff> <0b00>\n";
  11024. $stream .= "<0c00> <0cff> <0c00>\n";
  11025. $stream .= "<0d00> <0dff> <0d00>\n";
  11026. $stream .= "<0e00> <0eff> <0e00>\n";
  11027. $stream .= "<0f00> <0fff> <0f00>\n";
  11028. $stream .= "<1000> <10ff> <1000>\n";
  11029. $stream .= "<1100> <11ff> <1100>\n";
  11030. $stream .= "<1200> <12ff> <1200>\n";
  11031. $stream .= "<1300> <13ff> <1300>\n";
  11032. $stream .= "<1400> <14ff> <1400>\n";
  11033. $stream .= "<1500> <15ff> <1500>\n";
  11034. $stream .= "<1600> <16ff> <1600>\n";
  11035. $stream .= "<1700> <17ff> <1700>\n";
  11036. $stream .= "<1800> <18ff> <1800>\n";
  11037. $stream .= "<1900> <19ff> <1900>\n";
  11038. $stream .= "<1a00> <1aff> <1a00>\n";
  11039. $stream .= "<1b00> <1bff> <1b00>\n";
  11040. $stream .= "<1c00> <1cff> <1c00>\n";
  11041. $stream .= "<1d00> <1dff> <1d00>\n";
  11042. $stream .= "<1e00> <1eff> <1e00>\n";
  11043. $stream .= "<1f00> <1fff> <1f00>\n";
  11044. $stream .= "<2000> <20ff> <2000>\n";
  11045. $stream .= "<2100> <21ff> <2100>\n";
  11046. $stream .= "<2200> <22ff> <2200>\n";
  11047. $stream .= "<2300> <23ff> <2300>\n";
  11048. $stream .= "<2400> <24ff> <2400>\n";
  11049. $stream .= "<2500> <25ff> <2500>\n";
  11050. $stream .= "<2600> <26ff> <2600>\n";
  11051. $stream .= "<2700> <27ff> <2700>\n";
  11052. $stream .= "<2800> <28ff> <2800>\n";
  11053. $stream .= "<2900> <29ff> <2900>\n";
  11054. $stream .= "<2a00> <2aff> <2a00>\n";
  11055. $stream .= "<2b00> <2bff> <2b00>\n";
  11056. $stream .= "<2c00> <2cff> <2c00>\n";
  11057. $stream .= "<2d00> <2dff> <2d00>\n";
  11058. $stream .= "<2e00> <2eff> <2e00>\n";
  11059. $stream .= "<2f00> <2fff> <2f00>\n";
  11060. $stream .= "<3000> <30ff> <3000>\n";
  11061. $stream .= "<3100> <31ff> <3100>\n";
  11062. $stream .= "<3200> <32ff> <3200>\n";
  11063. $stream .= "<3300> <33ff> <3300>\n";
  11064. $stream .= "<3400> <34ff> <3400>\n";
  11065. $stream .= "<3500> <35ff> <3500>\n";
  11066. $stream .= "<3600> <36ff> <3600>\n";
  11067. $stream .= "<3700> <37ff> <3700>\n";
  11068. $stream .= "<3800> <38ff> <3800>\n";
  11069. $stream .= "<3900> <39ff> <3900>\n";
  11070. $stream .= "<3a00> <3aff> <3a00>\n";
  11071. $stream .= "<3b00> <3bff> <3b00>\n";
  11072. $stream .= "<3c00> <3cff> <3c00>\n";
  11073. $stream .= "<3d00> <3dff> <3d00>\n";
  11074. $stream .= "<3e00> <3eff> <3e00>\n";
  11075. $stream .= "<3f00> <3fff> <3f00>\n";
  11076. $stream .= "<4000> <40ff> <4000>\n";
  11077. $stream .= "<4100> <41ff> <4100>\n";
  11078. $stream .= "<4200> <42ff> <4200>\n";
  11079. $stream .= "<4300> <43ff> <4300>\n";
  11080. $stream .= "<4400> <44ff> <4400>\n";
  11081. $stream .= "<4500> <45ff> <4500>\n";
  11082. $stream .= "<4600> <46ff> <4600>\n";
  11083. $stream .= "<4700> <47ff> <4700>\n";
  11084. $stream .= "<4800> <48ff> <4800>\n";
  11085. $stream .= "<4900> <49ff> <4900>\n";
  11086. $stream .= "<4a00> <4aff> <4a00>\n";
  11087. $stream .= "<4b00> <4bff> <4b00>\n";
  11088. $stream .= "<4c00> <4cff> <4c00>\n";
  11089. $stream .= "<4d00> <4dff> <4d00>\n";
  11090. $stream .= "<4e00> <4eff> <4e00>\n";
  11091. $stream .= "<4f00> <4fff> <4f00>\n";
  11092. $stream .= "<5000> <50ff> <5000>\n";
  11093. $stream .= "<5100> <51ff> <5100>\n";
  11094. $stream .= "<5200> <52ff> <5200>\n";
  11095. $stream .= "<5300> <53ff> <5300>\n";
  11096. $stream .= "<5400> <54ff> <5400>\n";
  11097. $stream .= "<5500> <55ff> <5500>\n";
  11098. $stream .= "<5600> <56ff> <5600>\n";
  11099. $stream .= "<5700> <57ff> <5700>\n";
  11100. $stream .= "<5800> <58ff> <5800>\n";
  11101. $stream .= "<5900> <59ff> <5900>\n";
  11102. $stream .= "<5a00> <5aff> <5a00>\n";
  11103. $stream .= "<5b00> <5bff> <5b00>\n";
  11104. $stream .= "<5c00> <5cff> <5c00>\n";
  11105. $stream .= "<5d00> <5dff> <5d00>\n";
  11106. $stream .= "<5e00> <5eff> <5e00>\n";
  11107. $stream .= "<5f00> <5fff> <5f00>\n";
  11108. $stream .= "<6000> <60ff> <6000>\n";
  11109. $stream .= "<6100> <61ff> <6100>\n";
  11110. $stream .= "<6200> <62ff> <6200>\n";
  11111. $stream .= "<6300> <63ff> <6300>\n";
  11112. $stream .= "endbfrange\n";
  11113. $stream .= "100 beginbfrange\n";
  11114. $stream .= "<6400> <64ff> <6400>\n";
  11115. $stream .= "<6500> <65ff> <6500>\n";
  11116. $stream .= "<6600> <66ff> <6600>\n";
  11117. $stream .= "<6700> <67ff> <6700>\n";
  11118. $stream .= "<6800> <68ff> <6800>\n";
  11119. $stream .= "<6900> <69ff> <6900>\n";
  11120. $stream .= "<6a00> <6aff> <6a00>\n";
  11121. $stream .= "<6b00> <6bff> <6b00>\n";
  11122. $stream .= "<6c00> <6cff> <6c00>\n";
  11123. $stream .= "<6d00> <6dff> <6d00>\n";
  11124. $stream .= "<6e00> <6eff> <6e00>\n";
  11125. $stream .= "<6f00> <6fff> <6f00>\n";
  11126. $stream .= "<7000> <70ff> <7000>\n";
  11127. $stream .= "<7100> <71ff> <7100>\n";
  11128. $stream .= "<7200> <72ff> <7200>\n";
  11129. $stream .= "<7300> <73ff> <7300>\n";
  11130. $stream .= "<7400> <74ff> <7400>\n";
  11131. $stream .= "<7500> <75ff> <7500>\n";
  11132. $stream .= "<7600> <76ff> <7600>\n";
  11133. $stream .= "<7700> <77ff> <7700>\n";
  11134. $stream .= "<7800> <78ff> <7800>\n";
  11135. $stream .= "<7900> <79ff> <7900>\n";
  11136. $stream .= "<7a00> <7aff> <7a00>\n";
  11137. $stream .= "<7b00> <7bff> <7b00>\n";
  11138. $stream .= "<7c00> <7cff> <7c00>\n";
  11139. $stream .= "<7d00> <7dff> <7d00>\n";
  11140. $stream .= "<7e00> <7eff> <7e00>\n";
  11141. $stream .= "<7f00> <7fff> <7f00>\n";
  11142. $stream .= "<8000> <80ff> <8000>\n";
  11143. $stream .= "<8100> <81ff> <8100>\n";
  11144. $stream .= "<8200> <82ff> <8200>\n";
  11145. $stream .= "<8300> <83ff> <8300>\n";
  11146. $stream .= "<8400> <84ff> <8400>\n";
  11147. $stream .= "<8500> <85ff> <8500>\n";
  11148. $stream .= "<8600> <86ff> <8600>\n";
  11149. $stream .= "<8700> <87ff> <8700>\n";
  11150. $stream .= "<8800> <88ff> <8800>\n";
  11151. $stream .= "<8900> <89ff> <8900>\n";
  11152. $stream .= "<8a00> <8aff> <8a00>\n";
  11153. $stream .= "<8b00> <8bff> <8b00>\n";
  11154. $stream .= "<8c00> <8cff> <8c00>\n";
  11155. $stream .= "<8d00> <8dff> <8d00>\n";
  11156. $stream .= "<8e00> <8eff> <8e00>\n";
  11157. $stream .= "<8f00> <8fff> <8f00>\n";
  11158. $stream .= "<9000> <90ff> <9000>\n";
  11159. $stream .= "<9100> <91ff> <9100>\n";
  11160. $stream .= "<9200> <92ff> <9200>\n";
  11161. $stream .= "<9300> <93ff> <9300>\n";
  11162. $stream .= "<9400> <94ff> <9400>\n";
  11163. $stream .= "<9500> <95ff> <9500>\n";
  11164. $stream .= "<9600> <96ff> <9600>\n";
  11165. $stream .= "<9700> <97ff> <9700>\n";
  11166. $stream .= "<9800> <98ff> <9800>\n";
  11167. $stream .= "<9900> <99ff> <9900>\n";
  11168. $stream .= "<9a00> <9aff> <9a00>\n";
  11169. $stream .= "<9b00> <9bff> <9b00>\n";
  11170. $stream .= "<9c00> <9cff> <9c00>\n";
  11171. $stream .= "<9d00> <9dff> <9d00>\n";
  11172. $stream .= "<9e00> <9eff> <9e00>\n";
  11173. $stream .= "<9f00> <9fff> <9f00>\n";
  11174. $stream .= "<a000> <a0ff> <a000>\n";
  11175. $stream .= "<a100> <a1ff> <a100>\n";
  11176. $stream .= "<a200> <a2ff> <a200>\n";
  11177. $stream .= "<a300> <a3ff> <a300>\n";
  11178. $stream .= "<a400> <a4ff> <a400>\n";
  11179. $stream .= "<a500> <a5ff> <a500>\n";
  11180. $stream .= "<a600> <a6ff> <a600>\n";
  11181. $stream .= "<a700> <a7ff> <a700>\n";
  11182. $stream .= "<a800> <a8ff> <a800>\n";
  11183. $stream .= "<a900> <a9ff> <a900>\n";
  11184. $stream .= "<aa00> <aaff> <aa00>\n";
  11185. $stream .= "<ab00> <abff> <ab00>\n";
  11186. $stream .= "<ac00> <acff> <ac00>\n";
  11187. $stream .= "<ad00> <adff> <ad00>\n";
  11188. $stream .= "<ae00> <aeff> <ae00>\n";
  11189. $stream .= "<af00> <afff> <af00>\n";
  11190. $stream .= "<b000> <b0ff> <b000>\n";
  11191. $stream .= "<b100> <b1ff> <b100>\n";
  11192. $stream .= "<b200> <b2ff> <b200>\n";
  11193. $stream .= "<b300> <b3ff> <b300>\n";
  11194. $stream .= "<b400> <b4ff> <b400>\n";
  11195. $stream .= "<b500> <b5ff> <b500>\n";
  11196. $stream .= "<b600> <b6ff> <b600>\n";
  11197. $stream .= "<b700> <b7ff> <b700>\n";
  11198. $stream .= "<b800> <b8ff> <b800>\n";
  11199. $stream .= "<b900> <b9ff> <b900>\n";
  11200. $stream .= "<ba00> <baff> <ba00>\n";
  11201. $stream .= "<bb00> <bbff> <bb00>\n";
  11202. $stream .= "<bc00> <bcff> <bc00>\n";
  11203. $stream .= "<bd00> <bdff> <bd00>\n";
  11204. $stream .= "<be00> <beff> <be00>\n";
  11205. $stream .= "<bf00> <bfff> <bf00>\n";
  11206. $stream .= "<c000> <c0ff> <c000>\n";
  11207. $stream .= "<c100> <c1ff> <c100>\n";
  11208. $stream .= "<c200> <c2ff> <c200>\n";
  11209. $stream .= "<c300> <c3ff> <c300>\n";
  11210. $stream .= "<c400> <c4ff> <c400>\n";
  11211. $stream .= "<c500> <c5ff> <c500>\n";
  11212. $stream .= "<c600> <c6ff> <c600>\n";
  11213. $stream .= "<c700> <c7ff> <c700>\n";
  11214. $stream .= "endbfrange\n";
  11215. $stream .= "56 beginbfrange\n";
  11216. $stream .= "<c800> <c8ff> <c800>\n";
  11217. $stream .= "<c900> <c9ff> <c900>\n";
  11218. $stream .= "<ca00> <caff> <ca00>\n";
  11219. $stream .= "<cb00> <cbff> <cb00>\n";
  11220. $stream .= "<cc00> <ccff> <cc00>\n";
  11221. $stream .= "<cd00> <cdff> <cd00>\n";
  11222. $stream .= "<ce00> <ceff> <ce00>\n";
  11223. $stream .= "<cf00> <cfff> <cf00>\n";
  11224. $stream .= "<d000> <d0ff> <d000>\n";
  11225. $stream .= "<d100> <d1ff> <d100>\n";
  11226. $stream .= "<d200> <d2ff> <d200>\n";
  11227. $stream .= "<d300> <d3ff> <d300>\n";
  11228. $stream .= "<d400> <d4ff> <d400>\n";
  11229. $stream .= "<d500> <d5ff> <d500>\n";
  11230. $stream .= "<d600> <d6ff> <d600>\n";
  11231. $stream .= "<d700> <d7ff> <d700>\n";
  11232. $stream .= "<d800> <d8ff> <d800>\n";
  11233. $stream .= "<d900> <d9ff> <d900>\n";
  11234. $stream .= "<da00> <daff> <da00>\n";
  11235. $stream .= "<db00> <dbff> <db00>\n";
  11236. $stream .= "<dc00> <dcff> <dc00>\n";
  11237. $stream .= "<dd00> <ddff> <dd00>\n";
  11238. $stream .= "<de00> <deff> <de00>\n";
  11239. $stream .= "<df00> <dfff> <df00>\n";
  11240. $stream .= "<e000> <e0ff> <e000>\n";
  11241. $stream .= "<e100> <e1ff> <e100>\n";
  11242. $stream .= "<e200> <e2ff> <e200>\n";
  11243. $stream .= "<e300> <e3ff> <e300>\n";
  11244. $stream .= "<e400> <e4ff> <e400>\n";
  11245. $stream .= "<e500> <e5ff> <e500>\n";
  11246. $stream .= "<e600> <e6ff> <e600>\n";
  11247. $stream .= "<e700> <e7ff> <e700>\n";
  11248. $stream .= "<e800> <e8ff> <e800>\n";
  11249. $stream .= "<e900> <e9ff> <e900>\n";
  11250. $stream .= "<ea00> <eaff> <ea00>\n";
  11251. $stream .= "<eb00> <ebff> <eb00>\n";
  11252. $stream .= "<ec00> <ecff> <ec00>\n";
  11253. $stream .= "<ed00> <edff> <ed00>\n";
  11254. $stream .= "<ee00> <eeff> <ee00>\n";
  11255. $stream .= "<ef00> <efff> <ef00>\n";
  11256. $stream .= "<f000> <f0ff> <f000>\n";
  11257. $stream .= "<f100> <f1ff> <f100>\n";
  11258. $stream .= "<f200> <f2ff> <f200>\n";
  11259. $stream .= "<f300> <f3ff> <f300>\n";
  11260. $stream .= "<f400> <f4ff> <f400>\n";
  11261. $stream .= "<f500> <f5ff> <f500>\n";
  11262. $stream .= "<f600> <f6ff> <f600>\n";
  11263. $stream .= "<f700> <f7ff> <f700>\n";
  11264. $stream .= "<f800> <f8ff> <f800>\n";
  11265. $stream .= "<f900> <f9ff> <f900>\n";
  11266. $stream .= "<fa00> <faff> <fa00>\n";
  11267. $stream .= "<fb00> <fbff> <fb00>\n";
  11268. $stream .= "<fc00> <fcff> <fc00>\n";
  11269. $stream .= "<fd00> <fdff> <fd00>\n";
  11270. $stream .= "<fe00> <feff> <fe00>\n";
  11271. $stream .= "<ff00> <ffff> <ff00>\n";
  11272. $stream .= "endbfrange\n";
  11273. $stream .= "endcmap\n";
  11274. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11275. $stream .= "end\n";
  11276. $stream .= "end";
  11277. // ToUnicode Object
  11278. $this->_newobj();
  11279. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11280. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11281. $stream = $this->_getrawstream($stream);
  11282. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11283. // CIDFontType2
  11284. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11285. $oid = $this->_newobj();
  11286. $out = '<< /Type /Font';
  11287. $out .= ' /Subtype /CIDFontType2';
  11288. $out .= ' /BaseFont /'.$fontname;
  11289. // A dictionary containing entries that define the character collection of the CIDFont.
  11290. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11291. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11292. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11293. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11294. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11295. $out .= ' /DW '.$font['dw']; // default width
  11296. $out .= "\n".$this->_putfontwidths($font, 0);
  11297. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11298. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11299. }
  11300. $out .= ' >>';
  11301. $out .= "\n".'endobj';
  11302. $this->_out($out);
  11303. // Font descriptor
  11304. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11305. $this->_newobj();
  11306. $out = '<< /Type /FontDescriptor';
  11307. $out .= ' /FontName /'.$fontname;
  11308. foreach ($font['desc'] as $key => $value) {
  11309. if (is_float($value)) {
  11310. $value = sprintf('%.3F', $value);
  11311. }
  11312. $out .= ' /'.$key.' '.$value;
  11313. }
  11314. $fontdir = false;
  11315. if (!$this->empty_string($font['file'])) {
  11316. // A stream containing a TrueType font
  11317. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11318. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11319. }
  11320. $out .= ' >>';
  11321. $out .= "\n".'endobj';
  11322. $this->_out($out);
  11323. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11324. $this->_newobj();
  11325. // Embed CIDToGIDMap
  11326. // A specification of the mapping from CIDs to glyph indices
  11327. // search and get CTG font file to embedd
  11328. $ctgfile = strtolower($font['ctg']);
  11329. // search and get ctg font file to embedd
  11330. $fontfile = '';
  11331. // search files on various directories
  11332. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11333. $fontfile = $fontdir.$ctgfile;
  11334. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11335. $fontfile = $this->_getfontpath().$ctgfile;
  11336. } elseif (file_exists($ctgfile)) {
  11337. $fontfile = $ctgfile;
  11338. }
  11339. if ($this->empty_string($fontfile)) {
  11340. $this->Error('Font file not found: '.$ctgfile);
  11341. }
  11342. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11343. $out = '<< /Length '.strlen($stream).'';
  11344. if (substr($fontfile, -2) == '.z') { // check file extension
  11345. // Decompresses data encoded using the public-domain
  11346. // zlib/deflate compression method, reproducing the
  11347. // original text or binary data
  11348. $out .= ' /Filter /FlateDecode';
  11349. }
  11350. $out .= ' >>';
  11351. $out .= ' stream'."\n".$stream."\n".'endstream';
  11352. $out .= "\n".'endobj';
  11353. $this->_out($out);
  11354. }
  11355. }
  11356. /**
  11357. * Output CID-0 fonts.
  11358. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11359. * @param $font (array) font data
  11360. * @protected
  11361. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11362. * @since 3.2.000 (2008-06-23)
  11363. */
  11364. protected function _putcidfont0($font) {
  11365. $cidoffset = 0;
  11366. if (!isset($font['cw'][1])) {
  11367. $cidoffset = 31;
  11368. }
  11369. if (isset($font['cidinfo']['uni2cid'])) {
  11370. // convert unicode to cid.
  11371. $uni2cid = $font['cidinfo']['uni2cid'];
  11372. $cw = array();
  11373. foreach ($font['cw'] as $uni => $width) {
  11374. if (isset($uni2cid[$uni])) {
  11375. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11376. } elseif ($uni < 256) {
  11377. $cw[$uni] = $width;
  11378. } // else unknown character
  11379. }
  11380. $font = array_merge($font, array('cw' => $cw));
  11381. }
  11382. $name = $font['name'];
  11383. $enc = $font['enc'];
  11384. if ($enc) {
  11385. $longname = $name.'-'.$enc;
  11386. } else {
  11387. $longname = $name;
  11388. }
  11389. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11390. $out .= '<</Type /Font';
  11391. $out .= ' /Subtype /Type0';
  11392. $out .= ' /BaseFont /'.$longname;
  11393. $out .= ' /Name /F'.$font['i'];
  11394. if ($enc) {
  11395. $out .= ' /Encoding /'.$enc;
  11396. }
  11397. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11398. $out .= ' >>';
  11399. $out .= "\n".'endobj';
  11400. $this->_out($out);
  11401. $oid = $this->_newobj();
  11402. $out = '<</Type /Font';
  11403. $out .= ' /Subtype /CIDFontType0';
  11404. $out .= ' /BaseFont /'.$name;
  11405. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11406. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11407. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11408. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11409. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11410. $out .= ' /DW '.$font['dw'];
  11411. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11412. $out .= ' >>';
  11413. $out .= "\n".'endobj';
  11414. $this->_out($out);
  11415. $this->_newobj();
  11416. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11417. foreach ($font['desc'] as $k => $v) {
  11418. if ($k != 'Style') {
  11419. if (is_float($v)) {
  11420. $v = sprintf('%.3F', $v);
  11421. }
  11422. $s .= ' /'.$k.' '.$v.'';
  11423. }
  11424. }
  11425. $s .= '>>';
  11426. $s .= "\n".'endobj';
  11427. $this->_out($s);
  11428. }
  11429. /**
  11430. * Output images.
  11431. * @protected
  11432. */
  11433. protected function _putimages() {
  11434. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11435. foreach ($this->imagekeys as $file) {
  11436. $info = $this->getImageBuffer($file);
  11437. // set object for alternate images array
  11438. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11439. $altoid = $this->_newobj();
  11440. $out = '[';
  11441. foreach ($info['altimgs'] as $altimage) {
  11442. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11443. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11444. $out .= ' /DefaultForPrinting';
  11445. if ($altimage[1] === true) {
  11446. $out .= ' true';
  11447. } else {
  11448. $out .= ' false';
  11449. }
  11450. $out .= ' >>';
  11451. }
  11452. }
  11453. $out .= ' ]';
  11454. $out .= "\n".'endobj';
  11455. $this->_out($out);
  11456. }
  11457. // set image object
  11458. $oid = $this->_newobj();
  11459. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11460. $this->setImageSubBuffer($file, 'n', $this->n);
  11461. $out = '<</Type /XObject';
  11462. $out .= ' /Subtype /Image';
  11463. $out .= ' /Width '.$info['w'];
  11464. $out .= ' /Height '.$info['h'];
  11465. if (array_key_exists('masked', $info)) {
  11466. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11467. }
  11468. // set color space
  11469. $icc = false;
  11470. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11471. // ICC Colour Space
  11472. $icc = true;
  11473. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11474. } elseif ($info['cs'] == 'Indexed') {
  11475. // Indexed Colour Space
  11476. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11477. } else {
  11478. // Device Colour Space
  11479. $out .= ' /ColorSpace /'.$info['cs'];
  11480. }
  11481. if ($info['cs'] == 'DeviceCMYK') {
  11482. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11483. }
  11484. $out .= ' /BitsPerComponent '.$info['bpc'];
  11485. if (isset($altoid) AND ($altoid > 0)) {
  11486. // reference to alternate images dictionary
  11487. $out .= ' /Alternates '.$altoid.' 0 R';
  11488. }
  11489. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11490. // external stream
  11491. $out .= ' /Length 0';
  11492. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11493. if (isset($info['f'])) {
  11494. $out .= ' /FFilter /'.$info['f'];
  11495. }
  11496. $out .= ' >>';
  11497. $out .= ' stream'."\n".'endstream';
  11498. } else {
  11499. if (isset($info['f'])) {
  11500. $out .= ' /Filter /'.$info['f'];
  11501. }
  11502. if (isset($info['parms'])) {
  11503. $out .= ' '.$info['parms'];
  11504. }
  11505. if (isset($info['trns']) AND is_array($info['trns'])) {
  11506. $trns = '';
  11507. $count_info = count($info['trns']);
  11508. for ($i=0; $i < $count_info; ++$i) {
  11509. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11510. }
  11511. $out .= ' /Mask ['.$trns.']';
  11512. }
  11513. $stream = $this->_getrawstream($info['data']);
  11514. $out .= ' /Length '.strlen($stream).' >>';
  11515. $out .= ' stream'."\n".$stream."\n".'endstream';
  11516. }
  11517. $out .= "\n".'endobj';
  11518. $this->_out($out);
  11519. if ($icc) {
  11520. // ICC colour profile
  11521. $this->_newobj();
  11522. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11523. $icc = $this->_getrawstream($icc);
  11524. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11525. } elseif ($info['cs'] == 'Indexed') {
  11526. // colour palette
  11527. $this->_newobj();
  11528. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11529. $pal = $this->_getrawstream($pal);
  11530. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11531. }
  11532. }
  11533. }
  11534. /**
  11535. * Output Form XObjects Templates.
  11536. * @author Nicola Asuni
  11537. * @since 5.8.017 (2010-08-24)
  11538. * @protected
  11539. * @see startTemplate(), endTemplate(), printTemplate()
  11540. */
  11541. protected function _putxobjects() {
  11542. foreach ($this->xobjects as $key => $data) {
  11543. if (isset($data['outdata'])) {
  11544. $stream = trim($data['outdata']);
  11545. $out = $this->_getobj($data['n'])."\n";
  11546. $out .= '<<';
  11547. $out .= ' /Type /XObject';
  11548. $out .= ' /Subtype /Form';
  11549. $out .= ' /FormType 1';
  11550. if ($this->compress) {
  11551. $stream = gzcompress($stream);
  11552. $out .= ' /Filter /FlateDecode';
  11553. }
  11554. $out .= sprintf(' /BBox [%.2F %.2F %.2F %.2F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  11555. $out .= ' /Matrix [1 0 0 1 0 0]';
  11556. $out .= ' /Resources <<';
  11557. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11558. if (!$this->pdfa_mode) {
  11559. // transparency
  11560. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11561. $out .= ' /ExtGState <<';
  11562. foreach ($data['extgstates'] as $k => $extgstate) {
  11563. if (isset($this->extgstates[$k]['name'])) {
  11564. $out .= ' /'.$this->extgstates[$k]['name'];
  11565. } else {
  11566. $out .= ' /GS'.$k;
  11567. }
  11568. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11569. }
  11570. $out .= ' >>';
  11571. }
  11572. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11573. $gp = '';
  11574. $gs = '';
  11575. foreach ($data['gradients'] as $id => $grad) {
  11576. // gradient patterns
  11577. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11578. // gradient shadings
  11579. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11580. }
  11581. $out .= ' /Pattern <<'.$gp.' >>';
  11582. $out .= ' /Shading <<'.$gs.' >>';
  11583. }
  11584. }
  11585. // spot colors
  11586. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11587. $out .= ' /ColorSpace <<';
  11588. foreach ($data['spot_colors'] as $name => $color) {
  11589. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11590. }
  11591. $out .= ' >>';
  11592. }
  11593. // fonts
  11594. if (!empty($data['fonts'])) {
  11595. $out .= ' /Font <<';
  11596. foreach ($data['fonts'] as $fontkey => $fontid) {
  11597. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11598. }
  11599. $out .= ' >>';
  11600. }
  11601. // images or nested xobjects
  11602. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11603. $out .= ' /XObject <<';
  11604. foreach ($data['images'] as $imgid) {
  11605. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11606. }
  11607. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11608. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11609. }
  11610. $out .= ' >>';
  11611. }
  11612. $out .= ' >>'; //end resources
  11613. if (isset($data['group']) AND ($data['group'] !== false)) {
  11614. // set transparency group
  11615. $out .= ' /Group << /Type /Group /S /Transparency';
  11616. if (is_array($data['group'])) {
  11617. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11618. $out .= ' /CS /'.$data['group']['CS'];
  11619. }
  11620. if (isset($data['group']['I'])) {
  11621. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11622. }
  11623. if (isset($data['group']['K'])) {
  11624. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11625. }
  11626. }
  11627. $out .= ' >>';
  11628. }
  11629. $stream = $this->_getrawstream($stream, $data['n']);
  11630. $out .= ' /Length '.strlen($stream);
  11631. $out .= ' >>';
  11632. $out .= ' stream'."\n".$stream."\n".'endstream';
  11633. $out .= "\n".'endobj';
  11634. $this->_out($out);
  11635. }
  11636. }
  11637. }
  11638. /**
  11639. * Output Spot Colors Resources.
  11640. * @protected
  11641. * @since 4.0.024 (2008-09-12)
  11642. */
  11643. protected function _putspotcolors() {
  11644. foreach ($this->spot_colors as $name => $color) {
  11645. $this->_newobj();
  11646. $this->spot_colors[$name]['n'] = $this->n;
  11647. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  11648. $out .= ' /DeviceCMYK <<';
  11649. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  11650. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  11651. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  11652. $out .= "\n".'endobj';
  11653. $this->_out($out);
  11654. }
  11655. }
  11656. /**
  11657. * Return XObjects Dictionary.
  11658. * @return string XObjects dictionary
  11659. * @protected
  11660. * @since 5.8.014 (2010-08-23)
  11661. */
  11662. protected function _getxobjectdict() {
  11663. $out = '';
  11664. foreach ($this->xobjects as $id => $objid) {
  11665. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  11666. }
  11667. return $out;
  11668. }
  11669. /**
  11670. * Output Resources Dictionary.
  11671. * @protected
  11672. */
  11673. protected function _putresourcedict() {
  11674. $out = $this->_getobj(2)."\n";
  11675. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11676. $out .= ' /Font <<';
  11677. foreach ($this->fontkeys as $fontkey) {
  11678. $font = $this->getFontBuffer($fontkey);
  11679. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  11680. }
  11681. $out .= ' >>';
  11682. $out .= ' /XObject <<';
  11683. $out .= $this->_getxobjectdict();
  11684. $out .= ' >>';
  11685. // layers
  11686. if (!empty($this->pdflayers)) {
  11687. $out .= ' /Properties <<';
  11688. foreach ($this->pdflayers as $layer) {
  11689. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  11690. }
  11691. $out .= ' >>';
  11692. }
  11693. if (!$this->pdfa_mode) {
  11694. // transparency
  11695. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  11696. $out .= ' /ExtGState <<';
  11697. foreach ($this->extgstates as $k => $extgstate) {
  11698. if (isset($extgstate['name'])) {
  11699. $out .= ' /'.$extgstate['name'];
  11700. } else {
  11701. $out .= ' /GS'.$k;
  11702. }
  11703. $out .= ' '.$extgstate['n'].' 0 R';
  11704. }
  11705. $out .= ' >>';
  11706. }
  11707. if (isset($this->gradients) AND !empty($this->gradients)) {
  11708. $gp = '';
  11709. $gs = '';
  11710. foreach ($this->gradients as $id => $grad) {
  11711. // gradient patterns
  11712. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  11713. // gradient shadings
  11714. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  11715. }
  11716. $out .= ' /Pattern <<'.$gp.' >>';
  11717. $out .= ' /Shading <<'.$gs.' >>';
  11718. }
  11719. }
  11720. // spot colors
  11721. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  11722. $out .= ' /ColorSpace <<';
  11723. foreach ($this->spot_colors as $color) {
  11724. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  11725. }
  11726. $out .= ' >>';
  11727. }
  11728. $out .= ' >>';
  11729. $out .= "\n".'endobj';
  11730. $this->_out($out);
  11731. }
  11732. /**
  11733. * Output Resources.
  11734. * @protected
  11735. */
  11736. protected function _putresources() {
  11737. $this->_putextgstates();
  11738. $this->_putocg();
  11739. $this->_putfonts();
  11740. $this->_putimages();
  11741. $this->_putspotcolors();
  11742. $this->_putshaders();
  11743. $this->_putxobjects();
  11744. $this->_putresourcedict();
  11745. $this->_putdests();
  11746. $this->_putbookmarks();
  11747. $this->_putEmbeddedFiles();
  11748. $this->_putannotsobjs();
  11749. $this->_putjavascript();
  11750. $this->_putencryption();
  11751. }
  11752. /**
  11753. * Adds some Metadata information (Document Information Dictionary)
  11754. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  11755. * @return int object id
  11756. * @protected
  11757. */
  11758. protected function _putinfo() {
  11759. $oid = $this->_newobj();
  11760. $out = '<<';
  11761. // store current isunicode value
  11762. $prev_isunicode = $this->isunicode;
  11763. if ($this->docinfounicode) {
  11764. $this->isunicode = true;
  11765. }
  11766. if (!$this->empty_string($this->title)) {
  11767. // The document's title.
  11768. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  11769. }
  11770. if (!$this->empty_string($this->author)) {
  11771. // The name of the person who created the document.
  11772. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  11773. }
  11774. if (!$this->empty_string($this->subject)) {
  11775. // The subject of the document.
  11776. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  11777. }
  11778. if (!$this->empty_string($this->keywords)) {
  11779. // Keywords associated with the document.
  11780. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  11781. }
  11782. if (!$this->empty_string($this->creator)) {
  11783. // 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.
  11784. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  11785. }
  11786. // restore previous isunicode value
  11787. $this->isunicode = $prev_isunicode;
  11788. // default producer
  11789. $out .= ' /Producer '.$this->_textstring("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29", $oid);
  11790. // The date and time the document was created, in human-readable form
  11791. $out .= ' /CreationDate '.$this->_datestring();
  11792. // The date and time the document was most recently modified, in human-readable form
  11793. $out .= ' /ModDate '.$this->_datestring();
  11794. // A name object indicating whether the document has been modified to include trapping information
  11795. $out .= ' /Trapped /False';
  11796. $out .= ' >>';
  11797. $out .= "\n".'endobj';
  11798. $this->_out($out);
  11799. return $oid;
  11800. }
  11801. /**
  11802. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  11803. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  11804. * @param $xmp (string) Custom XMP data.
  11805. * @since 5.9.128 (2011-10-06)
  11806. * @public
  11807. */
  11808. public function setExtraXMP($xmp) {
  11809. $this->custom_xmp = $xmp;
  11810. }
  11811. /**
  11812. * Put XMP data object and return ID.
  11813. * @return (int) The object ID.
  11814. * @since 5.9.121 (2011-09-28)
  11815. * @protected
  11816. */
  11817. protected function _putXMP() {
  11818. $oid = $this->_newobj();
  11819. // store current isunicode value
  11820. $prev_isunicode = $this->isunicode;
  11821. $this->isunicode = true;
  11822. $prev_encrypted = $this->encrypted;
  11823. $this->encrypted = false;
  11824. // set XMP data
  11825. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  11826. $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";
  11827. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  11828. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  11829. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  11830. $xmp .= "\t\t\t".'<dc:title>'."\n";
  11831. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11832. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  11833. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11834. $xmp .= "\t\t\t".'</dc:title>'."\n";
  11835. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  11836. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  11837. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  11838. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  11839. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  11840. $xmp .= "\t\t\t".'<dc:description>'."\n";
  11841. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11842. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  11843. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11844. $xmp .= "\t\t\t".'</dc:description>'."\n";
  11845. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  11846. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11847. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  11848. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11849. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  11850. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11851. // convert date format
  11852. $docdate = substr($this->doc_date, 0, 4).'-'.substr($this->doc_date, 4, 2).'-'.substr($this->doc_date, 6, 2);
  11853. $docdate .= 'T'.substr($this->doc_date, 8, 2).':'.substr($this->doc_date, 10, 2).':'.substr($this->doc_date, 12, 2);
  11854. $docdate .= '+'.substr($this->doc_date, 15, 2).':'.substr($this->doc_date, 18, 2);
  11855. $docdate = $this->_escapeXML($docdate);
  11856. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  11857. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$docdate.'</xmp:CreateDate>'."\n";
  11858. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  11859. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docdate.'</xmp:ModifyDate>'."\n";
  11860. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$docdate.'</xmp:MetadataDate>'."\n";
  11861. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11862. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  11863. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  11864. $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29").'</pdf:Producer>'."\n";
  11865. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11866. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  11867. $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);
  11868. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  11869. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  11870. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11871. if ($this->pdfa_mode) {
  11872. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  11873. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  11874. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  11875. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11876. }
  11877. // XMP extension schemas
  11878. $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";
  11879. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  11880. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11881. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11882. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  11883. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  11884. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  11885. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11886. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11887. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  11888. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  11889. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  11890. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11891. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11892. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11893. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11894. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  11895. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  11896. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  11897. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11898. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11899. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11900. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11901. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11902. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  11903. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  11904. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  11905. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11906. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11907. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11908. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11909. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  11910. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  11911. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  11912. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11913. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11914. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11915. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  11916. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  11917. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11918. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11919. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11920. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11921. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  11922. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  11923. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11924. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11925. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11926. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11927. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11928. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11929. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  11930. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11931. $xmp .= "\t".'</rdf:RDF>'."\n";
  11932. $xmp .= $this->custom_xmp;
  11933. $xmp .= '</x:xmpmeta>'."\n";
  11934. $xmp .= '<?xpacket end="w"?>';
  11935. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  11936. // restore previous isunicode value
  11937. $this->isunicode = $prev_isunicode;
  11938. $this->encrypted = $prev_encrypted;
  11939. $this->_out($out);
  11940. return $oid;
  11941. }
  11942. /**
  11943. * Output Catalog.
  11944. * @return int object id
  11945. * @protected
  11946. */
  11947. protected function _putcatalog() {
  11948. // put XMP
  11949. $xmpobj = $this->_putXMP();
  11950. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  11951. if ($this->pdfa_mode OR $this->force_srgb) {
  11952. $iccobj = $this->_newobj();
  11953. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  11954. $filter = '';
  11955. if ($this->compress) {
  11956. $filter = ' /Filter /FlateDecode';
  11957. $icc = gzcompress($icc);
  11958. }
  11959. $icc = $this->_getrawstream($icc);
  11960. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11961. }
  11962. // start catalog
  11963. $oid = $this->_newobj();
  11964. $out = '<< /Type /Catalog';
  11965. $out .= ' /Version /'.$this->PDFVersion;
  11966. //$out .= ' /Extensions <<>>';
  11967. $out .= ' /Pages 1 0 R';
  11968. //$out .= ' /PageLabels ' //...;
  11969. $out .= ' /Names <<';
  11970. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  11971. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  11972. }
  11973. $out .= ' >>';
  11974. if (!empty($this->dests)) {
  11975. $out .= ' /Dests '.$this->n_dests.' 0 R';
  11976. }
  11977. $out .= $this->_putviewerpreferences();
  11978. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  11979. $out .= ' /PageLayout /'.$this->LayoutMode;
  11980. }
  11981. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  11982. $out .= ' /PageMode /'.$this->PageMode;
  11983. }
  11984. if (count($this->outlines) > 0) {
  11985. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  11986. $out .= ' /PageMode /UseOutlines';
  11987. }
  11988. //$out .= ' /Threads []';
  11989. if ($this->ZoomMode == 'fullpage') {
  11990. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  11991. } elseif ($this->ZoomMode == 'fullwidth') {
  11992. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  11993. } elseif ($this->ZoomMode == 'real') {
  11994. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  11995. } elseif (!is_string($this->ZoomMode)) {
  11996. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
  11997. }
  11998. //$out .= ' /AA <<>>';
  11999. //$out .= ' /URI <<>>';
  12000. $out .= ' /Metadata '.$xmpobj.' 0 R';
  12001. //$out .= ' /StructTreeRoot <<>>';
  12002. //$out .= ' /MarkInfo <<>>';
  12003. if (isset($this->l['a_meta_language'])) {
  12004. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  12005. }
  12006. //$out .= ' /SpiderInfo <<>>';
  12007. // set OutputIntent to sRGB IEC61966-2.1 if required
  12008. if ($this->pdfa_mode OR $this->force_srgb) {
  12009. $out .= ' /OutputIntents [<<';
  12010. $out .= ' /Type /OutputIntent';
  12011. $out .= ' /S /GTS_PDFA1';
  12012. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12013. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12014. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12015. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12016. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12017. $out .= ' >>]';
  12018. }
  12019. //$out .= ' /PieceInfo <<>>';
  12020. if (!empty($this->pdflayers)) {
  12021. $lyrobjs = '';
  12022. $lyrobjs_print = '';
  12023. $lyrobjs_view = '';
  12024. foreach ($this->pdflayers as $layer) {
  12025. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12026. if ($layer['print']) {
  12027. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12028. }
  12029. if ($layer['view']) {
  12030. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12031. }
  12032. }
  12033. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12034. $out .= ' /D <<';
  12035. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12036. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12037. $out .= ' /BaseState /ON';
  12038. $out .= ' /ON ['.$lyrobjs_print.']';
  12039. $out .= ' /OFF ['.$lyrobjs_view.']';
  12040. $out .= ' /Intent /View';
  12041. $out .= ' /AS [';
  12042. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12043. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12044. $out .= ' ]';
  12045. $out .= ' /Order ['.$lyrobjs.']';
  12046. $out .= ' /ListMode /AllPages';
  12047. //$out .= ' /RBGroups ['..']';
  12048. //$out .= ' /Locked ['..']';
  12049. $out .= ' >>';
  12050. $out .= ' >>';
  12051. }
  12052. // AcroForm
  12053. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12054. $out .= ' /AcroForm <<';
  12055. $objrefs = '';
  12056. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12057. // set reference for signature object
  12058. $objrefs .= $this->sig_obj_id.' 0 R';
  12059. }
  12060. if (!empty($this->empty_signature_appearance)) {
  12061. foreach ($this->empty_signature_appearance as $esa) {
  12062. // set reference for empty signature objects
  12063. $objrefs .= ' '.$esa['objid'].' 0 R';
  12064. }
  12065. }
  12066. if (!empty($this->form_obj_id)) {
  12067. foreach($this->form_obj_id as $objid) {
  12068. $objrefs .= ' '.$objid.' 0 R';
  12069. }
  12070. }
  12071. $out .= ' /Fields ['.$objrefs.']';
  12072. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12073. $out .= ' /NeedAppearances false';
  12074. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12075. if ($this->signature_data['cert_type'] > 0) {
  12076. $out .= ' /SigFlags 3';
  12077. } else {
  12078. $out .= ' /SigFlags 1';
  12079. }
  12080. }
  12081. //$out .= ' /CO ';
  12082. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12083. $out .= ' /DR <<';
  12084. $out .= ' /Font <<';
  12085. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12086. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12087. }
  12088. $out .= ' >> >>';
  12089. }
  12090. $font = $this->getFontBuffer('helvetica');
  12091. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12092. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12093. //$out .= ' /XFA ';
  12094. $out .= ' >>';
  12095. // signatures
  12096. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12097. if ($this->signature_data['cert_type'] > 0) {
  12098. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12099. } else {
  12100. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12101. }
  12102. }
  12103. }
  12104. //$out .= ' /Legal <<>>';
  12105. //$out .= ' /Requirements []';
  12106. //$out .= ' /Collection <<>>';
  12107. //$out .= ' /NeedsRendering true';
  12108. $out .= ' >>';
  12109. $out .= "\n".'endobj';
  12110. $this->_out($out);
  12111. return $oid;
  12112. }
  12113. /**
  12114. * Output viewer preferences.
  12115. * @return string for viewer preferences
  12116. * @author Nicola asuni
  12117. * @since 3.1.000 (2008-06-09)
  12118. * @protected
  12119. */
  12120. protected function _putviewerpreferences() {
  12121. $out = ' /ViewerPreferences <<';
  12122. if ($this->rtl) {
  12123. $out .= ' /Direction /R2L';
  12124. } else {
  12125. $out .= ' /Direction /L2R';
  12126. }
  12127. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12128. $out .= ' /HideToolbar true';
  12129. }
  12130. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12131. $out .= ' /HideMenubar true';
  12132. }
  12133. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12134. $out .= ' /HideWindowUI true';
  12135. }
  12136. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12137. $out .= ' /FitWindow true';
  12138. }
  12139. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12140. $out .= ' /CenterWindow true';
  12141. }
  12142. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12143. $out .= ' /DisplayDocTitle true';
  12144. }
  12145. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12146. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12147. }
  12148. if (isset($this->viewer_preferences['ViewArea'])) {
  12149. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12150. }
  12151. if (isset($this->viewer_preferences['ViewClip'])) {
  12152. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12153. }
  12154. if (isset($this->viewer_preferences['PrintArea'])) {
  12155. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12156. }
  12157. if (isset($this->viewer_preferences['PrintClip'])) {
  12158. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12159. }
  12160. if (isset($this->viewer_preferences['PrintScaling'])) {
  12161. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12162. }
  12163. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12164. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12165. }
  12166. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12167. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12168. $out .= ' /PickTrayByPDFSize true';
  12169. } else {
  12170. $out .= ' /PickTrayByPDFSize false';
  12171. }
  12172. }
  12173. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12174. $PrintPageRangeNum = '';
  12175. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12176. $PrintPageRangeNum .= ' '.($v - 1).'';
  12177. }
  12178. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12179. }
  12180. if (isset($this->viewer_preferences['NumCopies'])) {
  12181. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12182. }
  12183. $out .= ' >>';
  12184. return $out;
  12185. }
  12186. /**
  12187. * Output PDF File Header (7.5.2).
  12188. * @protected
  12189. */
  12190. protected function _putheader() {
  12191. $this->_out('%PDF-'.$this->PDFVersion);
  12192. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12193. }
  12194. /**
  12195. * Output end of document (EOF).
  12196. * @protected
  12197. */
  12198. protected function _enddoc() {
  12199. $this->state = 1;
  12200. $this->_putheader();
  12201. $this->_putpages();
  12202. $this->_putresources();
  12203. // empty signature fields
  12204. if (!empty($this->empty_signature_appearance)) {
  12205. foreach ($this->empty_signature_appearance as $key => $esa) {
  12206. // widget annotation for empty signature
  12207. $out = $this->_getobj($esa['objid'])."\n";
  12208. $out .= '<< /Type /Annot';
  12209. $out .= ' /Subtype /Widget';
  12210. $out .= ' /Rect ['.$esa['rect'].']';
  12211. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12212. $out .= ' /F 4';
  12213. $out .= ' /FT /Sig';
  12214. $signame = sprintf('Signature_%03d', ($key + 1));
  12215. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12216. $out .= ' /Ff 0';
  12217. $out .= ' >>';
  12218. $out .= "\n".'endobj';
  12219. $this->_out($out);
  12220. }
  12221. }
  12222. // Signature
  12223. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12224. // widget annotation for signature
  12225. $out = $this->_getobj($this->sig_obj_id)."\n";
  12226. $out .= '<< /Type /Annot';
  12227. $out .= ' /Subtype /Widget';
  12228. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12229. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12230. $out .= ' /F 4';
  12231. $out .= ' /FT /Sig';
  12232. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12233. $out .= ' /Ff 0';
  12234. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12235. $out .= ' >>';
  12236. $out .= "\n".'endobj';
  12237. $this->_out($out);
  12238. // signature
  12239. $this->_putsignature();
  12240. }
  12241. // Info
  12242. $objid_info = $this->_putinfo();
  12243. // Catalog
  12244. $objid_catalog = $this->_putcatalog();
  12245. // Cross-ref
  12246. $o = $this->bufferlen;
  12247. // XREF section
  12248. $this->_out('xref');
  12249. $this->_out('0 '.($this->n + 1));
  12250. $this->_out('0000000000 65535 f ');
  12251. for ($i=1; $i <= $this->n; ++$i) {
  12252. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12253. $this->offsets[$i] = $this->offsets[($i - 1)];
  12254. }
  12255. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12256. }
  12257. // TRAILER
  12258. $out = 'trailer'."\n";
  12259. $out .= '<<';
  12260. $out .= ' /Size '.($this->n + 1);
  12261. $out .= ' /Root '.$objid_catalog.' 0 R';
  12262. $out .= ' /Info '.$objid_info.' 0 R';
  12263. if ($this->encrypted) {
  12264. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12265. }
  12266. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12267. $out .= ' >>';
  12268. $this->_out($out);
  12269. $this->_out('startxref');
  12270. $this->_out($o);
  12271. $this->_out('%%EOF');
  12272. $this->state = 3; // end-of-doc
  12273. if ($this->diskcache) {
  12274. // remove temporary files used for images
  12275. foreach ($this->imagekeys as $key) {
  12276. // remove temporary files
  12277. unlink($this->images[$key]);
  12278. }
  12279. foreach ($this->fontkeys as $key) {
  12280. // remove temporary files
  12281. unlink($this->fonts[$key]);
  12282. }
  12283. }
  12284. }
  12285. /**
  12286. * Initialize a new page.
  12287. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12288. * @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().
  12289. * @protected
  12290. * @see getPageSizeFromFormat(), setPageFormat()
  12291. */
  12292. protected function _beginpage($orientation='', $format='') {
  12293. ++$this->page;
  12294. $this->setPageBuffer($this->page, '');
  12295. // initialize array for graphics tranformation positions inside a page buffer
  12296. $this->transfmrk[$this->page] = array();
  12297. $this->state = 2;
  12298. if ($this->empty_string($orientation)) {
  12299. if (isset($this->CurOrientation)) {
  12300. $orientation = $this->CurOrientation;
  12301. } elseif ($this->fwPt > $this->fhPt) {
  12302. // landscape
  12303. $orientation = 'L';
  12304. } else {
  12305. // portrait
  12306. $orientation = 'P';
  12307. }
  12308. }
  12309. if ($this->empty_string($format)) {
  12310. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12311. $this->setPageOrientation($orientation);
  12312. } else {
  12313. $this->setPageFormat($format, $orientation);
  12314. }
  12315. if ($this->rtl) {
  12316. $this->x = $this->w - $this->rMargin;
  12317. } else {
  12318. $this->x = $this->lMargin;
  12319. }
  12320. $this->y = $this->tMargin;
  12321. if (isset($this->newpagegroup[$this->page])) {
  12322. // start a new group
  12323. $this->currpagegroup = $this->newpagegroup[$this->page];
  12324. $this->pagegroups[$this->currpagegroup] = 1;
  12325. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12326. ++$this->pagegroups[$this->currpagegroup];
  12327. }
  12328. }
  12329. /**
  12330. * Mark end of page.
  12331. * @protected
  12332. */
  12333. protected function _endpage() {
  12334. $this->setVisibility('all');
  12335. $this->state = 1;
  12336. }
  12337. /**
  12338. * Begin a new object and return the object number.
  12339. * @return int object number
  12340. * @protected
  12341. */
  12342. protected function _newobj() {
  12343. $this->_out($this->_getobj());
  12344. return $this->n;
  12345. }
  12346. /**
  12347. * Return the starting object string for the selected object ID.
  12348. * @param $objid (int) Object ID (leave empty to get a new ID).
  12349. * @return string the starting object string
  12350. * @protected
  12351. * @since 5.8.009 (2010-08-20)
  12352. */
  12353. protected function _getobj($objid='') {
  12354. if ($objid === '') {
  12355. ++$this->n;
  12356. $objid = $this->n;
  12357. }
  12358. $this->offsets[$objid] = $this->bufferlen;
  12359. return $objid.' 0 obj';
  12360. }
  12361. /**
  12362. * Underline text.
  12363. * @param $x (int) X coordinate
  12364. * @param $y (int) Y coordinate
  12365. * @param $txt (string) text to underline
  12366. * @protected
  12367. */
  12368. protected function _dounderline($x, $y, $txt) {
  12369. $w = $this->GetStringWidth($txt);
  12370. return $this->_dounderlinew($x, $y, $w);
  12371. }
  12372. /**
  12373. * Underline for rectangular text area.
  12374. * @param $x (int) X coordinate
  12375. * @param $y (int) Y coordinate
  12376. * @param $w (int) width to underline
  12377. * @protected
  12378. * @since 4.8.008 (2009-09-29)
  12379. */
  12380. protected function _dounderlinew($x, $y, $w) {
  12381. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12382. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12383. }
  12384. /**
  12385. * Line through text.
  12386. * @param $x (int) X coordinate
  12387. * @param $y (int) Y coordinate
  12388. * @param $txt (string) text to linethrough
  12389. * @protected
  12390. */
  12391. protected function _dolinethrough($x, $y, $txt) {
  12392. $w = $this->GetStringWidth($txt);
  12393. return $this->_dolinethroughw($x, $y, $w);
  12394. }
  12395. /**
  12396. * Line through for rectangular text area.
  12397. * @param $x (int) X coordinate
  12398. * @param $y (int) Y coordinate
  12399. * @param $w (int) line length (width)
  12400. * @protected
  12401. * @since 4.9.008 (2009-09-29)
  12402. */
  12403. protected function _dolinethroughw($x, $y, $w) {
  12404. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12405. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12406. }
  12407. /**
  12408. * Overline text.
  12409. * @param $x (int) X coordinate
  12410. * @param $y (int) Y coordinate
  12411. * @param $txt (string) text to overline
  12412. * @protected
  12413. * @since 4.9.015 (2010-04-19)
  12414. */
  12415. protected function _dooverline($x, $y, $txt) {
  12416. $w = $this->GetStringWidth($txt);
  12417. return $this->_dooverlinew($x, $y, $w);
  12418. }
  12419. /**
  12420. * Overline for rectangular text area.
  12421. * @param $x (int) X coordinate
  12422. * @param $y (int) Y coordinate
  12423. * @param $w (int) width to overline
  12424. * @protected
  12425. * @since 4.9.015 (2010-04-19)
  12426. */
  12427. protected function _dooverlinew($x, $y, $w) {
  12428. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12429. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12430. }
  12431. /**
  12432. * Read a 4-byte (32 bit) integer from file.
  12433. * @param $f (string) file name.
  12434. * @return 4-byte integer
  12435. * @protected
  12436. */
  12437. protected function _freadint($f) {
  12438. $a = unpack('Ni', fread($f, 4));
  12439. return $a['i'];
  12440. }
  12441. /**
  12442. * Add "\" before "\", "(" and ")"
  12443. * @param $s (string) string to escape.
  12444. * @return string escaped string.
  12445. * @protected
  12446. */
  12447. protected function _escape($s) {
  12448. // the chr(13) substitution fixes the Bugs item #1421290.
  12449. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12450. }
  12451. /**
  12452. * Format a data string for meta information
  12453. * @param $s (string) data string to escape.
  12454. * @param $n (int) object ID
  12455. * @return string escaped string.
  12456. * @protected
  12457. */
  12458. protected function _datastring($s, $n=0) {
  12459. if ($n == 0) {
  12460. $n = $this->n;
  12461. }
  12462. $s = $this->_encrypt_data($n, $s);
  12463. return '('. $this->_escape($s).')';
  12464. }
  12465. /**
  12466. * Returns a formatted date for meta information
  12467. * @param $n (int) object ID
  12468. * @return string escaped date string.
  12469. * @protected
  12470. * @since 4.6.028 (2009-08-25)
  12471. */
  12472. protected function _datestring($n=0) {
  12473. return $this->_datastring('D:'.$this->doc_date, $n);
  12474. }
  12475. /**
  12476. * Format a text string for meta information
  12477. * @param $s (string) string to escape.
  12478. * @param $n (int) object ID
  12479. * @return string escaped string.
  12480. * @protected
  12481. */
  12482. protected function _textstring($s, $n=0) {
  12483. if ($this->isunicode) {
  12484. //Convert string to UTF-16BE
  12485. $s = $this->UTF8ToUTF16BE($s, true);
  12486. }
  12487. return $this->_datastring($s, $n);
  12488. }
  12489. /**
  12490. * THIS METHOD IS DEPRECATED
  12491. * Format a text string
  12492. * @param $s (string) string to escape.
  12493. * @return string escaped string.
  12494. * @protected
  12495. * @deprecated
  12496. */
  12497. protected function _escapetext($s) {
  12498. if ($this->isunicode) {
  12499. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12500. $s = $this->UTF8ToLatin1($s);
  12501. } else {
  12502. //Convert string to UTF-16BE and reverse RTL language
  12503. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12504. }
  12505. }
  12506. return $this->_escape($s);
  12507. }
  12508. /**
  12509. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12510. * @param $str (string) Input string to convert.
  12511. * @return converted string
  12512. * @since 5.9.121 (2011-09-28)
  12513. * @protected
  12514. */
  12515. protected function _escapeXML($str) {
  12516. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12517. $str = strtr($str, $replaceTable);
  12518. return $str;
  12519. }
  12520. /**
  12521. * get raw output stream.
  12522. * @param $s (string) string to output.
  12523. * @param $n (int) object reference for encryption mode
  12524. * @protected
  12525. * @author Nicola Asuni
  12526. * @since 5.5.000 (2010-06-22)
  12527. */
  12528. protected function _getrawstream($s, $n=0) {
  12529. if ($n <= 0) {
  12530. // default to current object
  12531. $n = $this->n;
  12532. }
  12533. return $this->_encrypt_data($n, $s);
  12534. }
  12535. /**
  12536. * Format output stream (DEPRECATED).
  12537. * @param $s (string) string to output.
  12538. * @param $n (int) object reference for encryption mode
  12539. * @protected
  12540. * @deprecated
  12541. */
  12542. protected function _getstream($s, $n=0) {
  12543. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12544. }
  12545. /**
  12546. * Output a stream (DEPRECATED).
  12547. * @param $s (string) string to output.
  12548. * @param $n (int) object reference for encryption mode
  12549. * @protected
  12550. * @deprecated
  12551. */
  12552. protected function _putstream($s, $n=0) {
  12553. $this->_out($this->_getstream($s, $n));
  12554. }
  12555. /**
  12556. * Output a string to the document.
  12557. * @param $s (string) string to output.
  12558. * @protected
  12559. */
  12560. protected function _out($s) {
  12561. if ($this->state == 2) {
  12562. if ($this->inxobj) {
  12563. // we are inside an XObject template
  12564. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  12565. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  12566. // puts data before page footer
  12567. $pagebuff = $this->getPageBuffer($this->page);
  12568. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  12569. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  12570. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  12571. // update footer position
  12572. $this->footerpos[$this->page] += strlen($s."\n");
  12573. } else {
  12574. $this->setPageBuffer($this->page, $s."\n", true);
  12575. }
  12576. } else {
  12577. $this->setBuffer($s."\n");
  12578. }
  12579. }
  12580. /**
  12581. * Converts UTF-8 strings to codepoints array.<br>
  12582. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  12583. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  12584. * <pre>
  12585. * Char. number range | UTF-8 octet sequence
  12586. * (hexadecimal) | (binary)
  12587. * --------------------+-----------------------------------------------
  12588. * 0000 0000-0000 007F | 0xxxxxxx
  12589. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  12590. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  12591. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  12592. * ---------------------------------------------------------------------
  12593. *
  12594. * ABFN notation:
  12595. * ---------------------------------------------------------------------
  12596. * UTF8-octets = *( UTF8-char )
  12597. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  12598. * UTF8-1 = %x00-7F
  12599. * UTF8-2 = %xC2-DF UTF8-tail
  12600. *
  12601. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  12602. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  12603. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  12604. * %xF4 %x80-8F 2( UTF8-tail )
  12605. * UTF8-tail = %x80-BF
  12606. * ---------------------------------------------------------------------
  12607. * </pre>
  12608. * @param $str (string) string to process.
  12609. * @return array containing codepoints (UTF-8 characters values)
  12610. * @protected
  12611. * @author Nicola Asuni
  12612. * @since 1.53.0.TC005 (2005-01-05)
  12613. */
  12614. protected function UTF8StringToArray($str) {
  12615. // build a unique string key
  12616. $strkey = md5($str);
  12617. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  12618. // return cached value
  12619. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  12620. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  12621. if ($this->isunicode) {
  12622. foreach ($chrarray as $chr) {
  12623. // store this char for font subsetting
  12624. $this->CurrentFont['subsetchars'][$chr] = true;
  12625. }
  12626. // update font subsetchars
  12627. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12628. }
  12629. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12630. }
  12631. return $chrarray;
  12632. }
  12633. // check cache size
  12634. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  12635. // remove first element
  12636. array_shift($this->cache_UTF8StringToArray);
  12637. }
  12638. // new cache array for selected string
  12639. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  12640. ++$this->cache_size_UTF8StringToArray;
  12641. if (!$this->isunicode) {
  12642. // split string into array of equivalent codes
  12643. $strarr = array();
  12644. $strlen = strlen($str);
  12645. for ($i=0; $i < $strlen; ++$i) {
  12646. $strarr[] = ord($str[$i]);
  12647. }
  12648. // insert new value on cache
  12649. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  12650. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12651. return $strarr;
  12652. }
  12653. $unichar = -1; // last unicode char
  12654. $unicode = array(); // array containing unicode values
  12655. $bytes = array(); // array containing single character byte sequences
  12656. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  12657. $str .= ''; // force $str to be a string
  12658. $length = strlen($str);
  12659. for ($i = 0; $i < $length; ++$i) {
  12660. $char = ord($str[$i]); // get one string character at time
  12661. if (count($bytes) == 0) { // get starting octect
  12662. if ($char <= 0x7F) {
  12663. $unichar = $char; // use the character "as is" because is ASCII
  12664. $numbytes = 1;
  12665. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  12666. $bytes[] = ($char - 0xC0) << 0x06;
  12667. $numbytes = 2;
  12668. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  12669. $bytes[] = ($char - 0xE0) << 0x0C;
  12670. $numbytes = 3;
  12671. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  12672. $bytes[] = ($char - 0xF0) << 0x12;
  12673. $numbytes = 4;
  12674. } else {
  12675. // use replacement character for other invalid sequences
  12676. $unichar = 0xFFFD;
  12677. $bytes = array();
  12678. $numbytes = 1;
  12679. }
  12680. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  12681. $bytes[] = $char - 0x80;
  12682. if (count($bytes) == $numbytes) {
  12683. // compose UTF-8 bytes to a single unicode value
  12684. $char = $bytes[0];
  12685. for ($j = 1; $j < $numbytes; ++$j) {
  12686. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  12687. }
  12688. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  12689. /* The definition of UTF-8 prohibits encoding character numbers between
  12690. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  12691. encoding form (as surrogate pairs) and do not directly represent
  12692. characters. */
  12693. $unichar = 0xFFFD; // use replacement character
  12694. } else {
  12695. $unichar = $char; // add char to array
  12696. }
  12697. // reset data for next char
  12698. $bytes = array();
  12699. $numbytes = 1;
  12700. }
  12701. } else {
  12702. // use replacement character for other invalid sequences
  12703. $unichar = 0xFFFD;
  12704. $bytes = array();
  12705. $numbytes = 1;
  12706. }
  12707. if ($unichar >= 0) {
  12708. // insert unicode value into array
  12709. $unicode[] = $unichar;
  12710. // store this char for font subsetting
  12711. $this->CurrentFont['subsetchars'][$unichar] = true;
  12712. $unichar = -1;
  12713. }
  12714. }
  12715. // update font subsetchars
  12716. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12717. // insert new value on cache
  12718. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  12719. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12720. return $unicode;
  12721. }
  12722. /**
  12723. * Converts UTF-8 strings to UTF16-BE.<br>
  12724. * @param $str (string) string to process.
  12725. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12726. * @return string
  12727. * @author Nicola Asuni
  12728. * @since 1.53.0.TC005 (2005-01-05)
  12729. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  12730. * @protected
  12731. */
  12732. protected function UTF8ToUTF16BE($str, $setbom=false) {
  12733. if (!$this->isunicode) {
  12734. return $str; // string is not in unicode
  12735. }
  12736. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12737. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  12738. }
  12739. /**
  12740. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  12741. * @param $str (string) string to process.
  12742. * @return string
  12743. * @author Andrew Whitehead, Nicola Asuni
  12744. * @protected
  12745. * @since 3.2.000 (2008-06-23)
  12746. */
  12747. protected function UTF8ToLatin1($str) {
  12748. if (!$this->isunicode) {
  12749. return $str; // string is not in unicode
  12750. }
  12751. $outstr = ''; // string to be returned
  12752. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12753. foreach ($unicode as $char) {
  12754. if ($char < 256) {
  12755. $outstr .= chr($char);
  12756. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12757. // map from UTF-8
  12758. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  12759. } elseif ($char == 0xFFFD) {
  12760. // skip
  12761. } else {
  12762. $outstr .= '?';
  12763. }
  12764. }
  12765. return $outstr;
  12766. }
  12767. /**
  12768. * Converts UTF-8 characters array to array of Latin1 characters<br>
  12769. * @param $unicode (array) array containing UTF-8 unicode values
  12770. * @return array
  12771. * @author Nicola Asuni
  12772. * @protected
  12773. * @since 4.8.023 (2010-01-15)
  12774. */
  12775. protected function UTF8ArrToLatin1($unicode) {
  12776. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  12777. return $unicode;
  12778. }
  12779. $outarr = array(); // array to be returned
  12780. foreach ($unicode as $char) {
  12781. if ($char < 256) {
  12782. $outarr[] = $char;
  12783. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12784. // map from UTF-8
  12785. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  12786. } elseif ($char == 0xFFFD) {
  12787. // skip
  12788. } else {
  12789. $outarr[] = 63; // '?' character
  12790. }
  12791. }
  12792. return $outarr;
  12793. }
  12794. /**
  12795. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  12796. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  12797. * <pre>
  12798. * Encoding UTF-16:
  12799. *
  12800. * Encoding of a single character from an ISO 10646 character value to
  12801. * UTF-16 proceeds as follows. Let U be the character number, no greater
  12802. * than 0x10FFFF.
  12803. *
  12804. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  12805. * terminate.
  12806. *
  12807. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  12808. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  12809. * represented in 20 bits.
  12810. *
  12811. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  12812. * 0xDC00, respectively. These integers each have 10 bits free to
  12813. * encode the character value, for a total of 20 bits.
  12814. *
  12815. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  12816. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  12817. * bits of W2. Terminate.
  12818. *
  12819. * Graphically, steps 2 through 4 look like:
  12820. * U' = yyyyyyyyyyxxxxxxxxxx
  12821. * W1 = 110110yyyyyyyyyy
  12822. * W2 = 110111xxxxxxxxxx
  12823. * </pre>
  12824. * @param $unicode (array) array containing UTF-8 unicode values
  12825. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12826. * @return string
  12827. * @protected
  12828. * @author Nicola Asuni
  12829. * @since 2.1.000 (2008-01-08)
  12830. * @see UTF8ToUTF16BE()
  12831. */
  12832. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  12833. $outstr = ''; // string to be returned
  12834. if ($setbom) {
  12835. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  12836. }
  12837. foreach ($unicode as $char) {
  12838. if ($char == 0x200b) {
  12839. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  12840. } elseif ($char == 0xFFFD) {
  12841. $outstr .= "\xFF\xFD"; // replacement character
  12842. } elseif ($char < 0x10000) {
  12843. $outstr .= chr($char >> 0x08);
  12844. $outstr .= chr($char & 0xFF);
  12845. } else {
  12846. $char -= 0x10000;
  12847. $w1 = 0xD800 | ($char >> 0x0a);
  12848. $w2 = 0xDC00 | ($char & 0x3FF);
  12849. $outstr .= chr($w1 >> 0x08);
  12850. $outstr .= chr($w1 & 0xFF);
  12851. $outstr .= chr($w2 >> 0x08);
  12852. $outstr .= chr($w2 & 0xFF);
  12853. }
  12854. }
  12855. return $outstr;
  12856. }
  12857. // ====================================================
  12858. /**
  12859. * Set header font.
  12860. * @param $font (array) font
  12861. * @public
  12862. * @since 1.1
  12863. */
  12864. public function setHeaderFont($font) {
  12865. $this->header_font = $font;
  12866. }
  12867. /**
  12868. * Get header font.
  12869. * @return array()
  12870. * @public
  12871. * @since 4.0.012 (2008-07-24)
  12872. */
  12873. public function getHeaderFont() {
  12874. return $this->header_font;
  12875. }
  12876. /**
  12877. * Set footer font.
  12878. * @param $font (array) font
  12879. * @public
  12880. * @since 1.1
  12881. */
  12882. public function setFooterFont($font) {
  12883. $this->footer_font = $font;
  12884. }
  12885. /**
  12886. * Get Footer font.
  12887. * @return array()
  12888. * @public
  12889. * @since 4.0.012 (2008-07-24)
  12890. */
  12891. public function getFooterFont() {
  12892. return $this->footer_font;
  12893. }
  12894. /**
  12895. * Set language array.
  12896. * @param $language (array)
  12897. * @public
  12898. * @since 1.1
  12899. */
  12900. public function setLanguageArray($language) {
  12901. $this->l = $language;
  12902. if (isset($this->l['a_meta_dir'])) {
  12903. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  12904. } else {
  12905. $this->rtl = false;
  12906. }
  12907. }
  12908. /**
  12909. * Returns the PDF data.
  12910. * @public
  12911. */
  12912. public function getPDFData() {
  12913. if ($this->state < 3) {
  12914. $this->Close();
  12915. }
  12916. return $this->buffer;
  12917. }
  12918. /**
  12919. * Output anchor link.
  12920. * @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;)
  12921. * @param $name (string) link name
  12922. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  12923. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  12924. * @param $color (array) array of RGB text color
  12925. * @param $style (string) font style (U, D, B, I)
  12926. * @param $firstblock (boolean) if true the string is the starting of a line.
  12927. * @return the number of cells used or the remaining text if $firstline = true;
  12928. * @public
  12929. */
  12930. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  12931. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  12932. // convert url to internal link
  12933. $lnkdata = explode(',', $url);
  12934. if (isset($lnkdata[0])) {
  12935. $page = intval(substr($lnkdata[0], 1));
  12936. if (empty($page) OR ($page <= 0)) {
  12937. $page = $this->page;
  12938. }
  12939. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  12940. $lnky = floatval($lnkdata[1]);
  12941. } else {
  12942. $lnky = 0;
  12943. }
  12944. $url = $this->AddLink();
  12945. $this->SetLink($url, $lnky, $page);
  12946. }
  12947. }
  12948. // store current settings
  12949. $prevcolor = $this->fgcolor;
  12950. $prevstyle = $this->FontStyle;
  12951. if (empty($color)) {
  12952. $this->SetTextColorArray($this->htmlLinkColorArray);
  12953. } else {
  12954. $this->SetTextColorArray($color);
  12955. }
  12956. if ($style == -1) {
  12957. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  12958. } else {
  12959. $this->SetFont('', $this->FontStyle.$style);
  12960. }
  12961. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  12962. // restore settings
  12963. $this->SetFont('', $prevstyle);
  12964. $this->SetTextColorArray($prevcolor);
  12965. return $ret;
  12966. }
  12967. /**
  12968. * Returns an array (RGB or CMYK) from an html color name, or a six-digit (i.e. #3FE5AA), or three-digit (i.e. #7FF) hexadecimal color, or a javascript color array, or javascript color name.
  12969. * @param $hcolor (string) HTML color.
  12970. * @param $defcol (array) Color to return in case of error.
  12971. * @return array RGB or CMYK color, or false in case of error.
  12972. * @public
  12973. */
  12974. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  12975. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  12976. $color = strtolower($color);
  12977. // check for javascript color array syntax
  12978. if (strpos($color, '[') !== false) {
  12979. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  12980. $returncolor = array();
  12981. switch ($m[1]) {
  12982. case 'cmyk': {
  12983. // RGB
  12984. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  12985. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  12986. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  12987. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  12988. break;
  12989. }
  12990. case 'rgb': {
  12991. // RGB
  12992. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  12993. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  12994. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  12995. break;
  12996. }
  12997. case 'g': {
  12998. // grayscale
  12999. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  13000. break;
  13001. }
  13002. case 't':
  13003. default: {
  13004. // transparent (empty array)
  13005. break;
  13006. }
  13007. }
  13008. return $returncolor;
  13009. }
  13010. } elseif (($dotpos = strpos($color, '.')) !== false) {
  13011. // remove class parent (i.e.: color.red)
  13012. $color = substr($color, ($dotpos + 1));
  13013. if ($color == 'transparent') {
  13014. // transparent (empty array)
  13015. return array();
  13016. }
  13017. }
  13018. if (strlen($color) == 0) {
  13019. return $defcol;
  13020. }
  13021. // RGB ARRAY
  13022. if (substr($color, 0, 3) == 'rgb') {
  13023. $codes = substr($color, 4);
  13024. $codes = str_replace(')', '', $codes);
  13025. $returncolor = explode(',', $codes);
  13026. foreach ($returncolor as $key => $val) {
  13027. if (strpos($val, '%') > 0) {
  13028. // percentage
  13029. $returncolor[$key] = (255 * intval($val) / 100);
  13030. } else {
  13031. $returncolor[$key] = intval($val);
  13032. }
  13033. // normalize value
  13034. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  13035. }
  13036. return $returncolor;
  13037. }
  13038. // CMYK ARRAY
  13039. if (substr($color, 0, 4) == 'cmyk') {
  13040. $codes = substr($color, 5);
  13041. $codes = str_replace(')', '', $codes);
  13042. $returncolor = explode(',', $codes);
  13043. foreach ($returncolor as $key => $val) {
  13044. if (strpos($val, '%') !== false) {
  13045. // percentage
  13046. $returncolor[$key] = (100 * intval($val) / 100);
  13047. } else {
  13048. $returncolor[$key] = intval($val);
  13049. }
  13050. // normalize value
  13051. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  13052. }
  13053. return $returncolor;
  13054. }
  13055. if ($color{0} != '#') {
  13056. // COLOR NAME
  13057. if (isset($this->webcolor[$color])) {
  13058. // web color
  13059. $color_code = $this->webcolor[$color];
  13060. } else {
  13061. // spot color
  13062. $returncolor = $this->getSpotColor($color);
  13063. if ($returncolor === false) {
  13064. $returncolor = $defcol;
  13065. }
  13066. return $returncolor;
  13067. }
  13068. } else {
  13069. $color_code = substr($color, 1);
  13070. }
  13071. // HEXADECIMAL REPRESENTATION
  13072. switch (strlen($color_code)) {
  13073. case 3: {
  13074. // 3-digit RGB hexadecimal representation
  13075. $r = substr($color_code, 0, 1);
  13076. $g = substr($color_code, 1, 1);
  13077. $b = substr($color_code, 2, 1);
  13078. $returncolor = array();
  13079. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  13080. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  13081. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  13082. break;
  13083. }
  13084. case 6: {
  13085. // 6-digit RGB hexadecimal representation
  13086. $returncolor = array();
  13087. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  13088. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  13089. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  13090. break;
  13091. }
  13092. case 8: {
  13093. // 8-digit CMYK hexadecimal representation
  13094. $returncolor = array();
  13095. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  13096. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  13097. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  13098. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  13099. break;
  13100. }
  13101. default: {
  13102. $returncolor = $defcol;
  13103. break;
  13104. }
  13105. }
  13106. return $returncolor;
  13107. }
  13108. /**
  13109. * Converts pixels to User's Units.
  13110. * @param $px (int) pixels
  13111. * @return float value in user's unit
  13112. * @public
  13113. * @see setImageScale(), getImageScale()
  13114. */
  13115. public function pixelsToUnits($px) {
  13116. return ($px / ($this->imgscale * $this->k));
  13117. }
  13118. /**
  13119. * Reverse function for htmlentities.
  13120. * Convert entities in UTF-8.
  13121. * @param $text_to_convert (string) Text to convert.
  13122. * @return string converted text string
  13123. * @public
  13124. */
  13125. public function unhtmlentities($text_to_convert) {
  13126. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  13127. }
  13128. // ENCRYPTION METHODS ----------------------------------
  13129. /**
  13130. * Returns a string containing random data to be used as a seed for encryption methods.
  13131. * @param $seed (string) starting seed value
  13132. * @return string containing random data
  13133. * @author Nicola Asuni
  13134. * @since 5.9.006 (2010-10-19)
  13135. * @protected
  13136. */
  13137. protected function getRandomSeed($seed='') {
  13138. $seed .= microtime();
  13139. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  13140. // this is not used on windows systems because it is very slow for a know bug
  13141. $seed .= openssl_random_pseudo_bytes(512);
  13142. } else {
  13143. for ($i = 0; $i < 23; ++$i) {
  13144. $seed .= uniqid('', true);
  13145. }
  13146. }
  13147. $seed .= uniqid('', true);
  13148. $seed .= rand();
  13149. $seed .= getmypid();
  13150. $seed .= __FILE__;
  13151. $seed .= $this->bufferlen;
  13152. if (isset($_SERVER['REMOTE_ADDR'])) {
  13153. $seed .= $_SERVER['REMOTE_ADDR'];
  13154. }
  13155. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  13156. $seed .= $_SERVER['HTTP_USER_AGENT'];
  13157. }
  13158. if (isset($_SERVER['HTTP_ACCEPT'])) {
  13159. $seed .= $_SERVER['HTTP_ACCEPT'];
  13160. }
  13161. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  13162. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  13163. }
  13164. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  13165. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  13166. }
  13167. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  13168. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  13169. }
  13170. $seed .= rand();
  13171. $seed .= uniqid('', true);
  13172. $seed .= microtime();
  13173. return $seed;
  13174. }
  13175. /**
  13176. * Compute encryption key depending on object number where the encrypted data is stored.
  13177. * This is used for all strings and streams without crypt filter specifier.
  13178. * @param $n (int) object number
  13179. * @return int object key
  13180. * @protected
  13181. * @author Nicola Asuni
  13182. * @since 2.0.000 (2008-01-02)
  13183. */
  13184. protected function _objectkey($n) {
  13185. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  13186. if ($this->encryptdata['mode'] == 2) { // AES-128
  13187. // AES padding
  13188. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  13189. }
  13190. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  13191. $objkey = substr($objkey, 0, 16);
  13192. return $objkey;
  13193. }
  13194. /**
  13195. * Encrypt the input string.
  13196. * @param $n (int) object number
  13197. * @param $s (string) data string to encrypt
  13198. * @return encrypted string
  13199. * @protected
  13200. * @author Nicola Asuni
  13201. * @since 5.0.005 (2010-05-11)
  13202. */
  13203. protected function _encrypt_data($n, $s) {
  13204. if (!$this->encrypted) {
  13205. return $s;
  13206. }
  13207. switch ($this->encryptdata['mode']) {
  13208. case 0: // RC4-40
  13209. case 1: { // RC4-128
  13210. $s = $this->_RC4($this->_objectkey($n), $s);
  13211. break;
  13212. }
  13213. case 2: { // AES-128
  13214. $s = $this->_AES($this->_objectkey($n), $s);
  13215. break;
  13216. }
  13217. case 3: { // AES-256
  13218. $s = $this->_AES($this->encryptdata['key'], $s);
  13219. break;
  13220. }
  13221. }
  13222. return $s;
  13223. }
  13224. /**
  13225. * Put encryption on PDF document.
  13226. * @protected
  13227. * @author Nicola Asuni
  13228. * @since 2.0.000 (2008-01-02)
  13229. */
  13230. protected function _putencryption() {
  13231. if (!$this->encrypted) {
  13232. return;
  13233. }
  13234. $this->encryptdata['objid'] = $this->_newobj();
  13235. $out = '<<';
  13236. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  13237. $this->encryptdata['Filter'] = 'Standard';
  13238. }
  13239. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  13240. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  13241. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  13242. }
  13243. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  13244. $this->encryptdata['V'] = 1;
  13245. }
  13246. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  13247. $out .= ' /V '.$this->encryptdata['V'];
  13248. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  13249. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  13250. $out .= ' /Length '.$this->encryptdata['Length'];
  13251. } else {
  13252. $out .= ' /Length 40';
  13253. }
  13254. if ($this->encryptdata['V'] >= 4) {
  13255. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  13256. $this->encryptdata['StmF'] = 'Identity';
  13257. }
  13258. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  13259. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13260. $this->encryptdata['StrF'] = 'Identity';
  13261. }
  13262. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  13263. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  13264. $out .= ' /CF <<';
  13265. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  13266. $out .= ' /Type /CryptFilter';
  13267. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  13268. // The method used
  13269. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  13270. if ($this->encryptdata['pubkey']) {
  13271. $out .= ' /Recipients [';
  13272. foreach ($this->encryptdata['Recipients'] as $rec) {
  13273. $out .= ' <'.$rec.'>';
  13274. }
  13275. $out .= ' ]';
  13276. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  13277. $out .= ' /EncryptMetadata false';
  13278. } else {
  13279. $out .= ' /EncryptMetadata true';
  13280. }
  13281. }
  13282. } else {
  13283. $out .= ' /CFM /None';
  13284. }
  13285. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  13286. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  13287. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  13288. } else {
  13289. $out .= ' /AuthEvent /DocOpen';
  13290. }
  13291. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  13292. // The bit length of the encryption key.
  13293. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  13294. }
  13295. $out .= ' >> >>';
  13296. }
  13297. // The name of the crypt filter that shall be used by default when decrypting streams.
  13298. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  13299. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13300. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  13301. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  13302. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  13303. $out .= ' /EFF /'.$this->encryptdata[''];
  13304. }
  13305. }
  13306. // Additional encryption dictionary entries for the standard security handler
  13307. if ($this->encryptdata['pubkey']) {
  13308. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  13309. $out .= ' /Recipients [';
  13310. foreach ($this->encryptdata['Recipients'] as $rec) {
  13311. $out .= ' <'.$rec.'>';
  13312. }
  13313. $out .= ' ]';
  13314. }
  13315. } else {
  13316. $out .= ' /R';
  13317. if ($this->encryptdata['V'] == 5) { // AES-256
  13318. $out .= ' 5';
  13319. $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
  13320. $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
  13321. $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
  13322. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  13323. $out .= ' 4';
  13324. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  13325. $out .= ' 2';
  13326. } else { // RC-128
  13327. $out .= ' 3';
  13328. }
  13329. $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
  13330. $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
  13331. $out .= ' /P '.$this->encryptdata['P'];
  13332. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  13333. $out .= ' /EncryptMetadata false';
  13334. } else {
  13335. $out .= ' /EncryptMetadata true';
  13336. }
  13337. }
  13338. $out .= ' >>';
  13339. $out .= "\n".'endobj';
  13340. $this->_out($out);
  13341. }
  13342. /**
  13343. * Returns the input text encrypted using RC4 algorithm and the specified key.
  13344. * RC4 is the standard encryption algorithm used in PDF format
  13345. * @param $key (string) encryption key
  13346. * @param $text (String) input text to be encrypted
  13347. * @return String encrypted text
  13348. * @protected
  13349. * @since 2.0.000 (2008-01-02)
  13350. * @author Klemen Vodopivec, Nicola Asuni
  13351. */
  13352. protected function _RC4($key, $text) {
  13353. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  13354. // try to use mcrypt function if exist
  13355. return $out;
  13356. }
  13357. if ($this->last_enc_key != $key) {
  13358. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  13359. $rc4 = range(0, 255);
  13360. $j = 0;
  13361. for ($i = 0; $i < 256; ++$i) {
  13362. $t = $rc4[$i];
  13363. $j = ($j + $t + ord($k[$i])) % 256;
  13364. $rc4[$i] = $rc4[$j];
  13365. $rc4[$j] = $t;
  13366. }
  13367. $this->last_enc_key = $key;
  13368. $this->last_enc_key_c = $rc4;
  13369. } else {
  13370. $rc4 = $this->last_enc_key_c;
  13371. }
  13372. $len = strlen($text);
  13373. $a = 0;
  13374. $b = 0;
  13375. $out = '';
  13376. for ($i = 0; $i < $len; ++$i) {
  13377. $a = ($a + 1) % 256;
  13378. $t = $rc4[$a];
  13379. $b = ($b + $t) % 256;
  13380. $rc4[$a] = $rc4[$b];
  13381. $rc4[$b] = $t;
  13382. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  13383. $out .= chr(ord($text[$i]) ^ $k);
  13384. }
  13385. return $out;
  13386. }
  13387. /**
  13388. * Returns the input text exrypted using AES algorithm and the specified key.
  13389. * This method requires mcrypt.
  13390. * @param $key (string) encryption key
  13391. * @param $text (String) input text to be encrypted
  13392. * @return String encrypted text
  13393. * @protected
  13394. * @author Nicola Asuni
  13395. * @since 5.0.005 (2010-05-11)
  13396. */
  13397. protected function _AES($key, $text) {
  13398. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  13399. $padding = 16 - (strlen($text) % 16);
  13400. $text .= str_repeat(chr($padding), $padding);
  13401. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  13402. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  13403. $text = $iv.$text;
  13404. return $text;
  13405. }
  13406. /**
  13407. * Encrypts a string using MD5 and returns it's value as a binary string.
  13408. * @param $str (string) input string
  13409. * @return String MD5 encrypted binary string
  13410. * @protected
  13411. * @since 2.0.000 (2008-01-02)
  13412. * @author Klemen Vodopivec
  13413. */
  13414. protected function _md5_16($str) {
  13415. return pack('H*', md5($str));
  13416. }
  13417. /**
  13418. * Compute U value (used for encryption)
  13419. * @return string U value
  13420. * @protected
  13421. * @since 2.0.000 (2008-01-02)
  13422. * @author Nicola Asuni
  13423. */
  13424. protected function _Uvalue() {
  13425. if ($this->encryptdata['mode'] == 0) { // RC4-40
  13426. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  13427. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  13428. $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
  13429. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  13430. $len = strlen($tmp);
  13431. for ($i = 1; $i <= 19; ++$i) {
  13432. $ek = '';
  13433. for ($j = 0; $j < $len; ++$j) {
  13434. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  13435. }
  13436. $enc = $this->_RC4($ek, $enc);
  13437. }
  13438. $enc .= str_repeat("\x00", 16);
  13439. return substr($enc, 0, 32);
  13440. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13441. $seed = $this->_md5_16($this->getRandomSeed());
  13442. // User Validation Salt
  13443. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  13444. // User Key Salt
  13445. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  13446. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  13447. }
  13448. }
  13449. /**
  13450. * Compute UE value (used for encryption)
  13451. * @return string UE value
  13452. * @protected
  13453. * @since 5.9.006 (2010-10-19)
  13454. * @author Nicola Asuni
  13455. */
  13456. protected function _UEvalue() {
  13457. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  13458. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13459. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13460. }
  13461. /**
  13462. * Compute O value (used for encryption)
  13463. * @return string O value
  13464. * @protected
  13465. * @since 2.0.000 (2008-01-02)
  13466. * @author Nicola Asuni
  13467. */
  13468. pr