PageRenderTime 140ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/phpMyAdmin/libraries/tcpdf/tcpdf.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 13952 lines | 8849 code | 485 blank | 4618 comment | 1693 complexity | 5961b6a722f3607ac94f56fe976b534d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.145
  5. // Begin : 2002-08-03
  6. // Last Update : 2012-01-28
  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.145
  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.145
  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.145';
  159. // Protected properties
  160. /**
  161. * Current page number.
  162. * @protected
  163. */
  164. protected $page;
  165. /**
  166. * Current object number.
  167. * @protected
  168. */
  169. protected $n;
  170. /**
  171. * Array of object offsets.
  172. * @protected
  173. */
  174. protected $offsets;
  175. /**
  176. * Buffer holding in-memory PDF.
  177. * @protected
  178. */
  179. protected $buffer;
  180. /**
  181. * Array containing pages.
  182. * @protected
  183. */
  184. protected $pages = array();
  185. /**
  186. * Current document state.
  187. * @protected
  188. */
  189. protected $state;
  190. /**
  191. * Compression flag.
  192. * @protected
  193. */
  194. protected $compress;
  195. /**
  196. * Current page orientation (P = Portrait, L = Landscape).
  197. * @protected
  198. */
  199. protected $CurOrientation;
  200. /**
  201. * Page dimensions.
  202. * @protected
  203. */
  204. protected $pagedim = array();
  205. /**
  206. * Scale factor (number of points in user unit).
  207. * @protected
  208. */
  209. protected $k;
  210. /**
  211. * Width of page format in points.
  212. * @protected
  213. */
  214. protected $fwPt;
  215. /**
  216. * Height of page format in points.
  217. * @protected
  218. */
  219. protected $fhPt;
  220. /**
  221. * Current width of page in points.
  222. * @protected
  223. */
  224. protected $wPt;
  225. /**
  226. * Current height of page in points.
  227. * @protected
  228. */
  229. protected $hPt;
  230. /**
  231. * Current width of page in user unit.
  232. * @protected
  233. */
  234. protected $w;
  235. /**
  236. * Current height of page in user unit.
  237. * @protected
  238. */
  239. protected $h;
  240. /**
  241. * Left margin.
  242. * @protected
  243. */
  244. protected $lMargin;
  245. /**
  246. * Top margin.
  247. * @protected
  248. */
  249. protected $tMargin;
  250. /**
  251. * Right margin.
  252. * @protected
  253. */
  254. protected $rMargin;
  255. /**
  256. * Page break margin.
  257. * @protected
  258. */
  259. protected $bMargin;
  260. /**
  261. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  262. * @since 5.9.000 (2010-10-03)
  263. * @protected
  264. */
  265. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  266. /**
  267. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  268. * @since 5.9.000 (2010-10-04)
  269. * @protected
  270. */
  271. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  272. /**
  273. * Current horizontal position in user unit for cell positioning.
  274. * @protected
  275. */
  276. protected $x;
  277. /**
  278. * Current vertical position in user unit for cell positioning.
  279. * @protected
  280. */
  281. protected $y;
  282. /**
  283. * Height of last cell printed.
  284. * @protected
  285. */
  286. protected $lasth;
  287. /**
  288. * Line width in user unit.
  289. * @protected
  290. */
  291. protected $LineWidth;
  292. /**
  293. * Array of standard font names.
  294. * @protected
  295. */
  296. protected $CoreFonts;
  297. /**
  298. * Array of used fonts.
  299. * @protected
  300. */
  301. protected $fonts = array();
  302. /**
  303. * Array of font files.
  304. * @protected
  305. */
  306. protected $FontFiles = array();
  307. /**
  308. * Array of encoding differences.
  309. * @protected
  310. */
  311. protected $diffs = array();
  312. /**
  313. * Array of used images.
  314. * @protected
  315. */
  316. protected $images = array();
  317. /**
  318. * Array of Annotations in pages.
  319. * @protected
  320. */
  321. protected $PageAnnots = array();
  322. /**
  323. * Array of internal links.
  324. * @protected
  325. */
  326. protected $links = array();
  327. /**
  328. * Current font family.
  329. * @protected
  330. */
  331. protected $FontFamily;
  332. /**
  333. * Current font style.
  334. * @protected
  335. */
  336. protected $FontStyle;
  337. /**
  338. * Current font ascent (distance between font top and baseline).
  339. * @protected
  340. * @since 2.8.000 (2007-03-29)
  341. */
  342. protected $FontAscent;
  343. /**
  344. * Current font descent (distance between font bottom and baseline).
  345. * @protected
  346. * @since 2.8.000 (2007-03-29)
  347. */
  348. protected $FontDescent;
  349. /**
  350. * Underlining flag.
  351. * @protected
  352. */
  353. protected $underline;
  354. /**
  355. * Overlining flag.
  356. * @protected
  357. */
  358. protected $overline;
  359. /**
  360. * Current font info.
  361. * @protected
  362. */
  363. protected $CurrentFont;
  364. /**
  365. * Current font size in points.
  366. * @protected
  367. */
  368. protected $FontSizePt;
  369. /**
  370. * Current font size in user unit.
  371. * @protected
  372. */
  373. protected $FontSize;
  374. /**
  375. * Commands for drawing color.
  376. * @protected
  377. */
  378. protected $DrawColor;
  379. /**
  380. * Commands for filling color.
  381. * @protected
  382. */
  383. protected $FillColor;
  384. /**
  385. * Commands for text color.
  386. * @protected
  387. */
  388. protected $TextColor;
  389. /**
  390. * Indicates whether fill and text colors are different.
  391. * @protected
  392. */
  393. protected $ColorFlag;
  394. /**
  395. * Automatic page breaking.
  396. * @protected
  397. */
  398. protected $AutoPageBreak;
  399. /**
  400. * Threshold used to trigger page breaks.
  401. * @protected
  402. */
  403. protected $PageBreakTrigger;
  404. /**
  405. * Flag set when processing page header.
  406. * @protected
  407. */
  408. protected $InHeader = false;
  409. /**
  410. * Flag set when processing page footer.
  411. * @protected
  412. */
  413. protected $InFooter = false;
  414. /**
  415. * Zoom display mode.
  416. * @protected
  417. */
  418. protected $ZoomMode;
  419. /**
  420. * Layout display mode.
  421. * @protected
  422. */
  423. protected $LayoutMode;
  424. /**
  425. * If true set the document information dictionary in Unicode.
  426. * @protected
  427. */
  428. protected $docinfounicode = true;
  429. /**
  430. * Document title.
  431. * @protected
  432. */
  433. protected $title = '';
  434. /**
  435. * Document subject.
  436. * @protected
  437. */
  438. protected $subject = '';
  439. /**
  440. * Document author.
  441. * @protected
  442. */
  443. protected $author = '';
  444. /**
  445. * Document keywords.
  446. * @protected
  447. */
  448. protected $keywords = '';
  449. /**
  450. * Document creator.
  451. * @protected
  452. */
  453. protected $creator = '';
  454. /**
  455. * Starting page number.
  456. * @protected
  457. */
  458. protected $starting_page_number = 1;
  459. /**
  460. * String alias for total number of pages.
  461. * @protected
  462. */
  463. protected $alias_tot_pages = '{:ptp:}';
  464. /**
  465. * String alias for page number.
  466. * @protected
  467. */
  468. protected $alias_num_page = '{:pnp:}';
  469. /**
  470. * String alias for total number of pages in a single group.
  471. * @protected
  472. */
  473. protected $alias_group_tot_pages = '{:ptg:}';
  474. /**
  475. * String alias for group page number.
  476. * @protected
  477. */
  478. protected $alias_group_num_page = '{:png:}';
  479. /**
  480. * String alias for right shift compensation used to correctly align page numbers on the right.
  481. * @protected
  482. */
  483. protected $alias_right_shift = '{rsc:';
  484. /**
  485. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  486. * @since 2002-07-31
  487. * @author Nicola Asuni
  488. * @protected
  489. */
  490. protected $img_rb_x;
  491. /**
  492. * The right-bottom corner Y coordinate of last inserted image.
  493. * @since 2002-07-31
  494. * @author Nicola Asuni
  495. * @protected
  496. */
  497. protected $img_rb_y;
  498. /**
  499. * Adjusting factor to convert pixels to user units.
  500. * @since 2004-06-14
  501. * @author Nicola Asuni
  502. * @protected
  503. */
  504. protected $imgscale = 1;
  505. /**
  506. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  507. * @since 2005-01-02
  508. * @author Nicola Asuni
  509. * @protected
  510. */
  511. protected $isunicode = false;
  512. /**
  513. * Object containing unicode data.
  514. * @since 5.9.004 (2010-10-18)
  515. * @author Nicola Asuni
  516. * @protected
  517. */
  518. protected $unicode;
  519. /**
  520. * Object containing font encoding maps.
  521. * @since 5.9.123 (2011-10-01)
  522. * @author Nicola Asuni
  523. * @protected
  524. */
  525. protected $encmaps;
  526. /**
  527. * PDF version.
  528. * @since 1.5.3
  529. * @protected
  530. */
  531. protected $PDFVersion = '1.7';
  532. /**
  533. * ID of the stored default header template (-1 = not set).
  534. * @protected
  535. */
  536. protected $header_xobjid = -1;
  537. /**
  538. * If true reset the Header Xobject template at each page
  539. * @protected
  540. */
  541. protected $header_xobj_autoreset = false;
  542. /**
  543. * Minimum distance between header and top page margin.
  544. * @protected
  545. */
  546. protected $header_margin;
  547. /**
  548. * Minimum distance between footer and bottom page margin.
  549. * @protected
  550. */
  551. protected $footer_margin;
  552. /**
  553. * Original left margin value.
  554. * @protected
  555. * @since 1.53.0.TC013
  556. */
  557. protected $original_lMargin;
  558. /**
  559. * Original right margin value.
  560. * @protected
  561. * @since 1.53.0.TC013
  562. */
  563. protected $original_rMargin;
  564. /**
  565. * Default font used on page header.
  566. * @protected
  567. */
  568. protected $header_font;
  569. /**
  570. * Default font used on page footer.
  571. * @protected
  572. */
  573. protected $footer_font;
  574. /**
  575. * Language templates.
  576. * @protected
  577. */
  578. protected $l;
  579. /**
  580. * Barcode to print on page footer (only if set).
  581. * @protected
  582. */
  583. protected $barcode = false;
  584. /**
  585. * Boolean flag to print/hide page header.
  586. * @protected
  587. */
  588. protected $print_header = true;
  589. /**
  590. * Boolean flag to print/hide page footer.
  591. * @protected
  592. */
  593. protected $print_footer = true;
  594. /**
  595. * Header image logo.
  596. * @protected
  597. */
  598. protected $header_logo = '';
  599. /**
  600. * Width of header image logo in user units.
  601. * @protected
  602. */
  603. protected $header_logo_width = 30;
  604. /**
  605. * Title to be printed on default page header.
  606. * @protected
  607. */
  608. protected $header_title = '';
  609. /**
  610. * String to pring on page header after title.
  611. * @protected
  612. */
  613. protected $header_string = '';
  614. /**
  615. * Default number of columns for html table.
  616. * @protected
  617. */
  618. protected $default_table_columns = 4;
  619. // variables for html parser
  620. /**
  621. * HTML PARSER: array to store current link and rendering styles.
  622. * @protected
  623. */
  624. protected $HREF = array();
  625. /**
  626. * List of available fonts on filesystem.
  627. * @protected
  628. */
  629. protected $fontlist = array();
  630. /**
  631. * Current foreground color.
  632. * @protected
  633. */
  634. protected $fgcolor;
  635. /**
  636. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  637. * @protected
  638. */
  639. protected $listordered = array();
  640. /**
  641. * HTML PARSER: array count list items on nested lists.
  642. * @protected
  643. */
  644. protected $listcount = array();
  645. /**
  646. * HTML PARSER: current list nesting level.
  647. * @protected
  648. */
  649. protected $listnum = 0;
  650. /**
  651. * HTML PARSER: indent amount for lists.
  652. * @protected
  653. */
  654. protected $listindent = 0;
  655. /**
  656. * HTML PARSER: current list indententation level.
  657. * @protected
  658. */
  659. protected $listindentlevel = 0;
  660. /**
  661. * Current background color.
  662. * @protected
  663. */
  664. protected $bgcolor;
  665. /**
  666. * Temporary font size in points.
  667. * @protected
  668. */
  669. protected $tempfontsize = 10;
  670. /**
  671. * Spacer string for LI tags.
  672. * @protected
  673. */
  674. protected $lispacer = '';
  675. /**
  676. * Default encoding.
  677. * @protected
  678. * @since 1.53.0.TC010
  679. */
  680. protected $encoding = 'UTF-8';
  681. /**
  682. * PHP internal encoding.
  683. * @protected
  684. * @since 1.53.0.TC016
  685. */
  686. protected $internal_encoding;
  687. /**
  688. * Boolean flag to indicate if the document language is Right-To-Left.
  689. * @protected
  690. * @since 2.0.000
  691. */
  692. protected $rtl = false;
  693. /**
  694. * Boolean flag used to force RTL or LTR string direction.
  695. * @protected
  696. * @since 2.0.000
  697. */
  698. protected $tmprtl = false;
  699. // --- Variables used for document encryption:
  700. /**
  701. * IBoolean flag indicating whether document is protected.
  702. * @protected
  703. * @since 2.0.000 (2008-01-02)
  704. */
  705. protected $encrypted;
  706. /**
  707. * Array containing encryption settings.
  708. * @protected
  709. * @since 5.0.005 (2010-05-11)
  710. */
  711. protected $encryptdata = array();
  712. /**
  713. * Last RC4 key encrypted (cached for optimisation).
  714. * @protected
  715. * @since 2.0.000 (2008-01-02)
  716. */
  717. protected $last_enc_key;
  718. /**
  719. * Last RC4 computed key.
  720. * @protected
  721. * @since 2.0.000 (2008-01-02)
  722. */
  723. protected $last_enc_key_c;
  724. /**
  725. * Encryption padding string.
  726. * @protected
  727. */
  728. protected $enc_padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  729. /**
  730. * File ID (used on document trailer).
  731. * @protected
  732. * @since 5.0.005 (2010-05-12)
  733. */
  734. protected $file_id;
  735. // --- bookmark ---
  736. /**
  737. * Outlines for bookmark.
  738. * @protected
  739. * @since 2.1.002 (2008-02-12)
  740. */
  741. protected $outlines = array();
  742. /**
  743. * Outline root for bookmark.
  744. * @protected
  745. * @since 2.1.002 (2008-02-12)
  746. */
  747. protected $OutlineRoot;
  748. // --- javascript and form ---
  749. /**
  750. * Javascript code.
  751. * @protected
  752. * @since 2.1.002 (2008-02-12)
  753. */
  754. protected $javascript = '';
  755. /**
  756. * Javascript counter.
  757. * @protected
  758. * @since 2.1.002 (2008-02-12)
  759. */
  760. protected $n_js;
  761. /**
  762. * line trough state
  763. * @protected
  764. * @since 2.8.000 (2008-03-19)
  765. */
  766. protected $linethrough;
  767. /**
  768. * Array with additional document-wide usage rights for the document.
  769. * @protected
  770. * @since 5.8.014 (2010-08-23)
  771. */
  772. protected $ur = array();
  773. /**
  774. * DPI (Dot Per Inch) Document Resolution (do not change).
  775. * @protected
  776. * @since 3.0.000 (2008-03-27)
  777. */
  778. protected $dpi = 72;
  779. /**
  780. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  781. * @protected
  782. * @since 3.0.000 (2008-03-27)
  783. */
  784. protected $newpagegroup = array();
  785. /**
  786. * Array that contains the number of pages in each page group.
  787. * @protected
  788. * @since 3.0.000 (2008-03-27)
  789. */
  790. protected $pagegroups = array();
  791. /**
  792. * Current page group number.
  793. * @protected
  794. * @since 3.0.000 (2008-03-27)
  795. */
  796. protected $currpagegroup = 0;
  797. /**
  798. * Array of transparency objects and parameters.
  799. * @protected
  800. * @since 3.0.000 (2008-03-27)
  801. */
  802. protected $extgstates;
  803. /**
  804. * Set the default JPEG compression quality (1-100).
  805. * @protected
  806. * @since 3.0.000 (2008-03-27)
  807. */
  808. protected $jpeg_quality;
  809. /**
  810. * Default cell height ratio.
  811. * @protected
  812. * @since 3.0.014 (2008-05-23)
  813. */
  814. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  815. /**
  816. * PDF viewer preferences.
  817. * @protected
  818. * @since 3.1.000 (2008-06-09)
  819. */
  820. protected $viewer_preferences;
  821. /**
  822. * A name object specifying how the document should be displayed when opened.
  823. * @protected
  824. * @since 3.1.000 (2008-06-09)
  825. */
  826. protected $PageMode;
  827. /**
  828. * Array for storing gradient information.
  829. * @protected
  830. * @since 3.1.000 (2008-06-09)
  831. */
  832. protected $gradients = array();
  833. /**
  834. * Array used to store positions inside the pages buffer (keys are the page numbers).
  835. * @protected
  836. * @since 3.2.000 (2008-06-26)
  837. */
  838. protected $intmrk = array();
  839. /**
  840. * Array used to store positions inside the pages buffer (keys are the page numbers).
  841. * @protected
  842. * @since 5.7.000 (2010-08-03)
  843. */
  844. protected $bordermrk = array();
  845. /**
  846. * Array used to store page positions to track empty pages (keys are the page numbers).
  847. * @protected
  848. * @since 5.8.007 (2010-08-18)
  849. */
  850. protected $emptypagemrk = array();
  851. /**
  852. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  853. * @protected
  854. * @since 4.6.021 (2009-07-20)
  855. */
  856. protected $cntmrk = array();
  857. /**
  858. * Array used to store footer positions of each page.
  859. * @protected
  860. * @since 3.2.000 (2008-07-01)
  861. */
  862. protected $footerpos = array();
  863. /**
  864. * Array used to store footer length of each page.
  865. * @protected
  866. * @since 4.0.014 (2008-07-29)
  867. */
  868. protected $footerlen = array();
  869. /**
  870. * Boolean flag to indicate if a new line is created.
  871. * @protected
  872. * @since 3.2.000 (2008-07-01)
  873. */
  874. protected $newline = true;
  875. /**
  876. * End position of the latest inserted line.
  877. * @protected
  878. * @since 3.2.000 (2008-07-01)
  879. */
  880. protected $endlinex = 0;
  881. /**
  882. * PDF string for width value of the last line.
  883. * @protected
  884. * @since 4.0.006 (2008-07-16)
  885. */
  886. protected $linestyleWidth = '';
  887. /**
  888. * PDF string for CAP value of the last line.
  889. * @protected
  890. * @since 4.0.006 (2008-07-16)
  891. */
  892. protected $linestyleCap = '0 J';
  893. /**
  894. * PDF string for join value of the last line.
  895. * @protected
  896. * @since 4.0.006 (2008-07-16)
  897. */
  898. protected $linestyleJoin = '0 j';
  899. /**
  900. * PDF string for dash value of the last line.
  901. * @protected
  902. * @since 4.0.006 (2008-07-16)
  903. */
  904. protected $linestyleDash = '[] 0 d';
  905. /**
  906. * Boolean flag to indicate if marked-content sequence is open.
  907. * @protected
  908. * @since 4.0.013 (2008-07-28)
  909. */
  910. protected $openMarkedContent = false;
  911. /**
  912. * Count the latest inserted vertical spaces on HTML.
  913. * @protected
  914. * @since 4.0.021 (2008-08-24)
  915. */
  916. protected $htmlvspace = 0;
  917. /**
  918. * Array of Spot colors.
  919. * @protected
  920. * @since 4.0.024 (2008-09-12)
  921. */
  922. protected $spot_colors = array();
  923. /**
  924. * Symbol used for HTML unordered list items.
  925. * @protected
  926. * @since 4.0.028 (2008-09-26)
  927. */
  928. protected $lisymbol = '';
  929. /**
  930. * String used to mark the beginning and end of EPS image blocks.
  931. * @protected
  932. * @since 4.1.000 (2008-10-18)
  933. */
  934. protected $epsmarker = 'x#!#EPS#!#x';
  935. /**
  936. * Array of transformation matrix.
  937. * @protected
  938. * @since 4.2.000 (2008-10-29)
  939. */
  940. protected $transfmatrix = array();
  941. /**
  942. * Current key for transformation matrix.
  943. * @protected
  944. * @since 4.8.005 (2009-09-17)
  945. */
  946. protected $transfmatrix_key = 0;
  947. /**
  948. * Booklet mode for double-sided pages.
  949. * @protected
  950. * @since 4.2.000 (2008-10-29)
  951. */
  952. protected $booklet = false;
  953. /**
  954. * Epsilon value used for float calculations.
  955. * @protected
  956. * @since 4.2.000 (2008-10-29)
  957. */
  958. protected $feps = 0.005;
  959. /**
  960. * Array used for custom vertical spaces for HTML tags.
  961. * @protected
  962. * @since 4.2.001 (2008-10-30)
  963. */
  964. protected $tagvspaces = array();
  965. /**
  966. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  967. * @protected
  968. * @since 4.2.007 (2008-11-12)
  969. */
  970. protected $customlistindent = -1;
  971. /**
  972. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  973. * @protected
  974. * @since 4.2.010 (2008-11-14)
  975. */
  976. protected $opencell = true;
  977. /**
  978. * Array of files to embedd.
  979. * @protected
  980. * @since 4.4.000 (2008-12-07)
  981. */
  982. protected $embeddedfiles = array();
  983. /**
  984. * Boolean flag to indicate if we are inside a PRE tag.
  985. * @protected
  986. * @since 4.4.001 (2008-12-08)
  987. */
  988. protected $premode = false;
  989. /**
  990. * Array used to store positions of graphics transformation blocks inside the page buffer.
  991. * keys are the page numbers
  992. * @protected
  993. * @since 4.4.002 (2008-12-09)
  994. */
  995. protected $transfmrk = array();
  996. /**
  997. * Default color for html links.
  998. * @protected
  999. * @since 4.4.003 (2008-12-09)
  1000. */
  1001. protected $htmlLinkColorArray = array(0, 0, 255);
  1002. /**
  1003. * Default font style to add to html links.
  1004. * @protected
  1005. * @since 4.4.003 (2008-12-09)
  1006. */
  1007. protected $htmlLinkFontStyle = 'U';
  1008. /**
  1009. * Counts the number of pages.
  1010. * @protected
  1011. * @since 4.5.000 (2008-12-31)
  1012. */
  1013. protected $numpages = 0;
  1014. /**
  1015. * Array containing page lengths in bytes.
  1016. * @protected
  1017. * @since 4.5.000 (2008-12-31)
  1018. */
  1019. protected $pagelen = array();
  1020. /**
  1021. * Counts the number of pages.
  1022. * @protected
  1023. * @since 4.5.000 (2008-12-31)
  1024. */
  1025. protected $numimages = 0;
  1026. /**
  1027. * Store the image keys.
  1028. * @protected
  1029. * @since 4.5.000 (2008-12-31)
  1030. */
  1031. protected $imagekeys = array();
  1032. /**
  1033. * Length of the buffer in bytes.
  1034. * @protected
  1035. * @since 4.5.000 (2008-12-31)
  1036. */
  1037. protected $bufferlen = 0;
  1038. /**
  1039. * If true enables disk caching.
  1040. * @protected
  1041. * @since 4.5.000 (2008-12-31)
  1042. */
  1043. protected $diskcache = false;
  1044. /**
  1045. * Counts the number of fonts.
  1046. * @protected
  1047. * @since 4.5.000 (2009-01-02)
  1048. */
  1049. protected $numfonts = 0;
  1050. /**
  1051. * Store the font keys.
  1052. * @protected
  1053. * @since 4.5.000 (2009-01-02)
  1054. */
  1055. protected $fontkeys = array();
  1056. /**
  1057. * Store the font object IDs.
  1058. * @protected
  1059. * @since 4.8.001 (2009-09-09)
  1060. */
  1061. protected $font_obj_ids = array();
  1062. /**
  1063. * Store the fage status (true when opened, false when closed).
  1064. * @protected
  1065. * @since 4.5.000 (2009-01-02)
  1066. */
  1067. protected $pageopen = array();
  1068. /**
  1069. * Default monospace font.
  1070. * @protected
  1071. * @since 4.5.025 (2009-03-10)
  1072. */
  1073. protected $default_monospaced_font = 'courier';
  1074. /**
  1075. * Cloned copy of the current class object.
  1076. * @protected
  1077. * @since 4.5.029 (2009-03-19)
  1078. */
  1079. protected $objcopy;
  1080. /**
  1081. * Array used to store the lengths of cache files.
  1082. * @protected
  1083. * @since 4.5.029 (2009-03-19)
  1084. */
  1085. protected $cache_file_length = array();
  1086. /**
  1087. * Table header content to be repeated on each new page.
  1088. * @protected
  1089. * @since 4.5.030 (2009-03-20)
  1090. */
  1091. protected $thead = '';
  1092. /**
  1093. * Margins used for table header.
  1094. * @protected
  1095. * @since 4.5.030 (2009-03-20)
  1096. */
  1097. protected $theadMargins = array();
  1098. /**
  1099. * Cache array for UTF8StringToArray() method.
  1100. * @protected
  1101. * @since 4.5.037 (2009-04-07)
  1102. */
  1103. protected $cache_UTF8StringToArray = array();
  1104. /**
  1105. * Maximum size of cache array used for UTF8StringToArray() method.
  1106. * @protected
  1107. * @since 4.5.037 (2009-04-07)
  1108. */
  1109. protected $cache_maxsize_UTF8StringToArray = 8;
  1110. /**
  1111. * Current size of cache array used for UTF8StringToArray() method.
  1112. * @protected
  1113. * @since 4.5.037 (2009-04-07)
  1114. */
  1115. protected $cache_size_UTF8StringToArray = 0;
  1116. /**
  1117. * Boolean flag to enable document digital signature.
  1118. * @protected
  1119. * @since 4.6.005 (2009-04-24)
  1120. */
  1121. protected $sign = false;
  1122. /**
  1123. * Digital signature data.
  1124. * @protected
  1125. * @since 4.6.005 (2009-04-24)
  1126. */
  1127. protected $signature_data = array();
  1128. /**
  1129. * Digital signature max length.
  1130. * @protected
  1131. * @since 4.6.005 (2009-04-24)
  1132. */
  1133. protected $signature_max_length = 11742;
  1134. /**
  1135. * Data for digital signature appearance.
  1136. * @protected
  1137. * @since 5.3.011 (2010-06-16)
  1138. */
  1139. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1140. /**
  1141. * Array of empty digital signature appearances.
  1142. * @protected
  1143. * @since 5.9.101 (2011-07-06)
  1144. */
  1145. protected $empty_signature_appearance = array();
  1146. /**
  1147. * Regular expression used to find blank characters (required for word-wrapping).
  1148. * @protected
  1149. * @since 4.6.006 (2009-04-28)
  1150. */
  1151. protected $re_spaces = '/[^\S\xa0]/';
  1152. /**
  1153. * Array of $re_spaces parts.
  1154. * @protected
  1155. * @since 5.5.011 (2010-07-09)
  1156. */
  1157. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1158. /**
  1159. * Digital signature object ID.
  1160. * @protected
  1161. * @since 4.6.022 (2009-06-23)
  1162. */
  1163. protected $sig_obj_id = 0;
  1164. /**
  1165. * ByteRange placemark used during digital signature process.
  1166. * @protected
  1167. * @since 4.6.028 (2009-08-25)
  1168. */
  1169. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1170. /**
  1171. * Placemark used during digital signature process.
  1172. * @protected
  1173. * @since 4.6.028 (2009-08-25)
  1174. */
  1175. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1176. /**
  1177. * ID of page objects.
  1178. * @protected
  1179. * @since 4.7.000 (2009-08-29)
  1180. */
  1181. protected $page_obj_id = array();
  1182. /**
  1183. * List of form annotations IDs.
  1184. * @protected
  1185. * @since 4.8.000 (2009-09-07)
  1186. */
  1187. protected $form_obj_id = array();
  1188. /**
  1189. * Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
  1190. * @protected
  1191. * @since 4.8.000 (2009-09-07)
  1192. */
  1193. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1194. /**
  1195. * Javascript objects array.
  1196. * @protected
  1197. * @since 4.8.000 (2009-09-07)
  1198. */
  1199. protected $js_objects = array();
  1200. /**
  1201. * Current form action (used during XHTML rendering).
  1202. * @protected
  1203. * @since 4.8.000 (2009-09-07)
  1204. */
  1205. protected $form_action = '';
  1206. /**
  1207. * Current form encryption type (used during XHTML rendering).
  1208. * @protected
  1209. * @since 4.8.000 (2009-09-07)
  1210. */
  1211. protected $form_enctype = 'application/x-www-form-urlencoded';
  1212. /**
  1213. * Current method to submit forms.
  1214. * @protected
  1215. * @since 4.8.000 (2009-09-07)
  1216. */
  1217. protected $form_mode = 'post';
  1218. /**
  1219. * List of fonts used on form fields (fontname => fontkey).
  1220. * @protected
  1221. * @since 4.8.001 (2009-09-09)
  1222. */
  1223. protected $annotation_fonts = array();
  1224. /**
  1225. * List of radio buttons parent objects.
  1226. * @protected
  1227. * @since 4.8.001 (2009-09-09)
  1228. */
  1229. protected $radiobutton_groups = array();
  1230. /**
  1231. * List of radio group objects IDs.
  1232. * @protected
  1233. * @since 4.8.001 (2009-09-09)
  1234. */
  1235. protected $radio_groups = array();
  1236. /**
  1237. * Text indentation value (used for text-indent CSS attribute).
  1238. * @protected
  1239. * @since 4.8.006 (2009-09-23)
  1240. */
  1241. protected $textindent = 0;
  1242. /**
  1243. * Store page number when startTransaction() is called.
  1244. * @protected
  1245. * @since 4.8.006 (2009-09-23)
  1246. */
  1247. protected $start_transaction_page = 0;
  1248. /**
  1249. * Store Y position when startTransaction() is called.
  1250. * @protected
  1251. * @since 4.9.001 (2010-03-28)
  1252. */
  1253. protected $start_transaction_y = 0;
  1254. /**
  1255. * True when we are printing the thead section on a new page.
  1256. * @protected
  1257. * @since 4.8.027 (2010-01-25)
  1258. */
  1259. protected $inthead = false;
  1260. /**
  1261. * Array of column measures (width, space, starting Y position).
  1262. * @protected
  1263. * @since 4.9.001 (2010-03-28)
  1264. */
  1265. protected $columns = array();
  1266. /**
  1267. * Number of colums.
  1268. * @protected
  1269. * @since 4.9.001 (2010-03-28)
  1270. */
  1271. protected $num_columns = 1;
  1272. /**
  1273. * Current column number.
  1274. * @protected
  1275. * @since 4.9.001 (2010-03-28)
  1276. */
  1277. protected $current_column = 0;
  1278. /**
  1279. * Starting page for columns.
  1280. * @protected
  1281. * @since 4.9.001 (2010-03-28)
  1282. */
  1283. protected $column_start_page = 0;
  1284. /**
  1285. * Maximum page and column selected.
  1286. * @protected
  1287. * @since 5.8.000 (2010-08-11)
  1288. */
  1289. protected $maxselcol = array('page' => 0, 'column' => 0);
  1290. /**
  1291. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1292. * @protected
  1293. * @since 5.8.000 (2010-08-11)
  1294. */
  1295. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1296. /**
  1297. * Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
  1298. * @protected
  1299. * @since 4.9.008 (2010-04-03)
  1300. */
  1301. protected $textrendermode = 0;
  1302. /**
  1303. * Text stroke width in doc units.
  1304. * @protected
  1305. * @since 4.9.008 (2010-04-03)
  1306. */
  1307. protected $textstrokewidth = 0;
  1308. /**
  1309. * Current stroke color.
  1310. * @protected
  1311. * @since 4.9.008 (2010-04-03)
  1312. */
  1313. protected $strokecolor;
  1314. /**
  1315. * Default unit of measure for document.
  1316. * @protected
  1317. * @since 5.0.000 (2010-04-22)
  1318. */
  1319. protected $pdfunit = 'mm';
  1320. /**
  1321. * Boolean flag true when we are on TOC (Table Of Content) page.
  1322. * @protected
  1323. */
  1324. protected $tocpage = false;
  1325. /**
  1326. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1327. * @protected
  1328. * @since 5.0.000 (2010-04-26)
  1329. */
  1330. protected $rasterize_vector_images = false;
  1331. /**
  1332. * Boolean flag: if true enables font subsetting by default.
  1333. * @protected
  1334. * @since 5.3.002 (2010-06-07)
  1335. */
  1336. protected $font_subsetting = true;
  1337. /**
  1338. * Array of default graphic settings.
  1339. * @protected
  1340. * @since 5.5.008 (2010-07-02)
  1341. */
  1342. protected $default_graphic_vars = array();
  1343. /**
  1344. * Array of XObjects.
  1345. * @protected
  1346. * @since 5.8.014 (2010-08-23)
  1347. */
  1348. protected $xobjects = array();
  1349. /**
  1350. * Boolean value true when we are inside an XObject.
  1351. * @protected
  1352. * @since 5.8.017 (2010-08-24)
  1353. */
  1354. protected $inxobj = false;
  1355. /**
  1356. * Current XObject ID.
  1357. * @protected
  1358. * @since 5.8.017 (2010-08-24)
  1359. */
  1360. protected $xobjid = '';
  1361. /**
  1362. * Percentage of character stretching.
  1363. * @protected
  1364. * @since 5.9.000 (2010-09-29)
  1365. */
  1366. protected $font_stretching = 100;
  1367. /**
  1368. * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
  1369. * @protected
  1370. * @since 5.9.000 (2010-09-29)
  1371. */
  1372. protected $font_spacing = 0;
  1373. /**
  1374. * Array of no-write regions.
  1375. * ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
  1376. * @protected
  1377. * @since 5.9.003 (2010-10-14)
  1378. */
  1379. protected $page_regions = array();
  1380. /**
  1381. * Array containing HTML color names and values.
  1382. * @protected
  1383. * @since 5.9.004 (2010-10-18)
  1384. */
  1385. protected $webcolor = array();
  1386. /**
  1387. * Array containing spot color names and values.
  1388. * @protected
  1389. * @since 5.9.012 (2010-11-11)
  1390. */
  1391. protected $spotcolor = array();
  1392. /**
  1393. * Array of PDF layers data.
  1394. * @protected
  1395. * @since 5.9.102 (2011-07-13)
  1396. */
  1397. protected $pdflayers = array();
  1398. /**
  1399. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1400. * @protected
  1401. * @since 5.9.097 (2011-06-23)
  1402. */
  1403. protected $dests = array();
  1404. /**
  1405. * Object ID for Named Destinations
  1406. * @protected
  1407. * @since 5.9.097 (2011-06-23)
  1408. */
  1409. protected $n_dests;
  1410. /**
  1411. * Directory used for the last SVG image.
  1412. * @protected
  1413. * @since 5.0.000 (2010-05-05)
  1414. */
  1415. protected $svgdir = '';
  1416. /**
  1417. * Deafult unit of measure for SVG.
  1418. * @protected
  1419. * @since 5.0.000 (2010-05-02)
  1420. */
  1421. protected $svgunit = 'px';
  1422. /**
  1423. * Array of SVG gradients.
  1424. * @protected
  1425. * @since 5.0.000 (2010-05-02)
  1426. */
  1427. protected $svggradients = array();
  1428. /**
  1429. * ID of last SVG gradient.
  1430. * @protected
  1431. * @since 5.0.000 (2010-05-02)
  1432. */
  1433. protected $svggradientid = 0;
  1434. /**
  1435. * Boolean value true when in SVG defs group.
  1436. * @protected
  1437. * @since 5.0.000 (2010-05-02)
  1438. */
  1439. protected $svgdefsmode = false;
  1440. /**
  1441. * Array of SVG defs.
  1442. * @protected
  1443. * @since 5.0.000 (2010-05-02)
  1444. */
  1445. protected $svgdefs = array();
  1446. /**
  1447. * Boolean value true when in SVG clipPath tag.
  1448. * @protected
  1449. * @since 5.0.000 (2010-04-26)
  1450. */
  1451. protected $svgclipmode = false;
  1452. /**
  1453. * Array of SVG clipPath commands.
  1454. * @protected
  1455. * @since 5.0.000 (2010-05-02)
  1456. */
  1457. protected $svgclippaths = array();
  1458. /**
  1459. * Array of SVG clipPath tranformation matrix.
  1460. * @protected
  1461. * @since 5.8.022 (2010-08-31)
  1462. */
  1463. protected $svgcliptm = array();
  1464. /**
  1465. * ID of last SVG clipPath.
  1466. * @protected
  1467. * @since 5.0.000 (2010-05-02)
  1468. */
  1469. protected $svgclipid = 0;
  1470. /**
  1471. * SVG text.
  1472. * @protected
  1473. * @since 5.0.000 (2010-05-02)
  1474. */
  1475. protected $svgtext = '';
  1476. /**
  1477. * SVG text properties.
  1478. * @protected
  1479. * @since 5.8.013 (2010-08-23)
  1480. */
  1481. protected $svgtextmode = array();
  1482. /**
  1483. * Array of hinheritable SVG properties.
  1484. * @protected
  1485. * @since 5.0.000 (2010-05-02)
  1486. */
  1487. protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
  1488. /**
  1489. * Array of SVG properties.
  1490. * @protected
  1491. * @since 5.0.000 (2010-05-02)
  1492. */
  1493. protected $svgstyles = array(array(
  1494. 'alignment-baseline' => 'auto',
  1495. 'baseline-shift' => 'baseline',
  1496. 'clip' => 'auto',
  1497. 'clip-path' => 'none',
  1498. 'clip-rule' => 'nonzero',
  1499. 'color' => 'black',
  1500. 'color-interpolation' => 'sRGB',
  1501. 'color-interpolation-filters' => 'linearRGB',
  1502. 'color-profile' => 'auto',
  1503. 'color-rendering' => 'auto',
  1504. 'cursor' => 'auto',
  1505. 'direction' => 'ltr',
  1506. 'display' => 'inline',
  1507. 'dominant-baseline' => 'auto',
  1508. 'enable-background' => 'accumulate',
  1509. 'fill' => 'black',
  1510. 'fill-opacity' => 1,
  1511. 'fill-rule' => 'nonzero',
  1512. 'filter' => 'none',
  1513. 'flood-color' => 'black',
  1514. 'flood-opacity' => 1,
  1515. 'font' => '',
  1516. 'font-family' => 'helvetica',
  1517. 'font-size' => 'medium',
  1518. 'font-size-adjust' => 'none',
  1519. 'font-stretch' => 'normal',
  1520. 'font-style' => 'normal',
  1521. 'font-variant' => 'normal',
  1522. 'font-weight' => 'normal',
  1523. 'glyph-orientation-horizontal' => '0deg',
  1524. 'glyph-orientation-vertical' => 'auto',
  1525. 'image-rendering' => 'auto',
  1526. 'kerning' => 'auto',
  1527. 'letter-spacing' => 'normal',
  1528. 'lighting-color' => 'white',
  1529. 'marker' => '',
  1530. 'marker-end' => 'none',
  1531. 'marker-mid' => 'none',
  1532. 'marker-start' => 'none',
  1533. 'mask' => 'none',
  1534. 'opacity' => 1,
  1535. 'overflow' => 'auto',
  1536. 'pointer-events' => 'visiblePainted',
  1537. 'shape-rendering' => 'auto',
  1538. 'stop-color' => 'black',
  1539. 'stop-opacity' => 1,
  1540. 'stroke' => 'none',
  1541. 'stroke-dasharray' => 'none',
  1542. 'stroke-dashoffset' => 0,
  1543. 'stroke-linecap' => 'butt',
  1544. 'stroke-linejoin' => 'miter',
  1545. 'stroke-miterlimit' => 4,
  1546. 'stroke-opacity' => 1,
  1547. 'stroke-width' => 1,
  1548. 'text-anchor' => 'start',
  1549. 'text-decoration' => 'none',
  1550. 'text-rendering' => 'auto',
  1551. 'unicode-bidi' => 'normal',
  1552. 'visibility' => 'visible',
  1553. 'word-spacing' => 'normal',
  1554. 'writing-mode' => 'lr-tb',
  1555. 'text-color' => 'black',
  1556. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1557. ));
  1558. /**
  1559. * If true force sRGB color profile for all document.
  1560. * @protected
  1561. * @since 5.9.121 (2011-09-28)
  1562. */
  1563. protected $force_srgb = false;
  1564. /**
  1565. * If true set the document to PDF/A mode.
  1566. * @protected
  1567. * @since 5.9.121 (2011-09-27)
  1568. */
  1569. protected $pdfa_mode = false;
  1570. /**
  1571. * Document creation date
  1572. * @protected
  1573. * @since 5.9.121 (2011-09-28)
  1574. */
  1575. protected $doc_date;
  1576. /**
  1577. * Custom XMP data.
  1578. * @protected
  1579. * @since 5.9.128 (2011-10-06)
  1580. */
  1581. protected $custom_xmp = '';
  1582. //------------------------------------------------------------
  1583. // METHODS
  1584. //------------------------------------------------------------
  1585. /**
  1586. * This is the class constructor.
  1587. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1588. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1589. * @param $unit (string) User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1590. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  1591. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1592. * @param $encoding (string) Charset encoding; default is UTF-8.
  1593. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1594. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1595. * @public
  1596. * @see getPageSizeFromFormat(), setPageFormat()
  1597. */
  1598. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1599. /* Set internal character encoding to ASCII */
  1600. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1601. $this->internal_encoding = mb_internal_encoding();
  1602. mb_internal_encoding('ASCII');
  1603. }
  1604. // get array of HTML colors
  1605. require(dirname(__FILE__).'/htmlcolors.php');
  1606. $this->webcolor = $webcolor;
  1607. // get array of custom spot colors
  1608. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1609. require(dirname(__FILE__).'/spotcolors.php');
  1610. $this->spotcolor = $spotcolor;
  1611. } else {
  1612. $this->spotcolor = array();
  1613. }
  1614. require_once(dirname(__FILE__).'/unicode_data.php');
  1615. $this->unicode = new TCPDF_UNICODE_DATA();
  1616. require_once(dirname(__FILE__).'/encodings_maps.php');
  1617. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1618. $this->font_obj_ids = array();
  1619. $this->page_obj_id = array();
  1620. $this->form_obj_id = array();
  1621. // set pdf/a mode
  1622. $this->pdfa_mode = $pdfa;
  1623. $this->force_srgb = false;
  1624. // set disk caching
  1625. $this->diskcache = $diskcache ? true : false;
  1626. // set language direction
  1627. $this->rtl = false;
  1628. $this->tmprtl = false;
  1629. // some checks
  1630. $this->_dochecks();
  1631. // initialization of properties
  1632. $this->isunicode = $unicode;
  1633. $this->page = 0;
  1634. $this->transfmrk[0] = array();
  1635. $this->pagedim = array();
  1636. $this->n = 2;
  1637. $this->buffer = '';
  1638. $this->pages = array();
  1639. $this->state = 0;
  1640. $this->fonts = array();
  1641. $this->FontFiles = array();
  1642. $this->diffs = array();
  1643. $this->images = array();
  1644. $this->links = array();
  1645. $this->gradients = array();
  1646. $this->InFooter = false;
  1647. $this->lasth = 0;
  1648. $this->FontFamily = 'helvetica';
  1649. $this->FontStyle = '';
  1650. $this->FontSizePt = 12;
  1651. $this->underline = false;
  1652. $this->overline = false;
  1653. $this->linethrough = false;
  1654. $this->DrawColor = '0 G';
  1655. $this->FillColor = '0 g';
  1656. $this->TextColor = '0 g';
  1657. $this->ColorFlag = false;
  1658. $this->pdflayers = array();
  1659. // encryption values
  1660. $this->encrypted = false;
  1661. $this->last_enc_key = '';
  1662. // standard Unicode fonts
  1663. $this->CoreFonts = array(
  1664. 'courier'=>'Courier',
  1665. 'courierB'=>'Courier-Bold',
  1666. 'courierI'=>'Courier-Oblique',
  1667. 'courierBI'=>'Courier-BoldOblique',
  1668. 'helvetica'=>'Helvetica',
  1669. 'helveticaB'=>'Helvetica-Bold',
  1670. 'helveticaI'=>'Helvetica-Oblique',
  1671. 'helveticaBI'=>'Helvetica-BoldOblique',
  1672. 'times'=>'Times-Roman',
  1673. 'timesB'=>'Times-Bold',
  1674. 'timesI'=>'Times-Italic',
  1675. 'timesBI'=>'Times-BoldItalic',
  1676. 'symbol'=>'Symbol',
  1677. 'zapfdingbats'=>'ZapfDingbats'
  1678. );
  1679. // set scale factor
  1680. $this->setPageUnit($unit);
  1681. // set page format and orientation
  1682. $this->setPageFormat($format, $orientation);
  1683. // page margins (1 cm)
  1684. $margin = 28.35 / $this->k;
  1685. $this->SetMargins($margin, $margin);
  1686. // internal cell padding
  1687. $cpadding = $margin / 10;
  1688. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1689. // cell margins
  1690. $this->setCellMargins(0, 0, 0, 0);
  1691. // line width (0.2 mm)
  1692. $this->LineWidth = 0.57 / $this->k;
  1693. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1694. $this->linestyleCap = '0 J';
  1695. $this->linestyleJoin = '0 j';
  1696. $this->linestyleDash = '[] 0 d';
  1697. // automatic page break
  1698. $this->SetAutoPageBreak(true, (2 * $margin));
  1699. // full width display mode
  1700. $this->SetDisplayMode('fullwidth');
  1701. // compression
  1702. $this->SetCompression();
  1703. // set default PDF version number
  1704. $this->setPDFVersion();
  1705. $this->encoding = $encoding;
  1706. $this->HREF = array();
  1707. $this->getFontsList();
  1708. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1709. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1710. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1711. $this->extgstates = array();
  1712. // user's rights
  1713. $this->sign = false;
  1714. $this->ur['enabled'] = false;
  1715. $this->ur['document'] = '/FullSave';
  1716. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1717. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1718. $this->ur['signature'] = '/Modify';
  1719. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1720. $this->ur['formex'] = '';
  1721. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1722. $this->empty_signature_appearance = array();
  1723. // set default JPEG quality
  1724. $this->jpeg_quality = 75;
  1725. // initialize some settings
  1726. $this->utf8Bidi(array(''), '');
  1727. // set default font
  1728. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1729. // check if PCRE Unicode support is enabled
  1730. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1731. // PCRE unicode support is turned ON
  1732. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1733. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1734. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1735. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1736. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1737. } else {
  1738. // PCRE unicode support is turned OFF
  1739. $this->setSpacesRE('/[^\S\xa0]/');
  1740. }
  1741. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1742. // set file ID for trailer
  1743. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1744. // set document date
  1745. $this->doc_date = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  1746. // get default graphic vars
  1747. $this->default_graphic_vars = $this->getGraphicVars();
  1748. $this->header_xobj_autoreset = false;
  1749. $this->custom_xmp = '';
  1750. }
  1751. /**
  1752. * Default destructor.
  1753. * @public
  1754. * @since 1.53.0.TC016
  1755. */
  1756. public function __destruct() {
  1757. // restore internal encoding
  1758. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1759. mb_internal_encoding($this->internal_encoding);
  1760. }
  1761. // unset all class variables
  1762. $this->_destroy(true);
  1763. }
  1764. /**
  1765. * Return the current TCPDF version.
  1766. * @return TCPDF version string
  1767. * @public
  1768. * @since 5.9.012 (2010-11-10)
  1769. */
  1770. public function getTCPDFVersion() {
  1771. return $this->tcpdf_version;
  1772. }
  1773. /**
  1774. * Set the units of measure for the document.
  1775. * @param $unit (string) User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1776. * @public
  1777. * @since 3.0.015 (2008-06-06)
  1778. */
  1779. public function setPageUnit($unit) {
  1780. $unit = strtolower($unit);
  1781. //Set scale factor
  1782. switch ($unit) {
  1783. // points
  1784. case 'px':
  1785. case 'pt': {
  1786. $this->k = 1;
  1787. break;
  1788. }
  1789. // millimeters
  1790. case 'mm': {
  1791. $this->k = $this->dpi / 25.4;
  1792. break;
  1793. }
  1794. // centimeters
  1795. case 'cm': {
  1796. $this->k = $this->dpi / 2.54;
  1797. break;
  1798. }
  1799. // inches
  1800. case 'in': {
  1801. $this->k = $this->dpi;
  1802. break;
  1803. }
  1804. // unsupported unit
  1805. default : {
  1806. $this->Error('Incorrect unit: '.$unit);
  1807. break;
  1808. }
  1809. }
  1810. $this->pdfunit = $unit;
  1811. if (isset($this->CurOrientation)) {
  1812. $this->setPageOrientation($this->CurOrientation);
  1813. }
  1814. }
  1815. /**
  1816. * Get page dimensions from format name.
  1817. * @param $format (mixed) The format name. It can be: <ul>
  1818. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1819. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1820. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1821. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1822. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1823. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1824. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1825. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1826. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1827. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1828. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1829. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1830. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1831. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1832. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1833. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1834. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1835. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1836. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1837. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1838. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1839. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1840. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1841. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1842. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1843. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1844. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1845. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1846. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1847. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1848. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1849. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1850. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1851. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1852. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1853. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1854. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1855. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1856. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1857. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1858. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1859. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1860. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1861. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1862. * <li><b>SIS 014711 E Series</b></li>
  1863. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1864. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1865. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1866. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1867. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1868. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1869. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1870. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1871. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1872. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1873. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1874. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1875. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1876. * <li><b>SIS 014711 G Series</b></li>
  1877. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1878. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1879. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1880. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1881. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1882. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1883. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1884. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1885. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1886. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1887. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1888. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1889. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1890. * <li><b>ISO Press</b></li>
  1891. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1892. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1893. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1894. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1895. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1896. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1897. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1898. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1899. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1900. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1901. * <li><b>German DIN 476</b></li>
  1902. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1903. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1904. * <li><b>Variations on the ISO Standard</b></li>
  1905. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1906. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1907. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1908. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1909. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1910. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1911. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1912. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1913. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1914. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1915. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1916. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1917. * <li><b>ANSI Series</b></li>
  1918. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1919. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1920. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1921. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1922. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1923. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1924. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1925. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1926. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1927. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1928. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1929. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1930. * <li><b>Other North American Paper Sizes</b></li>
  1931. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1932. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1933. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1934. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1935. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1936. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1937. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1938. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1939. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1940. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1941. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1942. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1943. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1944. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1945. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1946. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1947. * <li><b>North American Architectural Sizes</b></li>
  1948. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1949. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1950. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1951. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1952. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1953. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1954. * <li><b>Announcement Envelopes</b></li>
  1955. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1956. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1957. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1958. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  1959. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  1960. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  1961. * <li><b>Commercial Envelopes</b></li>
  1962. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  1963. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  1964. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  1965. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  1966. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  1967. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  1968. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  1969. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  1970. * <li><b>Catalogue Envelopes</b></li>
  1971. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  1972. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  1973. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  1974. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  1975. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  1976. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  1977. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  1978. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  1979. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  1980. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  1981. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  1982. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  1983. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  1984. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  1985. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  1986. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  1987. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  1988. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  1989. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  1990. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  1991. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  1992. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  1993. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  1994. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  1995. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  1996. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  1997. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  1998. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  1999. * <li><b>PA Series</b></li>
  2000. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2001. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2002. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2003. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2004. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2005. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2006. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2007. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2008. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2009. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2010. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2011. * <li><b>Standard Photographic Print Sizes</b></li>
  2012. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2013. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2014. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2015. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2016. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2017. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2018. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2019. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2020. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2021. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2022. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2023. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2024. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2025. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2026. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2027. * <li><b>Common Newspaper Sizes</b></li>
  2028. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2029. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2030. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2031. * <li><b>Business Cards</b></li>
  2032. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2033. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2034. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2035. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2036. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2037. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2038. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2039. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2040. * <li><b>Billboards</b></li>
  2041. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2042. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2043. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2044. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2045. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2046. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2047. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2048. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2049. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2050. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2051. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2052. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2053. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2054. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2055. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2056. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2057. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2058. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2059. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2060. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2061. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2062. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2063. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2064. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2065. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2066. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2067. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2068. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2069. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2070. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2071. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2072. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2073. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2074. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2075. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2076. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2077. * <li><b>Old Imperial Belgian</b></li>
  2078. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2079. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2080. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2081. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2082. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2083. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2084. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2085. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2086. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2087. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2088. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2089. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2090. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2091. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2092. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2093. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2094. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2095. * <li><b>Old Imperial French</b></li>
  2096. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2097. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2098. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2099. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2100. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2101. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2102. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2103. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2104. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2105. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2106. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2107. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2108. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2109. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2110. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2111. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2112. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2113. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2114. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2115. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2116. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2117. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2118. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2119. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2120. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2121. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2122. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2123. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2124. * </ul>
  2125. * @return array containing page width and height in points
  2126. * @public
  2127. * @since 5.0.010 (2010-05-17)
  2128. */
  2129. public function getPageSizeFromFormat($format) {
  2130. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2131. switch (strtoupper($format)) {
  2132. // ISO 216 A Series + 2 SIS 014711 extensions
  2133. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2134. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2135. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2136. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2137. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2138. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2139. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2140. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2141. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2142. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2143. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2144. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2145. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2146. // ISO 216 B Series + 2 SIS 014711 extensions
  2147. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2148. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2149. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2150. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2151. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2152. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2153. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2154. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2155. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2156. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2157. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2158. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2159. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2160. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2161. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2162. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2163. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2164. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2165. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2166. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2167. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2168. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2169. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2170. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2171. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2172. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2173. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2174. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2175. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2176. // SIS 014711 E Series
  2177. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2178. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2179. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2180. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2181. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2182. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2183. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2184. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2185. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2186. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2187. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2188. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2189. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2190. // SIS 014711 G Series
  2191. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2192. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2193. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2194. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2195. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2196. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2197. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2198. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2199. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2200. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2201. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2202. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2203. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2204. // ISO Press
  2205. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2206. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2207. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2208. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2209. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2210. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2211. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2212. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2213. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2214. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2215. // German DIN 476
  2216. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2217. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2218. // Variations on the ISO Standard
  2219. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2220. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2221. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2222. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2223. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2224. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2225. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2226. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2227. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2228. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2229. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2230. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2231. // ANSI Series
  2232. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2233. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2234. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2235. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2236. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2237. // Traditional 'Loose' North American Paper Sizes
  2238. case 'USLEDGER':
  2239. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2240. case 'ORGANIZERK':
  2241. case 'BIBLE':
  2242. case 'USTABLOID':
  2243. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2244. case 'ORGANIZERM':
  2245. case 'USLETTER':
  2246. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2247. case 'USLEGAL':
  2248. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2249. case 'GOVERNMENTLETTER':
  2250. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2251. case 'JUNIORLEGAL':
  2252. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2253. // Other North American Paper Sizes
  2254. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2255. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2256. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2257. case 'GOVERNMENTLEGAL':
  2258. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2259. case 'MONARCH':
  2260. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2261. case 'ORGANIZERL':
  2262. case 'STATEMENT':
  2263. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2264. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2265. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2266. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2267. // Canadian standard CAN 2-9.60M
  2268. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2269. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2270. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2271. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2272. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2273. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2274. // North American Architectural Sizes
  2275. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2276. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2277. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2278. case 'BROADSHEET':
  2279. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2280. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2281. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2282. // --- North American Envelope Sizes ---
  2283. // - Announcement Envelopes
  2284. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2285. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2286. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2287. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2288. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2289. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2290. // - Commercial Envelopes
  2291. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2292. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2293. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2294. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2295. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2296. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2297. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2298. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2299. // - Catalogue Envelopes
  2300. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2301. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2302. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2303. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2304. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2305. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2306. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2307. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2308. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2309. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2310. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2311. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2312. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2313. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2314. // Japanese (JIS P 0138-61) Standard B-Series
  2315. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2316. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2317. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2318. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2319. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2320. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2321. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2322. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2323. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2324. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2325. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2326. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2327. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2328. // PA Series
  2329. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2330. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2331. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2332. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2333. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2334. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2335. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2336. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2337. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2338. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2339. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2340. // Standard Photographic Print Sizes
  2341. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2342. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2343. case 'L':
  2344. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2345. case 'KG':
  2346. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2347. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2348. case '2L':
  2349. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2350. case '8P':
  2351. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2352. case '6P':
  2353. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2354. case '6PW':
  2355. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2356. case '4P':
  2357. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2358. case '4PW':
  2359. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2360. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2361. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2362. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2363. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2364. // Common Newspaper Sizes
  2365. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2366. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2367. case 'NEWSPAPER_TABLOID':
  2368. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2369. // Business Cards
  2370. case 'CREDIT_CARD':
  2371. case 'BUSINESS_CARD':
  2372. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2373. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2374. case 'BUSINESS_CARD_IT':
  2375. case 'BUSINESS_CARD_UK':
  2376. case 'BUSINESS_CARD_FR':
  2377. case 'BUSINESS_CARD_DE':
  2378. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2379. case 'BUSINESS_CARD_CA':
  2380. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2381. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2382. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2383. case 'BUSINESS_CARD_AU':
  2384. case 'BUSINESS_CARD_DK':
  2385. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2386. case 'BUSINESS_CARD_RU':
  2387. case 'BUSINESS_CARD_CZ':
  2388. case 'BUSINESS_CARD_FI':
  2389. case 'BUSINESS_CARD_HU':
  2390. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2391. // Billboards
  2392. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2393. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2394. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2395. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2396. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2397. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2398. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2399. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2400. // Old European Sizes
  2401. // - Old Imperial English Sizes
  2402. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2403. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2404. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2405. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2406. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2407. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2408. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2409. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2410. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2411. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2412. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2413. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2414. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2415. case 'EN_SHEET':
  2416. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2417. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2418. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2419. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2420. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2421. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2422. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2423. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2424. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2425. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2426. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2427. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2428. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2429. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2430. // - Old Imperial Belgian Sizes
  2431. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2432. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2433. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2434. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2435. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2436. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2437. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2438. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2439. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2440. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2441. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2442. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2443. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2444. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2445. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2446. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2447. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2448. // - Old Imperial French Sizes
  2449. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2450. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2451. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2452. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2453. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2454. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2455. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2456. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2457. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2458. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2459. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2460. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2461. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2462. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2463. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2464. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2465. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2466. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2467. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2468. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2469. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2470. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2471. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2472. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2473. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2474. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2475. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2476. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2477. // DEFAULT ISO A4
  2478. default: {$pf = array( 595.276, 841.890); break;}
  2479. }
  2480. return $pf;
  2481. }
  2482. /**
  2483. * Change the format of the current page
  2484. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numners (width, height) or an array containing the following measures and options:<ul>
  2485. * <li>['format'] = page format name (one of the above);</li>
  2486. * <li>['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li>
  2487. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2488. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2489. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2490. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2491. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2492. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2493. * <li>['CropBox'] : the visible region of default user space:</li>
  2494. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2495. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2496. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2497. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2498. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2499. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2500. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2501. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2502. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2503. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2504. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2505. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2506. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2507. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2508. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2509. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2510. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2511. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2512. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2513. * <li>['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:</li>
  2514. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2515. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2516. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2517. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2518. * <li>['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation</li>
  2519. * <li>['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li>
  2520. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2521. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2522. * <li>['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li>
  2523. * <li>['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li>
  2524. * <li>['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li>
  2525. * <li>['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.</li>
  2526. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2527. * </ul>
  2528. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2529. * <li>P or Portrait (default)</li>
  2530. * <li>L or Landscape</li>
  2531. * <li>'' (empty string) for automatic orientation</li>
  2532. * </ul>
  2533. * @protected
  2534. * @since 3.0.015 (2008-06-06)
  2535. * @see getPageSizeFromFormat()
  2536. */
  2537. protected function setPageFormat($format, $orientation='P') {
  2538. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2539. // remove inherited values
  2540. unset($this->pagedim[$this->page]);
  2541. }
  2542. if (is_string($format)) {
  2543. // get page measures from format name
  2544. $pf = $this->getPageSizeFromFormat($format);
  2545. $this->fwPt = $pf[0];
  2546. $this->fhPt = $pf[1];
  2547. } else {
  2548. // the boundaries of the physical medium on which the page shall be displayed or printed
  2549. if (isset($format['MediaBox'])) {
  2550. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2551. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2552. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2553. } else {
  2554. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2555. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2556. } else {
  2557. if (!isset($format['format'])) {
  2558. // default value
  2559. $format['format'] = 'A4';
  2560. }
  2561. $pf = $this->getPageSizeFromFormat($format['format']);
  2562. }
  2563. $this->fwPt = $pf[0];
  2564. $this->fhPt = $pf[1];
  2565. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2566. }
  2567. // the visible region of default user space
  2568. if (isset($format['CropBox'])) {
  2569. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2570. }
  2571. // the region to which the contents of the page shall be clipped when output in a production environment
  2572. if (isset($format['BleedBox'])) {
  2573. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2574. }
  2575. // the intended dimensions of the finished page after trimming
  2576. if (isset($format['TrimBox'])) {
  2577. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2578. }
  2579. // the page's meaningful content (including potential white space)
  2580. if (isset($format['ArtBox'])) {
  2581. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2582. }
  2583. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2584. if (isset($format['BoxColorInfo'])) {
  2585. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2586. }
  2587. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2588. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2589. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2590. }
  2591. if (isset($format['PZ'])) {
  2592. // The page's preferred zoom (magnification) factor
  2593. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2594. }
  2595. if (isset($format['trans'])) {
  2596. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2597. if (isset($format['trans']['Dur'])) {
  2598. // The page's display duration
  2599. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2600. }
  2601. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2602. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2603. // The transition style that shall be used when moving to this page from another during a presentation
  2604. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2605. $valid_effect = array('Split', 'Blinds');
  2606. $valid_vals = array('H', 'V');
  2607. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2608. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2609. }
  2610. $valid_effect = array('Split', 'Box', 'Fly');
  2611. $valid_vals = array('I', 'O');
  2612. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2613. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2614. }
  2615. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2616. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2617. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2618. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2619. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2620. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2621. }
  2622. }
  2623. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2624. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2625. }
  2626. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2627. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2628. }
  2629. } else {
  2630. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2631. }
  2632. if (isset($format['trans']['D'])) {
  2633. // The duration of the transition effect, in seconds
  2634. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2635. } else {
  2636. $this->pagedim[$this->page]['trans']['D'] = 1;
  2637. }
  2638. }
  2639. }
  2640. $this->setPageOrientation($orientation);
  2641. }
  2642. /**
  2643. * Set page boundaries.
  2644. * @param $page (int) page number
  2645. * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
  2646. * @param $llx (float) lower-left x coordinate in user units
  2647. * @param $lly (float) lower-left y coordinate in user units
  2648. * @param $urx (float) upper-right x coordinate in user units
  2649. * @param $ury (float) upper-right y coordinate in user units
  2650. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2651. * @public
  2652. * @since 5.0.010 (2010-05-17)
  2653. */
  2654. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2655. if (!isset($this->pagedim[$page])) {
  2656. // initialize array
  2657. $this->pagedim[$page] = array();
  2658. }
  2659. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2660. if (!in_array($type, $pageboxes)) {
  2661. return;
  2662. }
  2663. if ($points) {
  2664. $k = 1;
  2665. } else {
  2666. $k = $this->k;
  2667. }
  2668. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2669. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2670. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2671. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2672. }
  2673. /**
  2674. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2675. * @param $page (int) page number
  2676. * @protected
  2677. * @since 5.0.010 (2010-05-17)
  2678. */
  2679. protected function swapPageBoxCoordinates($page) {
  2680. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2681. foreach ($pageboxes as $type) {
  2682. // swap X and Y coordinates
  2683. if (isset($this->pagedim[$page][$type])) {
  2684. $tmp = $this->pagedim[$page][$type]['llx'];
  2685. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2686. $this->pagedim[$page][$type]['lly'] = $tmp;
  2687. $tmp = $this->pagedim[$page][$type]['urx'];
  2688. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2689. $this->pagedim[$page][$type]['ury'] = $tmp;
  2690. }
  2691. }
  2692. }
  2693. /**
  2694. * Set page orientation.
  2695. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  2696. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2697. * @param $bottommargin (float) bottom margin of the page.
  2698. * @public
  2699. * @since 3.0.015 (2008-06-06)
  2700. */
  2701. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2702. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2703. // the boundaries of the physical medium on which the page shall be displayed or printed
  2704. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2705. }
  2706. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2707. // the visible region of default user space
  2708. $this->setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true);
  2709. }
  2710. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2711. // the region to which the contents of the page shall be clipped when output in a production environment
  2712. $this->setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  2713. }
  2714. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2715. // the intended dimensions of the finished page after trimming
  2716. $this->setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  2717. }
  2718. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2719. // the page's meaningful content (including potential white space)
  2720. $this->setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  2721. }
  2722. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2723. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2724. $this->pagedim[$this->page]['Rotate'] = 0;
  2725. }
  2726. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2727. // The page's preferred zoom (magnification) factor
  2728. $this->pagedim[$this->page]['PZ'] = 1;
  2729. }
  2730. if ($this->fwPt > $this->fhPt) {
  2731. // landscape
  2732. $default_orientation = 'L';
  2733. } else {
  2734. // portrait
  2735. $default_orientation = 'P';
  2736. }
  2737. $valid_orientations = array('P', 'L');
  2738. if (empty($orientation)) {
  2739. $orientation = $default_orientation;
  2740. } else {
  2741. $orientation = strtoupper($orientation{0});
  2742. }
  2743. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2744. $this->CurOrientation = $orientation;
  2745. $this->wPt = $this->fhPt;
  2746. $this->hPt = $this->fwPt;
  2747. } else {
  2748. $this->CurOrientation = $default_orientation;
  2749. $this->wPt = $this->fwPt;
  2750. $this->hPt = $this->fhPt;
  2751. }
  2752. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2753. // swap X and Y coordinates (change page orientation)
  2754. $this->swapPageBoxCoordinates($this->page);
  2755. }
  2756. $this->w = $this->wPt / $this->k;
  2757. $this->h = $this->hPt / $this->k;
  2758. if ($this->empty_string($autopagebreak)) {
  2759. if (isset($this->AutoPageBreak)) {
  2760. $autopagebreak = $this->AutoPageBreak;
  2761. } else {
  2762. $autopagebreak = true;
  2763. }
  2764. }
  2765. if ($this->empty_string($bottommargin)) {
  2766. if (isset($this->bMargin)) {
  2767. $bottommargin = $this->bMargin;
  2768. } else {
  2769. // default value = 2 cm
  2770. $bottommargin = 2 * 28.35 / $this->k;
  2771. }
  2772. }
  2773. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2774. // store page dimensions
  2775. $this->pagedim[$this->page]['w'] = $this->wPt;
  2776. $this->pagedim[$this->page]['h'] = $this->hPt;
  2777. $this->pagedim[$this->page]['wk'] = $this->w;
  2778. $this->pagedim[$this->page]['hk'] = $this->h;
  2779. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2780. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2781. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2782. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2783. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2784. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2785. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2786. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2787. }
  2788. /**
  2789. * Set regular expression to detect withespaces or word separators.
  2790. * The pattern delimiter must be the forward-slash character "/".
  2791. * Some example patterns are:
  2792. * <pre>
  2793. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2794. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2795. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2796. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2797. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2798. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2799. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2800. * </pre>
  2801. * @param $re (string) regular expression (leave empty for default).
  2802. * @public
  2803. * @since 4.6.016 (2009-06-15)
  2804. */
  2805. public function setSpacesRE($re='/[^\S\xa0]/') {
  2806. $this->re_spaces = $re;
  2807. $re_parts = explode('/', $re);
  2808. // get pattern parts
  2809. $this->re_space = array();
  2810. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2811. $this->re_space['p'] = $re_parts[1];
  2812. } else {
  2813. $this->re_space['p'] = '[\s]';
  2814. }
  2815. // set pattern modifiers
  2816. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2817. $this->re_space['m'] = $re_parts[2];
  2818. } else {
  2819. $this->re_space['m'] = '';
  2820. }
  2821. }
  2822. /**
  2823. * Enable or disable Right-To-Left language mode
  2824. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2825. * @param $resetx (Boolean) if true reset the X position on direction change.
  2826. * @public
  2827. * @since 2.0.000 (2008-01-03)
  2828. */
  2829. public function setRTL($enable, $resetx=true) {
  2830. $enable = $enable ? true : false;
  2831. $resetx = ($resetx AND ($enable != $this->rtl));
  2832. $this->rtl = $enable;
  2833. $this->tmprtl = false;
  2834. if ($resetx) {
  2835. $this->Ln(0);
  2836. }
  2837. }
  2838. /**
  2839. * Return the RTL status
  2840. * @return boolean
  2841. * @public
  2842. * @since 4.0.012 (2008-07-24)
  2843. */
  2844. public function getRTL() {
  2845. return $this->rtl;
  2846. }
  2847. /**
  2848. * Force temporary RTL language direction
  2849. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2850. * @public
  2851. * @since 2.1.000 (2008-01-09)
  2852. */
  2853. public function setTempRTL($mode) {
  2854. $newmode = false;
  2855. switch (strtoupper($mode)) {
  2856. case 'LTR':
  2857. case 'L': {
  2858. if ($this->rtl) {
  2859. $newmode = 'L';
  2860. }
  2861. break;
  2862. }
  2863. case 'RTL':
  2864. case 'R': {
  2865. if (!$this->rtl) {
  2866. $newmode = 'R';
  2867. }
  2868. break;
  2869. }
  2870. case false:
  2871. default: {
  2872. $newmode = false;
  2873. break;
  2874. }
  2875. }
  2876. $this->tmprtl = $newmode;
  2877. }
  2878. /**
  2879. * Return the current temporary RTL status
  2880. * @return boolean
  2881. * @public
  2882. * @since 4.8.014 (2009-11-04)
  2883. */
  2884. public function isRTLTextDir() {
  2885. return ($this->rtl OR ($this->tmprtl == 'R'));
  2886. }
  2887. /**
  2888. * Set the last cell height.
  2889. * @param $h (float) cell height.
  2890. * @author Nicola Asuni
  2891. * @public
  2892. * @since 1.53.0.TC034
  2893. */
  2894. public function setLastH($h) {
  2895. $this->lasth = $h;
  2896. }
  2897. /**
  2898. * Reset the last cell height.
  2899. * @public
  2900. * @since 5.9.000 (2010-10-03)
  2901. */
  2902. public function resetLastH() {
  2903. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2904. }
  2905. /**
  2906. * Get the last cell height.
  2907. * @return last cell height
  2908. * @public
  2909. * @since 4.0.017 (2008-08-05)
  2910. */
  2911. public function getLastH() {
  2912. return $this->lasth;
  2913. }
  2914. /**
  2915. * Set the adjusting factor to convert pixels to user units.
  2916. * @param $scale (float) adjusting factor to convert pixels to user units.
  2917. * @author Nicola Asuni
  2918. * @public
  2919. * @since 1.5.2
  2920. */
  2921. public function setImageScale($scale) {
  2922. $this->imgscale = $scale;
  2923. }
  2924. /**
  2925. * Returns the adjusting factor to convert pixels to user units.
  2926. * @return float adjusting factor to convert pixels to user units.
  2927. * @author Nicola Asuni
  2928. * @public
  2929. * @since 1.5.2
  2930. */
  2931. public function getImageScale() {
  2932. return $this->imgscale;
  2933. }
  2934. /**
  2935. * Returns an array of page dimensions:
  2936. * <ul><li>$this->pagedim[$this->page]['w'] = page width in points</li><li>$this->pagedim[$this->page]['h'] = height in points</li><li>$this->pagedim[$this->page]['wk'] = page width in user units</li><li>$this->pagedim[$this->page]['hk'] = page height in user units</li><li>$this->pagedim[$this->page]['tm'] = top margin</li><li>$this->pagedim[$this->page]['bm'] = bottom margin</li><li>$this->pagedim[$this->page]['lm'] = left margin</li><li>$this->pagedim[$this->page]['rm'] = right margin</li><li>$this->pagedim[$this->page]['pb'] = auto page break</li><li>$this->pagedim[$this->page]['or'] = page orientation</li><li>$this->pagedim[$this->page]['olm'] = original left margin</li><li>$this->pagedim[$this->page]['orm'] = original right margin</li><li>$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li><li>$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.</li><li>$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation<ul><li>$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li><li>$this->pagedim[$this->page]['trans']['S'] = transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li><li>$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.</li><li>$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li><li>$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li><li>$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li><li>$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0. </li><li>$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li></ul></li><li>$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed<ul><li>$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['CropBox'] : the visible region of default user space<ul><li>$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment<ul><li>$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming<ul><li>$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content<ul><li>$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points</li></ul></li></ul>
  2937. * @param $pagenum (int) page number (empty = current page)
  2938. * @return array of page dimensions.
  2939. * @author Nicola Asuni
  2940. * @public
  2941. * @since 4.5.027 (2009-03-16)
  2942. */
  2943. public function getPageDimensions($pagenum='') {
  2944. if (empty($pagenum)) {
  2945. $pagenum = $this->page;
  2946. }
  2947. return $this->pagedim[$pagenum];
  2948. }
  2949. /**
  2950. * Returns the page width in units.
  2951. * @param $pagenum (int) page number (empty = current page)
  2952. * @return int page width.
  2953. * @author Nicola Asuni
  2954. * @public
  2955. * @since 1.5.2
  2956. * @see getPageDimensions()
  2957. */
  2958. public function getPageWidth($pagenum='') {
  2959. if (empty($pagenum)) {
  2960. return $this->w;
  2961. }
  2962. return $this->pagedim[$pagenum]['w'];
  2963. }
  2964. /**
  2965. * Returns the page height in units.
  2966. * @param $pagenum (int) page number (empty = current page)
  2967. * @return int page height.
  2968. * @author Nicola Asuni
  2969. * @public
  2970. * @since 1.5.2
  2971. * @see getPageDimensions()
  2972. */
  2973. public function getPageHeight($pagenum='') {
  2974. if (empty($pagenum)) {
  2975. return $this->h;
  2976. }
  2977. return $this->pagedim[$pagenum]['h'];
  2978. }
  2979. /**
  2980. * Returns the page break margin.
  2981. * @param $pagenum (int) page number (empty = current page)
  2982. * @return int page break margin.
  2983. * @author Nicola Asuni
  2984. * @public
  2985. * @since 1.5.2
  2986. * @see getPageDimensions()
  2987. */
  2988. public function getBreakMargin($pagenum='') {
  2989. if (empty($pagenum)) {
  2990. return $this->bMargin;
  2991. }
  2992. return $this->pagedim[$pagenum]['bm'];
  2993. }
  2994. /**
  2995. * Returns the scale factor (number of points in user unit).
  2996. * @return int scale factor.
  2997. * @author Nicola Asuni
  2998. * @public
  2999. * @since 1.5.2
  3000. */
  3001. public function getScaleFactor() {
  3002. return $this->k;
  3003. }
  3004. /**
  3005. * Defines the left, top and right margins.
  3006. * @param $left (float) Left margin.
  3007. * @param $top (float) Top margin.
  3008. * @param $right (float) Right margin. Default value is the left one.
  3009. * @param $keepmargins (boolean) if true overwrites the default page margins
  3010. * @public
  3011. * @since 1.0
  3012. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3013. */
  3014. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3015. //Set left, top and right margins
  3016. $this->lMargin = $left;
  3017. $this->tMargin = $top;
  3018. if ($right == -1) {
  3019. $right = $left;
  3020. }
  3021. $this->rMargin = $right;
  3022. if ($keepmargins) {
  3023. // overwrite original values
  3024. $this->original_lMargin = $this->lMargin;
  3025. $this->original_rMargin = $this->rMargin;
  3026. }
  3027. }
  3028. /**
  3029. * Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
  3030. * @param $margin (float) The margin.
  3031. * @public
  3032. * @since 1.4
  3033. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3034. */
  3035. public function SetLeftMargin($margin) {
  3036. //Set left margin
  3037. $this->lMargin = $margin;
  3038. if (($this->page > 0) AND ($this->x < $margin)) {
  3039. $this->x = $margin;
  3040. }
  3041. }
  3042. /**
  3043. * Defines the top margin. The method can be called before creating the first page.
  3044. * @param $margin (float) The margin.
  3045. * @public
  3046. * @since 1.5
  3047. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3048. */
  3049. public function SetTopMargin($margin) {
  3050. //Set top margin
  3051. $this->tMargin = $margin;
  3052. if (($this->page > 0) AND ($this->y < $margin)) {
  3053. $this->y = $margin;
  3054. }
  3055. }
  3056. /**
  3057. * Defines the right margin. The method can be called before creating the first page.
  3058. * @param $margin (float) The margin.
  3059. * @public
  3060. * @since 1.5
  3061. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3062. */
  3063. public function SetRightMargin($margin) {
  3064. $this->rMargin = $margin;
  3065. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3066. $this->x = $this->w - $margin;
  3067. }
  3068. }
  3069. /**
  3070. * Set the same internal Cell padding for top, right, bottom, left-
  3071. * @param $pad (float) internal padding.
  3072. * @public
  3073. * @since 2.1.000 (2008-01-09)
  3074. * @see getCellPaddings(), setCellPaddings()
  3075. */
  3076. public function SetCellPadding($pad) {
  3077. if ($pad >= 0) {
  3078. $this->cell_padding['L'] = $pad;
  3079. $this->cell_padding['T'] = $pad;
  3080. $this->cell_padding['R'] = $pad;
  3081. $this->cell_padding['B'] = $pad;
  3082. }
  3083. }
  3084. /**
  3085. * Set the internal Cell paddings.
  3086. * @param $left (float) left padding
  3087. * @param $top (float) top padding
  3088. * @param $right (float) right padding
  3089. * @param $bottom (float) bottom padding
  3090. * @public
  3091. * @since 5.9.000 (2010-10-03)
  3092. * @see getCellPaddings(), SetCellPadding()
  3093. */
  3094. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3095. if (($left !== '') AND ($left >= 0)) {
  3096. $this->cell_padding['L'] = $left;
  3097. }
  3098. if (($top !== '') AND ($top >= 0)) {
  3099. $this->cell_padding['T'] = $top;
  3100. }
  3101. if (($right !== '') AND ($right >= 0)) {
  3102. $this->cell_padding['R'] = $right;
  3103. }
  3104. if (($bottom !== '') AND ($bottom >= 0)) {
  3105. $this->cell_padding['B'] = $bottom;
  3106. }
  3107. }
  3108. /**
  3109. * Get the internal Cell padding array.
  3110. * @return array of padding values
  3111. * @public
  3112. * @since 5.9.000 (2010-10-03)
  3113. * @see setCellPaddings(), SetCellPadding()
  3114. */
  3115. public function getCellPaddings() {
  3116. return $this->cell_padding;
  3117. }
  3118. /**
  3119. * Set the internal Cell margins.
  3120. * @param $left (float) left margin
  3121. * @param $top (float) top margin
  3122. * @param $right (float) right margin
  3123. * @param $bottom (float) bottom margin
  3124. * @public
  3125. * @since 5.9.000 (2010-10-03)
  3126. * @see getCellMargins()
  3127. */
  3128. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3129. if (($left !== '') AND ($left >= 0)) {
  3130. $this->cell_margin['L'] = $left;
  3131. }
  3132. if (($top !== '') AND ($top >= 0)) {
  3133. $this->cell_margin['T'] = $top;
  3134. }
  3135. if (($right !== '') AND ($right >= 0)) {
  3136. $this->cell_margin['R'] = $right;
  3137. }
  3138. if (($bottom !== '') AND ($bottom >= 0)) {
  3139. $this->cell_margin['B'] = $bottom;
  3140. }
  3141. }
  3142. /**
  3143. * Get the internal Cell margin array.
  3144. * @return array of margin values
  3145. * @public
  3146. * @since 5.9.000 (2010-10-03)
  3147. * @see setCellMargins()
  3148. */
  3149. public function getCellMargins() {
  3150. return $this->cell_margin;
  3151. }
  3152. /**
  3153. * Adjust the internal Cell padding array to take account of the line width.
  3154. * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  3155. * @return array of adjustments
  3156. * @public
  3157. * @since 5.9.000 (2010-10-03)
  3158. */
  3159. protected function adjustCellPadding($brd=0) {
  3160. if (empty($brd)) {
  3161. return;
  3162. }
  3163. if (is_string($brd)) {
  3164. // convert string to array
  3165. $slen = strlen($brd);
  3166. $newbrd = array();
  3167. for ($i = 0; $i < $slen; ++$i) {
  3168. $newbrd[$brd[$i]] = true;
  3169. }
  3170. $brd = $newbrd;
  3171. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3172. $brd = array('LRTB' => true);
  3173. }
  3174. if (!is_array($brd)) {
  3175. return;
  3176. }
  3177. // store current cell padding
  3178. $cp = $this->cell_padding;
  3179. // select border mode
  3180. if (isset($brd['mode'])) {
  3181. $mode = $brd['mode'];
  3182. unset($brd['mode']);
  3183. } else {
  3184. $mode = 'normal';
  3185. }
  3186. // process borders
  3187. foreach ($brd as $border => $style) {
  3188. $line_width = $this->LineWidth;
  3189. if (is_array($style) AND isset($style['width'])) {
  3190. // get border width
  3191. $line_width = $style['width'];
  3192. }
  3193. $adj = 0; // line width inside the cell
  3194. switch ($mode) {
  3195. case 'ext': {
  3196. $adj = 0;
  3197. break;
  3198. }
  3199. case 'int': {
  3200. $adj = $line_width;
  3201. break;
  3202. }
  3203. case 'normal':
  3204. default: {
  3205. $adj = ($line_width / 2);
  3206. break;
  3207. }
  3208. }
  3209. // correct internal cell padding if required to avoid overlap between text and lines
  3210. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3211. $this->cell_padding['T'] = $adj;
  3212. }
  3213. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3214. $this->cell_padding['R'] = $adj;
  3215. }
  3216. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3217. $this->cell_padding['B'] = $adj;
  3218. }
  3219. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3220. $this->cell_padding['L'] = $adj;
  3221. }
  3222. }
  3223. return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
  3224. }
  3225. /**
  3226. * Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
  3227. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3228. * @param $margin (float) Distance from the bottom of the page.
  3229. * @public
  3230. * @since 1.0
  3231. * @see Cell(), MultiCell(), AcceptPageBreak()
  3232. */
  3233. public function SetAutoPageBreak($auto, $margin=0) {
  3234. $this->AutoPageBreak = $auto ? true : false;
  3235. $this->bMargin = $margin;
  3236. $this->PageBreakTrigger = $this->h - $margin;
  3237. }
  3238. /**
  3239. * Return the auto-page-break mode (true or false).
  3240. * @return boolean auto-page-break mode
  3241. * @public
  3242. * @since 5.9.088
  3243. */
  3244. public function getAutoPageBreak() {
  3245. return $this->AutoPageBreak;
  3246. }
  3247. /**
  3248. * Defines the way the document is to be displayed by the viewer.
  3249. * @param $zoom (mixed) The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use. <ul><li>fullpage: displays the entire page on screen </li><li>fullwidth: uses maximum width of window</li><li>real: uses real size (equivalent to 100% zoom)</li><li>default: uses viewer default mode</li></ul>
  3250. * @param $layout (string) The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
  3251. * @param $mode (string) A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
  3252. * @public
  3253. * @since 1.2
  3254. */
  3255. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3256. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3257. $this->ZoomMode = $zoom;
  3258. } else {
  3259. $this->Error('Incorrect zoom display mode: '.$zoom);
  3260. }
  3261. switch ($layout) {
  3262. case 'default':
  3263. case 'single':
  3264. case 'SinglePage': {
  3265. $this->LayoutMode = 'SinglePage';
  3266. break;
  3267. }
  3268. case 'continuous':
  3269. case 'OneColumn': {
  3270. $this->LayoutMode = 'OneColumn';
  3271. break;
  3272. }
  3273. case 'two':
  3274. case 'TwoColumnLeft': {
  3275. $this->LayoutMode = 'TwoColumnLeft';
  3276. break;
  3277. }
  3278. case 'TwoColumnRight': {
  3279. $this->LayoutMode = 'TwoColumnRight';
  3280. break;
  3281. }
  3282. case 'TwoPageLeft': {
  3283. $this->LayoutMode = 'TwoPageLeft';
  3284. break;
  3285. }
  3286. case 'TwoPageRight': {
  3287. $this->LayoutMode = 'TwoPageRight';
  3288. break;
  3289. }
  3290. default: {
  3291. $this->LayoutMode = 'SinglePage';
  3292. }
  3293. }
  3294. switch ($mode) {
  3295. case 'UseNone': {
  3296. $this->PageMode = 'UseNone';
  3297. break;
  3298. }
  3299. case 'UseOutlines': {
  3300. $this->PageMode = 'UseOutlines';
  3301. break;
  3302. }
  3303. case 'UseThumbs': {
  3304. $this->PageMode = 'UseThumbs';
  3305. break;
  3306. }
  3307. case 'FullScreen': {
  3308. $this->PageMode = 'FullScreen';
  3309. break;
  3310. }
  3311. case 'UseOC': {
  3312. $this->PageMode = 'UseOC';
  3313. break;
  3314. }
  3315. case '': {
  3316. $this->PageMode = 'UseAttachments';
  3317. break;
  3318. }
  3319. default: {
  3320. $this->PageMode = 'UseNone';
  3321. }
  3322. }
  3323. }
  3324. /**
  3325. * Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
  3326. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3327. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3328. * @public
  3329. * @since 1.4
  3330. */
  3331. public function SetCompression($compress=true) {
  3332. if (function_exists('gzcompress')) {
  3333. $this->compress = $compress ? true : false;
  3334. } else {
  3335. $this->compress = false;
  3336. }
  3337. }
  3338. /**
  3339. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3340. * @param $mode (boolean) If true force sRGB output intent.
  3341. * @public
  3342. * @since 5.9.121 (2011-09-28)
  3343. */
  3344. public function setSRGBmode($mode=false) {
  3345. $this->force_srgb = $mode ? true : false;
  3346. }
  3347. /**
  3348. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3349. * This has effect only when unicode mode is set to false.
  3350. * @param $unicode (boolean) if true set the meta information in Unicode
  3351. * @since 5.9.027 (2010-12-01)
  3352. * @public
  3353. */
  3354. public function SetDocInfoUnicode($unicode=true) {
  3355. $this->docinfounicode = $unicode ? true : false;
  3356. }
  3357. /**
  3358. * Defines the title of the document.
  3359. * @param $title (string) The title.
  3360. * @public
  3361. * @since 1.2
  3362. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3363. */
  3364. public function SetTitle($title) {
  3365. $this->title = $title;
  3366. }
  3367. /**
  3368. * Defines the subject of the document.
  3369. * @param $subject (string) The subject.
  3370. * @public
  3371. * @since 1.2
  3372. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3373. */
  3374. public function SetSubject($subject) {
  3375. $this->subject = $subject;
  3376. }
  3377. /**
  3378. * Defines the author of the document.
  3379. * @param $author (string) The name of the author.
  3380. * @public
  3381. * @since 1.2
  3382. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3383. */
  3384. public function SetAuthor($author) {
  3385. $this->author = $author;
  3386. }
  3387. /**
  3388. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3389. * @param $keywords (string) The list of keywords.
  3390. * @public
  3391. * @since 1.2
  3392. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3393. */
  3394. public function SetKeywords($keywords) {
  3395. $this->keywords = $keywords;
  3396. }
  3397. /**
  3398. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3399. * @param $creator (string) The name of the creator.
  3400. * @public
  3401. * @since 1.2
  3402. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3403. */
  3404. public function SetCreator($creator) {
  3405. $this->creator = $creator;
  3406. }
  3407. /**
  3408. * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
  3409. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3410. * @param $msg (string) The error message
  3411. * @public
  3412. * @since 1.0
  3413. */
  3414. public function Error($msg) {
  3415. // unset all class variables
  3416. $this->_destroy(true);
  3417. // exit program and print error
  3418. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3419. }
  3420. /**
  3421. * This method begins the generation of the PDF document.
  3422. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3423. * Note: no page is created by this method
  3424. * @public
  3425. * @since 1.0
  3426. * @see AddPage(), Close()
  3427. */
  3428. public function Open() {
  3429. $this->state = 1;
  3430. }
  3431. /**
  3432. * Terminates the PDF document.
  3433. * It is not necessary to call this method explicitly because Output() does it automatically.
  3434. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3435. * @public
  3436. * @since 1.0
  3437. * @see Open(), Output()
  3438. */
  3439. public function Close() {
  3440. if ($this->state == 3) {
  3441. return;
  3442. }
  3443. if ($this->page == 0) {
  3444. $this->AddPage();
  3445. }
  3446. $this->endLayer();
  3447. // save current graphic settings
  3448. $gvars = $this->getGraphicVars();
  3449. $this->setEqualColumns();
  3450. $this->lastpage(true);
  3451. $this->SetAutoPageBreak(false);
  3452. $this->x = 0;
  3453. $this->y = $this->h - (1 / $this->k);
  3454. $this->lMargin = 0;
  3455. $this->_out('q');
  3456. $this->SetFont('helvetica', '', 1);
  3457. $this->setTextRenderingMode(0, false, false);
  3458. $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";
  3459. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3460. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3461. $this->_out('Q');
  3462. // restore graphic settings
  3463. $this->setGraphicVars($gvars);
  3464. // close page
  3465. $this->endPage();
  3466. // close document
  3467. $this->_enddoc();
  3468. // unset all class variables (except critical ones)
  3469. $this->_destroy(false);
  3470. }
  3471. /**
  3472. * Move pointer at the specified document page and update page dimensions.
  3473. * @param $pnum (int) page number (1 ... numpages)
  3474. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3475. * @public
  3476. * @since 2.1.000 (2008-01-07)
  3477. * @see getPage(), lastpage(), getNumPages()
  3478. */
  3479. public function setPage($pnum, $resetmargins=false) {
  3480. if (($pnum == $this->page) AND ($this->state == 2)) {
  3481. return;
  3482. }
  3483. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3484. $this->state = 2;
  3485. // save current graphic settings
  3486. //$gvars = $this->getGraphicVars();
  3487. $oldpage = $this->page;
  3488. $this->page = $pnum;
  3489. $this->wPt = $this->pagedim[$this->page]['w'];
  3490. $this->hPt = $this->pagedim[$this->page]['h'];
  3491. $this->w = $this->pagedim[$this->page]['wk'];
  3492. $this->h = $this->pagedim[$this->page]['hk'];
  3493. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3494. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3495. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3496. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3497. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3498. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3499. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3500. // restore graphic settings
  3501. //$this->setGraphicVars($gvars);
  3502. if ($resetmargins) {
  3503. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3504. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3505. $this->SetY($this->tMargin);
  3506. } else {
  3507. // account for booklet mode
  3508. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3509. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3510. $this->lMargin += $deltam;
  3511. $this->rMargin -= $deltam;
  3512. }
  3513. }
  3514. } else {
  3515. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3516. }
  3517. }
  3518. /**
  3519. * Reset pointer to the last document page.
  3520. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3521. * @public
  3522. * @since 2.0.000 (2008-01-04)
  3523. * @see setPage(), getPage(), getNumPages()
  3524. */
  3525. public function lastPage($resetmargins=false) {
  3526. $this->setPage($this->getNumPages(), $resetmargins);
  3527. }
  3528. /**
  3529. * Get current document page number.
  3530. * @return int page number
  3531. * @public
  3532. * @since 2.1.000 (2008-01-07)
  3533. * @see setPage(), lastpage(), getNumPages()
  3534. */
  3535. public function getPage() {
  3536. return $this->page;
  3537. }
  3538. /**
  3539. * Get the total number of insered pages.
  3540. * @return int number of pages
  3541. * @public
  3542. * @since 2.1.000 (2008-01-07)
  3543. * @see setPage(), getPage(), lastpage()
  3544. */
  3545. public function getNumPages() {
  3546. return $this->numpages;
  3547. }
  3548. /**
  3549. * Adds a new TOC (Table Of Content) page to the document.
  3550. * @param $orientation (string) page orientation.
  3551. * @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().
  3552. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3553. * @public
  3554. * @since 5.0.001 (2010-05-06)
  3555. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3556. */
  3557. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3558. $this->AddPage($orientation, $format, $keepmargins, true);
  3559. }
  3560. /**
  3561. * Terminate the current TOC (Table Of Content) page
  3562. * @public
  3563. * @since 5.0.001 (2010-05-06)
  3564. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3565. */
  3566. public function endTOCPage() {
  3567. $this->endPage(true);
  3568. }
  3569. /**
  3570. * 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).
  3571. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3572. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3573. * @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().
  3574. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3575. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3576. * @public
  3577. * @since 1.0
  3578. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3579. */
  3580. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3581. if ($this->inxobj) {
  3582. // we are inside an XObject template
  3583. return;
  3584. }
  3585. if (!isset($this->original_lMargin) OR $keepmargins) {
  3586. $this->original_lMargin = $this->lMargin;
  3587. }
  3588. if (!isset($this->original_rMargin) OR $keepmargins) {
  3589. $this->original_rMargin = $this->rMargin;
  3590. }
  3591. // terminate previous page
  3592. $this->endPage();
  3593. // start new page
  3594. $this->startPage($orientation, $format, $tocpage);
  3595. }
  3596. /**
  3597. * Terminate the current page
  3598. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3599. * @public
  3600. * @since 4.2.010 (2008-11-14)
  3601. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3602. */
  3603. public function endPage($tocpage=false) {
  3604. // check if page is already closed
  3605. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3606. return;
  3607. }
  3608. // print page footer
  3609. $this->setFooter();
  3610. // close page
  3611. $this->_endpage();
  3612. // mark page as closed
  3613. $this->pageopen[$this->page] = false;
  3614. if ($tocpage) {
  3615. $this->tocpage = false;
  3616. }
  3617. }
  3618. /**
  3619. * Starts a new page to the document. The page must be closed using the endPage() function.
  3620. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3621. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3622. * @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().
  3623. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3624. * @since 4.2.010 (2008-11-14)
  3625. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3626. * @public
  3627. */
  3628. public function startPage($orientation='', $format='', $tocpage=false) {
  3629. if ($tocpage) {
  3630. $this->tocpage = true;
  3631. }
  3632. // move page numbers of documents to be attached
  3633. if ($this->tocpage) {
  3634. // move reference to unexistent pages (used for page attachments)
  3635. // adjust outlines
  3636. $tmpoutlines = $this->outlines;
  3637. foreach ($tmpoutlines as $key => $outline) {
  3638. if ($outline['p'] > $this->numpages) {
  3639. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3640. }
  3641. }
  3642. // adjust dests
  3643. $tmpdests = $this->dests;
  3644. foreach ($tmpdests as $key => $dest) {
  3645. if ($dest['p'] > $this->numpages) {
  3646. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3647. }
  3648. }
  3649. // adjust links
  3650. $tmplinks = $this->links;
  3651. foreach ($tmplinks as $key => $link) {
  3652. if ($link[0] > $this->numpages) {
  3653. $this->links[$key][0] = ($link[0] + 1);
  3654. }
  3655. }
  3656. }
  3657. if ($this->numpages > $this->page) {
  3658. // this page has been already added
  3659. $this->setPage($this->page + 1);
  3660. $this->SetY($this->tMargin);
  3661. return;
  3662. }
  3663. // start a new page
  3664. if ($this->state == 0) {
  3665. $this->Open();
  3666. }
  3667. ++$this->numpages;
  3668. $this->swapMargins($this->booklet);
  3669. // save current graphic settings
  3670. $gvars = $this->getGraphicVars();
  3671. // start new page
  3672. $this->_beginpage($orientation, $format);
  3673. // mark page as open
  3674. $this->pageopen[$this->page] = true;
  3675. // restore graphic settings
  3676. $this->setGraphicVars($gvars);
  3677. // mark this point
  3678. $this->setPageMark();
  3679. // print page header
  3680. $this->setHeader();
  3681. // restore graphic settings
  3682. $this->setGraphicVars($gvars);
  3683. // mark this point
  3684. $this->setPageMark();
  3685. // print table header (if any)
  3686. $this->setTableHeader();
  3687. // set mark for empty page check
  3688. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3689. }
  3690. /**
  3691. * Set start-writing mark on current page stream used to put borders and fills.
  3692. * Borders and fills are always created after content and inserted on the position marked by this method.
  3693. * This function must be called after calling Image() function for a background image.
  3694. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3695. * @public
  3696. * @since 4.0.016 (2008-07-30)
  3697. */
  3698. public function setPageMark() {
  3699. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3700. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3701. $this->setContentMark();
  3702. }
  3703. /**
  3704. * Set start-writing mark on selected page.
  3705. * Borders and fills are always created after content and inserted on the position marked by this method.
  3706. * @param $page (int) page number (default is the current page)
  3707. * @protected
  3708. * @since 4.6.021 (2009-07-20)
  3709. */
  3710. protected function setContentMark($page=0) {
  3711. if ($page <= 0) {
  3712. $page = $this->page;
  3713. }
  3714. if (isset($this->footerlen[$page])) {
  3715. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3716. } else {
  3717. $this->cntmrk[$page] = $this->pagelen[$page];
  3718. }
  3719. }
  3720. /**
  3721. * Set header data.
  3722. * @param $ln (string) header image logo
  3723. * @param $lw (string) header image logo width in mm
  3724. * @param $ht (string) string to print as title on document header
  3725. * @param $hs (string) string to print on document header
  3726. * @public
  3727. */
  3728. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3729. $this->header_logo = $ln;
  3730. $this->header_logo_width = $lw;
  3731. $this->header_title = $ht;
  3732. $this->header_string = $hs;
  3733. }
  3734. /**
  3735. * Returns header data:
  3736. * <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>
  3737. * @return array()
  3738. * @public
  3739. * @since 4.0.012 (2008-07-24)
  3740. */
  3741. public function getHeaderData() {
  3742. $ret = array();
  3743. $ret['logo'] = $this->header_logo;
  3744. $ret['logo_width'] = $this->header_logo_width;
  3745. $ret['title'] = $this->header_title;
  3746. $ret['string'] = $this->header_string;
  3747. return $ret;
  3748. }
  3749. /**
  3750. * Set header margin.
  3751. * (minimum distance between header and top page margin)
  3752. * @param $hm (int) distance in user units
  3753. * @public
  3754. */
  3755. public function setHeaderMargin($hm=10) {
  3756. $this->header_margin = $hm;
  3757. }
  3758. /**
  3759. * Returns header margin in user units.
  3760. * @return float
  3761. * @since 4.0.012 (2008-07-24)
  3762. * @public
  3763. */
  3764. public function getHeaderMargin() {
  3765. return $this->header_margin;
  3766. }
  3767. /**
  3768. * Set footer margin.
  3769. * (minimum distance between footer and bottom page margin)
  3770. * @param $fm (int) distance in user units
  3771. * @public
  3772. */
  3773. public function setFooterMargin($fm=10) {
  3774. $this->footer_margin = $fm;
  3775. }
  3776. /**
  3777. * Returns footer margin in user units.
  3778. * @return float
  3779. * @since 4.0.012 (2008-07-24)
  3780. * @public
  3781. */
  3782. public function getFooterMargin() {
  3783. return $this->footer_margin;
  3784. }
  3785. /**
  3786. * Set a flag to print page header.
  3787. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3788. * @public
  3789. */
  3790. public function setPrintHeader($val=true) {
  3791. $this->print_header = $val ? true : false;
  3792. }
  3793. /**
  3794. * Set a flag to print page footer.
  3795. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3796. * @public
  3797. */
  3798. public function setPrintFooter($val=true) {
  3799. $this->print_footer = $val ? true : false;
  3800. }
  3801. /**
  3802. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3803. * @return float
  3804. * @public
  3805. */
  3806. public function getImageRBX() {
  3807. return $this->img_rb_x;
  3808. }
  3809. /**
  3810. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3811. * @return float
  3812. * @public
  3813. */
  3814. public function getImageRBY() {
  3815. return $this->img_rb_y;
  3816. }
  3817. /**
  3818. * Reset the xobject template used by Header() method.
  3819. * @public
  3820. */
  3821. public function resetHeaderTemplate() {
  3822. $this->header_xobjid = -1;
  3823. }
  3824. /**
  3825. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3826. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3827. * @public
  3828. */
  3829. public function setHeaderTemplateAutoreset($val=true) {
  3830. $this->header_xobj_autoreset = $val ? true : false;
  3831. }
  3832. /**
  3833. * This method is used to render the page header.
  3834. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3835. * @public
  3836. */
  3837. public function Header() {
  3838. if ($this->header_xobjid < 0) {
  3839. // start a new XObject Template
  3840. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3841. $headerfont = $this->getHeaderFont();
  3842. $headerdata = $this->getHeaderData();
  3843. $this->y = $this->header_margin;
  3844. if ($this->rtl) {
  3845. $this->x = $this->w - $this->original_rMargin;
  3846. } else {
  3847. $this->x = $this->original_lMargin;
  3848. }
  3849. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3850. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3851. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3852. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3853. } elseif ($imgtype == 'svg') {
  3854. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3855. } else {
  3856. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3857. }
  3858. $imgy = $this->getImageRBY();
  3859. } else {
  3860. $imgy = $this->y;
  3861. }
  3862. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3863. // set starting margin for text data cell
  3864. if ($this->getRTL()) {
  3865. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3866. } else {
  3867. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3868. }
  3869. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3870. $this->SetTextColor(0, 0, 0);
  3871. // header title
  3872. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3873. $this->SetX($header_x);
  3874. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3875. // header string
  3876. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3877. $this->SetX($header_x);
  3878. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3879. // print an ending header line
  3880. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3881. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3882. if ($this->rtl) {
  3883. $this->SetX($this->original_rMargin);
  3884. } else {
  3885. $this->SetX($this->original_lMargin);
  3886. }
  3887. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3888. $this->endTemplate();
  3889. }
  3890. // print header template
  3891. $x = 0;
  3892. $dx = 0;
  3893. if ($this->booklet AND (($this->page % 2) == 0)) {
  3894. // adjust margins for booklet mode
  3895. $dx = ($this->original_lMargin - $this->original_rMargin);
  3896. }
  3897. if ($this->rtl) {
  3898. $x = $this->w + $dx;
  3899. } else {
  3900. $x = 0 + $dx;
  3901. }
  3902. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3903. if ($this->header_xobj_autoreset) {
  3904. // reset header xobject template at each page
  3905. $this->header_xobjid = -1;
  3906. }
  3907. }
  3908. /**
  3909. * This method is used to render the page footer.
  3910. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3911. * @public
  3912. */
  3913. public function Footer() {
  3914. $cur_y = $this->y;
  3915. $this->SetTextColor(0, 0, 0);
  3916. //set style for cell border
  3917. $line_width = 0.85 / $this->k;
  3918. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3919. //print document barcode
  3920. $barcode = $this->getBarcode();
  3921. if (!empty($barcode)) {
  3922. $this->Ln($line_width);
  3923. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3924. $style = array(
  3925. 'position' => $this->rtl?'R':'L',
  3926. 'align' => $this->rtl?'R':'L',
  3927. 'stretch' => false,
  3928. 'fitwidth' => true,
  3929. 'cellfitalign' => '',
  3930. 'border' => false,
  3931. 'padding' => 0,
  3932. 'fgcolor' => array(0,0,0),
  3933. 'bgcolor' => false,
  3934. 'text' => false
  3935. );
  3936. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3937. }
  3938. if (empty($this->pagegroups)) {
  3939. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3940. } else {
  3941. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3942. }
  3943. $this->SetY($cur_y);
  3944. //Print page number
  3945. if ($this->getRTL()) {
  3946. $this->SetX($this->original_rMargin);
  3947. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3948. } else {
  3949. $this->SetX($this->original_lMargin);
  3950. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3951. }
  3952. }
  3953. /**
  3954. * This method is used to render the page header.
  3955. * @protected
  3956. * @since 4.0.012 (2008-07-24)
  3957. */
  3958. protected function setHeader() {
  3959. if (!$this->print_header) {
  3960. return;
  3961. }
  3962. $this->InHeader = true;
  3963. $this->setGraphicVars($this->default_graphic_vars);
  3964. $temp_thead = $this->thead;
  3965. $temp_theadMargins = $this->theadMargins;
  3966. $lasth = $this->lasth;
  3967. $this->_out('q');
  3968. $this->rMargin = $this->original_rMargin;
  3969. $this->lMargin = $this->original_lMargin;
  3970. $this->SetCellPadding(0);
  3971. //set current position
  3972. if ($this->rtl) {
  3973. $this->SetXY($this->original_rMargin, $this->header_margin);
  3974. } else {
  3975. $this->SetXY($this->original_lMargin, $this->header_margin);
  3976. }
  3977. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3978. $this->Header();
  3979. //restore position
  3980. if ($this->rtl) {
  3981. $this->SetXY($this->original_rMargin, $this->tMargin);
  3982. } else {
  3983. $this->SetXY($this->original_lMargin, $this->tMargin);
  3984. }
  3985. $this->_out('Q');
  3986. $this->lasth = $lasth;
  3987. $this->thead = $temp_thead;
  3988. $this->theadMargins = $temp_theadMargins;
  3989. $this->newline = false;
  3990. $this->InHeader = false;
  3991. }
  3992. /**
  3993. * This method is used to render the page footer.
  3994. * @protected
  3995. * @since 4.0.012 (2008-07-24)
  3996. */
  3997. protected function setFooter() {
  3998. //Page footer
  3999. $this->InFooter = true;
  4000. // save current graphic settings
  4001. $gvars = $this->getGraphicVars();
  4002. // mark this point
  4003. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4004. $this->_out("\n");
  4005. if ($this->print_footer) {
  4006. $this->setGraphicVars($this->default_graphic_vars);
  4007. $this->current_column = 0;
  4008. $this->num_columns = 1;
  4009. $temp_thead = $this->thead;
  4010. $temp_theadMargins = $this->theadMargins;
  4011. $lasth = $this->lasth;
  4012. $this->_out('q');
  4013. $this->rMargin = $this->original_rMargin;
  4014. $this->lMargin = $this->original_lMargin;
  4015. $this->SetCellPadding(0);
  4016. //set current position
  4017. $footer_y = $this->h - $this->footer_margin;
  4018. if ($this->rtl) {
  4019. $this->SetXY($this->original_rMargin, $footer_y);
  4020. } else {
  4021. $this->SetXY($this->original_lMargin, $footer_y);
  4022. }
  4023. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4024. $this->Footer();
  4025. //restore position
  4026. if ($this->rtl) {
  4027. $this->SetXY($this->original_rMargin, $this->tMargin);
  4028. } else {
  4029. $this->SetXY($this->original_lMargin, $this->tMargin);
  4030. }
  4031. $this->_out('Q');
  4032. $this->lasth = $lasth;
  4033. $this->thead = $temp_thead;
  4034. $this->theadMargins = $temp_theadMargins;
  4035. }
  4036. // restore graphic settings
  4037. $this->setGraphicVars($gvars);
  4038. $this->current_column = $gvars['current_column'];
  4039. $this->num_columns = $gvars['num_columns'];
  4040. // calculate footer length
  4041. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4042. $this->InFooter = false;
  4043. }
  4044. /**
  4045. * Check if we are on the page body (excluding page header and footer).
  4046. * @return true if we are not in page header nor in page footer, false otherwise.
  4047. * @protected
  4048. * @since 5.9.091 (2011-06-15)
  4049. */
  4050. protected function inPageBody() {
  4051. return (($this->InHeader === false) AND ($this->InFooter === false));
  4052. }
  4053. /**
  4054. * This method is used to render the table header on new page (if any).
  4055. * @protected
  4056. * @since 4.5.030 (2009-03-25)
  4057. */
  4058. protected function setTableHeader() {
  4059. if ($this->num_columns > 1) {
  4060. // multi column mode
  4061. return;
  4062. }
  4063. if (isset($this->theadMargins['top'])) {
  4064. // restore the original top-margin
  4065. $this->tMargin = $this->theadMargins['top'];
  4066. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4067. $this->y = $this->tMargin;
  4068. }
  4069. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4070. // set margins
  4071. $prev_lMargin = $this->lMargin;
  4072. $prev_rMargin = $this->rMargin;
  4073. $prev_cell_padding = $this->cell_padding;
  4074. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4075. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4076. $this->cell_padding = $this->theadMargins['cell_padding'];
  4077. if ($this->rtl) {
  4078. $this->x = $this->w - $this->rMargin;
  4079. } else {
  4080. $this->x = $this->lMargin;
  4081. }
  4082. // account for special "cell" mode
  4083. if ($this->theadMargins['cell']) {
  4084. if ($this->rtl) {
  4085. $this->x -= $this->cell_padding['R'];
  4086. } else {
  4087. $this->x += $this->cell_padding['L'];
  4088. }
  4089. }
  4090. // print table header
  4091. $this->writeHTML($this->thead, false, false, false, false, '');
  4092. // set new top margin to skip the table headers
  4093. if (!isset($this->theadMargins['top'])) {
  4094. $this->theadMargins['top'] = $this->tMargin;
  4095. }
  4096. // store end of header position
  4097. if (!isset($this->columns[0]['th'])) {
  4098. $this->columns[0]['th'] = array();
  4099. }
  4100. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4101. $this->tMargin = $this->y;
  4102. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4103. $this->lasth = 0;
  4104. $this->lMargin = $prev_lMargin;
  4105. $this->rMargin = $prev_rMargin;
  4106. $this->cell_padding = $prev_cell_padding;
  4107. }
  4108. }
  4109. /**
  4110. * Returns the current page number.
  4111. * @return int page number
  4112. * @public
  4113. * @since 1.0
  4114. * @see getAliasNbPages()
  4115. */
  4116. public function PageNo() {
  4117. return $this->page;
  4118. }
  4119. /**
  4120. * Defines a new spot color.
  4121. * It can be expressed in RGB components or gray scale.
  4122. * The method can be called before the first page is created and the value is retained from page to page.
  4123. * @param $name (string) Full name of the spot color.
  4124. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4125. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4126. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4127. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4128. * @public
  4129. * @since 4.0.024 (2008-09-12)
  4130. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4131. */
  4132. public function AddSpotColor($name, $c, $m, $y, $k) {
  4133. if (!isset($this->spot_colors[$name])) {
  4134. $i = (1 + count($this->spot_colors));
  4135. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  4136. }
  4137. }
  4138. /**
  4139. * Return the Spot color array.
  4140. * @param $name (string) Name of the spot color.
  4141. * @return (array) Spot color array or false if not defined.
  4142. * @public
  4143. * @since 5.9.125 (2011-10-03)
  4144. */
  4145. public function getSpotColor($name) {
  4146. if (isset($this->spot_colors[$name])) {
  4147. return $this->spot_colors[$name];
  4148. }
  4149. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4150. $color = strtolower($color);
  4151. if (isset($this->spotcolor[$color])) {
  4152. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4153. return $this->spot_colors[$this->spotcolor[$color][4]];
  4154. }
  4155. return false;
  4156. }
  4157. /**
  4158. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4159. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4160. * @param $name (string) Name of the spot color.
  4161. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4162. * @return (string) PDF color command.
  4163. * @public
  4164. * @since 5.9.125 (2011-10-03)
  4165. */
  4166. public function setSpotColor($type, $name, $tint=100) {
  4167. $spotcolor = $this->getSpotColor($name);
  4168. if ($spotcolor === false) {
  4169. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4170. }
  4171. $tint = (max(0, min(100, $tint)) / 100);
  4172. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4173. switch ($type) {
  4174. case 'draw': {
  4175. $pdfcolor .= sprintf('CS %.3F SCN', $tint);
  4176. $this->DrawColor = $pdfcolor;
  4177. $this->strokecolor = $spotcolor;
  4178. break;
  4179. }
  4180. case 'fill': {
  4181. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4182. $this->FillColor = $pdfcolor;
  4183. $this->bgcolor = $spotcolor;
  4184. break;
  4185. }
  4186. case 'text': {
  4187. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4188. $this->TextColor = $pdfcolor;
  4189. $this->fgcolor = $spotcolor;
  4190. break;
  4191. }
  4192. }
  4193. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4194. if ($this->page > 0) {
  4195. $this->_out($pdfcolor);
  4196. }
  4197. if ($this->inxobj) {
  4198. // we are inside an XObject template
  4199. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4200. }
  4201. return $pdfcolor;
  4202. }
  4203. /**
  4204. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4205. * @param $name (string) Name of the spot color.
  4206. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4207. * @public
  4208. * @since 4.0.024 (2008-09-12)
  4209. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4210. */
  4211. public function SetDrawSpotColor($name, $tint=100) {
  4212. $this->setSpotColor('draw', $name, $tint);
  4213. }
  4214. /**
  4215. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4216. * @param $name (string) Name of the spot color.
  4217. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4218. * @public
  4219. * @since 4.0.024 (2008-09-12)
  4220. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4221. */
  4222. public function SetFillSpotColor($name, $tint=100) {
  4223. $this->setSpotColor('fill', $name, $tint);
  4224. }
  4225. /**
  4226. * Defines the spot color used for text.
  4227. * @param $name (string) Name of the spot color.
  4228. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4229. * @public
  4230. * @since 4.0.024 (2008-09-12)
  4231. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4232. */
  4233. public function SetTextSpotColor($name, $tint=100) {
  4234. $this->setSpotColor('text', $name, $tint);
  4235. }
  4236. /**
  4237. * Set the color array for the specified type ('draw', 'fill', 'text').
  4238. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4239. * The method can be called before the first page is created and the value is retained from page to page.
  4240. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4241. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  4242. * @param $ret (boolean) If true do not send the PDF command.
  4243. * @return (string) The PDF command or empty string.
  4244. * @public
  4245. * @since 3.1.000 (2008-06-11)
  4246. */
  4247. public function setColorArray($type, $color, $ret=false) {
  4248. if (is_array($color)) {
  4249. $color = array_values($color);
  4250. // component: grey, RGB red or CMYK cyan
  4251. $c = isset($color[0]) ? $color[0] : -1;
  4252. // component: RGB green or CMYK magenta
  4253. $m = isset($color[1]) ? $color[1] : -1;
  4254. // component: RGB blue or CMYK yellow
  4255. $y = isset($color[2]) ? $color[2] : -1;
  4256. // component: CMYK black
  4257. $k = isset($color[3]) ? $color[3] : -1;
  4258. // color name
  4259. $name = isset($color[4]) ? $color[4] : '';
  4260. if ($c >= 0) {
  4261. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  4262. }
  4263. }
  4264. return '';
  4265. }
  4266. /**
  4267. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4268. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4269. * The method can be called before the first page is created and the value is retained from page to page.
  4270. * @param $color (array) Array of colors (1, 3 or 4 values).
  4271. * @param $ret (boolean) If true do not send the PDF command.
  4272. * @return string the PDF command
  4273. * @public
  4274. * @since 3.1.000 (2008-06-11)
  4275. * @see SetDrawColor()
  4276. */
  4277. public function SetDrawColorArray($color, $ret=false) {
  4278. return $this->setColorArray('draw', $color, $ret);
  4279. }
  4280. /**
  4281. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  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 $color (array) Array of colors (1, 3 or 4 values).
  4285. * @param $ret (boolean) If true do not send the PDF command.
  4286. * @public
  4287. * @since 3.1.000 (2008-6-11)
  4288. * @see SetFillColor()
  4289. */
  4290. public function SetFillColorArray($color, $ret=false) {
  4291. return $this->setColorArray('fill', $color, $ret);
  4292. }
  4293. /**
  4294. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4295. * The method can be called before the first page is created and the value is retained from page to page.
  4296. * @param $color (array) Array of colors (1, 3 or 4 values).
  4297. * @param $ret (boolean) If true do not send the PDF command.
  4298. * @public
  4299. * @since 3.1.000 (2008-6-11)
  4300. * @see SetFillColor()
  4301. */
  4302. public function SetTextColorArray($color, $ret=false) {
  4303. return $this->setColorArray('text', $color, $ret);
  4304. }
  4305. /**
  4306. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4307. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4308. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4309. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4310. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4311. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4312. * @param $ret (boolean) If true do not send the command.
  4313. * @param $name (string) spot color name (if any)
  4314. * @return (string) The PDF command or empty string.
  4315. * @public
  4316. * @since 5.9.125 (2011-10-03)
  4317. */
  4318. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4319. // set default values
  4320. if (!is_numeric($col1)) {
  4321. $col1 = 0;
  4322. }
  4323. if (!is_numeric($col2)) {
  4324. $col2 = -1;
  4325. }
  4326. if (!is_numeric($col3)) {
  4327. $col3 = -1;
  4328. }
  4329. if (!is_numeric($col4)) {
  4330. $col4 = -1;
  4331. }
  4332. // set color by case
  4333. $suffix = '';
  4334. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4335. // Grey scale
  4336. $col1 = max(0, min(255, $col1));
  4337. $intcolor = array('G' => $col1);
  4338. $pdfcolor = sprintf('%.3F ', ($col1 / 255));
  4339. $suffix = 'g';
  4340. } elseif ($col4 == -1) {
  4341. // RGB
  4342. $col1 = max(0, min(255, $col1));
  4343. $col2 = max(0, min(255, $col2));
  4344. $col3 = max(0, min(255, $col3));
  4345. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4346. $pdfcolor = sprintf('%.3F %.3F %.3F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4347. $suffix = 'rg';
  4348. } else {
  4349. $col1 = max(0, min(100, $col1));
  4350. $col2 = max(0, min(100, $col2));
  4351. $col3 = max(0, min(100, $col3));
  4352. $col4 = max(0, min(100, $col4));
  4353. if (empty($name)) {
  4354. // CMYK
  4355. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4356. $pdfcolor = sprintf('%.3F %.3F %.3F %.3F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4357. $suffix = 'k';
  4358. } else {
  4359. // SPOT COLOR
  4360. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4361. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4362. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4363. }
  4364. }
  4365. switch ($type) {
  4366. case 'draw': {
  4367. $pdfcolor .= strtoupper($suffix);
  4368. $this->DrawColor = $pdfcolor;
  4369. $this->strokecolor = $intcolor;
  4370. break;
  4371. }
  4372. case 'fill': {
  4373. $pdfcolor .= $suffix;
  4374. $this->FillColor = $pdfcolor;
  4375. $this->bgcolor = $intcolor;
  4376. break;
  4377. }
  4378. case 'text': {
  4379. $pdfcolor .= $suffix;
  4380. $this->TextColor = $pdfcolor;
  4381. $this->fgcolor = $intcolor;
  4382. break;
  4383. }
  4384. }
  4385. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4386. if (($type != 'text') AND ($this->page > 0)) {
  4387. if (!$ret) {
  4388. $this->_out($pdfcolor);
  4389. }
  4390. return $pdfcolor;
  4391. }
  4392. return '';
  4393. }
  4394. /**
  4395. * Convert a color array into a string representation.
  4396. * @param $c (array) Array of colors.
  4397. * @return (string) The color array representation.
  4398. * @protected
  4399. * @since 5.9.137 (2011-12-01)
  4400. */
  4401. protected function getColorStringFromArray($c) {
  4402. $c = array_values($c);
  4403. $color = '[';
  4404. switch (count($c)) {
  4405. case 4: {
  4406. // CMYK
  4407. $color .= sprintf('%.3F %.3F %.3F %.3F', (max(0, min(100, floatval($c[0]))) / 100), (max(0, min(100, floatval($c[1]))) / 100), (max(0, min(100, floatval($c[2]))) / 100), (max(0, min(100, floatval($c[3]))) / 100));
  4408. break;
  4409. }
  4410. case 3: {
  4411. // RGB
  4412. $color .= sprintf('%.3F %.3F %.3F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  4413. break;
  4414. }
  4415. case 1: {
  4416. // grayscale
  4417. $color .= sprintf('%.3F', (max(0, min(255, floatval($c[0]))) / 255));
  4418. break;
  4419. }
  4420. }
  4421. $color .= ']';
  4422. return $color;
  4423. }
  4424. /**
  4425. * 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.
  4426. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4427. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4428. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4429. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4430. * @param $ret (boolean) If true do not send the command.
  4431. * @param $name (string) spot color name (if any)
  4432. * @return string the PDF command
  4433. * @public
  4434. * @since 1.3
  4435. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4436. */
  4437. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4438. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4439. }
  4440. /**
  4441. * 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.
  4442. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4443. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4444. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4445. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4446. * @param $ret (boolean) If true do not send the command.
  4447. * @param $name (string) Spot color name (if any).
  4448. * @return (string) The PDF command.
  4449. * @public
  4450. * @since 1.3
  4451. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4452. */
  4453. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4454. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4455. }
  4456. /**
  4457. * 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.
  4458. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4459. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4460. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4461. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4462. * @param $ret (boolean) If true do not send the command.
  4463. * @param $name (string) Spot color name (if any).
  4464. * @return (string) Empty string.
  4465. * @public
  4466. * @since 1.3
  4467. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4468. */
  4469. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4470. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4471. }
  4472. /**
  4473. * Returns the length of a string in user unit. A font must be selected.<br>
  4474. * @param $s (string) The string whose length is to be computed
  4475. * @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.
  4476. * @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.
  4477. * @param $fontsize (float) Font size in points. The default value is the current size.
  4478. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4479. * @return mixed int total string length or array of characted widths
  4480. * @author Nicola Asuni
  4481. * @public
  4482. * @since 1.2
  4483. */
  4484. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4485. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4486. }
  4487. /**
  4488. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4489. * @param $sa (string) The array of chars whose total length is to be computed
  4490. * @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.
  4491. * @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.
  4492. * @param $fontsize (float) Font size in points. The default value is the current size.
  4493. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4494. * @return mixed int total string length or array of characted widths
  4495. * @author Nicola Asuni
  4496. * @public
  4497. * @since 2.4.000 (2008-03-06)
  4498. */
  4499. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4500. // store current values
  4501. if (!$this->empty_string($fontname)) {
  4502. $prev_FontFamily = $this->FontFamily;
  4503. $prev_FontStyle = $this->FontStyle;
  4504. $prev_FontSizePt = $this->FontSizePt;
  4505. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4506. }
  4507. // convert UTF-8 array to Latin1 if required
  4508. $sa = $this->UTF8ArrToLatin1($sa);
  4509. $w = 0; // total width
  4510. $wa = array(); // array of characters widths
  4511. foreach ($sa as $ck => $char) {
  4512. // character width
  4513. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4514. $wa[] = $cw;
  4515. $w += $cw;
  4516. }
  4517. // restore previous values
  4518. if (!$this->empty_string($fontname)) {
  4519. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4520. }
  4521. if ($getarray) {
  4522. return $wa;
  4523. }
  4524. return $w;
  4525. }
  4526. /**
  4527. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4528. * @param $char (int) The char code whose length is to be returned
  4529. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4530. * @return float char width
  4531. * @author Nicola Asuni
  4532. * @public
  4533. * @since 2.4.000 (2008-03-06)
  4534. */
  4535. public function GetCharWidth($char, $notlast=true) {
  4536. // get raw width
  4537. $chw = $this->getRawCharWidth($char);
  4538. if (($this->font_spacing != 0) AND $notlast) {
  4539. // increase/decrease font spacing
  4540. $chw += $this->font_spacing;
  4541. }
  4542. if ($this->font_stretching != 100) {
  4543. // fixed stretching mode
  4544. $chw *= ($this->font_stretching / 100);
  4545. }
  4546. return $chw;
  4547. }
  4548. /**
  4549. * Returns the length of the char in user unit for the current font.
  4550. * @param $char (int) The char code whose length is to be returned
  4551. * @return float char width
  4552. * @author Nicola Asuni
  4553. * @public
  4554. * @since 5.9.000 (2010-09-28)
  4555. */
  4556. public function getRawCharWidth($char) {
  4557. if ($char == 173) {
  4558. // SHY character will not be printed
  4559. return (0);
  4560. }
  4561. if (isset($this->CurrentFont['cw'][$char])) {
  4562. $w = $this->CurrentFont['cw'][$char];
  4563. } elseif (isset($this->CurrentFont['dw'])) {
  4564. // default width
  4565. $w = $this->CurrentFont['dw'];
  4566. } elseif (isset($this->CurrentFont['cw'][32])) {
  4567. // default width
  4568. $w = $this->CurrentFont['cw'][32];
  4569. } else {
  4570. $w = 600;
  4571. }
  4572. return ($w * $this->FontSize / 1000);
  4573. }
  4574. /**
  4575. * Returns the numbero of characters in a string.
  4576. * @param $s (string) The input string.
  4577. * @return int number of characters
  4578. * @public
  4579. * @since 2.0.0001 (2008-01-07)
  4580. */
  4581. public function GetNumChars($s) {
  4582. if ($this->isUnicodeFont()) {
  4583. return count($this->UTF8StringToArray($s));
  4584. }
  4585. return strlen($s);
  4586. }
  4587. /**
  4588. * Fill the list of available fonts ($this->fontlist).
  4589. * @protected
  4590. * @since 4.0.013 (2008-07-28)
  4591. */
  4592. protected function getFontsList() {
  4593. $fontsdir = opendir($this->_getfontpath());
  4594. while (($file = readdir($fontsdir)) !== false) {
  4595. if (substr($file, -4) == '.php') {
  4596. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4597. }
  4598. }
  4599. closedir($fontsdir);
  4600. }
  4601. /**
  4602. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4603. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4604. * 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.
  4605. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4606. * @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>
  4607. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4608. * @return array containing the font data, or false in case of error.
  4609. * @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.
  4610. * @public
  4611. * @since 1.5
  4612. * @see SetFont(), setFontSubsetting()
  4613. */
  4614. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4615. if ($subset === 'default') {
  4616. $subset = $this->font_subsetting;
  4617. }
  4618. if ($this->pdfa_mode) {
  4619. $subset = false;
  4620. }
  4621. if ($this->empty_string($family)) {
  4622. if (!$this->empty_string($this->FontFamily)) {
  4623. $family = $this->FontFamily;
  4624. } else {
  4625. $this->Error('Empty font family');
  4626. }
  4627. }
  4628. // move embedded styles on $style
  4629. if (substr($family, -1) == 'I') {
  4630. $style .= 'I';
  4631. $family = substr($family, 0, -1);
  4632. }
  4633. if (substr($family, -1) == 'B') {
  4634. $style .= 'B';
  4635. $family = substr($family, 0, -1);
  4636. }
  4637. // normalize family name
  4638. $family = strtolower($family);
  4639. if ((!$this->isunicode) AND ($family == 'arial')) {
  4640. $family = 'helvetica';
  4641. }
  4642. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4643. $style = '';
  4644. }
  4645. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4646. // all fonts must be embedded
  4647. $family = 'pdfa'.$family;
  4648. }
  4649. $tempstyle = strtoupper($style);
  4650. $style = '';
  4651. // underline
  4652. if (strpos($tempstyle, 'U') !== false) {
  4653. $this->underline = true;
  4654. } else {
  4655. $this->underline = false;
  4656. }
  4657. // line-through (deleted)
  4658. if (strpos($tempstyle, 'D') !== false) {
  4659. $this->linethrough = true;
  4660. } else {
  4661. $this->linethrough = false;
  4662. }
  4663. // overline
  4664. if (strpos($tempstyle, 'O') !== false) {
  4665. $this->overline = true;
  4666. } else {
  4667. $this->overline = false;
  4668. }
  4669. // bold
  4670. if (strpos($tempstyle, 'B') !== false) {
  4671. $style .= 'B';
  4672. }
  4673. // oblique
  4674. if (strpos($tempstyle, 'I') !== false) {
  4675. $style .= 'I';
  4676. }
  4677. $bistyle = $style;
  4678. $fontkey = $family.$style;
  4679. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4680. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4681. // check if the font has been already added
  4682. $fb = $this->getFontBuffer($fontkey);
  4683. if ($fb !== false) {
  4684. if ($this->inxobj) {
  4685. // we are inside an XObject template
  4686. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4687. }
  4688. return $fontdata;
  4689. }
  4690. if (isset($type)) {
  4691. unset($type);
  4692. }
  4693. if (isset($cw)) {
  4694. unset($cw);
  4695. }
  4696. // get specified font directory (if any)
  4697. $fontdir = false;
  4698. if (!$this->empty_string($fontfile)) {
  4699. $fontdir = dirname($fontfile);
  4700. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4701. $fontdir = '';
  4702. } else {
  4703. $fontdir .= '/';
  4704. }
  4705. }
  4706. $missing_style = false; // true when the font style variation is missing
  4707. // search and include font file
  4708. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4709. // build a standard filenames for specified font
  4710. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4711. // search files on various directories
  4712. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4713. $fontfile = $fontdir.$tmp_fontfile;
  4714. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4715. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4716. } elseif (file_exists($tmp_fontfile)) {
  4717. $fontfile = $tmp_fontfile;
  4718. } elseif (!$this->empty_string($style)) {
  4719. $missing_style = true;
  4720. // try to remove the style part
  4721. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4722. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4723. $fontfile = $fontdir.$tmp_fontfile;
  4724. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4725. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4726. } else {
  4727. $fontfile = $tmp_fontfile;
  4728. }
  4729. }
  4730. }
  4731. // include font file
  4732. if (file_exists($fontfile)) {
  4733. include($fontfile);
  4734. } else {
  4735. $this->Error('Could not include font definition file: '.$family.'');
  4736. }
  4737. // check font parameters
  4738. if ((!isset($type)) OR (!isset($cw))) {
  4739. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4740. }
  4741. // SET default parameters
  4742. if (!isset($file) OR $this->empty_string($file)) {
  4743. $file = '';
  4744. }
  4745. if (!isset($enc) OR $this->empty_string($enc)) {
  4746. $enc = '';
  4747. }
  4748. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4749. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4750. $cidinfo['uni2cid'] = array();
  4751. }
  4752. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4753. $ctg = '';
  4754. }
  4755. if (!isset($desc) OR $this->empty_string($desc)) {
  4756. $desc = array();
  4757. }
  4758. if (!isset($up) OR $this->empty_string($up)) {
  4759. $up = -100;
  4760. }
  4761. if (!isset($ut) OR $this->empty_string($ut)) {
  4762. $ut = 50;
  4763. }
  4764. if (!isset($cw) OR $this->empty_string($cw)) {
  4765. $cw = array();
  4766. }
  4767. if (!isset($dw) OR $this->empty_string($dw)) {
  4768. // set default width
  4769. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4770. $dw = $desc['MissingWidth'];
  4771. } elseif (isset($cw[32])) {
  4772. $dw = $cw[32];
  4773. } else {
  4774. $dw = 600;
  4775. }
  4776. }
  4777. ++$this->numfonts;
  4778. if ($type == 'core') {
  4779. $name = $this->CoreFonts[$fontkey];
  4780. $subset = false;
  4781. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4782. $subset = false;
  4783. } elseif ($type == 'TrueTypeUnicode') {
  4784. $enc = 'Identity-H';
  4785. } elseif ($type == 'cidfont0') {
  4786. if ($this->pdfa_mode) {
  4787. $this->Error('All fonts must be embedded in PDF/A mode!');
  4788. }
  4789. } else {
  4790. $this->Error('Unknow font type: '.$type.'');
  4791. }
  4792. // set name if unset
  4793. if (!isset($name) OR empty($name)) {
  4794. $name = $fontkey;
  4795. }
  4796. // create artificial font style variations if missing (only works with non-embedded fonts)
  4797. if (($type != 'core') AND $missing_style) {
  4798. // style variations
  4799. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4800. $name .= $styles[$bistyle];
  4801. // artificial bold
  4802. if (strpos($bistyle, 'B') !== false) {
  4803. if (isset($desc['StemV'])) {
  4804. // from normal to bold
  4805. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4806. } else {
  4807. // bold
  4808. $desc['StemV'] = 123;
  4809. }
  4810. }
  4811. // artificial italic
  4812. if (strpos($bistyle, 'I') !== false) {
  4813. if (isset($desc['ItalicAngle'])) {
  4814. $desc['ItalicAngle'] -= 11;
  4815. } else {
  4816. $desc['ItalicAngle'] = -11;
  4817. }
  4818. if (isset($desc['Flags'])) {
  4819. $desc['Flags'] |= 64; //bit 7
  4820. } else {
  4821. $desc['Flags'] = 64;
  4822. }
  4823. }
  4824. }
  4825. // initialize subsetchars to contain default ASCII values (0-255)
  4826. $subsetchars = array_fill(0, 256, true);
  4827. $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));
  4828. if ($this->inxobj) {
  4829. // we are inside an XObject template
  4830. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4831. }
  4832. if (isset($diff) AND (!empty($diff))) {
  4833. //Search existing encodings
  4834. $d = 0;
  4835. $nb = count($this->diffs);
  4836. for ($i=1; $i <= $nb; ++$i) {
  4837. if ($this->diffs[$i] == $diff) {
  4838. $d = $i;
  4839. break;
  4840. }
  4841. }
  4842. if ($d == 0) {
  4843. $d = $nb + 1;
  4844. $this->diffs[$d] = $diff;
  4845. }
  4846. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4847. }
  4848. if (!$this->empty_string($file)) {
  4849. if (!isset($this->FontFiles[$file])) {
  4850. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4851. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4852. } elseif ($type != 'core') {
  4853. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4854. }
  4855. } else {
  4856. // update fontkeys that are sharing this font file
  4857. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4858. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4859. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4860. }
  4861. }
  4862. }
  4863. return $fontdata;
  4864. }
  4865. /**
  4866. * Sets the font used to print character strings.
  4867. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4868. * The method can be called before the first page is created and the font is retained from page to page.
  4869. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4870. * 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 />
  4871. * @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.
  4872. * @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.
  4873. * @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
  4874. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4875. * @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.
  4876. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4877. * @author Nicola Asuni
  4878. * @public
  4879. * @since 1.0
  4880. * @see AddFont(), SetFontSize()
  4881. */
  4882. public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default', $out=true) {
  4883. //Select a font; size given in points
  4884. if ($size == 0) {
  4885. $size = $this->FontSizePt;
  4886. }
  4887. // try to add font (if not already added)
  4888. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4889. $this->FontFamily = $fontdata['family'];
  4890. $this->FontStyle = $fontdata['style'];
  4891. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4892. $this->SetFontSize($size, $out);
  4893. }
  4894. /**
  4895. * Defines the size of the current font.
  4896. * @param $size (float) The font size in points.
  4897. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4898. * @public
  4899. * @since 1.0
  4900. * @see SetFont()
  4901. */
  4902. public function SetFontSize($size, $out=true) {
  4903. // font size in points
  4904. $this->FontSizePt = $size;
  4905. // font size in user units
  4906. $this->FontSize = $size / $this->k;
  4907. // calculate some font metrics
  4908. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4909. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4910. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4911. } else {
  4912. $font_height = $size * 1.219;
  4913. }
  4914. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4915. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4916. }
  4917. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4918. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4919. }
  4920. if (!isset($font_ascent) AND !isset($font_descent)) {
  4921. // core font
  4922. $font_ascent = 0.76 * $font_height;
  4923. $font_descent = $font_height - $font_ascent;
  4924. } elseif (!isset($font_descent)) {
  4925. $font_descent = $font_height - $font_ascent;
  4926. } elseif (!isset($font_ascent)) {
  4927. $font_ascent = $font_height - $font_descent;
  4928. }
  4929. $this->FontAscent = ($font_ascent / $this->k);
  4930. $this->FontDescent = ($font_descent / $this->k);
  4931. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4932. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4933. }
  4934. }
  4935. /**
  4936. * Return the font descent value
  4937. * @param $font (string) font name
  4938. * @param $style (string) font style
  4939. * @param $size (float) The size (in points)
  4940. * @return int font descent
  4941. * @public
  4942. * @author Nicola Asuni
  4943. * @since 4.9.003 (2010-03-30)
  4944. */
  4945. public function getFontDescent($font, $style='', $size=0) {
  4946. $fontdata = $this->AddFont($font, $style);
  4947. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4948. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4949. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4950. } else {
  4951. $descent = 1.219 * 0.24 * $size;
  4952. }
  4953. return ($descent / $this->k);
  4954. }
  4955. /**
  4956. * Return the font ascent value
  4957. * @param $font (string) font name
  4958. * @param $style (string) font style
  4959. * @param $size (float) The size (in points)
  4960. * @return int font ascent
  4961. * @public
  4962. * @author Nicola Asuni
  4963. * @since 4.9.003 (2010-03-30)
  4964. */
  4965. public function getFontAscent($font, $style='', $size=0) {
  4966. $fontdata = $this->AddFont($font, $style);
  4967. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4968. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4969. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4970. } else {
  4971. $ascent = 1.219 * 0.76 * $size;
  4972. }
  4973. return ($ascent / $this->k);
  4974. }
  4975. /**
  4976. * Defines the default monospaced font.
  4977. * @param $font (string) Font name.
  4978. * @public
  4979. * @since 4.5.025
  4980. */
  4981. public function SetDefaultMonospacedFont($font) {
  4982. $this->default_monospaced_font = $font;
  4983. }
  4984. /**
  4985. * 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 />
  4986. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4987. * @public
  4988. * @since 1.5
  4989. * @see Cell(), Write(), Image(), Link(), SetLink()
  4990. */
  4991. public function AddLink() {
  4992. //Create a new internal link
  4993. $n = count($this->links) + 1;
  4994. $this->links[$n] = array(0, 0);
  4995. return $n;
  4996. }
  4997. /**
  4998. * Defines the page and position a link points to.
  4999. * @param $link (int) The link identifier returned by AddLink()
  5000. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  5001. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  5002. * @public
  5003. * @since 1.5
  5004. * @see AddLink()
  5005. */
  5006. public function SetLink($link, $y=0, $page=-1) {
  5007. if ($y == -1) {
  5008. $y = $this->y;
  5009. }
  5010. if ($page == -1) {
  5011. $page = $this->page;
  5012. }
  5013. $this->links[$link] = array($page, $y);
  5014. }
  5015. /**
  5016. * Puts a link on a rectangular area of the page.
  5017. * 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.
  5018. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5019. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5020. * @param $w (float) Width of the rectangle
  5021. * @param $h (float) Height of the rectangle
  5022. * @param $link (mixed) URL or identifier returned by AddLink()
  5023. * @param $spaces (int) number of spaces on the text to link
  5024. * @public
  5025. * @since 1.5
  5026. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  5027. */
  5028. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5029. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5030. }
  5031. /**
  5032. * Puts a markup annotation on a rectangular area of the page.
  5033. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5034. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5035. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5036. * @param $w (float) Width of the rectangle
  5037. * @param $h (float) Height of the rectangle
  5038. * @param $text (string) annotation text or alternate content
  5039. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5040. * @param $spaces (int) number of spaces on the text to link
  5041. * @public
  5042. * @since 4.0.018 (2008-08-06)
  5043. */
  5044. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5045. if ($this->inxobj) {
  5046. // store parameters for later use on template
  5047. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5048. return;
  5049. }
  5050. if ($x === '') {
  5051. $x = $this->x;
  5052. }
  5053. if ($y === '') {
  5054. $y = $this->y;
  5055. }
  5056. // check page for no-write regions and adapt page margins if necessary
  5057. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5058. // recalculate coordinates to account for graphic transformations
  5059. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5060. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5061. $maxid = count($this->transfmatrix[$i]) - 1;
  5062. for ($j=$maxid; $j >= 0; --$j) {
  5063. $ctm = $this->transfmatrix[$i][$j];
  5064. if (isset($ctm['a'])) {
  5065. $x = $x * $this->k;
  5066. $y = ($this->h - $y) * $this->k;
  5067. $w = $w * $this->k;
  5068. $h = $h * $this->k;
  5069. // top left
  5070. $xt = $x;
  5071. $yt = $y;
  5072. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5073. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5074. // top right
  5075. $xt = $x + $w;
  5076. $yt = $y;
  5077. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5078. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5079. // bottom left
  5080. $xt = $x;
  5081. $yt = $y - $h;
  5082. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5083. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5084. // bottom right
  5085. $xt = $x + $w;
  5086. $yt = $y - $h;
  5087. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5088. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5089. // new coordinates (rectangle area)
  5090. $x = min($x1, $x2, $x3, $x4);
  5091. $y = max($y1, $y2, $y3, $y4);
  5092. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5093. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5094. $x = $x / $this->k;
  5095. $y = $this->h - ($y / $this->k);
  5096. }
  5097. }
  5098. }
  5099. }
  5100. if ($this->page <= 0) {
  5101. $page = 1;
  5102. } else {
  5103. $page = $this->page;
  5104. }
  5105. if (!isset($this->PageAnnots[$page])) {
  5106. $this->PageAnnots[$page] = array();
  5107. }
  5108. ++$this->n;
  5109. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5110. if (!$this->pdfa_mode) {
  5111. 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'])]))) {
  5112. ++$this->n;
  5113. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5114. }
  5115. }
  5116. // Add widgets annotation's icons
  5117. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5118. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5119. }
  5120. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5121. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5122. }
  5123. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5124. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5125. }
  5126. }
  5127. /**
  5128. * Embedd the attached files.
  5129. * @since 4.4.000 (2008-12-07)
  5130. * @protected
  5131. * @see Annotation()
  5132. */
  5133. protected function _putEmbeddedFiles() {
  5134. if ($this->pdfa_mode) {
  5135. // embedded files are not allowed in PDF/A mode
  5136. return;
  5137. }
  5138. reset($this->embeddedfiles);
  5139. foreach ($this->embeddedfiles as $filename => $filedata) {
  5140. $data = file_get_contents($filedata['file']);
  5141. $filter = '';
  5142. if ($this->compress) {
  5143. $data = gzcompress($data);
  5144. $filter = ' /Filter /FlateDecode';
  5145. }
  5146. $stream = $this->_getrawstream($data, $filedata['n']);
  5147. $out = $this->_getobj($filedata['n'])."\n";
  5148. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5149. $out .= ' stream'."\n".$stream."\n".'endstream';
  5150. $out .= "\n".'endobj';
  5151. $this->_out($out);
  5152. }
  5153. }
  5154. /**
  5155. * Prints a text cell at the specified position.
  5156. * This method allows to place a string precisely on the page.
  5157. * @param $x (float) Abscissa of the cell origin
  5158. * @param $y (float) Ordinate of the cell origin
  5159. * @param $txt (string) String to print
  5160. * @param $fstroke (int) outline size in user units (false = disable)
  5161. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5162. * @param $ffill (boolean) if true fills the text
  5163. * @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)))
  5164. * @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.
  5165. * @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>
  5166. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5167. * @param $link (mixed) URL or identifier returned by AddLink().
  5168. * @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.
  5169. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5170. * @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>
  5171. * @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>
  5172. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5173. * @public
  5174. * @since 1.0
  5175. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5176. */
  5177. 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) {
  5178. $textrendermode = $this->textrendermode;
  5179. $textstrokewidth = $this->textstrokewidth;
  5180. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5181. $this->SetXY($x, $y, $rtloff);
  5182. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5183. // restore previous rendering mode
  5184. $this->textrendermode = $textrendermode;
  5185. $this->textstrokewidth = $textstrokewidth;
  5186. }
  5187. /**
  5188. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5189. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5190. * This method is called automatically and should not be called directly by the application.
  5191. * @return boolean
  5192. * @public
  5193. * @since 1.4
  5194. * @see SetAutoPageBreak()
  5195. */
  5196. public function AcceptPageBreak() {
  5197. if ($this->num_columns > 1) {
  5198. // multi column mode
  5199. if ($this->current_column < ($this->num_columns - 1)) {
  5200. // go to next column
  5201. $this->selectColumn($this->current_column + 1);
  5202. } else {
  5203. // add a new page
  5204. $this->AddPage();
  5205. // set first column
  5206. $this->selectColumn(0);
  5207. }
  5208. // avoid page breaking from checkPageBreak()
  5209. return false;
  5210. }
  5211. return $this->AutoPageBreak;
  5212. }
  5213. /**
  5214. * Add page if needed.
  5215. * @param $h (float) Cell height. Default value: 0.
  5216. * @param $y (mixed) starting y position, leave empty for current position.
  5217. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5218. * @return boolean true in case of page break, false otherwise.
  5219. * @since 3.2.000 (2008-07-01)
  5220. * @protected
  5221. */
  5222. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5223. if ($this->empty_string($y)) {
  5224. $y = $this->y;
  5225. }
  5226. $current_page = $this->page;
  5227. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5228. if ($addpage) {
  5229. //Automatic page break
  5230. $x = $this->x;
  5231. $this->AddPage($this->CurOrientation);
  5232. $this->y = $this->tMargin;
  5233. $oldpage = $this->page - 1;
  5234. if ($this->rtl) {
  5235. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5236. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5237. } else {
  5238. $this->x = $x;
  5239. }
  5240. } else {
  5241. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5242. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5243. } else {
  5244. $this->x = $x;
  5245. }
  5246. }
  5247. }
  5248. return true;
  5249. }
  5250. if ($current_page != $this->page) {
  5251. // account for columns mode
  5252. return true;
  5253. }
  5254. return false;
  5255. }
  5256. /**
  5257. * Removes SHY characters from text.
  5258. * Unicode Data:<ul>
  5259. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5260. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5261. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5262. * <li>HTML Entity (named): "&amp;shy;"</li>
  5263. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5264. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5265. * <li>UTF-8 character: chr(194).chr(173)</li>
  5266. * </ul>
  5267. * @param $txt (string) input string
  5268. * @return string without SHY characters.
  5269. * @public
  5270. * @since (4.5.019) 2009-02-28
  5271. */
  5272. public function removeSHY($txt='') {
  5273. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5274. if (!$this->isunicode) {
  5275. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5276. }
  5277. return $txt;
  5278. }
  5279. /**
  5280. * 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 />
  5281. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5282. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5283. * @param $h (float) Cell height. Default value: 0.
  5284. * @param $txt (string) String to print. Default value: empty string.
  5285. * @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)))
  5286. * @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.
  5287. * @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>
  5288. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5289. * @param $link (mixed) URL or identifier returned by AddLink().
  5290. * @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.
  5291. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5292. * @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>
  5293. * @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>
  5294. * @public
  5295. * @since 1.0
  5296. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5297. */
  5298. 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') {
  5299. $prev_cell_margin = $this->cell_margin;
  5300. $prev_cell_padding = $this->cell_padding;
  5301. $this->adjustCellPadding($border);
  5302. if (!$ignore_min_height) {
  5303. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5304. if ($h < $min_cell_height) {
  5305. $h = $min_cell_height;
  5306. }
  5307. }
  5308. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5309. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5310. $this->cell_padding = $prev_cell_padding;
  5311. $this->cell_margin = $prev_cell_margin;
  5312. }
  5313. /**
  5314. * 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 />
  5315. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5316. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5317. * @param $h (float) Cell height. Default value: 0.
  5318. * @param $txt (string) String to print. Default value: empty string.
  5319. * @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)))
  5320. * @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.
  5321. * @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>
  5322. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5323. * @param $link (mixed) URL or identifier returned by AddLink().
  5324. * @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.
  5325. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5326. * @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>
  5327. * @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>
  5328. * @return string containing cell code
  5329. * @protected
  5330. * @since 1.0
  5331. * @see Cell()
  5332. */
  5333. 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') {
  5334. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5335. $txt = str_replace($this->unichr(160), ' ', $txt);
  5336. $prev_cell_margin = $this->cell_margin;
  5337. $prev_cell_padding = $this->cell_padding;
  5338. $txt = $this->removeSHY($txt);
  5339. $rs = ''; //string to be returned
  5340. $this->adjustCellPadding($border);
  5341. if (!$ignore_min_height) {
  5342. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5343. if ($h < $min_cell_height) {
  5344. $h = $min_cell_height;
  5345. }
  5346. }
  5347. $k = $this->k;
  5348. // check page for no-write regions and adapt page margins if necessary
  5349. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5350. if ($this->rtl) {
  5351. $x = $this->x - $this->cell_margin['R'];
  5352. } else {
  5353. $x = $this->x + $this->cell_margin['L'];
  5354. }
  5355. $y = $this->y + $this->cell_margin['T'];
  5356. $prev_font_stretching = $this->font_stretching;
  5357. $prev_font_spacing = $this->font_spacing;
  5358. // cell vertical alignment
  5359. switch ($calign) {
  5360. case 'A': {
  5361. // font top
  5362. switch ($valign) {
  5363. case 'T': {
  5364. // top
  5365. $y -= $this->cell_padding['T'];
  5366. break;
  5367. }
  5368. case 'B': {
  5369. // bottom
  5370. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5371. break;
  5372. }
  5373. default:
  5374. case 'C':
  5375. case 'M': {
  5376. // center
  5377. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5378. break;
  5379. }
  5380. }
  5381. break;
  5382. }
  5383. case 'L': {
  5384. // font baseline
  5385. switch ($valign) {
  5386. case 'T': {
  5387. // top
  5388. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5389. break;
  5390. }
  5391. case 'B': {
  5392. // bottom
  5393. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5394. break;
  5395. }
  5396. default:
  5397. case 'C':
  5398. case 'M': {
  5399. // center
  5400. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5401. break;
  5402. }
  5403. }
  5404. break;
  5405. }
  5406. case 'D': {
  5407. // font bottom
  5408. switch ($valign) {
  5409. case 'T': {
  5410. // top
  5411. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5412. break;
  5413. }
  5414. case 'B': {
  5415. // bottom
  5416. $y -= ($h - $this->cell_padding['B']);
  5417. break;
  5418. }
  5419. default:
  5420. case 'C':
  5421. case 'M': {
  5422. // center
  5423. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5424. break;
  5425. }
  5426. }
  5427. break;
  5428. }
  5429. case 'B': {
  5430. // cell bottom
  5431. $y -= $h;
  5432. break;
  5433. }
  5434. case 'C':
  5435. case 'M': {
  5436. // cell center
  5437. $y -= ($h / 2);
  5438. break;
  5439. }
  5440. default:
  5441. case 'T': {
  5442. // cell top
  5443. break;
  5444. }
  5445. }
  5446. // text vertical alignment
  5447. switch ($valign) {
  5448. case 'T': {
  5449. // top
  5450. $yt = $y + $this->cell_padding['T'];
  5451. break;
  5452. }
  5453. case 'B': {
  5454. // bottom
  5455. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5456. break;
  5457. }
  5458. default:
  5459. case 'C':
  5460. case 'M': {
  5461. // center
  5462. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5463. break;
  5464. }
  5465. }
  5466. $basefonty = $yt + $this->FontAscent;
  5467. if ($this->empty_string($w) OR ($w <= 0)) {
  5468. if ($this->rtl) {
  5469. $w = $x - $this->lMargin;
  5470. } else {
  5471. $w = $this->w - $this->rMargin - $x;
  5472. }
  5473. }
  5474. $s = '';
  5475. // fill and borders
  5476. if (is_string($border) AND (strlen($border) == 4)) {
  5477. // full border
  5478. $border = 1;
  5479. }
  5480. if ($fill OR ($border == 1)) {
  5481. if ($fill) {
  5482. $op = ($border == 1) ? 'B' : 'f';
  5483. } else {
  5484. $op = 'S';
  5485. }
  5486. if ($this->rtl) {
  5487. $xk = (($x - $w) * $k);
  5488. } else {
  5489. $xk = ($x * $k);
  5490. }
  5491. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5492. }
  5493. // draw borders
  5494. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5495. if ($txt != '') {
  5496. $txt2 = $txt;
  5497. if ($this->isunicode) {
  5498. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5499. $txt2 = $this->UTF8ToLatin1($txt2);
  5500. } else {
  5501. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5502. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5503. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5504. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5505. // NOTE: this doesn't work with HTML justification
  5506. // Symbols that could overlap on the font top (only works in LTR)
  5507. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5508. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5509. $numchars = count($unicode); // number of chars
  5510. $unik = 0;
  5511. $uniblock = array();
  5512. $uniblock[$unik] = array();
  5513. $uniblock[$unik][] = $unicode[0];
  5514. // resolve overlapping conflicts by splitting the string in several parts
  5515. for ($i = 1; $i < $numchars; ++$i) {
  5516. // check if symbols overlaps at top
  5517. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5518. // move symbols to another array
  5519. ++$unik;
  5520. $uniblock[$unik] = array();
  5521. $uniblock[$unik][] = $unicode[$i];
  5522. ++$unik;
  5523. $uniblock[$unik] = array();
  5524. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5525. } else {
  5526. $uniblock[$unik][] = $unicode[$i];
  5527. }
  5528. }
  5529. // ---- END OF Fix for bug #2977340
  5530. }
  5531. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5532. }
  5533. }
  5534. $txt2 = $this->_escape($txt2);
  5535. // get current text width (considering general font stretching and spacing)
  5536. $txwidth = $this->GetStringWidth($txt);
  5537. $width = $txwidth;
  5538. // check for stretch mode
  5539. if ($stretch > 0) {
  5540. // calculate ratio between cell width and text width
  5541. if ($width <= 0) {
  5542. $ratio = 1;
  5543. } else {
  5544. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5545. }
  5546. // check if stretching is required
  5547. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5548. // the text will be stretched to fit cell width
  5549. if ($stretch > 2) {
  5550. // set new character spacing
  5551. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5552. } else {
  5553. // set new horizontal stretching
  5554. $this->font_stretching *= $ratio;
  5555. }
  5556. // recalculate text width (the text fills the entire cell)
  5557. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5558. // reset alignment
  5559. $align = '';
  5560. }
  5561. }
  5562. if ($this->font_stretching != 100) {
  5563. // apply font stretching
  5564. $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
  5565. }
  5566. if ($this->font_spacing != 0) {
  5567. // increase/decrease font spacing
  5568. $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
  5569. }
  5570. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5571. $s .= 'q '.$this->TextColor.' ';
  5572. }
  5573. // rendering mode
  5574. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  5575. // count number of spaces
  5576. $ns = substr_count($txt, chr(32));
  5577. // Justification
  5578. $spacewidth = 0;
  5579. if (($align == 'J') AND ($ns > 0)) {
  5580. if ($this->isUnicodeFont()) {
  5581. // get string width without spaces
  5582. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5583. // calculate average space width
  5584. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5585. if ($this->font_stretching != 100) {
  5586. // word spacing is affected by stretching
  5587. $spacewidth /= ($this->font_stretching / 100);
  5588. }
  5589. // set word position to be used with TJ operator
  5590. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  5591. $unicode_justification = true;
  5592. } else {
  5593. // get string width
  5594. $width = $txwidth;
  5595. // new space width
  5596. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5597. if ($this->font_stretching != 100) {
  5598. // word spacing (Tw) is affected by stretching
  5599. $spacewidth /= ($this->font_stretching / 100);
  5600. }
  5601. // set word spacing
  5602. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  5603. }
  5604. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5605. }
  5606. // replace carriage return characters
  5607. $txt2 = str_replace("\r", ' ', $txt2);
  5608. switch ($align) {
  5609. case 'C': {
  5610. $dx = ($w - $width) / 2;
  5611. break;
  5612. }
  5613. case 'R': {
  5614. if ($this->rtl) {
  5615. $dx = $this->cell_padding['R'];
  5616. } else {
  5617. $dx = $w - $width - $this->cell_padding['R'];
  5618. }
  5619. break;
  5620. }
  5621. case 'L': {
  5622. if ($this->rtl) {
  5623. $dx = $w - $width - $this->cell_padding['L'];
  5624. } else {
  5625. $dx = $this->cell_padding['L'];
  5626. }
  5627. break;
  5628. }
  5629. case 'J':
  5630. default: {
  5631. if ($this->rtl) {
  5632. $dx = $this->cell_padding['R'];
  5633. } else {
  5634. $dx = $this->cell_padding['L'];
  5635. }
  5636. break;
  5637. }
  5638. }
  5639. if ($this->rtl) {
  5640. $xdx = $x - $dx - $width;
  5641. } else {
  5642. $xdx = $x + $dx;
  5643. }
  5644. $xdk = $xdx * $k;
  5645. // print text
  5646. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5647. if (isset($uniblock)) {
  5648. // print overlapping characters as separate string
  5649. $xshift = 0; // horizontal shift
  5650. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5651. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5652. foreach ($uniblock as $uk => $uniarr) {
  5653. if (($uk % 2) == 0) {
  5654. // x space to skip
  5655. if ($spacewidth != 0) {
  5656. // justification shift
  5657. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5658. }
  5659. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5660. } else {
  5661. // character to print
  5662. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5663. $topchr = $this->_escape($topchr);
  5664. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5665. }
  5666. }
  5667. }
  5668. if ($this->underline) {
  5669. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5670. }
  5671. if ($this->linethrough) {
  5672. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5673. }
  5674. if ($this->overline) {
  5675. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5676. }
  5677. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5678. $s .= ' Q';
  5679. }
  5680. if ($link) {
  5681. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5682. }
  5683. }
  5684. // output cell
  5685. if ($s) {
  5686. // output cell
  5687. $rs .= $s;
  5688. if ($this->font_spacing != 0) {
  5689. // reset font spacing mode
  5690. $rs .= ' BT 0 Tc ET';
  5691. }
  5692. if ($this->font_stretching != 100) {
  5693. // reset font stretching mode
  5694. $rs .= ' BT 100 Tz ET';
  5695. }
  5696. }
  5697. // reset word spacing
  5698. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5699. $rs .= ' BT 0 Tw ET';
  5700. }
  5701. // reset stretching and spacing
  5702. $this->font_stretching = $prev_font_stretching;
  5703. $this->font_spacing = $prev_font_spacing;
  5704. $this->lasth = $h;
  5705. if ($ln > 0) {
  5706. //Go to the beginning of the next line
  5707. $this->y = $y + $h + $this->cell_margin['B'];
  5708. if ($ln == 1) {
  5709. if ($this->rtl) {
  5710. $this->x = $this->w - $this->rMargin;
  5711. } else {
  5712. $this->x = $this->lMargin;
  5713. }
  5714. }
  5715. } else {
  5716. // go left or right by case
  5717. if ($this->rtl) {
  5718. $this->x = $x - $w - $this->cell_margin['L'];
  5719. } else {
  5720. $this->x = $x + $w + $this->cell_margin['R'];
  5721. }
  5722. }
  5723. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5724. $rs = $gstyles.$rs;
  5725. $this->cell_padding = $prev_cell_padding;
  5726. $this->cell_margin = $prev_cell_margin;
  5727. return $rs;
  5728. }
  5729. /**
  5730. * Returns the code to draw the cell border
  5731. * @param $x (float) X coordinate.
  5732. * @param $y (float) Y coordinate.
  5733. * @param $w (float) Cell width.
  5734. * @param $h (float) Cell height.
  5735. * @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)))
  5736. * @return string containing cell border code
  5737. * @protected
  5738. * @see SetLineStyle()
  5739. * @since 5.7.000 (2010-08-02)
  5740. */
  5741. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5742. $s = ''; // string to be returned
  5743. if (empty($brd)) {
  5744. return $s;
  5745. }
  5746. if ($brd == 1) {
  5747. $brd = array('LRTB' => true);
  5748. }
  5749. // calculate coordinates for border
  5750. $k = $this->k;
  5751. if ($this->rtl) {
  5752. $xeL = ($x - $w) * $k;
  5753. $xeR = $x * $k;
  5754. } else {
  5755. $xeL = $x * $k;
  5756. $xeR = ($x + $w) * $k;
  5757. }
  5758. $yeL = (($this->h - ($y + $h)) * $k);
  5759. $yeT = (($this->h - $y) * $k);
  5760. $xeT = $xeL;
  5761. $xeB = $xeR;
  5762. $yeR = $yeT;
  5763. $yeB = $yeL;
  5764. if (is_string($brd)) {
  5765. // convert string to array
  5766. $slen = strlen($brd);
  5767. $newbrd = array();
  5768. for ($i = 0; $i < $slen; ++$i) {
  5769. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5770. }
  5771. $brd = $newbrd;
  5772. }
  5773. if (isset($brd['mode'])) {
  5774. $mode = $brd['mode'];
  5775. unset($brd['mode']);
  5776. } else {
  5777. $mode = 'normal';
  5778. }
  5779. foreach ($brd as $border => $style) {
  5780. if (is_array($style) AND !empty($style)) {
  5781. // apply border style
  5782. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5783. $s .= $this->SetLineStyle($style, true)."\n";
  5784. }
  5785. switch ($mode) {
  5786. case 'ext': {
  5787. $off = (($this->LineWidth / 2) * $k);
  5788. $xL = $xeL - $off;
  5789. $xR = $xeR + $off;
  5790. $yT = $yeT + $off;
  5791. $yL = $yeL - $off;
  5792. $xT = $xL;
  5793. $xB = $xR;
  5794. $yR = $yT;
  5795. $yB = $yL;
  5796. $w += $this->LineWidth;
  5797. $h += $this->LineWidth;
  5798. break;
  5799. }
  5800. case 'int': {
  5801. $off = ($this->LineWidth / 2) * $k;
  5802. $xL = $xeL + $off;
  5803. $xR = $xeR - $off;
  5804. $yT = $yeT - $off;
  5805. $yL = $yeL + $off;
  5806. $xT = $xL;
  5807. $xB = $xR;
  5808. $yR = $yT;
  5809. $yB = $yL;
  5810. $w -= $this->LineWidth;
  5811. $h -= $this->LineWidth;
  5812. break;
  5813. }
  5814. case 'normal':
  5815. default: {
  5816. $xL = $xeL;
  5817. $xT = $xeT;
  5818. $xB = $xeB;
  5819. $xR = $xeR;
  5820. $yL = $yeL;
  5821. $yT = $yeT;
  5822. $yB = $yeB;
  5823. $yR = $yeR;
  5824. break;
  5825. }
  5826. }
  5827. // draw borders by case
  5828. if (strlen($border) == 4) {
  5829. $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5830. } elseif (strlen($border) == 3) {
  5831. if (strpos($border,'B') === false) { // LTR
  5832. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5833. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5834. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5835. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5836. $s .= 'S ';
  5837. } elseif (strpos($border,'L') === false) { // TRB
  5838. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5839. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5840. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5841. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5842. $s .= 'S ';
  5843. } elseif (strpos($border,'T') === false) { // RBL
  5844. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5845. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5846. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5847. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5848. $s .= 'S ';
  5849. } elseif (strpos($border,'R') === false) { // BLT
  5850. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5851. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5852. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5853. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5854. $s .= 'S ';
  5855. }
  5856. } elseif (strlen($border) == 2) {
  5857. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5858. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5859. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5860. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5861. $s .= 'S ';
  5862. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5863. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5864. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5865. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5866. $s .= 'S ';
  5867. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5868. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5869. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5870. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5871. $s .= 'S ';
  5872. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5873. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5874. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5875. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5876. $s .= 'S ';
  5877. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5878. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5879. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5880. $s .= 'S ';
  5881. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5882. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5883. $s .= 'S ';
  5884. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5885. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5886. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5887. $s .= 'S ';
  5888. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5889. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5890. $s .= 'S ';
  5891. }
  5892. } else { // strlen($border) == 1
  5893. if (strpos($border,'L') !== false) { // L
  5894. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5895. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5896. $s .= 'S ';
  5897. } elseif (strpos($border,'T') !== false) { // T
  5898. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5899. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5900. $s .= 'S ';
  5901. } elseif (strpos($border,'R') !== false) { // R
  5902. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5903. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5904. $s .= 'S ';
  5905. } elseif (strpos($border,'B') !== false) { // B
  5906. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5907. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5908. $s .= 'S ';
  5909. }
  5910. }
  5911. if (is_array($style) AND !empty($style)) {
  5912. // reset border style to previous value
  5913. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5914. }
  5915. }
  5916. return $s;
  5917. }
  5918. /**
  5919. * This method allows printing text with line breaks.
  5920. * 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 />
  5921. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5922. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5923. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5924. * @param $txt (string) String to print
  5925. * @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)))
  5926. * @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>
  5927. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5928. * @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>
  5929. * @param $x (float) x position in user units
  5930. * @param $y (float) y position in user units
  5931. * @param $reseth (boolean) if true reset the last cell height (default true).
  5932. * @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.
  5933. * @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.
  5934. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5935. * @param $maxh (float) maximum height. It should be >= $h and less than remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
  5936. * @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.
  5937. * @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).
  5938. * @return int Return the number of cells or 1 for html mode.
  5939. * @public
  5940. * @since 1.3
  5941. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5942. */
  5943. 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) {
  5944. $prev_cell_margin = $this->cell_margin;
  5945. $prev_cell_padding = $this->cell_padding;
  5946. // adjust internal padding
  5947. $this->adjustCellPadding($border);
  5948. $mc_padding = $this->cell_padding;
  5949. $mc_margin = $this->cell_margin;
  5950. $this->cell_padding['T'] = 0;
  5951. $this->cell_padding['B'] = 0;
  5952. $this->setCellMargins(0, 0, 0, 0);
  5953. if ($this->empty_string($this->lasth) OR $reseth) {
  5954. // reset row height
  5955. $this->resetLastH();
  5956. }
  5957. if (!$this->empty_string($y)) {
  5958. $this->SetY($y);
  5959. } else {
  5960. $y = $this->GetY();
  5961. }
  5962. $resth = 0;
  5963. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5964. // spit cell in more pages/columns
  5965. $newh = ($this->PageBreakTrigger - $y);
  5966. $resth = ($h - $newh); // cell to be printed on the next page/column
  5967. $h = $newh;
  5968. }
  5969. // get current page number
  5970. $startpage = $this->page;
  5971. // get current column
  5972. $startcolumn = $this->current_column;
  5973. if (!$this->empty_string($x)) {
  5974. $this->SetX($x);
  5975. } else {
  5976. $x = $this->GetX();
  5977. }
  5978. // check page for no-write regions and adapt page margins if necessary
  5979. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5980. // apply margins
  5981. $oy = $y + $mc_margin['T'];
  5982. if ($this->rtl) {
  5983. $ox = $this->w - $x - $mc_margin['R'];
  5984. } else {
  5985. $ox = $x + $mc_margin['L'];
  5986. }
  5987. $this->x = $ox;
  5988. $this->y = $oy;
  5989. // set width
  5990. if ($this->empty_string($w) OR ($w <= 0)) {
  5991. if ($this->rtl) {
  5992. $w = $this->x - $this->lMargin - $mc_margin['L'];
  5993. } else {
  5994. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  5995. }
  5996. }
  5997. // store original margin values
  5998. $lMargin = $this->lMargin;
  5999. $rMargin = $this->rMargin;
  6000. if ($this->rtl) {
  6001. $this->rMargin = $this->w - $this->x;
  6002. $this->lMargin = $this->x - $w;
  6003. } else {
  6004. $this->lMargin = $this->x;
  6005. $this->rMargin = $this->w - $this->x - $w;
  6006. }
  6007. if ($autopadding) {
  6008. // add top padding
  6009. $this->y += $mc_padding['T'];
  6010. }
  6011. if ($ishtml) { // ******* Write HTML text
  6012. $this->writeHTML($txt, true, false, $reseth, true, $align);
  6013. $nl = 1;
  6014. } else { // ******* Write simple text
  6015. $prev_FontSizePt = $this->FontSizePt;
  6016. // vertical alignment
  6017. if ($maxh > 0) {
  6018. // get text height
  6019. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6020. if ($fitcell) {
  6021. // try to reduce font size to fit text on cell (use a quick search algorithm)
  6022. $fmin = 1;
  6023. $fmax = $this->FontSizePt;
  6024. $prev_text_height = $text_height;
  6025. $maxit = 100; // max number of iterations
  6026. while ($maxit > 0) {
  6027. $fmid = (($fmax + $fmin) / 2);
  6028. $this->SetFontSize($fmid, false);
  6029. $this->resetLastH();
  6030. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6031. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6032. break;
  6033. } elseif ($text_height < $maxh) {
  6034. $fmin = $fmid;
  6035. } else {
  6036. $fmax = $fmid;
  6037. }
  6038. --$maxit;
  6039. }
  6040. $this->SetFontSize($this->FontSizePt);
  6041. }
  6042. if ($text_height < $maxh) {
  6043. if ($valign == 'M') {
  6044. // text vertically centered
  6045. $this->y += (($maxh - $text_height) / 2);
  6046. } elseif ($valign == 'B') {
  6047. // text vertically aligned on bottom
  6048. $this->y += ($maxh - $text_height);
  6049. }
  6050. }
  6051. }
  6052. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6053. if ($fitcell) {
  6054. // restore font size
  6055. $this->SetFontSize($prev_FontSizePt);
  6056. }
  6057. }
  6058. if ($autopadding) {
  6059. // add bottom padding
  6060. $this->y += $mc_padding['B'];
  6061. }
  6062. // Get end-of-text Y position
  6063. $currentY = $this->y;
  6064. // get latest page number
  6065. $endpage = $this->page;
  6066. if ($resth > 0) {
  6067. $skip = ($endpage - $startpage);
  6068. $tmpresth = $resth;
  6069. while ($tmpresth > 0) {
  6070. if ($skip <= 0) {
  6071. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6072. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6073. }
  6074. if ($this->num_columns > 1) {
  6075. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6076. } else {
  6077. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6078. }
  6079. --$skip;
  6080. }
  6081. $currentY = $this->y;
  6082. $endpage = $this->page;
  6083. }
  6084. // get latest column
  6085. $endcolumn = $this->current_column;
  6086. if ($this->num_columns == 0) {
  6087. $this->num_columns = 1;
  6088. }
  6089. // get border modes
  6090. $border_start = $this->getBorderMode($border, $position='start');
  6091. $border_end = $this->getBorderMode($border, $position='end');
  6092. $border_middle = $this->getBorderMode($border, $position='middle');
  6093. // design borders around HTML cells.
  6094. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6095. $ccode = '';
  6096. $this->setPage($page);
  6097. if ($this->num_columns < 2) {
  6098. // single-column mode
  6099. $this->SetX($x);
  6100. $this->y = $this->tMargin;
  6101. }
  6102. // account for margin changes
  6103. if ($page > $startpage) {
  6104. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6105. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6106. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6107. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6108. }
  6109. }
  6110. if ($startpage == $endpage) {
  6111. // single page
  6112. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6113. $this->selectColumn($column);
  6114. if ($this->rtl) {
  6115. $this->x -= $mc_margin['R'];
  6116. } else {
  6117. $this->x += $mc_margin['L'];
  6118. }
  6119. if ($startcolumn == $endcolumn) { // single column
  6120. $cborder = $border;
  6121. $h = max($h, ($currentY - $oy));
  6122. $this->y = $oy;
  6123. } elseif ($column == $startcolumn) { // first column
  6124. $cborder = $border_start;
  6125. $this->y = $oy;
  6126. $h = $this->h - $this->y - $this->bMargin;
  6127. } elseif ($column == $endcolumn) { // end column
  6128. $cborder = $border_end;
  6129. $h = $currentY - $this->y;
  6130. if ($resth > $h) {
  6131. $h = $resth;
  6132. }
  6133. } else { // middle column
  6134. $cborder = $border_middle;
  6135. $h = $this->h - $this->y - $this->bMargin;
  6136. $resth -= $h;
  6137. }
  6138. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6139. } // end for each column
  6140. } elseif ($page == $startpage) { // first page
  6141. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6142. $this->selectColumn($column);
  6143. if ($this->rtl) {
  6144. $this->x -= $mc_margin['R'];
  6145. } else {
  6146. $this->x += $mc_margin['L'];
  6147. }
  6148. if ($column == $startcolumn) { // first column
  6149. $cborder = $border_start;
  6150. $this->y = $oy;
  6151. $h = $this->h - $this->y - $this->bMargin;
  6152. } else { // middle column
  6153. $cborder = $border_middle;
  6154. $h = $this->h - $this->y - $this->bMargin;
  6155. $resth -= $h;
  6156. }
  6157. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6158. } // end for each column
  6159. } elseif ($page == $endpage) { // last page
  6160. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6161. $this->selectColumn($column);
  6162. if ($this->rtl) {
  6163. $this->x -= $mc_margin['R'];
  6164. } else {
  6165. $this->x += $mc_margin['L'];
  6166. }
  6167. if ($column == $endcolumn) {
  6168. // end column
  6169. $cborder = $border_end;
  6170. $h = $currentY - $this->y;
  6171. if ($resth > $h) {
  6172. $h = $resth;
  6173. }
  6174. } else {
  6175. // middle column
  6176. $cborder = $border_middle;
  6177. $h = $this->h - $this->y - $this->bMargin;
  6178. $resth -= $h;
  6179. }
  6180. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6181. } // end for each column
  6182. } else { // middle page
  6183. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6184. $this->selectColumn($column);
  6185. if ($this->rtl) {
  6186. $this->x -= $mc_margin['R'];
  6187. } else {
  6188. $this->x += $mc_margin['L'];
  6189. }
  6190. $cborder = $border_middle;
  6191. $h = $this->h - $this->y - $this->bMargin;
  6192. $resth -= $h;
  6193. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6194. } // end for each column
  6195. }
  6196. if ($cborder OR $fill) {
  6197. $offsetlen = strlen($ccode);
  6198. // draw border and fill
  6199. if ($this->inxobj) {
  6200. // we are inside an XObject template
  6201. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6202. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6203. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6204. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6205. } else {
  6206. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6207. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6208. }
  6209. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6210. $pstart = substr($pagebuff, 0, $pagemark);
  6211. $pend = substr($pagebuff, $pagemark);
  6212. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6213. } else {
  6214. if (end($this->transfmrk[$this->page]) !== false) {
  6215. $pagemarkkey = key($this->transfmrk[$this->page]);
  6216. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6217. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6218. } elseif ($this->InFooter) {
  6219. $pagemark = $this->footerpos[$this->page];
  6220. $this->footerpos[$this->page] += $offsetlen;
  6221. } else {
  6222. $pagemark = $this->intmrk[$this->page];
  6223. $this->intmrk[$this->page] += $offsetlen;
  6224. }
  6225. $pagebuff = $this->getPageBuffer($this->page);
  6226. $pstart = substr($pagebuff, 0, $pagemark);
  6227. $pend = substr($pagebuff, $pagemark);
  6228. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6229. }
  6230. }
  6231. } // end for each page
  6232. // Get end-of-cell Y position
  6233. $currentY = $this->GetY();
  6234. // restore previous values
  6235. if ($this->num_columns > 1) {
  6236. $this->selectColumn();
  6237. } else {
  6238. // restore original margins
  6239. $this->lMargin = $lMargin;
  6240. $this->rMargin = $rMargin;
  6241. if ($this->page > $startpage) {
  6242. // check for margin variations between pages (i.e. booklet mode)
  6243. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6244. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6245. if (($dl != 0) OR ($dr != 0)) {
  6246. $this->lMargin += $dl;
  6247. $this->rMargin += $dr;
  6248. }
  6249. }
  6250. }
  6251. if ($ln > 0) {
  6252. //Go to the beginning of the next line
  6253. $this->SetY($currentY + $mc_margin['B']);
  6254. if ($ln == 2) {
  6255. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6256. }
  6257. } else {
  6258. // go left or right by case
  6259. $this->setPage($startpage);
  6260. $this->y = $y;
  6261. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6262. }
  6263. $this->setContentMark();
  6264. $this->cell_padding = $prev_cell_padding;
  6265. $this->cell_margin = $prev_cell_margin;
  6266. return $nl;
  6267. }
  6268. /**
  6269. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6270. * @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)))
  6271. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6272. * @return border mode array
  6273. * @protected
  6274. * @since 4.4.002 (2008-12-09)
  6275. */
  6276. protected function getBorderMode($brd, $position='start') {
  6277. if ((!$this->opencell) OR empty($brd)) {
  6278. return $brd;
  6279. }
  6280. if ($brd == 1) {
  6281. $brd = 'LTRB';
  6282. }
  6283. if (is_string($brd)) {
  6284. // convert string to array
  6285. $slen = strlen($brd);
  6286. $newbrd = array();
  6287. for ($i = 0; $i < $slen; ++$i) {
  6288. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6289. }
  6290. $brd = $newbrd;
  6291. }
  6292. foreach ($brd as $border => $style) {
  6293. switch ($position) {
  6294. case 'start': {
  6295. if (strpos($border, 'B') !== false) {
  6296. // remove bottom line
  6297. $newkey = str_replace('B', '', $border);
  6298. if (strlen($newkey) > 0) {
  6299. $brd[$newkey] = $style;
  6300. }
  6301. unset($brd[$border]);
  6302. }
  6303. break;
  6304. }
  6305. case 'middle': {
  6306. if (strpos($border, 'B') !== false) {
  6307. // remove bottom line
  6308. $newkey = str_replace('B', '', $border);
  6309. if (strlen($newkey) > 0) {
  6310. $brd[$newkey] = $style;
  6311. }
  6312. unset($brd[$border]);
  6313. $border = $newkey;
  6314. }
  6315. if (strpos($border, 'T') !== false) {
  6316. // remove bottom line
  6317. $newkey = str_replace('T', '', $border);
  6318. if (strlen($newkey) > 0) {
  6319. $brd[$newkey] = $style;
  6320. }
  6321. unset($brd[$border]);
  6322. }
  6323. break;
  6324. }
  6325. case 'end': {
  6326. if (strpos($border, 'T') !== false) {
  6327. // remove bottom line
  6328. $newkey = str_replace('T', '', $border);
  6329. if (strlen($newkey) > 0) {
  6330. $brd[$newkey] = $style;
  6331. }
  6332. unset($brd[$border]);
  6333. }
  6334. break;
  6335. }
  6336. }
  6337. }
  6338. return $brd;
  6339. }
  6340. /**
  6341. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6342. * @param $txt (string) String for calculating his height
  6343. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6344. * @param $reseth (boolean) if true reset the last cell height (default false).
  6345. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6346. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6347. * @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)))
  6348. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6349. * @author Alexander Escalona Fernández, Nicola Asuni
  6350. * @public
  6351. * @since 4.5.011
  6352. */
  6353. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6354. if ($txt === '') {
  6355. // empty string
  6356. return 1;
  6357. }
  6358. // adjust internal padding
  6359. $prev_cell_padding = $this->cell_padding;
  6360. $prev_lasth = $this->lasth;
  6361. if (is_array($cellpadding)) {
  6362. $this->cell_padding = $cellpadding;
  6363. }
  6364. $this->adjustCellPadding($border);
  6365. if ($this->empty_string($w) OR ($w <= 0)) {
  6366. if ($this->rtl) {
  6367. $w = $this->x - $this->lMargin;
  6368. } else {
  6369. $w = $this->w - $this->rMargin - $this->x;
  6370. }
  6371. }
  6372. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6373. if ($reseth) {
  6374. // reset row height
  6375. $this->resetLastH();
  6376. }
  6377. $lines = 1;
  6378. $sum = 0;
  6379. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6380. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6381. $length = count($chars);
  6382. $lastSeparator = -1;
  6383. for ($i = 0; $i < $length; ++$i) {
  6384. $charWidth = $charsWidth[$i];
  6385. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6386. $lastSeparator = $i;
  6387. }
  6388. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6389. ++$lines;
  6390. if ($chars[$i] == 10) {
  6391. $lastSeparator = -1;
  6392. $sum = 0;
  6393. } elseif ($lastSeparator != -1) {
  6394. $i = $lastSeparator;
  6395. $lastSeparator = -1;
  6396. $sum = 0;
  6397. } else {
  6398. $sum = $charWidth;
  6399. }
  6400. } else {
  6401. $sum += $charWidth;
  6402. }
  6403. }
  6404. if ($chars[($length - 1)] == 10) {
  6405. --$lines;
  6406. }
  6407. $this->cell_padding = $prev_cell_padding;
  6408. $this->lasth = $prev_lasth;
  6409. return $lines;
  6410. }
  6411. /**
  6412. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6413. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6414. * @pre
  6415. * // store current object
  6416. * $pdf->startTransaction();
  6417. * // store starting values
  6418. * $start_y = $pdf->GetY();
  6419. * $start_page = $pdf->getPage();
  6420. * // call your printing functions with your parameters
  6421. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6422. * $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);
  6423. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6424. * // get the new Y
  6425. * $end_y = $pdf->GetY();
  6426. * $end_page = $pdf->getPage();
  6427. * // calculate height
  6428. * $height = 0;
  6429. * if ($end_page == $start_page) {
  6430. * $height = $end_y - $start_y;
  6431. * } else {
  6432. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6433. * $this->setPage($page);
  6434. * if ($page == $start_page) {
  6435. * // first page
  6436. * $height = $this->h - $start_y - $this->bMargin;
  6437. * } elseif ($page == $end_page) {
  6438. * // last page
  6439. * $height = $end_y - $this->tMargin;
  6440. * } else {
  6441. * $height = $this->h - $this->tMargin - $this->bMargin;
  6442. * }
  6443. * }
  6444. * }
  6445. * // restore previous object
  6446. * $pdf = $pdf->rollbackTransaction();
  6447. *
  6448. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6449. * @param $txt (string) String for calculating his height
  6450. * @param $reseth (boolean) if true reset the last cell height (default false).
  6451. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6452. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6453. * @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)))
  6454. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6455. * @author Nicola Asuni, Alexander Escalona Fernández
  6456. * @public
  6457. */
  6458. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6459. // adjust internal padding
  6460. $prev_cell_padding = $this->cell_padding;
  6461. $prev_lasth = $this->lasth;
  6462. if (is_array($cellpadding)) {
  6463. $this->cell_padding = $cellpadding;
  6464. }
  6465. $this->adjustCellPadding($border);
  6466. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6467. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6468. if ($autopadding) {
  6469. // add top and bottom padding
  6470. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6471. }
  6472. $this->cell_padding = $prev_cell_padding;
  6473. $this->lasth = $prev_lasth;
  6474. return $height;
  6475. }
  6476. /**
  6477. * This method prints text from the current position.<br />
  6478. * @param $h (float) Line height
  6479. * @param $txt (string) String to print
  6480. * @param $link (mixed) URL or identifier returned by AddLink()
  6481. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6482. * @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>
  6483. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6484. * @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.
  6485. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6486. * @param $firstblock (boolean) if true the string is the starting of a line.
  6487. * @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less than remaining space to the bottom of the page, or 0 for disable this feature.
  6488. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6489. * @param $margin (array) margin array of the parent container
  6490. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6491. * @public
  6492. * @since 1.5
  6493. */
  6494. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6495. // check page for no-write regions and adapt page margins if necessary
  6496. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6497. if (strlen($txt) == 0) {
  6498. // fix empty text
  6499. $txt = ' ';
  6500. }
  6501. if ($margin === '') {
  6502. // set default margins
  6503. $margin = $this->cell_margin;
  6504. }
  6505. // remove carriage returns
  6506. $s = str_replace("\r", '', $txt);
  6507. // check if string contains arabic text
  6508. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6509. $arabic = true;
  6510. } else {
  6511. $arabic = false;
  6512. }
  6513. // check if string contains RTL text
  6514. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6515. $rtlmode = true;
  6516. } else {
  6517. $rtlmode = false;
  6518. }
  6519. // get a char width
  6520. $chrwidth = $this->GetCharWidth(46); // dot character
  6521. // get array of unicode values
  6522. $chars = $this->UTF8StringToArray($s);
  6523. // get array of chars
  6524. $uchars = $this->UTF8ArrayToUniArray($chars);
  6525. // get the number of characters
  6526. $nb = count($chars);
  6527. // replacement for SHY character (minus symbol)
  6528. $shy_replacement = 45;
  6529. $shy_replacement_char = $this->unichr($shy_replacement);
  6530. // widht for SHY replacement
  6531. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6532. // max Y
  6533. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6534. // page width
  6535. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6536. // calculate remaining line width ($w)
  6537. if ($this->rtl) {
  6538. $w = $this->x - $this->lMargin;
  6539. } else {
  6540. $w = $this->w - $this->rMargin - $this->x;
  6541. }
  6542. // max column width
  6543. $wmax = $w - $wadj;
  6544. if (!$firstline) {
  6545. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6546. }
  6547. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6548. // a single character do not fit on column
  6549. return '';
  6550. }
  6551. // minimum row height
  6552. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6553. $start_page = $this->page;
  6554. $i = 0; // character position
  6555. $j = 0; // current starting position
  6556. $sep = -1; // position of the last blank space
  6557. $shy = false; // true if the last blank is a soft hypen (SHY)
  6558. $l = 0; // current string length
  6559. $nl = 0; //number of lines
  6560. $linebreak = false;
  6561. $pc = 0; // previous character
  6562. // for each character
  6563. while ($i < $nb) {
  6564. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6565. break;
  6566. }
  6567. //Get the current character
  6568. $c = $chars[$i];
  6569. if ($c == 10) { // 10 = "\n" = new line
  6570. //Explicit line break
  6571. if ($align == 'J') {
  6572. if ($this->rtl) {
  6573. $talign = 'R';
  6574. } else {
  6575. $talign = 'L';
  6576. }
  6577. } else {
  6578. $talign = $align;
  6579. }
  6580. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6581. if ($firstline) {
  6582. $startx = $this->x;
  6583. $tmparr = array_slice($chars, $j, ($i - $j));
  6584. if ($rtlmode) {
  6585. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6586. }
  6587. $linew = $this->GetArrStringWidth($tmparr);
  6588. unset($tmparr);
  6589. if ($this->rtl) {
  6590. $this->endlinex = $startx - $linew;
  6591. } else {
  6592. $this->endlinex = $startx + $linew;
  6593. }
  6594. $w = $linew;
  6595. $tmpcellpadding = $this->cell_padding;
  6596. if ($maxh == 0) {
  6597. $this->SetCellPadding(0);
  6598. }
  6599. }
  6600. if ($firstblock AND $this->isRTLTextDir()) {
  6601. $tmpstr = $this->stringRightTrim($tmpstr);
  6602. }
  6603. // Skip newlines at the begining of a page or column
  6604. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6605. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6606. }
  6607. unset($tmpstr);
  6608. if ($firstline) {
  6609. $this->cell_padding = $tmpcellpadding;
  6610. return ($this->UniArrSubString($uchars, $i));
  6611. }
  6612. ++$nl;
  6613. $j = $i + 1;
  6614. $l = 0;
  6615. $sep = -1;
  6616. $shy = false;
  6617. // account for margin changes
  6618. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6619. $this->AcceptPageBreak();
  6620. if ($this->rtl) {
  6621. $this->x -= $margin['R'];
  6622. } else {
  6623. $this->x += $margin['L'];
  6624. }
  6625. $this->lMargin += $margin['L'];
  6626. $this->rMargin += $margin['R'];
  6627. }
  6628. $w = $this->getRemainingWidth();
  6629. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6630. } else {
  6631. // 160 is the non-breaking space.
  6632. // 173 is SHY (Soft Hypen).
  6633. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6634. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6635. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6636. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6637. // update last blank space position
  6638. $sep = $i;
  6639. // check if is a SHY
  6640. if ($c == 173) {
  6641. $shy = true;
  6642. if ($pc == 45) {
  6643. $tmp_shy_replacement_width = 0;
  6644. $tmp_shy_replacement_char = '';
  6645. } else {
  6646. $tmp_shy_replacement_width = $shy_replacement_width;
  6647. $tmp_shy_replacement_char = $shy_replacement_char;
  6648. }
  6649. } else {
  6650. $shy = false;
  6651. }
  6652. }
  6653. // update string length
  6654. if ($this->isUnicodeFont() AND ($arabic)) {
  6655. // with bidirectional algorithm some chars may be changed affecting the line length
  6656. // *** very slow ***
  6657. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6658. } else {
  6659. $l += $this->GetCharWidth($c);
  6660. }
  6661. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6662. // we have reached the end of column
  6663. if ($sep == -1) {
  6664. // check if the line was already started
  6665. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6666. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6667. // print a void cell and go to next line
  6668. $this->Cell($w, $h, '', 0, 1);
  6669. $linebreak = true;
  6670. if ($firstline) {
  6671. return ($this->UniArrSubString($uchars, $j));
  6672. }
  6673. } else {
  6674. // truncate the word because do not fit on column
  6675. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6676. if ($firstline) {
  6677. $startx = $this->x;
  6678. $tmparr = array_slice($chars, $j, ($i - $j));
  6679. if ($rtlmode) {
  6680. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6681. }
  6682. $linew = $this->GetArrStringWidth($tmparr);
  6683. unset($tmparr);
  6684. if ($this->rtl) {
  6685. $this->endlinex = $startx - $linew;
  6686. } else {
  6687. $this->endlinex = $startx + $linew;
  6688. }
  6689. $w = $linew;
  6690. $tmpcellpadding = $this->cell_padding;
  6691. if ($maxh == 0) {
  6692. $this->SetCellPadding(0);
  6693. }
  6694. }
  6695. if ($firstblock AND $this->isRTLTextDir()) {
  6696. $tmpstr = $this->stringRightTrim($tmpstr);
  6697. }
  6698. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6699. unset($tmpstr);
  6700. if ($firstline) {
  6701. $this->cell_padding = $tmpcellpadding;
  6702. return ($this->UniArrSubString($uchars, $i));
  6703. }
  6704. $j = $i;
  6705. --$i;
  6706. }
  6707. } else {
  6708. // word wrapping
  6709. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6710. $endspace = 1;
  6711. } else {
  6712. $endspace = 0;
  6713. }
  6714. // check the length of the next string
  6715. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6716. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6717. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6718. // truncate the word because do not fit on a full page width
  6719. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6720. if ($firstline) {
  6721. $startx = $this->x;
  6722. $tmparr = array_slice($chars, $j, ($i - $j));
  6723. if ($rtlmode) {
  6724. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6725. }
  6726. $linew = $this->GetArrStringWidth($tmparr);
  6727. unset($tmparr);
  6728. if ($this->rtl) {
  6729. $this->endlinex = $startx - $linew;
  6730. } else {
  6731. $this->endlinex = $startx + $linew;
  6732. }
  6733. $w = $linew;
  6734. $tmpcellpadding = $this->cell_padding;
  6735. if ($maxh == 0) {
  6736. $this->SetCellPadding(0);
  6737. }
  6738. }
  6739. if ($firstblock AND $this->isRTLTextDir()) {
  6740. $tmpstr = $this->stringRightTrim($tmpstr);
  6741. }
  6742. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6743. unset($tmpstr);
  6744. if ($firstline) {
  6745. $this->cell_padding = $tmpcellpadding;
  6746. return ($this->UniArrSubString($uchars, $i));
  6747. }
  6748. $j = $i;
  6749. --$i;
  6750. } else {
  6751. // word wrapping
  6752. if ($shy) {
  6753. // add hypen (minus symbol) at the end of the line
  6754. $shy_width = $tmp_shy_replacement_width;
  6755. if ($this->rtl) {
  6756. $shy_char_left = $tmp_shy_replacement_char;
  6757. $shy_char_right = '';
  6758. } else {
  6759. $shy_char_left = '';
  6760. $shy_char_right = $tmp_shy_replacement_char;
  6761. }
  6762. } else {
  6763. $shy_width = 0;
  6764. $shy_char_left = '';
  6765. $shy_char_right = '';
  6766. }
  6767. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6768. if ($firstline) {
  6769. $startx = $this->x;
  6770. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6771. if ($rtlmode) {
  6772. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6773. }
  6774. $linew = $this->GetArrStringWidth($tmparr);
  6775. unset($tmparr);
  6776. if ($this->rtl) {
  6777. $this->endlinex = $startx - $linew - $shy_width;
  6778. } else {
  6779. $this->endlinex = $startx + $linew + $shy_width;
  6780. }
  6781. $w = $linew;
  6782. $tmpcellpadding = $this->cell_padding;
  6783. if ($maxh == 0) {
  6784. $this->SetCellPadding(0);
  6785. }
  6786. }
  6787. // print the line
  6788. if ($firstblock AND $this->isRTLTextDir()) {
  6789. $tmpstr = $this->stringRightTrim($tmpstr);
  6790. }
  6791. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6792. unset($tmpstr);
  6793. if ($firstline) {
  6794. // return the remaining text
  6795. $this->cell_padding = $tmpcellpadding;
  6796. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6797. }
  6798. $i = $sep;
  6799. $sep = -1;
  6800. $shy = false;
  6801. $j = ($i+1);
  6802. }
  6803. }
  6804. // account for margin changes
  6805. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6806. $this->AcceptPageBreak();
  6807. if ($this->rtl) {
  6808. $this->x -= $margin['R'];
  6809. } else {
  6810. $this->x += $margin['L'];
  6811. }
  6812. $this->lMargin += $margin['L'];
  6813. $this->rMargin += $margin['R'];
  6814. }
  6815. $w = $this->getRemainingWidth();
  6816. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6817. if ($linebreak) {
  6818. $linebreak = false;
  6819. } else {
  6820. ++$nl;
  6821. $l = 0;
  6822. }
  6823. }
  6824. }
  6825. // save last character
  6826. $pc = $c;
  6827. ++$i;
  6828. } // end while i < nb
  6829. // print last substring (if any)
  6830. if ($l > 0) {
  6831. switch ($align) {
  6832. case 'J':
  6833. case 'C': {
  6834. $w = $w;
  6835. break;
  6836. }
  6837. case 'L': {
  6838. if ($this->rtl) {
  6839. $w = $w;
  6840. } else {
  6841. $w = $l;
  6842. }
  6843. break;
  6844. }
  6845. case 'R': {
  6846. if ($this->rtl) {
  6847. $w = $l;
  6848. } else {
  6849. $w = $w;
  6850. }
  6851. break;
  6852. }
  6853. default: {
  6854. $w = $l;
  6855. break;
  6856. }
  6857. }
  6858. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  6859. if ($firstline) {
  6860. $startx = $this->x;
  6861. $tmparr = array_slice($chars, $j, ($nb - $j));
  6862. if ($rtlmode) {
  6863. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6864. }
  6865. $linew = $this->GetArrStringWidth($tmparr);
  6866. unset($tmparr);
  6867. if ($this->rtl) {
  6868. $this->endlinex = $startx - $linew;
  6869. } else {
  6870. $this->endlinex = $startx + $linew;
  6871. }
  6872. $w = $linew;
  6873. $tmpcellpadding = $this->cell_padding;
  6874. if ($maxh == 0) {
  6875. $this->SetCellPadding(0);
  6876. }
  6877. }
  6878. if ($firstblock AND $this->isRTLTextDir()) {
  6879. $tmpstr = $this->stringRightTrim($tmpstr);
  6880. }
  6881. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6882. unset($tmpstr);
  6883. if ($firstline) {
  6884. $this->cell_padding = $tmpcellpadding;
  6885. return ($this->UniArrSubString($uchars, $nb));
  6886. }
  6887. ++$nl;
  6888. }
  6889. if ($firstline) {
  6890. return '';
  6891. }
  6892. return $nl;
  6893. }
  6894. /**
  6895. * Returns the remaining width between the current position and margins.
  6896. * @return int Return the remaining width
  6897. * @protected
  6898. */
  6899. protected function getRemainingWidth() {
  6900. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6901. if ($this->rtl) {
  6902. return ($this->x - $this->lMargin);
  6903. } else {
  6904. return ($this->w - $this->rMargin - $this->x);
  6905. }
  6906. }
  6907. /**
  6908. * Extract a slice of the $strarr array and return it as string.
  6909. * @param $strarr (string) The input array of characters.
  6910. * @param $start (int) the starting element of $strarr.
  6911. * @param $end (int) first element that will not be returned.
  6912. * @return Return part of a string
  6913. * @public
  6914. */
  6915. public function UTF8ArrSubString($strarr, $start='', $end='') {
  6916. if (strlen($start) == 0) {
  6917. $start = 0;
  6918. }
  6919. if (strlen($end) == 0) {
  6920. $end = count($strarr);
  6921. }
  6922. $string = '';
  6923. for ($i=$start; $i < $end; ++$i) {
  6924. $string .= $this->unichr($strarr[$i]);
  6925. }
  6926. return $string;
  6927. }
  6928. /**
  6929. * Extract a slice of the $uniarr array and return it as string.
  6930. * @param $uniarr (string) The input array of characters.
  6931. * @param $start (int) the starting element of $strarr.
  6932. * @param $end (int) first element that will not be returned.
  6933. * @return Return part of a string
  6934. * @public
  6935. * @since 4.5.037 (2009-04-07)
  6936. */
  6937. public function UniArrSubString($uniarr, $start='', $end='') {
  6938. if (strlen($start) == 0) {
  6939. $start = 0;
  6940. }
  6941. if (strlen($end) == 0) {
  6942. $end = count($uniarr);
  6943. }
  6944. $string = '';
  6945. for ($i=$start; $i < $end; ++$i) {
  6946. $string .= $uniarr[$i];
  6947. }
  6948. return $string;
  6949. }
  6950. /**
  6951. * Convert an array of UTF8 values to array of unicode characters
  6952. * @param $ta (string) The input array of UTF8 values.
  6953. * @return Return array of unicode characters
  6954. * @public
  6955. * @since 4.5.037 (2009-04-07)
  6956. */
  6957. public function UTF8ArrayToUniArray($ta) {
  6958. return array_map(array($this, 'unichr'), $ta);
  6959. }
  6960. /**
  6961. * Returns the unicode caracter specified by UTF-8 value
  6962. * @param $c (int) UTF-8 value
  6963. * @return Returns the specified character.
  6964. * @author Miguel Perez, Nicola Asuni
  6965. * @public
  6966. * @since 2.3.000 (2008-03-05)
  6967. */
  6968. public function unichr($c) {
  6969. if (!$this->isunicode) {
  6970. return chr($c);
  6971. } elseif ($c <= 0x7F) {
  6972. // one byte
  6973. return chr($c);
  6974. } elseif ($c <= 0x7FF) {
  6975. // two bytes
  6976. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  6977. } elseif ($c <= 0xFFFF) {
  6978. // three bytes
  6979. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6980. } elseif ($c <= 0x10FFFF) {
  6981. // four bytes
  6982. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6983. } else {
  6984. return '';
  6985. }
  6986. }
  6987. /**
  6988. * Return the image type given the file name or array returned by getimagesize() function.
  6989. * @param $imgfile (string) image file name
  6990. * @param $iminfo (array) array of image information returned by getimagesize() function.
  6991. * @return string image type
  6992. * @since 4.8.017 (2009-11-27)
  6993. */
  6994. public function getImageFileType($imgfile, $iminfo=array()) {
  6995. $type = '';
  6996. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  6997. $mime = explode('/', $iminfo['mime']);
  6998. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  6999. $type = strtolower(trim($mime[1]));
  7000. }
  7001. }
  7002. if (empty($type)) {
  7003. $fileinfo = pathinfo($imgfile);
  7004. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  7005. $type = strtolower(trim($fileinfo['extension']));
  7006. }
  7007. }
  7008. if ($type == 'jpg') {
  7009. $type = 'jpeg';
  7010. }
  7011. return $type;
  7012. }
  7013. /**
  7014. * Set the block dimensions accounting for page breaks and page/column fitting
  7015. * @param $w (float) width
  7016. * @param $h (float) height
  7017. * @param $x (float) X coordinate
  7018. * @param $y (float) Y coodiante
  7019. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  7020. * @return array($w, $h, $x, $y)
  7021. * @protected
  7022. * @since 5.5.009 (2010-07-05)
  7023. */
  7024. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  7025. if ($w <= 0) {
  7026. // set maximum width
  7027. $w = ($this->w - $this->lMargin - $this->rMargin);
  7028. }
  7029. if ($h <= 0) {
  7030. // set maximum height
  7031. $h = ($this->PageBreakTrigger - $this->tMargin);
  7032. }
  7033. // resize the block to be vertically contained on a single page or single column
  7034. if ($fitonpage OR $this->AutoPageBreak) {
  7035. $ratio_wh = ($w / $h);
  7036. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7037. $h = $this->PageBreakTrigger - $this->tMargin;
  7038. $w = ($h * $ratio_wh);
  7039. }
  7040. // resize the block to be horizontally contained on a single page or single column
  7041. if ($fitonpage) {
  7042. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7043. if ($w > $maxw) {
  7044. $w = $maxw;
  7045. $h = ($w / $ratio_wh);
  7046. }
  7047. }
  7048. }
  7049. // Check whether we need a new page or new column first as this does not fit
  7050. $prev_x = $this->x;
  7051. $prev_y = $this->y;
  7052. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7053. $y = $this->y;
  7054. if ($this->rtl) {
  7055. $x += ($prev_x - $this->x);
  7056. } else {
  7057. $x += ($this->x - $prev_x);
  7058. }
  7059. $this->newline = true;
  7060. }
  7061. // resize the block to be contained on the remaining available page or column space
  7062. if ($fitonpage) {
  7063. $ratio_wh = ($w / $h);
  7064. if (($y + $h) > $this->PageBreakTrigger) {
  7065. $h = $this->PageBreakTrigger - $y;
  7066. $w = ($h * $ratio_wh);
  7067. }
  7068. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7069. $w = $this->w - $this->rMargin - $x;
  7070. $h = ($w / $ratio_wh);
  7071. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7072. $w = $x - $this->lMargin;
  7073. $h = ($w / $ratio_wh);
  7074. }
  7075. }
  7076. return array($w, $h, $x, $y);
  7077. }
  7078. /**
  7079. * Puts an image in the page.
  7080. * The upper-left corner must be given.
  7081. * The dimensions can be specified in different ways:<ul>
  7082. * <li>explicit width and height (expressed in user unit)</li>
  7083. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7084. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7085. * 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;
  7086. * The format can be specified explicitly or inferred from the file extension.<br />
  7087. * It is possible to put a link on the image.<br />
  7088. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7089. * @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').
  7090. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7091. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7092. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7093. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7094. * @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.
  7095. * @param $link (mixed) URL or identifier returned by AddLink().
  7096. * @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>
  7097. * @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).
  7098. * @param $dpi (int) dot-per-inch resolution used on resize
  7099. * @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>
  7100. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7101. * @param $imgmask (mixed) image object returned by this function or false
  7102. * @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)))
  7103. * @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).
  7104. * @param $hidden (boolean) If true do not display the image.
  7105. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7106. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7107. * @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.
  7108. * @return image information
  7109. * @public
  7110. * @since 1.1
  7111. */
  7112. 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()) {
  7113. if ($x === '') {
  7114. $x = $this->x;
  7115. }
  7116. if ($y === '') {
  7117. $y = $this->y;
  7118. }
  7119. // check page for no-write regions and adapt page margins if necessary
  7120. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7121. $cached_file = false; // true when the file is cached
  7122. $exurl = ''; // external streams
  7123. // check if we are passing an image as file or string
  7124. if ($file[0] === '@') {
  7125. // image from string
  7126. $imgdata = substr($file, 1);
  7127. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7128. $fp = fopen($file, 'w');
  7129. fwrite($fp, $imgdata);
  7130. fclose($fp);
  7131. unset($imgdata);
  7132. $cached_file = true;
  7133. $imsize = @getimagesize($file);
  7134. if ($imsize === FALSE) {
  7135. unlink($file);
  7136. $cached_file = false;
  7137. }
  7138. } else { // image file
  7139. if ($file{0} === '*') {
  7140. // image as external stream
  7141. $file = substr($file, 1);
  7142. $exurl = $file;
  7143. }
  7144. // check if is local file
  7145. if (!@file_exists($file)) {
  7146. // encode spaces on filename (file is probably an URL)
  7147. $file = str_replace(' ', '%20', $file);
  7148. }
  7149. if (@file_exists($file)) {
  7150. // get image dimensions
  7151. $imsize = @getimagesize($file);
  7152. } else {
  7153. $imsize = false;
  7154. }
  7155. if ($imsize === FALSE) {
  7156. if (function_exists('curl_init')) {
  7157. // try to get remote file data using cURL
  7158. $cs = curl_init(); // curl session
  7159. curl_setopt($cs, CURLOPT_URL, $file);
  7160. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7161. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7162. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7163. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7164. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7165. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7166. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7167. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7168. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7169. $imgdata = curl_exec($cs);
  7170. curl_close($cs);
  7171. if ($imgdata !== FALSE) {
  7172. // copy image to cache
  7173. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7174. $fp = fopen($file, 'w');
  7175. fwrite($fp, $imgdata);
  7176. fclose($fp);
  7177. unset($imgdata);
  7178. $cached_file = true;
  7179. $imsize = @getimagesize($file);
  7180. if ($imsize === FALSE) {
  7181. unlink($file);
  7182. $cached_file = false;
  7183. }
  7184. }
  7185. } elseif (($w > 0) AND ($h > 0)) {
  7186. // get measures from specified data
  7187. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7188. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7189. $imsize = array($pw, $ph);
  7190. }
  7191. }
  7192. }
  7193. if ($imsize === FALSE) {
  7194. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7195. // get measures from specified data
  7196. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7197. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7198. $imsize = array($pw, $ph);
  7199. } else {
  7200. $this->Error('[Image] Unable to get image: '.$file);
  7201. }
  7202. }
  7203. // get original image width and height in pixels
  7204. list($pixw, $pixh) = $imsize;
  7205. // calculate image width and height on document
  7206. if (($w <= 0) AND ($h <= 0)) {
  7207. // convert image size to document unit
  7208. $w = $this->pixelsToUnits($pixw);
  7209. $h = $this->pixelsToUnits($pixh);
  7210. } elseif ($w <= 0) {
  7211. $w = $h * $pixw / $pixh;
  7212. } elseif ($h <= 0) {
  7213. $h = $w * $pixh / $pixw;
  7214. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7215. if (strlen($fitbox) !== 2) {
  7216. // set default alignment
  7217. $fitbox = '--';
  7218. }
  7219. // scale image dimensions proportionally to fit within the ($w, $h) box
  7220. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7221. // store current height
  7222. $oldh = $h;
  7223. // calculate new height
  7224. $h = $w * $pixh / $pixw;
  7225. // height difference
  7226. $hdiff = ($oldh - $h);
  7227. // vertical alignment
  7228. switch (strtoupper($fitbox{1})) {
  7229. case 'T': {
  7230. break;
  7231. }
  7232. case 'M': {
  7233. $y += ($hdiff / 2);
  7234. break;
  7235. }
  7236. case 'B': {
  7237. $y += $hdiff;
  7238. break;
  7239. }
  7240. }
  7241. } else {
  7242. // store current width
  7243. $oldw = $w;
  7244. // calculate new width
  7245. $w = $h * $pixw / $pixh;
  7246. // width difference
  7247. $wdiff = ($oldw - $w);
  7248. // horizontal alignment
  7249. switch (strtoupper($fitbox{0})) {
  7250. case 'L': {
  7251. if ($this->rtl) {
  7252. $x -= $wdiff;
  7253. }
  7254. break;
  7255. }
  7256. case 'C': {
  7257. if ($this->rtl) {
  7258. $x -= ($wdiff / 2);
  7259. } else {
  7260. $x += ($wdiff / 2);
  7261. }
  7262. break;
  7263. }
  7264. case 'R': {
  7265. if (!$this->rtl) {
  7266. $x += $wdiff;
  7267. }
  7268. break;
  7269. }
  7270. }
  7271. }
  7272. }
  7273. // fit the image on available space
  7274. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7275. // calculate new minimum dimensions in pixels
  7276. $neww = round($w * $this->k * $dpi / $this->dpi);
  7277. $newh = round($h * $this->k * $dpi / $this->dpi);
  7278. // check if resize is necessary (resize is used only to reduce the image)
  7279. $newsize = ($neww * $newh);
  7280. $pixsize = ($pixw * $pixh);
  7281. if (intval($resize) == 2) {
  7282. $resize = true;
  7283. } elseif ($newsize >= $pixsize) {
  7284. $resize = false;
  7285. }
  7286. // check if image has been already added on document
  7287. $newimage = true;
  7288. if (in_array($file, $this->imagekeys)) {
  7289. $newimage = false;
  7290. // get existing image data
  7291. $info = $this->getImageBuffer($file);
  7292. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7293. // check if the newer image is larger
  7294. $oldsize = ($info['w'] * $info['h']);
  7295. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7296. $newimage = true;
  7297. }
  7298. }
  7299. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7300. // check for cached images with alpha channel
  7301. $filehash = md5($file);
  7302. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7303. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7304. if (in_array($tempfile_plain, $this->imagekeys)) {
  7305. // get existing image data
  7306. $info = $this->getImageBuffer($tempfile_plain);
  7307. // check if the newer image is larger
  7308. $oldsize = ($info['w'] * $info['h']);
  7309. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7310. $newimage = true;
  7311. } else {
  7312. $newimage = false;
  7313. // embed mask image
  7314. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7315. // embed image, masked with previously embedded mask
  7316. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7317. }
  7318. }
  7319. }
  7320. if ($newimage) {
  7321. //First use of image, get info
  7322. $type = strtolower($type);
  7323. if ($type == '') {
  7324. $type = $this->getImageFileType($file, $imsize);
  7325. } elseif ($type == 'jpg') {
  7326. $type = 'jpeg';
  7327. }
  7328. $mqr = $this->get_mqr();
  7329. $this->set_mqr(false);
  7330. // Specific image handlers
  7331. $mtd = '_parse'.$type;
  7332. // GD image handler function
  7333. $gdfunction = 'imagecreatefrom'.$type;
  7334. $info = false;
  7335. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7336. // TCPDF image functions
  7337. $info = $this->$mtd($file);
  7338. if ($info == 'pngalpha') {
  7339. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7340. }
  7341. }
  7342. if (!$info) {
  7343. if (function_exists($gdfunction)) {
  7344. // GD library
  7345. $img = $gdfunction($file);
  7346. if ($resize) {
  7347. $imgr = imagecreatetruecolor($neww, $newh);
  7348. if (($type == 'gif') OR ($type == 'png')) {
  7349. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7350. }
  7351. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7352. if (($type == 'gif') OR ($type == 'png')) {
  7353. $info = $this->_toPNG($imgr);
  7354. } else {
  7355. $info = $this->_toJPEG($imgr);
  7356. }
  7357. } else {
  7358. if (($type == 'gif') OR ($type == 'png')) {
  7359. $info = $this->_toPNG($img);
  7360. } else {
  7361. $info = $this->_toJPEG($img);
  7362. }
  7363. }
  7364. } elseif (extension_loaded('imagick')) {
  7365. // ImageMagick library
  7366. $img = new Imagick();
  7367. if ($type == 'SVG') {
  7368. // get SVG file content
  7369. $svgimg = file_get_contents($file);
  7370. // get width and height
  7371. $regs = array();
  7372. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7373. $svgtag = $regs[1];
  7374. $tmp = array();
  7375. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7376. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7377. $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
  7378. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7379. } else {
  7380. $ow = $w;
  7381. }
  7382. $tmp = array();
  7383. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7384. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7385. $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
  7386. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7387. } else {
  7388. $oh = $h;
  7389. }
  7390. $tmp = array();
  7391. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7392. $vbw = ($ow * $this->imgscale * $this->k);
  7393. $vbh = ($oh * $this->imgscale * $this->k);
  7394. $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
  7395. $svgtag = $vbox.$svgtag;
  7396. }
  7397. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7398. }
  7399. $img->readImageBlob($svgimg);
  7400. } else {
  7401. $img->readImage($file);
  7402. }
  7403. if ($resize) {
  7404. $img->resizeImage($neww, $newh, 10, 1, false);
  7405. }
  7406. $img->setCompressionQuality($this->jpeg_quality);
  7407. $img->setImageFormat('jpeg');
  7408. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7409. $img->writeImage($tempname);
  7410. $info = $this->_parsejpeg($tempname);
  7411. unlink($tempname);
  7412. $img->destroy();
  7413. } else {
  7414. return;
  7415. }
  7416. }
  7417. if ($info === false) {
  7418. //If false, we cannot process image
  7419. return;
  7420. }
  7421. $this->set_mqr($mqr);
  7422. if ($ismask) {
  7423. // force grayscale
  7424. $info['cs'] = 'DeviceGray';
  7425. }
  7426. $info['i'] = $this->numimages;
  7427. if (!in_array($file, $this->imagekeys)) {
  7428. ++$info['i'];
  7429. }
  7430. if ($imgmask !== false) {
  7431. $info['masked'] = $imgmask;
  7432. }
  7433. if (!empty($exurl)) {
  7434. $info['exurl'] = $exurl;
  7435. }
  7436. // array of alternative images
  7437. $info['altimgs'] = $altimgs;
  7438. // add image to document
  7439. $this->setImageBuffer($file, $info);
  7440. }
  7441. if ($cached_file) {
  7442. // remove cached file
  7443. unlink($file);
  7444. }
  7445. // set alignment
  7446. $this->img_rb_y = $y + $h;
  7447. // set alignment
  7448. if ($this->rtl) {
  7449. if ($palign == 'L') {
  7450. $ximg = $this->lMargin;
  7451. } elseif ($palign == 'C') {
  7452. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7453. } elseif ($palign == 'R') {
  7454. $ximg = $this->w - $this->rMargin - $w;
  7455. } else {
  7456. $ximg = $x - $w;
  7457. }
  7458. $this->img_rb_x = $ximg;
  7459. } else {
  7460. if ($palign == 'L') {
  7461. $ximg = $this->lMargin;
  7462. } elseif ($palign == 'C') {
  7463. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7464. } elseif ($palign == 'R') {
  7465. $ximg = $this->w - $this->rMargin - $w;
  7466. } else {
  7467. $ximg = $x;
  7468. }
  7469. $this->img_rb_x = $ximg + $w;
  7470. }
  7471. if ($ismask OR $hidden) {
  7472. // image is not displayed
  7473. return $info['i'];
  7474. }
  7475. $xkimg = $ximg * $this->k;
  7476. if (!$alt) {
  7477. // only non-alternative immages will be set
  7478. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  7479. }
  7480. if (!empty($border)) {
  7481. $bx = $this->x;
  7482. $by = $this->y;
  7483. $this->x = $ximg;
  7484. if ($this->rtl) {
  7485. $this->x += $w;
  7486. }
  7487. $this->y = $y;
  7488. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7489. $this->x = $bx;
  7490. $this->y = $by;
  7491. }
  7492. if ($link) {
  7493. $this->Link($ximg, $y, $w, $h, $link, 0);
  7494. }
  7495. // set pointer to align the next text/objects
  7496. switch($align) {
  7497. case 'T': {
  7498. $this->y = $y;
  7499. $this->x = $this->img_rb_x;
  7500. break;
  7501. }
  7502. case 'M': {
  7503. $this->y = $y + round($h/2);
  7504. $this->x = $this->img_rb_x;
  7505. break;
  7506. }
  7507. case 'B': {
  7508. $this->y = $this->img_rb_y;
  7509. $this->x = $this->img_rb_x;
  7510. break;
  7511. }
  7512. case 'N': {
  7513. $this->SetY($this->img_rb_y);
  7514. break;
  7515. }
  7516. default:{
  7517. break;
  7518. }
  7519. }
  7520. $this->endlinex = $this->img_rb_x;
  7521. if ($this->inxobj) {
  7522. // we are inside an XObject template
  7523. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7524. }
  7525. return $info['i'];
  7526. }
  7527. /**
  7528. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7529. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7530. * @since 4.6.025 (2009-08-17)
  7531. */
  7532. public function set_mqr($mqr) {
  7533. if (!defined('PHP_VERSION_ID')) {
  7534. $version = PHP_VERSION;
  7535. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7536. }
  7537. if (PHP_VERSION_ID < 50300) {
  7538. @set_magic_quotes_runtime($mqr);
  7539. }
  7540. }
  7541. /**
  7542. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7543. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7544. * @since 4.6.025 (2009-08-17)
  7545. */
  7546. public function get_mqr() {
  7547. if (!defined('PHP_VERSION_ID')) {
  7548. $version = PHP_VERSION;
  7549. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7550. }
  7551. if (PHP_VERSION_ID < 50300) {
  7552. return @get_magic_quotes_runtime();
  7553. }
  7554. return 0;
  7555. }
  7556. /**
  7557. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7558. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7559. * @param $image (image) Image object.
  7560. * return image JPEG image object.
  7561. * @protected
  7562. */
  7563. protected function _toJPEG($image) {
  7564. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7565. imagejpeg($image, $tempname, $this->jpeg_quality);
  7566. imagedestroy($image);
  7567. $retvars = $this->_parsejpeg($tempname);
  7568. // tidy up by removing temporary image
  7569. unlink($tempname);
  7570. return $retvars;
  7571. }
  7572. /**
  7573. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7574. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7575. * @param $image (image) Image object.
  7576. * return image PNG image object.
  7577. * @protected
  7578. * @since 4.9.016 (2010-04-20)
  7579. */
  7580. protected function _toPNG($image) {
  7581. // set temporary image file name
  7582. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7583. // turn off interlaced mode
  7584. imageinterlace($image, 0);
  7585. // create temporary PNG image
  7586. imagepng($image, $tempname);
  7587. // remove image from memory
  7588. imagedestroy($image);
  7589. // get PNG image data
  7590. $retvars = $this->_parsepng($tempname);
  7591. // tidy up by removing temporary image
  7592. unlink($tempname);
  7593. return $retvars;
  7594. }
  7595. /**
  7596. * Set the transparency for the given GD image.
  7597. * @param $new_image (image) GD image object
  7598. * @param $image (image) GD image object.
  7599. * return GD image object.
  7600. * @protected
  7601. * @since 4.9.016 (2010-04-20)
  7602. */
  7603. protected function _setGDImageTransparency($new_image, $image) {
  7604. // transparency index
  7605. $tid = imagecolortransparent($image);
  7606. // default transparency color
  7607. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7608. if ($tid >= 0) {
  7609. // get the colors for the transparency index
  7610. $tcol = imagecolorsforindex($image, $tid);
  7611. }
  7612. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7613. imagefill($new_image, 0, 0, $tid);
  7614. imagecolortransparent($new_image, $tid);
  7615. return $new_image;
  7616. }
  7617. /**
  7618. * Extract info from a JPEG file without using the GD library.
  7619. * @param $file (string) image file to parse
  7620. * @return array structure containing the image data
  7621. * @protected
  7622. */
  7623. protected function _parsejpeg($file) {
  7624. $a = getimagesize($file);
  7625. if (empty($a)) {
  7626. $this->Error('Missing or incorrect image file: '.$file);
  7627. }
  7628. if ($a[2] != 2) {
  7629. $this->Error('Not a JPEG file: '.$file);
  7630. }
  7631. // bits per pixel
  7632. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7633. // number of image channels
  7634. if (!isset($a['channels'])) {
  7635. $channels = 3;
  7636. } else {
  7637. $channels = intval($a['channels']);
  7638. }
  7639. // default colour space
  7640. switch ($channels) {
  7641. case 1: {
  7642. $colspace = 'DeviceGray';
  7643. break;
  7644. }
  7645. case 3: {
  7646. $colspace = 'DeviceRGB';
  7647. break;
  7648. }
  7649. case 4: {
  7650. $colspace = 'DeviceCMYK';
  7651. break;
  7652. }
  7653. default: {
  7654. $channels = 3;
  7655. $colspace = 'DeviceRGB';
  7656. break;
  7657. }
  7658. }
  7659. // get file content
  7660. $data = file_get_contents($file);
  7661. // check for embedded ICC profile
  7662. $icc = array();
  7663. $offset = 0;
  7664. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7665. // get ICC sequence length
  7666. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7667. // marker sequence number
  7668. $msn = max(1, ord($data[($pos + 12)]));
  7669. // number of markers (total of APP2 used)
  7670. $nom = max(1, ord($data[($pos + 13)]));
  7671. // get sequence segment
  7672. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7673. // move forward to next sequence
  7674. $offset = ($pos + 14 + $length);
  7675. }
  7676. // order and compact ICC segments
  7677. if (count($icc) > 0) {
  7678. ksort($icc);
  7679. $icc = implode('', $icc);
  7680. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7681. // invalid ICC profile
  7682. $icc = false;
  7683. }
  7684. } else {
  7685. $icc = false;
  7686. }
  7687. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7688. }
  7689. /**
  7690. * Extract info from a PNG file without using the GD library.
  7691. * @param $file (string) image file to parse
  7692. * @return array structure containing the image data
  7693. * @protected
  7694. */
  7695. protected function _parsepng($file) {
  7696. $f = fopen($file, 'rb');
  7697. if ($f === false) {
  7698. $this->Error('Can\'t open image file: '.$file);
  7699. }
  7700. //Check signature
  7701. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7702. $this->Error('Not a PNG file: '.$file);
  7703. }
  7704. //Read header chunk
  7705. fread($f, 4);
  7706. if (fread($f, 4) != 'IHDR') {
  7707. $this->Error('Incorrect PNG file: '.$file);
  7708. }
  7709. $w = $this->_freadint($f);
  7710. $h = $this->_freadint($f);
  7711. $bpc = ord(fread($f, 1));
  7712. if ($bpc > 8) {
  7713. //$this->Error('16-bit depth not supported: '.$file);
  7714. fclose($f);
  7715. return false;
  7716. }
  7717. $ct = ord(fread($f, 1));
  7718. if ($ct == 0) {
  7719. $colspace = 'DeviceGray';
  7720. } elseif ($ct == 2) {
  7721. $colspace = 'DeviceRGB';
  7722. } elseif ($ct == 3) {
  7723. $colspace = 'Indexed';
  7724. } else {
  7725. // alpha channel
  7726. fclose($f);
  7727. return 'pngalpha';
  7728. }
  7729. if (ord(fread($f, 1)) != 0) {
  7730. //$this->Error('Unknown compression method: '.$file);
  7731. fclose($f);
  7732. return false;
  7733. }
  7734. if (ord(fread($f, 1)) != 0) {
  7735. //$this->Error('Unknown filter method: '.$file);
  7736. fclose($f);
  7737. return false;
  7738. }
  7739. if (ord(fread($f, 1)) != 0) {
  7740. //$this->Error('Interlacing not supported: '.$file);
  7741. fclose($f);
  7742. return false;
  7743. }
  7744. fread($f, 4);
  7745. $channels = ($ct == 2 ? 3 : 1);
  7746. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7747. //Scan chunks looking for palette, transparency and image data
  7748. $pal = '';
  7749. $trns = '';
  7750. $data = '';
  7751. $icc = false;
  7752. do {
  7753. $n = $this->_freadint($f);
  7754. $type = fread($f, 4);
  7755. if ($type == 'PLTE') {
  7756. // read palette
  7757. $pal = $this->rfread($f, $n);
  7758. fread($f, 4);
  7759. } elseif ($type == 'tRNS') {
  7760. // read transparency info
  7761. $t = $this->rfread($f, $n);
  7762. if ($ct == 0) {
  7763. $trns = array(ord($t{1}));
  7764. } elseif ($ct == 2) {
  7765. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7766. } else {
  7767. $pos = strpos($t, chr(0));
  7768. if ($pos !== false) {
  7769. $trns = array($pos);
  7770. }
  7771. }
  7772. fread($f, 4);
  7773. } elseif ($type == 'IDAT') {
  7774. // read image data block
  7775. $data .= $this->rfread($f, $n);
  7776. fread($f, 4);
  7777. } elseif ($type == 'iCCP') {
  7778. // skip profile name and null separator
  7779. $len = 0;
  7780. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7781. ++$len;
  7782. }
  7783. // get compression method
  7784. if (ord(fread($f, 1)) != 0) {
  7785. //$this->Error('Unknown filter method: '.$file);
  7786. fclose($f);
  7787. return false;
  7788. }
  7789. // read ICC Color Profile
  7790. $icc = $this->rfread($f, ($n - $len - 2));
  7791. // decompress profile
  7792. $icc = gzuncompress($icc);
  7793. fread($f, 4);
  7794. } elseif ($type == 'IEND') {
  7795. break;
  7796. } else {
  7797. $this->rfread($f, $n + 4);
  7798. }
  7799. } while ($n);
  7800. if (($colspace == 'Indexed') AND (empty($pal))) {
  7801. //$this->Error('Missing palette in '.$file);
  7802. fclose($f);
  7803. return false;
  7804. }
  7805. fclose($f);
  7806. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7807. }
  7808. /**
  7809. * Binary-safe and URL-safe file read.
  7810. * 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.
  7811. * @param $handle (resource)
  7812. * @param $length (int)
  7813. * @return Returns the read string or FALSE in case of error.
  7814. * @author Nicola Asuni
  7815. * @protected
  7816. * @since 4.5.027 (2009-03-16)
  7817. */
  7818. protected function rfread($handle, $length) {
  7819. $data = fread($handle, $length);
  7820. if ($data === false) {
  7821. return false;
  7822. }
  7823. $rest = $length - strlen($data);
  7824. if ($rest > 0) {
  7825. $data .= $this->rfread($handle, $rest);
  7826. }
  7827. return $data;
  7828. }
  7829. /**
  7830. * Extract info from a PNG image with alpha channel using the GD library.
  7831. * @param $file (string) Name of the file containing the image.
  7832. * @param $x (float) Abscissa of the upper-left corner.
  7833. * @param $y (float) Ordinate of the upper-left corner.
  7834. * @param $wpx (float) Original width of the image in pixels.
  7835. * @param $hpx (float) original height of the image in pixels.
  7836. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7837. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7838. * @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.
  7839. * @param $link (mixed) URL or identifier returned by AddLink().
  7840. * @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>
  7841. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7842. * @param $dpi (int) dot-per-inch resolution used on resize
  7843. * @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>
  7844. * @param $filehash (string) File hash used to build unique file names.
  7845. * @author Nicola Asuni
  7846. * @protected
  7847. * @since 4.3.007 (2008-12-04)
  7848. * @see Image()
  7849. */
  7850. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  7851. if (empty($filehash)) {
  7852. $filehash = md5($file);
  7853. }
  7854. // create temp image file (without alpha channel)
  7855. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7856. // create temp alpha file
  7857. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7858. if (extension_loaded('imagick')) { // ImageMagick extension
  7859. // ImageMagick library
  7860. $img = new Imagick();
  7861. $img->readImage($file);
  7862. // clone image object
  7863. $imga = $img->clone();
  7864. // extract alpha channel
  7865. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  7866. $img->negateImage(true);
  7867. $img->setImageFormat('png');
  7868. $img->writeImage($tempfile_alpha);
  7869. // remove alpha channel
  7870. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  7871. $imga->setImageFormat('png');
  7872. $imga->writeImage($tempfile_plain);
  7873. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  7874. // generate images
  7875. $img = imagecreatefrompng($file);
  7876. $imgalpha = imagecreate($wpx, $hpx);
  7877. // generate gray scale palette (0 -> 255)
  7878. for ($c = 0; $c < 256; ++$c) {
  7879. ImageColorAllocate($imgalpha, $c, $c, $c);
  7880. }
  7881. // extract alpha channel
  7882. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7883. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7884. $color = imagecolorat($img, $xpx, $ypx);
  7885. $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)
  7886. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  7887. $alpha = $this->getGDgamma($alpha); // correct gamma
  7888. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  7889. }
  7890. }
  7891. imagepng($imgalpha, $tempfile_alpha);
  7892. imagedestroy($imgalpha);
  7893. // extract image without alpha channel
  7894. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7895. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7896. imagepng($imgplain, $tempfile_plain);
  7897. imagedestroy($imgplain);
  7898. } else {
  7899. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  7900. }
  7901. // embed mask image
  7902. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7903. // embed image, masked with previously embedded mask
  7904. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7905. // remove temp files
  7906. unlink($tempfile_alpha);
  7907. unlink($tempfile_plain);
  7908. }
  7909. /**
  7910. * Correct the gamma value to be used with GD library
  7911. * @param $v (float) the gamma value to be corrected
  7912. * @protected
  7913. * @since 4.3.007 (2008-12-04)
  7914. */
  7915. protected function getGDgamma($v) {
  7916. return (pow(($v / 255), 2.2) * 255);
  7917. }
  7918. /**
  7919. * Performs a line break.
  7920. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7921. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  7922. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  7923. * @public
  7924. * @since 1.0
  7925. * @see Cell()
  7926. */
  7927. public function Ln($h='', $cell=false) {
  7928. 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'])) {
  7929. // revove vertical space from the top of the column
  7930. return;
  7931. }
  7932. if ($cell) {
  7933. if ($this->rtl) {
  7934. $cellpadding = $this->cell_padding['R'];
  7935. } else {
  7936. $cellpadding = $this->cell_padding['L'];
  7937. }
  7938. } else {
  7939. $cellpadding = 0;
  7940. }
  7941. if ($this->rtl) {
  7942. $this->x = $this->w - $this->rMargin - $cellpadding;
  7943. } else {
  7944. $this->x = $this->lMargin + $cellpadding;
  7945. }
  7946. if (is_string($h)) {
  7947. $this->y += $this->lasth;
  7948. } else {
  7949. $this->y += $h;
  7950. }
  7951. $this->newline = true;
  7952. }
  7953. /**
  7954. * Returns the relative X value of current position.
  7955. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7956. * @return float
  7957. * @public
  7958. * @since 1.2
  7959. * @see SetX(), GetY(), SetY()
  7960. */
  7961. public function GetX() {
  7962. //Get x position
  7963. if ($this->rtl) {
  7964. return ($this->w - $this->x);
  7965. } else {
  7966. return $this->x;
  7967. }
  7968. }
  7969. /**
  7970. * Returns the absolute X value of current position.
  7971. * @return float
  7972. * @public
  7973. * @since 1.2
  7974. * @see SetX(), GetY(), SetY()
  7975. */
  7976. public function GetAbsX() {
  7977. return $this->x;
  7978. }
  7979. /**
  7980. * Returns the ordinate of the current position.
  7981. * @return float
  7982. * @public
  7983. * @since 1.0
  7984. * @see SetY(), GetX(), SetX()
  7985. */
  7986. public function GetY() {
  7987. return $this->y;
  7988. }
  7989. /**
  7990. * Defines the abscissa of the current position.
  7991. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7992. * @param $x (float) The value of the abscissa.
  7993. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7994. * @public
  7995. * @since 1.2
  7996. * @see GetX(), GetY(), SetY(), SetXY()
  7997. */
  7998. public function SetX($x, $rtloff=false) {
  7999. if (!$rtloff AND $this->rtl) {
  8000. if ($x >= 0) {
  8001. $this->x = $this->w - $x;
  8002. } else {
  8003. $this->x = abs($x);
  8004. }
  8005. } else {
  8006. if ($x >= 0) {
  8007. $this->x = $x;
  8008. } else {
  8009. $this->x = $this->w + $x;
  8010. }
  8011. }
  8012. if ($this->x < 0) {
  8013. $this->x = 0;
  8014. }
  8015. if ($this->x > $this->w) {
  8016. $this->x = $this->w;
  8017. }
  8018. }
  8019. /**
  8020. * Moves the current abscissa back to the left margin and sets the ordinate.
  8021. * If the passed value is negative, it is relative to the bottom of the page.
  8022. * @param $y (float) The value of the ordinate.
  8023. * @param $resetx (bool) if true (default) reset the X position.
  8024. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8025. * @public
  8026. * @since 1.0
  8027. * @see GetX(), GetY(), SetY(), SetXY()
  8028. */
  8029. public function SetY($y, $resetx=true, $rtloff=false) {
  8030. if ($resetx) {
  8031. //reset x
  8032. if (!$rtloff AND $this->rtl) {
  8033. $this->x = $this->w - $this->rMargin;
  8034. } else {
  8035. $this->x = $this->lMargin;
  8036. }
  8037. }
  8038. if ($y >= 0) {
  8039. $this->y = $y;
  8040. } else {
  8041. $this->y = $this->h + $y;
  8042. }
  8043. if ($this->y < 0) {
  8044. $this->y = 0;
  8045. }
  8046. if ($this->y > $this->h) {
  8047. $this->y = $this->h;
  8048. }
  8049. }
  8050. /**
  8051. * Defines the abscissa and ordinate of the current position.
  8052. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8053. * @param $x (float) The value of the abscissa.
  8054. * @param $y (float) The value of the ordinate.
  8055. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8056. * @public
  8057. * @since 1.2
  8058. * @see SetX(), SetY()
  8059. */
  8060. public function SetXY($x, $y, $rtloff=false) {
  8061. $this->SetY($y, false, $rtloff);
  8062. $this->SetX($x, $rtloff);
  8063. }
  8064. /**
  8065. * Ouput input data and compress it if possible.
  8066. * @param $data (string) Data to output.
  8067. * @param $length (int) Data length in bytes.
  8068. * @protected
  8069. * @since 5.9.086
  8070. */
  8071. protected function sendOutputData($data, $length) {
  8072. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8073. // the content length may vary if the server is using compression
  8074. header('Content-Length: '.$length);
  8075. }
  8076. echo $data;
  8077. }
  8078. /**
  8079. * Send the document to a given destination: string, local file or browser.
  8080. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8081. * The method first calls Close() if necessary to terminate the document.
  8082. * @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.
  8083. * @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>
  8084. * @public
  8085. * @since 1.0
  8086. * @see Close()
  8087. */
  8088. public function Output($name='doc.pdf', $dest='I') {
  8089. //Output PDF to some destination
  8090. //Finish document if necessary
  8091. if ($this->state < 3) {
  8092. $this->Close();
  8093. }
  8094. //Normalize parameters
  8095. if (is_bool($dest)) {
  8096. $dest = $dest ? 'D' : 'F';
  8097. }
  8098. $dest = strtoupper($dest);
  8099. if ($dest{0} != 'F') {
  8100. $name = preg_replace('/[\s]+/', '_', $name);
  8101. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8102. }
  8103. if ($this->sign) {
  8104. // *** apply digital signature to the document ***
  8105. // get the document content
  8106. $pdfdoc = $this->getBuffer();
  8107. // remove last newline
  8108. $pdfdoc = substr($pdfdoc, 0, -1);
  8109. // Remove the original buffer
  8110. if (isset($this->diskcache) AND $this->diskcache) {
  8111. // remove buffer file from cache
  8112. unlink($this->buffer);
  8113. }
  8114. unset($this->buffer);
  8115. // remove filler space
  8116. $byterange_string_len = strlen($this->byterange_string);
  8117. // define the ByteRange
  8118. $byte_range = array();
  8119. $byte_range[0] = 0;
  8120. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8121. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8122. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8123. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8124. // replace the ByteRange
  8125. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8126. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8127. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8128. // write the document to a temporary folder
  8129. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8130. $f = fopen($tempdoc, 'wb');
  8131. if (!$f) {
  8132. $this->Error('Unable to create temporary file: '.$tempdoc);
  8133. }
  8134. $pdfdoc_length = strlen($pdfdoc);
  8135. fwrite($f, $pdfdoc, $pdfdoc_length);
  8136. fclose($f);
  8137. // get digital signature via openssl library
  8138. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8139. if (empty($this->signature_data['extracerts'])) {
  8140. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8141. } else {
  8142. 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']);
  8143. }
  8144. unlink($tempdoc);
  8145. // read signature
  8146. $signature = file_get_contents($tempsign);
  8147. unlink($tempsign);
  8148. // extract signature
  8149. $signature = substr($signature, $pdfdoc_length);
  8150. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8151. $tmparr = explode("\n\n", $signature);
  8152. $signature = $tmparr[1];
  8153. unset($tmparr);
  8154. // decode signature
  8155. $signature = base64_decode(trim($signature));
  8156. // convert signature to hex
  8157. $signature = current(unpack('H*', $signature));
  8158. $signature = str_pad($signature, $this->signature_max_length, '0');
  8159. // disable disk caching
  8160. $this->diskcache = false;
  8161. // Add signature to the document
  8162. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8163. $this->bufferlen = strlen($this->buffer);
  8164. }
  8165. switch($dest) {
  8166. case 'I': {
  8167. // Send PDF to the standard output
  8168. if (ob_get_contents()) {
  8169. $this->Error('Some data has already been output, can\'t send PDF file');
  8170. }
  8171. if (php_sapi_name() != 'cli') {
  8172. // send output to a browser
  8173. header('Content-Type: application/pdf');
  8174. if (headers_sent()) {
  8175. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8176. }
  8177. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8178. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8179. header('Pragma: public');
  8180. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8181. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8182. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8183. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8184. } else {
  8185. echo $this->getBuffer();
  8186. }
  8187. break;
  8188. }
  8189. case 'D': {
  8190. // download PDF as file
  8191. if (ob_get_contents()) {
  8192. $this->Error('Some data has already been output, can\'t send PDF file');
  8193. }
  8194. header('Content-Description: File Transfer');
  8195. if (headers_sent()) {
  8196. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8197. }
  8198. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8199. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8200. header('Pragma: public');
  8201. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8202. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8203. // force download dialog
  8204. if (strpos(php_sapi_name(), 'cgi') === false) {
  8205. header('Content-Type: application/force-download');
  8206. header('Content-Type: application/octet-stream', false);
  8207. header('Content-Type: application/download', false);
  8208. header('Content-Type: application/pdf', false);
  8209. } else {
  8210. header('Content-Type: application/pdf');
  8211. }
  8212. // use the Content-Disposition header to supply a recommended filename
  8213. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8214. header('Content-Transfer-Encoding: binary');
  8215. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8216. break;
  8217. }
  8218. case 'F':
  8219. case 'FI':
  8220. case 'FD': {
  8221. // save PDF to a local file
  8222. if ($this->diskcache) {
  8223. copy($this->buffer, $name);
  8224. } else {
  8225. $f = fopen($name, 'wb');
  8226. if (!$f) {
  8227. $this->Error('Unable to create output file: '.$name);
  8228. }
  8229. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8230. fclose($f);
  8231. }
  8232. if ($dest == 'FI') {
  8233. // send headers to browser
  8234. header('Content-Type: application/pdf');
  8235. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8236. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8237. header('Pragma: public');
  8238. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8239. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8240. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8241. $this->sendOutputData(file_get_contents($name), filesize($name));
  8242. } elseif ($dest == 'FD') {
  8243. // send headers to browser
  8244. if (ob_get_contents()) {
  8245. $this->Error('Some data has already been output, can\'t send PDF file');
  8246. }
  8247. header('Content-Description: File Transfer');
  8248. if (headers_sent()) {
  8249. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8250. }
  8251. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8252. header('Pragma: public');
  8253. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8254. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8255. // force download dialog
  8256. if (strpos(php_sapi_name(), 'cgi') === false) {
  8257. header('Content-Type: application/force-download');
  8258. header('Content-Type: application/octet-stream', false);
  8259. header('Content-Type: application/download', false);
  8260. header('Content-Type: application/pdf', false);
  8261. } else {
  8262. header('Content-Type: application/pdf');
  8263. }
  8264. // use the Content-Disposition header to supply a recommended filename
  8265. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8266. header('Content-Transfer-Encoding: binary');
  8267. $this->sendOutputData(file_get_contents($name), filesize($name));
  8268. }
  8269. break;
  8270. }
  8271. case 'E': {
  8272. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8273. $retval = 'Content-Type: application/pdf;'."\r\n";
  8274. $retval .= ' name="'.$name.'"'."\r\n";
  8275. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8276. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8277. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8278. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8279. return $retval;
  8280. }
  8281. case 'S': {
  8282. // returns PDF as a string
  8283. return $this->getBuffer();
  8284. }
  8285. default: {
  8286. $this->Error('Incorrect output destination: '.$dest);
  8287. }
  8288. }
  8289. return '';
  8290. }
  8291. /**
  8292. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8293. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8294. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8295. * @public
  8296. * @since 4.5.016 (2009-02-24)
  8297. */
  8298. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8299. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8300. // remove buffer file from cache
  8301. unlink($this->buffer);
  8302. }
  8303. foreach (array_keys(get_object_vars($this)) as $val) {
  8304. if ($destroyall OR (
  8305. ($val != 'internal_encoding')
  8306. AND ($val != 'state')
  8307. AND ($val != 'bufferlen')
  8308. AND ($val != 'buffer')
  8309. AND ($val != 'diskcache')
  8310. AND ($val != 'sign')
  8311. AND ($val != 'signature_data')
  8312. AND ($val != 'signature_max_length')
  8313. AND ($val != 'byterange_string')
  8314. )) {
  8315. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8316. unset($this->$val);
  8317. }
  8318. }
  8319. }
  8320. }
  8321. /**
  8322. * Check for locale-related bug
  8323. * @protected
  8324. */
  8325. protected function _dochecks() {
  8326. //Check for locale-related bug
  8327. if (1.1 == 1) {
  8328. $this->Error('Don\'t alter the locale before including class file');
  8329. }
  8330. //Check for decimal separator
  8331. if (sprintf('%.1F', 1.0) != '1.0') {
  8332. setlocale(LC_NUMERIC, 'C');
  8333. }
  8334. }
  8335. /**
  8336. * Return fonts path
  8337. * @return string
  8338. * @protected
  8339. */
  8340. protected function _getfontpath() {
  8341. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8342. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8343. }
  8344. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8345. }
  8346. /**
  8347. * Return an array containing variations for the basic page number alias.
  8348. * @param $a (string) Base alias.
  8349. * @return array of page number aliases
  8350. * @protected
  8351. */
  8352. protected function getInternalPageNumberAliases($a= '') {
  8353. $alias = array();
  8354. // build array of Unicode + ASCII variants (the order is important)
  8355. $alias = array('u' => array(), 'a' => array());
  8356. $u = '{'.$a.'}';
  8357. $alias['u'][] = $this->_escape($u);
  8358. if ($this->isunicode) {
  8359. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8360. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8361. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8362. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8363. }
  8364. $alias['a'][] = $this->_escape($a);
  8365. return $alias;
  8366. }
  8367. /**
  8368. * Return an array containing all internal page aliases.
  8369. * @return array of page number aliases
  8370. * @protected
  8371. */
  8372. protected function getAllInternalPageNumberAliases() {
  8373. $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);
  8374. $pnalias = array();
  8375. foreach($basic_alias as $k => $a) {
  8376. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8377. }
  8378. return $pnalias;
  8379. }
  8380. /**
  8381. * Replace page number aliases with number.
  8382. * @param $page (string) Page content.
  8383. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8384. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8385. * @return replaced page content and updated $diff parameter as array.
  8386. * @protected
  8387. */
  8388. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8389. foreach ($replace as $rep) {
  8390. foreach ($rep[3] as $a) {
  8391. if (strpos($page, $a) !== false) {
  8392. $page = str_replace($a, $rep[0], $page);
  8393. $diff += ($rep[2] - $rep[1]);
  8394. }
  8395. }
  8396. }
  8397. return array($page, $diff);
  8398. }
  8399. /**
  8400. * Replace right shift page number aliases with spaces to correct right alignment.
  8401. * This works perfectly only when using monospaced fonts.
  8402. * @param $page (string) Page content.
  8403. * @param $aliases (array) Array of page aliases.
  8404. * @param $diff (int) initial difference to add.
  8405. * @return replaced page content.
  8406. * @protected
  8407. */
  8408. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8409. foreach ($aliases as $type => $alias) {
  8410. foreach ($alias as $a) {
  8411. // find position of compensation factor
  8412. $startnum = (strpos($a, ':') + 1);
  8413. $a = substr($a, 0, $startnum);
  8414. if (($pos = strpos($page, $a)) !== false) {
  8415. // end of alias
  8416. $endnum = strpos($page, '}', $pos);
  8417. // string to be replaced
  8418. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8419. // get compensation factor
  8420. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8421. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8422. $ratio = floatval($ratio);
  8423. if ($type == 'u') {
  8424. $chrdiff = floor(($diff + 12) * $ratio);
  8425. $shift = str_repeat(' ', $chrdiff);
  8426. $shift = $this->UTF8ToUTF16BE($shift, false);
  8427. } else {
  8428. $chrdiff = floor(($diff + 11) * $ratio);
  8429. $shift = str_repeat(' ', $chrdiff);
  8430. }
  8431. $page = str_replace($aa, $shift, $page);
  8432. }
  8433. }
  8434. }
  8435. return $page;
  8436. }
  8437. /**
  8438. * Output pages (and replace page number aliases).
  8439. * @protected
  8440. */
  8441. protected function _putpages() {
  8442. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8443. // get internal aliases for page numbers
  8444. $pnalias = $this->getAllInternalPageNumberAliases();
  8445. $num_pages = $this->numpages;
  8446. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8447. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8448. $ptp_num_chars = $this->GetNumChars($ptpa);
  8449. $pagegroupnum = 0;
  8450. $groupnum = 0;
  8451. $ptgu = 1;
  8452. $ptga = 1;
  8453. for ($n = 1; $n <= $num_pages; ++$n) {
  8454. // get current page
  8455. $temppage = $this->getPageBuffer($n);
  8456. $pagelen = strlen($temppage);
  8457. // set replacements for total pages number
  8458. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8459. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8460. $pnp_num_chars = $this->GetNumChars($pnpa);
  8461. $pdiff = 0; // difference used for right shift alignment of page numbers
  8462. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8463. if (!empty($this->pagegroups)) {
  8464. if (isset($this->newpagegroup[$n])) {
  8465. $pagegroupnum = 0;
  8466. ++$groupnum;
  8467. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8468. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8469. $ptg_num_chars = $this->GetNumChars($ptga);
  8470. }
  8471. ++$pagegroupnum;
  8472. $pnga = $this->formatPageNumber($pagegroupnum);
  8473. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8474. $png_num_chars = $this->GetNumChars($pnga);
  8475. // replace page numbers
  8476. $replace = array();
  8477. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8478. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8479. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8480. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8481. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8482. }
  8483. // replace page numbers
  8484. $replace = array();
  8485. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8486. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8487. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8488. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8489. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8490. // replace right shift alias
  8491. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8492. // replace EPS marker
  8493. $temppage = str_replace($this->epsmarker, '', $temppage);
  8494. //Page
  8495. $this->page_obj_id[$n] = $this->_newobj();
  8496. $out = '<<';
  8497. $out .= ' /Type /Page';
  8498. $out .= ' /Parent 1 0 R';
  8499. $out .= ' /LastModified '.$this->_datestring();
  8500. $out .= ' /Resources 2 0 R';
  8501. $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8502. foreach ($boxes as $box) {
  8503. $out .= ' /'.$box;
  8504. $out .= sprintf(' [%.2F %.2F %.2F %.2F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  8505. }
  8506. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8507. $out .= ' /BoxColorInfo <<';
  8508. foreach ($boxes as $box) {
  8509. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8510. $out .= ' /'.$box.' <<';
  8511. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8512. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8513. $out .= ' /C [';
  8514. $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  8515. $out .= ' ]';
  8516. }
  8517. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8518. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8519. }
  8520. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8521. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8522. }
  8523. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8524. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8525. $out .= ' /D [';
  8526. foreach ($dashes as $dash) {
  8527. $out .= sprintf(' %.3F', ($dash * $this->k));
  8528. }
  8529. $out .= ' ]';
  8530. }
  8531. $out .= ' >>';
  8532. }
  8533. }
  8534. $out .= ' >>';
  8535. }
  8536. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8537. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8538. if (!$this->pdfa_mode) {
  8539. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8540. }
  8541. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8542. // page transitions
  8543. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8544. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8545. }
  8546. $out .= ' /Trans <<';
  8547. $out .= ' /Type /Trans';
  8548. if (isset($this->pagedim[$n]['trans']['S'])) {
  8549. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8550. }
  8551. if (isset($this->pagedim[$n]['trans']['D'])) {
  8552. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8553. }
  8554. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8555. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8556. }
  8557. if (isset($this->pagedim[$n]['trans']['M'])) {
  8558. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8559. }
  8560. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8561. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8562. }
  8563. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8564. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8565. }
  8566. if (isset($this->pagedim[$n]['trans']['B'])) {
  8567. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8568. }
  8569. $out .= ' >>';
  8570. }
  8571. $out .= $this->_getannotsrefs($n);
  8572. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8573. $out .= ' >>';
  8574. $out .= "\n".'endobj';
  8575. $this->_out($out);
  8576. //Page content
  8577. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8578. $this->_newobj();
  8579. $p = $this->_getrawstream($p);
  8580. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8581. if ($this->diskcache) {
  8582. // remove temporary files
  8583. unlink($this->pages[$n]);
  8584. }
  8585. }
  8586. //Pages root
  8587. $out = $this->_getobj(1)."\n";
  8588. $out .= '<< /Type /Pages /Kids [';
  8589. foreach($this->page_obj_id as $page_obj) {
  8590. $out .= ' '.$page_obj.' 0 R';
  8591. }
  8592. $out .= ' ] /Count '.$num_pages.' >>';
  8593. $out .= "\n".'endobj';
  8594. $this->_out($out);
  8595. }
  8596. /**
  8597. * Output references to page annotations
  8598. * @param $n (int) page number
  8599. * @protected
  8600. * @author Nicola Asuni
  8601. * @since 4.7.000 (2008-08-29)
  8602. * @deprecated
  8603. */
  8604. protected function _putannotsrefs($n) {
  8605. $this->_out($this->_getannotsrefs($n));
  8606. }
  8607. /**
  8608. * Get references to page annotations.
  8609. * @param $n (int) page number
  8610. * @return string
  8611. * @protected
  8612. * @author Nicola Asuni
  8613. * @since 5.0.010 (2010-05-17)
  8614. */
  8615. protected function _getannotsrefs($n) {
  8616. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8617. return '';
  8618. }
  8619. $out = ' /Annots [';
  8620. if (isset($this->PageAnnots[$n])) {
  8621. foreach ($this->PageAnnots[$n] as $key => $val) {
  8622. if (!in_array($val['n'], $this->radio_groups)) {
  8623. $out .= ' '.$val['n'].' 0 R';
  8624. }
  8625. }
  8626. // add radiobutton groups
  8627. if (isset($this->radiobutton_groups[$n])) {
  8628. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8629. if (isset($data['n'])) {
  8630. $out .= ' '.$data['n'].' 0 R';
  8631. }
  8632. }
  8633. }
  8634. }
  8635. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8636. // set reference for signature object
  8637. $out .= ' '.$this->sig_obj_id.' 0 R';
  8638. }
  8639. if (!empty($this->empty_signature_appearance)) {
  8640. foreach ($this->empty_signature_appearance as $esa) {
  8641. if ($esa['page'] == $n) {
  8642. // set reference for empty signature objects
  8643. $out .= ' '.$esa['objid'].' 0 R';
  8644. }
  8645. }
  8646. }
  8647. $out .= ' ]';
  8648. return $out;
  8649. }
  8650. /**
  8651. * Output annotations objects for all pages.
  8652. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8653. * See section 12.5 of PDF 32000_2008 reference.
  8654. * @protected
  8655. * @author Nicola Asuni
  8656. * @since 4.0.018 (2008-08-06)
  8657. */
  8658. protected function _putannotsobjs() {
  8659. // reset object counter
  8660. for ($n=1; $n <= $this->numpages; ++$n) {
  8661. if (isset($this->PageAnnots[$n])) {
  8662. // set page annotations
  8663. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8664. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8665. // create annotation object for grouping radiobuttons
  8666. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8667. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8668. $annots = '<<';
  8669. $annots .= ' /Type /Annot';
  8670. $annots .= ' /Subtype /Widget';
  8671. $annots .= ' /Rect [0 0 0 0]';
  8672. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  8673. // read only
  8674. $annots .= ' /F 68';
  8675. $annots .= ' /Ff 49153';
  8676. } else {
  8677. $annots .= ' /F 4'; // default print for PDF/A
  8678. $annots .= ' /Ff 49152';
  8679. }
  8680. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8681. $annots .= ' /FT /Btn';
  8682. $annots .= ' /Kids [';
  8683. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8684. if (isset($data['kid'])) {
  8685. $annots .= ' '.$data['kid'].' 0 R';
  8686. if ($data['def'] !== 'Off') {
  8687. $defval = $data['def'];
  8688. }
  8689. }
  8690. }
  8691. $annots .= ' ]';
  8692. if (isset($defval)) {
  8693. $annots .= ' /V /'.$defval;
  8694. }
  8695. $annots .= ' >>';
  8696. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8697. $this->form_obj_id[] = $radio_button_obj_id;
  8698. // store object id to be used on Parent entry of Kids
  8699. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8700. }
  8701. $formfield = false;
  8702. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8703. $a = $pl['x'] * $this->k;
  8704. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8705. $c = $pl['w'] * $this->k;
  8706. $d = $pl['h'] * $this->k;
  8707. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  8708. // create new annotation object
  8709. $annots = '<</Type /Annot';
  8710. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8711. $annots .= ' /Rect ['.$rect.']';
  8712. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8713. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8714. $annots .= ' /FT /'.$pl['opt']['ft'];
  8715. $formfield = true;
  8716. }
  8717. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8718. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8719. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8720. $annots .= ' /M '.$this->_datestring($annot_obj_id);
  8721. if (isset($pl['opt']['f'])) {
  8722. $fval = 0;
  8723. if (is_array($pl['opt']['f'])) {
  8724. foreach ($pl['opt']['f'] as $f) {
  8725. switch (strtolower($f)) {
  8726. case 'invisible': {
  8727. $fval += 1 << 0;
  8728. break;
  8729. }
  8730. case 'hidden': {
  8731. $fval += 1 << 1;
  8732. break;
  8733. }
  8734. case 'print': {
  8735. $fval += 1 << 2;
  8736. break;
  8737. }
  8738. case 'nozoom': {
  8739. $fval += 1 << 3;
  8740. break;
  8741. }
  8742. case 'norotate': {
  8743. $fval += 1 << 4;
  8744. break;
  8745. }
  8746. case 'noview': {
  8747. $fval += 1 << 5;
  8748. break;
  8749. }
  8750. case 'readonly': {
  8751. $fval += 1 << 6;
  8752. break;
  8753. }
  8754. case 'locked': {
  8755. $fval += 1 << 8;
  8756. break;
  8757. }
  8758. case 'togglenoview': {
  8759. $fval += 1 << 9;
  8760. break;
  8761. }
  8762. case 'lockedcontents': {
  8763. $fval += 1 << 10;
  8764. break;
  8765. }
  8766. default: {
  8767. break;
  8768. }
  8769. }
  8770. }
  8771. } else {
  8772. $fval = intval($pl['opt']['f']);
  8773. }
  8774. } else {
  8775. $fval = 4;
  8776. }
  8777. if ($this->pdfa_mode) {
  8778. // force print flag for PDF/A mode
  8779. $fval |= 4;
  8780. }
  8781. $annots .= ' /F '.intval($fval);
  8782. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8783. $annots .= ' /AS /'.$pl['opt']['as'];
  8784. }
  8785. if (isset($pl['opt']['ap'])) {
  8786. // appearance stream
  8787. $annots .= ' /AP <<';
  8788. if (is_array($pl['opt']['ap'])) {
  8789. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8790. // $apmode can be: n = normal; r = rollover; d = down;
  8791. $annots .= ' /'.strtoupper($apmode);
  8792. if (is_array($apdef)) {
  8793. $annots .= ' <<';
  8794. foreach ($apdef as $apstate => $stream) {
  8795. // reference to XObject that define the appearance for this mode-state
  8796. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8797. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8798. }
  8799. $annots .= ' >>';
  8800. } else {
  8801. // reference to XObject that define the appearance for this mode
  8802. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8803. $annots .= ' '.$apsobjid.' 0 R';
  8804. }
  8805. }
  8806. } else {
  8807. $annots .= $pl['opt']['ap'];
  8808. }
  8809. $annots .= ' >>';
  8810. }
  8811. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8812. $annots .= ' /BS <<';
  8813. $annots .= ' /Type /Border';
  8814. if (isset($pl['opt']['bs']['w'])) {
  8815. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8816. }
  8817. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8818. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8819. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8820. }
  8821. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8822. $annots .= ' /D [';
  8823. foreach ($pl['opt']['bs']['d'] as $cord) {
  8824. $annots .= ' '.intval($cord);
  8825. }
  8826. $annots .= ']';
  8827. }
  8828. $annots .= ' >>';
  8829. } else {
  8830. $annots .= ' /Border [';
  8831. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8832. $annots .= intval($pl['opt']['border'][0]).' ';
  8833. $annots .= intval($pl['opt']['border'][1]).' ';
  8834. $annots .= intval($pl['opt']['border'][2]);
  8835. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  8836. $annots .= ' [';
  8837. foreach ($pl['opt']['border'][3] as $dash) {
  8838. $annots .= intval($dash).' ';
  8839. }
  8840. $annots .= ']';
  8841. }
  8842. } else {
  8843. $annots .= '0 0 0';
  8844. }
  8845. $annots .= ']';
  8846. }
  8847. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  8848. $annots .= ' /BE <<';
  8849. $bstyles = array('S', 'C');
  8850. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  8851. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8852. } else {
  8853. $annots .= ' /S /S';
  8854. }
  8855. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8856. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  8857. }
  8858. $annots .= '>>';
  8859. }
  8860. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8861. $annots .= ' /C '.$this->getColorStringFromArray($pl['opt']['c']);
  8862. }
  8863. //$annots .= ' /StructParent ';
  8864. //$annots .= ' /OC ';
  8865. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8866. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8867. // this is a markup type
  8868. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8869. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8870. }
  8871. //$annots .= ' /Popup ';
  8872. if (isset($pl['opt']['ca'])) {
  8873. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  8874. }
  8875. if (isset($pl['opt']['rc'])) {
  8876. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8877. }
  8878. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
  8879. //$annots .= ' /IRT ';
  8880. if (isset($pl['opt']['subj'])) {
  8881. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8882. }
  8883. //$annots .= ' /RT ';
  8884. //$annots .= ' /IT ';
  8885. //$annots .= ' /ExData ';
  8886. }
  8887. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8888. // Annotation types
  8889. switch (strtolower($pl['opt']['subtype'])) {
  8890. case 'text': {
  8891. if (isset($pl['opt']['open'])) {
  8892. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8893. }
  8894. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8895. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8896. $annots .= ' /Name /'.$pl['opt']['name'];
  8897. } else {
  8898. $annots .= ' /Name /Note';
  8899. }
  8900. $statemodels = array('Marked', 'Review');
  8901. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8902. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8903. } else {
  8904. $pl['opt']['statemodel'] = 'Marked';
  8905. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8906. }
  8907. if ($pl['opt']['statemodel'] == 'Marked') {
  8908. $states = array('Accepted', 'Unmarked');
  8909. } else {
  8910. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8911. }
  8912. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  8913. $annots .= ' /State /'.$pl['opt']['state'];
  8914. } else {
  8915. if ($pl['opt']['statemodel'] == 'Marked') {
  8916. $annots .= ' /State /Unmarked';
  8917. } else {
  8918. $annots .= ' /State /None';
  8919. }
  8920. }
  8921. break;
  8922. }
  8923. case 'link': {
  8924. if (is_string($pl['txt'])) {
  8925. // external URI link
  8926. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8927. } else {
  8928. // internal link
  8929. $l = $this->links[$pl['txt']];
  8930. if (isset($this->page_obj_id[($l[0])])) {
  8931. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  8932. }
  8933. }
  8934. $hmodes = array('N', 'I', 'O', 'P');
  8935. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8936. $annots .= ' /H /'.$pl['opt']['h'];
  8937. } else {
  8938. $annots .= ' /H /I';
  8939. }
  8940. //$annots .= ' /PA ';
  8941. //$annots .= ' /Quadpoints ';
  8942. break;
  8943. }
  8944. case 'freetext': {
  8945. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8946. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8947. }
  8948. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8949. $annots .= ' /Q '.intval($pl['opt']['q']);
  8950. }
  8951. if (isset($pl['opt']['rc'])) {
  8952. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8953. }
  8954. if (isset($pl['opt']['ds'])) {
  8955. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8956. }
  8957. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8958. $annots .= ' /CL [';
  8959. foreach ($pl['opt']['cl'] as $cl) {
  8960. $annots .= sprintf('%.4F ', $cl * $this->k);
  8961. }
  8962. $annots .= ']';
  8963. }
  8964. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8965. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8966. $annots .= ' /IT /'.$pl['opt']['it'];
  8967. }
  8968. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8969. $l = $pl['opt']['rd'][0] * $this->k;
  8970. $r = $pl['opt']['rd'][1] * $this->k;
  8971. $t = $pl['opt']['rd'][2] * $this->k;
  8972. $b = $pl['opt']['rd'][3] * $this->k;
  8973. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  8974. }
  8975. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8976. $annots .= ' /LE /'.$pl['opt']['le'];
  8977. }
  8978. break;
  8979. }
  8980. case 'line': {
  8981. break;
  8982. }
  8983. case 'square': {
  8984. break;
  8985. }
  8986. case 'circle': {
  8987. break;
  8988. }
  8989. case 'polygon': {
  8990. break;
  8991. }
  8992. case 'polyline': {
  8993. break;
  8994. }
  8995. case 'highlight': {
  8996. break;
  8997. }
  8998. case 'underline': {
  8999. break;
  9000. }
  9001. case 'squiggly': {
  9002. break;
  9003. }
  9004. case 'strikeout': {
  9005. break;
  9006. }
  9007. case 'stamp': {
  9008. break;
  9009. }
  9010. case 'caret': {
  9011. break;
  9012. }
  9013. case 'ink': {
  9014. break;
  9015. }
  9016. case 'popup': {
  9017. break;
  9018. }
  9019. case 'fileattachment': {
  9020. if ($this->pdfa_mode) {
  9021. // embedded files are not allowed in PDF/A mode
  9022. break;
  9023. }
  9024. if (!isset($pl['opt']['fs'])) {
  9025. break;
  9026. }
  9027. $filename = basename($pl['opt']['fs']);
  9028. if (isset($this->embeddedfiles[$filename]['n'])) {
  9029. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9030. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9031. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9032. $annots .= ' /Name /'.$pl['opt']['name'];
  9033. } else {
  9034. $annots .= ' /Name /PushPin';
  9035. }
  9036. }
  9037. break;
  9038. }
  9039. case 'sound': {
  9040. if (!isset($pl['opt']['fs'])) {
  9041. break;
  9042. }
  9043. $filename = basename($pl['opt']['fs']);
  9044. if (isset($this->embeddedfiles[$filename]['n'])) {
  9045. // ... TO BE COMPLETED ...
  9046. // /R /C /B /E /CO /CP
  9047. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9048. $iconsapp = array('Speaker', 'Mic');
  9049. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9050. $annots .= ' /Name /'.$pl['opt']['name'];
  9051. } else {
  9052. $annots .= ' /Name /Speaker';
  9053. }
  9054. }
  9055. break;
  9056. }
  9057. case 'movie': {
  9058. break;
  9059. }
  9060. case 'widget': {
  9061. $hmode = array('N', 'I', 'O', 'P', 'T');
  9062. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9063. $annots .= ' /H /'.$pl['opt']['h'];
  9064. }
  9065. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9066. $annots .= ' /MK <<';
  9067. if (isset($pl['opt']['mk']['r'])) {
  9068. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9069. }
  9070. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9071. $annots .= ' /BC '.$this->getColorStringFromArray($pl['opt']['mk']['bc']);
  9072. }
  9073. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9074. $annots .= ' /BG '.$this->getColorStringFromArray($pl['opt']['mk']['bg']);
  9075. }
  9076. if (isset($pl['opt']['mk']['ca'])) {
  9077. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9078. }
  9079. if (isset($pl['opt']['mk']['rc'])) {
  9080. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9081. }
  9082. if (isset($pl['opt']['mk']['ac'])) {
  9083. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9084. }
  9085. if (isset($pl['opt']['mk']['i'])) {
  9086. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9087. if ($info !== false) {
  9088. $annots .= ' /I '.$info['n'].' 0 R';
  9089. }
  9090. }
  9091. if (isset($pl['opt']['mk']['ri'])) {
  9092. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9093. if ($info !== false) {
  9094. $annots .= ' /RI '.$info['n'].' 0 R';
  9095. }
  9096. }
  9097. if (isset($pl['opt']['mk']['ix'])) {
  9098. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9099. if ($info !== false) {
  9100. $annots .= ' /IX '.$info['n'].' 0 R';
  9101. }
  9102. }
  9103. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9104. $annots .= ' /IF <<';
  9105. $if_sw = array('A', 'B', 'S', 'N');
  9106. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9107. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9108. }
  9109. $if_s = array('A', 'P');
  9110. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9111. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9112. }
  9113. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9114. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9115. }
  9116. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9117. $annots .= ' /FB true';
  9118. }
  9119. $annots .= '>>';
  9120. }
  9121. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9122. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9123. }
  9124. $annots .= '>>';
  9125. } // end MK
  9126. // --- Entries for field dictionaries ---
  9127. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9128. // set parent
  9129. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9130. }
  9131. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9132. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9133. }
  9134. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9135. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9136. }
  9137. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9138. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9139. }
  9140. if (isset($pl['opt']['ff'])) {
  9141. if (is_array($pl['opt']['ff'])) {
  9142. // array of bit settings
  9143. $flag = 0;
  9144. foreach($pl['opt']['ff'] as $val) {
  9145. $flag += 1 << ($val - 1);
  9146. }
  9147. } else {
  9148. $flag = intval($pl['opt']['ff']);
  9149. }
  9150. $annots .= ' /Ff '.$flag;
  9151. }
  9152. if (isset($pl['opt']['maxlen'])) {
  9153. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9154. }
  9155. if (isset($pl['opt']['v'])) {
  9156. $annots .= ' /V';
  9157. if (is_array($pl['opt']['v'])) {
  9158. foreach ($pl['opt']['v'] AS $optval) {
  9159. if (is_float($optval)) {
  9160. $optval = sprintf('%.2F', $optval);
  9161. }
  9162. $annots .= ' '.$optval;
  9163. }
  9164. } else {
  9165. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9166. }
  9167. }
  9168. if (isset($pl['opt']['dv'])) {
  9169. $annots .= ' /DV';
  9170. if (is_array($pl['opt']['dv'])) {
  9171. foreach ($pl['opt']['dv'] AS $optval) {
  9172. if (is_float($optval)) {
  9173. $optval = sprintf('%.2F', $optval);
  9174. }
  9175. $annots .= ' '.$optval;
  9176. }
  9177. } else {
  9178. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9179. }
  9180. }
  9181. if (isset($pl['opt']['rv'])) {
  9182. $annots .= ' /RV';
  9183. if (is_array($pl['opt']['rv'])) {
  9184. foreach ($pl['opt']['rv'] AS $optval) {
  9185. if (is_float($optval)) {
  9186. $optval = sprintf('%.2F', $optval);
  9187. }
  9188. $annots .= ' '.$optval;
  9189. }
  9190. } else {
  9191. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9192. }
  9193. }
  9194. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9195. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9196. }
  9197. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9198. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9199. }
  9200. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9201. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9202. }
  9203. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9204. $annots .= ' /Q '.intval($pl['opt']['q']);
  9205. }
  9206. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9207. $annots .= ' /Opt [';
  9208. foreach($pl['opt']['opt'] AS $copt) {
  9209. if (is_array($copt)) {
  9210. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9211. } else {
  9212. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9213. }
  9214. }
  9215. $annots .= ']';
  9216. }
  9217. if (isset($pl['opt']['ti'])) {
  9218. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9219. }
  9220. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9221. $annots .= ' /I [';
  9222. foreach($pl['opt']['i'] AS $copt) {
  9223. $annots .= intval($copt).' ';
  9224. }
  9225. $annots .= ']';
  9226. }
  9227. break;
  9228. }
  9229. case 'screen': {
  9230. break;
  9231. }
  9232. case 'printermark': {
  9233. break;
  9234. }
  9235. case 'trapnet': {
  9236. break;
  9237. }
  9238. case 'watermark': {
  9239. break;
  9240. }
  9241. case '3d': {
  9242. break;
  9243. }
  9244. default: {
  9245. break;
  9246. }
  9247. }
  9248. $annots .= '>>';
  9249. // create new annotation object
  9250. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9251. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9252. // store reference of form object
  9253. $this->form_obj_id[] = $annot_obj_id;
  9254. }
  9255. }
  9256. }
  9257. } // end for each page
  9258. }
  9259. /**
  9260. * Put appearance streams XObject used to define annotation's appearance states.
  9261. * @param $w (int) annotation width
  9262. * @param $h (int) annotation height
  9263. * @param $stream (string) appearance stream
  9264. * @return int object ID
  9265. * @protected
  9266. * @since 4.8.001 (2009-09-09)
  9267. */
  9268. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9269. $stream = trim($stream);
  9270. $out = $this->_getobj()."\n";
  9271. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9272. $out .= '<<';
  9273. $out .= ' /Type /XObject';
  9274. $out .= ' /Subtype /Form';
  9275. $out .= ' /FormType 1';
  9276. if ($this->compress) {
  9277. $stream = gzcompress($stream);
  9278. $out .= ' /Filter /FlateDecode';
  9279. }
  9280. $rect = sprintf('%.2F %.2F', $w, $h);
  9281. $out .= ' /BBox [0 0 '.$rect.']';
  9282. $out .= ' /Matrix [1 0 0 1 0 0]';
  9283. $out .= ' /Resources 2 0 R';
  9284. $stream = $this->_getrawstream($stream);
  9285. $out .= ' /Length '.strlen($stream);
  9286. $out .= ' >>';
  9287. $out .= ' stream'."\n".$stream."\n".'endstream';
  9288. $out .= "\n".'endobj';
  9289. $this->_out($out);
  9290. return $this->n;
  9291. }
  9292. /**
  9293. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9294. * @param $str (string) string from where to extract value
  9295. * @param $offset (int) point from where to read the data
  9296. * @return int 32 bit value
  9297. * @author Nicola Asuni
  9298. * @protected
  9299. * @since 5.2.000 (2010-06-02)
  9300. */
  9301. protected function _getULONG($str, $offset) {
  9302. $v = unpack('Ni', substr($str, $offset, 4));
  9303. return $v['i'];
  9304. }
  9305. /**
  9306. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9307. * @param $str (string) string from where to extract value
  9308. * @param $offset (int) point from where to read the data
  9309. * @return int 16 bit value
  9310. * @author Nicola Asuni
  9311. * @protected
  9312. * @since 5.2.000 (2010-06-02)
  9313. */
  9314. protected function _getUSHORT($str, $offset) {
  9315. $v = unpack('ni', substr($str, $offset, 2));
  9316. return $v['i'];
  9317. }
  9318. /**
  9319. * Get SHORT from string (Big Endian 16-bit signed integer).
  9320. * @param $str (string) string from where to extract value
  9321. * @param $offset (int) point from where to read the data
  9322. * @return int 16 bit value
  9323. * @author Nicola Asuni
  9324. * @protected
  9325. * @since 5.2.000 (2010-06-02)
  9326. */
  9327. protected function _getSHORT($str, $offset) {
  9328. $v = unpack('si', substr($str, $offset, 2));
  9329. return $v['i'];
  9330. }
  9331. /**
  9332. * Get FWORD from string (Big Endian 16-bit signed integer).
  9333. * @param $str (string) string from where to extract value
  9334. * @param $offset (int) point from where to read the data
  9335. * @return int 16 bit value
  9336. * @author Nicola Asuni
  9337. * @protected
  9338. * @since 5.9.123 (2011-09-30)
  9339. */
  9340. protected function _getFWORD($str, $offset) {
  9341. $v = $this->_getUSHORT($str, $offset);
  9342. if ($v > 0x7fff) {
  9343. $v -= 0x10000;
  9344. }
  9345. return $v;
  9346. }
  9347. /**
  9348. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  9349. * @param $str (string) string from where to extract value
  9350. * @param $offset (int) point from where to read the data
  9351. * @return int 16 bit value
  9352. * @author Nicola Asuni
  9353. * @protected
  9354. * @since 5.9.123 (2011-09-30)
  9355. */
  9356. protected function _getUFWORD($str, $offset) {
  9357. $v = $this->_getUSHORT($str, $offset);
  9358. return $v;
  9359. }
  9360. /**
  9361. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9362. * @param $str (string) string from where to extract value
  9363. * @param $offset (int) point from where to read the data
  9364. * @return int 16 bit value
  9365. * @author Nicola Asuni
  9366. * @protected
  9367. * @since 5.9.123 (2011-09-30)
  9368. */
  9369. protected function _getFIXED($str, $offset) {
  9370. // mantissa
  9371. $m = $this->_getFWORD($str, $offset);
  9372. // fraction
  9373. $f = $this->_getUSHORT($str, ($offset + 2));
  9374. $v = floatval(''.$m.'.'.$f.'');
  9375. return $v;
  9376. }
  9377. /**
  9378. * Get BYTE from string (8-bit unsigned integer).
  9379. * @param $str (string) String from where to extract value.
  9380. * @param $offset (int) Point from where to read the data.
  9381. * @return int 8 bit value
  9382. * @author Nicola Asuni
  9383. * @protected
  9384. * @since 5.2.000 (2010-06-02)
  9385. */
  9386. protected function _getBYTE($str, $offset) {
  9387. $v = unpack('Ci', substr($str, $offset, 1));
  9388. return $v['i'];
  9389. }
  9390. /**
  9391. * Update the CIDToGIDMap string with a new value.
  9392. * @param $map (string) CIDToGIDMap.
  9393. * @param $cid (int) CID value.
  9394. * @param $gid (int) GID value.
  9395. * @return (string) CIDToGIDMap.
  9396. * @author Nicola Asuni
  9397. * @protected
  9398. * @since 5.9.123 (2011-09-29)
  9399. */
  9400. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9401. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9402. if ($gid > 0xFFFF) {
  9403. $gid -= 0x10000;
  9404. }
  9405. $map[($cid * 2)] = chr($gid >> 8);
  9406. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9407. }
  9408. return $map;
  9409. }
  9410. /**
  9411. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9412. * @param $fontfile (string) TrueType or Type1 font file (full path).
  9413. * @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.
  9414. * @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.
  9415. * @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.
  9416. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9417. * @return (string) TCPDF font name.
  9418. * @author Nicola Asuni
  9419. * @public
  9420. * @since 5.9.123 (2010-09-30)
  9421. */
  9422. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='') {
  9423. if (!file_exists($fontfile)) {
  9424. $this->Error('Could not find file: '.$fontfile.'');
  9425. }
  9426. // font metrics
  9427. $fmetric = array();
  9428. // build new font name for TCPDF compatibility
  9429. $font_path_parts = pathinfo($fontfile);
  9430. if (!isset($font_path_parts['filename'])) {
  9431. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9432. }
  9433. $font_name = strtolower($font_path_parts['filename']);
  9434. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9435. $search = array('bold', 'oblique', 'italic', 'regular');
  9436. $replace = array('b', 'i', 'i', '');
  9437. $font_name = str_replace($search, $replace, $font_name);
  9438. if (empty($font_name)) {
  9439. // set generic name
  9440. $font_name = 'tcpdffont';
  9441. }
  9442. // set output path
  9443. if (empty($outpath)) {
  9444. $outpath = $this->_getfontpath();
  9445. }
  9446. // check if this font already exist
  9447. if (file_exists($outpath.$font_name.'.php')) {
  9448. // this font already exist (delete it from fonts folder to rebuild it)
  9449. return $font_name;
  9450. }
  9451. $fmetric['file'] = $font_name.'.z';
  9452. $fmetric['ctg'] = $font_name.'.ctg.z';
  9453. // get font data
  9454. $font = file_get_contents($fontfile);
  9455. $fmetric['originalsize'] = strlen($font);
  9456. // autodetect font type
  9457. if (empty($fonttype)) {
  9458. if ($this->_getULONG($font, 0) == 0x10000) {
  9459. // True Type (Unicode or not)
  9460. $fonttype = 'TrueTypeUnicode';
  9461. } elseif (substr($font, 0, 4) == 'OTTO') {
  9462. // Open Type (Unicode or not)
  9463. $this->Error('Unsupported font format: OpenType with CFF data.');
  9464. } else {
  9465. // Type 1
  9466. $fonttype = 'Type1';
  9467. }
  9468. }
  9469. // set font type
  9470. switch ($fonttype) {
  9471. case 'CID0CT':
  9472. case 'CID0CS':
  9473. case 'CID0KR':
  9474. case 'CID0JP': {
  9475. $fmetric['type'] = 'cidfont0';
  9476. break;
  9477. }
  9478. case 'Type1': {
  9479. $fmetric['type'] = 'Type1';
  9480. if (empty($enc) AND (($flags & 4) == 0)) {
  9481. $enc = 'cp1252';
  9482. }
  9483. break;
  9484. }
  9485. case 'TrueType': {
  9486. $fmetric['type'] = 'TrueType';
  9487. break;
  9488. }
  9489. case 'TrueTypeUnicode':
  9490. default: {
  9491. $fmetric['type'] = 'TrueTypeUnicode';
  9492. break;
  9493. }
  9494. }
  9495. // set encoding maps (if any)
  9496. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9497. $fmetric['diff'] = '';
  9498. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9499. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9500. // build differences from reference encoding
  9501. $enc_ref = $this->encmaps->encmap['cp1252'];
  9502. $enc_target = $this->encmaps->encmap[$enc];
  9503. $last = 0;
  9504. for ($i = 32; $i <= 255; ++$i) {
  9505. if ($enc_target != $enc_ref[$i]) {
  9506. if ($i != ($last + 1)) {
  9507. $fmetric['diff'] .= $i.' ';
  9508. }
  9509. $last = $i;
  9510. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9511. }
  9512. }
  9513. }
  9514. }
  9515. // parse the font by type
  9516. if ($fmetric['type'] == 'Type1') {
  9517. // ---------- TYPE 1 ----------
  9518. // read first segment
  9519. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9520. if ($a['marker'] != 128) {
  9521. $this->Error('Font file is not a valid binary Type1');
  9522. }
  9523. $fmetric['size1'] = $a['size'];
  9524. $data = substr($font, 6, $fmetric['size1']);
  9525. // read second segment
  9526. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9527. if ($a['marker'] != 128) {
  9528. $this->Error('Font file is not a valid binary Type1');
  9529. }
  9530. $fmetric['size2'] = $a['size'];
  9531. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9532. $data .= $encrypted;
  9533. // store compressed font
  9534. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9535. fwrite($fp, gzcompress($data));
  9536. fclose($fp);
  9537. // get font info
  9538. $fmetric['Flags'] = $flags;
  9539. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9540. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9541. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9542. $fmetric['bbox'] = trim($matches[1]);
  9543. $bv = explode(' ', $fmetric['bbox']);
  9544. $fmetric['Ascent'] = intval($bv[3]);
  9545. $fmetric['Descent'] = intval($bv[1]);
  9546. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9547. $fmetric['italicAngle'] = intval($matches[1]);
  9548. if ($fmetric['italicAngle'] != 0) {
  9549. $fmetric['Flags'] |= 64;
  9550. }
  9551. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9552. $fmetric['underlinePosition'] = intval($matches[1]);
  9553. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9554. $fmetric['underlineThickness'] = intval($matches[1]);
  9555. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9556. if ($matches[1] == 'true') {
  9557. $fmetric['Flags'] |= 1;
  9558. }
  9559. // get internal map
  9560. $imap = array();
  9561. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9562. foreach ($fmap as $v) {
  9563. $imap[$v[2]] = $v[1];
  9564. }
  9565. }
  9566. // decrypt eexec encrypted part
  9567. $r = 55665; // eexec encryption constant
  9568. $c1 = 52845;
  9569. $c2 = 22719;
  9570. $elen = strlen($encrypted);
  9571. $eplain = '';
  9572. for ($i = 0; $i < $elen; ++$i) {
  9573. $chr = ord($encrypted[$i]);
  9574. $eplain .= chr($chr ^ ($r >> 8));
  9575. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9576. }
  9577. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9578. if ($matches[1] == 'true') {
  9579. $fmetric['Flags'] |= 0x40000;
  9580. }
  9581. }
  9582. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9583. $fmetric['StemV'] = intval($matches[1]);
  9584. } else {
  9585. $fmetric['StemV'] = 70;
  9586. }
  9587. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9588. $fmetric['StemH'] = intval($matches[1]);
  9589. } else {
  9590. $fmetric['StemH'] = 30;
  9591. }
  9592. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9593. $bv = explode(' ', $matches[1]);
  9594. if (count($bv) >= 6) {
  9595. $v1 = intval($bv[2]);
  9596. $v2 = intval($bv[4]);
  9597. if ($v1 <= $v2) {
  9598. $fmetric['XHeight'] = $v1;
  9599. $fmetric['CapHeight'] = $v2;
  9600. } else {
  9601. $fmetric['XHeight'] = $v2;
  9602. $fmetric['CapHeight'] = $v1;
  9603. }
  9604. } else {
  9605. $fmetric['XHeight'] = 450;
  9606. $fmetric['CapHeight'] = 700;
  9607. }
  9608. } else {
  9609. $fmetric['XHeight'] = 450;
  9610. $fmetric['CapHeight'] = 700;
  9611. }
  9612. // get the number of random bytes at the beginning of charstrings
  9613. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9614. $lenIV = intval($matches[1]);
  9615. } else {
  9616. $lenIV = 4;
  9617. }
  9618. $fmetric['Leading'] = 0;
  9619. // get charstring data
  9620. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9621. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9622. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9623. $enc_map = $this->encmaps->encmap[$enc];
  9624. } else {
  9625. $enc_map = false;
  9626. }
  9627. $fmetric['cw'] = '';
  9628. $fmetric['MaxWidth'] = 0;
  9629. $cwidths = array();
  9630. foreach ($matches as $k => $v) {
  9631. $cid = 0;
  9632. if (isset($imap[$v[1]])) {
  9633. $cid = $imap[$v[1]];
  9634. } elseif ($enc_map !== false) {
  9635. $cid = array_search($v[1], $enc_map);
  9636. if ($cid === false) {
  9637. $cid = 0;
  9638. } elseif ($cid > 1000) {
  9639. $cid -= 1000;
  9640. }
  9641. }
  9642. // decrypt charstring encrypted part
  9643. $r = 4330; // charstring encryption constant
  9644. $c1 = 52845;
  9645. $c2 = 22719;
  9646. $cd = $v[2];
  9647. $clen = strlen($cd);
  9648. $ccom = array();
  9649. for ($i = 0; $i < $clen; ++$i) {
  9650. $chr = ord($cd[$i]);
  9651. $ccom[] = ($chr ^ ($r >> 8));
  9652. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9653. }
  9654. // decode numbers
  9655. $cdec = array();
  9656. $ck = 0;
  9657. $i = $lenIV;
  9658. while ($i < $clen) {
  9659. if ($ccom[$i] < 32) {
  9660. $cdec[$ck] = $ccom[$i];
  9661. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  9662. // hsbw command: update width
  9663. $cwidths[$cid] = $cdec[($ck - 1)];
  9664. }
  9665. ++$i;
  9666. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  9667. $cdec[$ck] = ($ccom[$i] - 139);
  9668. ++$i;
  9669. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  9670. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  9671. $i += 2;
  9672. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  9673. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  9674. $i += 2;
  9675. } elseif ($ccom[$i] == 255) {
  9676. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  9677. $vsval = unpack('li', $sval);
  9678. $cdec[$ck] = $vsval['i'];
  9679. $i += 5;
  9680. }
  9681. ++$ck;
  9682. }
  9683. } // end for each matches
  9684. $fmetric['MissingWidth'] = $cwidths[0];
  9685. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  9686. $fmetric['AvgWidth'] = 0;
  9687. // set chars widths
  9688. for ($cid = 0; $cid <= 255; ++$cid) {
  9689. if (isset($cwidths[$cid])) {
  9690. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  9691. $fmetric['MaxWidth'] = $cwidths[$cid];
  9692. }
  9693. $fmetric['AvgWidth'] += $cwidths[$cid];
  9694. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  9695. } else {
  9696. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  9697. }
  9698. }
  9699. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  9700. } else {
  9701. // ---------- TRUE TYPE ----------
  9702. if ($fmetric['type'] != 'cidfont0') {
  9703. // store compressed font
  9704. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9705. fwrite($fp, gzcompress($font));
  9706. fclose($fp);
  9707. }
  9708. $offset = 0; // offset position of the font data
  9709. if ($this->_getULONG($font, $offset) != 0x10000) {
  9710. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9711. return $font;
  9712. }
  9713. $offset += 4;
  9714. // get number of tables
  9715. $numTables = $this->_getUSHORT($font, $offset);
  9716. $offset += 2;
  9717. // skip searchRange, entrySelector and rangeShift
  9718. $offset += 6;
  9719. // tables array
  9720. $table = array();
  9721. // ---------- get tables ----------
  9722. for ($i = 0; $i < $numTables; ++$i) {
  9723. // get table info
  9724. $tag = substr($font, $offset, 4);
  9725. $offset += 4;
  9726. $table[$tag] = array();
  9727. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9728. $offset += 4;
  9729. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9730. $offset += 4;
  9731. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9732. $offset += 4;
  9733. }
  9734. // check magicNumber
  9735. $offset = $table['head']['offset'] + 12;
  9736. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9737. // magicNumber must be 0x5F0F3CF5
  9738. return $font;
  9739. }
  9740. $offset += 4;
  9741. $offset += 2; // skip flags
  9742. // get FUnits
  9743. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  9744. $offset += 2;
  9745. // units ratio constant
  9746. $urk = (1000 / $fmetric['unitsPerEm']);
  9747. $offset += 16; // skip created, modified
  9748. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  9749. $offset += 2;
  9750. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  9751. $offset += 2;
  9752. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  9753. $offset += 2;
  9754. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  9755. $offset += 2;
  9756. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  9757. $macStyle = $this->_getUSHORT($font, $offset);
  9758. $offset += 2;
  9759. // PDF font flags
  9760. $fmetric['Flags'] = $flags;
  9761. if (($macStyle & 2) == 2) {
  9762. // italic flag
  9763. $fmetric['Flags'] |= 64;
  9764. }
  9765. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9766. $offset = $table['head']['offset'] + 50;
  9767. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9768. $offset += 2;
  9769. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9770. $indexToLoc = array();
  9771. $offset = $table['loca']['offset'];
  9772. if ($short_offset) {
  9773. // short version
  9774. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9775. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9776. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9777. $offset += 2;
  9778. }
  9779. } else {
  9780. // long version
  9781. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9782. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9783. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9784. $offset += 4;
  9785. }
  9786. }
  9787. // get glyphs indexes of chars from cmap table
  9788. $offset = $table['cmap']['offset'] + 2;
  9789. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9790. $offset += 2;
  9791. $encodingTables = array();
  9792. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9793. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9794. $offset += 2;
  9795. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9796. $offset += 2;
  9797. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9798. $offset += 4;
  9799. }
  9800. // ---------- get os/2 metrics ----------
  9801. $offset = $table['OS/2']['offset'];
  9802. $offset += 2; // skip version
  9803. // xAvgCharWidth
  9804. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  9805. $offset += 2;
  9806. // usWeightClass
  9807. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  9808. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  9809. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  9810. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  9811. $offset += 2;
  9812. $offset += 2; // usWidthClass
  9813. $fsType = $this->_getSHORT($font, $offset);
  9814. $offset += 2;
  9815. if ($fsType == 2) {
  9816. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  9817. }
  9818. // ---------- get font name ----------
  9819. $fmetric['name'] = '';
  9820. $offset = $table['name']['offset'];
  9821. $offset += 2; // skip Format selector (=0).
  9822. // Number of NameRecords that follow n.
  9823. $numNameRecords = $this->_getUSHORT($font, $offset);
  9824. $offset += 2;
  9825. // Offset to start of string storage (from start of table).
  9826. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  9827. $offset += 2;
  9828. for ($i = 0; $i < $numNameRecords; ++$i) {
  9829. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  9830. // Name ID.
  9831. $nameID = $this->_getUSHORT($font, $offset);
  9832. $offset += 2;
  9833. if ($nameID == 6) {
  9834. // String length (in bytes).
  9835. $stringLength = $this->_getUSHORT($font, $offset);
  9836. $offset += 2;
  9837. // String offset from start of storage area (in bytes).
  9838. $stringOffset = $this->_getUSHORT($font, $offset);
  9839. $offset += 2;
  9840. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  9841. $fmetric['name'] = substr($font, $offset, $stringLength);
  9842. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  9843. break;
  9844. } else {
  9845. $offset += 4; // skip String length, String offset
  9846. }
  9847. }
  9848. if (empty($fmetric['name'])) {
  9849. $fmetric['name'] = $font_name;
  9850. }
  9851. // ---------- get post data ----------
  9852. $offset = $table['post']['offset'];
  9853. $offset += 4; // skip Format Type
  9854. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  9855. $offset += 4;
  9856. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  9857. $offset += 2;
  9858. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  9859. $offset += 2;
  9860. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  9861. $offset += 2;
  9862. if ($isFixedPitch) {
  9863. $fmetric['Flags'] |= 1;
  9864. }
  9865. // ---------- get hhea data ----------
  9866. $offset = $table['hhea']['offset'];
  9867. $offset += 4; // skip Table version number
  9868. // Ascender
  9869. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  9870. $offset += 2;
  9871. // Descender
  9872. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  9873. $offset += 2;
  9874. // LineGap
  9875. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  9876. $offset += 2;
  9877. // advanceWidthMax
  9878. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  9879. $offset += 2;
  9880. $offset += 22; // skip some values
  9881. // get the number of hMetric entries in hmtx table
  9882. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  9883. // ---------- get maxp data ----------
  9884. $offset = $table['maxp']['offset'];
  9885. $offset += 4; // skip Table version number
  9886. // get the the number of glyphs in the font.
  9887. $numGlyphs = $this->_getUSHORT($font, $offset);
  9888. // ---------- get CIDToGIDMap ----------
  9889. $ctg = array();
  9890. foreach ($encodingTables as $enctable) {
  9891. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  9892. $modesymbol = true;
  9893. } else {
  9894. $modesymbol = false;
  9895. }
  9896. $offset = $table['cmap']['offset'] + $enctable['offset'];
  9897. $format = $this->_getUSHORT($font, $offset);
  9898. $offset += 2;
  9899. switch ($format) {
  9900. case 0: { // Format 0: Byte encoding table
  9901. $offset += 4; // skip length and version/language
  9902. for ($c = 0; $c < 256; ++$c) {
  9903. $g = $this->_getBYTE($font, $offset);
  9904. $ctg[$c] = $g;
  9905. ++$offset;
  9906. }
  9907. break;
  9908. }
  9909. case 2: { // Format 2: High-byte mapping through table
  9910. $offset += 4; // skip length and version/language
  9911. $numSubHeaders = 0;
  9912. for ($i = 0; $i < 256; ++$i) {
  9913. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  9914. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  9915. $offset += 2;
  9916. if ($numSubHeaders < $subHeaderKeys[$i]) {
  9917. $numSubHeaders = $subHeaderKeys[$i];
  9918. }
  9919. }
  9920. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  9921. ++$numSubHeaders;
  9922. // read subHeader structures
  9923. $subHeaders = array();
  9924. $numGlyphIndexArray = 0;
  9925. for ($k = 0; $k < $numSubHeaders; ++$k) {
  9926. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  9927. $offset += 2;
  9928. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  9929. $offset += 2;
  9930. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  9931. $offset += 2;
  9932. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  9933. $offset += 2;
  9934. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  9935. $subHeaders[$k]['idRangeOffset'] /= 2;
  9936. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  9937. }
  9938. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  9939. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  9940. $offset += 2;
  9941. }
  9942. for ($i = 0; $i < 256; ++$i) {
  9943. $k = $subHeaderKeys[$i];
  9944. if ($k == 0) {
  9945. // one byte code
  9946. $c = $i;
  9947. $g = $glyphIndexArray[0];
  9948. $ctg[$c] = $g;
  9949. } else {
  9950. // two bytes code
  9951. $start_byte = $subHeaders[$k]['firstCode'];
  9952. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  9953. for ($j = $start_byte; $j < $end_byte; ++$j) {
  9954. // combine high and low bytes
  9955. $c = (($i << 8) + $j);
  9956. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  9957. $g = $glyphIndexArray[$idRangeOffset];
  9958. $g += ($idDelta[$k] - 65536);
  9959. if ($g < 0) {
  9960. $g = 0;
  9961. }
  9962. $ctg[$c] = $g;
  9963. }
  9964. }
  9965. }
  9966. break;
  9967. }
  9968. case 4: { // Format 4: Segment mapping to delta values
  9969. $length = $this->_getUSHORT($font, $offset);
  9970. $offset += 2;
  9971. $offset += 2; // skip version/language
  9972. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  9973. $offset += 2;
  9974. $offset += 6; // skip searchRange, entrySelector, rangeShift
  9975. $endCount = array(); // array of end character codes for each segment
  9976. for ($k = 0; $k < $segCount; ++$k) {
  9977. $endCount[$k] = $this->_getUSHORT($font, $offset);
  9978. $offset += 2;
  9979. }
  9980. $offset += 2; // skip reservedPad
  9981. $startCount = array(); // array of start character codes for each segment
  9982. for ($k = 0; $k < $segCount; ++$k) {
  9983. $startCount[$k] = $this->_getUSHORT($font, $offset);
  9984. $offset += 2;
  9985. }
  9986. $idDelta = array(); // delta for all character codes in segment
  9987. for ($k = 0; $k < $segCount; ++$k) {
  9988. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  9989. $offset += 2;
  9990. }
  9991. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  9992. for ($k = 0; $k < $segCount; ++$k) {
  9993. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  9994. $offset += 2;
  9995. }
  9996. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  9997. $glyphIdArray = array(); // glyph index array
  9998. for ($k = 0; $k < $gidlen; ++$k) {
  9999. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10000. $offset += 2;
  10001. }
  10002. for ($k = 0; $k < $segCount; ++$k) {
  10003. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10004. if ($idRangeOffset[$k] == 0) {
  10005. $g = $c;
  10006. } else {
  10007. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10008. $g = $glyphIdArray[$gid];
  10009. }
  10010. $g += ($idDelta[$k] - 65536);
  10011. if ($g < 0) {
  10012. $g = 0;
  10013. }
  10014. $ctg[$c] = $g;
  10015. }
  10016. }
  10017. break;
  10018. }
  10019. case 6: { // Format 6: Trimmed table mapping
  10020. $offset += 4; // skip length and version/language
  10021. $firstCode = $this->_getUSHORT($font, $offset);
  10022. $offset += 2;
  10023. $entryCount = $this->_getUSHORT($font, $offset);
  10024. $offset += 2;
  10025. for ($k = 0; $k < $entryCount; ++$k) {
  10026. $c = ($k + $firstCode);
  10027. $g = $this->_getUSHORT($font, $offset);
  10028. $ctg[$c] = $g;
  10029. $offset += 2;
  10030. }
  10031. break;
  10032. }
  10033. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10034. $offset += 10; // skip reserved, length and version/language
  10035. for ($k = 0; $k < 8192; ++$k) {
  10036. $is32[$k] = $this->_getBYTE($font, $offset);
  10037. ++$offset;
  10038. }
  10039. $nGroups = $this->_getULONG($font, $offset);
  10040. $offset += 4;
  10041. for ($i = 0; $i < $nGroups; ++$i) {
  10042. $startCharCode = $this->_getULONG($font, $offset);
  10043. $offset += 4;
  10044. $endCharCode = $this->_getULONG($font, $offset);
  10045. $offset += 4;
  10046. $startGlyphID = $this->_getULONG($font, $offset);
  10047. $offset += 4;
  10048. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10049. $is32idx = floor($c / 8);
  10050. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10051. $c = $k;
  10052. } else {
  10053. // 32 bit format
  10054. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10055. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10056. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10057. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10058. }
  10059. $ctg[$c] = $g;
  10060. ++$startGlyphID;
  10061. }
  10062. }
  10063. break;
  10064. }
  10065. case 10: { // Format 10: Trimmed array
  10066. $offset += 10; // skip reserved, length and version/language
  10067. $startCharCode = $this->_getULONG($font, $offset);
  10068. $offset += 4;
  10069. $numChars = $this->_getULONG($font, $offset);
  10070. $offset += 4;
  10071. for ($k = 0; $k < $numChars; ++$k) {
  10072. $c = ($k + $startCharCode);
  10073. $g = $this->_getUSHORT($font, $offset);
  10074. $ctg[$c] = $g;
  10075. $offset += 2;
  10076. }
  10077. break;
  10078. }
  10079. case 12: { // Format 12: Segmented coverage
  10080. $offset += 10; // skip length and version/language
  10081. $nGroups = $this->_getULONG($font, $offset);
  10082. $offset += 4;
  10083. for ($k = 0; $k < $nGroups; ++$k) {
  10084. $startCharCode = $this->_getULONG($font, $offset);
  10085. $offset += 4;
  10086. $endCharCode = $this->_getULONG($font, $offset);
  10087. $offset += 4;
  10088. $startGlyphCode = $this->_getULONG($font, $offset);
  10089. $offset += 4;
  10090. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10091. $ctg[$c] = $startGlyphCode;
  10092. ++$startGlyphCode;
  10093. }
  10094. }
  10095. break;
  10096. }
  10097. case 13: { // Format 13: Many-to-one range mappings
  10098. // to be implemented ...
  10099. break;
  10100. }
  10101. case 14: { // Format 14: Unicode Variation Sequences
  10102. // to be implemented ...
  10103. break;
  10104. }
  10105. }
  10106. }
  10107. if (!isset($ctg[0])) {
  10108. $ctg[0] = 0;
  10109. }
  10110. // get xHeight (height of x)
  10111. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10112. $yMin = $this->_getFWORD($font, $offset);
  10113. $offset += 4;
  10114. $yMax = $this->_getFWORD($font, $offset);
  10115. $offset += 2;
  10116. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10117. // get CapHeight (height of H)
  10118. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10119. $yMin = $this->_getFWORD($font, $offset);
  10120. $offset += 4;
  10121. $yMax = $this->_getFWORD($font, $offset);
  10122. $offset += 2;
  10123. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10124. // ceate widths array
  10125. $cw = array();
  10126. $offset = $table['hmtx']['offset'];
  10127. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10128. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10129. $offset += 4; // skip lsb
  10130. }
  10131. if ($numberOfHMetrics < $numGlyphs) {
  10132. // fill missing widths with the last value
  10133. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10134. }
  10135. $fmetric['MissingWidth'] = $cw[0];
  10136. $fmetric['cw'] = '';
  10137. for ($cid = 0; $cid <= 65535; ++$cid) {
  10138. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10139. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10140. }
  10141. }
  10142. } // end of true type
  10143. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10144. $fmetric['type'] == 'TrueType';
  10145. }
  10146. // ---------- create php font file ----------
  10147. $pfile = '<'.'?'.'php'."\n";
  10148. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10149. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10150. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10151. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10152. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10153. if ($fmetric['MissingWidth'] > 0) {
  10154. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10155. } else {
  10156. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10157. }
  10158. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10159. if ($fmetric['type'] == 'Type1') {
  10160. // Type 1
  10161. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10162. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10163. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10164. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10165. } else {
  10166. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10167. if ($fmetric['type'] == 'cidfont0') {
  10168. // CID-0
  10169. switch ($fonttype) {
  10170. case 'CID0JP': {
  10171. $pfile .= '// Japanese'."\n";
  10172. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10173. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10174. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10175. break;
  10176. }
  10177. case 'CID0KR': {
  10178. $pfile .= '// Korean'."\n";
  10179. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10180. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10181. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10182. break;
  10183. }
  10184. case 'CID0CS': {
  10185. $pfile .= '// Chinese Simplified'."\n";
  10186. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10187. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10188. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10189. break;
  10190. }
  10191. case 'CID0CT':
  10192. default: {
  10193. $pfile .= '// Chinese Traditional'."\n";
  10194. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10195. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10196. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10197. break;
  10198. }
  10199. }
  10200. } else {
  10201. // TrueType
  10202. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10203. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10204. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10205. // create CIDToGIDMap
  10206. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10207. foreach ($ctg as $cid => $gid) {
  10208. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10209. }
  10210. // store compressed CIDToGIDMap
  10211. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10212. fwrite($fp, gzcompress($cidtogidmap));
  10213. fclose($fp);
  10214. }
  10215. }
  10216. $pfile .= '$desc=array(';
  10217. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10218. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10219. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10220. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10221. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10222. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10223. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10224. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10225. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10226. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10227. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10228. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10229. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10230. $pfile .= ');'."\n";
  10231. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10232. $pfile .= '// --- EOF ---'."\n";
  10233. // store file
  10234. $fp = fopen($outpath.$font_name.'.php', 'w');
  10235. fwrite($fp, $pfile);
  10236. fclose($fp);
  10237. // return TCPDF font name
  10238. return $font_name;
  10239. }
  10240. /**
  10241. * Returns a subset of the TrueType font data without the unused glyphs.
  10242. * @param $font (string) TrueType font data.
  10243. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10244. * @return (string) A subset of TrueType font data without the unused glyphs.
  10245. * @author Nicola Asuni
  10246. * @protected
  10247. * @since 5.2.000 (2010-06-02)
  10248. */
  10249. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10250. ksort($subsetchars);
  10251. $offset = 0; // offset position of the font data
  10252. if ($this->_getULONG($font, $offset) != 0x10000) {
  10253. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10254. return $font;
  10255. }
  10256. $offset += 4;
  10257. // get number of tables
  10258. $numTables = $this->_getUSHORT($font, $offset);
  10259. $offset += 2;
  10260. // skip searchRange, entrySelector and rangeShift
  10261. $offset += 6;
  10262. // tables array
  10263. $table = array();
  10264. // for each table
  10265. for ($i = 0; $i < $numTables; ++$i) {
  10266. // get table info
  10267. $tag = substr($font, $offset, 4);
  10268. $offset += 4;
  10269. $table[$tag] = array();
  10270. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10271. $offset += 4;
  10272. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10273. $offset += 4;
  10274. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10275. $offset += 4;
  10276. }
  10277. // check magicNumber
  10278. $offset = $table['head']['offset'] + 12;
  10279. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10280. // magicNumber must be 0x5F0F3CF5
  10281. return $font;
  10282. }
  10283. $offset += 4;
  10284. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10285. $offset = $table['head']['offset'] + 50;
  10286. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10287. $offset += 2;
  10288. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10289. $indexToLoc = array();
  10290. $offset = $table['loca']['offset'];
  10291. if ($short_offset) {
  10292. // short version
  10293. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10294. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10295. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10296. $offset += 2;
  10297. }
  10298. } else {
  10299. // long version
  10300. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10301. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10302. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10303. $offset += 4;
  10304. }
  10305. }
  10306. // get glyphs indexes of chars from cmap table
  10307. $subsetglyphs = array(); // glyph IDs on key
  10308. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10309. $offset = $table['cmap']['offset'] + 2;
  10310. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10311. $offset += 2;
  10312. $encodingTables = array();
  10313. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10314. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10315. $offset += 2;
  10316. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10317. $offset += 2;
  10318. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10319. $offset += 4;
  10320. }
  10321. foreach ($encodingTables as $enctable) {
  10322. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  10323. $modesymbol = true;
  10324. } else {
  10325. $modesymbol = false;
  10326. }
  10327. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10328. $format = $this->_getUSHORT($font, $offset);
  10329. $offset += 2;
  10330. switch ($format) {
  10331. case 0: { // Format 0: Byte encoding table
  10332. $offset += 4; // skip length and version/language
  10333. for ($c = 0; $c < 256; ++$c) {
  10334. if (isset($subsetchars[$c])) {
  10335. $g = $this->_getBYTE($font, $offset);
  10336. $subsetglyphs[$g] = true;
  10337. }
  10338. ++$offset;
  10339. }
  10340. break;
  10341. }
  10342. case 2: { // Format 2: High-byte mapping through table
  10343. $offset += 4; // skip length and version/language
  10344. $numSubHeaders = 0;
  10345. for ($i = 0; $i < 256; ++$i) {
  10346. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10347. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10348. $offset += 2;
  10349. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10350. $numSubHeaders = $subHeaderKeys[$i];
  10351. }
  10352. }
  10353. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10354. ++$numSubHeaders;
  10355. // read subHeader structures
  10356. $subHeaders = array();
  10357. $numGlyphIndexArray = 0;
  10358. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10359. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10360. $offset += 2;
  10361. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10362. $offset += 2;
  10363. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  10364. $offset += 2;
  10365. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10366. $offset += 2;
  10367. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10368. $subHeaders[$k]['idRangeOffset'] /= 2;
  10369. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10370. }
  10371. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10372. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10373. $offset += 2;
  10374. }
  10375. for ($i = 0; $i < 256; ++$i) {
  10376. $k = $subHeaderKeys[$i];
  10377. if ($k == 0) {
  10378. // one byte code
  10379. $c = $i;
  10380. if (isset($subsetchars[$c])) {
  10381. $g = $glyphIndexArray[0];
  10382. $subsetglyphs[$g] = true;
  10383. }
  10384. } else {
  10385. // two bytes code
  10386. $start_byte = $subHeaders[$k]['firstCode'];
  10387. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10388. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10389. // combine high and low bytes
  10390. $c = (($i << 8) + $j);
  10391. if (isset($subsetchars[$c])) {
  10392. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10393. $g = $glyphIndexArray[$idRangeOffset];
  10394. $g += ($idDelta[$k] - 65536);
  10395. if ($g < 0) {
  10396. $g = 0;
  10397. }
  10398. $subsetglyphs[$g] = true;
  10399. }
  10400. }
  10401. }
  10402. }
  10403. break;
  10404. }
  10405. case 4: { // Format 4: Segment mapping to delta values
  10406. $length = $this->_getUSHORT($font, $offset);
  10407. $offset += 2;
  10408. $offset += 2; // skip version/language
  10409. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10410. $offset += 2;
  10411. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10412. $endCount = array(); // array of end character codes for each segment
  10413. for ($k = 0; $k < $segCount; ++$k) {
  10414. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10415. $offset += 2;
  10416. }
  10417. $offset += 2; // skip reservedPad
  10418. $startCount = array(); // array of start character codes for each segment
  10419. for ($k = 0; $k < $segCount; ++$k) {
  10420. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10421. $offset += 2;
  10422. }
  10423. $idDelta = array(); // delta for all character codes in segment
  10424. for ($k = 0; $k < $segCount; ++$k) {
  10425. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10426. $offset += 2;
  10427. }
  10428. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10429. for ($k = 0; $k < $segCount; ++$k) {
  10430. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10431. $offset += 2;
  10432. }
  10433. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10434. $glyphIdArray = array(); // glyph index array
  10435. for ($k = 0; $k < $gidlen; ++$k) {
  10436. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10437. $offset += 2;
  10438. }
  10439. for ($k = 0; $k < $segCount; ++$k) {
  10440. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10441. if (isset($subsetchars[$c])) {
  10442. if ($idRangeOffset[$k] == 0) {
  10443. $g = $c;
  10444. } else {
  10445. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10446. $g = $glyphIdArray[$gid];
  10447. }
  10448. $g += ($idDelta[$k] - 65536);
  10449. if ($g < 0) {
  10450. $g = 0;
  10451. }
  10452. $subsetglyphs[$g] = true;
  10453. }
  10454. }
  10455. }
  10456. break;
  10457. }
  10458. case 6: { // Format 6: Trimmed table mapping
  10459. $offset += 4; // skip length and version/language
  10460. $firstCode = $this->_getUSHORT($font, $offset);
  10461. $offset += 2;
  10462. $entryCount = $this->_getUSHORT($font, $offset);
  10463. $offset += 2;
  10464. for ($k = 0; $k < $entryCount; ++$k) {
  10465. $c = ($k + $firstCode);
  10466. if (isset($subsetchars[$c])) {
  10467. $g = $this->_getUSHORT($font, $offset);
  10468. $subsetglyphs[$g] = true;
  10469. }
  10470. $offset += 2;
  10471. }
  10472. break;
  10473. }
  10474. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10475. $offset += 10; // skip reserved, length and version/language
  10476. for ($k = 0; $k < 8192; ++$k) {
  10477. $is32[$k] = $this->_getBYTE($font, $offset);
  10478. ++$offset;
  10479. }
  10480. $nGroups = $this->_getULONG($font, $offset);
  10481. $offset += 4;
  10482. for ($i = 0; $i < $nGroups; ++$i) {
  10483. $startCharCode = $this->_getULONG($font, $offset);
  10484. $offset += 4;
  10485. $endCharCode = $this->_getULONG($font, $offset);
  10486. $offset += 4;
  10487. $startGlyphID = $this->_getULONG($font, $offset);
  10488. $offset += 4;
  10489. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10490. $is32idx = floor($c / 8);
  10491. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10492. $c = $k;
  10493. } else {
  10494. // 32 bit format
  10495. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10496. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10497. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10498. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10499. }
  10500. if (isset($subsetchars[$c])) {
  10501. $subsetglyphs[$startGlyphID] = true;
  10502. }
  10503. ++$startGlyphID;
  10504. }
  10505. }
  10506. break;
  10507. }
  10508. case 10: { // Format 10: Trimmed array
  10509. $offset += 10; // skip reserved, length and version/language
  10510. $startCharCode = $this->_getULONG($font, $offset);
  10511. $offset += 4;
  10512. $numChars = $this->_getULONG($font, $offset);
  10513. $offset += 4;
  10514. for ($k = 0; $k < $numChars; ++$k) {
  10515. $c = ($k + $startCharCode);
  10516. if (isset($subsetchars[$c])) {
  10517. $g = $this->_getUSHORT($font, $offset);
  10518. $subsetglyphs[$g] = true;
  10519. }
  10520. $offset += 2;
  10521. }
  10522. break;
  10523. }
  10524. case 12: { // Format 12: Segmented coverage
  10525. $offset += 10; // skip length and version/language
  10526. $nGroups = $this->_getULONG($font, $offset);
  10527. $offset += 4;
  10528. for ($k = 0; $k < $nGroups; ++$k) {
  10529. $startCharCode = $this->_getULONG($font, $offset);
  10530. $offset += 4;
  10531. $endCharCode = $this->_getULONG($font, $offset);
  10532. $offset += 4;
  10533. $startGlyphCode = $this->_getULONG($font, $offset);
  10534. $offset += 4;
  10535. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10536. if (isset($subsetchars[$c])) {
  10537. $subsetglyphs[$startGlyphCode] = true;
  10538. }
  10539. ++$startGlyphCode;
  10540. }
  10541. }
  10542. break;
  10543. }
  10544. case 13: { // Format 13: Many-to-one range mappings
  10545. // to be implemented ...
  10546. break;
  10547. }
  10548. case 14: { // Format 14: Unicode Variation Sequences
  10549. // to be implemented ...
  10550. break;
  10551. }
  10552. }
  10553. }
  10554. // include all parts of composite glyphs
  10555. $new_sga = $subsetglyphs;
  10556. while (!empty($new_sga)) {
  10557. $sga = $new_sga;
  10558. $new_sga = array();
  10559. foreach ($sga as $key => $val) {
  10560. if (isset($indexToLoc[$key])) {
  10561. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10562. $numberOfContours = $this->_getSHORT($font, $offset);
  10563. $offset += 2;
  10564. if ($numberOfContours < 0) { // composite glyph
  10565. $offset += 8; // skip xMin, yMin, xMax, yMax
  10566. do {
  10567. $flags = $this->_getUSHORT($font, $offset);
  10568. $offset += 2;
  10569. $glyphIndex = $this->_getUSHORT($font, $offset);
  10570. $offset += 2;
  10571. if (!isset($subsetglyphs[$glyphIndex])) {
  10572. // add missing glyphs
  10573. $new_sga[$glyphIndex] = true;
  10574. }
  10575. // skip some bytes by case
  10576. if ($flags & 1) {
  10577. $offset += 4;
  10578. } else {
  10579. $offset += 2;
  10580. }
  10581. if ($flags & 8) {
  10582. $offset += 2;
  10583. } elseif ($flags & 64) {
  10584. $offset += 4;
  10585. } elseif ($flags & 128) {
  10586. $offset += 8;
  10587. }
  10588. } while ($flags & 32);
  10589. }
  10590. }
  10591. }
  10592. $subsetglyphs += $new_sga;
  10593. }
  10594. // sort glyphs by key (and remove duplicates)
  10595. ksort($subsetglyphs);
  10596. // build new glyf and loca tables
  10597. $glyf = '';
  10598. $loca = '';
  10599. $offset = 0;
  10600. $glyf_offset = $table['glyf']['offset'];
  10601. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10602. if (isset($subsetglyphs[$i])) {
  10603. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10604. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10605. } else {
  10606. $length = 0;
  10607. }
  10608. if ($short_offset) {
  10609. $loca .= pack('n', ($offset / 2));
  10610. } else {
  10611. $loca .= pack('N', $offset);
  10612. }
  10613. $offset += $length;
  10614. }
  10615. // array of table names to preserve (loca and glyf tables will be added later)
  10616. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10617. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10618. // get the tables to preserve
  10619. $offset = 12;
  10620. foreach ($table as $tag => $val) {
  10621. if (in_array($tag, $table_names)) {
  10622. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10623. if ($tag == 'head') {
  10624. // set the checkSumAdjustment to 0
  10625. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10626. }
  10627. $pad = 4 - ($table[$tag]['length'] % 4);
  10628. if ($pad != 4) {
  10629. // the length of a table must be a multiple of four bytes
  10630. $table[$tag]['length'] += $pad;
  10631. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10632. }
  10633. $table[$tag]['offset'] = $offset;
  10634. $offset += $table[$tag]['length'];
  10635. // check sum is not changed (so keep the following line commented)
  10636. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10637. } else {
  10638. unset($table[$tag]);
  10639. }
  10640. }
  10641. // add loca
  10642. $table['loca']['data'] = $loca;
  10643. $table['loca']['length'] = strlen($loca);
  10644. $pad = 4 - ($table['loca']['length'] % 4);
  10645. if ($pad != 4) {
  10646. // the length of a table must be a multiple of four bytes
  10647. $table['loca']['length'] += $pad;
  10648. $table['loca']['data'] .= str_repeat("\x0", $pad);
  10649. }
  10650. $table['loca']['offset'] = $offset;
  10651. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  10652. $offset += $table['loca']['length'];
  10653. // add glyf
  10654. $table['glyf']['data'] = $glyf;
  10655. $table['glyf']['length'] = strlen($glyf);
  10656. $pad = 4 - ($table['glyf']['length'] % 4);
  10657. if ($pad != 4) {
  10658. // the length of a table must be a multiple of four bytes
  10659. $table['glyf']['length'] += $pad;
  10660. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  10661. }
  10662. $table['glyf']['offset'] = $offset;
  10663. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  10664. // rebuild font
  10665. $font = '';
  10666. $font .= pack('N', 0x10000); // sfnt version
  10667. $numTables = count($table);
  10668. $font .= pack('n', $numTables); // numTables
  10669. $entrySelector = floor(log($numTables, 2));
  10670. $searchRange = pow(2, $entrySelector) * 16;
  10671. $rangeShift = ($numTables * 16) - $searchRange;
  10672. $font .= pack('n', $searchRange); // searchRange
  10673. $font .= pack('n', $entrySelector); // entrySelector
  10674. $font .= pack('n', $rangeShift); // rangeShift
  10675. $offset = ($numTables * 16);
  10676. foreach ($table as $tag => $data) {
  10677. $font .= $tag; // tag
  10678. $font .= pack('N', $data['checkSum']); // checkSum
  10679. $font .= pack('N', ($data['offset'] + $offset)); // offset
  10680. $font .= pack('N', $data['length']); // length
  10681. }
  10682. foreach ($table as $data) {
  10683. $font .= $data['data'];
  10684. }
  10685. // set checkSumAdjustment on head table
  10686. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  10687. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  10688. return $font;
  10689. }
  10690. /**
  10691. * Returs the checksum of a TTF table.
  10692. * @param $table (string) table to check
  10693. * @param $length (int) length of table in bytes
  10694. * @return int checksum
  10695. * @author Nicola Asuni
  10696. * @protected
  10697. * @since 5.2.000 (2010-06-02)
  10698. */
  10699. protected function _getTTFtableChecksum($table, $length) {
  10700. $sum = 0;
  10701. $tlen = ($length / 4);
  10702. $offset = 0;
  10703. for ($i = 0; $i < $tlen; ++$i) {
  10704. $v = unpack('Ni', substr($table, $offset, 4));
  10705. $sum += $v['i'];
  10706. $offset += 4;
  10707. }
  10708. $sum = unpack('Ni', pack('N', $sum));
  10709. return $sum['i'];
  10710. }
  10711. /**
  10712. * Outputs font widths
  10713. * @param $font (array) font data
  10714. * @param $cidoffset (int) offset for CID values
  10715. * @return PDF command string for font widths
  10716. * @author Nicola Asuni
  10717. * @protected
  10718. * @since 4.4.000 (2008-12-07)
  10719. */
  10720. protected function _putfontwidths($font, $cidoffset=0) {
  10721. ksort($font['cw']);
  10722. $rangeid = 0;
  10723. $range = array();
  10724. $prevcid = -2;
  10725. $prevwidth = -1;
  10726. $interval = false;
  10727. // for each character
  10728. foreach ($font['cw'] as $cid => $width) {
  10729. $cid -= $cidoffset;
  10730. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  10731. // ignore the unused characters (font subsetting)
  10732. continue;
  10733. }
  10734. if ($width != $font['dw']) {
  10735. if ($cid == ($prevcid + 1)) {
  10736. // consecutive CID
  10737. if ($width == $prevwidth) {
  10738. if ($width == $range[$rangeid][0]) {
  10739. $range[$rangeid][] = $width;
  10740. } else {
  10741. array_pop($range[$rangeid]);
  10742. // new range
  10743. $rangeid = $prevcid;
  10744. $range[$rangeid] = array();
  10745. $range[$rangeid][] = $prevwidth;
  10746. $range[$rangeid][] = $width;
  10747. }
  10748. $interval = true;
  10749. $range[$rangeid]['interval'] = true;
  10750. } else {
  10751. if ($interval) {
  10752. // new range
  10753. $rangeid = $cid;
  10754. $range[$rangeid] = array();
  10755. $range[$rangeid][] = $width;
  10756. } else {
  10757. $range[$rangeid][] = $width;
  10758. }
  10759. $interval = false;
  10760. }
  10761. } else {
  10762. // new range
  10763. $rangeid = $cid;
  10764. $range[$rangeid] = array();
  10765. $range[$rangeid][] = $width;
  10766. $interval = false;
  10767. }
  10768. $prevcid = $cid;
  10769. $prevwidth = $width;
  10770. }
  10771. }
  10772. // optimize ranges
  10773. $prevk = -1;
  10774. $nextk = -1;
  10775. $prevint = false;
  10776. foreach ($range as $k => $ws) {
  10777. $cws = count($ws);
  10778. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  10779. if (isset($range[$k]['interval'])) {
  10780. unset($range[$k]['interval']);
  10781. }
  10782. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  10783. unset($range[$k]);
  10784. } else {
  10785. $prevk = $k;
  10786. }
  10787. $nextk = $k + $cws;
  10788. if (isset($ws['interval'])) {
  10789. if ($cws > 3) {
  10790. $prevint = true;
  10791. } else {
  10792. $prevint = false;
  10793. }
  10794. unset($range[$k]['interval']);
  10795. --$nextk;
  10796. } else {
  10797. $prevint = false;
  10798. }
  10799. }
  10800. // output data
  10801. $w = '';
  10802. foreach ($range as $k => $ws) {
  10803. if (count(array_count_values($ws)) == 1) {
  10804. // interval mode is more compact
  10805. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  10806. } else {
  10807. // range mode
  10808. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  10809. }
  10810. }
  10811. return '/W ['.$w.' ]';
  10812. }
  10813. /**
  10814. * Output fonts.
  10815. * @author Nicola Asuni
  10816. * @protected
  10817. */
  10818. protected function _putfonts() {
  10819. $nf = $this->n;
  10820. foreach ($this->diffs as $diff) {
  10821. //Encodings
  10822. $this->_newobj();
  10823. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  10824. }
  10825. $mqr = $this->get_mqr();
  10826. $this->set_mqr(false);
  10827. foreach ($this->FontFiles as $file => $info) {
  10828. // search and get font file to embedd
  10829. $fontdir = $info['fontdir'];
  10830. $file = strtolower($file);
  10831. $fontfile = '';
  10832. // search files on various directories
  10833. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  10834. $fontfile = $fontdir.$file;
  10835. } elseif (file_exists($this->_getfontpath().$file)) {
  10836. $fontfile = $this->_getfontpath().$file;
  10837. } elseif (file_exists($file)) {
  10838. $fontfile = $file;
  10839. }
  10840. if (!$this->empty_string($fontfile)) {
  10841. $font = file_get_contents($fontfile);
  10842. $compressed = (substr($file, -2) == '.z');
  10843. if ((!$compressed) AND (isset($info['length2']))) {
  10844. $header = (ord($font{0}) == 128);
  10845. if ($header) {
  10846. // strip first binary header
  10847. $font = substr($font, 6);
  10848. }
  10849. if ($header AND (ord($font[$info['length1']]) == 128)) {
  10850. // strip second binary header
  10851. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  10852. }
  10853. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  10854. if ($compressed) {
  10855. // uncompress font
  10856. $font = gzuncompress($font);
  10857. }
  10858. // merge subset characters
  10859. $subsetchars = array(); // used chars
  10860. foreach ($info['fontkeys'] as $fontkey) {
  10861. $fontinfo = $this->getFontBuffer($fontkey);
  10862. $subsetchars += $fontinfo['subsetchars'];
  10863. }
  10864. // rebuild a font subset
  10865. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  10866. // calculate new font length
  10867. $info['length1'] = strlen($font);
  10868. if ($compressed) {
  10869. // recompress font
  10870. $font = gzcompress($font);
  10871. }
  10872. }
  10873. $this->_newobj();
  10874. $this->FontFiles[$file]['n'] = $this->n;
  10875. $stream = $this->_getrawstream($font);
  10876. $out = '<< /Length '.strlen($stream);
  10877. if ($compressed) {
  10878. $out .= ' /Filter /FlateDecode';
  10879. }
  10880. $out .= ' /Length1 '.$info['length1'];
  10881. if (isset($info['length2'])) {
  10882. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  10883. }
  10884. $out .= ' >>';
  10885. $out .= ' stream'."\n".$stream."\n".'endstream';
  10886. $out .= "\n".'endobj';
  10887. $this->_out($out);
  10888. }
  10889. }
  10890. $this->set_mqr($mqr);
  10891. foreach ($this->fontkeys as $k) {
  10892. //Font objects
  10893. $font = $this->getFontBuffer($k);
  10894. $type = $font['type'];
  10895. $name = $font['name'];
  10896. if ($type == 'core') {
  10897. // standard core font
  10898. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10899. $out .= '<</Type /Font';
  10900. $out .= ' /Subtype /Type1';
  10901. $out .= ' /BaseFont /'.$name;
  10902. $out .= ' /Name /F'.$font['i'];
  10903. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  10904. $out .= ' /Encoding /WinAnsiEncoding';
  10905. }
  10906. if ($k == 'helvetica') {
  10907. // add default font for annotations
  10908. $this->annotation_fonts[$k] = $font['i'];
  10909. }
  10910. $out .= ' >>';
  10911. $out .= "\n".'endobj';
  10912. $this->_out($out);
  10913. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  10914. // additional Type1 or TrueType font
  10915. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10916. $out .= '<</Type /Font';
  10917. $out .= ' /Subtype /'.$type;
  10918. $out .= ' /BaseFont /'.$name;
  10919. $out .= ' /Name /F'.$font['i'];
  10920. $out .= ' /FirstChar 32 /LastChar 255';
  10921. $out .= ' /Widths '.($this->n + 1).' 0 R';
  10922. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  10923. if ($font['enc']) {
  10924. if (isset($font['diff'])) {
  10925. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  10926. } else {
  10927. $out .= ' /Encoding /WinAnsiEncoding';
  10928. }
  10929. }
  10930. $out .= ' >>';
  10931. $out .= "\n".'endobj';
  10932. $this->_out($out);
  10933. // Widths
  10934. $this->_newobj();
  10935. $s = '[';
  10936. for ($i = 32; $i < 256; ++$i) {
  10937. $s .= $font['cw'][$i].' ';
  10938. }
  10939. $s .= ']';
  10940. $s .= "\n".'endobj';
  10941. $this->_out($s);
  10942. //Descriptor
  10943. $this->_newobj();
  10944. $s = '<</Type /FontDescriptor /FontName /'.$name;
  10945. foreach ($font['desc'] as $fdk => $fdv) {
  10946. if (is_float($fdv)) {
  10947. $fdv = sprintf('%.3F', $fdv);
  10948. }
  10949. $s .= ' /'.$fdk.' '.$fdv.'';
  10950. }
  10951. if (!$this->empty_string($font['file'])) {
  10952. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  10953. }
  10954. $s .= '>>';
  10955. $s .= "\n".'endobj';
  10956. $this->_out($s);
  10957. } else {
  10958. // additional types
  10959. $mtd = '_put'.strtolower($type);
  10960. if (!method_exists($this, $mtd)) {
  10961. $this->Error('Unsupported font type: '.$type);
  10962. }
  10963. $this->$mtd($font);
  10964. }
  10965. }
  10966. }
  10967. /**
  10968. * Adds unicode fonts.<br>
  10969. * Based on PDF Reference 1.3 (section 5)
  10970. * @param $font (array) font data
  10971. * @protected
  10972. * @author Nicola Asuni
  10973. * @since 1.52.0.TC005 (2005-01-05)
  10974. */
  10975. protected function _puttruetypeunicode($font) {
  10976. $fontname = '';
  10977. if ($font['subset']) {
  10978. // change name for font subsetting
  10979. $subtag = sprintf('%06u', $font['i']);
  10980. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  10981. $fontname .= $subtag.'+';
  10982. }
  10983. $fontname .= $font['name'];
  10984. // Type0 Font
  10985. // A composite font composed of other fonts, organized hierarchically
  10986. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  10987. $out .= '<< /Type /Font';
  10988. $out .= ' /Subtype /Type0';
  10989. $out .= ' /BaseFont /'.$fontname;
  10990. $out .= ' /Name /F'.$font['i'];
  10991. $out .= ' /Encoding /'.$font['enc'];
  10992. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  10993. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  10994. $out .= ' >>';
  10995. $out .= "\n".'endobj';
  10996. $this->_out($out);
  10997. // ToUnicode map for Identity-H
  10998. $stream = "/CIDInit /ProcSet findresource begin\n";
  10999. $stream .= "12 dict begin\n";
  11000. $stream .= "begincmap\n";
  11001. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  11002. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  11003. $stream .= "/CMapType 2 def\n";
  11004. $stream .= "/WMode 0 def\n";
  11005. $stream .= "1 begincodespacerange\n";
  11006. $stream .= "<0000> <FFFF>\n";
  11007. $stream .= "endcodespacerange\n";
  11008. $stream .= "100 beginbfrange\n";
  11009. $stream .= "<0000> <00ff> <0000>\n";
  11010. $stream .= "<0100> <01ff> <0100>\n";
  11011. $stream .= "<0200> <02ff> <0200>\n";
  11012. $stream .= "<0300> <03ff> <0300>\n";
  11013. $stream .= "<0400> <04ff> <0400>\n";
  11014. $stream .= "<0500> <05ff> <0500>\n";
  11015. $stream .= "<0600> <06ff> <0600>\n";
  11016. $stream .= "<0700> <07ff> <0700>\n";
  11017. $stream .= "<0800> <08ff> <0800>\n";
  11018. $stream .= "<0900> <09ff> <0900>\n";
  11019. $stream .= "<0a00> <0aff> <0a00>\n";
  11020. $stream .= "<0b00> <0bff> <0b00>\n";
  11021. $stream .= "<0c00> <0cff> <0c00>\n";
  11022. $stream .= "<0d00> <0dff> <0d00>\n";
  11023. $stream .= "<0e00> <0eff> <0e00>\n";
  11024. $stream .= "<0f00> <0fff> <0f00>\n";
  11025. $stream .= "<1000> <10ff> <1000>\n";
  11026. $stream .= "<1100> <11ff> <1100>\n";
  11027. $stream .= "<1200> <12ff> <1200>\n";
  11028. $stream .= "<1300> <13ff> <1300>\n";
  11029. $stream .= "<1400> <14ff> <1400>\n";
  11030. $stream .= "<1500> <15ff> <1500>\n";
  11031. $stream .= "<1600> <16ff> <1600>\n";
  11032. $stream .= "<1700> <17ff> <1700>\n";
  11033. $stream .= "<1800> <18ff> <1800>\n";
  11034. $stream .= "<1900> <19ff> <1900>\n";
  11035. $stream .= "<1a00> <1aff> <1a00>\n";
  11036. $stream .= "<1b00> <1bff> <1b00>\n";
  11037. $stream .= "<1c00> <1cff> <1c00>\n";
  11038. $stream .= "<1d00> <1dff> <1d00>\n";
  11039. $stream .= "<1e00> <1eff> <1e00>\n";
  11040. $stream .= "<1f00> <1fff> <1f00>\n";
  11041. $stream .= "<2000> <20ff> <2000>\n";
  11042. $stream .= "<2100> <21ff> <2100>\n";
  11043. $stream .= "<2200> <22ff> <2200>\n";
  11044. $stream .= "<2300> <23ff> <2300>\n";
  11045. $stream .= "<2400> <24ff> <2400>\n";
  11046. $stream .= "<2500> <25ff> <2500>\n";
  11047. $stream .= "<2600> <26ff> <2600>\n";
  11048. $stream .= "<2700> <27ff> <2700>\n";
  11049. $stream .= "<2800> <28ff> <2800>\n";
  11050. $stream .= "<2900> <29ff> <2900>\n";
  11051. $stream .= "<2a00> <2aff> <2a00>\n";
  11052. $stream .= "<2b00> <2bff> <2b00>\n";
  11053. $stream .= "<2c00> <2cff> <2c00>\n";
  11054. $stream .= "<2d00> <2dff> <2d00>\n";
  11055. $stream .= "<2e00> <2eff> <2e00>\n";
  11056. $stream .= "<2f00> <2fff> <2f00>\n";
  11057. $stream .= "<3000> <30ff> <3000>\n";
  11058. $stream .= "<3100> <31ff> <3100>\n";
  11059. $stream .= "<3200> <32ff> <3200>\n";
  11060. $stream .= "<3300> <33ff> <3300>\n";
  11061. $stream .= "<3400> <34ff> <3400>\n";
  11062. $stream .= "<3500> <35ff> <3500>\n";
  11063. $stream .= "<3600> <36ff> <3600>\n";
  11064. $stream .= "<3700> <37ff> <3700>\n";
  11065. $stream .= "<3800> <38ff> <3800>\n";
  11066. $stream .= "<3900> <39ff> <3900>\n";
  11067. $stream .= "<3a00> <3aff> <3a00>\n";
  11068. $stream .= "<3b00> <3bff> <3b00>\n";
  11069. $stream .= "<3c00> <3cff> <3c00>\n";
  11070. $stream .= "<3d00> <3dff> <3d00>\n";
  11071. $stream .= "<3e00> <3eff> <3e00>\n";
  11072. $stream .= "<3f00> <3fff> <3f00>\n";
  11073. $stream .= "<4000> <40ff> <4000>\n";
  11074. $stream .= "<4100> <41ff> <4100>\n";
  11075. $stream .= "<4200> <42ff> <4200>\n";
  11076. $stream .= "<4300> <43ff> <4300>\n";
  11077. $stream .= "<4400> <44ff> <4400>\n";
  11078. $stream .= "<4500> <45ff> <4500>\n";
  11079. $stream .= "<4600> <46ff> <4600>\n";
  11080. $stream .= "<4700> <47ff> <4700>\n";
  11081. $stream .= "<4800> <48ff> <4800>\n";
  11082. $stream .= "<4900> <49ff> <4900>\n";
  11083. $stream .= "<4a00> <4aff> <4a00>\n";
  11084. $stream .= "<4b00> <4bff> <4b00>\n";
  11085. $stream .= "<4c00> <4cff> <4c00>\n";
  11086. $stream .= "<4d00> <4dff> <4d00>\n";
  11087. $stream .= "<4e00> <4eff> <4e00>\n";
  11088. $stream .= "<4f00> <4fff> <4f00>\n";
  11089. $stream .= "<5000> <50ff> <5000>\n";
  11090. $stream .= "<5100> <51ff> <5100>\n";
  11091. $stream .= "<5200> <52ff> <5200>\n";
  11092. $stream .= "<5300> <53ff> <5300>\n";
  11093. $stream .= "<5400> <54ff> <5400>\n";
  11094. $stream .= "<5500> <55ff> <5500>\n";
  11095. $stream .= "<5600> <56ff> <5600>\n";
  11096. $stream .= "<5700> <57ff> <5700>\n";
  11097. $stream .= "<5800> <58ff> <5800>\n";
  11098. $stream .= "<5900> <59ff> <5900>\n";
  11099. $stream .= "<5a00> <5aff> <5a00>\n";
  11100. $stream .= "<5b00> <5bff> <5b00>\n";
  11101. $stream .= "<5c00> <5cff> <5c00>\n";
  11102. $stream .= "<5d00> <5dff> <5d00>\n";
  11103. $stream .= "<5e00> <5eff> <5e00>\n";
  11104. $stream .= "<5f00> <5fff> <5f00>\n";
  11105. $stream .= "<6000> <60ff> <6000>\n";
  11106. $stream .= "<6100> <61ff> <6100>\n";
  11107. $stream .= "<6200> <62ff> <6200>\n";
  11108. $stream .= "<6300> <63ff> <6300>\n";
  11109. $stream .= "endbfrange\n";
  11110. $stream .= "100 beginbfrange\n";
  11111. $stream .= "<6400> <64ff> <6400>\n";
  11112. $stream .= "<6500> <65ff> <6500>\n";
  11113. $stream .= "<6600> <66ff> <6600>\n";
  11114. $stream .= "<6700> <67ff> <6700>\n";
  11115. $stream .= "<6800> <68ff> <6800>\n";
  11116. $stream .= "<6900> <69ff> <6900>\n";
  11117. $stream .= "<6a00> <6aff> <6a00>\n";
  11118. $stream .= "<6b00> <6bff> <6b00>\n";
  11119. $stream .= "<6c00> <6cff> <6c00>\n";
  11120. $stream .= "<6d00> <6dff> <6d00>\n";
  11121. $stream .= "<6e00> <6eff> <6e00>\n";
  11122. $stream .= "<6f00> <6fff> <6f00>\n";
  11123. $stream .= "<7000> <70ff> <7000>\n";
  11124. $stream .= "<7100> <71ff> <7100>\n";
  11125. $stream .= "<7200> <72ff> <7200>\n";
  11126. $stream .= "<7300> <73ff> <7300>\n";
  11127. $stream .= "<7400> <74ff> <7400>\n";
  11128. $stream .= "<7500> <75ff> <7500>\n";
  11129. $stream .= "<7600> <76ff> <7600>\n";
  11130. $stream .= "<7700> <77ff> <7700>\n";
  11131. $stream .= "<7800> <78ff> <7800>\n";
  11132. $stream .= "<7900> <79ff> <7900>\n";
  11133. $stream .= "<7a00> <7aff> <7a00>\n";
  11134. $stream .= "<7b00> <7bff> <7b00>\n";
  11135. $stream .= "<7c00> <7cff> <7c00>\n";
  11136. $stream .= "<7d00> <7dff> <7d00>\n";
  11137. $stream .= "<7e00> <7eff> <7e00>\n";
  11138. $stream .= "<7f00> <7fff> <7f00>\n";
  11139. $stream .= "<8000> <80ff> <8000>\n";
  11140. $stream .= "<8100> <81ff> <8100>\n";
  11141. $stream .= "<8200> <82ff> <8200>\n";
  11142. $stream .= "<8300> <83ff> <8300>\n";
  11143. $stream .= "<8400> <84ff> <8400>\n";
  11144. $stream .= "<8500> <85ff> <8500>\n";
  11145. $stream .= "<8600> <86ff> <8600>\n";
  11146. $stream .= "<8700> <87ff> <8700>\n";
  11147. $stream .= "<8800> <88ff> <8800>\n";
  11148. $stream .= "<8900> <89ff> <8900>\n";
  11149. $stream .= "<8a00> <8aff> <8a00>\n";
  11150. $stream .= "<8b00> <8bff> <8b00>\n";
  11151. $stream .= "<8c00> <8cff> <8c00>\n";
  11152. $stream .= "<8d00> <8dff> <8d00>\n";
  11153. $stream .= "<8e00> <8eff> <8e00>\n";
  11154. $stream .= "<8f00> <8fff> <8f00>\n";
  11155. $stream .= "<9000> <90ff> <9000>\n";
  11156. $stream .= "<9100> <91ff> <9100>\n";
  11157. $stream .= "<9200> <92ff> <9200>\n";
  11158. $stream .= "<9300> <93ff> <9300>\n";
  11159. $stream .= "<9400> <94ff> <9400>\n";
  11160. $stream .= "<9500> <95ff> <9500>\n";
  11161. $stream .= "<9600> <96ff> <9600>\n";
  11162. $stream .= "<9700> <97ff> <9700>\n";
  11163. $stream .= "<9800> <98ff> <9800>\n";
  11164. $stream .= "<9900> <99ff> <9900>\n";
  11165. $stream .= "<9a00> <9aff> <9a00>\n";
  11166. $stream .= "<9b00> <9bff> <9b00>\n";
  11167. $stream .= "<9c00> <9cff> <9c00>\n";
  11168. $stream .= "<9d00> <9dff> <9d00>\n";
  11169. $stream .= "<9e00> <9eff> <9e00>\n";
  11170. $stream .= "<9f00> <9fff> <9f00>\n";
  11171. $stream .= "<a000> <a0ff> <a000>\n";
  11172. $stream .= "<a100> <a1ff> <a100>\n";
  11173. $stream .= "<a200> <a2ff> <a200>\n";
  11174. $stream .= "<a300> <a3ff> <a300>\n";
  11175. $stream .= "<a400> <a4ff> <a400>\n";
  11176. $stream .= "<a500> <a5ff> <a500>\n";
  11177. $stream .= "<a600> <a6ff> <a600>\n";
  11178. $stream .= "<a700> <a7ff> <a700>\n";
  11179. $stream .= "<a800> <a8ff> <a800>\n";
  11180. $stream .= "<a900> <a9ff> <a900>\n";
  11181. $stream .= "<aa00> <aaff> <aa00>\n";
  11182. $stream .= "<ab00> <abff> <ab00>\n";
  11183. $stream .= "<ac00> <acff> <ac00>\n";
  11184. $stream .= "<ad00> <adff> <ad00>\n";
  11185. $stream .= "<ae00> <aeff> <ae00>\n";
  11186. $stream .= "<af00> <afff> <af00>\n";
  11187. $stream .= "<b000> <b0ff> <b000>\n";
  11188. $stream .= "<b100> <b1ff> <b100>\n";
  11189. $stream .= "<b200> <b2ff> <b200>\n";
  11190. $stream .= "<b300> <b3ff> <b300>\n";
  11191. $stream .= "<b400> <b4ff> <b400>\n";
  11192. $stream .= "<b500> <b5ff> <b500>\n";
  11193. $stream .= "<b600> <b6ff> <b600>\n";
  11194. $stream .= "<b700> <b7ff> <b700>\n";
  11195. $stream .= "<b800> <b8ff> <b800>\n";
  11196. $stream .= "<b900> <b9ff> <b900>\n";
  11197. $stream .= "<ba00> <baff> <ba00>\n";
  11198. $stream .= "<bb00> <bbff> <bb00>\n";
  11199. $stream .= "<bc00> <bcff> <bc00>\n";
  11200. $stream .= "<bd00> <bdff> <bd00>\n";
  11201. $stream .= "<be00> <beff> <be00>\n";
  11202. $stream .= "<bf00> <bfff> <bf00>\n";
  11203. $stream .= "<c000> <c0ff> <c000>\n";
  11204. $stream .= "<c100> <c1ff> <c100>\n";
  11205. $stream .= "<c200> <c2ff> <c200>\n";
  11206. $stream .= "<c300> <c3ff> <c300>\n";
  11207. $stream .= "<c400> <c4ff> <c400>\n";
  11208. $stream .= "<c500> <c5ff> <c500>\n";
  11209. $stream .= "<c600> <c6ff> <c600>\n";
  11210. $stream .= "<c700> <c7ff> <c700>\n";
  11211. $stream .= "endbfrange\n";
  11212. $stream .= "56 beginbfrange\n";
  11213. $stream .= "<c800> <c8ff> <c800>\n";
  11214. $stream .= "<c900> <c9ff> <c900>\n";
  11215. $stream .= "<ca00> <caff> <ca00>\n";
  11216. $stream .= "<cb00> <cbff> <cb00>\n";
  11217. $stream .= "<cc00> <ccff> <cc00>\n";
  11218. $stream .= "<cd00> <cdff> <cd00>\n";
  11219. $stream .= "<ce00> <ceff> <ce00>\n";
  11220. $stream .= "<cf00> <cfff> <cf00>\n";
  11221. $stream .= "<d000> <d0ff> <d000>\n";
  11222. $stream .= "<d100> <d1ff> <d100>\n";
  11223. $stream .= "<d200> <d2ff> <d200>\n";
  11224. $stream .= "<d300> <d3ff> <d300>\n";
  11225. $stream .= "<d400> <d4ff> <d400>\n";
  11226. $stream .= "<d500> <d5ff> <d500>\n";
  11227. $stream .= "<d600> <d6ff> <d600>\n";
  11228. $stream .= "<d700> <d7ff> <d700>\n";
  11229. $stream .= "<d800> <d8ff> <d800>\n";
  11230. $stream .= "<d900> <d9ff> <d900>\n";
  11231. $stream .= "<da00> <daff> <da00>\n";
  11232. $stream .= "<db00> <dbff> <db00>\n";
  11233. $stream .= "<dc00> <dcff> <dc00>\n";
  11234. $stream .= "<dd00> <ddff> <dd00>\n";
  11235. $stream .= "<de00> <deff> <de00>\n";
  11236. $stream .= "<df00> <dfff> <df00>\n";
  11237. $stream .= "<e000> <e0ff> <e000>\n";
  11238. $stream .= "<e100> <e1ff> <e100>\n";
  11239. $stream .= "<e200> <e2ff> <e200>\n";
  11240. $stream .= "<e300> <e3ff> <e300>\n";
  11241. $stream .= "<e400> <e4ff> <e400>\n";
  11242. $stream .= "<e500> <e5ff> <e500>\n";
  11243. $stream .= "<e600> <e6ff> <e600>\n";
  11244. $stream .= "<e700> <e7ff> <e700>\n";
  11245. $stream .= "<e800> <e8ff> <e800>\n";
  11246. $stream .= "<e900> <e9ff> <e900>\n";
  11247. $stream .= "<ea00> <eaff> <ea00>\n";
  11248. $stream .= "<eb00> <ebff> <eb00>\n";
  11249. $stream .= "<ec00> <ecff> <ec00>\n";
  11250. $stream .= "<ed00> <edff> <ed00>\n";
  11251. $stream .= "<ee00> <eeff> <ee00>\n";
  11252. $stream .= "<ef00> <efff> <ef00>\n";
  11253. $stream .= "<f000> <f0ff> <f000>\n";
  11254. $stream .= "<f100> <f1ff> <f100>\n";
  11255. $stream .= "<f200> <f2ff> <f200>\n";
  11256. $stream .= "<f300> <f3ff> <f300>\n";
  11257. $stream .= "<f400> <f4ff> <f400>\n";
  11258. $stream .= "<f500> <f5ff> <f500>\n";
  11259. $stream .= "<f600> <f6ff> <f600>\n";
  11260. $stream .= "<f700> <f7ff> <f700>\n";
  11261. $stream .= "<f800> <f8ff> <f800>\n";
  11262. $stream .= "<f900> <f9ff> <f900>\n";
  11263. $stream .= "<fa00> <faff> <fa00>\n";
  11264. $stream .= "<fb00> <fbff> <fb00>\n";
  11265. $stream .= "<fc00> <fcff> <fc00>\n";
  11266. $stream .= "<fd00> <fdff> <fd00>\n";
  11267. $stream .= "<fe00> <feff> <fe00>\n";
  11268. $stream .= "<ff00> <ffff> <ff00>\n";
  11269. $stream .= "endbfrange\n";
  11270. $stream .= "endcmap\n";
  11271. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11272. $stream .= "end\n";
  11273. $stream .= "end";
  11274. // ToUnicode Object
  11275. $this->_newobj();
  11276. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11277. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11278. $stream = $this->_getrawstream($stream);
  11279. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11280. // CIDFontType2
  11281. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11282. $oid = $this->_newobj();
  11283. $out = '<< /Type /Font';
  11284. $out .= ' /Subtype /CIDFontType2';
  11285. $out .= ' /BaseFont /'.$fontname;
  11286. // A dictionary containing entries that define the character collection of the CIDFont.
  11287. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11288. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11289. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11290. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11291. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11292. $out .= ' /DW '.$font['dw']; // default width
  11293. $out .= "\n".$this->_putfontwidths($font, 0);
  11294. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11295. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11296. }
  11297. $out .= ' >>';
  11298. $out .= "\n".'endobj';
  11299. $this->_out($out);
  11300. // Font descriptor
  11301. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11302. $this->_newobj();
  11303. $out = '<< /Type /FontDescriptor';
  11304. $out .= ' /FontName /'.$fontname;
  11305. foreach ($font['desc'] as $key => $value) {
  11306. if (is_float($value)) {
  11307. $value = sprintf('%.3F', $value);
  11308. }
  11309. $out .= ' /'.$key.' '.$value;
  11310. }
  11311. $fontdir = false;
  11312. if (!$this->empty_string($font['file'])) {
  11313. // A stream containing a TrueType font
  11314. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11315. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11316. }
  11317. $out .= ' >>';
  11318. $out .= "\n".'endobj';
  11319. $this->_out($out);
  11320. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11321. $this->_newobj();
  11322. // Embed CIDToGIDMap
  11323. // A specification of the mapping from CIDs to glyph indices
  11324. // search and get CTG font file to embedd
  11325. $ctgfile = strtolower($font['ctg']);
  11326. // search and get ctg font file to embedd
  11327. $fontfile = '';
  11328. // search files on various directories
  11329. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11330. $fontfile = $fontdir.$ctgfile;
  11331. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11332. $fontfile = $this->_getfontpath().$ctgfile;
  11333. } elseif (file_exists($ctgfile)) {
  11334. $fontfile = $ctgfile;
  11335. }
  11336. if ($this->empty_string($fontfile)) {
  11337. $this->Error('Font file not found: '.$ctgfile);
  11338. }
  11339. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11340. $out = '<< /Length '.strlen($stream).'';
  11341. if (substr($fontfile, -2) == '.z') { // check file extension
  11342. // Decompresses data encoded using the public-domain
  11343. // zlib/deflate compression method, reproducing the
  11344. // original text or binary data
  11345. $out .= ' /Filter /FlateDecode';
  11346. }
  11347. $out .= ' >>';
  11348. $out .= ' stream'."\n".$stream."\n".'endstream';
  11349. $out .= "\n".'endobj';
  11350. $this->_out($out);
  11351. }
  11352. }
  11353. /**
  11354. * Output CID-0 fonts.
  11355. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11356. * @param $font (array) font data
  11357. * @protected
  11358. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11359. * @since 3.2.000 (2008-06-23)
  11360. */
  11361. protected function _putcidfont0($font) {
  11362. $cidoffset = 0;
  11363. if (!isset($font['cw'][1])) {
  11364. $cidoffset = 31;
  11365. }
  11366. if (isset($font['cidinfo']['uni2cid'])) {
  11367. // convert unicode to cid.
  11368. $uni2cid = $font['cidinfo']['uni2cid'];
  11369. $cw = array();
  11370. foreach ($font['cw'] as $uni => $width) {
  11371. if (isset($uni2cid[$uni])) {
  11372. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11373. } elseif ($uni < 256) {
  11374. $cw[$uni] = $width;
  11375. } // else unknown character
  11376. }
  11377. $font = array_merge($font, array('cw' => $cw));
  11378. }
  11379. $name = $font['name'];
  11380. $enc = $font['enc'];
  11381. if ($enc) {
  11382. $longname = $name.'-'.$enc;
  11383. } else {
  11384. $longname = $name;
  11385. }
  11386. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11387. $out .= '<</Type /Font';
  11388. $out .= ' /Subtype /Type0';
  11389. $out .= ' /BaseFont /'.$longname;
  11390. $out .= ' /Name /F'.$font['i'];
  11391. if ($enc) {
  11392. $out .= ' /Encoding /'.$enc;
  11393. }
  11394. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11395. $out .= ' >>';
  11396. $out .= "\n".'endobj';
  11397. $this->_out($out);
  11398. $oid = $this->_newobj();
  11399. $out = '<</Type /Font';
  11400. $out .= ' /Subtype /CIDFontType0';
  11401. $out .= ' /BaseFont /'.$name;
  11402. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11403. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11404. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11405. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11406. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11407. $out .= ' /DW '.$font['dw'];
  11408. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11409. $out .= ' >>';
  11410. $out .= "\n".'endobj';
  11411. $this->_out($out);
  11412. $this->_newobj();
  11413. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11414. foreach ($font['desc'] as $k => $v) {
  11415. if ($k != 'Style') {
  11416. if (is_float($v)) {
  11417. $v = sprintf('%.3F', $v);
  11418. }
  11419. $s .= ' /'.$k.' '.$v.'';
  11420. }
  11421. }
  11422. $s .= '>>';
  11423. $s .= "\n".'endobj';
  11424. $this->_out($s);
  11425. }
  11426. /**
  11427. * Output images.
  11428. * @protected
  11429. */
  11430. protected function _putimages() {
  11431. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11432. foreach ($this->imagekeys as $file) {
  11433. $info = $this->getImageBuffer($file);
  11434. // set object for alternate images array
  11435. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11436. $altoid = $this->_newobj();
  11437. $out = '[';
  11438. foreach ($info['altimgs'] as $altimage) {
  11439. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11440. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11441. $out .= ' /DefaultForPrinting';
  11442. if ($altimage[1] === true) {
  11443. $out .= ' true';
  11444. } else {
  11445. $out .= ' false';
  11446. }
  11447. $out .= ' >>';
  11448. }
  11449. }
  11450. $out .= ' ]';
  11451. $out .= "\n".'endobj';
  11452. $this->_out($out);
  11453. }
  11454. // set image object
  11455. $oid = $this->_newobj();
  11456. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11457. $this->setImageSubBuffer($file, 'n', $this->n);
  11458. $out = '<</Type /XObject';
  11459. $out .= ' /Subtype /Image';
  11460. $out .= ' /Width '.$info['w'];
  11461. $out .= ' /Height '.$info['h'];
  11462. if (array_key_exists('masked', $info)) {
  11463. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11464. }
  11465. // set color space
  11466. $icc = false;
  11467. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11468. // ICC Colour Space
  11469. $icc = true;
  11470. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11471. } elseif ($info['cs'] == 'Indexed') {
  11472. // Indexed Colour Space
  11473. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11474. } else {
  11475. // Device Colour Space
  11476. $out .= ' /ColorSpace /'.$info['cs'];
  11477. }
  11478. if ($info['cs'] == 'DeviceCMYK') {
  11479. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11480. }
  11481. $out .= ' /BitsPerComponent '.$info['bpc'];
  11482. if (isset($altoid) AND ($altoid > 0)) {
  11483. // reference to alternate images dictionary
  11484. $out .= ' /Alternates '.$altoid.' 0 R';
  11485. }
  11486. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11487. // external stream
  11488. $out .= ' /Length 0';
  11489. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11490. if (isset($info['f'])) {
  11491. $out .= ' /FFilter /'.$info['f'];
  11492. }
  11493. $out .= ' >>';
  11494. $out .= ' stream'."\n".'endstream';
  11495. } else {
  11496. if (isset($info['f'])) {
  11497. $out .= ' /Filter /'.$info['f'];
  11498. }
  11499. if (isset($info['parms'])) {
  11500. $out .= ' '.$info['parms'];
  11501. }
  11502. if (isset($info['trns']) AND is_array($info['trns'])) {
  11503. $trns = '';
  11504. $count_info = count($info['trns']);
  11505. for ($i=0; $i < $count_info; ++$i) {
  11506. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11507. }
  11508. $out .= ' /Mask ['.$trns.']';
  11509. }
  11510. $stream = $this->_getrawstream($info['data']);
  11511. $out .= ' /Length '.strlen($stream).' >>';
  11512. $out .= ' stream'."\n".$stream."\n".'endstream';
  11513. }
  11514. $out .= "\n".'endobj';
  11515. $this->_out($out);
  11516. if ($icc) {
  11517. // ICC colour profile
  11518. $this->_newobj();
  11519. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11520. $icc = $this->_getrawstream($icc);
  11521. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11522. } elseif ($info['cs'] == 'Indexed') {
  11523. // colour palette
  11524. $this->_newobj();
  11525. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11526. $pal = $this->_getrawstream($pal);
  11527. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11528. }
  11529. }
  11530. }
  11531. /**
  11532. * Output Form XObjects Templates.
  11533. * @author Nicola Asuni
  11534. * @since 5.8.017 (2010-08-24)
  11535. * @protected
  11536. * @see startTemplate(), endTemplate(), printTemplate()
  11537. */
  11538. protected function _putxobjects() {
  11539. foreach ($this->xobjects as $key => $data) {
  11540. if (isset($data['outdata'])) {
  11541. $stream = trim($data['outdata']);
  11542. $out = $this->_getobj($data['n'])."\n";
  11543. $out .= '<<';
  11544. $out .= ' /Type /XObject';
  11545. $out .= ' /Subtype /Form';
  11546. $out .= ' /FormType 1';
  11547. if ($this->compress) {
  11548. $stream = gzcompress($stream);
  11549. $out .= ' /Filter /FlateDecode';
  11550. }
  11551. $out .= sprintf(' /BBox [%.2F %.2F %.2F %.2F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  11552. $out .= ' /Matrix [1 0 0 1 0 0]';
  11553. $out .= ' /Resources <<';
  11554. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11555. if (!$this->pdfa_mode) {
  11556. // transparency
  11557. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11558. $out .= ' /ExtGState <<';
  11559. foreach ($data['extgstates'] as $k => $extgstate) {
  11560. if (isset($this->extgstates[$k]['name'])) {
  11561. $out .= ' /'.$this->extgstates[$k]['name'];
  11562. } else {
  11563. $out .= ' /GS'.$k;
  11564. }
  11565. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11566. }
  11567. $out .= ' >>';
  11568. }
  11569. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11570. $gp = '';
  11571. $gs = '';
  11572. foreach ($data['gradients'] as $id => $grad) {
  11573. // gradient patterns
  11574. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11575. // gradient shadings
  11576. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11577. }
  11578. $out .= ' /Pattern <<'.$gp.' >>';
  11579. $out .= ' /Shading <<'.$gs.' >>';
  11580. }
  11581. }
  11582. // spot colors
  11583. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11584. $out .= ' /ColorSpace <<';
  11585. foreach ($data['spot_colors'] as $name => $color) {
  11586. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11587. }
  11588. $out .= ' >>';
  11589. }
  11590. // fonts
  11591. if (!empty($data['fonts'])) {
  11592. $out .= ' /Font <<';
  11593. foreach ($data['fonts'] as $fontkey => $fontid) {
  11594. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11595. }
  11596. $out .= ' >>';
  11597. }
  11598. // images or nested xobjects
  11599. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11600. $out .= ' /XObject <<';
  11601. foreach ($data['images'] as $imgid) {
  11602. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11603. }
  11604. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11605. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11606. }
  11607. $out .= ' >>';
  11608. }
  11609. $out .= ' >>'; //end resources
  11610. if (isset($data['group']) AND ($data['group'] !== false)) {
  11611. // set transparency group
  11612. $out .= ' /Group << /Type /Group /S /Transparency';
  11613. if (is_array($data['group'])) {
  11614. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11615. $out .= ' /CS /'.$data['group']['CS'];
  11616. }
  11617. if (isset($data['group']['I'])) {
  11618. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11619. }
  11620. if (isset($data['group']['K'])) {
  11621. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11622. }
  11623. }
  11624. $out .= ' >>';
  11625. }
  11626. $stream = $this->_getrawstream($stream, $data['n']);
  11627. $out .= ' /Length '.strlen($stream);
  11628. $out .= ' >>';
  11629. $out .= ' stream'."\n".$stream."\n".'endstream';
  11630. $out .= "\n".'endobj';
  11631. $this->_out($out);
  11632. }
  11633. }
  11634. }
  11635. /**
  11636. * Output Spot Colors Resources.
  11637. * @protected
  11638. * @since 4.0.024 (2008-09-12)
  11639. */
  11640. protected function _putspotcolors() {
  11641. foreach ($this->spot_colors as $name => $color) {
  11642. $this->_newobj();
  11643. $this->spot_colors[$name]['n'] = $this->n;
  11644. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  11645. $out .= ' /DeviceCMYK <<';
  11646. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  11647. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  11648. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  11649. $out .= "\n".'endobj';
  11650. $this->_out($out);
  11651. }
  11652. }
  11653. /**
  11654. * Return XObjects Dictionary.
  11655. * @return string XObjects dictionary
  11656. * @protected
  11657. * @since 5.8.014 (2010-08-23)
  11658. */
  11659. protected function _getxobjectdict() {
  11660. $out = '';
  11661. foreach ($this->xobjects as $id => $objid) {
  11662. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  11663. }
  11664. return $out;
  11665. }
  11666. /**
  11667. * Output Resources Dictionary.
  11668. * @protected
  11669. */
  11670. protected function _putresourcedict() {
  11671. $out = $this->_getobj(2)."\n";
  11672. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11673. $out .= ' /Font <<';
  11674. foreach ($this->fontkeys as $fontkey) {
  11675. $font = $this->getFontBuffer($fontkey);
  11676. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  11677. }
  11678. $out .= ' >>';
  11679. $out .= ' /XObject <<';
  11680. $out .= $this->_getxobjectdict();
  11681. $out .= ' >>';
  11682. // layers
  11683. if (!empty($this->pdflayers)) {
  11684. $out .= ' /Properties <<';
  11685. foreach ($this->pdflayers as $layer) {
  11686. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  11687. }
  11688. $out .= ' >>';
  11689. }
  11690. if (!$this->pdfa_mode) {
  11691. // transparency
  11692. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  11693. $out .= ' /ExtGState <<';
  11694. foreach ($this->extgstates as $k => $extgstate) {
  11695. if (isset($extgstate['name'])) {
  11696. $out .= ' /'.$extgstate['name'];
  11697. } else {
  11698. $out .= ' /GS'.$k;
  11699. }
  11700. $out .= ' '.$extgstate['n'].' 0 R';
  11701. }
  11702. $out .= ' >>';
  11703. }
  11704. if (isset($this->gradients) AND !empty($this->gradients)) {
  11705. $gp = '';
  11706. $gs = '';
  11707. foreach ($this->gradients as $id => $grad) {
  11708. // gradient patterns
  11709. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  11710. // gradient shadings
  11711. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  11712. }
  11713. $out .= ' /Pattern <<'.$gp.' >>';
  11714. $out .= ' /Shading <<'.$gs.' >>';
  11715. }
  11716. }
  11717. // spot colors
  11718. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  11719. $out .= ' /ColorSpace <<';
  11720. foreach ($this->spot_colors as $color) {
  11721. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  11722. }
  11723. $out .= ' >>';
  11724. }
  11725. $out .= ' >>';
  11726. $out .= "\n".'endobj';
  11727. $this->_out($out);
  11728. }
  11729. /**
  11730. * Output Resources.
  11731. * @protected
  11732. */
  11733. protected function _putresources() {
  11734. $this->_putextgstates();
  11735. $this->_putocg();
  11736. $this->_putfonts();
  11737. $this->_putimages();
  11738. $this->_putspotcolors();
  11739. $this->_putshaders();
  11740. $this->_putxobjects();
  11741. $this->_putresourcedict();
  11742. $this->_putdests();
  11743. $this->_putbookmarks();
  11744. $this->_putEmbeddedFiles();
  11745. $this->_putannotsobjs();
  11746. $this->_putjavascript();
  11747. $this->_putencryption();
  11748. }
  11749. /**
  11750. * Adds some Metadata information (Document Information Dictionary)
  11751. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  11752. * @return int object id
  11753. * @protected
  11754. */
  11755. protected function _putinfo() {
  11756. $oid = $this->_newobj();
  11757. $out = '<<';
  11758. // store current isunicode value
  11759. $prev_isunicode = $this->isunicode;
  11760. if ($this->docinfounicode) {
  11761. $this->isunicode = true;
  11762. }
  11763. if (!$this->empty_string($this->title)) {
  11764. // The document's title.
  11765. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  11766. }
  11767. if (!$this->empty_string($this->author)) {
  11768. // The name of the person who created the document.
  11769. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  11770. }
  11771. if (!$this->empty_string($this->subject)) {
  11772. // The subject of the document.
  11773. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  11774. }
  11775. if (!$this->empty_string($this->keywords)) {
  11776. // Keywords associated with the document.
  11777. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  11778. }
  11779. if (!$this->empty_string($this->creator)) {
  11780. // 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.
  11781. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  11782. }
  11783. // restore previous isunicode value
  11784. $this->isunicode = $prev_isunicode;
  11785. // default producer
  11786. $out .= ' /Producer '.$this->_textstring("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29", $oid);
  11787. // The date and time the document was created, in human-readable form
  11788. $out .= ' /CreationDate '.$this->_datestring();
  11789. // The date and time the document was most recently modified, in human-readable form
  11790. $out .= ' /ModDate '.$this->_datestring();
  11791. // A name object indicating whether the document has been modified to include trapping information
  11792. $out .= ' /Trapped /False';
  11793. $out .= ' >>';
  11794. $out .= "\n".'endobj';
  11795. $this->_out($out);
  11796. return $oid;
  11797. }
  11798. /**
  11799. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  11800. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  11801. * @param $xmp (string) Custom XMP data.
  11802. * @since 5.9.128 (2011-10-06)
  11803. * @public
  11804. */
  11805. public function setExtraXMP($xmp) {
  11806. $this->custom_xmp = $xmp;
  11807. }
  11808. /**
  11809. * Put XMP data object and return ID.
  11810. * @return (int) The object ID.
  11811. * @since 5.9.121 (2011-09-28)
  11812. * @protected
  11813. */
  11814. protected function _putXMP() {
  11815. $oid = $this->_newobj();
  11816. // store current isunicode value
  11817. $prev_isunicode = $this->isunicode;
  11818. $this->isunicode = true;
  11819. $prev_encrypted = $this->encrypted;
  11820. $this->encrypted = false;
  11821. // set XMP data
  11822. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  11823. $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";
  11824. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  11825. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  11826. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  11827. $xmp .= "\t\t\t".'<dc:title>'."\n";
  11828. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11829. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  11830. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11831. $xmp .= "\t\t\t".'</dc:title>'."\n";
  11832. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  11833. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  11834. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  11835. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  11836. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  11837. $xmp .= "\t\t\t".'<dc:description>'."\n";
  11838. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11839. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  11840. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11841. $xmp .= "\t\t\t".'</dc:description>'."\n";
  11842. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  11843. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11844. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  11845. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11846. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  11847. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11848. // convert date format
  11849. $docdate = substr($this->doc_date, 0, 4).'-'.substr($this->doc_date, 4, 2).'-'.substr($this->doc_date, 6, 2);
  11850. $docdate .= 'T'.substr($this->doc_date, 8, 2).':'.substr($this->doc_date, 10, 2).':'.substr($this->doc_date, 12, 2);
  11851. $docdate .= '+'.substr($this->doc_date, 15, 2).':'.substr($this->doc_date, 18, 2);
  11852. $docdate = $this->_escapeXML($docdate);
  11853. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  11854. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$docdate.'</xmp:CreateDate>'."\n";
  11855. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  11856. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docdate.'</xmp:ModifyDate>'."\n";
  11857. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$docdate.'</xmp:MetadataDate>'."\n";
  11858. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11859. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  11860. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  11861. $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29").'</pdf:Producer>'."\n";
  11862. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11863. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  11864. $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);
  11865. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  11866. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  11867. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11868. if ($this->pdfa_mode) {
  11869. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  11870. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  11871. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  11872. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11873. }
  11874. // XMP extension schemas
  11875. $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";
  11876. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  11877. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11878. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11879. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  11880. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  11881. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  11882. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11883. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11884. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  11885. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  11886. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  11887. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11888. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11889. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11890. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11891. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  11892. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  11893. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  11894. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11895. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11896. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11897. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11898. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11899. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  11900. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  11901. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  11902. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11903. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11904. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11905. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11906. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  11907. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  11908. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  11909. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11910. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11911. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11912. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  11913. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  11914. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11915. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11916. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11917. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11918. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  11919. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  11920. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11921. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11922. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11923. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11924. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11925. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11926. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  11927. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11928. $xmp .= "\t".'</rdf:RDF>'."\n";
  11929. $xmp .= $this->custom_xmp;
  11930. $xmp .= '</x:xmpmeta>'."\n";
  11931. $xmp .= '<?xpacket end="w"?>';
  11932. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  11933. // restore previous isunicode value
  11934. $this->isunicode = $prev_isunicode;
  11935. $this->encrypted = $prev_encrypted;
  11936. $this->_out($out);
  11937. return $oid;
  11938. }
  11939. /**
  11940. * Output Catalog.
  11941. * @return int object id
  11942. * @protected
  11943. */
  11944. protected function _putcatalog() {
  11945. // put XMP
  11946. $xmpobj = $this->_putXMP();
  11947. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  11948. if ($this->pdfa_mode OR $this->force_srgb) {
  11949. $iccobj = $this->_newobj();
  11950. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  11951. $filter = '';
  11952. if ($this->compress) {
  11953. $filter = ' /Filter /FlateDecode';
  11954. $icc = gzcompress($icc);
  11955. }
  11956. $icc = $this->_getrawstream($icc);
  11957. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11958. }
  11959. // start catalog
  11960. $oid = $this->_newobj();
  11961. $out = '<< /Type /Catalog';
  11962. $out .= ' /Version /'.$this->PDFVersion;
  11963. //$out .= ' /Extensions <<>>';
  11964. $out .= ' /Pages 1 0 R';
  11965. //$out .= ' /PageLabels ' //...;
  11966. $out .= ' /Names <<';
  11967. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  11968. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  11969. }
  11970. $out .= ' >>';
  11971. if (!empty($this->dests)) {
  11972. $out .= ' /Dests '.$this->n_dests.' 0 R';
  11973. }
  11974. $out .= $this->_putviewerpreferences();
  11975. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  11976. $out .= ' /PageLayout /'.$this->LayoutMode;
  11977. }
  11978. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  11979. $out .= ' /PageMode /'.$this->PageMode;
  11980. }
  11981. if (count($this->outlines) > 0) {
  11982. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  11983. $out .= ' /PageMode /UseOutlines';
  11984. }
  11985. //$out .= ' /Threads []';
  11986. if ($this->ZoomMode == 'fullpage') {
  11987. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  11988. } elseif ($this->ZoomMode == 'fullwidth') {
  11989. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  11990. } elseif ($this->ZoomMode == 'real') {
  11991. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  11992. } elseif (!is_string($this->ZoomMode)) {
  11993. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
  11994. }
  11995. //$out .= ' /AA <<>>';
  11996. //$out .= ' /URI <<>>';
  11997. $out .= ' /Metadata '.$xmpobj.' 0 R';
  11998. //$out .= ' /StructTreeRoot <<>>';
  11999. //$out .= ' /MarkInfo <<>>';
  12000. if (isset($this->l['a_meta_language'])) {
  12001. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  12002. }
  12003. //$out .= ' /SpiderInfo <<>>';
  12004. // set OutputIntent to sRGB IEC61966-2.1 if required
  12005. if ($this->pdfa_mode OR $this->force_srgb) {
  12006. $out .= ' /OutputIntents [<<';
  12007. $out .= ' /Type /OutputIntent';
  12008. $out .= ' /S /GTS_PDFA1';
  12009. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12010. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12011. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12012. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12013. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12014. $out .= ' >>]';
  12015. }
  12016. //$out .= ' /PieceInfo <<>>';
  12017. if (!empty($this->pdflayers)) {
  12018. $lyrobjs = '';
  12019. $lyrobjs_print = '';
  12020. $lyrobjs_view = '';
  12021. foreach ($this->pdflayers as $layer) {
  12022. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12023. if ($layer['print']) {
  12024. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12025. }
  12026. if ($layer['view']) {
  12027. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12028. }
  12029. }
  12030. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12031. $out .= ' /D <<';
  12032. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12033. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12034. $out .= ' /BaseState /ON';
  12035. $out .= ' /ON ['.$lyrobjs_print.']';
  12036. $out .= ' /OFF ['.$lyrobjs_view.']';
  12037. $out .= ' /Intent /View';
  12038. $out .= ' /AS [';
  12039. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12040. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12041. $out .= ' ]';
  12042. $out .= ' /Order ['.$lyrobjs.']';
  12043. $out .= ' /ListMode /AllPages';
  12044. //$out .= ' /RBGroups ['..']';
  12045. //$out .= ' /Locked ['..']';
  12046. $out .= ' >>';
  12047. $out .= ' >>';
  12048. }
  12049. // AcroForm
  12050. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12051. $out .= ' /AcroForm <<';
  12052. $objrefs = '';
  12053. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12054. // set reference for signature object
  12055. $objrefs .= $this->sig_obj_id.' 0 R';
  12056. }
  12057. if (!empty($this->empty_signature_appearance)) {
  12058. foreach ($this->empty_signature_appearance as $esa) {
  12059. // set reference for empty signature objects
  12060. $objrefs .= ' '.$esa['objid'].' 0 R';
  12061. }
  12062. }
  12063. if (!empty($this->form_obj_id)) {
  12064. foreach($this->form_obj_id as $objid) {
  12065. $objrefs .= ' '.$objid.' 0 R';
  12066. }
  12067. }
  12068. $out .= ' /Fields ['.$objrefs.']';
  12069. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12070. $out .= ' /NeedAppearances false';
  12071. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12072. if ($this->signature_data['cert_type'] > 0) {
  12073. $out .= ' /SigFlags 3';
  12074. } else {
  12075. $out .= ' /SigFlags 1';
  12076. }
  12077. }
  12078. //$out .= ' /CO ';
  12079. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12080. $out .= ' /DR <<';
  12081. $out .= ' /Font <<';
  12082. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12083. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12084. }
  12085. $out .= ' >> >>';
  12086. }
  12087. $font = $this->getFontBuffer('helvetica');
  12088. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12089. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12090. //$out .= ' /XFA ';
  12091. $out .= ' >>';
  12092. // signatures
  12093. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12094. if ($this->signature_data['cert_type'] > 0) {
  12095. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12096. } else {
  12097. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12098. }
  12099. }
  12100. }
  12101. //$out .= ' /Legal <<>>';
  12102. //$out .= ' /Requirements []';
  12103. //$out .= ' /Collection <<>>';
  12104. //$out .= ' /NeedsRendering true';
  12105. $out .= ' >>';
  12106. $out .= "\n".'endobj';
  12107. $this->_out($out);
  12108. return $oid;
  12109. }
  12110. /**
  12111. * Output viewer preferences.
  12112. * @return string for viewer preferences
  12113. * @author Nicola asuni
  12114. * @since 3.1.000 (2008-06-09)
  12115. * @protected
  12116. */
  12117. protected function _putviewerpreferences() {
  12118. $out = ' /ViewerPreferences <<';
  12119. if ($this->rtl) {
  12120. $out .= ' /Direction /R2L';
  12121. } else {
  12122. $out .= ' /Direction /L2R';
  12123. }
  12124. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12125. $out .= ' /HideToolbar true';
  12126. }
  12127. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12128. $out .= ' /HideMenubar true';
  12129. }
  12130. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12131. $out .= ' /HideWindowUI true';
  12132. }
  12133. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12134. $out .= ' /FitWindow true';
  12135. }
  12136. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12137. $out .= ' /CenterWindow true';
  12138. }
  12139. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12140. $out .= ' /DisplayDocTitle true';
  12141. }
  12142. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12143. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12144. }
  12145. if (isset($this->viewer_preferences['ViewArea'])) {
  12146. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12147. }
  12148. if (isset($this->viewer_preferences['ViewClip'])) {
  12149. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12150. }
  12151. if (isset($this->viewer_preferences['PrintArea'])) {
  12152. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12153. }
  12154. if (isset($this->viewer_preferences['PrintClip'])) {
  12155. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12156. }
  12157. if (isset($this->viewer_preferences['PrintScaling'])) {
  12158. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12159. }
  12160. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12161. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12162. }
  12163. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12164. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12165. $out .= ' /PickTrayByPDFSize true';
  12166. } else {
  12167. $out .= ' /PickTrayByPDFSize false';
  12168. }
  12169. }
  12170. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12171. $PrintPageRangeNum = '';
  12172. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12173. $PrintPageRangeNum .= ' '.($v - 1).'';
  12174. }
  12175. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12176. }
  12177. if (isset($this->viewer_preferences['NumCopies'])) {
  12178. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12179. }
  12180. $out .= ' >>';
  12181. return $out;
  12182. }
  12183. /**
  12184. * Output PDF File Header (7.5.2).
  12185. * @protected
  12186. */
  12187. protected function _putheader() {
  12188. $this->_out('%PDF-'.$this->PDFVersion);
  12189. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12190. }
  12191. /**
  12192. * Output end of document (EOF).
  12193. * @protected
  12194. */
  12195. protected function _enddoc() {
  12196. $this->state = 1;
  12197. $this->_putheader();
  12198. $this->_putpages();
  12199. $this->_putresources();
  12200. // empty signature fields
  12201. if (!empty($this->empty_signature_appearance)) {
  12202. foreach ($this->empty_signature_appearance as $key => $esa) {
  12203. // widget annotation for empty signature
  12204. $out = $this->_getobj($esa['objid'])."\n";
  12205. $out .= '<< /Type /Annot';
  12206. $out .= ' /Subtype /Widget';
  12207. $out .= ' /Rect ['.$esa['rect'].']';
  12208. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12209. $out .= ' /F 4';
  12210. $out .= ' /FT /Sig';
  12211. $signame = sprintf('Signature_%03d', ($key + 1));
  12212. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12213. $out .= ' /Ff 0';
  12214. $out .= ' >>';
  12215. $out .= "\n".'endobj';
  12216. $this->_out($out);
  12217. }
  12218. }
  12219. // Signature
  12220. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12221. // widget annotation for signature
  12222. $out = $this->_getobj($this->sig_obj_id)."\n";
  12223. $out .= '<< /Type /Annot';
  12224. $out .= ' /Subtype /Widget';
  12225. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12226. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12227. $out .= ' /F 4';
  12228. $out .= ' /FT /Sig';
  12229. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12230. $out .= ' /Ff 0';
  12231. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12232. $out .= ' >>';
  12233. $out .= "\n".'endobj';
  12234. $this->_out($out);
  12235. // signature
  12236. $this->_putsignature();
  12237. }
  12238. // Info
  12239. $objid_info = $this->_putinfo();
  12240. // Catalog
  12241. $objid_catalog = $this->_putcatalog();
  12242. // Cross-ref
  12243. $o = $this->bufferlen;
  12244. // XREF section
  12245. $this->_out('xref');
  12246. $this->_out('0 '.($this->n + 1));
  12247. $this->_out('0000000000 65535 f ');
  12248. for ($i=1; $i <= $this->n; ++$i) {
  12249. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12250. $this->offsets[$i] = $this->offsets[($i - 1)];
  12251. }
  12252. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12253. }
  12254. // TRAILER
  12255. $out = 'trailer'."\n";
  12256. $out .= '<<';
  12257. $out .= ' /Size '.($this->n + 1);
  12258. $out .= ' /Root '.$objid_catalog.' 0 R';
  12259. $out .= ' /Info '.$objid_info.' 0 R';
  12260. if ($this->encrypted) {
  12261. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12262. }
  12263. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12264. $out .= ' >>';
  12265. $this->_out($out);
  12266. $this->_out('startxref');
  12267. $this->_out($o);
  12268. $this->_out('%%EOF');
  12269. $this->state = 3; // end-of-doc
  12270. if ($this->diskcache) {
  12271. // remove temporary files used for images
  12272. foreach ($this->imagekeys as $key) {
  12273. // remove temporary files
  12274. unlink($this->images[$key]);
  12275. }
  12276. foreach ($this->fontkeys as $key) {
  12277. // remove temporary files
  12278. unlink($this->fonts[$key]);
  12279. }
  12280. }
  12281. }
  12282. /**
  12283. * Initialize a new page.
  12284. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12285. * @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().
  12286. * @protected
  12287. * @see getPageSizeFromFormat(), setPageFormat()
  12288. */
  12289. protected function _beginpage($orientation='', $format='') {
  12290. ++$this->page;
  12291. $this->setPageBuffer($this->page, '');
  12292. // initialize array for graphics tranformation positions inside a page buffer
  12293. $this->transfmrk[$this->page] = array();
  12294. $this->state = 2;
  12295. if ($this->empty_string($orientation)) {
  12296. if (isset($this->CurOrientation)) {
  12297. $orientation = $this->CurOrientation;
  12298. } elseif ($this->fwPt > $this->fhPt) {
  12299. // landscape
  12300. $orientation = 'L';
  12301. } else {
  12302. // portrait
  12303. $orientation = 'P';
  12304. }
  12305. }
  12306. if ($this->empty_string($format)) {
  12307. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12308. $this->setPageOrientation($orientation);
  12309. } else {
  12310. $this->setPageFormat($format, $orientation);
  12311. }
  12312. if ($this->rtl) {
  12313. $this->x = $this->w - $this->rMargin;
  12314. } else {
  12315. $this->x = $this->lMargin;
  12316. }
  12317. $this->y = $this->tMargin;
  12318. if (isset($this->newpagegroup[$this->page])) {
  12319. // start a new group
  12320. $this->currpagegroup = $this->newpagegroup[$this->page];
  12321. $this->pagegroups[$this->currpagegroup] = 1;
  12322. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12323. ++$this->pagegroups[$this->currpagegroup];
  12324. }
  12325. }
  12326. /**
  12327. * Mark end of page.
  12328. * @protected
  12329. */
  12330. protected function _endpage() {
  12331. $this->setVisibility('all');
  12332. $this->state = 1;
  12333. }
  12334. /**
  12335. * Begin a new object and return the object number.
  12336. * @return int object number
  12337. * @protected
  12338. */
  12339. protected function _newobj() {
  12340. $this->_out($this->_getobj());
  12341. return $this->n;
  12342. }
  12343. /**
  12344. * Return the starting object string for the selected object ID.
  12345. * @param $objid (int) Object ID (leave empty to get a new ID).
  12346. * @return string the starting object string
  12347. * @protected
  12348. * @since 5.8.009 (2010-08-20)
  12349. */
  12350. protected function _getobj($objid='') {
  12351. if ($objid === '') {
  12352. ++$this->n;
  12353. $objid = $this->n;
  12354. }
  12355. $this->offsets[$objid] = $this->bufferlen;
  12356. return $objid.' 0 obj';
  12357. }
  12358. /**
  12359. * Underline text.
  12360. * @param $x (int) X coordinate
  12361. * @param $y (int) Y coordinate
  12362. * @param $txt (string) text to underline
  12363. * @protected
  12364. */
  12365. protected function _dounderline($x, $y, $txt) {
  12366. $w = $this->GetStringWidth($txt);
  12367. return $this->_dounderlinew($x, $y, $w);
  12368. }
  12369. /**
  12370. * Underline for rectangular text area.
  12371. * @param $x (int) X coordinate
  12372. * @param $y (int) Y coordinate
  12373. * @param $w (int) width to underline
  12374. * @protected
  12375. * @since 4.8.008 (2009-09-29)
  12376. */
  12377. protected function _dounderlinew($x, $y, $w) {
  12378. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12379. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12380. }
  12381. /**
  12382. * Line through text.
  12383. * @param $x (int) X coordinate
  12384. * @param $y (int) Y coordinate
  12385. * @param $txt (string) text to linethrough
  12386. * @protected
  12387. */
  12388. protected function _dolinethrough($x, $y, $txt) {
  12389. $w = $this->GetStringWidth($txt);
  12390. return $this->_dolinethroughw($x, $y, $w);
  12391. }
  12392. /**
  12393. * Line through for rectangular text area.
  12394. * @param $x (int) X coordinate
  12395. * @param $y (int) Y coordinate
  12396. * @param $w (int) line length (width)
  12397. * @protected
  12398. * @since 4.9.008 (2009-09-29)
  12399. */
  12400. protected function _dolinethroughw($x, $y, $w) {
  12401. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12402. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12403. }
  12404. /**
  12405. * Overline text.
  12406. * @param $x (int) X coordinate
  12407. * @param $y (int) Y coordinate
  12408. * @param $txt (string) text to overline
  12409. * @protected
  12410. * @since 4.9.015 (2010-04-19)
  12411. */
  12412. protected function _dooverline($x, $y, $txt) {
  12413. $w = $this->GetStringWidth($txt);
  12414. return $this->_dooverlinew($x, $y, $w);
  12415. }
  12416. /**
  12417. * Overline for rectangular text area.
  12418. * @param $x (int) X coordinate
  12419. * @param $y (int) Y coordinate
  12420. * @param $w (int) width to overline
  12421. * @protected
  12422. * @since 4.9.015 (2010-04-19)
  12423. */
  12424. protected function _dooverlinew($x, $y, $w) {
  12425. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12426. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12427. }
  12428. /**
  12429. * Read a 4-byte (32 bit) integer from file.
  12430. * @param $f (string) file name.
  12431. * @return 4-byte integer
  12432. * @protected
  12433. */
  12434. protected function _freadint($f) {
  12435. $a = unpack('Ni', fread($f, 4));
  12436. return $a['i'];
  12437. }
  12438. /**
  12439. * Add "\" before "\", "(" and ")"
  12440. * @param $s (string) string to escape.
  12441. * @return string escaped string.
  12442. * @protected
  12443. */
  12444. protected function _escape($s) {
  12445. // the chr(13) substitution fixes the Bugs item #1421290.
  12446. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12447. }
  12448. /**
  12449. * Format a data string for meta information
  12450. * @param $s (string) data string to escape.
  12451. * @param $n (int) object ID
  12452. * @return string escaped string.
  12453. * @protected
  12454. */
  12455. protected function _datastring($s, $n=0) {
  12456. if ($n == 0) {
  12457. $n = $this->n;
  12458. }
  12459. $s = $this->_encrypt_data($n, $s);
  12460. return '('. $this->_escape($s).')';
  12461. }
  12462. /**
  12463. * Returns a formatted date for meta information
  12464. * @param $n (int) object ID
  12465. * @return string escaped date string.
  12466. * @protected
  12467. * @since 4.6.028 (2009-08-25)
  12468. */
  12469. protected function _datestring($n=0) {
  12470. return $this->_datastring('D:'.$this->doc_date, $n);
  12471. }
  12472. /**
  12473. * Format a text string for meta information
  12474. * @param $s (string) string to escape.
  12475. * @param $n (int) object ID
  12476. * @return string escaped string.
  12477. * @protected
  12478. */
  12479. protected function _textstring($s, $n=0) {
  12480. if ($this->isunicode) {
  12481. //Convert string to UTF-16BE
  12482. $s = $this->UTF8ToUTF16BE($s, true);
  12483. }
  12484. return $this->_datastring($s, $n);
  12485. }
  12486. /**
  12487. * THIS METHOD IS DEPRECATED
  12488. * Format a text string
  12489. * @param $s (string) string to escape.
  12490. * @return string escaped string.
  12491. * @protected
  12492. * @deprecated
  12493. */
  12494. protected function _escapetext($s) {
  12495. if ($this->isunicode) {
  12496. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12497. $s = $this->UTF8ToLatin1($s);
  12498. } else {
  12499. //Convert string to UTF-16BE and reverse RTL language
  12500. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12501. }
  12502. }
  12503. return $this->_escape($s);
  12504. }
  12505. /**
  12506. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12507. * @param $str (string) Input string to convert.
  12508. * @return converted string
  12509. * @since 5.9.121 (2011-09-28)
  12510. * @protected
  12511. */
  12512. protected function _escapeXML($str) {
  12513. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12514. $str = strtr($str, $replaceTable);
  12515. return $str;
  12516. }
  12517. /**
  12518. * get raw output stream.
  12519. * @param $s (string) string to output.
  12520. * @param $n (int) object reference for encryption mode
  12521. * @protected
  12522. * @author Nicola Asuni
  12523. * @since 5.5.000 (2010-06-22)
  12524. */
  12525. protected function _getrawstream($s, $n=0) {
  12526. if ($n <= 0) {
  12527. // default to current object
  12528. $n = $this->n;
  12529. }
  12530. return $this->_encrypt_data($n, $s);
  12531. }
  12532. /**
  12533. * Format output stream (DEPRECATED).
  12534. * @param $s (string) string to output.
  12535. * @param $n (int) object reference for encryption mode
  12536. * @protected
  12537. * @deprecated
  12538. */
  12539. protected function _getstream($s, $n=0) {
  12540. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12541. }
  12542. /**
  12543. * Output a stream (DEPRECATED).
  12544. * @param $s (string) string to output.
  12545. * @param $n (int) object reference for encryption mode
  12546. * @protected
  12547. * @deprecated
  12548. */
  12549. protected function _putstream($s, $n=0) {
  12550. $this->_out($this->_getstream($s, $n));
  12551. }
  12552. /**
  12553. * Output a string to the document.
  12554. * @param $s (string) string to output.
  12555. * @protected
  12556. */
  12557. protected function _out($s) {
  12558. if ($this->state == 2) {
  12559. if ($this->inxobj) {
  12560. // we are inside an XObject template
  12561. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  12562. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  12563. // puts data before page footer
  12564. $pagebuff = $this->getPageBuffer($this->page);
  12565. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  12566. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  12567. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  12568. // update footer position
  12569. $this->footerpos[$this->page] += strlen($s."\n");
  12570. } else {
  12571. $this->setPageBuffer($this->page, $s."\n", true);
  12572. }
  12573. } else {
  12574. $this->setBuffer($s."\n");
  12575. }
  12576. }
  12577. /**
  12578. * Converts UTF-8 strings to codepoints array.<br>
  12579. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  12580. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  12581. * <pre>
  12582. * Char. number range | UTF-8 octet sequence
  12583. * (hexadecimal) | (binary)
  12584. * --------------------+-----------------------------------------------
  12585. * 0000 0000-0000 007F | 0xxxxxxx
  12586. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  12587. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  12588. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  12589. * ---------------------------------------------------------------------
  12590. *
  12591. * ABFN notation:
  12592. * ---------------------------------------------------------------------
  12593. * UTF8-octets = *( UTF8-char )
  12594. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  12595. * UTF8-1 = %x00-7F
  12596. * UTF8-2 = %xC2-DF UTF8-tail
  12597. *
  12598. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  12599. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  12600. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  12601. * %xF4 %x80-8F 2( UTF8-tail )
  12602. * UTF8-tail = %x80-BF
  12603. * ---------------------------------------------------------------------
  12604. * </pre>
  12605. * @param $str (string) string to process.
  12606. * @return array containing codepoints (UTF-8 characters values)
  12607. * @protected
  12608. * @author Nicola Asuni
  12609. * @since 1.53.0.TC005 (2005-01-05)
  12610. */
  12611. protected function UTF8StringToArray($str) {
  12612. // build a unique string key
  12613. $strkey = md5($str);
  12614. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  12615. // return cached value
  12616. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  12617. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  12618. if ($this->isunicode) {
  12619. foreach ($chrarray as $chr) {
  12620. // store this char for font subsetting
  12621. $this->CurrentFont['subsetchars'][$chr] = true;
  12622. }
  12623. // update font subsetchars
  12624. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12625. }
  12626. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12627. }
  12628. return $chrarray;
  12629. }
  12630. // check cache size
  12631. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  12632. // remove first element
  12633. array_shift($this->cache_UTF8StringToArray);
  12634. }
  12635. // new cache array for selected string
  12636. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  12637. ++$this->cache_size_UTF8StringToArray;
  12638. if (!$this->isunicode) {
  12639. // split string into array of equivalent codes
  12640. $strarr = array();
  12641. $strlen = strlen($str);
  12642. for ($i=0; $i < $strlen; ++$i) {
  12643. $strarr[] = ord($str[$i]);
  12644. }
  12645. // insert new value on cache
  12646. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  12647. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12648. return $strarr;
  12649. }
  12650. $unichar = -1; // last unicode char
  12651. $unicode = array(); // array containing unicode values
  12652. $bytes = array(); // array containing single character byte sequences
  12653. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  12654. $str .= ''; // force $str to be a string
  12655. $length = strlen($str);
  12656. for ($i = 0; $i < $length; ++$i) {
  12657. $char = ord($str[$i]); // get one string character at time
  12658. if (count($bytes) == 0) { // get starting octect
  12659. if ($char <= 0x7F) {
  12660. $unichar = $char; // use the character "as is" because is ASCII
  12661. $numbytes = 1;
  12662. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  12663. $bytes[] = ($char - 0xC0) << 0x06;
  12664. $numbytes = 2;
  12665. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  12666. $bytes[] = ($char - 0xE0) << 0x0C;
  12667. $numbytes = 3;
  12668. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  12669. $bytes[] = ($char - 0xF0) << 0x12;
  12670. $numbytes = 4;
  12671. } else {
  12672. // use replacement character for other invalid sequences
  12673. $unichar = 0xFFFD;
  12674. $bytes = array();
  12675. $numbytes = 1;
  12676. }
  12677. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  12678. $bytes[] = $char - 0x80;
  12679. if (count($bytes) == $numbytes) {
  12680. // compose UTF-8 bytes to a single unicode value
  12681. $char = $bytes[0];
  12682. for ($j = 1; $j < $numbytes; ++$j) {
  12683. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  12684. }
  12685. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  12686. /* The definition of UTF-8 prohibits encoding character numbers between
  12687. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  12688. encoding form (as surrogate pairs) and do not directly represent
  12689. characters. */
  12690. $unichar = 0xFFFD; // use replacement character
  12691. } else {
  12692. $unichar = $char; // add char to array
  12693. }
  12694. // reset data for next char
  12695. $bytes = array();
  12696. $numbytes = 1;
  12697. }
  12698. } else {
  12699. // use replacement character for other invalid sequences
  12700. $unichar = 0xFFFD;
  12701. $bytes = array();
  12702. $numbytes = 1;
  12703. }
  12704. if ($unichar >= 0) {
  12705. // insert unicode value into array
  12706. $unicode[] = $unichar;
  12707. // store this char for font subsetting
  12708. $this->CurrentFont['subsetchars'][$unichar] = true;
  12709. $unichar = -1;
  12710. }
  12711. }
  12712. // update font subsetchars
  12713. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12714. // insert new value on cache
  12715. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  12716. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12717. return $unicode;
  12718. }
  12719. /**
  12720. * Converts UTF-8 strings to UTF16-BE.<br>
  12721. * @param $str (string) string to process.
  12722. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12723. * @return string
  12724. * @author Nicola Asuni
  12725. * @since 1.53.0.TC005 (2005-01-05)
  12726. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  12727. * @protected
  12728. */
  12729. protected function UTF8ToUTF16BE($str, $setbom=false) {
  12730. if (!$this->isunicode) {
  12731. return $str; // string is not in unicode
  12732. }
  12733. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12734. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  12735. }
  12736. /**
  12737. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  12738. * @param $str (string) string to process.
  12739. * @return string
  12740. * @author Andrew Whitehead, Nicola Asuni
  12741. * @protected
  12742. * @since 3.2.000 (2008-06-23)
  12743. */
  12744. protected function UTF8ToLatin1($str) {
  12745. if (!$this->isunicode) {
  12746. return $str; // string is not in unicode
  12747. }
  12748. $outstr = ''; // string to be returned
  12749. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12750. foreach ($unicode as $char) {
  12751. if ($char < 256) {
  12752. $outstr .= chr($char);
  12753. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12754. // map from UTF-8
  12755. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  12756. } elseif ($char == 0xFFFD) {
  12757. // skip
  12758. } else {
  12759. $outstr .= '?';
  12760. }
  12761. }
  12762. return $outstr;
  12763. }
  12764. /**
  12765. * Converts UTF-8 characters array to array of Latin1 characters<br>
  12766. * @param $unicode (array) array containing UTF-8 unicode values
  12767. * @return array
  12768. * @author Nicola Asuni
  12769. * @protected
  12770. * @since 4.8.023 (2010-01-15)
  12771. */
  12772. protected function UTF8ArrToLatin1($unicode) {
  12773. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  12774. return $unicode;
  12775. }
  12776. $outarr = array(); // array to be returned
  12777. foreach ($unicode as $char) {
  12778. if ($char < 256) {
  12779. $outarr[] = $char;
  12780. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12781. // map from UTF-8
  12782. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  12783. } elseif ($char == 0xFFFD) {
  12784. // skip
  12785. } else {
  12786. $outarr[] = 63; // '?' character
  12787. }
  12788. }
  12789. return $outarr;
  12790. }
  12791. /**
  12792. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  12793. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  12794. * <pre>
  12795. * Encoding UTF-16:
  12796. *
  12797. * Encoding of a single character from an ISO 10646 character value to
  12798. * UTF-16 proceeds as follows. Let U be the character number, no greater
  12799. * than 0x10FFFF.
  12800. *
  12801. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  12802. * terminate.
  12803. *
  12804. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  12805. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  12806. * represented in 20 bits.
  12807. *
  12808. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  12809. * 0xDC00, respectively. These integers each have 10 bits free to
  12810. * encode the character value, for a total of 20 bits.
  12811. *
  12812. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  12813. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  12814. * bits of W2. Terminate.
  12815. *
  12816. * Graphically, steps 2 through 4 look like:
  12817. * U' = yyyyyyyyyyxxxxxxxxxx
  12818. * W1 = 110110yyyyyyyyyy
  12819. * W2 = 110111xxxxxxxxxx
  12820. * </pre>
  12821. * @param $unicode (array) array containing UTF-8 unicode values
  12822. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12823. * @return string
  12824. * @protected
  12825. * @author Nicola Asuni
  12826. * @since 2.1.000 (2008-01-08)
  12827. * @see UTF8ToUTF16BE()
  12828. */
  12829. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  12830. $outstr = ''; // string to be returned
  12831. if ($setbom) {
  12832. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  12833. }
  12834. foreach ($unicode as $char) {
  12835. if ($char == 0x200b) {
  12836. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  12837. } elseif ($char == 0xFFFD) {
  12838. $outstr .= "\xFF\xFD"; // replacement character
  12839. } elseif ($char < 0x10000) {
  12840. $outstr .= chr($char >> 0x08);
  12841. $outstr .= chr($char & 0xFF);
  12842. } else {
  12843. $char -= 0x10000;
  12844. $w1 = 0xD800 | ($char >> 0x0a);
  12845. $w2 = 0xDC00 | ($char & 0x3FF);
  12846. $outstr .= chr($w1 >> 0x08);
  12847. $outstr .= chr($w1 & 0xFF);
  12848. $outstr .= chr($w2 >> 0x08);
  12849. $outstr .= chr($w2 & 0xFF);
  12850. }
  12851. }
  12852. return $outstr;
  12853. }
  12854. // ====================================================
  12855. /**
  12856. * Set header font.
  12857. * @param $font (array) font
  12858. * @public
  12859. * @since 1.1
  12860. */
  12861. public function setHeaderFont($font) {
  12862. $this->header_font = $font;
  12863. }
  12864. /**
  12865. * Get header font.
  12866. * @return array()
  12867. * @public
  12868. * @since 4.0.012 (2008-07-24)
  12869. */
  12870. public function getHeaderFont() {
  12871. return $this->header_font;
  12872. }
  12873. /**
  12874. * Set footer font.
  12875. * @param $font (array) font
  12876. * @public
  12877. * @since 1.1
  12878. */
  12879. public function setFooterFont($font) {
  12880. $this->footer_font = $font;
  12881. }
  12882. /**
  12883. * Get Footer font.
  12884. * @return array()
  12885. * @public
  12886. * @since 4.0.012 (2008-07-24)
  12887. */
  12888. public function getFooterFont() {
  12889. return $this->footer_font;
  12890. }
  12891. /**
  12892. * Set language array.
  12893. * @param $language (array)
  12894. * @public
  12895. * @since 1.1
  12896. */
  12897. public function setLanguageArray($language) {
  12898. $this->l = $language;
  12899. if (isset($this->l['a_meta_dir'])) {
  12900. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  12901. } else {
  12902. $this->rtl = false;
  12903. }
  12904. }
  12905. /**
  12906. * Returns the PDF data.
  12907. * @public
  12908. */
  12909. public function getPDFData() {
  12910. if ($this->state < 3) {
  12911. $this->Close();
  12912. }
  12913. return $this->buffer;
  12914. }
  12915. /**
  12916. * Output anchor link.
  12917. * @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;)
  12918. * @param $name (string) link name
  12919. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  12920. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  12921. * @param $color (array) array of RGB text color
  12922. * @param $style (string) font style (U, D, B, I)
  12923. * @param $firstblock (boolean) if true the string is the starting of a line.
  12924. * @return the number of cells used or the remaining text if $firstline = true;
  12925. * @public
  12926. */
  12927. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  12928. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  12929. // convert url to internal link
  12930. $lnkdata = explode(',', $url);
  12931. if (isset($lnkdata[0])) {
  12932. $page = intval(substr($lnkdata[0], 1));
  12933. if (empty($page) OR ($page <= 0)) {
  12934. $page = $this->page;
  12935. }
  12936. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  12937. $lnky = floatval($lnkdata[1]);
  12938. } else {
  12939. $lnky = 0;
  12940. }
  12941. $url = $this->AddLink();
  12942. $this->SetLink($url, $lnky, $page);
  12943. }
  12944. }
  12945. // store current settings
  12946. $prevcolor = $this->fgcolor;
  12947. $prevstyle = $this->FontStyle;
  12948. if (empty($color)) {
  12949. $this->SetTextColorArray($this->htmlLinkColorArray);
  12950. } else {
  12951. $this->SetTextColorArray($color);
  12952. }
  12953. if ($style == -1) {
  12954. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  12955. } else {
  12956. $this->SetFont('', $this->FontStyle.$style);
  12957. }
  12958. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  12959. // restore settings
  12960. $this->SetFont('', $prevstyle);
  12961. $this->SetTextColorArray($prevcolor);
  12962. return $ret;
  12963. }
  12964. /**
  12965. * 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.
  12966. * @param $hcolor (string) HTML color.
  12967. * @param $defcol (array) Color to return in case of error.
  12968. * @return array RGB or CMYK color, or false in case of error.
  12969. * @public
  12970. */
  12971. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  12972. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  12973. $color = strtolower($color);
  12974. // check for javascript color array syntax
  12975. if (strpos($color, '[') !== false) {
  12976. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  12977. $returncolor = array();
  12978. switch ($m[1]) {
  12979. case 'cmyk': {
  12980. // RGB
  12981. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  12982. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  12983. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  12984. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  12985. break;
  12986. }
  12987. case 'rgb': {
  12988. // RGB
  12989. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  12990. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  12991. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  12992. break;
  12993. }
  12994. case 'g': {
  12995. // grayscale
  12996. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  12997. break;
  12998. }
  12999. case 't':
  13000. default: {
  13001. // transparent (empty array)
  13002. break;
  13003. }
  13004. }
  13005. return $returncolor;
  13006. }
  13007. } elseif (($dotpos = strpos($color, '.')) !== false) {
  13008. // remove class parent (i.e.: color.red)
  13009. $color = substr($color, ($dotpos + 1));
  13010. if ($color == 'transparent') {
  13011. // transparent (empty array)
  13012. return array();
  13013. }
  13014. }
  13015. if (strlen($color) == 0) {
  13016. return $defcol;
  13017. }
  13018. // RGB ARRAY
  13019. if (substr($color, 0, 3) == 'rgb') {
  13020. $codes = substr($color, 4);
  13021. $codes = str_replace(')', '', $codes);
  13022. $returncolor = explode(',', $codes);
  13023. foreach ($returncolor as $key => $val) {
  13024. if (strpos($val, '%') > 0) {
  13025. // percentage
  13026. $returncolor[$key] = (255 * intval($val) / 100);
  13027. } else {
  13028. $returncolor[$key] = intval($val);
  13029. }
  13030. // normalize value
  13031. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  13032. }
  13033. return $returncolor;
  13034. }
  13035. // CMYK ARRAY
  13036. if (substr($color, 0, 4) == 'cmyk') {
  13037. $codes = substr($color, 5);
  13038. $codes = str_replace(')', '', $codes);
  13039. $returncolor = explode(',', $codes);
  13040. foreach ($returncolor as $key => $val) {
  13041. if (strpos($val, '%') !== false) {
  13042. // percentage
  13043. $returncolor[$key] = (100 * intval($val) / 100);
  13044. } else {
  13045. $returncolor[$key] = intval($val);
  13046. }
  13047. // normalize value
  13048. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  13049. }
  13050. return $returncolor;
  13051. }
  13052. if ($color{0} != '#') {
  13053. // COLOR NAME
  13054. if (isset($this->webcolor[$color])) {
  13055. // web color
  13056. $color_code = $this->webcolor[$color];
  13057. } else {
  13058. // spot color
  13059. $returncolor = $this->getSpotColor($color);
  13060. if ($returncolor === false) {
  13061. $returncolor = $defcol;
  13062. }
  13063. return $returncolor;
  13064. }
  13065. } else {
  13066. $color_code = substr($color, 1);
  13067. }
  13068. // HEXADECIMAL REPRESENTATION
  13069. switch (strlen($color_code)) {
  13070. case 3: {
  13071. // 3-digit RGB hexadecimal representation
  13072. $r = substr($color_code, 0, 1);
  13073. $g = substr($color_code, 1, 1);
  13074. $b = substr($color_code, 2, 1);
  13075. $returncolor = array();
  13076. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  13077. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  13078. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  13079. break;
  13080. }
  13081. case 6: {
  13082. // 6-digit RGB hexadecimal representation
  13083. $returncolor = array();
  13084. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  13085. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  13086. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  13087. break;
  13088. }
  13089. case 8: {
  13090. // 8-digit CMYK hexadecimal representation
  13091. $returncolor = array();
  13092. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  13093. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  13094. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  13095. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  13096. break;
  13097. }
  13098. default: {
  13099. $returncolor = $defcol;
  13100. break;
  13101. }
  13102. }
  13103. return $returncolor;
  13104. }
  13105. /**
  13106. * Converts pixels to User's Units.
  13107. * @param $px (int) pixels
  13108. * @return float value in user's unit
  13109. * @public
  13110. * @see setImageScale(), getImageScale()
  13111. */
  13112. public function pixelsToUnits($px) {
  13113. return ($px / ($this->imgscale * $this->k));
  13114. }
  13115. /**
  13116. * Reverse function for htmlentities.
  13117. * Convert entities in UTF-8.
  13118. * @param $text_to_convert (string) Text to convert.
  13119. * @return string converted text string
  13120. * @public
  13121. */
  13122. public function unhtmlentities($text_to_convert) {
  13123. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  13124. }
  13125. // ENCRYPTION METHODS ----------------------------------
  13126. /**
  13127. * Returns a string containing random data to be used as a seed for encryption methods.
  13128. * @param $seed (string) starting seed value
  13129. * @return string containing random data
  13130. * @author Nicola Asuni
  13131. * @since 5.9.006 (2010-10-19)
  13132. * @protected
  13133. */
  13134. protected function getRandomSeed($seed='') {
  13135. $seed .= microtime();
  13136. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  13137. // this is not used on windows systems because it is very slow for a know bug
  13138. $seed .= openssl_random_pseudo_bytes(512);
  13139. } else {
  13140. for ($i = 0; $i < 23; ++$i) {
  13141. $seed .= uniqid('', true);
  13142. }
  13143. }
  13144. $seed .= uniqid('', true);
  13145. $seed .= rand();
  13146. $seed .= getmypid();
  13147. $seed .= __FILE__;
  13148. $seed .= $this->bufferlen;
  13149. if (isset($_SERVER['REMOTE_ADDR'])) {
  13150. $seed .= $_SERVER['REMOTE_ADDR'];
  13151. }
  13152. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  13153. $seed .= $_SERVER['HTTP_USER_AGENT'];
  13154. }
  13155. if (isset($_SERVER['HTTP_ACCEPT'])) {
  13156. $seed .= $_SERVER['HTTP_ACCEPT'];
  13157. }
  13158. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  13159. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  13160. }
  13161. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  13162. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  13163. }
  13164. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  13165. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  13166. }
  13167. $seed .= rand();
  13168. $seed .= uniqid('', true);
  13169. $seed .= microtime();
  13170. return $seed;
  13171. }
  13172. /**
  13173. * Compute encryption key depending on object number where the encrypted data is stored.
  13174. * This is used for all strings and streams without crypt filter specifier.
  13175. * @param $n (int) object number
  13176. * @return int object key
  13177. * @protected
  13178. * @author Nicola Asuni
  13179. * @since 2.0.000 (2008-01-02)
  13180. */
  13181. protected function _objectkey($n) {
  13182. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  13183. if ($this->encryptdata['mode'] == 2) { // AES-128
  13184. // AES padding
  13185. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  13186. }
  13187. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  13188. $objkey = substr($objkey, 0, 16);
  13189. return $objkey;
  13190. }
  13191. /**
  13192. * Encrypt the input string.
  13193. * @param $n (int) object number
  13194. * @param $s (string) data string to encrypt
  13195. * @return encrypted string
  13196. * @protected
  13197. * @author Nicola Asuni
  13198. * @since 5.0.005 (2010-05-11)
  13199. */
  13200. protected function _encrypt_data($n, $s) {
  13201. if (!$this->encrypted) {
  13202. return $s;
  13203. }
  13204. switch ($this->encryptdata['mode']) {
  13205. case 0: // RC4-40
  13206. case 1: { // RC4-128
  13207. $s = $this->_RC4($this->_objectkey($n), $s);
  13208. break;
  13209. }
  13210. case 2: { // AES-128
  13211. $s = $this->_AES($this->_objectkey($n), $s);
  13212. break;
  13213. }
  13214. case 3: { // AES-256
  13215. $s = $this->_AES($this->encryptdata['key'], $s);
  13216. break;
  13217. }
  13218. }
  13219. return $s;
  13220. }
  13221. /**
  13222. * Put encryption on PDF document.
  13223. * @protected
  13224. * @author Nicola Asuni
  13225. * @since 2.0.000 (2008-01-02)
  13226. */
  13227. protected function _putencryption() {
  13228. if (!$this->encrypted) {
  13229. return;
  13230. }
  13231. $this->encryptdata['objid'] = $this->_newobj();
  13232. $out = '<<';
  13233. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  13234. $this->encryptdata['Filter'] = 'Standard';
  13235. }
  13236. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  13237. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  13238. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  13239. }
  13240. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  13241. $this->encryptdata['V'] = 1;
  13242. }
  13243. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  13244. $out .= ' /V '.$this->encryptdata['V'];
  13245. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  13246. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  13247. $out .= ' /Length '.$this->encryptdata['Length'];
  13248. } else {
  13249. $out .= ' /Length 40';
  13250. }
  13251. if ($this->encryptdata['V'] >= 4) {
  13252. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  13253. $this->encryptdata['StmF'] = 'Identity';
  13254. }
  13255. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  13256. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13257. $this->encryptdata['StrF'] = 'Identity';
  13258. }
  13259. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  13260. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  13261. $out .= ' /CF <<';
  13262. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  13263. $out .= ' /Type /CryptFilter';
  13264. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  13265. // The method used
  13266. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  13267. if ($this->encryptdata['pubkey']) {
  13268. $out .= ' /Recipients [';
  13269. foreach ($this->encryptdata['Recipients'] as $rec) {
  13270. $out .= ' <'.$rec.'>';
  13271. }
  13272. $out .= ' ]';
  13273. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  13274. $out .= ' /EncryptMetadata false';
  13275. } else {
  13276. $out .= ' /EncryptMetadata true';
  13277. }
  13278. }
  13279. } else {
  13280. $out .= ' /CFM /None';
  13281. }
  13282. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  13283. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  13284. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  13285. } else {
  13286. $out .= ' /AuthEvent /DocOpen';
  13287. }
  13288. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  13289. // The bit length of the encryption key.
  13290. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  13291. }
  13292. $out .= ' >> >>';
  13293. }
  13294. // The name of the crypt filter that shall be used by default when decrypting streams.
  13295. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  13296. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13297. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  13298. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  13299. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  13300. $out .= ' /EFF /'.$this->encryptdata[''];
  13301. }
  13302. }
  13303. // Additional encryption dictionary entries for the standard security handler
  13304. if ($this->encryptdata['pubkey']) {
  13305. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  13306. $out .= ' /Recipients [';
  13307. foreach ($this->encryptdata['Recipients'] as $rec) {
  13308. $out .= ' <'.$rec.'>';
  13309. }
  13310. $out .= ' ]';
  13311. }
  13312. } else {
  13313. $out .= ' /R';
  13314. if ($this->encryptdata['V'] == 5) { // AES-256
  13315. $out .= ' 5';
  13316. $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
  13317. $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
  13318. $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
  13319. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  13320. $out .= ' 4';
  13321. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  13322. $out .= ' 2';
  13323. } else { // RC-128
  13324. $out .= ' 3';
  13325. }
  13326. $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
  13327. $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
  13328. $out .= ' /P '.$this->encryptdata['P'];
  13329. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  13330. $out .= ' /EncryptMetadata false';
  13331. } else {
  13332. $out .= ' /EncryptMetadata true';
  13333. }
  13334. }
  13335. $out .= ' >>';
  13336. $out .= "\n".'endobj';
  13337. $this->_out($out);
  13338. }
  13339. /**
  13340. * Returns the input text encrypted using RC4 algorithm and the specified key.
  13341. * RC4 is the standard encryption algorithm used in PDF format
  13342. * @param $key (string) encryption key
  13343. * @param $text (String) input text to be encrypted
  13344. * @return String encrypted text
  13345. * @protected
  13346. * @since 2.0.000 (2008-01-02)
  13347. * @author Klemen Vodopivec, Nicola Asuni
  13348. */
  13349. protected function _RC4($key, $text) {
  13350. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  13351. // try to use mcrypt function if exist
  13352. return $out;
  13353. }
  13354. if ($this->last_enc_key != $key) {
  13355. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  13356. $rc4 = range(0, 255);
  13357. $j = 0;
  13358. for ($i = 0; $i < 256; ++$i) {
  13359. $t = $rc4[$i];
  13360. $j = ($j + $t + ord($k[$i])) % 256;
  13361. $rc4[$i] = $rc4[$j];
  13362. $rc4[$j] = $t;
  13363. }
  13364. $this->last_enc_key = $key;
  13365. $this->last_enc_key_c = $rc4;
  13366. } else {
  13367. $rc4 = $this->last_enc_key_c;
  13368. }
  13369. $len = strlen($text);
  13370. $a = 0;
  13371. $b = 0;
  13372. $out = '';
  13373. for ($i = 0; $i < $len; ++$i) {
  13374. $a = ($a + 1) % 256;
  13375. $t = $rc4[$a];
  13376. $b = ($b + $t) % 256;
  13377. $rc4[$a] = $rc4[$b];
  13378. $rc4[$b] = $t;
  13379. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  13380. $out .= chr(ord($text[$i]) ^ $k);
  13381. }
  13382. return $out;
  13383. }
  13384. /**
  13385. * Returns the input text exrypted using AES algorithm and the specified key.
  13386. * This method requires mcrypt.
  13387. * @param $key (string) encryption key
  13388. * @param $text (String) input text to be encrypted
  13389. * @return String encrypted text
  13390. * @protected
  13391. * @author Nicola Asuni
  13392. * @since 5.0.005 (2010-05-11)
  13393. */
  13394. protected function _AES($key, $text) {
  13395. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  13396. $padding = 16 - (strlen($text) % 16);
  13397. $text .= str_repeat(chr($padding), $padding);
  13398. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  13399. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  13400. $text = $iv.$text;
  13401. return $text;
  13402. }
  13403. /**
  13404. * Encrypts a string using MD5 and returns it's value as a binary string.
  13405. * @param $str (string) input string
  13406. * @return String MD5 encrypted binary string
  13407. * @protected
  13408. * @since 2.0.000 (2008-01-02)
  13409. * @author Klemen Vodopivec
  13410. */
  13411. protected function _md5_16($str) {
  13412. return pack('H*', md5($str));
  13413. }
  13414. /**
  13415. * Compute U value (used for encryption)
  13416. * @return string U value
  13417. * @protected
  13418. * @since 2.0.000 (2008-01-02)
  13419. * @author Nicola Asuni
  13420. */
  13421. protected function _Uvalue() {
  13422. if ($this->encryptdata['mode'] == 0) { // RC4-40
  13423. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  13424. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  13425. $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
  13426. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  13427. $len = strlen($tmp);
  13428. for ($i = 1; $i <= 19; ++$i) {
  13429. $ek = '';
  13430. for ($j = 0; $j < $len; ++$j) {
  13431. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  13432. }
  13433. $enc = $this->_RC4($ek, $enc);
  13434. }
  13435. $enc .= str_repeat("\x00", 16);
  13436. return substr($enc, 0, 32);
  13437. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13438. $seed = $this->_md5_16($this->getRandomSeed());
  13439. // User Validation Salt
  13440. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  13441. // User Key Salt
  13442. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  13443. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  13444. }
  13445. }
  13446. /**
  13447. * Compute UE value (used for encryption)
  13448. * @return string UE value
  13449. * @protected
  13450. * @since 5.9.006 (2010-10-19)
  13451. * @author Nicola Asuni
  13452. */
  13453. protected function _UEvalue() {
  13454. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  13455. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13456. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13457. }
  13458. /**
  13459. * Compute O value (used for encryption)
  13460. * @return string O value
  13461. * @protected
  13462. * @since 2.0.000 (2008-01-02)
  13463. * @author Nicola Asuni
  13464. */
  13465. protected function _Ovalue() {
  13466. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  13467. $tmp = $this->_md5_16($this->encryptdata['own