PageRenderTime 158ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 3ms

/lib/tcpdf/tcpdf.php

https://bitbucket.org/ngmares/moodle
PHP | 13977 lines | 8814 code | 495 blank | 4668 comment | 1678 complexity | 3b3fe3a35d21fce7c11f9e832e3cea9a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.156
  5. // Begin : 2002-08-03
  6. // Last Update : 2012-04-10
  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.156
  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.156
  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.156';
  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-time
  1572. * @protected
  1573. * @since 5.9.152 (2012-03-22)
  1574. */
  1575. protected $doc_creation_timestamp;
  1576. /**
  1577. * Document modification date-time
  1578. * @protected
  1579. * @since 5.9.152 (2012-03-22)
  1580. */
  1581. protected $doc_modification_timestamp;
  1582. /**
  1583. * Custom XMP data.
  1584. * @protected
  1585. * @since 5.9.128 (2011-10-06)
  1586. */
  1587. protected $custom_xmp = '';
  1588. /**
  1589. * Overprint mode array.
  1590. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1591. * @protected
  1592. * @since 5.9.152 (2012-03-23)
  1593. */
  1594. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1595. /**
  1596. * Alpha mode array.
  1597. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1598. * @protected
  1599. * @since 5.9.152 (2012-03-23)
  1600. */
  1601. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1602. /**
  1603. * Define the page boundaries boxes to be set on document.
  1604. * @protected
  1605. * @since 5.9.152 (2012-03-23)
  1606. */
  1607. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1608. /**
  1609. * Set the document producer metadata.
  1610. * @protected
  1611. * @since 5.9.152 (2012-03-23)
  1612. */
  1613. protected $pdfproducer;
  1614. /**
  1615. * If true print TCPDF meta link.
  1616. * @protected
  1617. * @since 5.9.152 (2012-03-23)
  1618. */
  1619. protected $tcpdflink = true;
  1620. //------------------------------------------------------------
  1621. // METHODS
  1622. //------------------------------------------------------------
  1623. /**
  1624. * This is the class constructor.
  1625. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1626. * @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>
  1627. * @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.
  1628. * @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().
  1629. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1630. * @param $encoding (string) Charset encoding; default is UTF-8.
  1631. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1632. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1633. * @public
  1634. * @see getPageSizeFromFormat(), setPageFormat()
  1635. */
  1636. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1637. /* Set internal character encoding to ASCII */
  1638. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1639. $this->internal_encoding = mb_internal_encoding();
  1640. mb_internal_encoding('ASCII');
  1641. }
  1642. // get array of HTML colors
  1643. require(dirname(__FILE__).'/htmlcolors.php');
  1644. $this->webcolor = $webcolor;
  1645. // get array of custom spot colors
  1646. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1647. require(dirname(__FILE__).'/spotcolors.php');
  1648. $this->spotcolor = $spotcolor;
  1649. } else {
  1650. $this->spotcolor = array();
  1651. }
  1652. require_once(dirname(__FILE__).'/unicode_data.php');
  1653. $this->unicode = new TCPDF_UNICODE_DATA();
  1654. require_once(dirname(__FILE__).'/encodings_maps.php');
  1655. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1656. $this->font_obj_ids = array();
  1657. $this->page_obj_id = array();
  1658. $this->form_obj_id = array();
  1659. // set pdf/a mode
  1660. $this->pdfa_mode = $pdfa;
  1661. $this->force_srgb = false;
  1662. // set disk caching
  1663. $this->diskcache = $diskcache ? true : false;
  1664. // set language direction
  1665. $this->rtl = false;
  1666. $this->tmprtl = false;
  1667. // some checks
  1668. $this->_dochecks();
  1669. // initialization of properties
  1670. $this->isunicode = $unicode;
  1671. $this->page = 0;
  1672. $this->transfmrk[0] = array();
  1673. $this->pagedim = array();
  1674. $this->n = 2;
  1675. $this->buffer = '';
  1676. $this->pages = array();
  1677. $this->state = 0;
  1678. $this->fonts = array();
  1679. $this->FontFiles = array();
  1680. $this->diffs = array();
  1681. $this->images = array();
  1682. $this->links = array();
  1683. $this->gradients = array();
  1684. $this->InFooter = false;
  1685. $this->lasth = 0;
  1686. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1687. $this->FontStyle = '';
  1688. $this->FontSizePt = 12;
  1689. $this->underline = false;
  1690. $this->overline = false;
  1691. $this->linethrough = false;
  1692. $this->DrawColor = '0 G';
  1693. $this->FillColor = '0 g';
  1694. $this->TextColor = '0 g';
  1695. $this->ColorFlag = false;
  1696. $this->pdflayers = array();
  1697. // encryption values
  1698. $this->encrypted = false;
  1699. $this->last_enc_key = '';
  1700. // standard Unicode fonts
  1701. $this->CoreFonts = array(
  1702. 'courier'=>'Courier',
  1703. 'courierB'=>'Courier-Bold',
  1704. 'courierI'=>'Courier-Oblique',
  1705. 'courierBI'=>'Courier-BoldOblique',
  1706. 'helvetica'=>'Helvetica',
  1707. 'helveticaB'=>'Helvetica-Bold',
  1708. 'helveticaI'=>'Helvetica-Oblique',
  1709. 'helveticaBI'=>'Helvetica-BoldOblique',
  1710. 'times'=>'Times-Roman',
  1711. 'timesB'=>'Times-Bold',
  1712. 'timesI'=>'Times-Italic',
  1713. 'timesBI'=>'Times-BoldItalic',
  1714. 'symbol'=>'Symbol',
  1715. 'zapfdingbats'=>'ZapfDingbats'
  1716. );
  1717. // set scale factor
  1718. $this->setPageUnit($unit);
  1719. // set page format and orientation
  1720. $this->setPageFormat($format, $orientation);
  1721. // page margins (1 cm)
  1722. $margin = 28.35 / $this->k;
  1723. $this->SetMargins($margin, $margin);
  1724. // internal cell padding
  1725. $cpadding = $margin / 10;
  1726. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1727. // cell margins
  1728. $this->setCellMargins(0, 0, 0, 0);
  1729. // line width (0.2 mm)
  1730. $this->LineWidth = 0.57 / $this->k;
  1731. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1732. $this->linestyleCap = '0 J';
  1733. $this->linestyleJoin = '0 j';
  1734. $this->linestyleDash = '[] 0 d';
  1735. // automatic page break
  1736. $this->SetAutoPageBreak(true, (2 * $margin));
  1737. // full width display mode
  1738. $this->SetDisplayMode('fullwidth');
  1739. // compression
  1740. $this->SetCompression();
  1741. // set default PDF version number
  1742. $this->setPDFVersion();
  1743. $this->pdfproducer = "\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";
  1744. $this->tcpdflink = true;
  1745. $this->encoding = $encoding;
  1746. $this->HREF = array();
  1747. $this->getFontsList();
  1748. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1749. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1750. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1751. $this->extgstates = array();
  1752. // user's rights
  1753. $this->sign = false;
  1754. $this->ur['enabled'] = false;
  1755. $this->ur['document'] = '/FullSave';
  1756. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1757. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1758. $this->ur['signature'] = '/Modify';
  1759. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1760. $this->ur['formex'] = '';
  1761. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1762. $this->empty_signature_appearance = array();
  1763. // set default JPEG quality
  1764. $this->jpeg_quality = 75;
  1765. // initialize some settings
  1766. $this->utf8Bidi(array(''), '');
  1767. // set default font
  1768. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1769. // check if PCRE Unicode support is enabled
  1770. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1771. // PCRE unicode support is turned ON
  1772. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1773. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1774. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1775. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1776. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1777. } else {
  1778. // PCRE unicode support is turned OFF
  1779. $this->setSpacesRE('/[^\S\xa0]/');
  1780. }
  1781. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1782. // set file ID for trailer
  1783. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1784. // set document creation and modification timestamp
  1785. $this->doc_creation_timestamp = time();
  1786. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1787. // get default graphic vars
  1788. $this->default_graphic_vars = $this->getGraphicVars();
  1789. $this->header_xobj_autoreset = false;
  1790. $this->custom_xmp = '';
  1791. }
  1792. /**
  1793. * Default destructor.
  1794. * @public
  1795. * @since 1.53.0.TC016
  1796. */
  1797. public function __destruct() {
  1798. // restore internal encoding
  1799. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1800. mb_internal_encoding($this->internal_encoding);
  1801. }
  1802. // unset all class variables
  1803. $this->_destroy(true);
  1804. }
  1805. /**
  1806. * Return the current TCPDF version.
  1807. * @return TCPDF version string
  1808. * @public
  1809. * @since 5.9.012 (2010-11-10)
  1810. */
  1811. public function getTCPDFVersion() {
  1812. return $this->tcpdf_version;
  1813. }
  1814. /**
  1815. * Set the units of measure for the document.
  1816. * @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.
  1817. * @public
  1818. * @since 3.0.015 (2008-06-06)
  1819. */
  1820. public function setPageUnit($unit) {
  1821. $unit = strtolower($unit);
  1822. //Set scale factor
  1823. switch ($unit) {
  1824. // points
  1825. case 'px':
  1826. case 'pt': {
  1827. $this->k = 1;
  1828. break;
  1829. }
  1830. // millimeters
  1831. case 'mm': {
  1832. $this->k = $this->dpi / 25.4;
  1833. break;
  1834. }
  1835. // centimeters
  1836. case 'cm': {
  1837. $this->k = $this->dpi / 2.54;
  1838. break;
  1839. }
  1840. // inches
  1841. case 'in': {
  1842. $this->k = $this->dpi;
  1843. break;
  1844. }
  1845. // unsupported unit
  1846. default : {
  1847. $this->Error('Incorrect unit: '.$unit);
  1848. break;
  1849. }
  1850. }
  1851. $this->pdfunit = $unit;
  1852. if (isset($this->CurOrientation)) {
  1853. $this->setPageOrientation($this->CurOrientation);
  1854. }
  1855. }
  1856. /**
  1857. * Get page dimensions from format name.
  1858. * @param $format (mixed) The format name. It can be: <ul>
  1859. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1860. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1861. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1862. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1863. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1864. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1865. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1866. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1867. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1868. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1869. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1870. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1871. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1872. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1873. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1874. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1875. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1876. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1877. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1878. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1879. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1880. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1881. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1882. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1883. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1884. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1885. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1886. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1887. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1888. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1889. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1890. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1891. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1892. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1893. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1894. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1895. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1896. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1897. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1898. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1899. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1900. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1901. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1902. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1903. * <li><b>SIS 014711 E Series</b></li>
  1904. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1905. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1906. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1907. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1908. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1909. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1910. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1911. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1912. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1913. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1914. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1915. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1916. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1917. * <li><b>SIS 014711 G Series</b></li>
  1918. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1919. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1920. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1921. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1922. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1923. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1924. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1925. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1926. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1927. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1928. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1929. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1930. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1931. * <li><b>ISO Press</b></li>
  1932. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1933. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1934. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1935. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1936. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1937. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1938. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1939. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1940. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1941. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1942. * <li><b>German DIN 476</b></li>
  1943. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1944. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1945. * <li><b>Variations on the ISO Standard</b></li>
  1946. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1947. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1948. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1949. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1950. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1951. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1952. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1953. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1954. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1955. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1956. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1957. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1958. * <li><b>ANSI Series</b></li>
  1959. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1960. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1961. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1962. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1963. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1964. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1965. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1966. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1967. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1968. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1969. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1970. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1971. * <li><b>Other North American Paper Sizes</b></li>
  1972. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1973. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1974. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1975. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1976. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1977. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1978. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1979. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1980. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1981. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1982. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1983. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1984. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1985. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1986. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1987. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1988. * <li><b>North American Architectural Sizes</b></li>
  1989. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1990. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1991. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1992. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1993. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1994. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1995. * <li><b>Announcement Envelopes</b></li>
  1996. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1997. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1998. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1999. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  2000. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  2001. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  2002. * <li><b>Commercial Envelopes</b></li>
  2003. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  2004. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  2005. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  2006. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  2007. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  2008. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  2009. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  2010. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  2011. * <li><b>Catalogue Envelopes</b></li>
  2012. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  2013. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  2014. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  2015. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  2016. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  2017. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  2018. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  2019. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  2020. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  2021. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  2022. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  2023. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  2024. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  2025. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  2026. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  2027. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  2028. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  2029. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  2030. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  2031. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  2032. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  2033. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  2034. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  2035. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  2036. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  2037. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  2038. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  2039. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  2040. * <li><b>PA Series</b></li>
  2041. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2042. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2043. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2044. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2045. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2046. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2047. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2048. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2049. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2050. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2051. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2052. * <li><b>Standard Photographic Print Sizes</b></li>
  2053. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2054. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2055. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2056. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2057. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2058. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2059. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2060. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2061. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2062. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2063. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2064. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2065. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2066. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2067. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2068. * <li><b>Common Newspaper Sizes</b></li>
  2069. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2070. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2071. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2072. * <li><b>Business Cards</b></li>
  2073. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2074. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2075. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2076. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2077. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2078. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2079. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2080. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2081. * <li><b>Billboards</b></li>
  2082. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2083. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2084. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2085. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2086. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2087. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2088. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2089. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2090. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2091. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2092. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2093. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2094. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2095. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2096. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2097. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2098. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2099. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2100. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2101. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2102. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2103. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2104. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2105. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2106. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2107. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2108. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2109. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2110. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2111. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2112. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2113. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2114. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2115. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2116. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2117. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2118. * <li><b>Old Imperial Belgian</b></li>
  2119. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2120. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2121. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2122. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2123. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2124. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2125. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2126. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2127. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2128. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2129. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2130. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2131. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2132. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2133. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2134. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2135. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2136. * <li><b>Old Imperial French</b></li>
  2137. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2138. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2139. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2140. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2141. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2142. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2143. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2144. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2145. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2146. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2147. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2148. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2149. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2150. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2151. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2152. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2153. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2154. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2155. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2156. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2157. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2158. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2159. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2160. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2161. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2162. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2163. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2164. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2165. * </ul>
  2166. * @return array containing page width and height in points
  2167. * @public
  2168. * @since 5.0.010 (2010-05-17)
  2169. */
  2170. public function getPageSizeFromFormat($format) {
  2171. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2172. switch (strtoupper($format)) {
  2173. // ISO 216 A Series + 2 SIS 014711 extensions
  2174. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2175. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2176. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2177. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2178. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2179. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2180. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2181. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2182. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2183. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2184. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2185. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2186. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2187. // ISO 216 B Series + 2 SIS 014711 extensions
  2188. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2189. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2190. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2191. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2192. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2193. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2194. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2195. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2196. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2197. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2198. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2199. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2200. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2201. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2202. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2203. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2204. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2205. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2206. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2207. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2208. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2209. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2210. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2211. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2212. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2213. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2214. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2215. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2216. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2217. // SIS 014711 E Series
  2218. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2219. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2220. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2221. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2222. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2223. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2224. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2225. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2226. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2227. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2228. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2229. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2230. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2231. // SIS 014711 G Series
  2232. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2233. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2234. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2235. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2236. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2237. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2238. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2239. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2240. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2241. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2242. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2243. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2244. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2245. // ISO Press
  2246. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2247. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2248. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2249. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2250. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2251. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2252. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2253. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2254. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2255. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2256. // German DIN 476
  2257. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2258. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2259. // Variations on the ISO Standard
  2260. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2261. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2262. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2263. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2264. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2265. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2266. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2267. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2268. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2269. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2270. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2271. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2272. // ANSI Series
  2273. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2274. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2275. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2276. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2277. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2278. // Traditional 'Loose' North American Paper Sizes
  2279. case 'USLEDGER':
  2280. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2281. case 'ORGANIZERK':
  2282. case 'BIBLE':
  2283. case 'USTABLOID':
  2284. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2285. case 'ORGANIZERM':
  2286. case 'USLETTER':
  2287. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2288. case 'USLEGAL':
  2289. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2290. case 'GOVERNMENTLETTER':
  2291. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2292. case 'JUNIORLEGAL':
  2293. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2294. // Other North American Paper Sizes
  2295. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2296. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2297. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2298. case 'GOVERNMENTLEGAL':
  2299. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2300. case 'MONARCH':
  2301. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2302. case 'ORGANIZERL':
  2303. case 'STATEMENT':
  2304. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2305. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2306. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2307. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2308. // Canadian standard CAN 2-9.60M
  2309. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2310. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2311. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2312. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2313. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2314. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2315. // North American Architectural Sizes
  2316. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2317. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2318. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2319. case 'BROADSHEET':
  2320. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2321. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2322. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2323. // --- North American Envelope Sizes ---
  2324. // - Announcement Envelopes
  2325. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2326. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2327. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2328. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2329. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2330. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2331. // - Commercial Envelopes
  2332. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2333. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2334. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2335. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2336. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2337. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2338. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2339. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2340. // - Catalogue Envelopes
  2341. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2342. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2343. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2344. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2345. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2346. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2347. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2348. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2349. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2350. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2351. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2352. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2353. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2354. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2355. // Japanese (JIS P 0138-61) Standard B-Series
  2356. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2357. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2358. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2359. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2360. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2361. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2362. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2363. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2364. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2365. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2366. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2367. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2368. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2369. // PA Series
  2370. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2371. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2372. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2373. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2374. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2375. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2376. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2377. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2378. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2379. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2380. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2381. // Standard Photographic Print Sizes
  2382. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2383. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2384. case 'L':
  2385. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2386. case 'KG':
  2387. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2388. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2389. case '2L':
  2390. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2391. case '8P':
  2392. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2393. case '6P':
  2394. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2395. case '6PW':
  2396. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2397. case '4P':
  2398. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2399. case '4PW':
  2400. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2401. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2402. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2403. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2404. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2405. // Common Newspaper Sizes
  2406. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2407. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2408. case 'NEWSPAPER_TABLOID':
  2409. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2410. // Business Cards
  2411. case 'CREDIT_CARD':
  2412. case 'BUSINESS_CARD':
  2413. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2414. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2415. case 'BUSINESS_CARD_IT':
  2416. case 'BUSINESS_CARD_UK':
  2417. case 'BUSINESS_CARD_FR':
  2418. case 'BUSINESS_CARD_DE':
  2419. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2420. case 'BUSINESS_CARD_CA':
  2421. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2422. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2423. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2424. case 'BUSINESS_CARD_AU':
  2425. case 'BUSINESS_CARD_DK':
  2426. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2427. case 'BUSINESS_CARD_RU':
  2428. case 'BUSINESS_CARD_CZ':
  2429. case 'BUSINESS_CARD_FI':
  2430. case 'BUSINESS_CARD_HU':
  2431. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2432. // Billboards
  2433. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2434. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2435. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2436. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2437. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2438. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2439. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2440. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2441. // Old European Sizes
  2442. // - Old Imperial English Sizes
  2443. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2444. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2445. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2446. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2447. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2448. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2449. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2450. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2451. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2452. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2453. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2454. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2455. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2456. case 'EN_SHEET':
  2457. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2458. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2459. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2460. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2461. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2462. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2463. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2464. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2465. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2466. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2467. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2468. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2469. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2470. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2471. // - Old Imperial Belgian Sizes
  2472. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2473. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2474. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2475. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2476. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2477. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2478. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2479. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2480. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2481. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2482. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2483. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2484. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2485. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2486. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2487. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2488. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2489. // - Old Imperial French Sizes
  2490. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2491. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2492. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2493. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2494. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2495. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2496. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2497. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2498. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2499. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2500. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2501. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2502. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2503. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2504. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2505. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2506. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2507. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2508. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2509. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2510. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2511. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2512. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2513. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2514. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2515. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2516. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2517. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2518. // DEFAULT ISO A4
  2519. default: {$pf = array( 595.276, 841.890); break;}
  2520. }
  2521. return $pf;
  2522. }
  2523. /**
  2524. * Change the format of the current page
  2525. * @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>
  2526. * <li>['format'] = page format name (one of the above);</li>
  2527. * <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>
  2528. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2529. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2530. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2531. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2532. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2533. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2534. * <li>['CropBox'] : the visible region of default user space:</li>
  2535. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2536. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2537. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2538. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2539. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2540. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2541. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2542. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2543. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2544. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2545. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2546. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2547. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2548. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2549. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2550. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2551. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2552. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2553. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2554. * <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>
  2555. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2556. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2557. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2558. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2559. * <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>
  2560. * <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>
  2561. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2562. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2563. * <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>
  2564. * <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>
  2565. * <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>
  2566. * <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>
  2567. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2568. * </ul>
  2569. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2570. * <li>P or Portrait (default)</li>
  2571. * <li>L or Landscape</li>
  2572. * <li>'' (empty string) for automatic orientation</li>
  2573. * </ul>
  2574. * @protected
  2575. * @since 3.0.015 (2008-06-06)
  2576. * @see getPageSizeFromFormat()
  2577. */
  2578. protected function setPageFormat($format, $orientation='P') {
  2579. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2580. // remove inherited values
  2581. unset($this->pagedim[$this->page]);
  2582. }
  2583. if (is_string($format)) {
  2584. // get page measures from format name
  2585. $pf = $this->getPageSizeFromFormat($format);
  2586. $this->fwPt = $pf[0];
  2587. $this->fhPt = $pf[1];
  2588. } else {
  2589. // the boundaries of the physical medium on which the page shall be displayed or printed
  2590. if (isset($format['MediaBox'])) {
  2591. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2592. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2593. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2594. } else {
  2595. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2596. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2597. } else {
  2598. if (!isset($format['format'])) {
  2599. // default value
  2600. $format['format'] = 'A4';
  2601. }
  2602. $pf = $this->getPageSizeFromFormat($format['format']);
  2603. }
  2604. $this->fwPt = $pf[0];
  2605. $this->fhPt = $pf[1];
  2606. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2607. }
  2608. // the visible region of default user space
  2609. if (isset($format['CropBox'])) {
  2610. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2611. }
  2612. // the region to which the contents of the page shall be clipped when output in a production environment
  2613. if (isset($format['BleedBox'])) {
  2614. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2615. }
  2616. // the intended dimensions of the finished page after trimming
  2617. if (isset($format['TrimBox'])) {
  2618. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2619. }
  2620. // the page's meaningful content (including potential white space)
  2621. if (isset($format['ArtBox'])) {
  2622. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2623. }
  2624. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2625. if (isset($format['BoxColorInfo'])) {
  2626. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2627. }
  2628. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2629. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2630. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2631. }
  2632. if (isset($format['PZ'])) {
  2633. // The page's preferred zoom (magnification) factor
  2634. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2635. }
  2636. if (isset($format['trans'])) {
  2637. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2638. if (isset($format['trans']['Dur'])) {
  2639. // The page's display duration
  2640. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2641. }
  2642. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2643. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2644. // The transition style that shall be used when moving to this page from another during a presentation
  2645. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2646. $valid_effect = array('Split', 'Blinds');
  2647. $valid_vals = array('H', 'V');
  2648. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2649. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2650. }
  2651. $valid_effect = array('Split', 'Box', 'Fly');
  2652. $valid_vals = array('I', 'O');
  2653. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2654. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2655. }
  2656. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2657. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2658. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2659. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2660. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2661. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2662. }
  2663. }
  2664. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2665. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2666. }
  2667. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2668. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2669. }
  2670. } else {
  2671. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2672. }
  2673. if (isset($format['trans']['D'])) {
  2674. // The duration of the transition effect, in seconds
  2675. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2676. } else {
  2677. $this->pagedim[$this->page]['trans']['D'] = 1;
  2678. }
  2679. }
  2680. }
  2681. $this->setPageOrientation($orientation);
  2682. }
  2683. /**
  2684. * Set page boundaries.
  2685. * @param $page (int) page number
  2686. * @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>
  2687. * @param $llx (float) lower-left x coordinate in user units
  2688. * @param $lly (float) lower-left y coordinate in user units
  2689. * @param $urx (float) upper-right x coordinate in user units
  2690. * @param $ury (float) upper-right y coordinate in user units
  2691. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2692. * @public
  2693. * @since 5.0.010 (2010-05-17)
  2694. */
  2695. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2696. if (!isset($this->pagedim[$page])) {
  2697. // initialize array
  2698. $this->pagedim[$page] = array();
  2699. }
  2700. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2701. if (!in_array($type, $pageboxes)) {
  2702. return;
  2703. }
  2704. if ($points) {
  2705. $k = 1;
  2706. } else {
  2707. $k = $this->k;
  2708. }
  2709. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2710. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2711. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2712. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2713. }
  2714. /**
  2715. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2716. * @param $page (int) page number
  2717. * @protected
  2718. * @since 5.0.010 (2010-05-17)
  2719. */
  2720. protected function swapPageBoxCoordinates($page) {
  2721. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2722. foreach ($pageboxes as $type) {
  2723. // swap X and Y coordinates
  2724. if (isset($this->pagedim[$page][$type])) {
  2725. $tmp = $this->pagedim[$page][$type]['llx'];
  2726. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2727. $this->pagedim[$page][$type]['lly'] = $tmp;
  2728. $tmp = $this->pagedim[$page][$type]['urx'];
  2729. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2730. $this->pagedim[$page][$type]['ury'] = $tmp;
  2731. }
  2732. }
  2733. }
  2734. /**
  2735. * Set page orientation.
  2736. * @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>
  2737. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2738. * @param $bottommargin (float) bottom margin of the page.
  2739. * @public
  2740. * @since 3.0.015 (2008-06-06)
  2741. */
  2742. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2743. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2744. // the boundaries of the physical medium on which the page shall be displayed or printed
  2745. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2746. }
  2747. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2748. // the visible region of default user space
  2749. $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);
  2750. }
  2751. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2752. // the region to which the contents of the page shall be clipped when output in a production environment
  2753. $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);
  2754. }
  2755. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2756. // the intended dimensions of the finished page after trimming
  2757. $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);
  2758. }
  2759. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2760. // the page's meaningful content (including potential white space)
  2761. $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);
  2762. }
  2763. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2764. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2765. $this->pagedim[$this->page]['Rotate'] = 0;
  2766. }
  2767. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2768. // The page's preferred zoom (magnification) factor
  2769. $this->pagedim[$this->page]['PZ'] = 1;
  2770. }
  2771. if ($this->fwPt > $this->fhPt) {
  2772. // landscape
  2773. $default_orientation = 'L';
  2774. } else {
  2775. // portrait
  2776. $default_orientation = 'P';
  2777. }
  2778. $valid_orientations = array('P', 'L');
  2779. if (empty($orientation)) {
  2780. $orientation = $default_orientation;
  2781. } else {
  2782. $orientation = strtoupper($orientation{0});
  2783. }
  2784. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2785. $this->CurOrientation = $orientation;
  2786. $this->wPt = $this->fhPt;
  2787. $this->hPt = $this->fwPt;
  2788. } else {
  2789. $this->CurOrientation = $default_orientation;
  2790. $this->wPt = $this->fwPt;
  2791. $this->hPt = $this->fhPt;
  2792. }
  2793. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2794. // swap X and Y coordinates (change page orientation)
  2795. $this->swapPageBoxCoordinates($this->page);
  2796. }
  2797. $this->w = $this->wPt / $this->k;
  2798. $this->h = $this->hPt / $this->k;
  2799. if ($this->empty_string($autopagebreak)) {
  2800. if (isset($this->AutoPageBreak)) {
  2801. $autopagebreak = $this->AutoPageBreak;
  2802. } else {
  2803. $autopagebreak = true;
  2804. }
  2805. }
  2806. if ($this->empty_string($bottommargin)) {
  2807. if (isset($this->bMargin)) {
  2808. $bottommargin = $this->bMargin;
  2809. } else {
  2810. // default value = 2 cm
  2811. $bottommargin = 2 * 28.35 / $this->k;
  2812. }
  2813. }
  2814. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2815. // store page dimensions
  2816. $this->pagedim[$this->page]['w'] = $this->wPt;
  2817. $this->pagedim[$this->page]['h'] = $this->hPt;
  2818. $this->pagedim[$this->page]['wk'] = $this->w;
  2819. $this->pagedim[$this->page]['hk'] = $this->h;
  2820. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2821. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2822. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2823. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2824. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2825. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2826. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2827. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2828. }
  2829. /**
  2830. * Set regular expression to detect withespaces or word separators.
  2831. * The pattern delimiter must be the forward-slash character "/".
  2832. * Some example patterns are:
  2833. * <pre>
  2834. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2835. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2836. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2837. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2838. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2839. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2840. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2841. * </pre>
  2842. * @param $re (string) regular expression (leave empty for default).
  2843. * @public
  2844. * @since 4.6.016 (2009-06-15)
  2845. */
  2846. public function setSpacesRE($re='/[^\S\xa0]/') {
  2847. $this->re_spaces = $re;
  2848. $re_parts = explode('/', $re);
  2849. // get pattern parts
  2850. $this->re_space = array();
  2851. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2852. $this->re_space['p'] = $re_parts[1];
  2853. } else {
  2854. $this->re_space['p'] = '[\s]';
  2855. }
  2856. // set pattern modifiers
  2857. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2858. $this->re_space['m'] = $re_parts[2];
  2859. } else {
  2860. $this->re_space['m'] = '';
  2861. }
  2862. }
  2863. /**
  2864. * Enable or disable Right-To-Left language mode
  2865. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2866. * @param $resetx (Boolean) if true reset the X position on direction change.
  2867. * @public
  2868. * @since 2.0.000 (2008-01-03)
  2869. */
  2870. public function setRTL($enable, $resetx=true) {
  2871. $enable = $enable ? true : false;
  2872. $resetx = ($resetx AND ($enable != $this->rtl));
  2873. $this->rtl = $enable;
  2874. $this->tmprtl = false;
  2875. if ($resetx) {
  2876. $this->Ln(0);
  2877. }
  2878. }
  2879. /**
  2880. * Return the RTL status
  2881. * @return boolean
  2882. * @public
  2883. * @since 4.0.012 (2008-07-24)
  2884. */
  2885. public function getRTL() {
  2886. return $this->rtl;
  2887. }
  2888. /**
  2889. * Force temporary RTL language direction
  2890. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2891. * @public
  2892. * @since 2.1.000 (2008-01-09)
  2893. */
  2894. public function setTempRTL($mode) {
  2895. $newmode = false;
  2896. switch (strtoupper($mode)) {
  2897. case 'LTR':
  2898. case 'L': {
  2899. if ($this->rtl) {
  2900. $newmode = 'L';
  2901. }
  2902. break;
  2903. }
  2904. case 'RTL':
  2905. case 'R': {
  2906. if (!$this->rtl) {
  2907. $newmode = 'R';
  2908. }
  2909. break;
  2910. }
  2911. case false:
  2912. default: {
  2913. $newmode = false;
  2914. break;
  2915. }
  2916. }
  2917. $this->tmprtl = $newmode;
  2918. }
  2919. /**
  2920. * Return the current temporary RTL status
  2921. * @return boolean
  2922. * @public
  2923. * @since 4.8.014 (2009-11-04)
  2924. */
  2925. public function isRTLTextDir() {
  2926. return ($this->rtl OR ($this->tmprtl == 'R'));
  2927. }
  2928. /**
  2929. * Set the last cell height.
  2930. * @param $h (float) cell height.
  2931. * @author Nicola Asuni
  2932. * @public
  2933. * @since 1.53.0.TC034
  2934. */
  2935. public function setLastH($h) {
  2936. $this->lasth = $h;
  2937. }
  2938. /**
  2939. * Reset the last cell height.
  2940. * @public
  2941. * @since 5.9.000 (2010-10-03)
  2942. */
  2943. public function resetLastH() {
  2944. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2945. }
  2946. /**
  2947. * Get the last cell height.
  2948. * @return last cell height
  2949. * @public
  2950. * @since 4.0.017 (2008-08-05)
  2951. */
  2952. public function getLastH() {
  2953. return $this->lasth;
  2954. }
  2955. /**
  2956. * Set the adjusting factor to convert pixels to user units.
  2957. * @param $scale (float) adjusting factor to convert pixels to user units.
  2958. * @author Nicola Asuni
  2959. * @public
  2960. * @since 1.5.2
  2961. */
  2962. public function setImageScale($scale) {
  2963. $this->imgscale = $scale;
  2964. }
  2965. /**
  2966. * Returns the adjusting factor to convert pixels to user units.
  2967. * @return float adjusting factor to convert pixels to user units.
  2968. * @author Nicola Asuni
  2969. * @public
  2970. * @since 1.5.2
  2971. */
  2972. public function getImageScale() {
  2973. return $this->imgscale;
  2974. }
  2975. /**
  2976. * Returns an array of page dimensions:
  2977. * <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>
  2978. * @param $pagenum (int) page number (empty = current page)
  2979. * @return array of page dimensions.
  2980. * @author Nicola Asuni
  2981. * @public
  2982. * @since 4.5.027 (2009-03-16)
  2983. */
  2984. public function getPageDimensions($pagenum='') {
  2985. if (empty($pagenum)) {
  2986. $pagenum = $this->page;
  2987. }
  2988. return $this->pagedim[$pagenum];
  2989. }
  2990. /**
  2991. * Returns the page width in units.
  2992. * @param $pagenum (int) page number (empty = current page)
  2993. * @return int page width.
  2994. * @author Nicola Asuni
  2995. * @public
  2996. * @since 1.5.2
  2997. * @see getPageDimensions()
  2998. */
  2999. public function getPageWidth($pagenum='') {
  3000. if (empty($pagenum)) {
  3001. return $this->w;
  3002. }
  3003. return $this->pagedim[$pagenum]['w'];
  3004. }
  3005. /**
  3006. * Returns the page height in units.
  3007. * @param $pagenum (int) page number (empty = current page)
  3008. * @return int page height.
  3009. * @author Nicola Asuni
  3010. * @public
  3011. * @since 1.5.2
  3012. * @see getPageDimensions()
  3013. */
  3014. public function getPageHeight($pagenum='') {
  3015. if (empty($pagenum)) {
  3016. return $this->h;
  3017. }
  3018. return $this->pagedim[$pagenum]['h'];
  3019. }
  3020. /**
  3021. * Returns the page break margin.
  3022. * @param $pagenum (int) page number (empty = current page)
  3023. * @return int page break margin.
  3024. * @author Nicola Asuni
  3025. * @public
  3026. * @since 1.5.2
  3027. * @see getPageDimensions()
  3028. */
  3029. public function getBreakMargin($pagenum='') {
  3030. if (empty($pagenum)) {
  3031. return $this->bMargin;
  3032. }
  3033. return $this->pagedim[$pagenum]['bm'];
  3034. }
  3035. /**
  3036. * Returns the scale factor (number of points in user unit).
  3037. * @return int scale factor.
  3038. * @author Nicola Asuni
  3039. * @public
  3040. * @since 1.5.2
  3041. */
  3042. public function getScaleFactor() {
  3043. return $this->k;
  3044. }
  3045. /**
  3046. * Defines the left, top and right margins.
  3047. * @param $left (float) Left margin.
  3048. * @param $top (float) Top margin.
  3049. * @param $right (float) Right margin. Default value is the left one.
  3050. * @param $keepmargins (boolean) if true overwrites the default page margins
  3051. * @public
  3052. * @since 1.0
  3053. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3054. */
  3055. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3056. //Set left, top and right margins
  3057. $this->lMargin = $left;
  3058. $this->tMargin = $top;
  3059. if ($right == -1) {
  3060. $right = $left;
  3061. }
  3062. $this->rMargin = $right;
  3063. if ($keepmargins) {
  3064. // overwrite original values
  3065. $this->original_lMargin = $this->lMargin;
  3066. $this->original_rMargin = $this->rMargin;
  3067. }
  3068. }
  3069. /**
  3070. * 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.
  3071. * @param $margin (float) The margin.
  3072. * @public
  3073. * @since 1.4
  3074. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3075. */
  3076. public function SetLeftMargin($margin) {
  3077. //Set left margin
  3078. $this->lMargin = $margin;
  3079. if (($this->page > 0) AND ($this->x < $margin)) {
  3080. $this->x = $margin;
  3081. }
  3082. }
  3083. /**
  3084. * Defines the top margin. The method can be called before creating the first page.
  3085. * @param $margin (float) The margin.
  3086. * @public
  3087. * @since 1.5
  3088. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3089. */
  3090. public function SetTopMargin($margin) {
  3091. //Set top margin
  3092. $this->tMargin = $margin;
  3093. if (($this->page > 0) AND ($this->y < $margin)) {
  3094. $this->y = $margin;
  3095. }
  3096. }
  3097. /**
  3098. * Defines the right margin. The method can be called before creating the first page.
  3099. * @param $margin (float) The margin.
  3100. * @public
  3101. * @since 1.5
  3102. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3103. */
  3104. public function SetRightMargin($margin) {
  3105. $this->rMargin = $margin;
  3106. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3107. $this->x = $this->w - $margin;
  3108. }
  3109. }
  3110. /**
  3111. * Set the same internal Cell padding for top, right, bottom, left-
  3112. * @param $pad (float) internal padding.
  3113. * @public
  3114. * @since 2.1.000 (2008-01-09)
  3115. * @see getCellPaddings(), setCellPaddings()
  3116. */
  3117. public function SetCellPadding($pad) {
  3118. if ($pad >= 0) {
  3119. $this->cell_padding['L'] = $pad;
  3120. $this->cell_padding['T'] = $pad;
  3121. $this->cell_padding['R'] = $pad;
  3122. $this->cell_padding['B'] = $pad;
  3123. }
  3124. }
  3125. /**
  3126. * Set the internal Cell paddings.
  3127. * @param $left (float) left padding
  3128. * @param $top (float) top padding
  3129. * @param $right (float) right padding
  3130. * @param $bottom (float) bottom padding
  3131. * @public
  3132. * @since 5.9.000 (2010-10-03)
  3133. * @see getCellPaddings(), SetCellPadding()
  3134. */
  3135. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3136. if (($left !== '') AND ($left >= 0)) {
  3137. $this->cell_padding['L'] = $left;
  3138. }
  3139. if (($top !== '') AND ($top >= 0)) {
  3140. $this->cell_padding['T'] = $top;
  3141. }
  3142. if (($right !== '') AND ($right >= 0)) {
  3143. $this->cell_padding['R'] = $right;
  3144. }
  3145. if (($bottom !== '') AND ($bottom >= 0)) {
  3146. $this->cell_padding['B'] = $bottom;
  3147. }
  3148. }
  3149. /**
  3150. * Get the internal Cell padding array.
  3151. * @return array of padding values
  3152. * @public
  3153. * @since 5.9.000 (2010-10-03)
  3154. * @see setCellPaddings(), SetCellPadding()
  3155. */
  3156. public function getCellPaddings() {
  3157. return $this->cell_padding;
  3158. }
  3159. /**
  3160. * Set the internal Cell margins.
  3161. * @param $left (float) left margin
  3162. * @param $top (float) top margin
  3163. * @param $right (float) right margin
  3164. * @param $bottom (float) bottom margin
  3165. * @public
  3166. * @since 5.9.000 (2010-10-03)
  3167. * @see getCellMargins()
  3168. */
  3169. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3170. if (($left !== '') AND ($left >= 0)) {
  3171. $this->cell_margin['L'] = $left;
  3172. }
  3173. if (($top !== '') AND ($top >= 0)) {
  3174. $this->cell_margin['T'] = $top;
  3175. }
  3176. if (($right !== '') AND ($right >= 0)) {
  3177. $this->cell_margin['R'] = $right;
  3178. }
  3179. if (($bottom !== '') AND ($bottom >= 0)) {
  3180. $this->cell_margin['B'] = $bottom;
  3181. }
  3182. }
  3183. /**
  3184. * Get the internal Cell margin array.
  3185. * @return array of margin values
  3186. * @public
  3187. * @since 5.9.000 (2010-10-03)
  3188. * @see setCellMargins()
  3189. */
  3190. public function getCellMargins() {
  3191. return $this->cell_margin;
  3192. }
  3193. /**
  3194. * Adjust the internal Cell padding array to take account of the line width.
  3195. * @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)))
  3196. * @return array of adjustments
  3197. * @public
  3198. * @since 5.9.000 (2010-10-03)
  3199. */
  3200. protected function adjustCellPadding($brd=0) {
  3201. if (empty($brd)) {
  3202. return;
  3203. }
  3204. if (is_string($brd)) {
  3205. // convert string to array
  3206. $slen = strlen($brd);
  3207. $newbrd = array();
  3208. for ($i = 0; $i < $slen; ++$i) {
  3209. $newbrd[$brd[$i]] = true;
  3210. }
  3211. $brd = $newbrd;
  3212. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3213. $brd = array('LRTB' => true);
  3214. }
  3215. if (!is_array($brd)) {
  3216. return;
  3217. }
  3218. // store current cell padding
  3219. $cp = $this->cell_padding;
  3220. // select border mode
  3221. if (isset($brd['mode'])) {
  3222. $mode = $brd['mode'];
  3223. unset($brd['mode']);
  3224. } else {
  3225. $mode = 'normal';
  3226. }
  3227. // process borders
  3228. foreach ($brd as $border => $style) {
  3229. $line_width = $this->LineWidth;
  3230. if (is_array($style) AND isset($style['width'])) {
  3231. // get border width
  3232. $line_width = $style['width'];
  3233. }
  3234. $adj = 0; // line width inside the cell
  3235. switch ($mode) {
  3236. case 'ext': {
  3237. $adj = 0;
  3238. break;
  3239. }
  3240. case 'int': {
  3241. $adj = $line_width;
  3242. break;
  3243. }
  3244. case 'normal':
  3245. default: {
  3246. $adj = ($line_width / 2);
  3247. break;
  3248. }
  3249. }
  3250. // correct internal cell padding if required to avoid overlap between text and lines
  3251. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3252. $this->cell_padding['T'] = $adj;
  3253. }
  3254. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3255. $this->cell_padding['R'] = $adj;
  3256. }
  3257. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3258. $this->cell_padding['B'] = $adj;
  3259. }
  3260. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3261. $this->cell_padding['L'] = $adj;
  3262. }
  3263. }
  3264. 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']));
  3265. }
  3266. /**
  3267. * 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.
  3268. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3269. * @param $margin (float) Distance from the bottom of the page.
  3270. * @public
  3271. * @since 1.0
  3272. * @see Cell(), MultiCell(), AcceptPageBreak()
  3273. */
  3274. public function SetAutoPageBreak($auto, $margin=0) {
  3275. $this->AutoPageBreak = $auto ? true : false;
  3276. $this->bMargin = $margin;
  3277. $this->PageBreakTrigger = $this->h - $margin;
  3278. }
  3279. /**
  3280. * Return the auto-page-break mode (true or false).
  3281. * @return boolean auto-page-break mode
  3282. * @public
  3283. * @since 5.9.088
  3284. */
  3285. public function getAutoPageBreak() {
  3286. return $this->AutoPageBreak;
  3287. }
  3288. /**
  3289. * Defines the way the document is to be displayed by the viewer.
  3290. * @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>
  3291. * @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>
  3292. * @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>
  3293. * @public
  3294. * @since 1.2
  3295. */
  3296. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3297. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3298. $this->ZoomMode = $zoom;
  3299. } else {
  3300. $this->Error('Incorrect zoom display mode: '.$zoom);
  3301. }
  3302. switch ($layout) {
  3303. case 'default':
  3304. case 'single':
  3305. case 'SinglePage': {
  3306. $this->LayoutMode = 'SinglePage';
  3307. break;
  3308. }
  3309. case 'continuous':
  3310. case 'OneColumn': {
  3311. $this->LayoutMode = 'OneColumn';
  3312. break;
  3313. }
  3314. case 'two':
  3315. case 'TwoColumnLeft': {
  3316. $this->LayoutMode = 'TwoColumnLeft';
  3317. break;
  3318. }
  3319. case 'TwoColumnRight': {
  3320. $this->LayoutMode = 'TwoColumnRight';
  3321. break;
  3322. }
  3323. case 'TwoPageLeft': {
  3324. $this->LayoutMode = 'TwoPageLeft';
  3325. break;
  3326. }
  3327. case 'TwoPageRight': {
  3328. $this->LayoutMode = 'TwoPageRight';
  3329. break;
  3330. }
  3331. default: {
  3332. $this->LayoutMode = 'SinglePage';
  3333. }
  3334. }
  3335. switch ($mode) {
  3336. case 'UseNone': {
  3337. $this->PageMode = 'UseNone';
  3338. break;
  3339. }
  3340. case 'UseOutlines': {
  3341. $this->PageMode = 'UseOutlines';
  3342. break;
  3343. }
  3344. case 'UseThumbs': {
  3345. $this->PageMode = 'UseThumbs';
  3346. break;
  3347. }
  3348. case 'FullScreen': {
  3349. $this->PageMode = 'FullScreen';
  3350. break;
  3351. }
  3352. case 'UseOC': {
  3353. $this->PageMode = 'UseOC';
  3354. break;
  3355. }
  3356. case '': {
  3357. $this->PageMode = 'UseAttachments';
  3358. break;
  3359. }
  3360. default: {
  3361. $this->PageMode = 'UseNone';
  3362. }
  3363. }
  3364. }
  3365. /**
  3366. * 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.
  3367. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3368. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3369. * @public
  3370. * @since 1.4
  3371. */
  3372. public function SetCompression($compress=true) {
  3373. if (function_exists('gzcompress')) {
  3374. $this->compress = $compress ? true : false;
  3375. } else {
  3376. $this->compress = false;
  3377. }
  3378. }
  3379. /**
  3380. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3381. * @param $mode (boolean) If true force sRGB output intent.
  3382. * @public
  3383. * @since 5.9.121 (2011-09-28)
  3384. */
  3385. public function setSRGBmode($mode=false) {
  3386. $this->force_srgb = $mode ? true : false;
  3387. }
  3388. /**
  3389. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3390. * This has effect only when unicode mode is set to false.
  3391. * @param $unicode (boolean) if true set the meta information in Unicode
  3392. * @since 5.9.027 (2010-12-01)
  3393. * @public
  3394. */
  3395. public function SetDocInfoUnicode($unicode=true) {
  3396. $this->docinfounicode = $unicode ? true : false;
  3397. }
  3398. /**
  3399. * Defines the title of the document.
  3400. * @param $title (string) The title.
  3401. * @public
  3402. * @since 1.2
  3403. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3404. */
  3405. public function SetTitle($title) {
  3406. $this->title = $title;
  3407. }
  3408. /**
  3409. * Defines the subject of the document.
  3410. * @param $subject (string) The subject.
  3411. * @public
  3412. * @since 1.2
  3413. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3414. */
  3415. public function SetSubject($subject) {
  3416. $this->subject = $subject;
  3417. }
  3418. /**
  3419. * Defines the author of the document.
  3420. * @param $author (string) The name of the author.
  3421. * @public
  3422. * @since 1.2
  3423. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3424. */
  3425. public function SetAuthor($author) {
  3426. $this->author = $author;
  3427. }
  3428. /**
  3429. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3430. * @param $keywords (string) The list of keywords.
  3431. * @public
  3432. * @since 1.2
  3433. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3434. */
  3435. public function SetKeywords($keywords) {
  3436. $this->keywords = $keywords;
  3437. }
  3438. /**
  3439. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3440. * @param $creator (string) The name of the creator.
  3441. * @public
  3442. * @since 1.2
  3443. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3444. */
  3445. public function SetCreator($creator) {
  3446. $this->creator = $creator;
  3447. }
  3448. /**
  3449. * 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.
  3450. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3451. * @param $msg (string) The error message
  3452. * @public
  3453. * @since 1.0
  3454. */
  3455. public function Error($msg) {
  3456. // unset all class variables
  3457. $this->_destroy(true);
  3458. // exit program and print error
  3459. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3460. }
  3461. /**
  3462. * This method begins the generation of the PDF document.
  3463. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3464. * Note: no page is created by this method
  3465. * @public
  3466. * @since 1.0
  3467. * @see AddPage(), Close()
  3468. */
  3469. public function Open() {
  3470. $this->state = 1;
  3471. }
  3472. /**
  3473. * Terminates the PDF document.
  3474. * It is not necessary to call this method explicitly because Output() does it automatically.
  3475. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3476. * @public
  3477. * @since 1.0
  3478. * @see Open(), Output()
  3479. */
  3480. public function Close() {
  3481. if ($this->state == 3) {
  3482. return;
  3483. }
  3484. if ($this->page == 0) {
  3485. $this->AddPage();
  3486. }
  3487. $this->endLayer();
  3488. if ($this->tcpdflink) {
  3489. // save current graphic settings
  3490. $gvars = $this->getGraphicVars();
  3491. $this->setEqualColumns();
  3492. $this->lastpage(true);
  3493. $this->SetAutoPageBreak(false);
  3494. $this->x = 0;
  3495. $this->y = $this->h - (1 / $this->k);
  3496. $this->lMargin = 0;
  3497. $this->_out('q');
  3498. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  3499. $this->SetFont($font, '', 1);
  3500. $this->setTextRenderingMode(0, false, false);
  3501. $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";
  3502. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3503. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3504. $this->_out('Q');
  3505. // restore graphic settings
  3506. $this->setGraphicVars($gvars);
  3507. }
  3508. // close page
  3509. $this->endPage();
  3510. // close document
  3511. $this->_enddoc();
  3512. // unset all class variables (except critical ones)
  3513. $this->_destroy(false);
  3514. }
  3515. /**
  3516. * Move pointer at the specified document page and update page dimensions.
  3517. * @param $pnum (int) page number (1 ... numpages)
  3518. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3519. * @public
  3520. * @since 2.1.000 (2008-01-07)
  3521. * @see getPage(), lastpage(), getNumPages()
  3522. */
  3523. public function setPage($pnum, $resetmargins=false) {
  3524. if (($pnum == $this->page) AND ($this->state == 2)) {
  3525. return;
  3526. }
  3527. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3528. $this->state = 2;
  3529. // save current graphic settings
  3530. //$gvars = $this->getGraphicVars();
  3531. $oldpage = $this->page;
  3532. $this->page = $pnum;
  3533. $this->wPt = $this->pagedim[$this->page]['w'];
  3534. $this->hPt = $this->pagedim[$this->page]['h'];
  3535. $this->w = $this->pagedim[$this->page]['wk'];
  3536. $this->h = $this->pagedim[$this->page]['hk'];
  3537. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3538. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3539. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3540. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3541. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3542. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3543. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3544. // restore graphic settings
  3545. //$this->setGraphicVars($gvars);
  3546. if ($resetmargins) {
  3547. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3548. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3549. $this->SetY($this->tMargin);
  3550. } else {
  3551. // account for booklet mode
  3552. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3553. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3554. $this->lMargin += $deltam;
  3555. $this->rMargin -= $deltam;
  3556. }
  3557. }
  3558. } else {
  3559. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3560. }
  3561. }
  3562. /**
  3563. * Reset pointer to the last document page.
  3564. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3565. * @public
  3566. * @since 2.0.000 (2008-01-04)
  3567. * @see setPage(), getPage(), getNumPages()
  3568. */
  3569. public function lastPage($resetmargins=false) {
  3570. $this->setPage($this->getNumPages(), $resetmargins);
  3571. }
  3572. /**
  3573. * Get current document page number.
  3574. * @return int page number
  3575. * @public
  3576. * @since 2.1.000 (2008-01-07)
  3577. * @see setPage(), lastpage(), getNumPages()
  3578. */
  3579. public function getPage() {
  3580. return $this->page;
  3581. }
  3582. /**
  3583. * Get the total number of insered pages.
  3584. * @return int number of pages
  3585. * @public
  3586. * @since 2.1.000 (2008-01-07)
  3587. * @see setPage(), getPage(), lastpage()
  3588. */
  3589. public function getNumPages() {
  3590. return $this->numpages;
  3591. }
  3592. /**
  3593. * Adds a new TOC (Table Of Content) page to the document.
  3594. * @param $orientation (string) page orientation.
  3595. * @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().
  3596. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3597. * @public
  3598. * @since 5.0.001 (2010-05-06)
  3599. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3600. */
  3601. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3602. $this->AddPage($orientation, $format, $keepmargins, true);
  3603. }
  3604. /**
  3605. * Terminate the current TOC (Table Of Content) page
  3606. * @public
  3607. * @since 5.0.001 (2010-05-06)
  3608. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3609. */
  3610. public function endTOCPage() {
  3611. $this->endPage(true);
  3612. }
  3613. /**
  3614. * 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).
  3615. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3616. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3617. * @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().
  3618. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3619. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3620. * @public
  3621. * @since 1.0
  3622. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3623. */
  3624. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3625. if ($this->inxobj) {
  3626. // we are inside an XObject template
  3627. return;
  3628. }
  3629. if (!isset($this->original_lMargin) OR $keepmargins) {
  3630. $this->original_lMargin = $this->lMargin;
  3631. }
  3632. if (!isset($this->original_rMargin) OR $keepmargins) {
  3633. $this->original_rMargin = $this->rMargin;
  3634. }
  3635. // terminate previous page
  3636. $this->endPage();
  3637. // start new page
  3638. $this->startPage($orientation, $format, $tocpage);
  3639. }
  3640. /**
  3641. * Terminate the current page
  3642. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3643. * @public
  3644. * @since 4.2.010 (2008-11-14)
  3645. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3646. */
  3647. public function endPage($tocpage=false) {
  3648. // check if page is already closed
  3649. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3650. return;
  3651. }
  3652. // print page footer
  3653. $this->setFooter();
  3654. // close page
  3655. $this->_endpage();
  3656. // mark page as closed
  3657. $this->pageopen[$this->page] = false;
  3658. if ($tocpage) {
  3659. $this->tocpage = false;
  3660. }
  3661. }
  3662. /**
  3663. * Starts a new page to the document. The page must be closed using the endPage() function.
  3664. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3665. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3666. * @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().
  3667. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3668. * @since 4.2.010 (2008-11-14)
  3669. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3670. * @public
  3671. */
  3672. public function startPage($orientation='', $format='', $tocpage=false) {
  3673. if ($tocpage) {
  3674. $this->tocpage = true;
  3675. }
  3676. // move page numbers of documents to be attached
  3677. if ($this->tocpage) {
  3678. // move reference to unexistent pages (used for page attachments)
  3679. // adjust outlines
  3680. $tmpoutlines = $this->outlines;
  3681. foreach ($tmpoutlines as $key => $outline) {
  3682. if ($outline['p'] > $this->numpages) {
  3683. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3684. }
  3685. }
  3686. // adjust dests
  3687. $tmpdests = $this->dests;
  3688. foreach ($tmpdests as $key => $dest) {
  3689. if ($dest['p'] > $this->numpages) {
  3690. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3691. }
  3692. }
  3693. // adjust links
  3694. $tmplinks = $this->links;
  3695. foreach ($tmplinks as $key => $link) {
  3696. if ($link[0] > $this->numpages) {
  3697. $this->links[$key][0] = ($link[0] + 1);
  3698. }
  3699. }
  3700. }
  3701. if ($this->numpages > $this->page) {
  3702. // this page has been already added
  3703. $this->setPage($this->page + 1);
  3704. $this->SetY($this->tMargin);
  3705. return;
  3706. }
  3707. // start a new page
  3708. if ($this->state == 0) {
  3709. $this->Open();
  3710. }
  3711. ++$this->numpages;
  3712. $this->swapMargins($this->booklet);
  3713. // save current graphic settings
  3714. $gvars = $this->getGraphicVars();
  3715. // start new page
  3716. $this->_beginpage($orientation, $format);
  3717. // mark page as open
  3718. $this->pageopen[$this->page] = true;
  3719. // restore graphic settings
  3720. $this->setGraphicVars($gvars);
  3721. // mark this point
  3722. $this->setPageMark();
  3723. // print page header
  3724. $this->setHeader();
  3725. // restore graphic settings
  3726. $this->setGraphicVars($gvars);
  3727. // mark this point
  3728. $this->setPageMark();
  3729. // print table header (if any)
  3730. $this->setTableHeader();
  3731. // set mark for empty page check
  3732. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3733. }
  3734. /**
  3735. * Set start-writing mark on current page stream used to put borders and fills.
  3736. * Borders and fills are always created after content and inserted on the position marked by this method.
  3737. * This function must be called after calling Image() function for a background image.
  3738. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3739. * @public
  3740. * @since 4.0.016 (2008-07-30)
  3741. */
  3742. public function setPageMark() {
  3743. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3744. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3745. $this->setContentMark();
  3746. }
  3747. /**
  3748. * Set start-writing mark on selected page.
  3749. * Borders and fills are always created after content and inserted on the position marked by this method.
  3750. * @param $page (int) page number (default is the current page)
  3751. * @protected
  3752. * @since 4.6.021 (2009-07-20)
  3753. */
  3754. protected function setContentMark($page=0) {
  3755. if ($page <= 0) {
  3756. $page = $this->page;
  3757. }
  3758. if (isset($this->footerlen[$page])) {
  3759. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3760. } else {
  3761. $this->cntmrk[$page] = $this->pagelen[$page];
  3762. }
  3763. }
  3764. /**
  3765. * Set header data.
  3766. * @param $ln (string) header image logo
  3767. * @param $lw (string) header image logo width in mm
  3768. * @param $ht (string) string to print as title on document header
  3769. * @param $hs (string) string to print on document header
  3770. * @public
  3771. */
  3772. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3773. $this->header_logo = $ln;
  3774. $this->header_logo_width = $lw;
  3775. $this->header_title = $ht;
  3776. $this->header_string = $hs;
  3777. }
  3778. /**
  3779. * Returns header data:
  3780. * <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>
  3781. * @return array()
  3782. * @public
  3783. * @since 4.0.012 (2008-07-24)
  3784. */
  3785. public function getHeaderData() {
  3786. $ret = array();
  3787. $ret['logo'] = $this->header_logo;
  3788. $ret['logo_width'] = $this->header_logo_width;
  3789. $ret['title'] = $this->header_title;
  3790. $ret['string'] = $this->header_string;
  3791. return $ret;
  3792. }
  3793. /**
  3794. * Set header margin.
  3795. * (minimum distance between header and top page margin)
  3796. * @param $hm (int) distance in user units
  3797. * @public
  3798. */
  3799. public function setHeaderMargin($hm=10) {
  3800. $this->header_margin = $hm;
  3801. }
  3802. /**
  3803. * Returns header margin in user units.
  3804. * @return float
  3805. * @since 4.0.012 (2008-07-24)
  3806. * @public
  3807. */
  3808. public function getHeaderMargin() {
  3809. return $this->header_margin;
  3810. }
  3811. /**
  3812. * Set footer margin.
  3813. * (minimum distance between footer and bottom page margin)
  3814. * @param $fm (int) distance in user units
  3815. * @public
  3816. */
  3817. public function setFooterMargin($fm=10) {
  3818. $this->footer_margin = $fm;
  3819. }
  3820. /**
  3821. * Returns footer margin in user units.
  3822. * @return float
  3823. * @since 4.0.012 (2008-07-24)
  3824. * @public
  3825. */
  3826. public function getFooterMargin() {
  3827. return $this->footer_margin;
  3828. }
  3829. /**
  3830. * Set a flag to print page header.
  3831. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3832. * @public
  3833. */
  3834. public function setPrintHeader($val=true) {
  3835. $this->print_header = $val ? true : false;
  3836. }
  3837. /**
  3838. * Set a flag to print page footer.
  3839. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3840. * @public
  3841. */
  3842. public function setPrintFooter($val=true) {
  3843. $this->print_footer = $val ? true : false;
  3844. }
  3845. /**
  3846. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3847. * @return float
  3848. * @public
  3849. */
  3850. public function getImageRBX() {
  3851. return $this->img_rb_x;
  3852. }
  3853. /**
  3854. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3855. * @return float
  3856. * @public
  3857. */
  3858. public function getImageRBY() {
  3859. return $this->img_rb_y;
  3860. }
  3861. /**
  3862. * Reset the xobject template used by Header() method.
  3863. * @public
  3864. */
  3865. public function resetHeaderTemplate() {
  3866. $this->header_xobjid = -1;
  3867. }
  3868. /**
  3869. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3870. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3871. * @public
  3872. */
  3873. public function setHeaderTemplateAutoreset($val=true) {
  3874. $this->header_xobj_autoreset = $val ? true : false;
  3875. }
  3876. /**
  3877. * This method is used to render the page header.
  3878. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3879. * @public
  3880. */
  3881. public function Header() {
  3882. if ($this->header_xobjid < 0) {
  3883. // start a new XObject Template
  3884. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3885. $headerfont = $this->getHeaderFont();
  3886. $headerdata = $this->getHeaderData();
  3887. $this->y = $this->header_margin;
  3888. if ($this->rtl) {
  3889. $this->x = $this->w - $this->original_rMargin;
  3890. } else {
  3891. $this->x = $this->original_lMargin;
  3892. }
  3893. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3894. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3895. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3896. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3897. } elseif ($imgtype == 'svg') {
  3898. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3899. } else {
  3900. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3901. }
  3902. $imgy = $this->getImageRBY();
  3903. } else {
  3904. $imgy = $this->y;
  3905. }
  3906. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3907. // set starting margin for text data cell
  3908. if ($this->getRTL()) {
  3909. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3910. } else {
  3911. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3912. }
  3913. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3914. $this->SetTextColor(0, 0, 0);
  3915. // header title
  3916. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3917. $this->SetX($header_x);
  3918. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3919. // header string
  3920. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3921. $this->SetX($header_x);
  3922. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3923. // print an ending header line
  3924. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3925. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3926. if ($this->rtl) {
  3927. $this->SetX($this->original_rMargin);
  3928. } else {
  3929. $this->SetX($this->original_lMargin);
  3930. }
  3931. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3932. $this->endTemplate();
  3933. }
  3934. // print header template
  3935. $x = 0;
  3936. $dx = 0;
  3937. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3938. // adjust margins for booklet mode
  3939. $dx = ($this->original_lMargin - $this->original_rMargin);
  3940. }
  3941. if ($this->rtl) {
  3942. $x = $this->w + $dx;
  3943. } else {
  3944. $x = 0 + $dx;
  3945. }
  3946. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3947. if ($this->header_xobj_autoreset) {
  3948. // reset header xobject template at each page
  3949. $this->header_xobjid = -1;
  3950. }
  3951. }
  3952. /**
  3953. * This method is used to render the page footer.
  3954. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3955. * @public
  3956. */
  3957. public function Footer() {
  3958. $cur_y = $this->y;
  3959. $this->SetTextColor(0, 0, 0);
  3960. //set style for cell border
  3961. $line_width = 0.85 / $this->k;
  3962. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3963. //print document barcode
  3964. $barcode = $this->getBarcode();
  3965. if (!empty($barcode)) {
  3966. $this->Ln($line_width);
  3967. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3968. $style = array(
  3969. 'position' => $this->rtl?'R':'L',
  3970. 'align' => $this->rtl?'R':'L',
  3971. 'stretch' => false,
  3972. 'fitwidth' => true,
  3973. 'cellfitalign' => '',
  3974. 'border' => false,
  3975. 'padding' => 0,
  3976. 'fgcolor' => array(0,0,0),
  3977. 'bgcolor' => false,
  3978. 'text' => false
  3979. );
  3980. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3981. }
  3982. if (empty($this->pagegroups)) {
  3983. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3984. } else {
  3985. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3986. }
  3987. $this->SetY($cur_y);
  3988. //Print page number
  3989. if ($this->getRTL()) {
  3990. $this->SetX($this->original_rMargin);
  3991. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3992. } else {
  3993. $this->SetX($this->original_lMargin);
  3994. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3995. }
  3996. }
  3997. /**
  3998. * This method is used to render the page header.
  3999. * @protected
  4000. * @since 4.0.012 (2008-07-24)
  4001. */
  4002. protected function setHeader() {
  4003. if (!$this->print_header) {
  4004. return;
  4005. }
  4006. $this->InHeader = true;
  4007. $this->setGraphicVars($this->default_graphic_vars);
  4008. $temp_thead = $this->thead;
  4009. $temp_theadMargins = $this->theadMargins;
  4010. $lasth = $this->lasth;
  4011. $this->_out('q');
  4012. $this->rMargin = $this->original_rMargin;
  4013. $this->lMargin = $this->original_lMargin;
  4014. $this->SetCellPadding(0);
  4015. //set current position
  4016. if ($this->rtl) {
  4017. $this->SetXY($this->original_rMargin, $this->header_margin);
  4018. } else {
  4019. $this->SetXY($this->original_lMargin, $this->header_margin);
  4020. }
  4021. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  4022. $this->Header();
  4023. //restore position
  4024. if ($this->rtl) {
  4025. $this->SetXY($this->original_rMargin, $this->tMargin);
  4026. } else {
  4027. $this->SetXY($this->original_lMargin, $this->tMargin);
  4028. }
  4029. $this->_out('Q');
  4030. $this->lasth = $lasth;
  4031. $this->thead = $temp_thead;
  4032. $this->theadMargins = $temp_theadMargins;
  4033. $this->newline = false;
  4034. $this->InHeader = false;
  4035. }
  4036. /**
  4037. * This method is used to render the page footer.
  4038. * @protected
  4039. * @since 4.0.012 (2008-07-24)
  4040. */
  4041. protected function setFooter() {
  4042. //Page footer
  4043. $this->InFooter = true;
  4044. // save current graphic settings
  4045. $gvars = $this->getGraphicVars();
  4046. // mark this point
  4047. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4048. $this->_out("\n");
  4049. if ($this->print_footer) {
  4050. $this->setGraphicVars($this->default_graphic_vars);
  4051. $this->current_column = 0;
  4052. $this->num_columns = 1;
  4053. $temp_thead = $this->thead;
  4054. $temp_theadMargins = $this->theadMargins;
  4055. $lasth = $this->lasth;
  4056. $this->_out('q');
  4057. $this->rMargin = $this->original_rMargin;
  4058. $this->lMargin = $this->original_lMargin;
  4059. $this->SetCellPadding(0);
  4060. //set current position
  4061. $footer_y = $this->h - $this->footer_margin;
  4062. if ($this->rtl) {
  4063. $this->SetXY($this->original_rMargin, $footer_y);
  4064. } else {
  4065. $this->SetXY($this->original_lMargin, $footer_y);
  4066. }
  4067. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4068. $this->Footer();
  4069. //restore position
  4070. if ($this->rtl) {
  4071. $this->SetXY($this->original_rMargin, $this->tMargin);
  4072. } else {
  4073. $this->SetXY($this->original_lMargin, $this->tMargin);
  4074. }
  4075. $this->_out('Q');
  4076. $this->lasth = $lasth;
  4077. $this->thead = $temp_thead;
  4078. $this->theadMargins = $temp_theadMargins;
  4079. }
  4080. // restore graphic settings
  4081. $this->setGraphicVars($gvars);
  4082. $this->current_column = $gvars['current_column'];
  4083. $this->num_columns = $gvars['num_columns'];
  4084. // calculate footer length
  4085. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4086. $this->InFooter = false;
  4087. }
  4088. /**
  4089. * Check if we are on the page body (excluding page header and footer).
  4090. * @return true if we are not in page header nor in page footer, false otherwise.
  4091. * @protected
  4092. * @since 5.9.091 (2011-06-15)
  4093. */
  4094. protected function inPageBody() {
  4095. return (($this->InHeader === false) AND ($this->InFooter === false));
  4096. }
  4097. /**
  4098. * This method is used to render the table header on new page (if any).
  4099. * @protected
  4100. * @since 4.5.030 (2009-03-25)
  4101. */
  4102. protected function setTableHeader() {
  4103. if ($this->num_columns > 1) {
  4104. // multi column mode
  4105. return;
  4106. }
  4107. if (isset($this->theadMargins['top'])) {
  4108. // restore the original top-margin
  4109. $this->tMargin = $this->theadMargins['top'];
  4110. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4111. $this->y = $this->tMargin;
  4112. }
  4113. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4114. // set margins
  4115. $prev_lMargin = $this->lMargin;
  4116. $prev_rMargin = $this->rMargin;
  4117. $prev_cell_padding = $this->cell_padding;
  4118. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4119. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4120. $this->cell_padding = $this->theadMargins['cell_padding'];
  4121. if ($this->rtl) {
  4122. $this->x = $this->w - $this->rMargin;
  4123. } else {
  4124. $this->x = $this->lMargin;
  4125. }
  4126. // account for special "cell" mode
  4127. if ($this->theadMargins['cell']) {
  4128. if ($this->rtl) {
  4129. $this->x -= $this->cell_padding['R'];
  4130. } else {
  4131. $this->x += $this->cell_padding['L'];
  4132. }
  4133. }
  4134. // print table header
  4135. $this->writeHTML($this->thead, false, false, false, false, '');
  4136. // set new top margin to skip the table headers
  4137. if (!isset($this->theadMargins['top'])) {
  4138. $this->theadMargins['top'] = $this->tMargin;
  4139. }
  4140. // store end of header position
  4141. if (!isset($this->columns[0]['th'])) {
  4142. $this->columns[0]['th'] = array();
  4143. }
  4144. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4145. $this->tMargin = $this->y;
  4146. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4147. $this->lasth = 0;
  4148. $this->lMargin = $prev_lMargin;
  4149. $this->rMargin = $prev_rMargin;
  4150. $this->cell_padding = $prev_cell_padding;
  4151. }
  4152. }
  4153. /**
  4154. * Returns the current page number.
  4155. * @return int page number
  4156. * @public
  4157. * @since 1.0
  4158. * @see getAliasNbPages()
  4159. */
  4160. public function PageNo() {
  4161. return $this->page;
  4162. }
  4163. /**
  4164. * Defines a new spot color.
  4165. * It can be expressed in RGB components or gray scale.
  4166. * The method can be called before the first page is created and the value is retained from page to page.
  4167. * @param $name (string) Full name of the spot color.
  4168. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4169. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4170. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4171. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4172. * @public
  4173. * @since 4.0.024 (2008-09-12)
  4174. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4175. */
  4176. public function AddSpotColor($name, $c, $m, $y, $k) {
  4177. if (!isset($this->spot_colors[$name])) {
  4178. $i = (1 + count($this->spot_colors));
  4179. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  4180. }
  4181. }
  4182. /**
  4183. * Return the Spot color array.
  4184. * @param $name (string) Name of the spot color.
  4185. * @return (array) Spot color array or false if not defined.
  4186. * @public
  4187. * @since 5.9.125 (2011-10-03)
  4188. */
  4189. public function getSpotColor($name) {
  4190. if (isset($this->spot_colors[$name])) {
  4191. return $this->spot_colors[$name];
  4192. }
  4193. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4194. $color = strtolower($color);
  4195. if (isset($this->spotcolor[$color])) {
  4196. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4197. return $this->spot_colors[$this->spotcolor[$color][4]];
  4198. }
  4199. return false;
  4200. }
  4201. /**
  4202. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4203. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4204. * @param $name (string) Name of the spot color.
  4205. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4206. * @return (string) PDF color command.
  4207. * @public
  4208. * @since 5.9.125 (2011-10-03)
  4209. */
  4210. public function setSpotColor($type, $name, $tint=100) {
  4211. $spotcolor = $this->getSpotColor($name);
  4212. if ($spotcolor === false) {
  4213. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4214. }
  4215. $tint = (max(0, min(100, $tint)) / 100);
  4216. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4217. switch ($type) {
  4218. case 'draw': {
  4219. $pdfcolor .= sprintf('CS %F SCN', $tint);
  4220. $this->DrawColor = $pdfcolor;
  4221. $this->strokecolor = $spotcolor;
  4222. break;
  4223. }
  4224. case 'fill': {
  4225. $pdfcolor .= sprintf('cs %F scn', $tint);
  4226. $this->FillColor = $pdfcolor;
  4227. $this->bgcolor = $spotcolor;
  4228. break;
  4229. }
  4230. case 'text': {
  4231. $pdfcolor .= sprintf('cs %F scn', $tint);
  4232. $this->TextColor = $pdfcolor;
  4233. $this->fgcolor = $spotcolor;
  4234. break;
  4235. }
  4236. }
  4237. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4238. if ($this->page > 0) {
  4239. $this->_out($pdfcolor);
  4240. }
  4241. if ($this->inxobj) {
  4242. // we are inside an XObject template
  4243. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4244. }
  4245. return $pdfcolor;
  4246. }
  4247. /**
  4248. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4249. * @param $name (string) Name of the spot color.
  4250. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4251. * @public
  4252. * @since 4.0.024 (2008-09-12)
  4253. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4254. */
  4255. public function SetDrawSpotColor($name, $tint=100) {
  4256. $this->setSpotColor('draw', $name, $tint);
  4257. }
  4258. /**
  4259. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4260. * @param $name (string) Name of the spot color.
  4261. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4262. * @public
  4263. * @since 4.0.024 (2008-09-12)
  4264. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4265. */
  4266. public function SetFillSpotColor($name, $tint=100) {
  4267. $this->setSpotColor('fill', $name, $tint);
  4268. }
  4269. /**
  4270. * Defines the spot color used for text.
  4271. * @param $name (string) Name of the spot color.
  4272. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4273. * @public
  4274. * @since 4.0.024 (2008-09-12)
  4275. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4276. */
  4277. public function SetTextSpotColor($name, $tint=100) {
  4278. $this->setSpotColor('text', $name, $tint);
  4279. }
  4280. /**
  4281. * Set the color array for the specified type ('draw', 'fill', 'text').
  4282. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4283. * The method can be called before the first page is created and the value is retained from page to page.
  4284. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4285. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  4286. * @param $ret (boolean) If true do not send the PDF command.
  4287. * @return (string) The PDF command or empty string.
  4288. * @public
  4289. * @since 3.1.000 (2008-06-11)
  4290. */
  4291. public function setColorArray($type, $color, $ret=false) {
  4292. if (is_array($color)) {
  4293. $color = array_values($color);
  4294. // component: grey, RGB red or CMYK cyan
  4295. $c = isset($color[0]) ? $color[0] : -1;
  4296. // component: RGB green or CMYK magenta
  4297. $m = isset($color[1]) ? $color[1] : -1;
  4298. // component: RGB blue or CMYK yellow
  4299. $y = isset($color[2]) ? $color[2] : -1;
  4300. // component: CMYK black
  4301. $k = isset($color[3]) ? $color[3] : -1;
  4302. // color name
  4303. $name = isset($color[4]) ? $color[4] : '';
  4304. if ($c >= 0) {
  4305. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  4306. }
  4307. }
  4308. return '';
  4309. }
  4310. /**
  4311. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4312. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4313. * The method can be called before the first page is created and the value is retained from page to page.
  4314. * @param $color (array) Array of colors (1, 3 or 4 values).
  4315. * @param $ret (boolean) If true do not send the PDF command.
  4316. * @return string the PDF command
  4317. * @public
  4318. * @since 3.1.000 (2008-06-11)
  4319. * @see SetDrawColor()
  4320. */
  4321. public function SetDrawColorArray($color, $ret=false) {
  4322. return $this->setColorArray('draw', $color, $ret);
  4323. }
  4324. /**
  4325. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4326. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4327. * The method can be called before the first page is created and the value is retained from page to page.
  4328. * @param $color (array) Array of colors (1, 3 or 4 values).
  4329. * @param $ret (boolean) If true do not send the PDF command.
  4330. * @public
  4331. * @since 3.1.000 (2008-6-11)
  4332. * @see SetFillColor()
  4333. */
  4334. public function SetFillColorArray($color, $ret=false) {
  4335. return $this->setColorArray('fill', $color, $ret);
  4336. }
  4337. /**
  4338. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4339. * The method can be called before the first page is created and the value is retained from page to page.
  4340. * @param $color (array) Array of colors (1, 3 or 4 values).
  4341. * @param $ret (boolean) If true do not send the PDF command.
  4342. * @public
  4343. * @since 3.1.000 (2008-6-11)
  4344. * @see SetFillColor()
  4345. */
  4346. public function SetTextColorArray($color, $ret=false) {
  4347. return $this->setColorArray('text', $color, $ret);
  4348. }
  4349. /**
  4350. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4351. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4352. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4353. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4354. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4355. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4356. * @param $ret (boolean) If true do not send the command.
  4357. * @param $name (string) spot color name (if any)
  4358. * @return (string) The PDF command or empty string.
  4359. * @public
  4360. * @since 5.9.125 (2011-10-03)
  4361. */
  4362. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4363. // set default values
  4364. if (!is_numeric($col1)) {
  4365. $col1 = 0;
  4366. }
  4367. if (!is_numeric($col2)) {
  4368. $col2 = -1;
  4369. }
  4370. if (!is_numeric($col3)) {
  4371. $col3 = -1;
  4372. }
  4373. if (!is_numeric($col4)) {
  4374. $col4 = -1;
  4375. }
  4376. // set color by case
  4377. $suffix = '';
  4378. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4379. // Grey scale
  4380. $col1 = max(0, min(255, $col1));
  4381. $intcolor = array('G' => $col1);
  4382. $pdfcolor = sprintf('%F ', ($col1 / 255));
  4383. $suffix = 'g';
  4384. } elseif ($col4 == -1) {
  4385. // RGB
  4386. $col1 = max(0, min(255, $col1));
  4387. $col2 = max(0, min(255, $col2));
  4388. $col3 = max(0, min(255, $col3));
  4389. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4390. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4391. $suffix = 'rg';
  4392. } else {
  4393. $col1 = max(0, min(100, $col1));
  4394. $col2 = max(0, min(100, $col2));
  4395. $col3 = max(0, min(100, $col3));
  4396. $col4 = max(0, min(100, $col4));
  4397. if (empty($name)) {
  4398. // CMYK
  4399. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4400. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4401. $suffix = 'k';
  4402. } else {
  4403. // SPOT COLOR
  4404. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4405. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4406. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4407. }
  4408. }
  4409. switch ($type) {
  4410. case 'draw': {
  4411. $pdfcolor .= strtoupper($suffix);
  4412. $this->DrawColor = $pdfcolor;
  4413. $this->strokecolor = $intcolor;
  4414. break;
  4415. }
  4416. case 'fill': {
  4417. $pdfcolor .= $suffix;
  4418. $this->FillColor = $pdfcolor;
  4419. $this->bgcolor = $intcolor;
  4420. break;
  4421. }
  4422. case 'text': {
  4423. $pdfcolor .= $suffix;
  4424. $this->TextColor = $pdfcolor;
  4425. $this->fgcolor = $intcolor;
  4426. break;
  4427. }
  4428. }
  4429. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4430. if (($type != 'text') AND ($this->page > 0)) {
  4431. if (!$ret) {
  4432. $this->_out($pdfcolor);
  4433. }
  4434. return $pdfcolor;
  4435. }
  4436. return '';
  4437. }
  4438. /**
  4439. * Convert a color array into a string representation.
  4440. * @param $c (array) Array of colors.
  4441. * @return (string) The color array representation.
  4442. * @protected
  4443. * @since 5.9.137 (2011-12-01)
  4444. */
  4445. protected function getColorStringFromArray($c) {
  4446. $c = array_values($c);
  4447. $color = '[';
  4448. switch (count($c)) {
  4449. case 4: {
  4450. // CMYK
  4451. $color .= sprintf('%F %F %F %F', (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));
  4452. break;
  4453. }
  4454. case 3: {
  4455. // RGB
  4456. $color .= sprintf('%F %F %F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  4457. break;
  4458. }
  4459. case 1: {
  4460. // grayscale
  4461. $color .= sprintf('%F', (max(0, min(255, floatval($c[0]))) / 255));
  4462. break;
  4463. }
  4464. }
  4465. $color .= ']';
  4466. return $color;
  4467. }
  4468. /**
  4469. * 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.
  4470. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4471. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4472. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4473. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4474. * @param $ret (boolean) If true do not send the command.
  4475. * @param $name (string) spot color name (if any)
  4476. * @return string the PDF command
  4477. * @public
  4478. * @since 1.3
  4479. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4480. */
  4481. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4482. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4483. }
  4484. /**
  4485. * 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.
  4486. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4487. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4488. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4489. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4490. * @param $ret (boolean) If true do not send the command.
  4491. * @param $name (string) Spot color name (if any).
  4492. * @return (string) The PDF command.
  4493. * @public
  4494. * @since 1.3
  4495. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4496. */
  4497. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4498. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4499. }
  4500. /**
  4501. * 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.
  4502. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4503. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4504. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4505. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4506. * @param $ret (boolean) If true do not send the command.
  4507. * @param $name (string) Spot color name (if any).
  4508. * @return (string) Empty string.
  4509. * @public
  4510. * @since 1.3
  4511. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4512. */
  4513. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4514. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4515. }
  4516. /**
  4517. * Returns the length of a string in user unit. A font must be selected.<br>
  4518. * @param $s (string) The string whose length is to be computed
  4519. * @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.
  4520. * @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.
  4521. * @param $fontsize (float) Font size in points. The default value is the current size.
  4522. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4523. * @return mixed int total string length or array of characted widths
  4524. * @author Nicola Asuni
  4525. * @public
  4526. * @since 1.2
  4527. */
  4528. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4529. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4530. }
  4531. /**
  4532. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4533. * @param $sa (string) The array of chars whose total length is to be computed
  4534. * @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.
  4535. * @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.
  4536. * @param $fontsize (float) Font size in points. The default value is the current size.
  4537. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4538. * @return mixed int total string length or array of characted widths
  4539. * @author Nicola Asuni
  4540. * @public
  4541. * @since 2.4.000 (2008-03-06)
  4542. */
  4543. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4544. // store current values
  4545. if (!$this->empty_string($fontname)) {
  4546. $prev_FontFamily = $this->FontFamily;
  4547. $prev_FontStyle = $this->FontStyle;
  4548. $prev_FontSizePt = $this->FontSizePt;
  4549. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4550. }
  4551. // convert UTF-8 array to Latin1 if required
  4552. $sa = $this->UTF8ArrToLatin1($sa);
  4553. $w = 0; // total width
  4554. $wa = array(); // array of characters widths
  4555. foreach ($sa as $ck => $char) {
  4556. // character width
  4557. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4558. $wa[] = $cw;
  4559. $w += $cw;
  4560. }
  4561. // restore previous values
  4562. if (!$this->empty_string($fontname)) {
  4563. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4564. }
  4565. if ($getarray) {
  4566. return $wa;
  4567. }
  4568. return $w;
  4569. }
  4570. /**
  4571. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4572. * @param $char (int) The char code whose length is to be returned
  4573. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4574. * @return float char width
  4575. * @author Nicola Asuni
  4576. * @public
  4577. * @since 2.4.000 (2008-03-06)
  4578. */
  4579. public function GetCharWidth($char, $notlast=true) {
  4580. // get raw width
  4581. $chw = $this->getRawCharWidth($char);
  4582. if (($this->font_spacing != 0) AND $notlast) {
  4583. // increase/decrease font spacing
  4584. $chw += $this->font_spacing;
  4585. }
  4586. if ($this->font_stretching != 100) {
  4587. // fixed stretching mode
  4588. $chw *= ($this->font_stretching / 100);
  4589. }
  4590. return $chw;
  4591. }
  4592. /**
  4593. * Returns the length of the char in user unit for the current font.
  4594. * @param $char (int) The char code whose length is to be returned
  4595. * @return float char width
  4596. * @author Nicola Asuni
  4597. * @public
  4598. * @since 5.9.000 (2010-09-28)
  4599. */
  4600. public function getRawCharWidth($char) {
  4601. if ($char == 173) {
  4602. // SHY character will not be printed
  4603. return (0);
  4604. }
  4605. if (isset($this->CurrentFont['cw'][$char])) {
  4606. $w = $this->CurrentFont['cw'][$char];
  4607. } elseif (isset($this->CurrentFont['dw'])) {
  4608. // default width
  4609. $w = $this->CurrentFont['dw'];
  4610. } elseif (isset($this->CurrentFont['cw'][32])) {
  4611. // default width
  4612. $w = $this->CurrentFont['cw'][32];
  4613. } else {
  4614. $w = 600;
  4615. }
  4616. return ($w * $this->FontSize / 1000);
  4617. }
  4618. /**
  4619. * Returns the numbero of characters in a string.
  4620. * @param $s (string) The input string.
  4621. * @return int number of characters
  4622. * @public
  4623. * @since 2.0.0001 (2008-01-07)
  4624. */
  4625. public function GetNumChars($s) {
  4626. if ($this->isUnicodeFont()) {
  4627. return count($this->UTF8StringToArray($s));
  4628. }
  4629. return strlen($s);
  4630. }
  4631. /**
  4632. * Fill the list of available fonts ($this->fontlist).
  4633. * @protected
  4634. * @since 4.0.013 (2008-07-28)
  4635. */
  4636. protected function getFontsList() {
  4637. $fontsdir = opendir($this->_getfontpath());
  4638. while (($file = readdir($fontsdir)) !== false) {
  4639. if (substr($file, -4) == '.php') {
  4640. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4641. }
  4642. }
  4643. closedir($fontsdir);
  4644. }
  4645. /**
  4646. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4647. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4648. * 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.
  4649. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4650. * @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>
  4651. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4652. * @return array containing the font data, or false in case of error.
  4653. * @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.
  4654. * @public
  4655. * @since 1.5
  4656. * @see SetFont(), setFontSubsetting()
  4657. */
  4658. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4659. if ($subset === 'default') {
  4660. $subset = $this->font_subsetting;
  4661. }
  4662. if ($this->pdfa_mode) {
  4663. $subset = false;
  4664. }
  4665. if ($this->empty_string($family)) {
  4666. if (!$this->empty_string($this->FontFamily)) {
  4667. $family = $this->FontFamily;
  4668. } else {
  4669. $this->Error('Empty font family');
  4670. }
  4671. }
  4672. // move embedded styles on $style
  4673. if (substr($family, -1) == 'I') {
  4674. $style .= 'I';
  4675. $family = substr($family, 0, -1);
  4676. }
  4677. if (substr($family, -1) == 'B') {
  4678. $style .= 'B';
  4679. $family = substr($family, 0, -1);
  4680. }
  4681. // normalize family name
  4682. $family = strtolower($family);
  4683. if ((!$this->isunicode) AND ($family == 'arial')) {
  4684. $family = 'helvetica';
  4685. }
  4686. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4687. $style = '';
  4688. }
  4689. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4690. // all fonts must be embedded
  4691. $family = 'pdfa'.$family;
  4692. }
  4693. $tempstyle = strtoupper($style);
  4694. $style = '';
  4695. // underline
  4696. if (strpos($tempstyle, 'U') !== false) {
  4697. $this->underline = true;
  4698. } else {
  4699. $this->underline = false;
  4700. }
  4701. // line-through (deleted)
  4702. if (strpos($tempstyle, 'D') !== false) {
  4703. $this->linethrough = true;
  4704. } else {
  4705. $this->linethrough = false;
  4706. }
  4707. // overline
  4708. if (strpos($tempstyle, 'O') !== false) {
  4709. $this->overline = true;
  4710. } else {
  4711. $this->overline = false;
  4712. }
  4713. // bold
  4714. if (strpos($tempstyle, 'B') !== false) {
  4715. $style .= 'B';
  4716. }
  4717. // oblique
  4718. if (strpos($tempstyle, 'I') !== false) {
  4719. $style .= 'I';
  4720. }
  4721. $bistyle = $style;
  4722. $fontkey = $family.$style;
  4723. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4724. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4725. // check if the font has been already added
  4726. $fb = $this->getFontBuffer($fontkey);
  4727. if ($fb !== false) {
  4728. if ($this->inxobj) {
  4729. // we are inside an XObject template
  4730. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4731. }
  4732. return $fontdata;
  4733. }
  4734. if (isset($type)) {
  4735. unset($type);
  4736. }
  4737. if (isset($cw)) {
  4738. unset($cw);
  4739. }
  4740. // get specified font directory (if any)
  4741. $fontdir = false;
  4742. if (!$this->empty_string($fontfile)) {
  4743. $fontdir = dirname($fontfile);
  4744. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4745. $fontdir = '';
  4746. } else {
  4747. $fontdir .= '/';
  4748. }
  4749. }
  4750. $missing_style = false; // true when the font style variation is missing
  4751. // search and include font file
  4752. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4753. // build a standard filenames for specified font
  4754. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4755. // search files on various directories
  4756. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4757. $fontfile = $fontdir.$tmp_fontfile;
  4758. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4759. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4760. } elseif (file_exists($tmp_fontfile)) {
  4761. $fontfile = $tmp_fontfile;
  4762. } elseif (!$this->empty_string($style)) {
  4763. $missing_style = true;
  4764. // try to remove the style part
  4765. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4766. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4767. $fontfile = $fontdir.$tmp_fontfile;
  4768. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4769. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4770. } else {
  4771. $fontfile = $tmp_fontfile;
  4772. }
  4773. }
  4774. }
  4775. // include font file
  4776. if (file_exists($fontfile)) {
  4777. include($fontfile);
  4778. } else {
  4779. $this->Error('Could not include font definition file: '.$family.'');
  4780. }
  4781. // check font parameters
  4782. if ((!isset($type)) OR (!isset($cw))) {
  4783. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4784. }
  4785. // SET default parameters
  4786. if (!isset($file) OR $this->empty_string($file)) {
  4787. $file = '';
  4788. }
  4789. if (!isset($enc) OR $this->empty_string($enc)) {
  4790. $enc = '';
  4791. }
  4792. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4793. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4794. $cidinfo['uni2cid'] = array();
  4795. }
  4796. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4797. $ctg = '';
  4798. }
  4799. if (!isset($desc) OR $this->empty_string($desc)) {
  4800. $desc = array();
  4801. }
  4802. if (!isset($up) OR $this->empty_string($up)) {
  4803. $up = -100;
  4804. }
  4805. if (!isset($ut) OR $this->empty_string($ut)) {
  4806. $ut = 50;
  4807. }
  4808. if (!isset($cw) OR $this->empty_string($cw)) {
  4809. $cw = array();
  4810. }
  4811. if (!isset($dw) OR $this->empty_string($dw)) {
  4812. // set default width
  4813. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4814. $dw = $desc['MissingWidth'];
  4815. } elseif (isset($cw[32])) {
  4816. $dw = $cw[32];
  4817. } else {
  4818. $dw = 600;
  4819. }
  4820. }
  4821. ++$this->numfonts;
  4822. if ($type == 'core') {
  4823. $name = $this->CoreFonts[$fontkey];
  4824. $subset = false;
  4825. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4826. $subset = false;
  4827. } elseif ($type == 'TrueTypeUnicode') {
  4828. $enc = 'Identity-H';
  4829. } elseif ($type == 'cidfont0') {
  4830. if ($this->pdfa_mode) {
  4831. $this->Error('All fonts must be embedded in PDF/A mode!');
  4832. }
  4833. } else {
  4834. $this->Error('Unknow font type: '.$type.'');
  4835. }
  4836. // set name if unset
  4837. if (!isset($name) OR empty($name)) {
  4838. $name = $fontkey;
  4839. }
  4840. // create artificial font style variations if missing (only works with non-embedded fonts)
  4841. if (($type != 'core') AND $missing_style) {
  4842. // style variations
  4843. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4844. $name .= $styles[$bistyle];
  4845. // artificial bold
  4846. if (strpos($bistyle, 'B') !== false) {
  4847. if (isset($desc['StemV'])) {
  4848. // from normal to bold
  4849. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4850. } else {
  4851. // bold
  4852. $desc['StemV'] = 123;
  4853. }
  4854. }
  4855. // artificial italic
  4856. if (strpos($bistyle, 'I') !== false) {
  4857. if (isset($desc['ItalicAngle'])) {
  4858. $desc['ItalicAngle'] -= 11;
  4859. } else {
  4860. $desc['ItalicAngle'] = -11;
  4861. }
  4862. if (isset($desc['Flags'])) {
  4863. $desc['Flags'] |= 64; //bit 7
  4864. } else {
  4865. $desc['Flags'] = 64;
  4866. }
  4867. }
  4868. }
  4869. // initialize subsetchars to contain default ASCII values (0-255)
  4870. $subsetchars = array_fill(0, 256, true);
  4871. $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));
  4872. if ($this->inxobj) {
  4873. // we are inside an XObject template
  4874. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4875. }
  4876. if (isset($diff) AND (!empty($diff))) {
  4877. //Search existing encodings
  4878. $d = 0;
  4879. $nb = count($this->diffs);
  4880. for ($i=1; $i <= $nb; ++$i) {
  4881. if ($this->diffs[$i] == $diff) {
  4882. $d = $i;
  4883. break;
  4884. }
  4885. }
  4886. if ($d == 0) {
  4887. $d = $nb + 1;
  4888. $this->diffs[$d] = $diff;
  4889. }
  4890. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4891. }
  4892. if (!$this->empty_string($file)) {
  4893. if (!isset($this->FontFiles[$file])) {
  4894. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4895. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4896. } elseif ($type != 'core') {
  4897. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4898. }
  4899. } else {
  4900. // update fontkeys that are sharing this font file
  4901. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4902. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4903. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4904. }
  4905. }
  4906. }
  4907. return $fontdata;
  4908. }
  4909. /**
  4910. * Sets the font used to print character strings.
  4911. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4912. * The method can be called before the first page is created and the font is retained from page to page.
  4913. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4914. * 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 />
  4915. * @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.
  4916. * @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.
  4917. * @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
  4918. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4919. * @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.
  4920. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4921. * @author Nicola Asuni
  4922. * @public
  4923. * @since 1.0
  4924. * @see AddFont(), SetFontSize()
  4925. */
  4926. public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4927. //Select a font; size given in points
  4928. if ($size === null) {
  4929. $size = $this->FontSizePt;
  4930. }
  4931. if ($size < 0) {
  4932. $size = 0;
  4933. }
  4934. // try to add font (if not already added)
  4935. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4936. $this->FontFamily = $fontdata['family'];
  4937. $this->FontStyle = $fontdata['style'];
  4938. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4939. $this->SetFontSize($size, $out);
  4940. }
  4941. /**
  4942. * Defines the size of the current font.
  4943. * @param $size (float) The font size in points.
  4944. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4945. * @public
  4946. * @since 1.0
  4947. * @see SetFont()
  4948. */
  4949. public function SetFontSize($size, $out=true) {
  4950. // font size in points
  4951. $this->FontSizePt = $size;
  4952. // font size in user units
  4953. $this->FontSize = $size / $this->k;
  4954. // calculate some font metrics
  4955. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4956. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4957. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4958. } else {
  4959. $font_height = $size * 1.219;
  4960. }
  4961. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4962. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4963. }
  4964. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4965. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4966. }
  4967. if (!isset($font_ascent) AND !isset($font_descent)) {
  4968. // core font
  4969. $font_ascent = 0.76 * $font_height;
  4970. $font_descent = $font_height - $font_ascent;
  4971. } elseif (!isset($font_descent)) {
  4972. $font_descent = $font_height - $font_ascent;
  4973. } elseif (!isset($font_ascent)) {
  4974. $font_ascent = $font_height - $font_descent;
  4975. }
  4976. $this->FontAscent = ($font_ascent / $this->k);
  4977. $this->FontDescent = ($font_descent / $this->k);
  4978. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4979. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4980. }
  4981. }
  4982. /**
  4983. * Returns the bounding box of the current font in user units.
  4984. * @return array
  4985. * @public
  4986. * @since 5.9.152 (2012-03-23)
  4987. */
  4988. public function getFontBBox() {
  4989. $result = array();
  4990. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4991. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4992. foreach ($bbox as $v) {
  4993. $result[] = (intval($v) * $this->FontSize / 1000);
  4994. }
  4995. } else {
  4996. // Find max width
  4997. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  4998. $maxw = (intval($this->CurrentFont['desc']['MaxWidth']) * $this->FontSize / 1000);
  4999. } else {
  5000. $maxw = 0;
  5001. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  5002. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  5003. }
  5004. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  5005. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  5006. }
  5007. if (isset($this->CurrentFont['dw'])) {
  5008. $maxw = max($maxw, $this->CurrentFont['dw']);
  5009. }
  5010. foreach ($this->CurrentFont['cw'] as $char => $w) {
  5011. $maxw = max($maxw, $w);
  5012. }
  5013. if ($maxw == 0) {
  5014. $maxw = 600;
  5015. }
  5016. $maxw = ($maxw * $this->FontSize / 1000);
  5017. }
  5018. $result = array(0, -$this->FontDescent, $maxw, $this->FontAscent);
  5019. }
  5020. return $result;
  5021. }
  5022. /**
  5023. * Return the font descent value
  5024. * @param $font (string) font name
  5025. * @param $style (string) font style
  5026. * @param $size (float) The size (in points)
  5027. * @return int font descent
  5028. * @public
  5029. * @author Nicola Asuni
  5030. * @since 4.9.003 (2010-03-30)
  5031. */
  5032. public function getFontDescent($font, $style='', $size=0) {
  5033. $fontdata = $this->AddFont($font, $style);
  5034. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5035. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  5036. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  5037. } else {
  5038. $descent = 1.219 * 0.24 * $size;
  5039. }
  5040. return ($descent / $this->k);
  5041. }
  5042. /**
  5043. * Return the font ascent value
  5044. * @param $font (string) font name
  5045. * @param $style (string) font style
  5046. * @param $size (float) The size (in points)
  5047. * @return int font ascent
  5048. * @public
  5049. * @author Nicola Asuni
  5050. * @since 4.9.003 (2010-03-30)
  5051. */
  5052. public function getFontAscent($font, $style='', $size=0) {
  5053. $fontdata = $this->AddFont($font, $style);
  5054. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5055. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  5056. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  5057. } else {
  5058. $ascent = 1.219 * 0.76 * $size;
  5059. }
  5060. return ($ascent / $this->k);
  5061. }
  5062. /**
  5063. * Return the font descent value
  5064. * @param $char (mixed) Character to check (integer value or string)
  5065. * @param $font (string) Font name (family name).
  5066. * @param $style (string) Font style.
  5067. * @return (boolean) true if the char is defined, false otherwise.
  5068. * @public
  5069. * @since 5.9.153 (2012-03-28)
  5070. */
  5071. public function isCharDefined($char, $font='', $style='') {
  5072. if (is_string($char)) {
  5073. // get character code
  5074. $char = $this->UTF8StringToArray($char);
  5075. $char = $char[0];
  5076. }
  5077. if ($this->empty_string($font)) {
  5078. $font = $this->FontFamily;
  5079. }
  5080. $fontdata = $this->AddFont($font, $style);
  5081. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5082. return (isset($fontinfo['cw'][intval($char)]));
  5083. }
  5084. /**
  5085. * Replace missing font characters on selected font with specified substitutions.
  5086. * @param $text (string) Text to process.
  5087. * @param $font (string) Font name (family name).
  5088. * @param $style (string) Font style.
  5089. * @param $subs (array) Array of possible character substitutions. The key is the character to check (integer value) and the value is a single intege value or an array of possible substitutes.
  5090. * @return (string) Processed text.
  5091. * @public
  5092. * @since 5.9.153 (2012-03-28)
  5093. */
  5094. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  5095. if (empty($subs)) {
  5096. return $text;
  5097. }
  5098. if ($this->empty_string($font)) {
  5099. $font = $this->FontFamily;
  5100. }
  5101. $fontdata = $this->AddFont($font, $style);
  5102. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5103. $uniarr = $this->UTF8StringToArray($text);
  5104. foreach ($uniarr as $k => $chr) {
  5105. if (!isset($fontinfo['cw'][$chr])) {
  5106. // this character is missing on the selected font
  5107. if (isset($subs[$chr])) {
  5108. // we have available substitutions
  5109. if (is_array($subs[$chr])) {
  5110. foreach($subs[$chr] as $s) {
  5111. if (isset($fontinfo['cw'][$s])) {
  5112. $uniarr[$k] = $s;
  5113. break;
  5114. }
  5115. }
  5116. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  5117. $uniarr[$k] = $subs[$chr];
  5118. }
  5119. }
  5120. }
  5121. }
  5122. return $this->UniArrSubString($this->UTF8ArrayToUniArray($uniarr));
  5123. }
  5124. /**
  5125. * Defines the default monospaced font.
  5126. * @param $font (string) Font name.
  5127. * @public
  5128. * @since 4.5.025
  5129. */
  5130. public function SetDefaultMonospacedFont($font) {
  5131. $this->default_monospaced_font = $font;
  5132. }
  5133. /**
  5134. * 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 />
  5135. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  5136. * @public
  5137. * @since 1.5
  5138. * @see Cell(), Write(), Image(), Link(), SetLink()
  5139. */
  5140. public function AddLink() {
  5141. //Create a new internal link
  5142. $n = count($this->links) + 1;
  5143. $this->links[$n] = array(0, 0);
  5144. return $n;
  5145. }
  5146. /**
  5147. * Defines the page and position a link points to.
  5148. * @param $link (int) The link identifier returned by AddLink()
  5149. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  5150. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  5151. * @public
  5152. * @since 1.5
  5153. * @see AddLink()
  5154. */
  5155. public function SetLink($link, $y=0, $page=-1) {
  5156. if ($y == -1) {
  5157. $y = $this->y;
  5158. }
  5159. if ($page == -1) {
  5160. $page = $this->page;
  5161. }
  5162. $this->links[$link] = array($page, $y);
  5163. }
  5164. /**
  5165. * Puts a link on a rectangular area of the page.
  5166. * 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.
  5167. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5168. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5169. * @param $w (float) Width of the rectangle
  5170. * @param $h (float) Height of the rectangle
  5171. * @param $link (mixed) URL or identifier returned by AddLink()
  5172. * @param $spaces (int) number of spaces on the text to link
  5173. * @public
  5174. * @since 1.5
  5175. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  5176. */
  5177. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5178. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5179. }
  5180. /**
  5181. * Puts a markup annotation on a rectangular area of the page.
  5182. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5183. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5184. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5185. * @param $w (float) Width of the rectangle
  5186. * @param $h (float) Height of the rectangle
  5187. * @param $text (string) annotation text or alternate content
  5188. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5189. * @param $spaces (int) number of spaces on the text to link
  5190. * @public
  5191. * @since 4.0.018 (2008-08-06)
  5192. */
  5193. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5194. if ($this->inxobj) {
  5195. // store parameters for later use on template
  5196. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5197. return;
  5198. }
  5199. if ($x === '') {
  5200. $x = $this->x;
  5201. }
  5202. if ($y === '') {
  5203. $y = $this->y;
  5204. }
  5205. // check page for no-write regions and adapt page margins if necessary
  5206. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5207. // recalculate coordinates to account for graphic transformations
  5208. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5209. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5210. $maxid = count($this->transfmatrix[$i]) - 1;
  5211. for ($j=$maxid; $j >= 0; --$j) {
  5212. $ctm = $this->transfmatrix[$i][$j];
  5213. if (isset($ctm['a'])) {
  5214. $x = $x * $this->k;
  5215. $y = ($this->h - $y) * $this->k;
  5216. $w = $w * $this->k;
  5217. $h = $h * $this->k;
  5218. // top left
  5219. $xt = $x;
  5220. $yt = $y;
  5221. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5222. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5223. // top right
  5224. $xt = $x + $w;
  5225. $yt = $y;
  5226. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5227. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5228. // bottom left
  5229. $xt = $x;
  5230. $yt = $y - $h;
  5231. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5232. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5233. // bottom right
  5234. $xt = $x + $w;
  5235. $yt = $y - $h;
  5236. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5237. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5238. // new coordinates (rectangle area)
  5239. $x = min($x1, $x2, $x3, $x4);
  5240. $y = max($y1, $y2, $y3, $y4);
  5241. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5242. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5243. $x = $x / $this->k;
  5244. $y = $this->h - ($y / $this->k);
  5245. }
  5246. }
  5247. }
  5248. }
  5249. if ($this->page <= 0) {
  5250. $page = 1;
  5251. } else {
  5252. $page = $this->page;
  5253. }
  5254. if (!isset($this->PageAnnots[$page])) {
  5255. $this->PageAnnots[$page] = array();
  5256. }
  5257. ++$this->n;
  5258. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5259. if (!$this->pdfa_mode) {
  5260. 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'])]))) {
  5261. ++$this->n;
  5262. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5263. }
  5264. }
  5265. // Add widgets annotation's icons
  5266. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5267. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5268. }
  5269. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5270. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5271. }
  5272. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5273. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5274. }
  5275. }
  5276. /**
  5277. * Embedd the attached files.
  5278. * @since 4.4.000 (2008-12-07)
  5279. * @protected
  5280. * @see Annotation()
  5281. */
  5282. protected function _putEmbeddedFiles() {
  5283. if ($this->pdfa_mode) {
  5284. // embedded files are not allowed in PDF/A mode
  5285. return;
  5286. }
  5287. reset($this->embeddedfiles);
  5288. foreach ($this->embeddedfiles as $filename => $filedata) {
  5289. $data = file_get_contents($filedata['file']);
  5290. $filter = '';
  5291. if ($this->compress) {
  5292. $data = gzcompress($data);
  5293. $filter = ' /Filter /FlateDecode';
  5294. }
  5295. $stream = $this->_getrawstream($data, $filedata['n']);
  5296. $out = $this->_getobj($filedata['n'])."\n";
  5297. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5298. $out .= ' stream'."\n".$stream."\n".'endstream';
  5299. $out .= "\n".'endobj';
  5300. $this->_out($out);
  5301. }
  5302. }
  5303. /**
  5304. * Prints a text cell at the specified position.
  5305. * This method allows to place a string precisely on the page.
  5306. * @param $x (float) Abscissa of the cell origin
  5307. * @param $y (float) Ordinate of the cell origin
  5308. * @param $txt (string) String to print
  5309. * @param $fstroke (int) outline size in user units (false = disable)
  5310. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5311. * @param $ffill (boolean) if true fills the text
  5312. * @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)))
  5313. * @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.
  5314. * @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>
  5315. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5316. * @param $link (mixed) URL or identifier returned by AddLink().
  5317. * @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.
  5318. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5319. * @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>
  5320. * @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>
  5321. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5322. * @public
  5323. * @since 1.0
  5324. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5325. */
  5326. 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) {
  5327. $textrendermode = $this->textrendermode;
  5328. $textstrokewidth = $this->textstrokewidth;
  5329. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5330. $this->SetXY($x, $y, $rtloff);
  5331. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5332. // restore previous rendering mode
  5333. $this->textrendermode = $textrendermode;
  5334. $this->textstrokewidth = $textstrokewidth;
  5335. }
  5336. /**
  5337. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5338. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5339. * This method is called automatically and should not be called directly by the application.
  5340. * @return boolean
  5341. * @public
  5342. * @since 1.4
  5343. * @see SetAutoPageBreak()
  5344. */
  5345. public function AcceptPageBreak() {
  5346. if ($this->num_columns > 1) {
  5347. // multi column mode
  5348. if ($this->current_column < ($this->num_columns - 1)) {
  5349. // go to next column
  5350. $this->selectColumn($this->current_column + 1);
  5351. } elseif ($this->AutoPageBreak) {
  5352. // add a new page
  5353. $this->AddPage();
  5354. // set first column
  5355. $this->selectColumn(0);
  5356. }
  5357. // avoid page breaking from checkPageBreak()
  5358. return false;
  5359. }
  5360. return $this->AutoPageBreak;
  5361. }
  5362. /**
  5363. * Add page if needed.
  5364. * @param $h (float) Cell height. Default value: 0.
  5365. * @param $y (mixed) starting y position, leave empty for current position.
  5366. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5367. * @return boolean true in case of page break, false otherwise.
  5368. * @since 3.2.000 (2008-07-01)
  5369. * @protected
  5370. */
  5371. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5372. if ($this->empty_string($y)) {
  5373. $y = $this->y;
  5374. }
  5375. $current_page = $this->page;
  5376. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5377. if ($addpage) {
  5378. //Automatic page break
  5379. $x = $this->x;
  5380. $this->AddPage($this->CurOrientation);
  5381. $this->y = $this->tMargin;
  5382. $oldpage = $this->page - 1;
  5383. if ($this->rtl) {
  5384. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5385. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5386. } else {
  5387. $this->x = $x;
  5388. }
  5389. } else {
  5390. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5391. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5392. } else {
  5393. $this->x = $x;
  5394. }
  5395. }
  5396. }
  5397. return true;
  5398. }
  5399. if ($current_page != $this->page) {
  5400. // account for columns mode
  5401. return true;
  5402. }
  5403. return false;
  5404. }
  5405. /**
  5406. * Removes SHY characters from text.
  5407. * Unicode Data:<ul>
  5408. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5409. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5410. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5411. * <li>HTML Entity (named): "&amp;shy;"</li>
  5412. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5413. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5414. * <li>UTF-8 character: chr(194).chr(173)</li>
  5415. * </ul>
  5416. * @param $txt (string) input string
  5417. * @return string without SHY characters.
  5418. * @public
  5419. * @since (4.5.019) 2009-02-28
  5420. */
  5421. public function removeSHY($txt='') {
  5422. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5423. if (!$this->isunicode) {
  5424. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5425. }
  5426. return $txt;
  5427. }
  5428. /**
  5429. * 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 />
  5430. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5431. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5432. * @param $h (float) Cell height. Default value: 0.
  5433. * @param $txt (string) String to print. Default value: empty string.
  5434. * @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)))
  5435. * @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.
  5436. * @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>
  5437. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5438. * @param $link (mixed) URL or identifier returned by AddLink().
  5439. * @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.
  5440. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5441. * @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>
  5442. * @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>
  5443. * @public
  5444. * @since 1.0
  5445. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5446. */
  5447. 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') {
  5448. $prev_cell_margin = $this->cell_margin;
  5449. $prev_cell_padding = $this->cell_padding;
  5450. $this->adjustCellPadding($border);
  5451. if (!$ignore_min_height) {
  5452. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5453. if ($h < $min_cell_height) {
  5454. $h = $min_cell_height;
  5455. }
  5456. }
  5457. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5458. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5459. $this->cell_padding = $prev_cell_padding;
  5460. $this->cell_margin = $prev_cell_margin;
  5461. }
  5462. /**
  5463. * 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 />
  5464. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5465. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5466. * @param $h (float) Cell height. Default value: 0.
  5467. * @param $txt (string) String to print. Default value: empty string.
  5468. * @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)))
  5469. * @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.
  5470. * @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>
  5471. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5472. * @param $link (mixed) URL or identifier returned by AddLink().
  5473. * @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.
  5474. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5475. * @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>
  5476. * @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>
  5477. * @return string containing cell code
  5478. * @protected
  5479. * @since 1.0
  5480. * @see Cell()
  5481. */
  5482. 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') {
  5483. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5484. $txt = str_replace($this->unichr(160), ' ', $txt);
  5485. $prev_cell_margin = $this->cell_margin;
  5486. $prev_cell_padding = $this->cell_padding;
  5487. $txt = $this->removeSHY($txt);
  5488. $rs = ''; //string to be returned
  5489. $this->adjustCellPadding($border);
  5490. if (!$ignore_min_height) {
  5491. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5492. if ($h < $min_cell_height) {
  5493. $h = $min_cell_height;
  5494. }
  5495. }
  5496. $k = $this->k;
  5497. // check page for no-write regions and adapt page margins if necessary
  5498. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5499. if ($this->rtl) {
  5500. $x = $this->x - $this->cell_margin['R'];
  5501. } else {
  5502. $x = $this->x + $this->cell_margin['L'];
  5503. }
  5504. $y = $this->y + $this->cell_margin['T'];
  5505. $prev_font_stretching = $this->font_stretching;
  5506. $prev_font_spacing = $this->font_spacing;
  5507. // cell vertical alignment
  5508. switch ($calign) {
  5509. case 'A': {
  5510. // font top
  5511. switch ($valign) {
  5512. case 'T': {
  5513. // top
  5514. $y -= $this->cell_padding['T'];
  5515. break;
  5516. }
  5517. case 'B': {
  5518. // bottom
  5519. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5520. break;
  5521. }
  5522. default:
  5523. case 'C':
  5524. case 'M': {
  5525. // center
  5526. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5527. break;
  5528. }
  5529. }
  5530. break;
  5531. }
  5532. case 'L': {
  5533. // font baseline
  5534. switch ($valign) {
  5535. case 'T': {
  5536. // top
  5537. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5538. break;
  5539. }
  5540. case 'B': {
  5541. // bottom
  5542. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5543. break;
  5544. }
  5545. default:
  5546. case 'C':
  5547. case 'M': {
  5548. // center
  5549. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5550. break;
  5551. }
  5552. }
  5553. break;
  5554. }
  5555. case 'D': {
  5556. // font bottom
  5557. switch ($valign) {
  5558. case 'T': {
  5559. // top
  5560. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5561. break;
  5562. }
  5563. case 'B': {
  5564. // bottom
  5565. $y -= ($h - $this->cell_padding['B']);
  5566. break;
  5567. }
  5568. default:
  5569. case 'C':
  5570. case 'M': {
  5571. // center
  5572. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5573. break;
  5574. }
  5575. }
  5576. break;
  5577. }
  5578. case 'B': {
  5579. // cell bottom
  5580. $y -= $h;
  5581. break;
  5582. }
  5583. case 'C':
  5584. case 'M': {
  5585. // cell center
  5586. $y -= ($h / 2);
  5587. break;
  5588. }
  5589. default:
  5590. case 'T': {
  5591. // cell top
  5592. break;
  5593. }
  5594. }
  5595. // text vertical alignment
  5596. switch ($valign) {
  5597. case 'T': {
  5598. // top
  5599. $yt = $y + $this->cell_padding['T'];
  5600. break;
  5601. }
  5602. case 'B': {
  5603. // bottom
  5604. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5605. break;
  5606. }
  5607. default:
  5608. case 'C':
  5609. case 'M': {
  5610. // center
  5611. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5612. break;
  5613. }
  5614. }
  5615. $basefonty = $yt + $this->FontAscent;
  5616. if ($this->empty_string($w) OR ($w <= 0)) {
  5617. if ($this->rtl) {
  5618. $w = $x - $this->lMargin;
  5619. } else {
  5620. $w = $this->w - $this->rMargin - $x;
  5621. }
  5622. }
  5623. $s = '';
  5624. // fill and borders
  5625. if (is_string($border) AND (strlen($border) == 4)) {
  5626. // full border
  5627. $border = 1;
  5628. }
  5629. if ($fill OR ($border == 1)) {
  5630. if ($fill) {
  5631. $op = ($border == 1) ? 'B' : 'f';
  5632. } else {
  5633. $op = 'S';
  5634. }
  5635. if ($this->rtl) {
  5636. $xk = (($x - $w) * $k);
  5637. } else {
  5638. $xk = ($x * $k);
  5639. }
  5640. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5641. }
  5642. // draw borders
  5643. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5644. if ($txt != '') {
  5645. $txt2 = $txt;
  5646. if ($this->isunicode) {
  5647. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5648. $txt2 = $this->UTF8ToLatin1($txt2);
  5649. } else {
  5650. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5651. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5652. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5653. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5654. // NOTE: this doesn't work with HTML justification
  5655. // Symbols that could overlap on the font top (only works in LTR)
  5656. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5657. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5658. $numchars = count($unicode); // number of chars
  5659. $unik = 0;
  5660. $uniblock = array();
  5661. $uniblock[$unik] = array();
  5662. $uniblock[$unik][] = $unicode[0];
  5663. // resolve overlapping conflicts by splitting the string in several parts
  5664. for ($i = 1; $i < $numchars; ++$i) {
  5665. // check if symbols overlaps at top
  5666. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5667. // move symbols to another array
  5668. ++$unik;
  5669. $uniblock[$unik] = array();
  5670. $uniblock[$unik][] = $unicode[$i];
  5671. ++$unik;
  5672. $uniblock[$unik] = array();
  5673. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5674. } else {
  5675. $uniblock[$unik][] = $unicode[$i];
  5676. }
  5677. }
  5678. // ---- END OF Fix for bug #2977340
  5679. }
  5680. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5681. }
  5682. }
  5683. $txt2 = $this->_escape($txt2);
  5684. // get current text width (considering general font stretching and spacing)
  5685. $txwidth = $this->GetStringWidth($txt);
  5686. $width = $txwidth;
  5687. // check for stretch mode
  5688. if ($stretch > 0) {
  5689. // calculate ratio between cell width and text width
  5690. if ($width <= 0) {
  5691. $ratio = 1;
  5692. } else {
  5693. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5694. }
  5695. // check if stretching is required
  5696. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5697. // the text will be stretched to fit cell width
  5698. if ($stretch > 2) {
  5699. // set new character spacing
  5700. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5701. } else {
  5702. // set new horizontal stretching
  5703. $this->font_stretching *= $ratio;
  5704. }
  5705. // recalculate text width (the text fills the entire cell)
  5706. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5707. // reset alignment
  5708. $align = '';
  5709. }
  5710. }
  5711. if ($this->font_stretching != 100) {
  5712. // apply font stretching
  5713. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  5714. }
  5715. if ($this->font_spacing != 0) {
  5716. // increase/decrease font spacing
  5717. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  5718. }
  5719. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5720. $s .= 'q '.$this->TextColor.' ';
  5721. }
  5722. // rendering mode
  5723. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, $this->textstrokewidth);
  5724. // count number of spaces
  5725. $ns = substr_count($txt, chr(32));
  5726. // Justification
  5727. $spacewidth = 0;
  5728. if (($align == 'J') AND ($ns > 0)) {
  5729. if ($this->isUnicodeFont()) {
  5730. // get string width without spaces
  5731. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5732. // calculate average space width
  5733. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5734. if ($this->font_stretching != 100) {
  5735. // word spacing is affected by stretching
  5736. $spacewidth /= ($this->font_stretching / 100);
  5737. }
  5738. // set word position to be used with TJ operator
  5739. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  5740. $unicode_justification = true;
  5741. } else {
  5742. // get string width
  5743. $width = $txwidth;
  5744. // new space width
  5745. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5746. if ($this->font_stretching != 100) {
  5747. // word spacing (Tw) is affected by stretching
  5748. $spacewidth /= ($this->font_stretching / 100);
  5749. }
  5750. // set word spacing
  5751. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5752. }
  5753. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5754. }
  5755. // replace carriage return characters
  5756. $txt2 = str_replace("\r", ' ', $txt2);
  5757. switch ($align) {
  5758. case 'C': {
  5759. $dx = ($w - $width) / 2;
  5760. break;
  5761. }
  5762. case 'R': {
  5763. if ($this->rtl) {
  5764. $dx = $this->cell_padding['R'];
  5765. } else {
  5766. $dx = $w - $width - $this->cell_padding['R'];
  5767. }
  5768. break;
  5769. }
  5770. case 'L': {
  5771. if ($this->rtl) {
  5772. $dx = $w - $width - $this->cell_padding['L'];
  5773. } else {
  5774. $dx = $this->cell_padding['L'];
  5775. }
  5776. break;
  5777. }
  5778. case 'J':
  5779. default: {
  5780. if ($this->rtl) {
  5781. $dx = $this->cell_padding['R'];
  5782. } else {
  5783. $dx = $this->cell_padding['L'];
  5784. }
  5785. break;
  5786. }
  5787. }
  5788. if ($this->rtl) {
  5789. $xdx = $x - $dx - $width;
  5790. } else {
  5791. $xdx = $x + $dx;
  5792. }
  5793. $xdk = $xdx * $k;
  5794. // print text
  5795. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5796. if (isset($uniblock)) {
  5797. // print overlapping characters as separate string
  5798. $xshift = 0; // horizontal shift
  5799. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5800. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5801. foreach ($uniblock as $uk => $uniarr) {
  5802. if (($uk % 2) == 0) {
  5803. // x space to skip
  5804. if ($spacewidth != 0) {
  5805. // justification shift
  5806. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5807. }
  5808. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5809. } else {
  5810. // character to print
  5811. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5812. $topchr = $this->_escape($topchr);
  5813. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5814. }
  5815. }
  5816. }
  5817. if ($this->underline) {
  5818. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5819. }
  5820. if ($this->linethrough) {
  5821. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5822. }
  5823. if ($this->overline) {
  5824. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5825. }
  5826. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5827. $s .= ' Q';
  5828. }
  5829. if ($link) {
  5830. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5831. }
  5832. }
  5833. // output cell
  5834. if ($s) {
  5835. // output cell
  5836. $rs .= $s;
  5837. if ($this->font_spacing != 0) {
  5838. // reset font spacing mode
  5839. $rs .= ' BT 0 Tc ET';
  5840. }
  5841. if ($this->font_stretching != 100) {
  5842. // reset font stretching mode
  5843. $rs .= ' BT 100 Tz ET';
  5844. }
  5845. }
  5846. // reset word spacing
  5847. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5848. $rs .= ' BT 0 Tw ET';
  5849. }
  5850. // reset stretching and spacing
  5851. $this->font_stretching = $prev_font_stretching;
  5852. $this->font_spacing = $prev_font_spacing;
  5853. $this->lasth = $h;
  5854. if ($ln > 0) {
  5855. //Go to the beginning of the next line
  5856. $this->y = $y + $h + $this->cell_margin['B'];
  5857. if ($ln == 1) {
  5858. if ($this->rtl) {
  5859. $this->x = $this->w - $this->rMargin;
  5860. } else {
  5861. $this->x = $this->lMargin;
  5862. }
  5863. }
  5864. } else {
  5865. // go left or right by case
  5866. if ($this->rtl) {
  5867. $this->x = $x - $w - $this->cell_margin['L'];
  5868. } else {
  5869. $this->x = $x + $w + $this->cell_margin['R'];
  5870. }
  5871. }
  5872. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5873. $rs = $gstyles.$rs;
  5874. $this->cell_padding = $prev_cell_padding;
  5875. $this->cell_margin = $prev_cell_margin;
  5876. return $rs;
  5877. }
  5878. /**
  5879. * Returns the code to draw the cell border
  5880. * @param $x (float) X coordinate.
  5881. * @param $y (float) Y coordinate.
  5882. * @param $w (float) Cell width.
  5883. * @param $h (float) Cell height.
  5884. * @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)))
  5885. * @return string containing cell border code
  5886. * @protected
  5887. * @see SetLineStyle()
  5888. * @since 5.7.000 (2010-08-02)
  5889. */
  5890. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5891. $s = ''; // string to be returned
  5892. if (empty($brd)) {
  5893. return $s;
  5894. }
  5895. if ($brd == 1) {
  5896. $brd = array('LRTB' => true);
  5897. }
  5898. // calculate coordinates for border
  5899. $k = $this->k;
  5900. if ($this->rtl) {
  5901. $xeL = ($x - $w) * $k;
  5902. $xeR = $x * $k;
  5903. } else {
  5904. $xeL = $x * $k;
  5905. $xeR = ($x + $w) * $k;
  5906. }
  5907. $yeL = (($this->h - ($y + $h)) * $k);
  5908. $yeT = (($this->h - $y) * $k);
  5909. $xeT = $xeL;
  5910. $xeB = $xeR;
  5911. $yeR = $yeT;
  5912. $yeB = $yeL;
  5913. if (is_string($brd)) {
  5914. // convert string to array
  5915. $slen = strlen($brd);
  5916. $newbrd = array();
  5917. for ($i = 0; $i < $slen; ++$i) {
  5918. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5919. }
  5920. $brd = $newbrd;
  5921. }
  5922. if (isset($brd['mode'])) {
  5923. $mode = $brd['mode'];
  5924. unset($brd['mode']);
  5925. } else {
  5926. $mode = 'normal';
  5927. }
  5928. foreach ($brd as $border => $style) {
  5929. if (is_array($style) AND !empty($style)) {
  5930. // apply border style
  5931. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5932. $s .= $this->SetLineStyle($style, true)."\n";
  5933. }
  5934. switch ($mode) {
  5935. case 'ext': {
  5936. $off = (($this->LineWidth / 2) * $k);
  5937. $xL = $xeL - $off;
  5938. $xR = $xeR + $off;
  5939. $yT = $yeT + $off;
  5940. $yL = $yeL - $off;
  5941. $xT = $xL;
  5942. $xB = $xR;
  5943. $yR = $yT;
  5944. $yB = $yL;
  5945. $w += $this->LineWidth;
  5946. $h += $this->LineWidth;
  5947. break;
  5948. }
  5949. case 'int': {
  5950. $off = ($this->LineWidth / 2) * $k;
  5951. $xL = $xeL + $off;
  5952. $xR = $xeR - $off;
  5953. $yT = $yeT - $off;
  5954. $yL = $yeL + $off;
  5955. $xT = $xL;
  5956. $xB = $xR;
  5957. $yR = $yT;
  5958. $yB = $yL;
  5959. $w -= $this->LineWidth;
  5960. $h -= $this->LineWidth;
  5961. break;
  5962. }
  5963. case 'normal':
  5964. default: {
  5965. $xL = $xeL;
  5966. $xT = $xeT;
  5967. $xB = $xeB;
  5968. $xR = $xeR;
  5969. $yL = $yeL;
  5970. $yT = $yeT;
  5971. $yB = $yeB;
  5972. $yR = $yeR;
  5973. break;
  5974. }
  5975. }
  5976. // draw borders by case
  5977. if (strlen($border) == 4) {
  5978. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5979. } elseif (strlen($border) == 3) {
  5980. if (strpos($border,'B') === false) { // LTR
  5981. $s .= sprintf('%F %F m ', $xL, $yL);
  5982. $s .= sprintf('%F %F l ', $xT, $yT);
  5983. $s .= sprintf('%F %F l ', $xR, $yR);
  5984. $s .= sprintf('%F %F l ', $xB, $yB);
  5985. $s .= 'S ';
  5986. } elseif (strpos($border,'L') === false) { // TRB
  5987. $s .= sprintf('%F %F m ', $xT, $yT);
  5988. $s .= sprintf('%F %F l ', $xR, $yR);
  5989. $s .= sprintf('%F %F l ', $xB, $yB);
  5990. $s .= sprintf('%F %F l ', $xL, $yL);
  5991. $s .= 'S ';
  5992. } elseif (strpos($border,'T') === false) { // RBL
  5993. $s .= sprintf('%F %F m ', $xR, $yR);
  5994. $s .= sprintf('%F %F l ', $xB, $yB);
  5995. $s .= sprintf('%F %F l ', $xL, $yL);
  5996. $s .= sprintf('%F %F l ', $xT, $yT);
  5997. $s .= 'S ';
  5998. } elseif (strpos($border,'R') === false) { // BLT
  5999. $s .= sprintf('%F %F m ', $xB, $yB);
  6000. $s .= sprintf('%F %F l ', $xL, $yL);
  6001. $s .= sprintf('%F %F l ', $xT, $yT);
  6002. $s .= sprintf('%F %F l ', $xR, $yR);
  6003. $s .= 'S ';
  6004. }
  6005. } elseif (strlen($border) == 2) {
  6006. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  6007. $s .= sprintf('%F %F m ', $xL, $yL);
  6008. $s .= sprintf('%F %F l ', $xT, $yT);
  6009. $s .= sprintf('%F %F l ', $xR, $yR);
  6010. $s .= 'S ';
  6011. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  6012. $s .= sprintf('%F %F m ', $xT, $yT);
  6013. $s .= sprintf('%F %F l ', $xR, $yR);
  6014. $s .= sprintf('%F %F l ', $xB, $yB);
  6015. $s .= 'S ';
  6016. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  6017. $s .= sprintf('%F %F m ', $xR, $yR);
  6018. $s .= sprintf('%F %F l ', $xB, $yB);
  6019. $s .= sprintf('%F %F l ', $xL, $yL);
  6020. $s .= 'S ';
  6021. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  6022. $s .= sprintf('%F %F m ', $xB, $yB);
  6023. $s .= sprintf('%F %F l ', $xL, $yL);
  6024. $s .= sprintf('%F %F l ', $xT, $yT);
  6025. $s .= 'S ';
  6026. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  6027. $s .= sprintf('%F %F m ', $xL, $yL);
  6028. $s .= sprintf('%F %F l ', $xT, $yT);
  6029. $s .= 'S ';
  6030. $s .= sprintf('%F %F m ', $xR, $yR);
  6031. $s .= sprintf('%F %F l ', $xB, $yB);
  6032. $s .= 'S ';
  6033. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  6034. $s .= sprintf('%F %F m ', $xT, $yT);
  6035. $s .= sprintf('%F %F l ', $xR, $yR);
  6036. $s .= 'S ';
  6037. $s .= sprintf('%F %F m ', $xB, $yB);
  6038. $s .= sprintf('%F %F l ', $xL, $yL);
  6039. $s .= 'S ';
  6040. }
  6041. } else { // strlen($border) == 1
  6042. if (strpos($border,'L') !== false) { // L
  6043. $s .= sprintf('%F %F m ', $xL, $yL);
  6044. $s .= sprintf('%F %F l ', $xT, $yT);
  6045. $s .= 'S ';
  6046. } elseif (strpos($border,'T') !== false) { // T
  6047. $s .= sprintf('%F %F m ', $xT, $yT);
  6048. $s .= sprintf('%F %F l ', $xR, $yR);
  6049. $s .= 'S ';
  6050. } elseif (strpos($border,'R') !== false) { // R
  6051. $s .= sprintf('%F %F m ', $xR, $yR);
  6052. $s .= sprintf('%F %F l ', $xB, $yB);
  6053. $s .= 'S ';
  6054. } elseif (strpos($border,'B') !== false) { // B
  6055. $s .= sprintf('%F %F m ', $xB, $yB);
  6056. $s .= sprintf('%F %F l ', $xL, $yL);
  6057. $s .= 'S ';
  6058. }
  6059. }
  6060. if (is_array($style) AND !empty($style)) {
  6061. // reset border style to previous value
  6062. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  6063. }
  6064. }
  6065. return $s;
  6066. }
  6067. /**
  6068. * This method allows printing text with line breaks.
  6069. * 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 />
  6070. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  6071. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6072. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  6073. * @param $txt (string) String to print
  6074. * @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)))
  6075. * @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>
  6076. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6077. * @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>
  6078. * @param $x (float) x position in user units
  6079. * @param $y (float) y position in user units
  6080. * @param $reseth (boolean) if true reset the last cell height (default true).
  6081. * @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.
  6082. * @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.
  6083. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  6084. * @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.
  6085. * @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.
  6086. * @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).
  6087. * @return int Return the number of cells or 1 for html mode.
  6088. * @public
  6089. * @since 1.3
  6090. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  6091. */
  6092. 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) {
  6093. $prev_cell_margin = $this->cell_margin;
  6094. $prev_cell_padding = $this->cell_padding;
  6095. // adjust internal padding
  6096. $this->adjustCellPadding($border);
  6097. $mc_padding = $this->cell_padding;
  6098. $mc_margin = $this->cell_margin;
  6099. $this->cell_padding['T'] = 0;
  6100. $this->cell_padding['B'] = 0;
  6101. $this->setCellMargins(0, 0, 0, 0);
  6102. if ($this->empty_string($this->lasth) OR $reseth) {
  6103. // reset row height
  6104. $this->resetLastH();
  6105. }
  6106. if (!$this->empty_string($y)) {
  6107. $this->SetY($y);
  6108. } else {
  6109. $y = $this->GetY();
  6110. }
  6111. $resth = 0;
  6112. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  6113. // spit cell in more pages/columns
  6114. $newh = ($this->PageBreakTrigger - $y);
  6115. $resth = ($h - $newh); // cell to be printed on the next page/column
  6116. $h = $newh;
  6117. }
  6118. // get current page number
  6119. $startpage = $this->page;
  6120. // get current column
  6121. $startcolumn = $this->current_column;
  6122. if (!$this->empty_string($x)) {
  6123. $this->SetX($x);
  6124. } else {
  6125. $x = $this->GetX();
  6126. }
  6127. // check page for no-write regions and adapt page margins if necessary
  6128. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  6129. // apply margins
  6130. $oy = $y + $mc_margin['T'];
  6131. if ($this->rtl) {
  6132. $ox = $this->w - $x - $mc_margin['R'];
  6133. } else {
  6134. $ox = $x + $mc_margin['L'];
  6135. }
  6136. $this->x = $ox;
  6137. $this->y = $oy;
  6138. // set width
  6139. if ($this->empty_string($w) OR ($w <= 0)) {
  6140. if ($this->rtl) {
  6141. $w = $this->x - $this->lMargin - $mc_margin['L'];
  6142. } else {
  6143. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  6144. }
  6145. }
  6146. // store original margin values
  6147. $lMargin = $this->lMargin;
  6148. $rMargin = $this->rMargin;
  6149. if ($this->rtl) {
  6150. $this->rMargin = $this->w - $this->x;
  6151. $this->lMargin = $this->x - $w;
  6152. } else {
  6153. $this->lMargin = $this->x;
  6154. $this->rMargin = $this->w - $this->x - $w;
  6155. }
  6156. if ($autopadding) {
  6157. // add top padding
  6158. $this->y += $mc_padding['T'];
  6159. }
  6160. if ($ishtml) { // ******* Write HTML text
  6161. $this->writeHTML($txt, true, false, $reseth, true, $align);
  6162. $nl = 1;
  6163. } else { // ******* Write simple text
  6164. $prev_FontSizePt = $this->FontSizePt;
  6165. // vertical alignment
  6166. if ($maxh > 0) {
  6167. // get text height
  6168. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6169. if ($fitcell) {
  6170. // try to reduce font size to fit text on cell (use a quick search algorithm)
  6171. $fmin = 1;
  6172. $fmax = $this->FontSizePt;
  6173. $prev_text_height = $text_height;
  6174. $maxit = 100; // max number of iterations
  6175. while ($maxit > 0) {
  6176. $fmid = (($fmax + $fmin) / 2);
  6177. $this->SetFontSize($fmid, false);
  6178. $this->resetLastH();
  6179. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6180. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6181. break;
  6182. } elseif ($text_height < $maxh) {
  6183. $fmin = $fmid;
  6184. } else {
  6185. $fmax = $fmid;
  6186. }
  6187. --$maxit;
  6188. }
  6189. $this->SetFontSize($this->FontSizePt);
  6190. }
  6191. if ($text_height < $maxh) {
  6192. if ($valign == 'M') {
  6193. // text vertically centered
  6194. $this->y += (($maxh - $text_height) / 2);
  6195. } elseif ($valign == 'B') {
  6196. // text vertically aligned on bottom
  6197. $this->y += ($maxh - $text_height);
  6198. }
  6199. }
  6200. }
  6201. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6202. if ($fitcell) {
  6203. // restore font size
  6204. $this->SetFontSize($prev_FontSizePt);
  6205. }
  6206. }
  6207. if ($autopadding) {
  6208. // add bottom padding
  6209. $this->y += $mc_padding['B'];
  6210. }
  6211. // Get end-of-text Y position
  6212. $currentY = $this->y;
  6213. // get latest page number
  6214. $endpage = $this->page;
  6215. if ($resth > 0) {
  6216. $skip = ($endpage - $startpage);
  6217. $tmpresth = $resth;
  6218. while ($tmpresth > 0) {
  6219. if ($skip <= 0) {
  6220. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6221. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6222. }
  6223. if ($this->num_columns > 1) {
  6224. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6225. } else {
  6226. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6227. }
  6228. --$skip;
  6229. }
  6230. $currentY = $this->y;
  6231. $endpage = $this->page;
  6232. }
  6233. // get latest column
  6234. $endcolumn = $this->current_column;
  6235. if ($this->num_columns == 0) {
  6236. $this->num_columns = 1;
  6237. }
  6238. // get border modes
  6239. $border_start = $this->getBorderMode($border, $position='start');
  6240. $border_end = $this->getBorderMode($border, $position='end');
  6241. $border_middle = $this->getBorderMode($border, $position='middle');
  6242. // design borders around HTML cells.
  6243. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6244. $ccode = '';
  6245. $this->setPage($page);
  6246. if ($this->num_columns < 2) {
  6247. // single-column mode
  6248. $this->SetX($x);
  6249. $this->y = $this->tMargin;
  6250. }
  6251. // account for margin changes
  6252. if ($page > $startpage) {
  6253. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6254. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6255. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6256. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6257. }
  6258. }
  6259. if ($startpage == $endpage) {
  6260. // single page
  6261. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6262. $this->selectColumn($column);
  6263. if ($this->rtl) {
  6264. $this->x -= $mc_margin['R'];
  6265. } else {
  6266. $this->x += $mc_margin['L'];
  6267. }
  6268. if ($startcolumn == $endcolumn) { // single column
  6269. $cborder = $border;
  6270. $h = max($h, ($currentY - $oy));
  6271. $this->y = $oy;
  6272. } elseif ($column == $startcolumn) { // first column
  6273. $cborder = $border_start;
  6274. $this->y = $oy;
  6275. $h = $this->h - $this->y - $this->bMargin;
  6276. } elseif ($column == $endcolumn) { // end column
  6277. $cborder = $border_end;
  6278. $h = $currentY - $this->y;
  6279. if ($resth > $h) {
  6280. $h = $resth;
  6281. }
  6282. } else { // middle column
  6283. $cborder = $border_middle;
  6284. $h = $this->h - $this->y - $this->bMargin;
  6285. $resth -= $h;
  6286. }
  6287. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6288. } // end for each column
  6289. } elseif ($page == $startpage) { // first page
  6290. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6291. $this->selectColumn($column);
  6292. if ($this->rtl) {
  6293. $this->x -= $mc_margin['R'];
  6294. } else {
  6295. $this->x += $mc_margin['L'];
  6296. }
  6297. if ($column == $startcolumn) { // first column
  6298. $cborder = $border_start;
  6299. $this->y = $oy;
  6300. $h = $this->h - $this->y - $this->bMargin;
  6301. } else { // middle column
  6302. $cborder = $border_middle;
  6303. $h = $this->h - $this->y - $this->bMargin;
  6304. $resth -= $h;
  6305. }
  6306. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6307. } // end for each column
  6308. } elseif ($page == $endpage) { // last page
  6309. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6310. $this->selectColumn($column);
  6311. if ($this->rtl) {
  6312. $this->x -= $mc_margin['R'];
  6313. } else {
  6314. $this->x += $mc_margin['L'];
  6315. }
  6316. if ($column == $endcolumn) {
  6317. // end column
  6318. $cborder = $border_end;
  6319. $h = $currentY - $this->y;
  6320. if ($resth > $h) {
  6321. $h = $resth;
  6322. }
  6323. } else {
  6324. // middle column
  6325. $cborder = $border_middle;
  6326. $h = $this->h - $this->y - $this->bMargin;
  6327. $resth -= $h;
  6328. }
  6329. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6330. } // end for each column
  6331. } else { // middle page
  6332. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6333. $this->selectColumn($column);
  6334. if ($this->rtl) {
  6335. $this->x -= $mc_margin['R'];
  6336. } else {
  6337. $this->x += $mc_margin['L'];
  6338. }
  6339. $cborder = $border_middle;
  6340. $h = $this->h - $this->y - $this->bMargin;
  6341. $resth -= $h;
  6342. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6343. } // end for each column
  6344. }
  6345. if ($cborder OR $fill) {
  6346. $offsetlen = strlen($ccode);
  6347. // draw border and fill
  6348. if ($this->inxobj) {
  6349. // we are inside an XObject template
  6350. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6351. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6352. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6353. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6354. } else {
  6355. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6356. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6357. }
  6358. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6359. $pstart = substr($pagebuff, 0, $pagemark);
  6360. $pend = substr($pagebuff, $pagemark);
  6361. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6362. } else {
  6363. if (end($this->transfmrk[$this->page]) !== false) {
  6364. $pagemarkkey = key($this->transfmrk[$this->page]);
  6365. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6366. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6367. } elseif ($this->InFooter) {
  6368. $pagemark = $this->footerpos[$this->page];
  6369. $this->footerpos[$this->page] += $offsetlen;
  6370. } else {
  6371. $pagemark = $this->intmrk[$this->page];
  6372. $this->intmrk[$this->page] += $offsetlen;
  6373. }
  6374. $pagebuff = $this->getPageBuffer($this->page);
  6375. $pstart = substr($pagebuff, 0, $pagemark);
  6376. $pend = substr($pagebuff, $pagemark);
  6377. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6378. }
  6379. }
  6380. } // end for each page
  6381. // Get end-of-cell Y position
  6382. $currentY = $this->GetY();
  6383. // restore previous values
  6384. if ($this->num_columns > 1) {
  6385. $this->selectColumn();
  6386. } else {
  6387. // restore original margins
  6388. $this->lMargin = $lMargin;
  6389. $this->rMargin = $rMargin;
  6390. if ($this->page > $startpage) {
  6391. // check for margin variations between pages (i.e. booklet mode)
  6392. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6393. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6394. if (($dl != 0) OR ($dr != 0)) {
  6395. $this->lMargin += $dl;
  6396. $this->rMargin += $dr;
  6397. }
  6398. }
  6399. }
  6400. if ($ln > 0) {
  6401. //Go to the beginning of the next line
  6402. $this->SetY($currentY + $mc_margin['B']);
  6403. if ($ln == 2) {
  6404. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6405. }
  6406. } else {
  6407. // go left or right by case
  6408. $this->setPage($startpage);
  6409. $this->y = $y;
  6410. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6411. }
  6412. $this->setContentMark();
  6413. $this->cell_padding = $prev_cell_padding;
  6414. $this->cell_margin = $prev_cell_margin;
  6415. return $nl;
  6416. }
  6417. /**
  6418. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6419. * @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)))
  6420. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6421. * @return border mode array
  6422. * @protected
  6423. * @since 4.4.002 (2008-12-09)
  6424. */
  6425. protected function getBorderMode($brd, $position='start') {
  6426. if ((!$this->opencell) OR empty($brd)) {
  6427. return $brd;
  6428. }
  6429. if ($brd == 1) {
  6430. $brd = 'LTRB';
  6431. }
  6432. if (is_string($brd)) {
  6433. // convert string to array
  6434. $slen = strlen($brd);
  6435. $newbrd = array();
  6436. for ($i = 0; $i < $slen; ++$i) {
  6437. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6438. }
  6439. $brd = $newbrd;
  6440. }
  6441. foreach ($brd as $border => $style) {
  6442. switch ($position) {
  6443. case 'start': {
  6444. if (strpos($border, 'B') !== false) {
  6445. // remove bottom line
  6446. $newkey = str_replace('B', '', $border);
  6447. if (strlen($newkey) > 0) {
  6448. $brd[$newkey] = $style;
  6449. }
  6450. unset($brd[$border]);
  6451. }
  6452. break;
  6453. }
  6454. case 'middle': {
  6455. if (strpos($border, 'B') !== false) {
  6456. // remove bottom line
  6457. $newkey = str_replace('B', '', $border);
  6458. if (strlen($newkey) > 0) {
  6459. $brd[$newkey] = $style;
  6460. }
  6461. unset($brd[$border]);
  6462. $border = $newkey;
  6463. }
  6464. if (strpos($border, 'T') !== false) {
  6465. // remove bottom line
  6466. $newkey = str_replace('T', '', $border);
  6467. if (strlen($newkey) > 0) {
  6468. $brd[$newkey] = $style;
  6469. }
  6470. unset($brd[$border]);
  6471. }
  6472. break;
  6473. }
  6474. case 'end': {
  6475. if (strpos($border, 'T') !== false) {
  6476. // remove bottom line
  6477. $newkey = str_replace('T', '', $border);
  6478. if (strlen($newkey) > 0) {
  6479. $brd[$newkey] = $style;
  6480. }
  6481. unset($brd[$border]);
  6482. }
  6483. break;
  6484. }
  6485. }
  6486. }
  6487. return $brd;
  6488. }
  6489. /**
  6490. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6491. * @param $txt (string) String for calculating his height
  6492. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6493. * @param $reseth (boolean) if true reset the last cell height (default false).
  6494. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6495. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6496. * @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)))
  6497. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6498. * @author Alexander Escalona Fernández, Nicola Asuni
  6499. * @public
  6500. * @since 4.5.011
  6501. */
  6502. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6503. if ($txt === '') {
  6504. // empty string
  6505. return 1;
  6506. }
  6507. // adjust internal padding
  6508. $prev_cell_padding = $this->cell_padding;
  6509. $prev_lasth = $this->lasth;
  6510. if (is_array($cellpadding)) {
  6511. $this->cell_padding = $cellpadding;
  6512. }
  6513. $this->adjustCellPadding($border);
  6514. if ($this->empty_string($w) OR ($w <= 0)) {
  6515. if ($this->rtl) {
  6516. $w = $this->x - $this->lMargin;
  6517. } else {
  6518. $w = $this->w - $this->rMargin - $this->x;
  6519. }
  6520. }
  6521. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6522. if ($reseth) {
  6523. // reset row height
  6524. $this->resetLastH();
  6525. }
  6526. $lines = 1;
  6527. $sum = 0;
  6528. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6529. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6530. $length = count($chars);
  6531. $lastSeparator = -1;
  6532. for ($i = 0; $i < $length; ++$i) {
  6533. $charWidth = $charsWidth[$i];
  6534. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6535. $lastSeparator = $i;
  6536. }
  6537. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6538. ++$lines;
  6539. if ($chars[$i] == 10) {
  6540. $lastSeparator = -1;
  6541. $sum = 0;
  6542. } elseif ($lastSeparator != -1) {
  6543. $i = $lastSeparator;
  6544. $lastSeparator = -1;
  6545. $sum = 0;
  6546. } else {
  6547. $sum = $charWidth;
  6548. }
  6549. } else {
  6550. $sum += $charWidth;
  6551. }
  6552. }
  6553. if ($chars[($length - 1)] == 10) {
  6554. --$lines;
  6555. }
  6556. $this->cell_padding = $prev_cell_padding;
  6557. $this->lasth = $prev_lasth;
  6558. return $lines;
  6559. }
  6560. /**
  6561. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6562. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6563. * @pre
  6564. * // store current object
  6565. * $pdf->startTransaction();
  6566. * // store starting values
  6567. * $start_y = $pdf->GetY();
  6568. * $start_page = $pdf->getPage();
  6569. * // call your printing functions with your parameters
  6570. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6571. * $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);
  6572. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6573. * // get the new Y
  6574. * $end_y = $pdf->GetY();
  6575. * $end_page = $pdf->getPage();
  6576. * // calculate height
  6577. * $height = 0;
  6578. * if ($end_page == $start_page) {
  6579. * $height = $end_y - $start_y;
  6580. * } else {
  6581. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6582. * $this->setPage($page);
  6583. * if ($page == $start_page) {
  6584. * // first page
  6585. * $height = $this->h - $start_y - $this->bMargin;
  6586. * } elseif ($page == $end_page) {
  6587. * // last page
  6588. * $height = $end_y - $this->tMargin;
  6589. * } else {
  6590. * $height = $this->h - $this->tMargin - $this->bMargin;
  6591. * }
  6592. * }
  6593. * }
  6594. * // restore previous object
  6595. * $pdf = $pdf->rollbackTransaction();
  6596. *
  6597. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6598. * @param $txt (string) String for calculating his height
  6599. * @param $reseth (boolean) if true reset the last cell height (default false).
  6600. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6601. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6602. * @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)))
  6603. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6604. * @author Nicola Asuni, Alexander Escalona Fernández
  6605. * @public
  6606. */
  6607. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6608. // adjust internal padding
  6609. $prev_cell_padding = $this->cell_padding;
  6610. $prev_lasth = $this->lasth;
  6611. if (is_array($cellpadding)) {
  6612. $this->cell_padding = $cellpadding;
  6613. }
  6614. $this->adjustCellPadding($border);
  6615. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6616. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6617. if ($autopadding) {
  6618. // add top and bottom padding
  6619. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6620. }
  6621. $this->cell_padding = $prev_cell_padding;
  6622. $this->lasth = $prev_lasth;
  6623. return $height;
  6624. }
  6625. /**
  6626. * This method prints text from the current position.<br />
  6627. * @param $h (float) Line height
  6628. * @param $txt (string) String to print
  6629. * @param $link (mixed) URL or identifier returned by AddLink()
  6630. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6631. * @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>
  6632. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6633. * @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.
  6634. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6635. * @param $firstblock (boolean) if true the string is the starting of a line.
  6636. * @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.
  6637. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6638. * @param $margin (array) margin array of the parent container
  6639. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6640. * @public
  6641. * @since 1.5
  6642. */
  6643. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6644. // check page for no-write regions and adapt page margins if necessary
  6645. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6646. if (strlen($txt) == 0) {
  6647. // fix empty text
  6648. $txt = ' ';
  6649. }
  6650. if ($margin === '') {
  6651. // set default margins
  6652. $margin = $this->cell_margin;
  6653. }
  6654. // remove carriage returns
  6655. $s = str_replace("\r", '', $txt);
  6656. // check if string contains arabic text
  6657. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6658. $arabic = true;
  6659. } else {
  6660. $arabic = false;
  6661. }
  6662. // check if string contains RTL text
  6663. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6664. $rtlmode = true;
  6665. } else {
  6666. $rtlmode = false;
  6667. }
  6668. // get a char width
  6669. $chrwidth = $this->GetCharWidth(46); // dot character
  6670. // get array of unicode values
  6671. $chars = $this->UTF8StringToArray($s);
  6672. // get array of chars
  6673. $uchars = $this->UTF8ArrayToUniArray($chars);
  6674. // get the number of characters
  6675. $nb = count($chars);
  6676. // replacement for SHY character (minus symbol)
  6677. $shy_replacement = 45;
  6678. $shy_replacement_char = $this->unichr($shy_replacement);
  6679. // widht for SHY replacement
  6680. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6681. // max Y
  6682. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6683. // page width
  6684. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6685. // calculate remaining line width ($w)
  6686. if ($this->rtl) {
  6687. $w = $this->x - $this->lMargin;
  6688. } else {
  6689. $w = $this->w - $this->rMargin - $this->x;
  6690. }
  6691. // max column width
  6692. $wmax = $w - $wadj;
  6693. if (!$firstline) {
  6694. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6695. }
  6696. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6697. // a single character do not fit on column
  6698. return '';
  6699. }
  6700. // minimum row height
  6701. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6702. $start_page = $this->page;
  6703. $i = 0; // character position
  6704. $j = 0; // current starting position
  6705. $sep = -1; // position of the last blank space
  6706. $shy = false; // true if the last blank is a soft hypen (SHY)
  6707. $l = 0; // current string length
  6708. $nl = 0; //number of lines
  6709. $linebreak = false;
  6710. $pc = 0; // previous character
  6711. // for each character
  6712. while ($i < $nb) {
  6713. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6714. break;
  6715. }
  6716. //Get the current character
  6717. $c = $chars[$i];
  6718. if ($c == 10) { // 10 = "\n" = new line
  6719. //Explicit line break
  6720. if ($align == 'J') {
  6721. if ($this->rtl) {
  6722. $talign = 'R';
  6723. } else {
  6724. $talign = 'L';
  6725. }
  6726. } else {
  6727. $talign = $align;
  6728. }
  6729. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6730. if ($firstline) {
  6731. $startx = $this->x;
  6732. $tmparr = array_slice($chars, $j, ($i - $j));
  6733. if ($rtlmode) {
  6734. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6735. }
  6736. $linew = $this->GetArrStringWidth($tmparr);
  6737. unset($tmparr);
  6738. if ($this->rtl) {
  6739. $this->endlinex = $startx - $linew;
  6740. } else {
  6741. $this->endlinex = $startx + $linew;
  6742. }
  6743. $w = $linew;
  6744. $tmpcellpadding = $this->cell_padding;
  6745. if ($maxh == 0) {
  6746. $this->SetCellPadding(0);
  6747. }
  6748. }
  6749. if ($firstblock AND $this->isRTLTextDir()) {
  6750. $tmpstr = $this->stringRightTrim($tmpstr);
  6751. }
  6752. // Skip newlines at the begining of a page or column
  6753. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6754. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6755. }
  6756. unset($tmpstr);
  6757. if ($firstline) {
  6758. $this->cell_padding = $tmpcellpadding;
  6759. return ($this->UniArrSubString($uchars, $i));
  6760. }
  6761. ++$nl;
  6762. $j = $i + 1;
  6763. $l = 0;
  6764. $sep = -1;
  6765. $shy = false;
  6766. // account for margin changes
  6767. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6768. $this->AcceptPageBreak();
  6769. if ($this->rtl) {
  6770. $this->x -= $margin['R'];
  6771. } else {
  6772. $this->x += $margin['L'];
  6773. }
  6774. $this->lMargin += $margin['L'];
  6775. $this->rMargin += $margin['R'];
  6776. }
  6777. $w = $this->getRemainingWidth();
  6778. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6779. } else {
  6780. // 160 is the non-breaking space.
  6781. // 173 is SHY (Soft Hypen).
  6782. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6783. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6784. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6785. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6786. // update last blank space position
  6787. $sep = $i;
  6788. // check if is a SHY
  6789. if ($c == 173) {
  6790. $shy = true;
  6791. if ($pc == 45) {
  6792. $tmp_shy_replacement_width = 0;
  6793. $tmp_shy_replacement_char = '';
  6794. } else {
  6795. $tmp_shy_replacement_width = $shy_replacement_width;
  6796. $tmp_shy_replacement_char = $shy_replacement_char;
  6797. }
  6798. } else {
  6799. $shy = false;
  6800. }
  6801. }
  6802. // update string length
  6803. if ($this->isUnicodeFont() AND ($arabic)) {
  6804. // with bidirectional algorithm some chars may be changed affecting the line length
  6805. // *** very slow ***
  6806. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6807. } else {
  6808. $l += $this->GetCharWidth($c);
  6809. }
  6810. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6811. // we have reached the end of column
  6812. if ($sep == -1) {
  6813. // check if the line was already started
  6814. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6815. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6816. // print a void cell and go to next line
  6817. $this->Cell($w, $h, '', 0, 1);
  6818. $linebreak = true;
  6819. if ($firstline) {
  6820. return ($this->UniArrSubString($uchars, $j));
  6821. }
  6822. } else {
  6823. // truncate the word because do not fit on column
  6824. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6825. if ($firstline) {
  6826. $startx = $this->x;
  6827. $tmparr = array_slice($chars, $j, ($i - $j));
  6828. if ($rtlmode) {
  6829. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6830. }
  6831. $linew = $this->GetArrStringWidth($tmparr);
  6832. unset($tmparr);
  6833. if ($this->rtl) {
  6834. $this->endlinex = $startx - $linew;
  6835. } else {
  6836. $this->endlinex = $startx + $linew;
  6837. }
  6838. $w = $linew;
  6839. $tmpcellpadding = $this->cell_padding;
  6840. if ($maxh == 0) {
  6841. $this->SetCellPadding(0);
  6842. }
  6843. }
  6844. if ($firstblock AND $this->isRTLTextDir()) {
  6845. $tmpstr = $this->stringRightTrim($tmpstr);
  6846. }
  6847. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6848. unset($tmpstr);
  6849. if ($firstline) {
  6850. $this->cell_padding = $tmpcellpadding;
  6851. return ($this->UniArrSubString($uchars, $i));
  6852. }
  6853. $j = $i;
  6854. --$i;
  6855. }
  6856. } else {
  6857. // word wrapping
  6858. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6859. $endspace = 1;
  6860. } else {
  6861. $endspace = 0;
  6862. }
  6863. // check the length of the next string
  6864. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6865. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6866. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6867. // truncate the word because do not fit on a full page width
  6868. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6869. if ($firstline) {
  6870. $startx = $this->x;
  6871. $tmparr = array_slice($chars, $j, ($i - $j));
  6872. if ($rtlmode) {
  6873. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6874. }
  6875. $linew = $this->GetArrStringWidth($tmparr);
  6876. unset($tmparr);
  6877. if ($this->rtl) {
  6878. $this->endlinex = $startx - $linew;
  6879. } else {
  6880. $this->endlinex = $startx + $linew;
  6881. }
  6882. $w = $linew;
  6883. $tmpcellpadding = $this->cell_padding;
  6884. if ($maxh == 0) {
  6885. $this->SetCellPadding(0);
  6886. }
  6887. }
  6888. if ($firstblock AND $this->isRTLTextDir()) {
  6889. $tmpstr = $this->stringRightTrim($tmpstr);
  6890. }
  6891. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6892. unset($tmpstr);
  6893. if ($firstline) {
  6894. $this->cell_padding = $tmpcellpadding;
  6895. return ($this->UniArrSubString($uchars, $i));
  6896. }
  6897. $j = $i;
  6898. --$i;
  6899. } else {
  6900. // word wrapping
  6901. if ($shy) {
  6902. // add hypen (minus symbol) at the end of the line
  6903. $shy_width = $tmp_shy_replacement_width;
  6904. if ($this->rtl) {
  6905. $shy_char_left = $tmp_shy_replacement_char;
  6906. $shy_char_right = '';
  6907. } else {
  6908. $shy_char_left = '';
  6909. $shy_char_right = $tmp_shy_replacement_char;
  6910. }
  6911. } else {
  6912. $shy_width = 0;
  6913. $shy_char_left = '';
  6914. $shy_char_right = '';
  6915. }
  6916. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6917. if ($firstline) {
  6918. $startx = $this->x;
  6919. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6920. if ($rtlmode) {
  6921. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6922. }
  6923. $linew = $this->GetArrStringWidth($tmparr);
  6924. unset($tmparr);
  6925. if ($this->rtl) {
  6926. $this->endlinex = $startx - $linew - $shy_width;
  6927. } else {
  6928. $this->endlinex = $startx + $linew + $shy_width;
  6929. }
  6930. $w = $linew;
  6931. $tmpcellpadding = $this->cell_padding;
  6932. if ($maxh == 0) {
  6933. $this->SetCellPadding(0);
  6934. }
  6935. }
  6936. // print the line
  6937. if ($firstblock AND $this->isRTLTextDir()) {
  6938. $tmpstr = $this->stringRightTrim($tmpstr);
  6939. }
  6940. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6941. unset($tmpstr);
  6942. if ($firstline) {
  6943. // return the remaining text
  6944. $this->cell_padding = $tmpcellpadding;
  6945. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6946. }
  6947. $i = $sep;
  6948. $sep = -1;
  6949. $shy = false;
  6950. $j = ($i+1);
  6951. }
  6952. }
  6953. // account for margin changes
  6954. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6955. $this->AcceptPageBreak();
  6956. if ($this->rtl) {
  6957. $this->x -= $margin['R'];
  6958. } else {
  6959. $this->x += $margin['L'];
  6960. }
  6961. $this->lMargin += $margin['L'];
  6962. $this->rMargin += $margin['R'];
  6963. }
  6964. $w = $this->getRemainingWidth();
  6965. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6966. if ($linebreak) {
  6967. $linebreak = false;
  6968. } else {
  6969. ++$nl;
  6970. $l = 0;
  6971. }
  6972. }
  6973. }
  6974. // save last character
  6975. $pc = $c;
  6976. ++$i;
  6977. } // end while i < nb
  6978. // print last substring (if any)
  6979. if ($l > 0) {
  6980. switch ($align) {
  6981. case 'J':
  6982. case 'C': {
  6983. $w = $w;
  6984. break;
  6985. }
  6986. case 'L': {
  6987. if ($this->rtl) {
  6988. $w = $w;
  6989. } else {
  6990. $w = $l;
  6991. }
  6992. break;
  6993. }
  6994. case 'R': {
  6995. if ($this->rtl) {
  6996. $w = $l;
  6997. } else {
  6998. $w = $w;
  6999. }
  7000. break;
  7001. }
  7002. default: {
  7003. $w = $l;
  7004. break;
  7005. }
  7006. }
  7007. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  7008. if ($firstline) {
  7009. $startx = $this->x;
  7010. $tmparr = array_slice($chars, $j, ($nb - $j));
  7011. if ($rtlmode) {
  7012. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7013. }
  7014. $linew = $this->GetArrStringWidth($tmparr);
  7015. unset($tmparr);
  7016. if ($this->rtl) {
  7017. $this->endlinex = $startx - $linew;
  7018. } else {
  7019. $this->endlinex = $startx + $linew;
  7020. }
  7021. $w = $linew;
  7022. $tmpcellpadding = $this->cell_padding;
  7023. if ($maxh == 0) {
  7024. $this->SetCellPadding(0);
  7025. }
  7026. }
  7027. if ($firstblock AND $this->isRTLTextDir()) {
  7028. $tmpstr = $this->stringRightTrim($tmpstr);
  7029. }
  7030. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  7031. unset($tmpstr);
  7032. if ($firstline) {
  7033. $this->cell_padding = $tmpcellpadding;
  7034. return ($this->UniArrSubString($uchars, $nb));
  7035. }
  7036. ++$nl;
  7037. }
  7038. if ($firstline) {
  7039. return '';
  7040. }
  7041. return $nl;
  7042. }
  7043. /**
  7044. * Returns the remaining width between the current position and margins.
  7045. * @return int Return the remaining width
  7046. * @protected
  7047. */
  7048. protected function getRemainingWidth() {
  7049. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  7050. if ($this->rtl) {
  7051. return ($this->x - $this->lMargin);
  7052. } else {
  7053. return ($this->w - $this->rMargin - $this->x);
  7054. }
  7055. }
  7056. /**
  7057. * Extract a slice of the $strarr array and return it as string.
  7058. * @param $strarr (string) The input array of characters.
  7059. * @param $start (int) the starting element of $strarr.
  7060. * @param $end (int) first element that will not be returned.
  7061. * @return Return part of a string
  7062. * @public
  7063. */
  7064. public function UTF8ArrSubString($strarr, $start='', $end='') {
  7065. if (strlen($start) == 0) {
  7066. $start = 0;
  7067. }
  7068. if (strlen($end) == 0) {
  7069. $end = count($strarr);
  7070. }
  7071. $string = '';
  7072. for ($i=$start; $i < $end; ++$i) {
  7073. $string .= $this->unichr($strarr[$i]);
  7074. }
  7075. return $string;
  7076. }
  7077. /**
  7078. * Extract a slice of the $uniarr array and return it as string.
  7079. * @param $uniarr (string) The input array of characters.
  7080. * @param $start (int) the starting element of $strarr.
  7081. * @param $end (int) first element that will not be returned.
  7082. * @return Return part of a string
  7083. * @public
  7084. * @since 4.5.037 (2009-04-07)
  7085. */
  7086. public function UniArrSubString($uniarr, $start='', $end='') {
  7087. if (strlen($start) == 0) {
  7088. $start = 0;
  7089. }
  7090. if (strlen($end) == 0) {
  7091. $end = count($uniarr);
  7092. }
  7093. $string = '';
  7094. for ($i=$start; $i < $end; ++$i) {
  7095. $string .= $uniarr[$i];
  7096. }
  7097. return $string;
  7098. }
  7099. /**
  7100. * Convert an array of UTF8 values to array of unicode characters
  7101. * @param $ta (string) The input array of UTF8 values.
  7102. * @return Return array of unicode characters
  7103. * @public
  7104. * @since 4.5.037 (2009-04-07)
  7105. */
  7106. public function UTF8ArrayToUniArray($ta) {
  7107. return array_map(array($this, 'unichr'), $ta);
  7108. }
  7109. /**
  7110. * Returns the unicode caracter specified by UTF-8 value
  7111. * @param $c (int) UTF-8 value
  7112. * @return Returns the specified character.
  7113. * @author Miguel Perez, Nicola Asuni
  7114. * @public
  7115. * @since 2.3.000 (2008-03-05)
  7116. */
  7117. public function unichr($c) {
  7118. if (!$this->isunicode) {
  7119. return chr($c);
  7120. } elseif ($c <= 0x7F) {
  7121. // one byte
  7122. return chr($c);
  7123. } elseif ($c <= 0x7FF) {
  7124. // two bytes
  7125. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  7126. } elseif ($c <= 0xFFFF) {
  7127. // three bytes
  7128. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  7129. } elseif ($c <= 0x10FFFF) {
  7130. // four bytes
  7131. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  7132. } else {
  7133. return '';
  7134. }
  7135. }
  7136. /**
  7137. * Return the image type given the file name or array returned by getimagesize() function.
  7138. * @param $imgfile (string) image file name
  7139. * @param $iminfo (array) array of image information returned by getimagesize() function.
  7140. * @return string image type
  7141. * @since 4.8.017 (2009-11-27)
  7142. */
  7143. public function getImageFileType($imgfile, $iminfo=array()) {
  7144. $type = '';
  7145. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  7146. $mime = explode('/', $iminfo['mime']);
  7147. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  7148. $type = strtolower(trim($mime[1]));
  7149. }
  7150. }
  7151. if (empty($type)) {
  7152. $fileinfo = pathinfo($imgfile);
  7153. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  7154. $type = strtolower(trim($fileinfo['extension']));
  7155. }
  7156. }
  7157. if ($type == 'jpg') {
  7158. $type = 'jpeg';
  7159. }
  7160. return $type;
  7161. }
  7162. /**
  7163. * Set the block dimensions accounting for page breaks and page/column fitting
  7164. * @param $w (float) width
  7165. * @param $h (float) height
  7166. * @param $x (float) X coordinate
  7167. * @param $y (float) Y coodiante
  7168. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  7169. * @return array($w, $h, $x, $y)
  7170. * @protected
  7171. * @since 5.5.009 (2010-07-05)
  7172. */
  7173. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  7174. if ($w <= 0) {
  7175. // set maximum width
  7176. $w = ($this->w - $this->lMargin - $this->rMargin);
  7177. }
  7178. if ($h <= 0) {
  7179. // set maximum height
  7180. $h = ($this->PageBreakTrigger - $this->tMargin);
  7181. }
  7182. // resize the block to be vertically contained on a single page or single column
  7183. if ($fitonpage OR $this->AutoPageBreak) {
  7184. $ratio_wh = ($w / $h);
  7185. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7186. $h = $this->PageBreakTrigger - $this->tMargin;
  7187. $w = ($h * $ratio_wh);
  7188. }
  7189. // resize the block to be horizontally contained on a single page or single column
  7190. if ($fitonpage) {
  7191. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7192. if ($w > $maxw) {
  7193. $w = $maxw;
  7194. $h = ($w / $ratio_wh);
  7195. }
  7196. }
  7197. }
  7198. // Check whether we need a new page or new column first as this does not fit
  7199. $prev_x = $this->x;
  7200. $prev_y = $this->y;
  7201. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7202. $y = $this->y;
  7203. if ($this->rtl) {
  7204. $x += ($prev_x - $this->x);
  7205. } else {
  7206. $x += ($this->x - $prev_x);
  7207. }
  7208. $this->newline = true;
  7209. }
  7210. // resize the block to be contained on the remaining available page or column space
  7211. if ($fitonpage) {
  7212. $ratio_wh = ($w / $h);
  7213. if (($y + $h) > $this->PageBreakTrigger) {
  7214. $h = $this->PageBreakTrigger - $y;
  7215. $w = ($h * $ratio_wh);
  7216. }
  7217. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7218. $w = $this->w - $this->rMargin - $x;
  7219. $h = ($w / $ratio_wh);
  7220. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7221. $w = $x - $this->lMargin;
  7222. $h = ($w / $ratio_wh);
  7223. }
  7224. }
  7225. return array($w, $h, $x, $y);
  7226. }
  7227. /**
  7228. * Puts an image in the page.
  7229. * The upper-left corner must be given.
  7230. * The dimensions can be specified in different ways:<ul>
  7231. * <li>explicit width and height (expressed in user unit)</li>
  7232. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7233. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7234. * 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;
  7235. * The format can be specified explicitly or inferred from the file extension.<br />
  7236. * It is possible to put a link on the image.<br />
  7237. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7238. * @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').
  7239. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7240. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7241. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7242. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7243. * @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.
  7244. * @param $link (mixed) URL or identifier returned by AddLink().
  7245. * @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>
  7246. * @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).
  7247. * @param $dpi (int) dot-per-inch resolution used on resize
  7248. * @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>
  7249. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7250. * @param $imgmask (mixed) image object returned by this function or false
  7251. * @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)))
  7252. * @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).
  7253. * @param $hidden (boolean) If true do not display the image.
  7254. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7255. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7256. * @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.
  7257. * @return image information
  7258. * @public
  7259. * @since 1.1
  7260. */
  7261. 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()) {
  7262. if ($x === '') {
  7263. $x = $this->x;
  7264. }
  7265. if ($y === '') {
  7266. $y = $this->y;
  7267. }
  7268. // check page for no-write regions and adapt page margins if necessary
  7269. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7270. $cached_file = false; // true when the file is cached
  7271. $exurl = ''; // external streams
  7272. // check if we are passing an image as file or string
  7273. if ($file[0] === '@') {
  7274. // image from string
  7275. $imgdata = substr($file, 1);
  7276. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7277. $fp = fopen($file, 'w');
  7278. fwrite($fp, $imgdata);
  7279. fclose($fp);
  7280. unset($imgdata);
  7281. $cached_file = true;
  7282. $imsize = @getimagesize($file);
  7283. if ($imsize === FALSE) {
  7284. unlink($file);
  7285. $cached_file = false;
  7286. }
  7287. } else { // image file
  7288. if ($file{0} === '*') {
  7289. // image as external stream
  7290. $file = substr($file, 1);
  7291. $exurl = $file;
  7292. }
  7293. // check if is local file
  7294. if (!@file_exists($file)) {
  7295. // encode spaces on filename (file is probably an URL)
  7296. $file = str_replace(' ', '%20', $file);
  7297. }
  7298. if (@file_exists($file)) {
  7299. // get image dimensions
  7300. $imsize = @getimagesize($file);
  7301. } else {
  7302. $imsize = false;
  7303. }
  7304. if ($imsize === FALSE) {
  7305. if (function_exists('curl_init')) {
  7306. // try to get remote file data using cURL
  7307. $cs = curl_init(); // curl session
  7308. curl_setopt($cs, CURLOPT_URL, $file);
  7309. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7310. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7311. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7312. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7313. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7314. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7315. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7316. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7317. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7318. $imgdata = curl_exec($cs);
  7319. curl_close($cs);
  7320. if ($imgdata !== FALSE) {
  7321. // copy image to cache
  7322. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7323. $fp = fopen($file, 'w');
  7324. fwrite($fp, $imgdata);
  7325. fclose($fp);
  7326. unset($imgdata);
  7327. $cached_file = true;
  7328. $imsize = @getimagesize($file);
  7329. if ($imsize === FALSE) {
  7330. unlink($file);
  7331. $cached_file = false;
  7332. }
  7333. }
  7334. } elseif (($w > 0) AND ($h > 0)) {
  7335. // get measures from specified data
  7336. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7337. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7338. $imsize = array($pw, $ph);
  7339. }
  7340. }
  7341. }
  7342. if ($imsize === FALSE) {
  7343. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7344. // get measures from specified data
  7345. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7346. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7347. $imsize = array($pw, $ph);
  7348. } else {
  7349. $this->Error('[Image] Unable to get image: '.$file);
  7350. }
  7351. }
  7352. // file hash
  7353. $filehash = md5($file);
  7354. // get original image width and height in pixels
  7355. list($pixw, $pixh) = $imsize;
  7356. // calculate image width and height on document
  7357. if (($w <= 0) AND ($h <= 0)) {
  7358. // convert image size to document unit
  7359. $w = $this->pixelsToUnits($pixw);
  7360. $h = $this->pixelsToUnits($pixh);
  7361. } elseif ($w <= 0) {
  7362. $w = $h * $pixw / $pixh;
  7363. } elseif ($h <= 0) {
  7364. $h = $w * $pixh / $pixw;
  7365. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7366. if (strlen($fitbox) !== 2) {
  7367. // set default alignment
  7368. $fitbox = '--';
  7369. }
  7370. // scale image dimensions proportionally to fit within the ($w, $h) box
  7371. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7372. // store current height
  7373. $oldh = $h;
  7374. // calculate new height
  7375. $h = $w * $pixh / $pixw;
  7376. // height difference
  7377. $hdiff = ($oldh - $h);
  7378. // vertical alignment
  7379. switch (strtoupper($fitbox{1})) {
  7380. case 'T': {
  7381. break;
  7382. }
  7383. case 'M': {
  7384. $y += ($hdiff / 2);
  7385. break;
  7386. }
  7387. case 'B': {
  7388. $y += $hdiff;
  7389. break;
  7390. }
  7391. }
  7392. } else {
  7393. // store current width
  7394. $oldw = $w;
  7395. // calculate new width
  7396. $w = $h * $pixw / $pixh;
  7397. // width difference
  7398. $wdiff = ($oldw - $w);
  7399. // horizontal alignment
  7400. switch (strtoupper($fitbox{0})) {
  7401. case 'L': {
  7402. if ($this->rtl) {
  7403. $x -= $wdiff;
  7404. }
  7405. break;
  7406. }
  7407. case 'C': {
  7408. if ($this->rtl) {
  7409. $x -= ($wdiff / 2);
  7410. } else {
  7411. $x += ($wdiff / 2);
  7412. }
  7413. break;
  7414. }
  7415. case 'R': {
  7416. if (!$this->rtl) {
  7417. $x += $wdiff;
  7418. }
  7419. break;
  7420. }
  7421. }
  7422. }
  7423. }
  7424. // fit the image on available space
  7425. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7426. // calculate new minimum dimensions in pixels
  7427. $neww = round($w * $this->k * $dpi / $this->dpi);
  7428. $newh = round($h * $this->k * $dpi / $this->dpi);
  7429. // check if resize is necessary (resize is used only to reduce the image)
  7430. $newsize = ($neww * $newh);
  7431. $pixsize = ($pixw * $pixh);
  7432. if (intval($resize) == 2) {
  7433. $resize = true;
  7434. } elseif ($newsize >= $pixsize) {
  7435. $resize = false;
  7436. }
  7437. // check if image has been already added on document
  7438. $newimage = true;
  7439. if (in_array($file, $this->imagekeys)) {
  7440. $newimage = false;
  7441. // get existing image data
  7442. $info = $this->getImageBuffer($file);
  7443. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7444. // check if the newer image is larger
  7445. $oldsize = ($info['w'] * $info['h']);
  7446. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7447. $newimage = true;
  7448. }
  7449. }
  7450. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7451. // check for cached images with alpha channel
  7452. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7453. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7454. if (in_array($tempfile_plain, $this->imagekeys)) {
  7455. // get existing image data
  7456. $info = $this->getImageBuffer($tempfile_plain);
  7457. // check if the newer image is larger
  7458. $oldsize = ($info['w'] * $info['h']);
  7459. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7460. $newimage = true;
  7461. } else {
  7462. $newimage = false;
  7463. // embed mask image
  7464. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7465. // embed image, masked with previously embedded mask
  7466. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7467. }
  7468. }
  7469. }
  7470. if ($newimage) {
  7471. //First use of image, get info
  7472. $type = strtolower($type);
  7473. if ($type == '') {
  7474. $type = $this->getImageFileType($file, $imsize);
  7475. } elseif ($type == 'jpg') {
  7476. $type = 'jpeg';
  7477. }
  7478. $mqr = $this->get_mqr();
  7479. $this->set_mqr(false);
  7480. // Specific image handlers
  7481. $mtd = '_parse'.$type;
  7482. // GD image handler function
  7483. $gdfunction = 'imagecreatefrom'.$type;
  7484. $info = false;
  7485. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7486. // TCPDF image functions
  7487. $info = $this->$mtd($file);
  7488. if ($info == 'pngalpha') {
  7489. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7490. }
  7491. }
  7492. if (!$info) {
  7493. if (function_exists($gdfunction)) {
  7494. // GD library
  7495. $img = $gdfunction($file);
  7496. if ($resize) {
  7497. $imgr = imagecreatetruecolor($neww, $newh);
  7498. if (($type == 'gif') OR ($type == 'png')) {
  7499. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7500. }
  7501. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7502. if (($type == 'gif') OR ($type == 'png')) {
  7503. $info = $this->_toPNG($imgr);
  7504. } else {
  7505. $info = $this->_toJPEG($imgr);
  7506. }
  7507. } else {
  7508. if (($type == 'gif') OR ($type == 'png')) {
  7509. $info = $this->_toPNG($img);
  7510. } else {
  7511. $info = $this->_toJPEG($img);
  7512. }
  7513. }
  7514. } elseif (extension_loaded('imagick')) {
  7515. // ImageMagick library
  7516. $img = new Imagick();
  7517. if ($type == 'SVG') {
  7518. // get SVG file content
  7519. $svgimg = file_get_contents($file);
  7520. // get width and height
  7521. $regs = array();
  7522. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7523. $svgtag = $regs[1];
  7524. $tmp = array();
  7525. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7526. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7527. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  7528. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7529. } else {
  7530. $ow = $w;
  7531. }
  7532. $tmp = array();
  7533. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7534. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7535. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  7536. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7537. } else {
  7538. $oh = $h;
  7539. }
  7540. $tmp = array();
  7541. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7542. $vbw = ($ow * $this->imgscale * $this->k);
  7543. $vbh = ($oh * $this->imgscale * $this->k);
  7544. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  7545. $svgtag = $vbox.$svgtag;
  7546. }
  7547. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7548. }
  7549. $img->readImageBlob($svgimg);
  7550. } else {
  7551. $img->readImage($file);
  7552. }
  7553. if ($resize) {
  7554. $img->resizeImage($neww, $newh, 10, 1, false);
  7555. }
  7556. $img->setCompressionQuality($this->jpeg_quality);
  7557. $img->setImageFormat('jpeg');
  7558. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7559. $img->writeImage($tempname);
  7560. $info = $this->_parsejpeg($tempname);
  7561. unlink($tempname);
  7562. $img->destroy();
  7563. } else {
  7564. return;
  7565. }
  7566. }
  7567. if ($info === false) {
  7568. //If false, we cannot process image
  7569. return;
  7570. }
  7571. $this->set_mqr($mqr);
  7572. if ($ismask) {
  7573. // force grayscale
  7574. $info['cs'] = 'DeviceGray';
  7575. }
  7576. $info['i'] = $this->numimages;
  7577. if (!in_array($file, $this->imagekeys)) {
  7578. ++$info['i'];
  7579. }
  7580. if ($imgmask !== false) {
  7581. $info['masked'] = $imgmask;
  7582. }
  7583. if (!empty($exurl)) {
  7584. $info['exurl'] = $exurl;
  7585. }
  7586. // array of alternative images
  7587. $info['altimgs'] = $altimgs;
  7588. // add image to document
  7589. $this->setImageBuffer($file, $info);
  7590. }
  7591. if ($cached_file) {
  7592. // remove cached file
  7593. unlink($file);
  7594. }
  7595. // set alignment
  7596. $this->img_rb_y = $y + $h;
  7597. // set alignment
  7598. if ($this->rtl) {
  7599. if ($palign == 'L') {
  7600. $ximg = $this->lMargin;
  7601. } elseif ($palign == 'C') {
  7602. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7603. } elseif ($palign == 'R') {
  7604. $ximg = $this->w - $this->rMargin - $w;
  7605. } else {
  7606. $ximg = $x - $w;
  7607. }
  7608. $this->img_rb_x = $ximg;
  7609. } else {
  7610. if ($palign == 'L') {
  7611. $ximg = $this->lMargin;
  7612. } elseif ($palign == 'C') {
  7613. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7614. } elseif ($palign == 'R') {
  7615. $ximg = $this->w - $this->rMargin - $w;
  7616. } else {
  7617. $ximg = $x;
  7618. }
  7619. $this->img_rb_x = $ximg + $w;
  7620. }
  7621. if ($ismask OR $hidden) {
  7622. // image is not displayed
  7623. return $info['i'];
  7624. }
  7625. $xkimg = $ximg * $this->k;
  7626. if (!$alt) {
  7627. // only non-alternative immages will be set
  7628. $this->_out(sprintf('q %F 0 0 %F %F %F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  7629. }
  7630. if (!empty($border)) {
  7631. $bx = $this->x;
  7632. $by = $this->y;
  7633. $this->x = $ximg;
  7634. if ($this->rtl) {
  7635. $this->x += $w;
  7636. }
  7637. $this->y = $y;
  7638. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7639. $this->x = $bx;
  7640. $this->y = $by;
  7641. }
  7642. if ($link) {
  7643. $this->Link($ximg, $y, $w, $h, $link, 0);
  7644. }
  7645. // set pointer to align the next text/objects
  7646. switch($align) {
  7647. case 'T': {
  7648. $this->y = $y;
  7649. $this->x = $this->img_rb_x;
  7650. break;
  7651. }
  7652. case 'M': {
  7653. $this->y = $y + round($h/2);
  7654. $this->x = $this->img_rb_x;
  7655. break;
  7656. }
  7657. case 'B': {
  7658. $this->y = $this->img_rb_y;
  7659. $this->x = $this->img_rb_x;
  7660. break;
  7661. }
  7662. case 'N': {
  7663. $this->SetY($this->img_rb_y);
  7664. break;
  7665. }
  7666. default:{
  7667. break;
  7668. }
  7669. }
  7670. $this->endlinex = $this->img_rb_x;
  7671. if ($this->inxobj) {
  7672. // we are inside an XObject template
  7673. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7674. }
  7675. return $info['i'];
  7676. }
  7677. /**
  7678. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7679. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7680. * @since 4.6.025 (2009-08-17)
  7681. */
  7682. public function set_mqr($mqr) {
  7683. if (!defined('PHP_VERSION_ID')) {
  7684. $version = PHP_VERSION;
  7685. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7686. }
  7687. if (PHP_VERSION_ID < 50300) {
  7688. @set_magic_quotes_runtime($mqr);
  7689. }
  7690. }
  7691. /**
  7692. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7693. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7694. * @since 4.6.025 (2009-08-17)
  7695. */
  7696. public function get_mqr() {
  7697. if (!defined('PHP_VERSION_ID')) {
  7698. $version = PHP_VERSION;
  7699. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7700. }
  7701. if (PHP_VERSION_ID < 50300) {
  7702. return @get_magic_quotes_runtime();
  7703. }
  7704. return 0;
  7705. }
  7706. /**
  7707. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7708. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7709. * @param $image (image) Image object.
  7710. * return image JPEG image object.
  7711. * @protected
  7712. */
  7713. protected function _toJPEG($image) {
  7714. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7715. imagejpeg($image, $tempname, $this->jpeg_quality);
  7716. imagedestroy($image);
  7717. $retvars = $this->_parsejpeg($tempname);
  7718. // tidy up by removing temporary image
  7719. unlink($tempname);
  7720. return $retvars;
  7721. }
  7722. /**
  7723. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7724. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7725. * @param $image (image) Image object.
  7726. * return image PNG image object.
  7727. * @protected
  7728. * @since 4.9.016 (2010-04-20)
  7729. */
  7730. protected function _toPNG($image) {
  7731. // set temporary image file name
  7732. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7733. // turn off interlaced mode
  7734. imageinterlace($image, 0);
  7735. // create temporary PNG image
  7736. imagepng($image, $tempname);
  7737. // remove image from memory
  7738. imagedestroy($image);
  7739. // get PNG image data
  7740. $retvars = $this->_parsepng($tempname);
  7741. // tidy up by removing temporary image
  7742. unlink($tempname);
  7743. return $retvars;
  7744. }
  7745. /**
  7746. * Set the transparency for the given GD image.
  7747. * @param $new_image (image) GD image object
  7748. * @param $image (image) GD image object.
  7749. * return GD image object.
  7750. * @protected
  7751. * @since 4.9.016 (2010-04-20)
  7752. */
  7753. protected function _setGDImageTransparency($new_image, $image) {
  7754. // transparency index
  7755. $tid = imagecolortransparent($image);
  7756. // default transparency color
  7757. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7758. if ($tid >= 0) {
  7759. // get the colors for the transparency index
  7760. $tcol = imagecolorsforindex($image, $tid);
  7761. }
  7762. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7763. imagefill($new_image, 0, 0, $tid);
  7764. imagecolortransparent($new_image, $tid);
  7765. return $new_image;
  7766. }
  7767. /**
  7768. * Extract info from a JPEG file without using the GD library.
  7769. * @param $file (string) image file to parse
  7770. * @return array structure containing the image data
  7771. * @protected
  7772. */
  7773. protected function _parsejpeg($file) {
  7774. $a = getimagesize($file);
  7775. if (empty($a)) {
  7776. $this->Error('Missing or incorrect image file: '.$file);
  7777. }
  7778. if ($a[2] != 2) {
  7779. $this->Error('Not a JPEG file: '.$file);
  7780. }
  7781. // bits per pixel
  7782. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7783. // number of image channels
  7784. if (!isset($a['channels'])) {
  7785. $channels = 3;
  7786. } else {
  7787. $channels = intval($a['channels']);
  7788. }
  7789. // default colour space
  7790. switch ($channels) {
  7791. case 1: {
  7792. $colspace = 'DeviceGray';
  7793. break;
  7794. }
  7795. case 3: {
  7796. $colspace = 'DeviceRGB';
  7797. break;
  7798. }
  7799. case 4: {
  7800. $colspace = 'DeviceCMYK';
  7801. break;
  7802. }
  7803. default: {
  7804. $channels = 3;
  7805. $colspace = 'DeviceRGB';
  7806. break;
  7807. }
  7808. }
  7809. // get file content
  7810. $data = file_get_contents($file);
  7811. // check for embedded ICC profile
  7812. $icc = array();
  7813. $offset = 0;
  7814. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7815. // get ICC sequence length
  7816. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7817. // marker sequence number
  7818. $msn = max(1, ord($data[($pos + 12)]));
  7819. // number of markers (total of APP2 used)
  7820. $nom = max(1, ord($data[($pos + 13)]));
  7821. // get sequence segment
  7822. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7823. // move forward to next sequence
  7824. $offset = ($pos + 14 + $length);
  7825. }
  7826. // order and compact ICC segments
  7827. if (count($icc) > 0) {
  7828. ksort($icc);
  7829. $icc = implode('', $icc);
  7830. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7831. // invalid ICC profile
  7832. $icc = false;
  7833. }
  7834. } else {
  7835. $icc = false;
  7836. }
  7837. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7838. }
  7839. /**
  7840. * Extract info from a PNG file without using the GD library.
  7841. * @param $file (string) image file to parse
  7842. * @return array structure containing the image data
  7843. * @protected
  7844. */
  7845. protected function _parsepng($file) {
  7846. $f = fopen($file, 'rb');
  7847. if ($f === false) {
  7848. $this->Error('Can\'t open image file: '.$file);
  7849. }
  7850. //Check signature
  7851. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7852. $this->Error('Not a PNG file: '.$file);
  7853. }
  7854. //Read header chunk
  7855. fread($f, 4);
  7856. if (fread($f, 4) != 'IHDR') {
  7857. $this->Error('Incorrect PNG file: '.$file);
  7858. }
  7859. $w = $this->_freadint($f);
  7860. $h = $this->_freadint($f);
  7861. $bpc = ord(fread($f, 1));
  7862. if ($bpc > 8) {
  7863. //$this->Error('16-bit depth not supported: '.$file);
  7864. fclose($f);
  7865. return false;
  7866. }
  7867. $ct = ord(fread($f, 1));
  7868. if ($ct == 0) {
  7869. $colspace = 'DeviceGray';
  7870. } elseif ($ct == 2) {
  7871. $colspace = 'DeviceRGB';
  7872. } elseif ($ct == 3) {
  7873. $colspace = 'Indexed';
  7874. } else {
  7875. // alpha channel
  7876. fclose($f);
  7877. return 'pngalpha';
  7878. }
  7879. if (ord(fread($f, 1)) != 0) {
  7880. //$this->Error('Unknown compression method: '.$file);
  7881. fclose($f);
  7882. return false;
  7883. }
  7884. if (ord(fread($f, 1)) != 0) {
  7885. //$this->Error('Unknown filter method: '.$file);
  7886. fclose($f);
  7887. return false;
  7888. }
  7889. if (ord(fread($f, 1)) != 0) {
  7890. //$this->Error('Interlacing not supported: '.$file);
  7891. fclose($f);
  7892. return false;
  7893. }
  7894. fread($f, 4);
  7895. $channels = ($ct == 2 ? 3 : 1);
  7896. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7897. //Scan chunks looking for palette, transparency and image data
  7898. $pal = '';
  7899. $trns = '';
  7900. $data = '';
  7901. $icc = false;
  7902. do {
  7903. $n = $this->_freadint($f);
  7904. $type = fread($f, 4);
  7905. if ($type == 'PLTE') {
  7906. // read palette
  7907. $pal = $this->rfread($f, $n);
  7908. fread($f, 4);
  7909. } elseif ($type == 'tRNS') {
  7910. // read transparency info
  7911. $t = $this->rfread($f, $n);
  7912. if ($ct == 0) {
  7913. $trns = array(ord($t{1}));
  7914. } elseif ($ct == 2) {
  7915. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7916. } else {
  7917. $pos = strpos($t, chr(0));
  7918. if ($pos !== false) {
  7919. $trns = array($pos);
  7920. }
  7921. }
  7922. fread($f, 4);
  7923. } elseif ($type == 'IDAT') {
  7924. // read image data block
  7925. $data .= $this->rfread($f, $n);
  7926. fread($f, 4);
  7927. } elseif ($type == 'iCCP') {
  7928. // skip profile name and null separator
  7929. $len = 0;
  7930. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7931. ++$len;
  7932. }
  7933. // get compression method
  7934. if (ord(fread($f, 1)) != 0) {
  7935. //$this->Error('Unknown filter method: '.$file);
  7936. fclose($f);
  7937. return false;
  7938. }
  7939. // read ICC Color Profile
  7940. $icc = $this->rfread($f, ($n - $len - 2));
  7941. // decompress profile
  7942. $icc = gzuncompress($icc);
  7943. fread($f, 4);
  7944. } elseif ($type == 'IEND') {
  7945. break;
  7946. } else {
  7947. $this->rfread($f, $n + 4);
  7948. }
  7949. } while ($n);
  7950. if (($colspace == 'Indexed') AND (empty($pal))) {
  7951. //$this->Error('Missing palette in '.$file);
  7952. fclose($f);
  7953. return false;
  7954. }
  7955. fclose($f);
  7956. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7957. }
  7958. /**
  7959. * Binary-safe and URL-safe file read.
  7960. * 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.
  7961. * @param $handle (resource)
  7962. * @param $length (int)
  7963. * @return Returns the read string or FALSE in case of error.
  7964. * @author Nicola Asuni
  7965. * @protected
  7966. * @since 4.5.027 (2009-03-16)
  7967. */
  7968. protected function rfread($handle, $length) {
  7969. $data = fread($handle, $length);
  7970. if ($data === false) {
  7971. return false;
  7972. }
  7973. $rest = $length - strlen($data);
  7974. if ($rest > 0) {
  7975. $data .= $this->rfread($handle, $rest);
  7976. }
  7977. return $data;
  7978. }
  7979. /**
  7980. * Extract info from a PNG image with alpha channel using the GD library.
  7981. * @param $file (string) Name of the file containing the image.
  7982. * @param $x (float) Abscissa of the upper-left corner.
  7983. * @param $y (float) Ordinate of the upper-left corner.
  7984. * @param $wpx (float) Original width of the image in pixels.
  7985. * @param $hpx (float) original height of the image in pixels.
  7986. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7987. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7988. * @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.
  7989. * @param $link (mixed) URL or identifier returned by AddLink().
  7990. * @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>
  7991. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7992. * @param $dpi (int) dot-per-inch resolution used on resize
  7993. * @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>
  7994. * @param $filehash (string) File hash used to build unique file names.
  7995. * @author Nicola Asuni
  7996. * @protected
  7997. * @since 4.3.007 (2008-12-04)
  7998. * @see Image()
  7999. */
  8000. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  8001. if (empty($filehash)) {
  8002. $filehash = md5($file);
  8003. }
  8004. // create temp image file (without alpha channel)
  8005. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  8006. // create temp alpha file
  8007. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  8008. if (extension_loaded('imagick')) { // ImageMagick extension
  8009. // ImageMagick library
  8010. $img = new Imagick();
  8011. $img->readImage($file);
  8012. // clone image object
  8013. $imga = $img->clone();
  8014. // extract alpha channel
  8015. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  8016. $img->negateImage(true);
  8017. $img->setImageFormat('png');
  8018. $img->writeImage($tempfile_alpha);
  8019. // remove alpha channel
  8020. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  8021. $imga->setImageFormat('png');
  8022. $imga->writeImage($tempfile_plain);
  8023. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  8024. // generate images
  8025. $img = imagecreatefrompng($file);
  8026. $imgalpha = imagecreate($wpx, $hpx);
  8027. // generate gray scale palette (0 -> 255)
  8028. for ($c = 0; $c < 256; ++$c) {
  8029. ImageColorAllocate($imgalpha, $c, $c, $c);
  8030. }
  8031. // extract alpha channel
  8032. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  8033. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  8034. $color = imagecolorat($img, $xpx, $ypx);
  8035. $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)
  8036. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  8037. $alpha = $this->getGDgamma($alpha); // correct gamma
  8038. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  8039. }
  8040. }
  8041. imagepng($imgalpha, $tempfile_alpha);
  8042. imagedestroy($imgalpha);
  8043. // extract image without alpha channel
  8044. $imgplain = imagecreatetruecolor($wpx, $hpx);
  8045. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  8046. imagepng($imgplain, $tempfile_plain);
  8047. imagedestroy($imgplain);
  8048. } else {
  8049. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  8050. }
  8051. // embed mask image
  8052. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  8053. // embed image, masked with previously embedded mask
  8054. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  8055. // remove temp files
  8056. unlink($tempfile_alpha);
  8057. unlink($tempfile_plain);
  8058. }
  8059. /**
  8060. * Correct the gamma value to be used with GD library
  8061. * @param $v (float) the gamma value to be corrected
  8062. * @protected
  8063. * @since 4.3.007 (2008-12-04)
  8064. */
  8065. protected function getGDgamma($v) {
  8066. return (pow(($v / 255), 2.2) * 255);
  8067. }
  8068. /**
  8069. * Performs a line break.
  8070. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  8071. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  8072. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  8073. * @public
  8074. * @since 1.0
  8075. * @see Cell()
  8076. */
  8077. public function Ln($h='', $cell=false) {
  8078. 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'])) {
  8079. // revove vertical space from the top of the column
  8080. return;
  8081. }
  8082. if ($cell) {
  8083. if ($this->rtl) {
  8084. $cellpadding = $this->cell_padding['R'];
  8085. } else {
  8086. $cellpadding = $this->cell_padding['L'];
  8087. }
  8088. } else {
  8089. $cellpadding = 0;
  8090. }
  8091. if ($this->rtl) {
  8092. $this->x = $this->w - $this->rMargin - $cellpadding;
  8093. } else {
  8094. $this->x = $this->lMargin + $cellpadding;
  8095. }
  8096. if (is_string($h)) {
  8097. $this->y += $this->lasth;
  8098. } else {
  8099. $this->y += $h;
  8100. }
  8101. $this->newline = true;
  8102. }
  8103. /**
  8104. * Returns the relative X value of current position.
  8105. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  8106. * @return float
  8107. * @public
  8108. * @since 1.2
  8109. * @see SetX(), GetY(), SetY()
  8110. */
  8111. public function GetX() {
  8112. //Get x position
  8113. if ($this->rtl) {
  8114. return ($this->w - $this->x);
  8115. } else {
  8116. return $this->x;
  8117. }
  8118. }
  8119. /**
  8120. * Returns the absolute X value of current position.
  8121. * @return float
  8122. * @public
  8123. * @since 1.2
  8124. * @see SetX(), GetY(), SetY()
  8125. */
  8126. public function GetAbsX() {
  8127. return $this->x;
  8128. }
  8129. /**
  8130. * Returns the ordinate of the current position.
  8131. * @return float
  8132. * @public
  8133. * @since 1.0
  8134. * @see SetY(), GetX(), SetX()
  8135. */
  8136. public function GetY() {
  8137. return $this->y;
  8138. }
  8139. /**
  8140. * Defines the abscissa of the current position.
  8141. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  8142. * @param $x (float) The value of the abscissa.
  8143. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8144. * @public
  8145. * @since 1.2
  8146. * @see GetX(), GetY(), SetY(), SetXY()
  8147. */
  8148. public function SetX($x, $rtloff=false) {
  8149. if (!$rtloff AND $this->rtl) {
  8150. if ($x >= 0) {
  8151. $this->x = $this->w - $x;
  8152. } else {
  8153. $this->x = abs($x);
  8154. }
  8155. } else {
  8156. if ($x >= 0) {
  8157. $this->x = $x;
  8158. } else {
  8159. $this->x = $this->w + $x;
  8160. }
  8161. }
  8162. if ($this->x < 0) {
  8163. $this->x = 0;
  8164. }
  8165. if ($this->x > $this->w) {
  8166. $this->x = $this->w;
  8167. }
  8168. }
  8169. /**
  8170. * Moves the current abscissa back to the left margin and sets the ordinate.
  8171. * If the passed value is negative, it is relative to the bottom of the page.
  8172. * @param $y (float) The value of the ordinate.
  8173. * @param $resetx (bool) if true (default) reset the X position.
  8174. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8175. * @public
  8176. * @since 1.0
  8177. * @see GetX(), GetY(), SetY(), SetXY()
  8178. */
  8179. public function SetY($y, $resetx=true, $rtloff=false) {
  8180. if ($resetx) {
  8181. //reset x
  8182. if (!$rtloff AND $this->rtl) {
  8183. $this->x = $this->w - $this->rMargin;
  8184. } else {
  8185. $this->x = $this->lMargin;
  8186. }
  8187. }
  8188. if ($y >= 0) {
  8189. $this->y = $y;
  8190. } else {
  8191. $this->y = $this->h + $y;
  8192. }
  8193. if ($this->y < 0) {
  8194. $this->y = 0;
  8195. }
  8196. if ($this->y > $this->h) {
  8197. $this->y = $this->h;
  8198. }
  8199. }
  8200. /**
  8201. * Defines the abscissa and ordinate of the current position.
  8202. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8203. * @param $x (float) The value of the abscissa.
  8204. * @param $y (float) The value of the ordinate.
  8205. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8206. * @public
  8207. * @since 1.2
  8208. * @see SetX(), SetY()
  8209. */
  8210. public function SetXY($x, $y, $rtloff=false) {
  8211. $this->SetY($y, false, $rtloff);
  8212. $this->SetX($x, $rtloff);
  8213. }
  8214. /**
  8215. * Ouput input data and compress it if possible.
  8216. * @param $data (string) Data to output.
  8217. * @param $length (int) Data length in bytes.
  8218. * @protected
  8219. * @since 5.9.086
  8220. */
  8221. protected function sendOutputData($data, $length) {
  8222. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8223. // the content length may vary if the server is using compression
  8224. header('Content-Length: '.$length);
  8225. }
  8226. echo $data;
  8227. }
  8228. /**
  8229. * Send the document to a given destination: string, local file or browser.
  8230. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8231. * The method first calls Close() if necessary to terminate the document.
  8232. * @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.
  8233. * @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>
  8234. * @public
  8235. * @since 1.0
  8236. * @see Close()
  8237. */
  8238. public function Output($name='doc.pdf', $dest='I') {
  8239. //Output PDF to some destination
  8240. //Finish document if necessary
  8241. if ($this->state < 3) {
  8242. $this->Close();
  8243. }
  8244. //Normalize parameters
  8245. if (is_bool($dest)) {
  8246. $dest = $dest ? 'D' : 'F';
  8247. }
  8248. $dest = strtoupper($dest);
  8249. if ($dest{0} != 'F') {
  8250. $name = preg_replace('/[\s]+/', '_', $name);
  8251. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8252. }
  8253. if ($this->sign) {
  8254. // *** apply digital signature to the document ***
  8255. // get the document content
  8256. $pdfdoc = $this->getBuffer();
  8257. // remove last newline
  8258. $pdfdoc = substr($pdfdoc, 0, -1);
  8259. // Remove the original buffer
  8260. if (isset($this->diskcache) AND $this->diskcache) {
  8261. // remove buffer file from cache
  8262. unlink($this->buffer);
  8263. }
  8264. unset($this->buffer);
  8265. // remove filler space
  8266. $byterange_string_len = strlen($this->byterange_string);
  8267. // define the ByteRange
  8268. $byte_range = array();
  8269. $byte_range[0] = 0;
  8270. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8271. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8272. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8273. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8274. // replace the ByteRange
  8275. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8276. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8277. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8278. // write the document to a temporary folder
  8279. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8280. $f = fopen($tempdoc, 'wb');
  8281. if (!$f) {
  8282. $this->Error('Unable to create temporary file: '.$tempdoc);
  8283. }
  8284. $pdfdoc_length = strlen($pdfdoc);
  8285. fwrite($f, $pdfdoc, $pdfdoc_length);
  8286. fclose($f);
  8287. // get digital signature via openssl library
  8288. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8289. if (empty($this->signature_data['extracerts'])) {
  8290. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8291. } else {
  8292. 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']);
  8293. }
  8294. unlink($tempdoc);
  8295. // read signature
  8296. $signature = file_get_contents($tempsign);
  8297. unlink($tempsign);
  8298. // extract signature
  8299. $signature = substr($signature, $pdfdoc_length);
  8300. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8301. $tmparr = explode("\n\n", $signature);
  8302. $signature = $tmparr[1];
  8303. unset($tmparr);
  8304. // decode signature
  8305. $signature = base64_decode(trim($signature));
  8306. // convert signature to hex
  8307. $signature = current(unpack('H*', $signature));
  8308. $signature = str_pad($signature, $this->signature_max_length, '0');
  8309. // disable disk caching
  8310. $this->diskcache = false;
  8311. // Add signature to the document
  8312. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8313. $this->bufferlen = strlen($this->buffer);
  8314. }
  8315. switch($dest) {
  8316. case 'I': {
  8317. // Send PDF to the standard output
  8318. if (ob_get_contents()) {
  8319. $this->Error('Some data has already been output, can\'t send PDF file');
  8320. }
  8321. if (php_sapi_name() != 'cli') {
  8322. // send output to a browser
  8323. header('Content-Type: application/pdf');
  8324. if (headers_sent()) {
  8325. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8326. }
  8327. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8328. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8329. header('Pragma: public');
  8330. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8331. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8332. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8333. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8334. } else {
  8335. echo $this->getBuffer();
  8336. }
  8337. break;
  8338. }
  8339. case 'D': {
  8340. // download PDF as file
  8341. if (ob_get_contents()) {
  8342. $this->Error('Some data has already been output, can\'t send PDF file');
  8343. }
  8344. header('Content-Description: File Transfer');
  8345. if (headers_sent()) {
  8346. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8347. }
  8348. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8349. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8350. header('Pragma: public');
  8351. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8352. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8353. // force download dialog
  8354. if (strpos(php_sapi_name(), 'cgi') === false) {
  8355. header('Content-Type: application/force-download');
  8356. header('Content-Type: application/octet-stream', false);
  8357. header('Content-Type: application/download', false);
  8358. header('Content-Type: application/pdf', false);
  8359. } else {
  8360. header('Content-Type: application/pdf');
  8361. }
  8362. // use the Content-Disposition header to supply a recommended filename
  8363. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8364. header('Content-Transfer-Encoding: binary');
  8365. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8366. break;
  8367. }
  8368. case 'F':
  8369. case 'FI':
  8370. case 'FD': {
  8371. // save PDF to a local file
  8372. if ($this->diskcache) {
  8373. copy($this->buffer, $name);
  8374. } else {
  8375. $f = fopen($name, 'wb');
  8376. if (!$f) {
  8377. $this->Error('Unable to create output file: '.$name);
  8378. }
  8379. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8380. fclose($f);
  8381. }
  8382. if ($dest == 'FI') {
  8383. // send headers to browser
  8384. header('Content-Type: application/pdf');
  8385. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8386. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8387. header('Pragma: public');
  8388. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8389. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8390. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8391. $this->sendOutputData(file_get_contents($name), filesize($name));
  8392. } elseif ($dest == 'FD') {
  8393. // send headers to browser
  8394. if (ob_get_contents()) {
  8395. $this->Error('Some data has already been output, can\'t send PDF file');
  8396. }
  8397. header('Content-Description: File Transfer');
  8398. if (headers_sent()) {
  8399. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8400. }
  8401. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8402. header('Pragma: public');
  8403. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8404. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8405. // force download dialog
  8406. if (strpos(php_sapi_name(), 'cgi') === false) {
  8407. header('Content-Type: application/force-download');
  8408. header('Content-Type: application/octet-stream', false);
  8409. header('Content-Type: application/download', false);
  8410. header('Content-Type: application/pdf', false);
  8411. } else {
  8412. header('Content-Type: application/pdf');
  8413. }
  8414. // use the Content-Disposition header to supply a recommended filename
  8415. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8416. header('Content-Transfer-Encoding: binary');
  8417. $this->sendOutputData(file_get_contents($name), filesize($name));
  8418. }
  8419. break;
  8420. }
  8421. case 'E': {
  8422. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8423. $retval = 'Content-Type: application/pdf;'."\r\n";
  8424. $retval .= ' name="'.$name.'"'."\r\n";
  8425. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8426. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8427. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8428. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8429. return $retval;
  8430. }
  8431. case 'S': {
  8432. // returns PDF as a string
  8433. return $this->getBuffer();
  8434. }
  8435. default: {
  8436. $this->Error('Incorrect output destination: '.$dest);
  8437. }
  8438. }
  8439. return '';
  8440. }
  8441. /**
  8442. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8443. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8444. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8445. * @public
  8446. * @since 4.5.016 (2009-02-24)
  8447. */
  8448. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8449. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8450. // remove buffer file from cache
  8451. unlink($this->buffer);
  8452. }
  8453. foreach (array_keys(get_object_vars($this)) as $val) {
  8454. if ($destroyall OR (
  8455. ($val != 'internal_encoding')
  8456. AND ($val != 'state')
  8457. AND ($val != 'bufferlen')
  8458. AND ($val != 'buffer')
  8459. AND ($val != 'diskcache')
  8460. AND ($val != 'sign')
  8461. AND ($val != 'signature_data')
  8462. AND ($val != 'signature_max_length')
  8463. AND ($val != 'byterange_string')
  8464. )) {
  8465. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8466. unset($this->$val);
  8467. }
  8468. }
  8469. }
  8470. }
  8471. /**
  8472. * Check for locale-related bug
  8473. * @protected
  8474. */
  8475. protected function _dochecks() {
  8476. //Check for locale-related bug
  8477. if (1.1 == 1) {
  8478. $this->Error('Don\'t alter the locale before including class file');
  8479. }
  8480. //Check for decimal separator
  8481. if (sprintf('%.1F', 1.0) != '1.0') {
  8482. setlocale(LC_NUMERIC, 'C');
  8483. }
  8484. }
  8485. /**
  8486. * Return fonts path
  8487. * @return string
  8488. * @protected
  8489. */
  8490. protected function _getfontpath() {
  8491. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8492. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8493. }
  8494. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8495. }
  8496. /**
  8497. * Return an array containing variations for the basic page number alias.
  8498. * @param $a (string) Base alias.
  8499. * @return array of page number aliases
  8500. * @protected
  8501. */
  8502. protected function getInternalPageNumberAliases($a= '') {
  8503. $alias = array();
  8504. // build array of Unicode + ASCII variants (the order is important)
  8505. $alias = array('u' => array(), 'a' => array());
  8506. $u = '{'.$a.'}';
  8507. $alias['u'][] = $this->_escape($u);
  8508. if ($this->isunicode) {
  8509. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8510. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8511. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8512. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8513. }
  8514. $alias['a'][] = $this->_escape($a);
  8515. return $alias;
  8516. }
  8517. /**
  8518. * Return an array containing all internal page aliases.
  8519. * @return array of page number aliases
  8520. * @protected
  8521. */
  8522. protected function getAllInternalPageNumberAliases() {
  8523. $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);
  8524. $pnalias = array();
  8525. foreach($basic_alias as $k => $a) {
  8526. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8527. }
  8528. return $pnalias;
  8529. }
  8530. /**
  8531. * Replace page number aliases with number.
  8532. * @param $page (string) Page content.
  8533. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8534. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8535. * @return replaced page content and updated $diff parameter as array.
  8536. * @protected
  8537. */
  8538. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8539. foreach ($replace as $rep) {
  8540. foreach ($rep[3] as $a) {
  8541. if (strpos($page, $a) !== false) {
  8542. $page = str_replace($a, $rep[0], $page);
  8543. $diff += ($rep[2] - $rep[1]);
  8544. }
  8545. }
  8546. }
  8547. return array($page, $diff);
  8548. }
  8549. /**
  8550. * Replace right shift page number aliases with spaces to correct right alignment.
  8551. * This works perfectly only when using monospaced fonts.
  8552. * @param $page (string) Page content.
  8553. * @param $aliases (array) Array of page aliases.
  8554. * @param $diff (int) initial difference to add.
  8555. * @return replaced page content.
  8556. * @protected
  8557. */
  8558. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8559. foreach ($aliases as $type => $alias) {
  8560. foreach ($alias as $a) {
  8561. // find position of compensation factor
  8562. $startnum = (strpos($a, ':') + 1);
  8563. $a = substr($a, 0, $startnum);
  8564. if (($pos = strpos($page, $a)) !== false) {
  8565. // end of alias
  8566. $endnum = strpos($page, '}', $pos);
  8567. // string to be replaced
  8568. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8569. // get compensation factor
  8570. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8571. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8572. $ratio = floatval($ratio);
  8573. if ($type == 'u') {
  8574. $chrdiff = floor(($diff + 12) * $ratio);
  8575. $shift = str_repeat(' ', $chrdiff);
  8576. $shift = $this->UTF8ToUTF16BE($shift, false);
  8577. } else {
  8578. $chrdiff = floor(($diff + 11) * $ratio);
  8579. $shift = str_repeat(' ', $chrdiff);
  8580. }
  8581. $page = str_replace($aa, $shift, $page);
  8582. }
  8583. }
  8584. }
  8585. return $page;
  8586. }
  8587. /**
  8588. * Set page boxes to be included on page descriptions.
  8589. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  8590. * @protected
  8591. */
  8592. protected function setPageBoxTypes($boxes) {
  8593. $validboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8594. $this->page_boxes = array();
  8595. foreach ($boxes as $box) {
  8596. if (in_array($box, $validboxes)) {
  8597. $this->page_boxes[] = $box;
  8598. }
  8599. }
  8600. }
  8601. /**
  8602. * Output pages (and replace page number aliases).
  8603. * @protected
  8604. */
  8605. protected function _putpages() {
  8606. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8607. // get internal aliases for page numbers
  8608. $pnalias = $this->getAllInternalPageNumberAliases();
  8609. $num_pages = $this->numpages;
  8610. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8611. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8612. $ptp_num_chars = $this->GetNumChars($ptpa);
  8613. $pagegroupnum = 0;
  8614. $groupnum = 0;
  8615. $ptgu = 1;
  8616. $ptga = 1;
  8617. for ($n = 1; $n <= $num_pages; ++$n) {
  8618. // get current page
  8619. $temppage = $this->getPageBuffer($n);
  8620. $pagelen = strlen($temppage);
  8621. // set replacements for total pages number
  8622. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8623. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8624. $pnp_num_chars = $this->GetNumChars($pnpa);
  8625. $pdiff = 0; // difference used for right shift alignment of page numbers
  8626. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8627. if (!empty($this->pagegroups)) {
  8628. if (isset($this->newpagegroup[$n])) {
  8629. $pagegroupnum = 0;
  8630. ++$groupnum;
  8631. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8632. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8633. $ptg_num_chars = $this->GetNumChars($ptga);
  8634. }
  8635. ++$pagegroupnum;
  8636. $pnga = $this->formatPageNumber($pagegroupnum);
  8637. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8638. $png_num_chars = $this->GetNumChars($pnga);
  8639. // replace page numbers
  8640. $replace = array();
  8641. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8642. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8643. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8644. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8645. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8646. }
  8647. // replace page numbers
  8648. $replace = array();
  8649. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8650. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8651. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8652. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8653. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8654. // replace right shift alias
  8655. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8656. // replace EPS marker
  8657. $temppage = str_replace($this->epsmarker, '', $temppage);
  8658. //Page
  8659. $this->page_obj_id[$n] = $this->_newobj();
  8660. $out = '<<';
  8661. $out .= ' /Type /Page';
  8662. $out .= ' /Parent 1 0 R';
  8663. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  8664. $out .= ' /Resources 2 0 R';
  8665. foreach ($this->page_boxes as $box) {
  8666. $out .= ' /'.$box;
  8667. $out .= sprintf(' [%F %F %F %F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  8668. }
  8669. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8670. $out .= ' /BoxColorInfo <<';
  8671. foreach ($this->page_boxes as $box) {
  8672. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8673. $out .= ' /'.$box.' <<';
  8674. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8675. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8676. $out .= ' /C [';
  8677. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  8678. $out .= ' ]';
  8679. }
  8680. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8681. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8682. }
  8683. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8684. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8685. }
  8686. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8687. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8688. $out .= ' /D [';
  8689. foreach ($dashes as $dash) {
  8690. $out .= sprintf(' %F', ($dash * $this->k));
  8691. }
  8692. $out .= ' ]';
  8693. }
  8694. $out .= ' >>';
  8695. }
  8696. }
  8697. $out .= ' >>';
  8698. }
  8699. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8700. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8701. if (!$this->pdfa_mode) {
  8702. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8703. }
  8704. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8705. // page transitions
  8706. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8707. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8708. }
  8709. $out .= ' /Trans <<';
  8710. $out .= ' /Type /Trans';
  8711. if (isset($this->pagedim[$n]['trans']['S'])) {
  8712. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8713. }
  8714. if (isset($this->pagedim[$n]['trans']['D'])) {
  8715. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8716. }
  8717. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8718. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8719. }
  8720. if (isset($this->pagedim[$n]['trans']['M'])) {
  8721. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8722. }
  8723. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8724. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8725. }
  8726. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8727. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8728. }
  8729. if (isset($this->pagedim[$n]['trans']['B'])) {
  8730. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8731. }
  8732. $out .= ' >>';
  8733. }
  8734. $out .= $this->_getannotsrefs($n);
  8735. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8736. $out .= ' >>';
  8737. $out .= "\n".'endobj';
  8738. $this->_out($out);
  8739. //Page content
  8740. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8741. $this->_newobj();
  8742. $p = $this->_getrawstream($p);
  8743. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8744. if ($this->diskcache) {
  8745. // remove temporary files
  8746. unlink($this->pages[$n]);
  8747. }
  8748. }
  8749. //Pages root
  8750. $out = $this->_getobj(1)."\n";
  8751. $out .= '<< /Type /Pages /Kids [';
  8752. foreach($this->page_obj_id as $page_obj) {
  8753. $out .= ' '.$page_obj.' 0 R';
  8754. }
  8755. $out .= ' ] /Count '.$num_pages.' >>';
  8756. $out .= "\n".'endobj';
  8757. $this->_out($out);
  8758. }
  8759. /**
  8760. * Output references to page annotations
  8761. * @param $n (int) page number
  8762. * @protected
  8763. * @author Nicola Asuni
  8764. * @since 4.7.000 (2008-08-29)
  8765. * @deprecated
  8766. */
  8767. protected function _putannotsrefs($n) {
  8768. $this->_out($this->_getannotsrefs($n));
  8769. }
  8770. /**
  8771. * Get references to page annotations.
  8772. * @param $n (int) page number
  8773. * @return string
  8774. * @protected
  8775. * @author Nicola Asuni
  8776. * @since 5.0.010 (2010-05-17)
  8777. */
  8778. protected function _getannotsrefs($n) {
  8779. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8780. return '';
  8781. }
  8782. $out = ' /Annots [';
  8783. if (isset($this->PageAnnots[$n])) {
  8784. foreach ($this->PageAnnots[$n] as $key => $val) {
  8785. if (!in_array($val['n'], $this->radio_groups)) {
  8786. $out .= ' '.$val['n'].' 0 R';
  8787. }
  8788. }
  8789. // add radiobutton groups
  8790. if (isset($this->radiobutton_groups[$n])) {
  8791. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8792. if (isset($data['n'])) {
  8793. $out .= ' '.$data['n'].' 0 R';
  8794. }
  8795. }
  8796. }
  8797. }
  8798. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8799. // set reference for signature object
  8800. $out .= ' '.$this->sig_obj_id.' 0 R';
  8801. }
  8802. if (!empty($this->empty_signature_appearance)) {
  8803. foreach ($this->empty_signature_appearance as $esa) {
  8804. if ($esa['page'] == $n) {
  8805. // set reference for empty signature objects
  8806. $out .= ' '.$esa['objid'].' 0 R';
  8807. }
  8808. }
  8809. }
  8810. $out .= ' ]';
  8811. return $out;
  8812. }
  8813. /**
  8814. * Output annotations objects for all pages.
  8815. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8816. * See section 12.5 of PDF 32000_2008 reference.
  8817. * @protected
  8818. * @author Nicola Asuni
  8819. * @since 4.0.018 (2008-08-06)
  8820. */
  8821. protected function _putannotsobjs() {
  8822. // reset object counter
  8823. for ($n=1; $n <= $this->numpages; ++$n) {
  8824. if (isset($this->PageAnnots[$n])) {
  8825. // set page annotations
  8826. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8827. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8828. // create annotation object for grouping radiobuttons
  8829. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8830. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8831. $annots = '<<';
  8832. $annots .= ' /Type /Annot';
  8833. $annots .= ' /Subtype /Widget';
  8834. $annots .= ' /Rect [0 0 0 0]';
  8835. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  8836. // read only
  8837. $annots .= ' /F 68';
  8838. $annots .= ' /Ff 49153';
  8839. } else {
  8840. $annots .= ' /F 4'; // default print for PDF/A
  8841. $annots .= ' /Ff 49152';
  8842. }
  8843. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8844. $annots .= ' /FT /Btn';
  8845. $annots .= ' /Kids [';
  8846. $defval = '';
  8847. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8848. if (isset($data['kid'])) {
  8849. $annots .= ' '.$data['kid'].' 0 R';
  8850. if ($data['def'] !== 'Off') {
  8851. $defval = $data['def'];
  8852. }
  8853. }
  8854. }
  8855. $annots .= ' ]';
  8856. if (!empty($defval)) {
  8857. $annots .= ' /V /'.$defval;
  8858. }
  8859. $annots .= ' >>';
  8860. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8861. $this->form_obj_id[] = $radio_button_obj_id;
  8862. // store object id to be used on Parent entry of Kids
  8863. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8864. }
  8865. $formfield = false;
  8866. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8867. $a = $pl['x'] * $this->k;
  8868. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8869. $c = $pl['w'] * $this->k;
  8870. $d = $pl['h'] * $this->k;
  8871. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  8872. // create new annotation object
  8873. $annots = '<</Type /Annot';
  8874. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8875. $annots .= ' /Rect ['.$rect.']';
  8876. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8877. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8878. $annots .= ' /FT /'.$pl['opt']['ft'];
  8879. $formfield = true;
  8880. }
  8881. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8882. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8883. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8884. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  8885. if (isset($pl['opt']['f'])) {
  8886. $fval = 0;
  8887. if (is_array($pl['opt']['f'])) {
  8888. foreach ($pl['opt']['f'] as $f) {
  8889. switch (strtolower($f)) {
  8890. case 'invisible': {
  8891. $fval += 1 << 0;
  8892. break;
  8893. }
  8894. case 'hidden': {
  8895. $fval += 1 << 1;
  8896. break;
  8897. }
  8898. case 'print': {
  8899. $fval += 1 << 2;
  8900. break;
  8901. }
  8902. case 'nozoom': {
  8903. $fval += 1 << 3;
  8904. break;
  8905. }
  8906. case 'norotate': {
  8907. $fval += 1 << 4;
  8908. break;
  8909. }
  8910. case 'noview': {
  8911. $fval += 1 << 5;
  8912. break;
  8913. }
  8914. case 'readonly': {
  8915. $fval += 1 << 6;
  8916. break;
  8917. }
  8918. case 'locked': {
  8919. $fval += 1 << 8;
  8920. break;
  8921. }
  8922. case 'togglenoview': {
  8923. $fval += 1 << 9;
  8924. break;
  8925. }
  8926. case 'lockedcontents': {
  8927. $fval += 1 << 10;
  8928. break;
  8929. }
  8930. default: {
  8931. break;
  8932. }
  8933. }
  8934. }
  8935. } else {
  8936. $fval = intval($pl['opt']['f']);
  8937. }
  8938. } else {
  8939. $fval = 4;
  8940. }
  8941. if ($this->pdfa_mode) {
  8942. // force print flag for PDF/A mode
  8943. $fval |= 4;
  8944. }
  8945. $annots .= ' /F '.intval($fval);
  8946. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8947. $annots .= ' /AS /'.$pl['opt']['as'];
  8948. }
  8949. if (isset($pl['opt']['ap'])) {
  8950. // appearance stream
  8951. $annots .= ' /AP <<';
  8952. if (is_array($pl['opt']['ap'])) {
  8953. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8954. // $apmode can be: n = normal; r = rollover; d = down;
  8955. $annots .= ' /'.strtoupper($apmode);
  8956. if (is_array($apdef)) {
  8957. $annots .= ' <<';
  8958. foreach ($apdef as $apstate => $stream) {
  8959. // reference to XObject that define the appearance for this mode-state
  8960. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8961. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8962. }
  8963. $annots .= ' >>';
  8964. } else {
  8965. // reference to XObject that define the appearance for this mode
  8966. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8967. $annots .= ' '.$apsobjid.' 0 R';
  8968. }
  8969. }
  8970. } else {
  8971. $annots .= $pl['opt']['ap'];
  8972. }
  8973. $annots .= ' >>';
  8974. }
  8975. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8976. $annots .= ' /BS <<';
  8977. $annots .= ' /Type /Border';
  8978. if (isset($pl['opt']['bs']['w'])) {
  8979. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8980. }
  8981. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8982. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8983. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8984. }
  8985. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8986. $annots .= ' /D [';
  8987. foreach ($pl['opt']['bs']['d'] as $cord) {
  8988. $annots .= ' '.intval($cord);
  8989. }
  8990. $annots .= ']';
  8991. }
  8992. $annots .= ' >>';
  8993. } else {
  8994. $annots .= ' /Border [';
  8995. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8996. $annots .= intval($pl['opt']['border'][0]).' ';
  8997. $annots .= intval($pl['opt']['border'][1]).' ';
  8998. $annots .= intval($pl['opt']['border'][2]);
  8999. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  9000. $annots .= ' [';
  9001. foreach ($pl['opt']['border'][3] as $dash) {
  9002. $annots .= intval($dash).' ';
  9003. }
  9004. $annots .= ']';
  9005. }
  9006. } else {
  9007. $annots .= '0 0 0';
  9008. }
  9009. $annots .= ']';
  9010. }
  9011. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  9012. $annots .= ' /BE <<';
  9013. $bstyles = array('S', 'C');
  9014. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  9015. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  9016. } else {
  9017. $annots .= ' /S /S';
  9018. }
  9019. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  9020. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  9021. }
  9022. $annots .= '>>';
  9023. }
  9024. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  9025. $annots .= ' /C '.$this->getColorStringFromArray($pl['opt']['c']);
  9026. }
  9027. //$annots .= ' /StructParent ';
  9028. //$annots .= ' /OC ';
  9029. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  9030. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  9031. // this is a markup type
  9032. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9033. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  9034. }
  9035. //$annots .= ' /Popup ';
  9036. if (isset($pl['opt']['ca'])) {
  9037. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  9038. }
  9039. if (isset($pl['opt']['rc'])) {
  9040. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  9041. }
  9042. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  9043. //$annots .= ' /IRT ';
  9044. if (isset($pl['opt']['subj'])) {
  9045. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  9046. }
  9047. //$annots .= ' /RT ';
  9048. //$annots .= ' /IT ';
  9049. //$annots .= ' /ExData ';
  9050. }
  9051. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  9052. // Annotation types
  9053. switch (strtolower($pl['opt']['subtype'])) {
  9054. case 'text': {
  9055. if (isset($pl['opt']['open'])) {
  9056. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  9057. }
  9058. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  9059. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9060. $annots .= ' /Name /'.$pl['opt']['name'];
  9061. } else {
  9062. $annots .= ' /Name /Note';
  9063. }
  9064. $statemodels = array('Marked', 'Review');
  9065. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  9066. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  9067. } else {
  9068. $pl['opt']['statemodel'] = 'Marked';
  9069. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  9070. }
  9071. if ($pl['opt']['statemodel'] == 'Marked') {
  9072. $states = array('Accepted', 'Unmarked');
  9073. } else {
  9074. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  9075. }
  9076. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  9077. $annots .= ' /State /'.$pl['opt']['state'];
  9078. } else {
  9079. if ($pl['opt']['statemodel'] == 'Marked') {
  9080. $annots .= ' /State /Unmarked';
  9081. } else {
  9082. $annots .= ' /State /None';
  9083. }
  9084. }
  9085. break;
  9086. }
  9087. case 'link': {
  9088. if (is_string($pl['txt'])) {
  9089. // external URI link
  9090. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  9091. } else {
  9092. // internal link
  9093. $l = $this->links[$pl['txt']];
  9094. if (isset($this->page_obj_id[($l[0])])) {
  9095. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  9096. }
  9097. }
  9098. $hmodes = array('N', 'I', 'O', 'P');
  9099. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  9100. $annots .= ' /H /'.$pl['opt']['h'];
  9101. } else {
  9102. $annots .= ' /H /I';
  9103. }
  9104. //$annots .= ' /PA ';
  9105. //$annots .= ' /Quadpoints ';
  9106. break;
  9107. }
  9108. case 'freetext': {
  9109. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9110. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9111. }
  9112. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9113. $annots .= ' /Q '.intval($pl['opt']['q']);
  9114. }
  9115. if (isset($pl['opt']['rc'])) {
  9116. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  9117. }
  9118. if (isset($pl['opt']['ds'])) {
  9119. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  9120. }
  9121. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  9122. $annots .= ' /CL [';
  9123. foreach ($pl['opt']['cl'] as $cl) {
  9124. $annots .= sprintf('%F ', $cl * $this->k);
  9125. }
  9126. $annots .= ']';
  9127. }
  9128. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  9129. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  9130. $annots .= ' /IT /'.$pl['opt']['it'];
  9131. }
  9132. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  9133. $l = $pl['opt']['rd'][0] * $this->k;
  9134. $r = $pl['opt']['rd'][1] * $this->k;
  9135. $t = $pl['opt']['rd'][2] * $this->k;
  9136. $b = $pl['opt']['rd'][3] * $this->k;
  9137. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  9138. }
  9139. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  9140. $annots .= ' /LE /'.$pl['opt']['le'];
  9141. }
  9142. break;
  9143. }
  9144. case 'line': {
  9145. break;
  9146. }
  9147. case 'square': {
  9148. break;
  9149. }
  9150. case 'circle': {
  9151. break;
  9152. }
  9153. case 'polygon': {
  9154. break;
  9155. }
  9156. case 'polyline': {
  9157. break;
  9158. }
  9159. case 'highlight': {
  9160. break;
  9161. }
  9162. case 'underline': {
  9163. break;
  9164. }
  9165. case 'squiggly': {
  9166. break;
  9167. }
  9168. case 'strikeout': {
  9169. break;
  9170. }
  9171. case 'stamp': {
  9172. break;
  9173. }
  9174. case 'caret': {
  9175. break;
  9176. }
  9177. case 'ink': {
  9178. break;
  9179. }
  9180. case 'popup': {
  9181. break;
  9182. }
  9183. case 'fileattachment': {
  9184. if ($this->pdfa_mode) {
  9185. // embedded files are not allowed in PDF/A mode
  9186. break;
  9187. }
  9188. if (!isset($pl['opt']['fs'])) {
  9189. break;
  9190. }
  9191. $filename = basename($pl['opt']['fs']);
  9192. if (isset($this->embeddedfiles[$filename]['n'])) {
  9193. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9194. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9195. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9196. $annots .= ' /Name /'.$pl['opt']['name'];
  9197. } else {
  9198. $annots .= ' /Name /PushPin';
  9199. }
  9200. }
  9201. break;
  9202. }
  9203. case 'sound': {
  9204. if (!isset($pl['opt']['fs'])) {
  9205. break;
  9206. }
  9207. $filename = basename($pl['opt']['fs']);
  9208. if (isset($this->embeddedfiles[$filename]['n'])) {
  9209. // ... TO BE COMPLETED ...
  9210. // /R /C /B /E /CO /CP
  9211. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9212. $iconsapp = array('Speaker', 'Mic');
  9213. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9214. $annots .= ' /Name /'.$pl['opt']['name'];
  9215. } else {
  9216. $annots .= ' /Name /Speaker';
  9217. }
  9218. }
  9219. break;
  9220. }
  9221. case 'movie': {
  9222. break;
  9223. }
  9224. case 'widget': {
  9225. $hmode = array('N', 'I', 'O', 'P', 'T');
  9226. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9227. $annots .= ' /H /'.$pl['opt']['h'];
  9228. }
  9229. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9230. $annots .= ' /MK <<';
  9231. if (isset($pl['opt']['mk']['r'])) {
  9232. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9233. }
  9234. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9235. $annots .= ' /BC '.$this->getColorStringFromArray($pl['opt']['mk']['bc']);
  9236. }
  9237. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9238. $annots .= ' /BG '.$this->getColorStringFromArray($pl['opt']['mk']['bg']);
  9239. }
  9240. if (isset($pl['opt']['mk']['ca'])) {
  9241. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9242. }
  9243. if (isset($pl['opt']['mk']['rc'])) {
  9244. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9245. }
  9246. if (isset($pl['opt']['mk']['ac'])) {
  9247. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9248. }
  9249. if (isset($pl['opt']['mk']['i'])) {
  9250. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9251. if ($info !== false) {
  9252. $annots .= ' /I '.$info['n'].' 0 R';
  9253. }
  9254. }
  9255. if (isset($pl['opt']['mk']['ri'])) {
  9256. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9257. if ($info !== false) {
  9258. $annots .= ' /RI '.$info['n'].' 0 R';
  9259. }
  9260. }
  9261. if (isset($pl['opt']['mk']['ix'])) {
  9262. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9263. if ($info !== false) {
  9264. $annots .= ' /IX '.$info['n'].' 0 R';
  9265. }
  9266. }
  9267. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9268. $annots .= ' /IF <<';
  9269. $if_sw = array('A', 'B', 'S', 'N');
  9270. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9271. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9272. }
  9273. $if_s = array('A', 'P');
  9274. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9275. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9276. }
  9277. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9278. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9279. }
  9280. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9281. $annots .= ' /FB true';
  9282. }
  9283. $annots .= '>>';
  9284. }
  9285. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9286. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9287. }
  9288. $annots .= '>>';
  9289. } // end MK
  9290. // --- Entries for field dictionaries ---
  9291. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9292. // set parent
  9293. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9294. }
  9295. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9296. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9297. }
  9298. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9299. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9300. }
  9301. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9302. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9303. }
  9304. if (isset($pl['opt']['ff'])) {
  9305. if (is_array($pl['opt']['ff'])) {
  9306. // array of bit settings
  9307. $flag = 0;
  9308. foreach($pl['opt']['ff'] as $val) {
  9309. $flag += 1 << ($val - 1);
  9310. }
  9311. } else {
  9312. $flag = intval($pl['opt']['ff']);
  9313. }
  9314. $annots .= ' /Ff '.$flag;
  9315. }
  9316. if (isset($pl['opt']['maxlen'])) {
  9317. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9318. }
  9319. if (isset($pl['opt']['v'])) {
  9320. $annots .= ' /V';
  9321. if (is_array($pl['opt']['v'])) {
  9322. foreach ($pl['opt']['v'] AS $optval) {
  9323. if (is_float($optval)) {
  9324. $optval = sprintf('%F', $optval);
  9325. }
  9326. $annots .= ' '.$optval;
  9327. }
  9328. } else {
  9329. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9330. }
  9331. }
  9332. if (isset($pl['opt']['dv'])) {
  9333. $annots .= ' /DV';
  9334. if (is_array($pl['opt']['dv'])) {
  9335. foreach ($pl['opt']['dv'] AS $optval) {
  9336. if (is_float($optval)) {
  9337. $optval = sprintf('%F', $optval);
  9338. }
  9339. $annots .= ' '.$optval;
  9340. }
  9341. } else {
  9342. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9343. }
  9344. }
  9345. if (isset($pl['opt']['rv'])) {
  9346. $annots .= ' /RV';
  9347. if (is_array($pl['opt']['rv'])) {
  9348. foreach ($pl['opt']['rv'] AS $optval) {
  9349. if (is_float($optval)) {
  9350. $optval = sprintf('%F', $optval);
  9351. }
  9352. $annots .= ' '.$optval;
  9353. }
  9354. } else {
  9355. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9356. }
  9357. }
  9358. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9359. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9360. }
  9361. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9362. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9363. }
  9364. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9365. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9366. }
  9367. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9368. $annots .= ' /Q '.intval($pl['opt']['q']);
  9369. }
  9370. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9371. $annots .= ' /Opt [';
  9372. foreach($pl['opt']['opt'] AS $copt) {
  9373. if (is_array($copt)) {
  9374. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9375. } else {
  9376. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9377. }
  9378. }
  9379. $annots .= ']';
  9380. }
  9381. if (isset($pl['opt']['ti'])) {
  9382. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9383. }
  9384. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9385. $annots .= ' /I [';
  9386. foreach($pl['opt']['i'] AS $copt) {
  9387. $annots .= intval($copt).' ';
  9388. }
  9389. $annots .= ']';
  9390. }
  9391. break;
  9392. }
  9393. case 'screen': {
  9394. break;
  9395. }
  9396. case 'printermark': {
  9397. break;
  9398. }
  9399. case 'trapnet': {
  9400. break;
  9401. }
  9402. case 'watermark': {
  9403. break;
  9404. }
  9405. case '3d': {
  9406. break;
  9407. }
  9408. default: {
  9409. break;
  9410. }
  9411. }
  9412. $annots .= '>>';
  9413. // create new annotation object
  9414. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9415. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9416. // store reference of form object
  9417. $this->form_obj_id[] = $annot_obj_id;
  9418. }
  9419. }
  9420. }
  9421. } // end for each page
  9422. }
  9423. /**
  9424. * Put appearance streams XObject used to define annotation's appearance states.
  9425. * @param $w (int) annotation width
  9426. * @param $h (int) annotation height
  9427. * @param $stream (string) appearance stream
  9428. * @return int object ID
  9429. * @protected
  9430. * @since 4.8.001 (2009-09-09)
  9431. */
  9432. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9433. $stream = trim($stream);
  9434. $out = $this->_getobj()."\n";
  9435. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9436. $out .= '<<';
  9437. $out .= ' /Type /XObject';
  9438. $out .= ' /Subtype /Form';
  9439. $out .= ' /FormType 1';
  9440. if ($this->compress) {
  9441. $stream = gzcompress($stream);
  9442. $out .= ' /Filter /FlateDecode';
  9443. }
  9444. $rect = sprintf('%F %F', $w, $h);
  9445. $out .= ' /BBox [0 0 '.$rect.']';
  9446. $out .= ' /Matrix [1 0 0 1 0 0]';
  9447. $out .= ' /Resources 2 0 R';
  9448. $stream = $this->_getrawstream($stream);
  9449. $out .= ' /Length '.strlen($stream);
  9450. $out .= ' >>';
  9451. $out .= ' stream'."\n".$stream."\n".'endstream';
  9452. $out .= "\n".'endobj';
  9453. $this->_out($out);
  9454. return $this->n;
  9455. }
  9456. /**
  9457. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9458. * @param $str (string) string from where to extract value
  9459. * @param $offset (int) point from where to read the data
  9460. * @return int 32 bit value
  9461. * @author Nicola Asuni
  9462. * @protected
  9463. * @since 5.2.000 (2010-06-02)
  9464. */
  9465. protected function _getULONG($str, $offset) {
  9466. $v = unpack('Ni', substr($str, $offset, 4));
  9467. return $v['i'];
  9468. }
  9469. /**
  9470. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9471. * @param $str (string) string from where to extract value
  9472. * @param $offset (int) point from where to read the data
  9473. * @return int 16 bit value
  9474. * @author Nicola Asuni
  9475. * @protected
  9476. * @since 5.2.000 (2010-06-02)
  9477. */
  9478. protected function _getUSHORT($str, $offset) {
  9479. $v = unpack('ni', substr($str, $offset, 2));
  9480. return $v['i'];
  9481. }
  9482. /**
  9483. * Get SHORT from string (Big Endian 16-bit signed integer).
  9484. * @param $str (string) String from where to extract value.
  9485. * @param $offset (int) Point from where to read the data.
  9486. * @return int 16 bit value
  9487. * @author Nicola Asuni
  9488. * @protected
  9489. * @since 5.2.000 (2010-06-02)
  9490. */
  9491. protected function _getSHORT($str, $offset) {
  9492. $v = unpack('si', substr($str, $offset, 2));
  9493. return $v['i'];
  9494. }
  9495. /**
  9496. * Get FWORD from string (Big Endian 16-bit signed integer).
  9497. * @param $str (string) String from where to extract value.
  9498. * @param $offset (int) Point from where to read the data.
  9499. * @return int 16 bit value
  9500. * @author Nicola Asuni
  9501. * @protected
  9502. * @since 5.9.123 (2011-09-30)
  9503. */
  9504. protected function _getFWORD($str, $offset) {
  9505. $v = $this->_getUSHORT($str, $offset);
  9506. if ($v > 0x7fff) {
  9507. $v -= 0x10000;
  9508. }
  9509. return $v;
  9510. }
  9511. /**
  9512. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  9513. * @param $str (string) string from where to extract value
  9514. * @param $offset (int) point from where to read the data
  9515. * @return int 16 bit value
  9516. * @author Nicola Asuni
  9517. * @protected
  9518. * @since 5.9.123 (2011-09-30)
  9519. */
  9520. protected function _getUFWORD($str, $offset) {
  9521. $v = $this->_getUSHORT($str, $offset);
  9522. return $v;
  9523. }
  9524. /**
  9525. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9526. * @param $str (string) string from where to extract value
  9527. * @param $offset (int) point from where to read the data
  9528. * @return int 16 bit value
  9529. * @author Nicola Asuni
  9530. * @protected
  9531. * @since 5.9.123 (2011-09-30)
  9532. */
  9533. protected function _getFIXED($str, $offset) {
  9534. // mantissa
  9535. $m = $this->_getFWORD($str, $offset);
  9536. // fraction
  9537. $f = $this->_getUSHORT($str, ($offset + 2));
  9538. $v = floatval(''.$m.'.'.$f.'');
  9539. return $v;
  9540. }
  9541. /**
  9542. * Get BYTE from string (8-bit unsigned integer).
  9543. * @param $str (string) String from where to extract value.
  9544. * @param $offset (int) Point from where to read the data.
  9545. * @return int 8 bit value
  9546. * @author Nicola Asuni
  9547. * @protected
  9548. * @since 5.2.000 (2010-06-02)
  9549. */
  9550. protected function _getBYTE($str, $offset) {
  9551. $v = unpack('Ci', substr($str, $offset, 1));
  9552. return $v['i'];
  9553. }
  9554. /**
  9555. * Update the CIDToGIDMap string with a new value.
  9556. * @param $map (string) CIDToGIDMap.
  9557. * @param $cid (int) CID value.
  9558. * @param $gid (int) GID value.
  9559. * @return (string) CIDToGIDMap.
  9560. * @author Nicola Asuni
  9561. * @protected
  9562. * @since 5.9.123 (2011-09-29)
  9563. */
  9564. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9565. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9566. if ($gid > 0xFFFF) {
  9567. $gid -= 0x10000;
  9568. }
  9569. $map[($cid * 2)] = chr($gid >> 8);
  9570. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9571. }
  9572. return $map;
  9573. }
  9574. /**
  9575. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9576. * @param $fontfile (string) Font file (full path).
  9577. * @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.
  9578. * @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.
  9579. * @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.
  9580. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9581. * @param $platid (int) Platform ID for CMAP table to extract (when building a Unicode font for Windows this value should be 3, for Macintosh should be 1).
  9582. * @param $encid (int) Encoding ID for CMAP table to extract (when building a Unicode font for Windows this value should be 1, for Macintosh should be 0). When Platform ID is 3, legal values for Encoding ID are: 0=Symbol, 1=Unicode, 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab, 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
  9583. * @return (string) TCPDF font name.
  9584. * @author Nicola Asuni
  9585. * @public
  9586. * @since 5.9.123 (2010-09-30)
  9587. */
  9588. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1) {
  9589. if (!file_exists($fontfile)) {
  9590. $this->Error('Could not find file: '.$fontfile.'');
  9591. }
  9592. // font metrics
  9593. $fmetric = array();
  9594. // build new font name for TCPDF compatibility
  9595. $font_path_parts = pathinfo($fontfile);
  9596. if (!isset($font_path_parts['filename'])) {
  9597. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9598. }
  9599. $font_name = strtolower($font_path_parts['filename']);
  9600. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9601. $search = array('bold', 'oblique', 'italic', 'regular');
  9602. $replace = array('b', 'i', 'i', '');
  9603. $font_name = str_replace($search, $replace, $font_name);
  9604. if (empty($font_name)) {
  9605. // set generic name
  9606. $font_name = 'tcpdffont';
  9607. }
  9608. // set output path
  9609. if (empty($outpath)) {
  9610. $outpath = $this->_getfontpath();
  9611. }
  9612. // check if this font already exist
  9613. if (file_exists($outpath.$font_name.'.php')) {
  9614. // this font already exist (delete it from fonts folder to rebuild it)
  9615. return $font_name;
  9616. }
  9617. $fmetric['file'] = $font_name.'.z';
  9618. $fmetric['ctg'] = $font_name.'.ctg.z';
  9619. // get font data
  9620. $font = file_get_contents($fontfile);
  9621. $fmetric['originalsize'] = strlen($font);
  9622. // autodetect font type
  9623. if (empty($fonttype)) {
  9624. if ($this->_getULONG($font, 0) == 0x10000) {
  9625. // True Type (Unicode or not)
  9626. $fonttype = 'TrueTypeUnicode';
  9627. } elseif (substr($font, 0, 4) == 'OTTO') {
  9628. // Open Type (Unicode or not)
  9629. $this->Error('Unsupported font format: OpenType with CFF data.');
  9630. } else {
  9631. // Type 1
  9632. $fonttype = 'Type1';
  9633. }
  9634. }
  9635. // set font type
  9636. switch ($fonttype) {
  9637. case 'CID0CT':
  9638. case 'CID0CS':
  9639. case 'CID0KR':
  9640. case 'CID0JP': {
  9641. $fmetric['type'] = 'cidfont0';
  9642. break;
  9643. }
  9644. case 'Type1': {
  9645. $fmetric['type'] = 'Type1';
  9646. if (empty($enc) AND (($flags & 4) == 0)) {
  9647. $enc = 'cp1252';
  9648. }
  9649. break;
  9650. }
  9651. case 'TrueType': {
  9652. $fmetric['type'] = 'TrueType';
  9653. break;
  9654. }
  9655. case 'TrueTypeUnicode':
  9656. default: {
  9657. $fmetric['type'] = 'TrueTypeUnicode';
  9658. break;
  9659. }
  9660. }
  9661. // set encoding maps (if any)
  9662. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9663. $fmetric['diff'] = '';
  9664. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9665. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9666. // build differences from reference encoding
  9667. $enc_ref = $this->encmaps->encmap['cp1252'];
  9668. $enc_target = $this->encmaps->encmap[$enc];
  9669. $last = 0;
  9670. for ($i = 32; $i <= 255; ++$i) {
  9671. if ($enc_target != $enc_ref[$i]) {
  9672. if ($i != ($last + 1)) {
  9673. $fmetric['diff'] .= $i.' ';
  9674. }
  9675. $last = $i;
  9676. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9677. }
  9678. }
  9679. }
  9680. }
  9681. // parse the font by type
  9682. if ($fmetric['type'] == 'Type1') {
  9683. // ---------- TYPE 1 ----------
  9684. // read first segment
  9685. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9686. if ($a['marker'] != 128) {
  9687. $this->Error('Font file is not a valid binary Type1');
  9688. }
  9689. $fmetric['size1'] = $a['size'];
  9690. $data = substr($font, 6, $fmetric['size1']);
  9691. // read second segment
  9692. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9693. if ($a['marker'] != 128) {
  9694. $this->Error('Font file is not a valid binary Type1');
  9695. }
  9696. $fmetric['size2'] = $a['size'];
  9697. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9698. $data .= $encrypted;
  9699. // store compressed font
  9700. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9701. fwrite($fp, gzcompress($data));
  9702. fclose($fp);
  9703. // get font info
  9704. $fmetric['Flags'] = $flags;
  9705. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9706. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9707. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9708. $fmetric['bbox'] = trim($matches[1]);
  9709. $bv = explode(' ', $fmetric['bbox']);
  9710. $fmetric['Ascent'] = intval($bv[3]);
  9711. $fmetric['Descent'] = intval($bv[1]);
  9712. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9713. $fmetric['italicAngle'] = intval($matches[1]);
  9714. if ($fmetric['italicAngle'] != 0) {
  9715. $fmetric['Flags'] |= 64;
  9716. }
  9717. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9718. $fmetric['underlinePosition'] = intval($matches[1]);
  9719. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9720. $fmetric['underlineThickness'] = intval($matches[1]);
  9721. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9722. if ($matches[1] == 'true') {
  9723. $fmetric['Flags'] |= 1;
  9724. }
  9725. // get internal map
  9726. $imap = array();
  9727. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9728. foreach ($fmap as $v) {
  9729. $imap[$v[2]] = $v[1];
  9730. }
  9731. }
  9732. // decrypt eexec encrypted part
  9733. $r = 55665; // eexec encryption constant
  9734. $c1 = 52845;
  9735. $c2 = 22719;
  9736. $elen = strlen($encrypted);
  9737. $eplain = '';
  9738. for ($i = 0; $i < $elen; ++$i) {
  9739. $chr = ord($encrypted[$i]);
  9740. $eplain .= chr($chr ^ ($r >> 8));
  9741. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9742. }
  9743. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9744. if ($matches[1] == 'true') {
  9745. $fmetric['Flags'] |= 0x40000;
  9746. }
  9747. }
  9748. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9749. $fmetric['StemV'] = intval($matches[1]);
  9750. } else {
  9751. $fmetric['StemV'] = 70;
  9752. }
  9753. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9754. $fmetric['StemH'] = intval($matches[1]);
  9755. } else {
  9756. $fmetric['StemH'] = 30;
  9757. }
  9758. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9759. $bv = explode(' ', $matches[1]);
  9760. if (count($bv) >= 6) {
  9761. $v1 = intval($bv[2]);
  9762. $v2 = intval($bv[4]);
  9763. if ($v1 <= $v2) {
  9764. $fmetric['XHeight'] = $v1;
  9765. $fmetric['CapHeight'] = $v2;
  9766. } else {
  9767. $fmetric['XHeight'] = $v2;
  9768. $fmetric['CapHeight'] = $v1;
  9769. }
  9770. } else {
  9771. $fmetric['XHeight'] = 450;
  9772. $fmetric['CapHeight'] = 700;
  9773. }
  9774. } else {
  9775. $fmetric['XHeight'] = 450;
  9776. $fmetric['CapHeight'] = 700;
  9777. }
  9778. // get the number of random bytes at the beginning of charstrings
  9779. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9780. $lenIV = intval($matches[1]);
  9781. } else {
  9782. $lenIV = 4;
  9783. }
  9784. $fmetric['Leading'] = 0;
  9785. // get charstring data
  9786. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9787. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9788. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9789. $enc_map = $this->encmaps->encmap[$enc];
  9790. } else {
  9791. $enc_map = false;
  9792. }
  9793. $fmetric['cw'] = '';
  9794. $fmetric['MaxWidth'] = 0;
  9795. $cwidths = array();
  9796. foreach ($matches as $k => $v) {
  9797. $cid = 0;
  9798. if (isset($imap[$v[1]])) {
  9799. $cid = $imap[$v[1]];
  9800. } elseif ($enc_map !== false) {
  9801. $cid = array_search($v[1], $enc_map);
  9802. if ($cid === false) {
  9803. $cid = 0;
  9804. } elseif ($cid > 1000) {
  9805. $cid -= 1000;
  9806. }
  9807. }
  9808. // decrypt charstring encrypted part
  9809. $r = 4330; // charstring encryption constant
  9810. $c1 = 52845;
  9811. $c2 = 22719;
  9812. $cd = $v[2];
  9813. $clen = strlen($cd);
  9814. $ccom = array();
  9815. for ($i = 0; $i < $clen; ++$i) {
  9816. $chr = ord($cd[$i]);
  9817. $ccom[] = ($chr ^ ($r >> 8));
  9818. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9819. }
  9820. // decode numbers
  9821. $cdec = array();
  9822. $ck = 0;
  9823. $i = $lenIV;
  9824. while ($i < $clen) {
  9825. if ($ccom[$i] < 32) {
  9826. $cdec[$ck] = $ccom[$i];
  9827. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  9828. // hsbw command: update width
  9829. $cwidths[$cid] = $cdec[($ck - 1)];
  9830. }
  9831. ++$i;
  9832. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  9833. $cdec[$ck] = ($ccom[$i] - 139);
  9834. ++$i;
  9835. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  9836. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  9837. $i += 2;
  9838. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  9839. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  9840. $i += 2;
  9841. } elseif ($ccom[$i] == 255) {
  9842. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  9843. $vsval = unpack('li', $sval);
  9844. $cdec[$ck] = $vsval['i'];
  9845. $i += 5;
  9846. }
  9847. ++$ck;
  9848. }
  9849. } // end for each matches
  9850. $fmetric['MissingWidth'] = $cwidths[0];
  9851. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  9852. $fmetric['AvgWidth'] = 0;
  9853. // set chars widths
  9854. for ($cid = 0; $cid <= 255; ++$cid) {
  9855. if (isset($cwidths[$cid])) {
  9856. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  9857. $fmetric['MaxWidth'] = $cwidths[$cid];
  9858. }
  9859. $fmetric['AvgWidth'] += $cwidths[$cid];
  9860. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  9861. } else {
  9862. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  9863. }
  9864. }
  9865. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  9866. } else {
  9867. // ---------- TRUE TYPE ----------
  9868. if ($fmetric['type'] != 'cidfont0') {
  9869. // store compressed font
  9870. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9871. fwrite($fp, gzcompress($font));
  9872. fclose($fp);
  9873. }
  9874. $offset = 0; // offset position of the font data
  9875. if ($this->_getULONG($font, $offset) != 0x10000) {
  9876. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9877. return $font;
  9878. }
  9879. $offset += 4;
  9880. // get number of tables
  9881. $numTables = $this->_getUSHORT($font, $offset);
  9882. $offset += 2;
  9883. // skip searchRange, entrySelector and rangeShift
  9884. $offset += 6;
  9885. // tables array
  9886. $table = array();
  9887. // ---------- get tables ----------
  9888. for ($i = 0; $i < $numTables; ++$i) {
  9889. // get table info
  9890. $tag = substr($font, $offset, 4);
  9891. $offset += 4;
  9892. $table[$tag] = array();
  9893. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9894. $offset += 4;
  9895. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9896. $offset += 4;
  9897. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9898. $offset += 4;
  9899. }
  9900. // check magicNumber
  9901. $offset = $table['head']['offset'] + 12;
  9902. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9903. // magicNumber must be 0x5F0F3CF5
  9904. return $font;
  9905. }
  9906. $offset += 4;
  9907. $offset += 2; // skip flags
  9908. // get FUnits
  9909. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  9910. $offset += 2;
  9911. // units ratio constant
  9912. $urk = (1000 / $fmetric['unitsPerEm']);
  9913. $offset += 16; // skip created, modified
  9914. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  9915. $offset += 2;
  9916. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  9917. $offset += 2;
  9918. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  9919. $offset += 2;
  9920. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  9921. $offset += 2;
  9922. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  9923. $macStyle = $this->_getUSHORT($font, $offset);
  9924. $offset += 2;
  9925. // PDF font flags
  9926. $fmetric['Flags'] = $flags;
  9927. if (($macStyle & 2) == 2) {
  9928. // italic flag
  9929. $fmetric['Flags'] |= 64;
  9930. }
  9931. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9932. $offset = $table['head']['offset'] + 50;
  9933. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9934. $offset += 2;
  9935. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9936. $indexToLoc = array();
  9937. $offset = $table['loca']['offset'];
  9938. if ($short_offset) {
  9939. // short version
  9940. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9941. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9942. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9943. $offset += 2;
  9944. }
  9945. } else {
  9946. // long version
  9947. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9948. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9949. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9950. $offset += 4;
  9951. }
  9952. }
  9953. // get glyphs indexes of chars from cmap table
  9954. $offset = $table['cmap']['offset'] + 2;
  9955. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9956. $offset += 2;
  9957. $encodingTables = array();
  9958. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9959. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9960. $offset += 2;
  9961. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9962. $offset += 2;
  9963. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9964. $offset += 4;
  9965. }
  9966. // ---------- get os/2 metrics ----------
  9967. $offset = $table['OS/2']['offset'];
  9968. $offset += 2; // skip version
  9969. // xAvgCharWidth
  9970. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  9971. $offset += 2;
  9972. // usWeightClass
  9973. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  9974. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  9975. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  9976. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  9977. $offset += 2;
  9978. $offset += 2; // usWidthClass
  9979. $fsType = $this->_getSHORT($font, $offset);
  9980. $offset += 2;
  9981. if ($fsType == 2) {
  9982. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  9983. }
  9984. // ---------- get font name ----------
  9985. $fmetric['name'] = '';
  9986. $offset = $table['name']['offset'];
  9987. $offset += 2; // skip Format selector (=0).
  9988. // Number of NameRecords that follow n.
  9989. $numNameRecords = $this->_getUSHORT($font, $offset);
  9990. $offset += 2;
  9991. // Offset to start of string storage (from start of table).
  9992. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  9993. $offset += 2;
  9994. for ($i = 0; $i < $numNameRecords; ++$i) {
  9995. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  9996. // Name ID.
  9997. $nameID = $this->_getUSHORT($font, $offset);
  9998. $offset += 2;
  9999. if ($nameID == 6) {
  10000. // String length (in bytes).
  10001. $stringLength = $this->_getUSHORT($font, $offset);
  10002. $offset += 2;
  10003. // String offset from start of storage area (in bytes).
  10004. $stringOffset = $this->_getUSHORT($font, $offset);
  10005. $offset += 2;
  10006. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  10007. $fmetric['name'] = substr($font, $offset, $stringLength);
  10008. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  10009. break;
  10010. } else {
  10011. $offset += 4; // skip String length, String offset
  10012. }
  10013. }
  10014. if (empty($fmetric['name'])) {
  10015. $fmetric['name'] = $font_name;
  10016. }
  10017. // ---------- get post data ----------
  10018. $offset = $table['post']['offset'];
  10019. $offset += 4; // skip Format Type
  10020. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  10021. $offset += 4;
  10022. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  10023. $offset += 2;
  10024. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  10025. $offset += 2;
  10026. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  10027. $offset += 2;
  10028. if ($isFixedPitch) {
  10029. $fmetric['Flags'] |= 1;
  10030. }
  10031. // ---------- get hhea data ----------
  10032. $offset = $table['hhea']['offset'];
  10033. $offset += 4; // skip Table version number
  10034. // Ascender
  10035. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  10036. $offset += 2;
  10037. // Descender
  10038. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  10039. $offset += 2;
  10040. // LineGap
  10041. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  10042. $offset += 2;
  10043. // advanceWidthMax
  10044. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  10045. $offset += 2;
  10046. $offset += 22; // skip some values
  10047. // get the number of hMetric entries in hmtx table
  10048. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  10049. // ---------- get maxp data ----------
  10050. $offset = $table['maxp']['offset'];
  10051. $offset += 4; // skip Table version number
  10052. // get the the number of glyphs in the font.
  10053. $numGlyphs = $this->_getUSHORT($font, $offset);
  10054. // ---------- get CIDToGIDMap ----------
  10055. $ctg = array();
  10056. foreach ($encodingTables as $enctable) {
  10057. // get only specified Platform ID and Encoding ID
  10058. if (($enctable['platformID'] == $platid) AND ($enctable['encodingID'] == $encid)) {
  10059. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10060. $format = $this->_getUSHORT($font, $offset);
  10061. $offset += 2;
  10062. switch ($format) {
  10063. case 0: { // Format 0: Byte encoding table
  10064. $offset += 4; // skip length and version/language
  10065. for ($c = 0; $c < 256; ++$c) {
  10066. $g = $this->_getBYTE($font, $offset);
  10067. $ctg[$c] = $g;
  10068. ++$offset;
  10069. }
  10070. break;
  10071. }
  10072. case 2: { // Format 2: High-byte mapping through table
  10073. $offset += 4; // skip length and version/language
  10074. $numSubHeaders = 0;
  10075. for ($i = 0; $i < 256; ++$i) {
  10076. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10077. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10078. $offset += 2;
  10079. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10080. $numSubHeaders = $subHeaderKeys[$i];
  10081. }
  10082. }
  10083. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10084. ++$numSubHeaders;
  10085. // read subHeader structures
  10086. $subHeaders = array();
  10087. $numGlyphIndexArray = 0;
  10088. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10089. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10090. $offset += 2;
  10091. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10092. $offset += 2;
  10093. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  10094. $offset += 2;
  10095. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10096. $offset += 2;
  10097. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10098. $subHeaders[$k]['idRangeOffset'] /= 2;
  10099. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10100. }
  10101. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10102. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10103. $offset += 2;
  10104. }
  10105. for ($i = 0; $i < 256; ++$i) {
  10106. $k = $subHeaderKeys[$i];
  10107. if ($k == 0) {
  10108. // one byte code
  10109. $c = $i;
  10110. $g = $glyphIndexArray[0];
  10111. $ctg[$c] = $g;
  10112. } else {
  10113. // two bytes code
  10114. $start_byte = $subHeaders[$k]['firstCode'];
  10115. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10116. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10117. // combine high and low bytes
  10118. $c = (($i << 8) + $j);
  10119. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10120. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  10121. if ($g < 0) {
  10122. $g = 0;
  10123. }
  10124. $ctg[$c] = $g;
  10125. }
  10126. }
  10127. }
  10128. break;
  10129. }
  10130. case 4: { // Format 4: Segment mapping to delta values
  10131. $length = $this->_getUSHORT($font, $offset);
  10132. $offset += 2;
  10133. $offset += 2; // skip version/language
  10134. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10135. $offset += 2;
  10136. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10137. $endCount = array(); // array of end character codes for each segment
  10138. for ($k = 0; $k < $segCount; ++$k) {
  10139. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10140. $offset += 2;
  10141. }
  10142. $offset += 2; // skip reservedPad
  10143. $startCount = array(); // array of start character codes for each segment
  10144. for ($k = 0; $k < $segCount; ++$k) {
  10145. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10146. $offset += 2;
  10147. }
  10148. $idDelta = array(); // delta for all character codes in segment
  10149. for ($k = 0; $k < $segCount; ++$k) {
  10150. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10151. $offset += 2;
  10152. }
  10153. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10154. for ($k = 0; $k < $segCount; ++$k) {
  10155. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10156. $offset += 2;
  10157. }
  10158. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10159. $glyphIdArray = array(); // glyph index array
  10160. for ($k = 0; $k < $gidlen; ++$k) {
  10161. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10162. $offset += 2;
  10163. }
  10164. for ($k = 0; $k < $segCount; ++$k) {
  10165. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10166. if ($idRangeOffset[$k] == 0) {
  10167. $g = ($idDelta[$k] + $c) % 65536;
  10168. } else {
  10169. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10170. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  10171. }
  10172. if ($g < 0) {
  10173. $g = 0;
  10174. }
  10175. $ctg[$c] = $g;
  10176. }
  10177. }
  10178. break;
  10179. }
  10180. case 6: { // Format 6: Trimmed table mapping
  10181. $offset += 4; // skip length and version/language
  10182. $firstCode = $this->_getUSHORT($font, $offset);
  10183. $offset += 2;
  10184. $entryCount = $this->_getUSHORT($font, $offset);
  10185. $offset += 2;
  10186. for ($k = 0; $k < $entryCount; ++$k) {
  10187. $c = ($k + $firstCode);
  10188. $g = $this->_getUSHORT($font, $offset);
  10189. $offset += 2;
  10190. $ctg[$c] = $g;
  10191. }
  10192. break;
  10193. }
  10194. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10195. $offset += 10; // skip reserved, length and version/language
  10196. for ($k = 0; $k < 8192; ++$k) {
  10197. $is32[$k] = $this->_getBYTE($font, $offset);
  10198. ++$offset;
  10199. }
  10200. $nGroups = $this->_getULONG($font, $offset);
  10201. $offset += 4;
  10202. for ($i = 0; $i < $nGroups; ++$i) {
  10203. $startCharCode = $this->_getULONG($font, $offset);
  10204. $offset += 4;
  10205. $endCharCode = $this->_getULONG($font, $offset);
  10206. $offset += 4;
  10207. $startGlyphID = $this->_getULONG($font, $offset);
  10208. $offset += 4;
  10209. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10210. $is32idx = floor($c / 8);
  10211. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10212. $c = $k;
  10213. } else {
  10214. // 32 bit format
  10215. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10216. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10217. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10218. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10219. }
  10220. $ctg[$c] = 0;
  10221. ++$startGlyphID;
  10222. }
  10223. }
  10224. break;
  10225. }
  10226. case 10: { // Format 10: Trimmed array
  10227. $offset += 10; // skip reserved, length and version/language
  10228. $startCharCode = $this->_getULONG($font, $offset);
  10229. $offset += 4;
  10230. $numChars = $this->_getULONG($font, $offset);
  10231. $offset += 4;
  10232. for ($k = 0; $k < $numChars; ++$k) {
  10233. $c = ($k + $startCharCode);
  10234. $g = $this->_getUSHORT($font, $offset);
  10235. $ctg[$c] = $g;
  10236. $offset += 2;
  10237. }
  10238. break;
  10239. }
  10240. case 12: { // Format 12: Segmented coverage
  10241. $offset += 10; // skip length and version/language
  10242. $nGroups = $this->_getULONG($font, $offset);
  10243. $offset += 4;
  10244. for ($k = 0; $k < $nGroups; ++$k) {
  10245. $startCharCode = $this->_getULONG($font, $offset);
  10246. $offset += 4;
  10247. $endCharCode = $this->_getULONG($font, $offset);
  10248. $offset += 4;
  10249. $startGlyphCode = $this->_getULONG($font, $offset);
  10250. $offset += 4;
  10251. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10252. $ctg[$c] = $startGlyphCode;
  10253. ++$startGlyphCode;
  10254. }
  10255. }
  10256. break;
  10257. }
  10258. case 13: { // Format 13: Many-to-one range mappings
  10259. // to be implemented ...
  10260. break;
  10261. }
  10262. case 14: { // Format 14: Unicode Variation Sequences
  10263. // to be implemented ...
  10264. break;
  10265. }
  10266. }
  10267. }
  10268. }
  10269. if (!isset($ctg[0])) {
  10270. $ctg[0] = 0;
  10271. }
  10272. // get xHeight (height of x)
  10273. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10274. $yMin = $this->_getFWORD($font, $offset);
  10275. $offset += 4;
  10276. $yMax = $this->_getFWORD($font, $offset);
  10277. $offset += 2;
  10278. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10279. // get CapHeight (height of H)
  10280. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10281. $yMin = $this->_getFWORD($font, $offset);
  10282. $offset += 4;
  10283. $yMax = $this->_getFWORD($font, $offset);
  10284. $offset += 2;
  10285. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10286. // ceate widths array
  10287. $cw = array();
  10288. $offset = $table['hmtx']['offset'];
  10289. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10290. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10291. $offset += 4; // skip lsb
  10292. }
  10293. if ($numberOfHMetrics < $numGlyphs) {
  10294. // fill missing widths with the last value
  10295. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10296. }
  10297. $fmetric['MissingWidth'] = $cw[0];
  10298. $fmetric['cw'] = '';
  10299. for ($cid = 0; $cid <= 65535; ++$cid) {
  10300. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10301. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10302. }
  10303. }
  10304. } // end of true type
  10305. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10306. $fmetric['type'] == 'TrueType';
  10307. }
  10308. // ---------- create php font file ----------
  10309. $pfile = '<'.'?'.'php'."\n";
  10310. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10311. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10312. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10313. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10314. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10315. if ($fmetric['MissingWidth'] > 0) {
  10316. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10317. } else {
  10318. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10319. }
  10320. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10321. if ($fmetric['type'] == 'Type1') {
  10322. // Type 1
  10323. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10324. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10325. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10326. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10327. } else {
  10328. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10329. if ($fmetric['type'] == 'cidfont0') {
  10330. // CID-0
  10331. switch ($fonttype) {
  10332. case 'CID0JP': {
  10333. $pfile .= '// Japanese'."\n";
  10334. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10335. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10336. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10337. break;
  10338. }
  10339. case 'CID0KR': {
  10340. $pfile .= '// Korean'."\n";
  10341. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10342. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10343. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10344. break;
  10345. }
  10346. case 'CID0CS': {
  10347. $pfile .= '// Chinese Simplified'."\n";
  10348. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10349. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10350. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10351. break;
  10352. }
  10353. case 'CID0CT':
  10354. default: {
  10355. $pfile .= '// Chinese Traditional'."\n";
  10356. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10357. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10358. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10359. break;
  10360. }
  10361. }
  10362. } else {
  10363. // TrueType
  10364. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10365. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10366. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10367. // create CIDToGIDMap
  10368. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10369. foreach ($ctg as $cid => $gid) {
  10370. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10371. }
  10372. // store compressed CIDToGIDMap
  10373. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10374. fwrite($fp, gzcompress($cidtogidmap));
  10375. fclose($fp);
  10376. }
  10377. }
  10378. $pfile .= '$desc=array(';
  10379. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10380. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10381. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10382. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10383. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10384. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10385. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10386. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10387. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10388. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10389. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10390. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10391. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10392. $pfile .= ');'."\n";
  10393. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10394. $pfile .= '// --- EOF ---'."\n";
  10395. // store file
  10396. $fp = fopen($outpath.$font_name.'.php', 'w');
  10397. fwrite($fp, $pfile);
  10398. fclose($fp);
  10399. // return TCPDF font name
  10400. return $font_name;
  10401. }
  10402. /**
  10403. * Returns a subset of the TrueType font data without the unused glyphs.
  10404. * @param $font (string) TrueType font data.
  10405. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10406. * @return (string) A subset of TrueType font data without the unused glyphs.
  10407. * @author Nicola Asuni
  10408. * @protected
  10409. * @since 5.2.000 (2010-06-02)
  10410. */
  10411. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10412. ksort($subsetchars);
  10413. $offset = 0; // offset position of the font data
  10414. if ($this->_getULONG($font, $offset) != 0x10000) {
  10415. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10416. return $font;
  10417. }
  10418. $offset += 4;
  10419. // get number of tables
  10420. $numTables = $this->_getUSHORT($font, $offset);
  10421. $offset += 2;
  10422. // skip searchRange, entrySelector and rangeShift
  10423. $offset += 6;
  10424. // tables array
  10425. $table = array();
  10426. // for each table
  10427. for ($i = 0; $i < $numTables; ++$i) {
  10428. // get table info
  10429. $tag = substr($font, $offset, 4);
  10430. $offset += 4;
  10431. $table[$tag] = array();
  10432. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10433. $offset += 4;
  10434. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10435. $offset += 4;
  10436. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10437. $offset += 4;
  10438. }
  10439. // check magicNumber
  10440. $offset = $table['head']['offset'] + 12;
  10441. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10442. // magicNumber must be 0x5F0F3CF5
  10443. return $font;
  10444. }
  10445. $offset += 4;
  10446. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10447. $offset = $table['head']['offset'] + 50;
  10448. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10449. $offset += 2;
  10450. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10451. $indexToLoc = array();
  10452. $offset = $table['loca']['offset'];
  10453. if ($short_offset) {
  10454. // short version
  10455. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10456. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10457. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10458. $offset += 2;
  10459. }
  10460. } else {
  10461. // long version
  10462. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10463. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10464. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10465. $offset += 4;
  10466. }
  10467. }
  10468. // get glyphs indexes of chars from cmap table
  10469. $subsetglyphs = array(); // glyph IDs on key
  10470. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10471. $offset = $table['cmap']['offset'] + 2;
  10472. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10473. $offset += 2;
  10474. $encodingTables = array();
  10475. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10476. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10477. $offset += 2;
  10478. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10479. $offset += 2;
  10480. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10481. $offset += 4;
  10482. }
  10483. foreach ($encodingTables as $enctable) {
  10484. // get all platforms and encodings
  10485. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10486. $format = $this->_getUSHORT($font, $offset);
  10487. $offset += 2;
  10488. switch ($format) {
  10489. case 0: { // Format 0: Byte encoding table
  10490. $offset += 4; // skip length and version/language
  10491. for ($c = 0; $c < 256; ++$c) {
  10492. if (isset($subsetchars[$c])) {
  10493. $g = $this->_getBYTE($font, $offset);
  10494. $subsetglyphs[$g] = true;
  10495. }
  10496. ++$offset;
  10497. }
  10498. break;
  10499. }
  10500. case 2: { // Format 2: High-byte mapping through table
  10501. $offset += 4; // skip length and version/language
  10502. $numSubHeaders = 0;
  10503. for ($i = 0; $i < 256; ++$i) {
  10504. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10505. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10506. $offset += 2;
  10507. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10508. $numSubHeaders = $subHeaderKeys[$i];
  10509. }
  10510. }
  10511. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10512. ++$numSubHeaders;
  10513. // read subHeader structures
  10514. $subHeaders = array();
  10515. $numGlyphIndexArray = 0;
  10516. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10517. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10518. $offset += 2;
  10519. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10520. $offset += 2;
  10521. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  10522. $offset += 2;
  10523. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10524. $offset += 2;
  10525. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10526. $subHeaders[$k]['idRangeOffset'] /= 2;
  10527. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10528. }
  10529. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10530. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10531. $offset += 2;
  10532. }
  10533. for ($i = 0; $i < 256; ++$i) {
  10534. $k = $subHeaderKeys[$i];
  10535. if ($k == 0) {
  10536. // one byte code
  10537. $c = $i;
  10538. if (isset($subsetchars[$c])) {
  10539. $g = $glyphIndexArray[0];
  10540. $subsetglyphs[$g] = true;
  10541. }
  10542. } else {
  10543. // two bytes code
  10544. $start_byte = $subHeaders[$k]['firstCode'];
  10545. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10546. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10547. // combine high and low bytes
  10548. $c = (($i << 8) + $j);
  10549. if (isset($subsetchars[$c])) {
  10550. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10551. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  10552. if ($g < 0) {
  10553. $g = 0;
  10554. }
  10555. $subsetglyphs[$g] = true;
  10556. }
  10557. }
  10558. }
  10559. }
  10560. break;
  10561. }
  10562. case 4: { // Format 4: Segment mapping to delta values
  10563. $length = $this->_getUSHORT($font, $offset);
  10564. $offset += 2;
  10565. $offset += 2; // skip version/language
  10566. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10567. $offset += 2;
  10568. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10569. $endCount = array(); // array of end character codes for each segment
  10570. for ($k = 0; $k < $segCount; ++$k) {
  10571. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10572. $offset += 2;
  10573. }
  10574. $offset += 2; // skip reservedPad
  10575. $startCount = array(); // array of start character codes for each segment
  10576. for ($k = 0; $k < $segCount; ++$k) {
  10577. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10578. $offset += 2;
  10579. }
  10580. $idDelta = array(); // delta for all character codes in segment
  10581. for ($k = 0; $k < $segCount; ++$k) {
  10582. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10583. $offset += 2;
  10584. }
  10585. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10586. for ($k = 0; $k < $segCount; ++$k) {
  10587. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10588. $offset += 2;
  10589. }
  10590. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10591. $glyphIdArray = array(); // glyph index array
  10592. for ($k = 0; $k < $gidlen; ++$k) {
  10593. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10594. $offset += 2;
  10595. }
  10596. for ($k = 0; $k < $segCount; ++$k) {
  10597. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10598. if (isset($subsetchars[$c])) {
  10599. if ($idRangeOffset[$k] == 0) {
  10600. $g = ($idDelta[$k] + $c) % 65536;
  10601. } else {
  10602. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10603. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  10604. }
  10605. if ($g < 0) {
  10606. $g = 0;
  10607. }
  10608. $subsetglyphs[$g] = true;
  10609. }
  10610. }
  10611. }
  10612. break;
  10613. }
  10614. case 6: { // Format 6: Trimmed table mapping
  10615. $offset += 4; // skip length and version/language
  10616. $firstCode = $this->_getUSHORT($font, $offset);
  10617. $offset += 2;
  10618. $entryCount = $this->_getUSHORT($font, $offset);
  10619. $offset += 2;
  10620. for ($k = 0; $k < $entryCount; ++$k) {
  10621. $c = ($k + $firstCode);
  10622. if (isset($subsetchars[$c])) {
  10623. $g = $this->_getUSHORT($font, $offset);
  10624. $subsetglyphs[$g] = true;
  10625. }
  10626. $offset += 2;
  10627. }
  10628. break;
  10629. }
  10630. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10631. $offset += 10; // skip reserved, length and version/language
  10632. for ($k = 0; $k < 8192; ++$k) {
  10633. $is32[$k] = $this->_getBYTE($font, $offset);
  10634. ++$offset;
  10635. }
  10636. $nGroups = $this->_getULONG($font, $offset);
  10637. $offset += 4;
  10638. for ($i = 0; $i < $nGroups; ++$i) {
  10639. $startCharCode = $this->_getULONG($font, $offset);
  10640. $offset += 4;
  10641. $endCharCode = $this->_getULONG($font, $offset);
  10642. $offset += 4;
  10643. $startGlyphID = $this->_getULONG($font, $offset);
  10644. $offset += 4;
  10645. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10646. $is32idx = floor($c / 8);
  10647. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10648. $c = $k;
  10649. } else {
  10650. // 32 bit format
  10651. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10652. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10653. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10654. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10655. }
  10656. if (isset($subsetchars[$c])) {
  10657. $subsetglyphs[$startGlyphID] = true;
  10658. }
  10659. ++$startGlyphID;
  10660. }
  10661. }
  10662. break;
  10663. }
  10664. case 10: { // Format 10: Trimmed array
  10665. $offset += 10; // skip reserved, length and version/language
  10666. $startCharCode = $this->_getULONG($font, $offset);
  10667. $offset += 4;
  10668. $numChars = $this->_getULONG($font, $offset);
  10669. $offset += 4;
  10670. for ($k = 0; $k < $numChars; ++$k) {
  10671. $c = ($k + $startCharCode);
  10672. if (isset($subsetchars[$c])) {
  10673. $g = $this->_getUSHORT($font, $offset);
  10674. $subsetglyphs[$g] = true;
  10675. }
  10676. $offset += 2;
  10677. }
  10678. break;
  10679. }
  10680. case 12: { // Format 12: Segmented coverage
  10681. $offset += 10; // skip length and version/language
  10682. $nGroups = $this->_getULONG($font, $offset);
  10683. $offset += 4;
  10684. for ($k = 0; $k < $nGroups; ++$k) {
  10685. $startCharCode = $this->_getULONG($font, $offset);
  10686. $offset += 4;
  10687. $endCharCode = $this->_getULONG($font, $offset);
  10688. $offset += 4;
  10689. $startGlyphCode = $this->_getULONG($font, $offset);
  10690. $offset += 4;
  10691. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10692. if (isset($subsetchars[$c])) {
  10693. $subsetglyphs[$startGlyphCode] = true;
  10694. }
  10695. ++$startGlyphCode;
  10696. }
  10697. }
  10698. break;
  10699. }
  10700. case 13: { // Format 13: Many-to-one range mappings
  10701. // to be implemented ...
  10702. break;
  10703. }
  10704. case 14: { // Format 14: Unicode Variation Sequences
  10705. // to be implemented ...
  10706. break;
  10707. }
  10708. }
  10709. }
  10710. // include all parts of composite glyphs
  10711. $new_sga = $subsetglyphs;
  10712. while (!empty($new_sga)) {
  10713. $sga = $new_sga;
  10714. $new_sga = array();
  10715. foreach ($sga as $key => $val) {
  10716. if (isset($indexToLoc[$key])) {
  10717. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10718. $numberOfContours = $this->_getSHORT($font, $offset);
  10719. $offset += 2;
  10720. if ($numberOfContours < 0) { // composite glyph
  10721. $offset += 8; // skip xMin, yMin, xMax, yMax
  10722. do {
  10723. $flags = $this->_getUSHORT($font, $offset);
  10724. $offset += 2;
  10725. $glyphIndex = $this->_getUSHORT($font, $offset);
  10726. $offset += 2;
  10727. if (!isset($subsetglyphs[$glyphIndex])) {
  10728. // add missing glyphs
  10729. $new_sga[$glyphIndex] = true;
  10730. }
  10731. // skip some bytes by case
  10732. if ($flags & 1) {
  10733. $offset += 4;
  10734. } else {
  10735. $offset += 2;
  10736. }
  10737. if ($flags & 8) {
  10738. $offset += 2;
  10739. } elseif ($flags & 64) {
  10740. $offset += 4;
  10741. } elseif ($flags & 128) {
  10742. $offset += 8;
  10743. }
  10744. } while ($flags & 32);
  10745. }
  10746. }
  10747. }
  10748. $subsetglyphs += $new_sga;
  10749. }
  10750. // sort glyphs by key (and remove duplicates)
  10751. ksort($subsetglyphs);
  10752. // build new glyf and loca tables
  10753. $glyf = '';
  10754. $loca = '';
  10755. $offset = 0;
  10756. $glyf_offset = $table['glyf']['offset'];
  10757. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10758. if (isset($subsetglyphs[$i])) {
  10759. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10760. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10761. } else {
  10762. $length = 0;
  10763. }
  10764. if ($short_offset) {
  10765. $loca .= pack('n', ($offset / 2));
  10766. } else {
  10767. $loca .= pack('N', $offset);
  10768. }
  10769. $offset += $length;
  10770. }
  10771. // array of table names to preserve (loca and glyf tables will be added later)
  10772. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10773. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10774. // get the tables to preserve
  10775. $offset = 12;
  10776. foreach ($table as $tag => $val) {
  10777. if (in_array($tag, $table_names)) {
  10778. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10779. if ($tag == 'head') {
  10780. // set the checkSumAdjustment to 0
  10781. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10782. }
  10783. $pad = 4 - ($table[$tag]['length'] % 4);
  10784. if ($pad != 4) {
  10785. // the length of a table must be a multiple of four bytes
  10786. $table[$tag]['length'] += $pad;
  10787. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10788. }
  10789. $table[$tag]['offset'] = $offset;
  10790. $offset += $table[$tag]['length'];
  10791. // check sum is not changed (so keep the following line commented)
  10792. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10793. } else {
  10794. unset($table[$tag]);
  10795. }
  10796. }
  10797. // add loca
  10798. $table['loca']['data'] = $loca;
  10799. $table['loca']['length'] = strlen($loca);
  10800. $pad = 4 - ($table['loca']['length'] % 4);
  10801. if ($pad != 4) {
  10802. // the length of a table must be a multiple of four bytes
  10803. $table['loca']['length'] += $pad;
  10804. $table['loca']['data'] .= str_repeat("\x0", $pad);
  10805. }
  10806. $table['loca']['offset'] = $offset;
  10807. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  10808. $offset += $table['loca']['length'];
  10809. // add glyf
  10810. $table['glyf']['data'] = $glyf;
  10811. $table['glyf']['length'] = strlen($glyf);
  10812. $pad = 4 - ($table['glyf']['length'] % 4);
  10813. if ($pad != 4) {
  10814. // the length of a table must be a multiple of four bytes
  10815. $table['glyf']['length'] += $pad;
  10816. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  10817. }
  10818. $table['glyf']['offset'] = $offset;
  10819. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  10820. // rebuild font
  10821. $font = '';
  10822. $font .= pack('N', 0x10000); // sfnt version
  10823. $numTables = count($table);
  10824. $font .= pack('n', $numTables); // numTables
  10825. $entrySelector = floor(log($numTables, 2));
  10826. $searchRange = pow(2, $entrySelector) * 16;
  10827. $rangeShift = ($numTables * 16) - $searchRange;
  10828. $font .= pack('n', $searchRange); // searchRange
  10829. $font .= pack('n', $entrySelector); // entrySelector
  10830. $font .= pack('n', $rangeShift); // rangeShift
  10831. $offset = ($numTables * 16);
  10832. foreach ($table as $tag => $data) {
  10833. $font .= $tag; // tag
  10834. $font .= pack('N', $data['checkSum']); // checkSum
  10835. $font .= pack('N', ($data['offset'] + $offset)); // offset
  10836. $font .= pack('N', $data['length']); // length
  10837. }
  10838. foreach ($table as $data) {
  10839. $font .= $data['data'];
  10840. }
  10841. // set checkSumAdjustment on head table
  10842. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  10843. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  10844. return $font;
  10845. }
  10846. /**
  10847. * Returs the checksum of a TTF table.
  10848. * @param $table (string) table to check
  10849. * @param $length (int) length of table in bytes
  10850. * @return int checksum
  10851. * @author Nicola Asuni
  10852. * @protected
  10853. * @since 5.2.000 (2010-06-02)
  10854. */
  10855. protected function _getTTFtableChecksum($table, $length) {
  10856. $sum = 0;
  10857. $tlen = ($length / 4);
  10858. $offset = 0;
  10859. for ($i = 0; $i < $tlen; ++$i) {
  10860. $v = unpack('Ni', substr($table, $offset, 4));
  10861. $sum += $v['i'];
  10862. $offset += 4;
  10863. }
  10864. $sum = unpack('Ni', pack('N', $sum));
  10865. return $sum['i'];
  10866. }
  10867. /**
  10868. * Outputs font widths
  10869. * @param $font (array) font data
  10870. * @param $cidoffset (int) offset for CID values
  10871. * @return PDF command string for font widths
  10872. * @author Nicola Asuni
  10873. * @protected
  10874. * @since 4.4.000 (2008-12-07)
  10875. */
  10876. protected function _putfontwidths($font, $cidoffset=0) {
  10877. ksort($font['cw']);
  10878. $rangeid = 0;
  10879. $range = array();
  10880. $prevcid = -2;
  10881. $prevwidth = -1;
  10882. $interval = false;
  10883. // for each character
  10884. foreach ($font['cw'] as $cid => $width) {
  10885. $cid -= $cidoffset;
  10886. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  10887. // ignore the unused characters (font subsetting)
  10888. continue;
  10889. }
  10890. if ($width != $font['dw']) {
  10891. if ($cid == ($prevcid + 1)) {
  10892. // consecutive CID
  10893. if ($width == $prevwidth) {
  10894. if ($width == $range[$rangeid][0]) {
  10895. $range[$rangeid][] = $width;
  10896. } else {
  10897. array_pop($range[$rangeid]);
  10898. // new range
  10899. $rangeid = $prevcid;
  10900. $range[$rangeid] = array();
  10901. $range[$rangeid][] = $prevwidth;
  10902. $range[$rangeid][] = $width;
  10903. }
  10904. $interval = true;
  10905. $range[$rangeid]['interval'] = true;
  10906. } else {
  10907. if ($interval) {
  10908. // new range
  10909. $rangeid = $cid;
  10910. $range[$rangeid] = array();
  10911. $range[$rangeid][] = $width;
  10912. } else {
  10913. $range[$rangeid][] = $width;
  10914. }
  10915. $interval = false;
  10916. }
  10917. } else {
  10918. // new range
  10919. $rangeid = $cid;
  10920. $range[$rangeid] = array();
  10921. $range[$rangeid][] = $width;
  10922. $interval = false;
  10923. }
  10924. $prevcid = $cid;
  10925. $prevwidth = $width;
  10926. }
  10927. }
  10928. // optimize ranges
  10929. $prevk = -1;
  10930. $nextk = -1;
  10931. $prevint = false;
  10932. foreach ($range as $k => $ws) {
  10933. $cws = count($ws);
  10934. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  10935. if (isset($range[$k]['interval'])) {
  10936. unset($range[$k]['interval']);
  10937. }
  10938. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  10939. unset($range[$k]);
  10940. } else {
  10941. $prevk = $k;
  10942. }
  10943. $nextk = $k + $cws;
  10944. if (isset($ws['interval'])) {
  10945. if ($cws > 3) {
  10946. $prevint = true;
  10947. } else {
  10948. $prevint = false;
  10949. }
  10950. unset($range[$k]['interval']);
  10951. --$nextk;
  10952. } else {
  10953. $prevint = false;
  10954. }
  10955. }
  10956. // output data
  10957. $w = '';
  10958. foreach ($range as $k => $ws) {
  10959. if (count(array_count_values($ws)) == 1) {
  10960. // interval mode is more compact
  10961. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  10962. } else {
  10963. // range mode
  10964. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  10965. }
  10966. }
  10967. return '/W ['.$w.' ]';
  10968. }
  10969. /**
  10970. * Output fonts.
  10971. * @author Nicola Asuni
  10972. * @protected
  10973. */
  10974. protected function _putfonts() {
  10975. $nf = $this->n;
  10976. foreach ($this->diffs as $diff) {
  10977. //Encodings
  10978. $this->_newobj();
  10979. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  10980. }
  10981. $mqr = $this->get_mqr();
  10982. $this->set_mqr(false);
  10983. foreach ($this->FontFiles as $file => $info) {
  10984. // search and get font file to embedd
  10985. $fontdir = $info['fontdir'];
  10986. $file = strtolower($file);
  10987. $fontfile = '';
  10988. // search files on various directories
  10989. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  10990. $fontfile = $fontdir.$file;
  10991. } elseif (file_exists($this->_getfontpath().$file)) {
  10992. $fontfile = $this->_getfontpath().$file;
  10993. } elseif (file_exists($file)) {
  10994. $fontfile = $file;
  10995. }
  10996. if (!$this->empty_string($fontfile)) {
  10997. $font = file_get_contents($fontfile);
  10998. $compressed = (substr($file, -2) == '.z');
  10999. if ((!$compressed) AND (isset($info['length2']))) {
  11000. $header = (ord($font{0}) == 128);
  11001. if ($header) {
  11002. // strip first binary header
  11003. $font = substr($font, 6);
  11004. }
  11005. if ($header AND (ord($font[$info['length1']]) == 128)) {
  11006. // strip second binary header
  11007. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  11008. }
  11009. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  11010. if ($compressed) {
  11011. // uncompress font
  11012. $font = gzuncompress($font);
  11013. }
  11014. // merge subset characters
  11015. $subsetchars = array(); // used chars
  11016. foreach ($info['fontkeys'] as $fontkey) {
  11017. $fontinfo = $this->getFontBuffer($fontkey);
  11018. $subsetchars += $fontinfo['subsetchars'];
  11019. }
  11020. // rebuild a font subset
  11021. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  11022. // calculate new font length
  11023. $info['length1'] = strlen($font);
  11024. if ($compressed) {
  11025. // recompress font
  11026. $font = gzcompress($font);
  11027. }
  11028. }
  11029. $this->_newobj();
  11030. $this->FontFiles[$file]['n'] = $this->n;
  11031. $stream = $this->_getrawstream($font);
  11032. $out = '<< /Length '.strlen($stream);
  11033. if ($compressed) {
  11034. $out .= ' /Filter /FlateDecode';
  11035. }
  11036. $out .= ' /Length1 '.$info['length1'];
  11037. if (isset($info['length2'])) {
  11038. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  11039. }
  11040. $out .= ' >>';
  11041. $out .= ' stream'."\n".$stream."\n".'endstream';
  11042. $out .= "\n".'endobj';
  11043. $this->_out($out);
  11044. }
  11045. }
  11046. $this->set_mqr($mqr);
  11047. foreach ($this->fontkeys as $k) {
  11048. //Font objects
  11049. $font = $this->getFontBuffer($k);
  11050. $type = $font['type'];
  11051. $name = $font['name'];
  11052. if ($type == 'core') {
  11053. // standard core font
  11054. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  11055. $out .= '<</Type /Font';
  11056. $out .= ' /Subtype /Type1';
  11057. $out .= ' /BaseFont /'.$name;
  11058. $out .= ' /Name /F'.$font['i'];
  11059. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  11060. $out .= ' /Encoding /WinAnsiEncoding';
  11061. }
  11062. if ($k == 'helvetica') {
  11063. // add default font for annotations
  11064. $this->annotation_fonts[$k] = $font['i'];
  11065. }
  11066. $out .= ' >>';
  11067. $out .= "\n".'endobj';
  11068. $this->_out($out);
  11069. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  11070. // additional Type1 or TrueType font
  11071. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  11072. $out .= '<</Type /Font';
  11073. $out .= ' /Subtype /'.$type;
  11074. $out .= ' /BaseFont /'.$name;
  11075. $out .= ' /Name /F'.$font['i'];
  11076. $out .= ' /FirstChar 32 /LastChar 255';
  11077. $out .= ' /Widths '.($this->n + 1).' 0 R';
  11078. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  11079. if ($font['enc']) {
  11080. if (isset($font['diff'])) {
  11081. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  11082. } else {
  11083. $out .= ' /Encoding /WinAnsiEncoding';
  11084. }
  11085. }
  11086. $out .= ' >>';
  11087. $out .= "\n".'endobj';
  11088. $this->_out($out);
  11089. // Widths
  11090. $this->_newobj();
  11091. $s = '[';
  11092. for ($i = 32; $i < 256; ++$i) {
  11093. $s .= $font['cw'][$i].' ';
  11094. }
  11095. $s .= ']';
  11096. $s .= "\n".'endobj';
  11097. $this->_out($s);
  11098. //Descriptor
  11099. $this->_newobj();
  11100. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11101. foreach ($font['desc'] as $fdk => $fdv) {
  11102. if (is_float($fdv)) {
  11103. $fdv = sprintf('%F', $fdv);
  11104. }
  11105. $s .= ' /'.$fdk.' '.$fdv.'';
  11106. }
  11107. if (!$this->empty_string($font['file'])) {
  11108. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11109. }
  11110. $s .= '>>';
  11111. $s .= "\n".'endobj';
  11112. $this->_out($s);
  11113. } else {
  11114. // additional types
  11115. $mtd = '_put'.strtolower($type);
  11116. if (!method_exists($this, $mtd)) {
  11117. $this->Error('Unsupported font type: '.$type);
  11118. }
  11119. $this->$mtd($font);
  11120. }
  11121. }
  11122. }
  11123. /**
  11124. * Adds unicode fonts.<br>
  11125. * Based on PDF Reference 1.3 (section 5)
  11126. * @param $font (array) font data
  11127. * @protected
  11128. * @author Nicola Asuni
  11129. * @since 1.52.0.TC005 (2005-01-05)
  11130. */
  11131. protected function _puttruetypeunicode($font) {
  11132. $fontname = '';
  11133. if ($font['subset']) {
  11134. // change name for font subsetting
  11135. $subtag = sprintf('%06u', $font['i']);
  11136. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  11137. $fontname .= $subtag.'+';
  11138. }
  11139. $fontname .= $font['name'];
  11140. // Type0 Font
  11141. // A composite font composed of other fonts, organized hierarchically
  11142. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11143. $out .= '<< /Type /Font';
  11144. $out .= ' /Subtype /Type0';
  11145. $out .= ' /BaseFont /'.$fontname;
  11146. $out .= ' /Name /F'.$font['i'];
  11147. $out .= ' /Encoding /'.$font['enc'];
  11148. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  11149. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  11150. $out .= ' >>';
  11151. $out .= "\n".'endobj';
  11152. $this->_out($out);
  11153. // ToUnicode map for Identity-H
  11154. $stream = "/CIDInit /ProcSet findresource begin\n";
  11155. $stream .= "12 dict begin\n";
  11156. $stream .= "begincmap\n";
  11157. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  11158. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  11159. $stream .= "/CMapType 2 def\n";
  11160. $stream .= "/WMode 0 def\n";
  11161. $stream .= "1 begincodespacerange\n";
  11162. $stream .= "<0000> <FFFF>\n";
  11163. $stream .= "endcodespacerange\n";
  11164. $stream .= "100 beginbfrange\n";
  11165. $stream .= "<0000> <00ff> <0000>\n";
  11166. $stream .= "<0100> <01ff> <0100>\n";
  11167. $stream .= "<0200> <02ff> <0200>\n";
  11168. $stream .= "<0300> <03ff> <0300>\n";
  11169. $stream .= "<0400> <04ff> <0400>\n";
  11170. $stream .= "<0500> <05ff> <0500>\n";
  11171. $stream .= "<0600> <06ff> <0600>\n";
  11172. $stream .= "<0700> <07ff> <0700>\n";
  11173. $stream .= "<0800> <08ff> <0800>\n";
  11174. $stream .= "<0900> <09ff> <0900>\n";
  11175. $stream .= "<0a00> <0aff> <0a00>\n";
  11176. $stream .= "<0b00> <0bff> <0b00>\n";
  11177. $stream .= "<0c00> <0cff> <0c00>\n";
  11178. $stream .= "<0d00> <0dff> <0d00>\n";
  11179. $stream .= "<0e00> <0eff> <0e00>\n";
  11180. $stream .= "<0f00> <0fff> <0f00>\n";
  11181. $stream .= "<1000> <10ff> <1000>\n";
  11182. $stream .= "<1100> <11ff> <1100>\n";
  11183. $stream .= "<1200> <12ff> <1200>\n";
  11184. $stream .= "<1300> <13ff> <1300>\n";
  11185. $stream .= "<1400> <14ff> <1400>\n";
  11186. $stream .= "<1500> <15ff> <1500>\n";
  11187. $stream .= "<1600> <16ff> <1600>\n";
  11188. $stream .= "<1700> <17ff> <1700>\n";
  11189. $stream .= "<1800> <18ff> <1800>\n";
  11190. $stream .= "<1900> <19ff> <1900>\n";
  11191. $stream .= "<1a00> <1aff> <1a00>\n";
  11192. $stream .= "<1b00> <1bff> <1b00>\n";
  11193. $stream .= "<1c00> <1cff> <1c00>\n";
  11194. $stream .= "<1d00> <1dff> <1d00>\n";
  11195. $stream .= "<1e00> <1eff> <1e00>\n";
  11196. $stream .= "<1f00> <1fff> <1f00>\n";
  11197. $stream .= "<2000> <20ff> <2000>\n";
  11198. $stream .= "<2100> <21ff> <2100>\n";
  11199. $stream .= "<2200> <22ff> <2200>\n";
  11200. $stream .= "<2300> <23ff> <2300>\n";
  11201. $stream .= "<2400> <24ff> <2400>\n";
  11202. $stream .= "<2500> <25ff> <2500>\n";
  11203. $stream .= "<2600> <26ff> <2600>\n";
  11204. $stream .= "<2700> <27ff> <2700>\n";
  11205. $stream .= "<2800> <28ff> <2800>\n";
  11206. $stream .= "<2900> <29ff> <2900>\n";
  11207. $stream .= "<2a00> <2aff> <2a00>\n";
  11208. $stream .= "<2b00> <2bff> <2b00>\n";
  11209. $stream .= "<2c00> <2cff> <2c00>\n";
  11210. $stream .= "<2d00> <2dff> <2d00>\n";
  11211. $stream .= "<2e00> <2eff> <2e00>\n";
  11212. $stream .= "<2f00> <2fff> <2f00>\n";
  11213. $stream .= "<3000> <30ff> <3000>\n";
  11214. $stream .= "<3100> <31ff> <3100>\n";
  11215. $stream .= "<3200> <32ff> <3200>\n";
  11216. $stream .= "<3300> <33ff> <3300>\n";
  11217. $stream .= "<3400> <34ff> <3400>\n";
  11218. $stream .= "<3500> <35ff> <3500>\n";
  11219. $stream .= "<3600> <36ff> <3600>\n";
  11220. $stream .= "<3700> <37ff> <3700>\n";
  11221. $stream .= "<3800> <38ff> <3800>\n";
  11222. $stream .= "<3900> <39ff> <3900>\n";
  11223. $stream .= "<3a00> <3aff> <3a00>\n";
  11224. $stream .= "<3b00> <3bff> <3b00>\n";
  11225. $stream .= "<3c00> <3cff> <3c00>\n";
  11226. $stream .= "<3d00> <3dff> <3d00>\n";
  11227. $stream .= "<3e00> <3eff> <3e00>\n";
  11228. $stream .= "<3f00> <3fff> <3f00>\n";
  11229. $stream .= "<4000> <40ff> <4000>\n";
  11230. $stream .= "<4100> <41ff> <4100>\n";
  11231. $stream .= "<4200> <42ff> <4200>\n";
  11232. $stream .= "<4300> <43ff> <4300>\n";
  11233. $stream .= "<4400> <44ff> <4400>\n";
  11234. $stream .= "<4500> <45ff> <4500>\n";
  11235. $stream .= "<4600> <46ff> <4600>\n";
  11236. $stream .= "<4700> <47ff> <4700>\n";
  11237. $stream .= "<4800> <48ff> <4800>\n";
  11238. $stream .= "<4900> <49ff> <4900>\n";
  11239. $stream .= "<4a00> <4aff> <4a00>\n";
  11240. $stream .= "<4b00> <4bff> <4b00>\n";
  11241. $stream .= "<4c00> <4cff> <4c00>\n";
  11242. $stream .= "<4d00> <4dff> <4d00>\n";
  11243. $stream .= "<4e00> <4eff> <4e00>\n";
  11244. $stream .= "<4f00> <4fff> <4f00>\n";
  11245. $stream .= "<5000> <50ff> <5000>\n";
  11246. $stream .= "<5100> <51ff> <5100>\n";
  11247. $stream .= "<5200> <52ff> <5200>\n";
  11248. $stream .= "<5300> <53ff> <5300>\n";
  11249. $stream .= "<5400> <54ff> <5400>\n";
  11250. $stream .= "<5500> <55ff> <5500>\n";
  11251. $stream .= "<5600> <56ff> <5600>\n";
  11252. $stream .= "<5700> <57ff> <5700>\n";
  11253. $stream .= "<5800> <58ff> <5800>\n";
  11254. $stream .= "<5900> <59ff> <5900>\n";
  11255. $stream .= "<5a00> <5aff> <5a00>\n";
  11256. $stream .= "<5b00> <5bff> <5b00>\n";
  11257. $stream .= "<5c00> <5cff> <5c00>\n";
  11258. $stream .= "<5d00> <5dff> <5d00>\n";
  11259. $stream .= "<5e00> <5eff> <5e00>\n";
  11260. $stream .= "<5f00> <5fff> <5f00>\n";
  11261. $stream .= "<6000> <60ff> <6000>\n";
  11262. $stream .= "<6100> <61ff> <6100>\n";
  11263. $stream .= "<6200> <62ff> <6200>\n";
  11264. $stream .= "<6300> <63ff> <6300>\n";
  11265. $stream .= "endbfrange\n";
  11266. $stream .= "100 beginbfrange\n";
  11267. $stream .= "<6400> <64ff> <6400>\n";
  11268. $stream .= "<6500> <65ff> <6500>\n";
  11269. $stream .= "<6600> <66ff> <6600>\n";
  11270. $stream .= "<6700> <67ff> <6700>\n";
  11271. $stream .= "<6800> <68ff> <6800>\n";
  11272. $stream .= "<6900> <69ff> <6900>\n";
  11273. $stream .= "<6a00> <6aff> <6a00>\n";
  11274. $stream .= "<6b00> <6bff> <6b00>\n";
  11275. $stream .= "<6c00> <6cff> <6c00>\n";
  11276. $stream .= "<6d00> <6dff> <6d00>\n";
  11277. $stream .= "<6e00> <6eff> <6e00>\n";
  11278. $stream .= "<6f00> <6fff> <6f00>\n";
  11279. $stream .= "<7000> <70ff> <7000>\n";
  11280. $stream .= "<7100> <71ff> <7100>\n";
  11281. $stream .= "<7200> <72ff> <7200>\n";
  11282. $stream .= "<7300> <73ff> <7300>\n";
  11283. $stream .= "<7400> <74ff> <7400>\n";
  11284. $stream .= "<7500> <75ff> <7500>\n";
  11285. $stream .= "<7600> <76ff> <7600>\n";
  11286. $stream .= "<7700> <77ff> <7700>\n";
  11287. $stream .= "<7800> <78ff> <7800>\n";
  11288. $stream .= "<7900> <79ff> <7900>\n";
  11289. $stream .= "<7a00> <7aff> <7a00>\n";
  11290. $stream .= "<7b00> <7bff> <7b00>\n";
  11291. $stream .= "<7c00> <7cff> <7c00>\n";
  11292. $stream .= "<7d00> <7dff> <7d00>\n";
  11293. $stream .= "<7e00> <7eff> <7e00>\n";
  11294. $stream .= "<7f00> <7fff> <7f00>\n";
  11295. $stream .= "<8000> <80ff> <8000>\n";
  11296. $stream .= "<8100> <81ff> <8100>\n";
  11297. $stream .= "<8200> <82ff> <8200>\n";
  11298. $stream .= "<8300> <83ff> <8300>\n";
  11299. $stream .= "<8400> <84ff> <8400>\n";
  11300. $stream .= "<8500> <85ff> <8500>\n";
  11301. $stream .= "<8600> <86ff> <8600>\n";
  11302. $stream .= "<8700> <87ff> <8700>\n";
  11303. $stream .= "<8800> <88ff> <8800>\n";
  11304. $stream .= "<8900> <89ff> <8900>\n";
  11305. $stream .= "<8a00> <8aff> <8a00>\n";
  11306. $stream .= "<8b00> <8bff> <8b00>\n";
  11307. $stream .= "<8c00> <8cff> <8c00>\n";
  11308. $stream .= "<8d00> <8dff> <8d00>\n";
  11309. $stream .= "<8e00> <8eff> <8e00>\n";
  11310. $stream .= "<8f00> <8fff> <8f00>\n";
  11311. $stream .= "<9000> <90ff> <9000>\n";
  11312. $stream .= "<9100> <91ff> <9100>\n";
  11313. $stream .= "<9200> <92ff> <9200>\n";
  11314. $stream .= "<9300> <93ff> <9300>\n";
  11315. $stream .= "<9400> <94ff> <9400>\n";
  11316. $stream .= "<9500> <95ff> <9500>\n";
  11317. $stream .= "<9600> <96ff> <9600>\n";
  11318. $stream .= "<9700> <97ff> <9700>\n";
  11319. $stream .= "<9800> <98ff> <9800>\n";
  11320. $stream .= "<9900> <99ff> <9900>\n";
  11321. $stream .= "<9a00> <9aff> <9a00>\n";
  11322. $stream .= "<9b00> <9bff> <9b00>\n";
  11323. $stream .= "<9c00> <9cff> <9c00>\n";
  11324. $stream .= "<9d00> <9dff> <9d00>\n";
  11325. $stream .= "<9e00> <9eff> <9e00>\n";
  11326. $stream .= "<9f00> <9fff> <9f00>\n";
  11327. $stream .= "<a000> <a0ff> <a000>\n";
  11328. $stream .= "<a100> <a1ff> <a100>\n";
  11329. $stream .= "<a200> <a2ff> <a200>\n";
  11330. $stream .= "<a300> <a3ff> <a300>\n";
  11331. $stream .= "<a400> <a4ff> <a400>\n";
  11332. $stream .= "<a500> <a5ff> <a500>\n";
  11333. $stream .= "<a600> <a6ff> <a600>\n";
  11334. $stream .= "<a700> <a7ff> <a700>\n";
  11335. $stream .= "<a800> <a8ff> <a800>\n";
  11336. $stream .= "<a900> <a9ff> <a900>\n";
  11337. $stream .= "<aa00> <aaff> <aa00>\n";
  11338. $stream .= "<ab00> <abff> <ab00>\n";
  11339. $stream .= "<ac00> <acff> <ac00>\n";
  11340. $stream .= "<ad00> <adff> <ad00>\n";
  11341. $stream .= "<ae00> <aeff> <ae00>\n";
  11342. $stream .= "<af00> <afff> <af00>\n";
  11343. $stream .= "<b000> <b0ff> <b000>\n";
  11344. $stream .= "<b100> <b1ff> <b100>\n";
  11345. $stream .= "<b200> <b2ff> <b200>\n";
  11346. $stream .= "<b300> <b3ff> <b300>\n";
  11347. $stream .= "<b400> <b4ff> <b400>\n";
  11348. $stream .= "<b500> <b5ff> <b500>\n";
  11349. $stream .= "<b600> <b6ff> <b600>\n";
  11350. $stream .= "<b700> <b7ff> <b700>\n";
  11351. $stream .= "<b800> <b8ff> <b800>\n";
  11352. $stream .= "<b900> <b9ff> <b900>\n";
  11353. $stream .= "<ba00> <baff> <ba00>\n";
  11354. $stream .= "<bb00> <bbff> <bb00>\n";
  11355. $stream .= "<bc00> <bcff> <bc00>\n";
  11356. $stream .= "<bd00> <bdff> <bd00>\n";
  11357. $stream .= "<be00> <beff> <be00>\n";
  11358. $stream .= "<bf00> <bfff> <bf00>\n";
  11359. $stream .= "<c000> <c0ff> <c000>\n";
  11360. $stream .= "<c100> <c1ff> <c100>\n";
  11361. $stream .= "<c200> <c2ff> <c200>\n";
  11362. $stream .= "<c300> <c3ff> <c300>\n";
  11363. $stream .= "<c400> <c4ff> <c400>\n";
  11364. $stream .= "<c500> <c5ff> <c500>\n";
  11365. $stream .= "<c600> <c6ff> <c600>\n";
  11366. $stream .= "<c700> <c7ff> <c700>\n";
  11367. $stream .= "endbfrange\n";
  11368. $stream .= "56 beginbfrange\n";
  11369. $stream .= "<c800> <c8ff> <c800>\n";
  11370. $stream .= "<c900> <c9ff> <c900>\n";
  11371. $stream .= "<ca00> <caff> <ca00>\n";
  11372. $stream .= "<cb00> <cbff> <cb00>\n";
  11373. $stream .= "<cc00> <ccff> <cc00>\n";
  11374. $stream .= "<cd00> <cdff> <cd00>\n";
  11375. $stream .= "<ce00> <ceff> <ce00>\n";
  11376. $stream .= "<cf00> <cfff> <cf00>\n";
  11377. $stream .= "<d000> <d0ff> <d000>\n";
  11378. $stream .= "<d100> <d1ff> <d100>\n";
  11379. $stream .= "<d200> <d2ff> <d200>\n";
  11380. $stream .= "<d300> <d3ff> <d300>\n";
  11381. $stream .= "<d400> <d4ff> <d400>\n";
  11382. $stream .= "<d500> <d5ff> <d500>\n";
  11383. $stream .= "<d600> <d6ff> <d600>\n";
  11384. $stream .= "<d700> <d7ff> <d700>\n";
  11385. $stream .= "<d800> <d8ff> <d800>\n";
  11386. $stream .= "<d900> <d9ff> <d900>\n";
  11387. $stream .= "<da00> <daff> <da00>\n";
  11388. $stream .= "<db00> <dbff> <db00>\n";
  11389. $stream .= "<dc00> <dcff> <dc00>\n";
  11390. $stream .= "<dd00> <ddff> <dd00>\n";
  11391. $stream .= "<de00> <deff> <de00>\n";
  11392. $stream .= "<df00> <dfff> <df00>\n";
  11393. $stream .= "<e000> <e0ff> <e000>\n";
  11394. $stream .= "<e100> <e1ff> <e100>\n";
  11395. $stream .= "<e200> <e2ff> <e200>\n";
  11396. $stream .= "<e300> <e3ff> <e300>\n";
  11397. $stream .= "<e400> <e4ff> <e400>\n";
  11398. $stream .= "<e500> <e5ff> <e500>\n";
  11399. $stream .= "<e600> <e6ff> <e600>\n";
  11400. $stream .= "<e700> <e7ff> <e700>\n";
  11401. $stream .= "<e800> <e8ff> <e800>\n";
  11402. $stream .= "<e900> <e9ff> <e900>\n";
  11403. $stream .= "<ea00> <eaff> <ea00>\n";
  11404. $stream .= "<eb00> <ebff> <eb00>\n";
  11405. $stream .= "<ec00> <ecff> <ec00>\n";
  11406. $stream .= "<ed00> <edff> <ed00>\n";
  11407. $stream .= "<ee00> <eeff> <ee00>\n";
  11408. $stream .= "<ef00> <efff> <ef00>\n";
  11409. $stream .= "<f000> <f0ff> <f000>\n";
  11410. $stream .= "<f100> <f1ff> <f100>\n";
  11411. $stream .= "<f200> <f2ff> <f200>\n";
  11412. $stream .= "<f300> <f3ff> <f300>\n";
  11413. $stream .= "<f400> <f4ff> <f400>\n";
  11414. $stream .= "<f500> <f5ff> <f500>\n";
  11415. $stream .= "<f600> <f6ff> <f600>\n";
  11416. $stream .= "<f700> <f7ff> <f700>\n";
  11417. $stream .= "<f800> <f8ff> <f800>\n";
  11418. $stream .= "<f900> <f9ff> <f900>\n";
  11419. $stream .= "<fa00> <faff> <fa00>\n";
  11420. $stream .= "<fb00> <fbff> <fb00>\n";
  11421. $stream .= "<fc00> <fcff> <fc00>\n";
  11422. $stream .= "<fd00> <fdff> <fd00>\n";
  11423. $stream .= "<fe00> <feff> <fe00>\n";
  11424. $stream .= "<ff00> <ffff> <ff00>\n";
  11425. $stream .= "endbfrange\n";
  11426. $stream .= "endcmap\n";
  11427. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11428. $stream .= "end\n";
  11429. $stream .= "end";
  11430. // ToUnicode Object
  11431. $this->_newobj();
  11432. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11433. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11434. $stream = $this->_getrawstream($stream);
  11435. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11436. // CIDFontType2
  11437. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11438. $oid = $this->_newobj();
  11439. $out = '<< /Type /Font';
  11440. $out .= ' /Subtype /CIDFontType2';
  11441. $out .= ' /BaseFont /'.$fontname;
  11442. // A dictionary containing entries that define the character collection of the CIDFont.
  11443. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11444. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11445. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11446. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11447. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11448. $out .= ' /DW '.$font['dw']; // default width
  11449. $out .= "\n".$this->_putfontwidths($font, 0);
  11450. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11451. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11452. }
  11453. $out .= ' >>';
  11454. $out .= "\n".'endobj';
  11455. $this->_out($out);
  11456. // Font descriptor
  11457. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11458. $this->_newobj();
  11459. $out = '<< /Type /FontDescriptor';
  11460. $out .= ' /FontName /'.$fontname;
  11461. foreach ($font['desc'] as $key => $value) {
  11462. if (is_float($value)) {
  11463. $value = sprintf('%F', $value);
  11464. }
  11465. $out .= ' /'.$key.' '.$value;
  11466. }
  11467. $fontdir = false;
  11468. if (!$this->empty_string($font['file'])) {
  11469. // A stream containing a TrueType font
  11470. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11471. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11472. }
  11473. $out .= ' >>';
  11474. $out .= "\n".'endobj';
  11475. $this->_out($out);
  11476. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11477. $this->_newobj();
  11478. // Embed CIDToGIDMap
  11479. // A specification of the mapping from CIDs to glyph indices
  11480. // search and get CTG font file to embedd
  11481. $ctgfile = strtolower($font['ctg']);
  11482. // search and get ctg font file to embedd
  11483. $fontfile = '';
  11484. // search files on various directories
  11485. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11486. $fontfile = $fontdir.$ctgfile;
  11487. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11488. $fontfile = $this->_getfontpath().$ctgfile;
  11489. } elseif (file_exists($ctgfile)) {
  11490. $fontfile = $ctgfile;
  11491. }
  11492. if ($this->empty_string($fontfile)) {
  11493. $this->Error('Font file not found: '.$ctgfile);
  11494. }
  11495. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11496. $out = '<< /Length '.strlen($stream).'';
  11497. if (substr($fontfile, -2) == '.z') { // check file extension
  11498. // Decompresses data encoded using the public-domain
  11499. // zlib/deflate compression method, reproducing the
  11500. // original text or binary data
  11501. $out .= ' /Filter /FlateDecode';
  11502. }
  11503. $out .= ' >>';
  11504. $out .= ' stream'."\n".$stream."\n".'endstream';
  11505. $out .= "\n".'endobj';
  11506. $this->_out($out);
  11507. }
  11508. }
  11509. /**
  11510. * Output CID-0 fonts.
  11511. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11512. * @param $font (array) font data
  11513. * @protected
  11514. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11515. * @since 3.2.000 (2008-06-23)
  11516. */
  11517. protected function _putcidfont0($font) {
  11518. $cidoffset = 0;
  11519. if (!isset($font['cw'][1])) {
  11520. $cidoffset = 31;
  11521. }
  11522. if (isset($font['cidinfo']['uni2cid'])) {
  11523. // convert unicode to cid.
  11524. $uni2cid = $font['cidinfo']['uni2cid'];
  11525. $cw = array();
  11526. foreach ($font['cw'] as $uni => $width) {
  11527. if (isset($uni2cid[$uni])) {
  11528. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11529. } elseif ($uni < 256) {
  11530. $cw[$uni] = $width;
  11531. } // else unknown character
  11532. }
  11533. $font = array_merge($font, array('cw' => $cw));
  11534. }
  11535. $name = $font['name'];
  11536. $enc = $font['enc'];
  11537. if ($enc) {
  11538. $longname = $name.'-'.$enc;
  11539. } else {
  11540. $longname = $name;
  11541. }
  11542. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11543. $out .= '<</Type /Font';
  11544. $out .= ' /Subtype /Type0';
  11545. $out .= ' /BaseFont /'.$longname;
  11546. $out .= ' /Name /F'.$font['i'];
  11547. if ($enc) {
  11548. $out .= ' /Encoding /'.$enc;
  11549. }
  11550. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11551. $out .= ' >>';
  11552. $out .= "\n".'endobj';
  11553. $this->_out($out);
  11554. $oid = $this->_newobj();
  11555. $out = '<</Type /Font';
  11556. $out .= ' /Subtype /CIDFontType0';
  11557. $out .= ' /BaseFont /'.$name;
  11558. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11559. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11560. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11561. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11562. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11563. $out .= ' /DW '.$font['dw'];
  11564. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11565. $out .= ' >>';
  11566. $out .= "\n".'endobj';
  11567. $this->_out($out);
  11568. $this->_newobj();
  11569. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11570. foreach ($font['desc'] as $k => $v) {
  11571. if ($k != 'Style') {
  11572. if (is_float($v)) {
  11573. $v = sprintf('%F', $v);
  11574. }
  11575. $s .= ' /'.$k.' '.$v.'';
  11576. }
  11577. }
  11578. $s .= '>>';
  11579. $s .= "\n".'endobj';
  11580. $this->_out($s);
  11581. }
  11582. /**
  11583. * Output images.
  11584. * @protected
  11585. */
  11586. protected function _putimages() {
  11587. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11588. foreach ($this->imagekeys as $file) {
  11589. $info = $this->getImageBuffer($file);
  11590. // set object for alternate images array
  11591. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11592. $altoid = $this->_newobj();
  11593. $out = '[';
  11594. foreach ($info['altimgs'] as $altimage) {
  11595. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11596. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11597. $out .= ' /DefaultForPrinting';
  11598. if ($altimage[1] === true) {
  11599. $out .= ' true';
  11600. } else {
  11601. $out .= ' false';
  11602. }
  11603. $out .= ' >>';
  11604. }
  11605. }
  11606. $out .= ' ]';
  11607. $out .= "\n".'endobj';
  11608. $this->_out($out);
  11609. }
  11610. // set image object
  11611. $oid = $this->_newobj();
  11612. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11613. $this->setImageSubBuffer($file, 'n', $this->n);
  11614. $out = '<</Type /XObject';
  11615. $out .= ' /Subtype /Image';
  11616. $out .= ' /Width '.$info['w'];
  11617. $out .= ' /Height '.$info['h'];
  11618. if (array_key_exists('masked', $info)) {
  11619. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11620. }
  11621. // set color space
  11622. $icc = false;
  11623. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11624. // ICC Colour Space
  11625. $icc = true;
  11626. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11627. } elseif ($info['cs'] == 'Indexed') {
  11628. // Indexed Colour Space
  11629. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11630. } else {
  11631. // Device Colour Space
  11632. $out .= ' /ColorSpace /'.$info['cs'];
  11633. }
  11634. if ($info['cs'] == 'DeviceCMYK') {
  11635. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11636. }
  11637. $out .= ' /BitsPerComponent '.$info['bpc'];
  11638. if (isset($altoid) AND ($altoid > 0)) {
  11639. // reference to alternate images dictionary
  11640. $out .= ' /Alternates '.$altoid.' 0 R';
  11641. }
  11642. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11643. // external stream
  11644. $out .= ' /Length 0';
  11645. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11646. if (isset($info['f'])) {
  11647. $out .= ' /FFilter /'.$info['f'];
  11648. }
  11649. $out .= ' >>';
  11650. $out .= ' stream'."\n".'endstream';
  11651. } else {
  11652. if (isset($info['f'])) {
  11653. $out .= ' /Filter /'.$info['f'];
  11654. }
  11655. if (isset($info['parms'])) {
  11656. $out .= ' '.$info['parms'];
  11657. }
  11658. if (isset($info['trns']) AND is_array($info['trns'])) {
  11659. $trns = '';
  11660. $count_info = count($info['trns']);
  11661. for ($i=0; $i < $count_info; ++$i) {
  11662. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11663. }
  11664. $out .= ' /Mask ['.$trns.']';
  11665. }
  11666. $stream = $this->_getrawstream($info['data']);
  11667. $out .= ' /Length '.strlen($stream).' >>';
  11668. $out .= ' stream'."\n".$stream."\n".'endstream';
  11669. }
  11670. $out .= "\n".'endobj';
  11671. $this->_out($out);
  11672. if ($icc) {
  11673. // ICC colour profile
  11674. $this->_newobj();
  11675. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11676. $icc = $this->_getrawstream($icc);
  11677. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11678. } elseif ($info['cs'] == 'Indexed') {
  11679. // colour palette
  11680. $this->_newobj();
  11681. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11682. $pal = $this->_getrawstream($pal);
  11683. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11684. }
  11685. }
  11686. }
  11687. /**
  11688. * Output Form XObjects Templates.
  11689. * @author Nicola Asuni
  11690. * @since 5.8.017 (2010-08-24)
  11691. * @protected
  11692. * @see startTemplate(), endTemplate(), printTemplate()
  11693. */
  11694. protected function _putxobjects() {
  11695. foreach ($this->xobjects as $key => $data) {
  11696. if (isset($data['outdata'])) {
  11697. $stream = trim($data['outdata']);
  11698. $out = $this->_getobj($data['n'])."\n";
  11699. $out .= '<<';
  11700. $out .= ' /Type /XObject';
  11701. $out .= ' /Subtype /Form';
  11702. $out .= ' /FormType 1';
  11703. if ($this->compress) {
  11704. $stream = gzcompress($stream);
  11705. $out .= ' /Filter /FlateDecode';
  11706. }
  11707. $out .= sprintf(' /BBox [%F %F %F %F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  11708. $out .= ' /Matrix [1 0 0 1 0 0]';
  11709. $out .= ' /Resources <<';
  11710. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11711. if (!$this->pdfa_mode) {
  11712. // transparency
  11713. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11714. $out .= ' /ExtGState <<';
  11715. foreach ($data['extgstates'] as $k => $extgstate) {
  11716. if (isset($this->extgstates[$k]['name'])) {
  11717. $out .= ' /'.$this->extgstates[$k]['name'];
  11718. } else {
  11719. $out .= ' /GS'.$k;
  11720. }
  11721. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11722. }
  11723. $out .= ' >>';
  11724. }
  11725. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11726. $gp = '';
  11727. $gs = '';
  11728. foreach ($data['gradients'] as $id => $grad) {
  11729. // gradient patterns
  11730. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11731. // gradient shadings
  11732. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11733. }
  11734. $out .= ' /Pattern <<'.$gp.' >>';
  11735. $out .= ' /Shading <<'.$gs.' >>';
  11736. }
  11737. }
  11738. // spot colors
  11739. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11740. $out .= ' /ColorSpace <<';
  11741. foreach ($data['spot_colors'] as $name => $color) {
  11742. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11743. }
  11744. $out .= ' >>';
  11745. }
  11746. // fonts
  11747. if (!empty($data['fonts'])) {
  11748. $out .= ' /Font <<';
  11749. foreach ($data['fonts'] as $fontkey => $fontid) {
  11750. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11751. }
  11752. $out .= ' >>';
  11753. }
  11754. // images or nested xobjects
  11755. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11756. $out .= ' /XObject <<';
  11757. foreach ($data['images'] as $imgid) {
  11758. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11759. }
  11760. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11761. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11762. }
  11763. $out .= ' >>';
  11764. }
  11765. $out .= ' >>'; //end resources
  11766. if (isset($data['group']) AND ($data['group'] !== false)) {
  11767. // set transparency group
  11768. $out .= ' /Group << /Type /Group /S /Transparency';
  11769. if (is_array($data['group'])) {
  11770. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11771. $out .= ' /CS /'.$data['group']['CS'];
  11772. }
  11773. if (isset($data['group']['I'])) {
  11774. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11775. }
  11776. if (isset($data['group']['K'])) {
  11777. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11778. }
  11779. }
  11780. $out .= ' >>';
  11781. }
  11782. $stream = $this->_getrawstream($stream, $data['n']);
  11783. $out .= ' /Length '.strlen($stream);
  11784. $out .= ' >>';
  11785. $out .= ' stream'."\n".$stream."\n".'endstream';
  11786. $out .= "\n".'endobj';
  11787. $this->_out($out);
  11788. }
  11789. }
  11790. }
  11791. /**
  11792. * Output Spot Colors Resources.
  11793. * @protected
  11794. * @since 4.0.024 (2008-09-12)
  11795. */
  11796. protected function _putspotcolors() {
  11797. foreach ($this->spot_colors as $name => $color) {
  11798. $this->_newobj();
  11799. $this->spot_colors[$name]['n'] = $this->n;
  11800. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  11801. $out .= ' /DeviceCMYK <<';
  11802. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  11803. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  11804. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  11805. $out .= "\n".'endobj';
  11806. $this->_out($out);
  11807. }
  11808. }
  11809. /**
  11810. * Return XObjects Dictionary.
  11811. * @return string XObjects dictionary
  11812. * @protected
  11813. * @since 5.8.014 (2010-08-23)
  11814. */
  11815. protected function _getxobjectdict() {
  11816. $out = '';
  11817. foreach ($this->xobjects as $id => $objid) {
  11818. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  11819. }
  11820. return $out;
  11821. }
  11822. /**
  11823. * Output Resources Dictionary.
  11824. * @protected
  11825. */
  11826. protected function _putresourcedict() {
  11827. $out = $this->_getobj(2)."\n";
  11828. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11829. $out .= ' /Font <<';
  11830. foreach ($this->fontkeys as $fontkey) {
  11831. $font = $this->getFontBuffer($fontkey);
  11832. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  11833. }
  11834. $out .= ' >>';
  11835. $out .= ' /XObject <<';
  11836. $out .= $this->_getxobjectdict();
  11837. $out .= ' >>';
  11838. // layers
  11839. if (!empty($this->pdflayers)) {
  11840. $out .= ' /Properties <<';
  11841. foreach ($this->pdflayers as $layer) {
  11842. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  11843. }
  11844. $out .= ' >>';
  11845. }
  11846. if (!$this->pdfa_mode) {
  11847. // transparency
  11848. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  11849. $out .= ' /ExtGState <<';
  11850. foreach ($this->extgstates as $k => $extgstate) {
  11851. if (isset($extgstate['name'])) {
  11852. $out .= ' /'.$extgstate['name'];
  11853. } else {
  11854. $out .= ' /GS'.$k;
  11855. }
  11856. $out .= ' '.$extgstate['n'].' 0 R';
  11857. }
  11858. $out .= ' >>';
  11859. }
  11860. if (isset($this->gradients) AND !empty($this->gradients)) {
  11861. $gp = '';
  11862. $gs = '';
  11863. foreach ($this->gradients as $id => $grad) {
  11864. // gradient patterns
  11865. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  11866. // gradient shadings
  11867. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  11868. }
  11869. $out .= ' /Pattern <<'.$gp.' >>';
  11870. $out .= ' /Shading <<'.$gs.' >>';
  11871. }
  11872. }
  11873. // spot colors
  11874. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  11875. $out .= ' /ColorSpace <<';
  11876. foreach ($this->spot_colors as $color) {
  11877. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  11878. }
  11879. $out .= ' >>';
  11880. }
  11881. $out .= ' >>';
  11882. $out .= "\n".'endobj';
  11883. $this->_out($out);
  11884. }
  11885. /**
  11886. * Output Resources.
  11887. * @protected
  11888. */
  11889. protected function _putresources() {
  11890. $this->_putextgstates();
  11891. $this->_putocg();
  11892. $this->_putfonts();
  11893. $this->_putimages();
  11894. $this->_putspotcolors();
  11895. $this->_putshaders();
  11896. $this->_putxobjects();
  11897. $this->_putresourcedict();
  11898. $this->_putdests();
  11899. $this->_putbookmarks();
  11900. $this->_putEmbeddedFiles();
  11901. $this->_putannotsobjs();
  11902. $this->_putjavascript();
  11903. $this->_putencryption();
  11904. }
  11905. /**
  11906. * Adds some Metadata information (Document Information Dictionary)
  11907. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  11908. * @return int object id
  11909. * @protected
  11910. */
  11911. protected function _putinfo() {
  11912. $oid = $this->_newobj();
  11913. $out = '<<';
  11914. // store current isunicode value
  11915. $prev_isunicode = $this->isunicode;
  11916. if ($this->docinfounicode) {
  11917. $this->isunicode = true;
  11918. }
  11919. if (!$this->empty_string($this->title)) {
  11920. // The document's title.
  11921. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  11922. }
  11923. if (!$this->empty_string($this->author)) {
  11924. // The name of the person who created the document.
  11925. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  11926. }
  11927. if (!$this->empty_string($this->subject)) {
  11928. // The subject of the document.
  11929. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  11930. }
  11931. if (!$this->empty_string($this->keywords)) {
  11932. // Keywords associated with the document.
  11933. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  11934. }
  11935. if (!$this->empty_string($this->creator)) {
  11936. // 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.
  11937. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  11938. }
  11939. // restore previous isunicode value
  11940. $this->isunicode = $prev_isunicode;
  11941. // default producer
  11942. $out .= ' /Producer '.$this->_textstring($this->pdfproducer, $oid);
  11943. // The date and time the document was created, in human-readable form
  11944. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  11945. // The date and time the document was most recently modified, in human-readable form
  11946. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  11947. // A name object indicating whether the document has been modified to include trapping information
  11948. $out .= ' /Trapped /False';
  11949. $out .= ' >>';
  11950. $out .= "\n".'endobj';
  11951. $this->_out($out);
  11952. return $oid;
  11953. }
  11954. /**
  11955. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  11956. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  11957. * @param $xmp (string) Custom XMP data.
  11958. * @since 5.9.128 (2011-10-06)
  11959. * @public
  11960. */
  11961. public function setExtraXMP($xmp) {
  11962. $this->custom_xmp = $xmp;
  11963. }
  11964. /**
  11965. * Put XMP data object and return ID.
  11966. * @return (int) The object ID.
  11967. * @since 5.9.121 (2011-09-28)
  11968. * @protected
  11969. */
  11970. protected function _putXMP() {
  11971. $oid = $this->_newobj();
  11972. // store current isunicode value
  11973. $prev_isunicode = $this->isunicode;
  11974. $this->isunicode = true;
  11975. $prev_encrypted = $this->encrypted;
  11976. $this->encrypted = false;
  11977. // set XMP data
  11978. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  11979. $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";
  11980. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  11981. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  11982. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  11983. $xmp .= "\t\t\t".'<dc:title>'."\n";
  11984. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11985. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  11986. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11987. $xmp .= "\t\t\t".'</dc:title>'."\n";
  11988. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  11989. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  11990. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  11991. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  11992. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  11993. $xmp .= "\t\t\t".'<dc:description>'."\n";
  11994. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11995. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  11996. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11997. $xmp .= "\t\t\t".'</dc:description>'."\n";
  11998. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  11999. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  12000. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  12001. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  12002. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  12003. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12004. // convert doc creation date format
  12005. $dcdate = $this->getFormattedDate($this->doc_creation_timestamp);
  12006. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  12007. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  12008. $doccreationdate .= '+'.substr($dcdate, 15, 2).':'.substr($dcdate, 18, 2);
  12009. $doccreationdate = $this->_escapeXML($doccreationdate);
  12010. // convert doc modification date format
  12011. $dmdate = $this->getFormattedDate($this->doc_modification_timestamp);
  12012. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  12013. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  12014. $docmoddate .= '+'.substr($dmdate, 15, 2).':'.substr($dmdate, 18, 2);
  12015. $docmoddate = $this->_escapeXML($docmoddate);
  12016. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  12017. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  12018. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  12019. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  12020. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  12021. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12022. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  12023. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  12024. $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML($this->pdfproducer).'</pdf:Producer>'."\n";
  12025. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12026. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  12027. $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);
  12028. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  12029. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  12030. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12031. if ($this->pdfa_mode) {
  12032. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  12033. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  12034. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  12035. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12036. }
  12037. // XMP extension schemas
  12038. $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";
  12039. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  12040. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  12041. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12042. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  12043. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  12044. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  12045. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12046. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12047. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  12048. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  12049. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  12050. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  12051. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  12052. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12053. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12054. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  12055. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  12056. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  12057. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12058. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  12059. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  12060. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12061. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12062. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  12063. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  12064. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  12065. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  12066. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  12067. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12068. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12069. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  12070. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  12071. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  12072. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12073. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12074. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12075. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  12076. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  12077. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  12078. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12079. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12080. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12081. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  12082. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  12083. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  12084. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12085. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  12086. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  12087. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12088. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  12089. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  12090. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12091. $xmp .= "\t".'</rdf:RDF>'."\n";
  12092. $xmp .= $this->custom_xmp;
  12093. $xmp .= '</x:xmpmeta>'."\n";
  12094. $xmp .= '<?xpacket end="w"?>';
  12095. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  12096. // restore previous isunicode value
  12097. $this->isunicode = $prev_isunicode;
  12098. $this->encrypted = $prev_encrypted;
  12099. $this->_out($out);
  12100. return $oid;
  12101. }
  12102. /**
  12103. * Output Catalog.
  12104. * @return int object id
  12105. * @protected
  12106. */
  12107. protected function _putcatalog() {
  12108. // put XMP
  12109. $xmpobj = $this->_putXMP();
  12110. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  12111. if ($this->pdfa_mode OR $this->force_srgb) {
  12112. $iccobj = $this->_newobj();
  12113. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  12114. $filter = '';
  12115. if ($this->compress) {
  12116. $filter = ' /Filter /FlateDecode';
  12117. $icc = gzcompress($icc);
  12118. }
  12119. $icc = $this->_getrawstream($icc);
  12120. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  12121. }
  12122. // start catalog
  12123. $oid = $this->_newobj();
  12124. $out = '<< /Type /Catalog';
  12125. $out .= ' /Version /'.$this->PDFVersion;
  12126. //$out .= ' /Extensions <<>>';
  12127. $out .= ' /Pages 1 0 R';
  12128. //$out .= ' /PageLabels ' //...;
  12129. $out .= ' /Names <<';
  12130. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  12131. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  12132. }
  12133. $out .= ' >>';
  12134. if (!empty($this->dests)) {
  12135. $out .= ' /Dests '.$this->n_dests.' 0 R';
  12136. }
  12137. $out .= $this->_putviewerpreferences();
  12138. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  12139. $out .= ' /PageLayout /'.$this->LayoutMode;
  12140. }
  12141. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  12142. $out .= ' /PageMode /'.$this->PageMode;
  12143. }
  12144. if (count($this->outlines) > 0) {
  12145. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  12146. $out .= ' /PageMode /UseOutlines';
  12147. }
  12148. //$out .= ' /Threads []';
  12149. if ($this->ZoomMode == 'fullpage') {
  12150. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  12151. } elseif ($this->ZoomMode == 'fullwidth') {
  12152. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  12153. } elseif ($this->ZoomMode == 'real') {
  12154. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  12155. } elseif (!is_string($this->ZoomMode)) {
  12156. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  12157. }
  12158. //$out .= ' /AA <<>>';
  12159. //$out .= ' /URI <<>>';
  12160. $out .= ' /Metadata '.$xmpobj.' 0 R';
  12161. //$out .= ' /StructTreeRoot <<>>';
  12162. //$out .= ' /MarkInfo <<>>';
  12163. if (isset($this->l['a_meta_language'])) {
  12164. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  12165. }
  12166. //$out .= ' /SpiderInfo <<>>';
  12167. // set OutputIntent to sRGB IEC61966-2.1 if required
  12168. if ($this->pdfa_mode OR $this->force_srgb) {
  12169. $out .= ' /OutputIntents [<<';
  12170. $out .= ' /Type /OutputIntent';
  12171. $out .= ' /S /GTS_PDFA1';
  12172. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12173. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12174. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12175. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12176. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12177. $out .= ' >>]';
  12178. }
  12179. //$out .= ' /PieceInfo <<>>';
  12180. if (!empty($this->pdflayers)) {
  12181. $lyrobjs = '';
  12182. $lyrobjs_print = '';
  12183. $lyrobjs_view = '';
  12184. foreach ($this->pdflayers as $layer) {
  12185. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12186. if ($layer['print']) {
  12187. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12188. }
  12189. if ($layer['view']) {
  12190. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12191. }
  12192. }
  12193. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12194. $out .= ' /D <<';
  12195. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12196. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12197. $out .= ' /BaseState /ON';
  12198. $out .= ' /ON ['.$lyrobjs_print.']';
  12199. $out .= ' /OFF ['.$lyrobjs_view.']';
  12200. $out .= ' /Intent /View';
  12201. $out .= ' /AS [';
  12202. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12203. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12204. $out .= ' ]';
  12205. $out .= ' /Order ['.$lyrobjs.']';
  12206. $out .= ' /ListMode /AllPages';
  12207. //$out .= ' /RBGroups ['..']';
  12208. //$out .= ' /Locked ['..']';
  12209. $out .= ' >>';
  12210. $out .= ' >>';
  12211. }
  12212. // AcroForm
  12213. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12214. $out .= ' /AcroForm <<';
  12215. $objrefs = '';
  12216. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12217. // set reference for signature object
  12218. $objrefs .= $this->sig_obj_id.' 0 R';
  12219. }
  12220. if (!empty($this->empty_signature_appearance)) {
  12221. foreach ($this->empty_signature_appearance as $esa) {
  12222. // set reference for empty signature objects
  12223. $objrefs .= ' '.$esa['objid'].' 0 R';
  12224. }
  12225. }
  12226. if (!empty($this->form_obj_id)) {
  12227. foreach($this->form_obj_id as $objid) {
  12228. $objrefs .= ' '.$objid.' 0 R';
  12229. }
  12230. }
  12231. $out .= ' /Fields ['.$objrefs.']';
  12232. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12233. $out .= ' /NeedAppearances false';
  12234. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12235. if ($this->signature_data['cert_type'] > 0) {
  12236. $out .= ' /SigFlags 3';
  12237. } else {
  12238. $out .= ' /SigFlags 1';
  12239. }
  12240. }
  12241. //$out .= ' /CO ';
  12242. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12243. $out .= ' /DR <<';
  12244. $out .= ' /Font <<';
  12245. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12246. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12247. }
  12248. $out .= ' >> >>';
  12249. }
  12250. $font = $this->getFontBuffer('helvetica');
  12251. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12252. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12253. //$out .= ' /XFA ';
  12254. $out .= ' >>';
  12255. // signatures
  12256. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12257. if ($this->signature_data['cert_type'] > 0) {
  12258. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12259. } else {
  12260. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12261. }
  12262. }
  12263. }
  12264. //$out .= ' /Legal <<>>';
  12265. //$out .= ' /Requirements []';
  12266. //$out .= ' /Collection <<>>';
  12267. //$out .= ' /NeedsRendering true';
  12268. $out .= ' >>';
  12269. $out .= "\n".'endobj';
  12270. $this->_out($out);
  12271. return $oid;
  12272. }
  12273. /**
  12274. * Output viewer preferences.
  12275. * @return string for viewer preferences
  12276. * @author Nicola asuni
  12277. * @since 3.1.000 (2008-06-09)
  12278. * @protected
  12279. */
  12280. protected function _putviewerpreferences() {
  12281. $out = ' /ViewerPreferences <<';
  12282. if ($this->rtl) {
  12283. $out .= ' /Direction /R2L';
  12284. } else {
  12285. $out .= ' /Direction /L2R';
  12286. }
  12287. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12288. $out .= ' /HideToolbar true';
  12289. }
  12290. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12291. $out .= ' /HideMenubar true';
  12292. }
  12293. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12294. $out .= ' /HideWindowUI true';
  12295. }
  12296. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12297. $out .= ' /FitWindow true';
  12298. }
  12299. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12300. $out .= ' /CenterWindow true';
  12301. }
  12302. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12303. $out .= ' /DisplayDocTitle true';
  12304. }
  12305. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12306. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12307. }
  12308. if (isset($this->viewer_preferences['ViewArea'])) {
  12309. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12310. }
  12311. if (isset($this->viewer_preferences['ViewClip'])) {
  12312. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12313. }
  12314. if (isset($this->viewer_preferences['PrintArea'])) {
  12315. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12316. }
  12317. if (isset($this->viewer_preferences['PrintClip'])) {
  12318. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12319. }
  12320. if (isset($this->viewer_preferences['PrintScaling'])) {
  12321. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12322. }
  12323. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12324. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12325. }
  12326. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12327. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12328. $out .= ' /PickTrayByPDFSize true';
  12329. } else {
  12330. $out .= ' /PickTrayByPDFSize false';
  12331. }
  12332. }
  12333. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12334. $PrintPageRangeNum = '';
  12335. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12336. $PrintPageRangeNum .= ' '.($v - 1).'';
  12337. }
  12338. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12339. }
  12340. if (isset($this->viewer_preferences['NumCopies'])) {
  12341. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12342. }
  12343. $out .= ' >>';
  12344. return $out;
  12345. }
  12346. /**
  12347. * Output PDF File Header (7.5.2).
  12348. * @protected
  12349. */
  12350. protected function _putheader() {
  12351. $this->_out('%PDF-'.$this->PDFVersion);
  12352. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12353. }
  12354. /**
  12355. * Output end of document (EOF).
  12356. * @protected
  12357. */
  12358. protected function _enddoc() {
  12359. $this->state = 1;
  12360. $this->_putheader();
  12361. $this->_putpages();
  12362. $this->_putresources();
  12363. // empty signature fields
  12364. if (!empty($this->empty_signature_appearance)) {
  12365. foreach ($this->empty_signature_appearance as $key => $esa) {
  12366. // widget annotation for empty signature
  12367. $out = $this->_getobj($esa['objid'])."\n";
  12368. $out .= '<< /Type /Annot';
  12369. $out .= ' /Subtype /Widget';
  12370. $out .= ' /Rect ['.$esa['rect'].']';
  12371. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12372. $out .= ' /F 4';
  12373. $out .= ' /FT /Sig';
  12374. $signame = sprintf('Signature_%03d', ($key + 1));
  12375. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12376. $out .= ' /Ff 0';
  12377. $out .= ' >>';
  12378. $out .= "\n".'endobj';
  12379. $this->_out($out);
  12380. }
  12381. }
  12382. // Signature
  12383. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12384. // widget annotation for signature
  12385. $out = $this->_getobj($this->sig_obj_id)."\n";
  12386. $out .= '<< /Type /Annot';
  12387. $out .= ' /Subtype /Widget';
  12388. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12389. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12390. $out .= ' /F 4';
  12391. $out .= ' /FT /Sig';
  12392. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12393. $out .= ' /Ff 0';
  12394. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12395. $out .= ' >>';
  12396. $out .= "\n".'endobj';
  12397. $this->_out($out);
  12398. // signature
  12399. $this->_putsignature();
  12400. }
  12401. // Info
  12402. $objid_info = $this->_putinfo();
  12403. // Catalog
  12404. $objid_catalog = $this->_putcatalog();
  12405. // Cross-ref
  12406. $o = $this->bufferlen;
  12407. // XREF section
  12408. $this->_out('xref');
  12409. $this->_out('0 '.($this->n + 1));
  12410. $this->_out('0000000000 65535 f ');
  12411. for ($i=1; $i <= $this->n; ++$i) {
  12412. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12413. $this->offsets[$i] = $this->offsets[($i - 1)];
  12414. }
  12415. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12416. }
  12417. // TRAILER
  12418. $out = 'trailer'."\n";
  12419. $out .= '<<';
  12420. $out .= ' /Size '.($this->n + 1);
  12421. $out .= ' /Root '.$objid_catalog.' 0 R';
  12422. $out .= ' /Info '.$objid_info.' 0 R';
  12423. if ($this->encrypted) {
  12424. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12425. }
  12426. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12427. $out .= ' >>';
  12428. $this->_out($out);
  12429. $this->_out('startxref');
  12430. $this->_out($o);
  12431. $this->_out('%%EOF');
  12432. $this->state = 3; // end-of-doc
  12433. if ($this->diskcache) {
  12434. // remove temporary files used for images
  12435. foreach ($this->imagekeys as $key) {
  12436. // remove temporary files
  12437. unlink($this->images[$key]);
  12438. }
  12439. foreach ($this->fontkeys as $key) {
  12440. // remove temporary files
  12441. unlink($this->fonts[$key]);
  12442. }
  12443. }
  12444. }
  12445. /**
  12446. * Initialize a new page.
  12447. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12448. * @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().
  12449. * @protected
  12450. * @see getPageSizeFromFormat(), setPageFormat()
  12451. */
  12452. protected function _beginpage($orientation='', $format='') {
  12453. ++$this->page;
  12454. $this->setPageBuffer($this->page, '');
  12455. // initialize array for graphics tranformation positions inside a page buffer
  12456. $this->transfmrk[$this->page] = array();
  12457. $this->state = 2;
  12458. if ($this->empty_string($orientation)) {
  12459. if (isset($this->CurOrientation)) {
  12460. $orientation = $this->CurOrientation;
  12461. } elseif ($this->fwPt > $this->fhPt) {
  12462. // landscape
  12463. $orientation = 'L';
  12464. } else {
  12465. // portrait
  12466. $orientation = 'P';
  12467. }
  12468. }
  12469. if ($this->empty_string($format)) {
  12470. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12471. $this->setPageOrientation($orientation);
  12472. } else {
  12473. $this->setPageFormat($format, $orientation);
  12474. }
  12475. if ($this->rtl) {
  12476. $this->x = $this->w - $this->rMargin;
  12477. } else {
  12478. $this->x = $this->lMargin;
  12479. }
  12480. $this->y = $this->tMargin;
  12481. if (isset($this->newpagegroup[$this->page])) {
  12482. // start a new group
  12483. $this->currpagegroup = $this->newpagegroup[$this->page];
  12484. $this->pagegroups[$this->currpagegroup] = 1;
  12485. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12486. ++$this->pagegroups[$this->currpagegroup];
  12487. }
  12488. }
  12489. /**
  12490. * Mark end of page.
  12491. * @protected
  12492. */
  12493. protected function _endpage() {
  12494. $this->setVisibility('all');
  12495. $this->state = 1;
  12496. }
  12497. /**
  12498. * Begin a new object and return the object number.
  12499. * @return int object number
  12500. * @protected
  12501. */
  12502. protected function _newobj() {
  12503. $this->_out($this->_getobj());
  12504. return $this->n;
  12505. }
  12506. /**
  12507. * Return the starting object string for the selected object ID.
  12508. * @param $objid (int) Object ID (leave empty to get a new ID).
  12509. * @return string the starting object string
  12510. * @protected
  12511. * @since 5.8.009 (2010-08-20)
  12512. */
  12513. protected function _getobj($objid='') {
  12514. if ($objid === '') {
  12515. ++$this->n;
  12516. $objid = $this->n;
  12517. }
  12518. $this->offsets[$objid] = $this->bufferlen;
  12519. return $objid.' 0 obj';
  12520. }
  12521. /**
  12522. * Underline text.
  12523. * @param $x (int) X coordinate
  12524. * @param $y (int) Y coordinate
  12525. * @param $txt (string) text to underline
  12526. * @protected
  12527. */
  12528. protected function _dounderline($x, $y, $txt) {
  12529. $w = $this->GetStringWidth($txt);
  12530. return $this->_dounderlinew($x, $y, $w);
  12531. }
  12532. /**
  12533. * Underline for rectangular text area.
  12534. * @param $x (int) X coordinate
  12535. * @param $y (int) Y coordinate
  12536. * @param $w (int) width to underline
  12537. * @protected
  12538. * @since 4.8.008 (2009-09-29)
  12539. */
  12540. protected function _dounderlinew($x, $y, $w) {
  12541. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12542. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12543. }
  12544. /**
  12545. * Line through text.
  12546. * @param $x (int) X coordinate
  12547. * @param $y (int) Y coordinate
  12548. * @param $txt (string) text to linethrough
  12549. * @protected
  12550. */
  12551. protected function _dolinethrough($x, $y, $txt) {
  12552. $w = $this->GetStringWidth($txt);
  12553. return $this->_dolinethroughw($x, $y, $w);
  12554. }
  12555. /**
  12556. * Line through for rectangular text area.
  12557. * @param $x (int) X coordinate
  12558. * @param $y (int) Y coordinate
  12559. * @param $w (int) line length (width)
  12560. * @protected
  12561. * @since 4.9.008 (2009-09-29)
  12562. */
  12563. protected function _dolinethroughw($x, $y, $w) {
  12564. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12565. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12566. }
  12567. /**
  12568. * Overline text.
  12569. * @param $x (int) X coordinate
  12570. * @param $y (int) Y coordinate
  12571. * @param $txt (string) text to overline
  12572. * @protected
  12573. * @since 4.9.015 (2010-04-19)
  12574. */
  12575. protected function _dooverline($x, $y, $txt) {
  12576. $w = $this->GetStringWidth($txt);
  12577. return $this->_dooverlinew($x, $y, $w);
  12578. }
  12579. /**
  12580. * Overline for rectangular text area.
  12581. * @param $x (int) X coordinate
  12582. * @param $y (int) Y coordinate
  12583. * @param $w (int) width to overline
  12584. * @protected
  12585. * @since 4.9.015 (2010-04-19)
  12586. */
  12587. protected function _dooverlinew($x, $y, $w) {
  12588. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12589. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12590. }
  12591. /**
  12592. * Read a 4-byte (32 bit) integer from file.
  12593. * @param $f (string) file name.
  12594. * @return 4-byte integer
  12595. * @protected
  12596. */
  12597. protected function _freadint($f) {
  12598. $a = unpack('Ni', fread($f, 4));
  12599. return $a['i'];
  12600. }
  12601. /**
  12602. * Add "\" before "\", "(" and ")"
  12603. * @param $s (string) string to escape.
  12604. * @return string escaped string.
  12605. * @protected
  12606. */
  12607. protected function _escape($s) {
  12608. // the chr(13) substitution fixes the Bugs item #1421290.
  12609. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12610. }
  12611. /**
  12612. * Format a data string for meta information
  12613. * @param $s (string) data string to escape.
  12614. * @param $n (int) object ID
  12615. * @return string escaped string.
  12616. * @protected
  12617. */
  12618. protected function _datastring($s, $n=0) {
  12619. if ($n == 0) {
  12620. $n = $this->n;
  12621. }
  12622. $s = $this->_encrypt_data($n, $s);
  12623. return '('. $this->_escape($s).')';
  12624. }
  12625. /**
  12626. * Set the document creation timestamp
  12627. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  12628. * @public
  12629. * @since 5.9.152 (2012-03-23)
  12630. */
  12631. public function setDocCreationTimestamp($time) {
  12632. if (is_string($time)) {
  12633. $time = getTimestamp($time);
  12634. }
  12635. $this->doc_creation_timestamp = intval($time);
  12636. }
  12637. /**
  12638. * Set the document modification timestamp
  12639. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  12640. * @public
  12641. * @since 5.9.152 (2012-03-23)
  12642. */
  12643. public function setDocModificationTimestamp($time) {
  12644. if (is_string($time)) {
  12645. $time = getTimestamp($time);
  12646. }
  12647. $this->doc_modification_timestamp = intval($time);
  12648. }
  12649. /**
  12650. * Returns document creation timestamp in seconds.
  12651. * @return (int) Creation timestamp in seconds.
  12652. * @public
  12653. * @since 5.9.152 (2012-03-23)
  12654. */
  12655. public function getDocCreationTimestamp() {
  12656. return $this->doc_creation_timestamp;
  12657. }
  12658. /**
  12659. * Returns document modification timestamp in seconds.
  12660. * @return (int) Modfication timestamp in seconds.
  12661. * @public
  12662. * @since 5.9.152 (2012-03-23)
  12663. */
  12664. public function getDocModificationTimestamp() {
  12665. return $this->doc_modification_timestamp;
  12666. }
  12667. /**
  12668. * Returns timestamp in seconds from formatted date-time.
  12669. * @param $date (string) Formatted date-time.
  12670. * @return int seconds.
  12671. * @public
  12672. * @since 5.9.152 (2012-03-23)
  12673. */
  12674. public function getTimestamp($date) {
  12675. if (($date[0] == 'D') AND ($date[1] == ':')) {
  12676. // remove date prefix if present
  12677. $date = substr($date, 2);
  12678. }
  12679. return strtotime($date);
  12680. }
  12681. /**
  12682. * Returns a formatted date-time.
  12683. * @param $time (int) Time in seconds.
  12684. * @return string escaped date string.
  12685. * @public
  12686. * @since 5.9.152 (2012-03-23)
  12687. */
  12688. public function getFormattedDate($time) {
  12689. return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
  12690. }
  12691. /**
  12692. * Returns a formatted date for meta information
  12693. * @param $n (int) Object ID.
  12694. * @param $timestamp (int) Timestamp to convert.
  12695. * @return string escaped date string.
  12696. * @protected
  12697. * @since 4.6.028 (2009-08-25)
  12698. */
  12699. protected function _datestring($n=0, $timestamp=0) {
  12700. if ((empty($timestamp)) OR ($timestamp < 0)) {
  12701. $timestamp = $this->doc_creation_timestamp;
  12702. }
  12703. return $this->_datastring('D:'.$this->getFormattedDate($timestamp), $n);
  12704. }
  12705. /**
  12706. * Format a text string for meta information
  12707. * @param $s (string) string to escape.
  12708. * @param $n (int) object ID
  12709. * @return string escaped string.
  12710. * @protected
  12711. */
  12712. protected function _textstring($s, $n=0) {
  12713. if ($this->isunicode) {
  12714. //Convert string to UTF-16BE
  12715. $s = $this->UTF8ToUTF16BE($s, true);
  12716. }
  12717. return $this->_datastring($s, $n);
  12718. }
  12719. /**
  12720. * THIS METHOD IS DEPRECATED
  12721. * Format a text string
  12722. * @param $s (string) string to escape.
  12723. * @return string escaped string.
  12724. * @protected
  12725. * @deprecated
  12726. */
  12727. protected function _escapetext($s) {
  12728. if ($this->isunicode) {
  12729. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12730. $s = $this->UTF8ToLatin1($s);
  12731. } else {
  12732. //Convert string to UTF-16BE and reverse RTL language
  12733. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12734. }
  12735. }
  12736. return $this->_escape($s);
  12737. }
  12738. /**
  12739. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12740. * @param $str (string) Input string to convert.
  12741. * @return converted string
  12742. * @since 5.9.121 (2011-09-28)
  12743. * @protected
  12744. */
  12745. protected function _escapeXML($str) {
  12746. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12747. $str = strtr($str, $replaceTable);
  12748. return $str;
  12749. }
  12750. /**
  12751. * get raw output stream.
  12752. * @param $s (string) string to output.
  12753. * @param $n (int) object reference for encryption mode
  12754. * @protected
  12755. * @author Nicola Asuni
  12756. * @since 5.5.000 (2010-06-22)
  12757. */
  12758. protected function _getrawstream($s, $n=0) {
  12759. if ($n <= 0) {
  12760. // default to current object
  12761. $n = $this->n;
  12762. }
  12763. return $this->_encrypt_data($n, $s);
  12764. }
  12765. /**
  12766. * Format output stream (DEPRECATED).
  12767. * @param $s (string) string to output.
  12768. * @param $n (int) object reference for encryption mode
  12769. * @protected
  12770. * @deprecated
  12771. */
  12772. protected function _getstream($s, $n=0) {
  12773. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12774. }
  12775. /**
  12776. * Output a stream (DEPRECATED).
  12777. * @param $s (string) string to output.
  12778. * @param $n (int) object reference for encryption mode
  12779. * @protected
  12780. * @deprecated
  12781. */
  12782. protected function _putstream($s, $n=0) {
  12783. $this->_out($this->_getstream($s, $n));
  12784. }
  12785. /**
  12786. * Output a string to the document.
  12787. * @param $s (string) string to output.
  12788. * @protected
  12789. */
  12790. protected function _out($s) {
  12791. if ($this->state == 2) {
  12792. if ($this->inxobj) {
  12793. // we are inside an XObject template
  12794. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  12795. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  12796. // puts data before page footer
  12797. $pagebuff = $this->getPageBuffer($this->page);
  12798. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  12799. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  12800. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  12801. // update footer position
  12802. $this->footerpos[$this->page] += strlen($s."\n");
  12803. } else {
  12804. $this->setPageBuffer($this->page, $s."\n", true);
  12805. }
  12806. } else {
  12807. $this->setBuffer($s."\n");
  12808. }
  12809. }
  12810. /**
  12811. * Converts UTF-8 strings to codepoints array.<br>
  12812. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  12813. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  12814. * <pre>
  12815. * Char. number range | UTF-8 octet sequence
  12816. * (hexadecimal) | (binary)
  12817. * --------------------+-----------------------------------------------
  12818. * 0000 0000-0000 007F | 0xxxxxxx
  12819. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  12820. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  12821. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  12822. * ---------------------------------------------------------------------
  12823. *
  12824. * ABFN notation:
  12825. * ---------------------------------------------------------------------
  12826. * UTF8-octets = *( UTF8-char )
  12827. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  12828. * UTF8-1 = %x00-7F
  12829. * UTF8-2 = %xC2-DF UTF8-tail
  12830. *
  12831. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  12832. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  12833. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  12834. * %xF4 %x80-8F 2( UTF8-tail )
  12835. * UTF8-tail = %x80-BF
  12836. * ---------------------------------------------------------------------
  12837. * </pre>
  12838. * @param $str (string) string to process.
  12839. * @return array containing codepoints (UTF-8 characters values)
  12840. * @protected
  12841. * @author Nicola Asuni
  12842. * @since 1.53.0.TC005 (2005-01-05)
  12843. */
  12844. protected function UTF8StringToArray($str) {
  12845. // build a unique string key
  12846. $strkey = md5($str);
  12847. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  12848. // return cached value
  12849. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  12850. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  12851. if ($this->isunicode) {
  12852. foreach ($chrarray as $chr) {
  12853. // store this char for font subsetting
  12854. $this->CurrentFont['subsetchars'][$chr] = true;
  12855. }
  12856. // update font subsetchars
  12857. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12858. }
  12859. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12860. }
  12861. return $chrarray;
  12862. }
  12863. // check cache size
  12864. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  12865. // remove first element
  12866. array_shift($this->cache_UTF8StringToArray);
  12867. }
  12868. // new cache array for selected string
  12869. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  12870. ++$this->cache_size_UTF8StringToArray;
  12871. if (!$this->isunicode) {
  12872. // split string into array of equivalent codes
  12873. $strarr = array();
  12874. $strlen = strlen($str);
  12875. for ($i=0; $i < $strlen; ++$i) {
  12876. $strarr[] = ord($str[$i]);
  12877. }
  12878. // insert new value on cache
  12879. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  12880. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12881. return $strarr;
  12882. }
  12883. $unichar = -1; // last unicode char
  12884. $unicode = array(); // array containing unicode values
  12885. $bytes = array(); // array containing single character byte sequences
  12886. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  12887. $str .= ''; // force $str to be a string
  12888. $length = strlen($str);
  12889. for ($i = 0; $i < $length; ++$i) {
  12890. $char = ord($str[$i]); // get one string character at time
  12891. if (count($bytes) == 0) { // get starting octect
  12892. if ($char <= 0x7F) {
  12893. $unichar = $char; // use the character "as is" because is ASCII
  12894. $numbytes = 1;
  12895. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  12896. $bytes[] = ($char - 0xC0) << 0x06;
  12897. $numbytes = 2;
  12898. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  12899. $bytes[] = ($char - 0xE0) << 0x0C;
  12900. $numbytes = 3;
  12901. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  12902. $bytes[] = ($char - 0xF0) << 0x12;
  12903. $numbytes = 4;
  12904. } else {
  12905. // use replacement character for other invalid sequences
  12906. $unichar = 0xFFFD;
  12907. $bytes = array();
  12908. $numbytes = 1;
  12909. }
  12910. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  12911. $bytes[] = $char - 0x80;
  12912. if (count($bytes) == $numbytes) {
  12913. // compose UTF-8 bytes to a single unicode value
  12914. $char = $bytes[0];
  12915. for ($j = 1; $j < $numbytes; ++$j) {
  12916. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  12917. }
  12918. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  12919. /* The definition of UTF-8 prohibits encoding character numbers between
  12920. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  12921. encoding form (as surrogate pairs) and do not directly represent
  12922. characters. */
  12923. $unichar = 0xFFFD; // use replacement character
  12924. } else {
  12925. $unichar = $char; // add char to array
  12926. }
  12927. // reset data for next char
  12928. $bytes = array();
  12929. $numbytes = 1;
  12930. }
  12931. } else {
  12932. // use replacement character for other invalid sequences
  12933. $unichar = 0xFFFD;
  12934. $bytes = array();
  12935. $numbytes = 1;
  12936. }
  12937. if ($unichar >= 0) {
  12938. // insert unicode value into array
  12939. $unicode[] = $unichar;
  12940. // store this char for font subsetting
  12941. $this->CurrentFont['subsetchars'][$unichar] = true;
  12942. $unichar = -1;
  12943. }
  12944. }
  12945. // update font subsetchars
  12946. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12947. // insert new value on cache
  12948. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  12949. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12950. return $unicode;
  12951. }
  12952. /**
  12953. * Converts UTF-8 strings to UTF16-BE.<br>
  12954. * @param $str (string) string to process.
  12955. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12956. * @return string
  12957. * @author Nicola Asuni
  12958. * @since 1.53.0.TC005 (2005-01-05)
  12959. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  12960. * @protected
  12961. */
  12962. protected function UTF8ToUTF16BE($str, $setbom=false) {
  12963. if (!$this->isunicode) {
  12964. return $str; // string is not in unicode
  12965. }
  12966. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12967. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  12968. }
  12969. /**
  12970. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  12971. * @param $str (string) string to process.
  12972. * @return string
  12973. * @author Andrew Whitehead, Nicola Asuni
  12974. * @protected
  12975. * @since 3.2.000 (2008-06-23)
  12976. */
  12977. protected function UTF8ToLatin1($str) {
  12978. if (!$this->isunicode) {
  12979. return $str; // string is not in unicode
  12980. }
  12981. $outstr = ''; // string to be returned
  12982. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12983. foreach ($unicode as $char) {
  12984. if ($char < 256) {
  12985. $outstr .= chr($char);
  12986. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12987. // map from UTF-8
  12988. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  12989. } elseif ($char == 0xFFFD) {
  12990. // skip
  12991. } else {
  12992. $outstr .= '?';
  12993. }
  12994. }
  12995. return $outstr;
  12996. }
  12997. /**
  12998. * Converts UTF-8 characters array to array of Latin1 characters<br>
  12999. * @param $unicode (array) array containing UTF-8 unicode values
  13000. * @return array
  13001. * @author Nicola Asuni
  13002. * @protected
  13003. * @since 4.8.023 (2010-01-15)
  13004. */
  13005. protected function UTF8ArrToLatin1($unicode) {
  13006. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  13007. return $unicode;
  13008. }
  13009. $outarr = array(); // array to be returned
  13010. foreach ($unicode as $char) {
  13011. if ($char < 256) {
  13012. $outarr[] = $char;
  13013. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  13014. // map from UTF-8
  13015. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  13016. } elseif ($char == 0xFFFD) {
  13017. // skip
  13018. } else {
  13019. $outarr[] = 63; // '?' character
  13020. }
  13021. }
  13022. return $outarr;
  13023. }
  13024. /**
  13025. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  13026. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  13027. * <pre>
  13028. * Encoding UTF-16:
  13029. *
  13030. * Encoding of a single character from an ISO 10646 character value to
  13031. * UTF-16 proceeds as follows. Let U be the character number, no greater
  13032. * than 0x10FFFF.
  13033. *
  13034. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  13035. * terminate.
  13036. *
  13037. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  13038. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  13039. * represented in 20 bits.
  13040. *
  13041. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  13042. * 0xDC00, respectively. These integers each have 10 bits free to
  13043. * encode the character value, for a total of 20 bits.
  13044. *
  13045. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  13046. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  13047. * bits of W2. Terminate.
  13048. *
  13049. * Graphically, steps 2 through 4 look like:
  13050. * U' = yyyyyyyyyyxxxxxxxxxx
  13051. * W1 = 110110yyyyyyyyyy
  13052. * W2 = 110111xxxxxxxxxx
  13053. * </pre>
  13054. * @param $unicode (array) array containing UTF-8 unicode values
  13055. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  13056. * @return string
  13057. * @protected
  13058. * @author Nicola Asuni
  13059. * @since 2.1.000 (2008-01-08)
  13060. * @see UTF8ToUTF16BE()
  13061. */
  13062. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  13063. $outstr = ''; // string to be returned
  13064. if ($setbom) {
  13065. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  13066. }
  13067. foreach ($unicode as $char) {
  13068. if ($char == 0x200b) {
  13069. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  13070. } elseif ($char == 0xFFFD) {
  13071. $outstr .= "\xFF\xFD"; // replacement character
  13072. } elseif ($char < 0x10000) {
  13073. $outstr .= chr($char >> 0x08);
  13074. $outstr .= chr($char & 0xFF);
  13075. } else {
  13076. $char -= 0x10000;
  13077. $w1 = 0xD800 | ($char >> 0x0a);
  13078. $w2 = 0xDC00 | ($char & 0x3FF);
  13079. $outstr .= chr($w1 >> 0x08);
  13080. $outstr .= chr($w1 & 0xFF);
  13081. $outstr .= chr($w2 >> 0x08);
  13082. $outstr .= chr($w2 & 0xFF);
  13083. }
  13084. }
  13085. return $outstr;
  13086. }
  13087. // ====================================================
  13088. /**
  13089. * Set header font.
  13090. * @param $font (array) font
  13091. * @public
  13092. * @since 1.1
  13093. */
  13094. public function setHeaderFont($font) {
  13095. $this->header_font = $font;
  13096. }
  13097. /**
  13098. * Get header font.
  13099. * @return array()
  13100. * @public
  13101. * @since 4.0.012 (2008-07-24)
  13102. */
  13103. public function getHeaderFont() {
  13104. return $this->header_font;
  13105. }
  13106. /**
  13107. * Set footer font.
  13108. * @param $font (array) font
  13109. * @public
  13110. * @since 1.1
  13111. */
  13112. public function setFooterFont($font) {
  13113. $this->footer_font = $font;
  13114. }
  13115. /**
  13116. * Get Footer font.
  13117. * @return array()
  13118. * @public
  13119. * @since 4.0.012 (2008-07-24)
  13120. */
  13121. public function getFooterFont() {
  13122. return $this->footer_font;
  13123. }
  13124. /**
  13125. * Set language array.
  13126. * @param $language (array)
  13127. * @public
  13128. * @since 1.1
  13129. */
  13130. public function setLanguageArray($language) {
  13131. $this->l = $language;
  13132. if (isset($this->l['a_meta_dir'])) {
  13133. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  13134. } else {
  13135. $this->rtl = false;
  13136. }
  13137. }
  13138. /**
  13139. * Returns the PDF data.
  13140. * @public
  13141. */
  13142. public function getPDFData() {
  13143. if ($this->state < 3) {
  13144. $this->Close();
  13145. }
  13146. return $this->buffer;
  13147. }
  13148. /**
  13149. * Output anchor link.
  13150. * @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;)
  13151. * @param $name (string) link name
  13152. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  13153. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  13154. * @param $color (array) array of RGB text color
  13155. * @param $style (string) font style (U, D, B, I)
  13156. * @param $firstblock (boolean) if true the string is the starting of a line.
  13157. * @return the number of cells used or the remaining text if $firstline = true;
  13158. * @public
  13159. */
  13160. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  13161. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  13162. // convert url to internal link
  13163. $lnkdata = explode(',', $url);
  13164. if (isset($lnkdata[0])) {
  13165. $page = intval(substr($lnkdata[0], 1));
  13166. if (empty($page) OR ($page <= 0)) {
  13167. $page = $this->page;
  13168. }
  13169. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  13170. $lnky = floatval($lnkdata[1]);
  13171. } else {
  13172. $lnky = 0;
  13173. }
  13174. $url = $this->AddLink();
  13175. $this->SetLink($url, $lnky, $page);
  13176. }
  13177. }
  13178. // store current settings
  13179. $prevcolor = $this->fgcolor;
  13180. $prevstyle = $this->FontStyle;
  13181. if (empty($color)) {
  13182. $this->SetTextColorArray($this->htmlLinkColorArray);
  13183. } else {
  13184. $this->SetTextColorArray($color);
  13185. }
  13186. if ($style == -1) {
  13187. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  13188. } else {
  13189. $this->SetFont('', $this->FontStyle.$style);
  13190. }
  13191. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  13192. // restore settings
  13193. $this->SetFont('', $prevstyle);
  13194. $this->SetTextColorArray($prevcolor);
  13195. return $ret;
  13196. }
  13197. /**
  13198. * 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.
  13199. * @param $hcolor (string) HTML color.
  13200. * @param $defcol (array) Color to return in case of error.
  13201. * @return array RGB or CMYK color, or false in case of error.
  13202. * @public
  13203. */
  13204. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  13205. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  13206. $color = strtolower($color);
  13207. // check for javascript color array syntax
  13208. if (strpos($color, '[') !== false) {
  13209. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  13210. $returncolor = array();
  13211. switch ($m[1]) {
  13212. case 'cmyk': {
  13213. // RGB
  13214. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  13215. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  13216. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  13217. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  13218. break;
  13219. }
  13220. case 'rgb': {
  13221. // RGB
  13222. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  13223. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  13224. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  13225. break;
  13226. }
  13227. case 'g': {
  13228. // grayscale
  13229. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  13230. break;
  13231. }
  13232. case 't':
  13233. default: {
  13234. // transparent (empty array)
  13235. break;
  13236. }
  13237. }
  13238. return $returncolor;
  13239. }
  13240. } elseif (($dotpos = strpos($color, '.')) !== false) {
  13241. // remove class parent (i.e.: color.red)
  13242. $color = substr($color, ($dotpos + 1));
  13243. if ($color == 'transparent') {
  13244. // transparent (empty array)
  13245. return array();
  13246. }
  13247. }
  13248. if (strlen($color) == 0) {
  13249. return $defcol;
  13250. }
  13251. // RGB ARRAY
  13252. if (substr($color, 0, 3) == 'rgb') {
  13253. $codes = substr($color, 4);
  13254. $codes = str_replace(')', '', $codes);
  13255. $returncolor = explode(',', $codes);
  13256. foreach ($returncolor as $key => $val) {
  13257. if (strpos($val, '%') > 0) {
  13258. // percentage
  13259. $returncolor[$key] = (255 * intval($val) / 100);
  13260. } else {
  13261. $returncolor[$key] = intval($val);
  13262. }
  13263. // normalize value
  13264. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  13265. }
  13266. return $returncolor;
  13267. }
  13268. // CMYK ARRAY
  13269. if (substr($color, 0, 4) == 'cmyk') {
  13270. $codes = substr($color, 5);
  13271. $codes = str_replace(')', '', $codes);
  13272. $returncolor = explode(',', $codes);
  13273. foreach ($returncolor as $key => $val) {
  13274. if (strpos($val, '%') !== false) {
  13275. // percentage
  13276. $returncolor[$key] = (100 * intval($val) / 100);
  13277. } else {
  13278. $returncolor[$key] = intval($val);
  13279. }
  13280. // normalize value
  13281. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  13282. }
  13283. return $returncolor;
  13284. }
  13285. if ($color{0} != '#') {
  13286. // COLOR NAME
  13287. if (isset($this->webcolor[$color])) {
  13288. // web color
  13289. $color_code = $this->webcolor[$color];
  13290. } else {
  13291. // spot color
  13292. $returncolor = $this->getSpotColor($color);
  13293. if ($returncolor === false) {
  13294. $returncolor = $defcol;
  13295. }
  13296. return $returncolor;
  13297. }
  13298. } else {
  13299. $color_code = substr($color, 1);
  13300. }
  13301. // HEXADECIMAL REPRESENTATION
  13302. switch (strlen($color_code)) {
  13303. case 3: {
  13304. // 3-digit RGB hexadecimal representation
  13305. $r = substr($color_code, 0, 1);
  13306. $g = substr($color_code, 1, 1);
  13307. $b = substr($color_code, 2, 1);
  13308. $returncolor = array();
  13309. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  13310. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  13311. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  13312. break;
  13313. }
  13314. case 6: {
  13315. // 6-digit RGB hexadecimal representation
  13316. $returncolor = array();
  13317. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  13318. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  13319. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  13320. break;
  13321. }
  13322. case 8: {
  13323. // 8-digit CMYK hexadecimal representation
  13324. $returncolor = array();
  13325. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  13326. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  13327. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  13328. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  13329. break;
  13330. }
  13331. default: {
  13332. $returncolor = $defcol;
  13333. break;
  13334. }
  13335. }
  13336. return $returncolor;
  13337. }
  13338. /**
  13339. * Converts pixels to User's Units.
  13340. * @param $px (int) pixels
  13341. * @return float value in user's unit
  13342. * @public
  13343. * @see setImageScale(), getImageScale()
  13344. */
  13345. public function pixelsToUnits($px) {
  13346. return ($px / ($this->imgscale * $this->k));
  13347. }
  13348. /**
  13349. * Reverse function for htmlentities.
  13350. * Convert entities in UTF-8.
  13351. * @param $text_to_convert (string) Text to convert.
  13352. * @return string converted text string
  13353. * @public
  13354. */
  13355. public function unhtmlentities($text_to_convert) {
  13356. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  13357. }
  13358. // ENCRYPTION METHODS ----------------------------------
  13359. /**
  13360. * Returns a string containing random data to be used as a seed for encryption methods.
  13361. * @param $seed (string) starting seed value
  13362. * @return string containing random data
  13363. * @author Nicola Asuni
  13364. * @since 5.9.006 (2010-10-19)
  13365. * @protected
  13366. */
  13367. protected function getRandomSeed($seed='') {
  13368. $seed .= microtime();
  13369. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  13370. // this is not used on windows systems because it is very slow for a know bug
  13371. $seed .= openssl_random_pseudo_bytes(512);
  13372. } else {
  13373. for ($i = 0; $i < 23; ++$i) {
  13374. $seed .= uniqid('', true);
  13375. }
  13376. }
  13377. $seed .= uniqid('', true);
  13378. $seed .= rand();
  13379. $seed .= getmypid();
  13380. $seed .= __FILE__;
  13381. $seed .= $this->bufferlen;
  13382. if (isset($_SERVER['REMOTE_ADDR'])) {
  13383. $seed .= $_SERVER['REMOTE_ADDR'];
  13384. }
  13385. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  13386. $seed .= $_SERVER['HTTP_USER_AGENT'];
  13387. }
  13388. if (isset($_SERVER['HTTP_ACCEPT'])) {
  13389. $seed .= $_SERVER['HTTP_ACCEPT'];
  13390. }
  13391. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  13392. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  13393. }
  13394. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  13395. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  13396. }
  13397. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  13398. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  13399. }
  13400. $seed .= rand();
  13401. $seed .= uniqid('', true);
  13402. $seed .= microtime();
  13403. return $seed;
  13404. }
  13405. /**
  13406. * Compute encryption key depending on object number where the encrypted data is stored.
  13407. * This is used for all strings and streams without crypt filter specifier.
  13408. * @param $n (int) object number
  13409. * @return int object key
  13410. * @protected
  13411. * @author Nicola Asuni
  13412. * @since 2.0.000 (2008-01-02)
  13413. */
  13414. protected function _objectkey($n) {
  13415. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  13416. if ($this->encryptdata['mode'] == 2) { // AES-128
  13417. // AES padding
  13418. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  13419. }
  13420. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  13421. $objkey = substr($objkey, 0, 16);
  13422. return $objkey;
  13423. }
  13424. /**
  13425. * Encrypt the input string.
  13426. * @param $n (int) object number
  13427. * @param $s (string) data string to encrypt
  13428. * @return encrypted string
  13429. * @protected
  13430. * @author Nicola Asuni
  13431. * @since 5.0.005 (2010-05-11)
  13432. */
  13433. protected function _encrypt_data($n, $s) {
  13434. if (!$this->encrypted) {
  13435. return $s;
  13436. }
  13437. switch ($this->encryptdata['mode']) {
  13438. case 0: // RC4-40
  13439. case 1: { // RC4-128
  13440. $s = $this->_RC4($this->_objectkey($n), $s);
  13441. break;
  13442. }
  13443. case 2: { // AES-128
  13444. $s = $this->_AES($this->_objectkey($n), $s);
  13445. break;
  13446. }
  13447. case 3: { // AES-256
  13448. $s = $this->_AES($this->encryptdata['key'], $s);
  13449. break;
  13450. }
  13451. }
  13452. return $s;
  13453. }
  13454. /**
  13455. * Put encryption on PDF document.
  13456. * @protected
  13457. * @author Nicola Asuni
  13458. * @since 2.0.000 (2008-01-02)
  13459. */
  13460. protected function _putencryption() {
  13461. if (!$this->encrypted) {
  13462. return;
  13463. }
  13464. $this->encryptdata['objid'] = $this->_newobj();
  13465. $out = '<<';
  13466. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  13467. $this->encryptdata['Filter'] = 'Standard';
  13468. }
  13469. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  13470. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  13471. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  13472. }
  13473. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  13474. $this->encryptdata['V'] = 1;
  13475. }
  13476. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  13477. $out .= ' /V '.$this->encryptdata['V'];
  13478. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  13479. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  13480. $out .= ' /Length '.$this->encryptdata['Length'];
  13481. } else {
  13482. $out .= ' /Length 40'